-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshadow_math.f90
3360 lines (3129 loc) · 136 KB
/
shadow_math.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!----
!----
!---- MODULE: shadow_math
!----
!---- Contains the mathematical routines in Shadow
!----
!----
!---- todo: Much of the functionality of these routines is now in
!---- fortran 95. It should be desirable to use then the intrinsic
!---- f95 routines, but this is left for the future.
!----
!---- Example of use: see test_math
!
!
Module shadow_math
use shadow_globaldefinitions !, only : ski, skr, skc, sklen
implicit none
!! uncomment this for PENELOPE random mumber generator
!! integer(kind=4) :: iseed1=2347,iseed2=2377
!---- Everything is private unless explicitly made public ----!
private
!---- public routines :
!---- used in the geometrical source: wran, rotate, spl_int, atan_2, gauss
!---- the vectorial calculus tools: scalar, dot, cross, norm, vector,
!---- versor, proj, vsum, vdist
public :: wran, init_random_seed, mysqrt
public :: rotate, spl_int, lin_int, atan_2, gauss, binormal
public :: scalar, dot, cross, norm, vector, versor, proj, vsum, vdist
public :: gnormal, rotvector, mfp, cross_m_flag
public :: qsf,cubspl
! used in shadow_preprocessors: ibcccu, ibcdcu
public :: ibcccu, ibcdcu
private :: gcf,gser
private :: erfc,gammp,gammq,gammln
!----
!---- Some mathematical routines from IMSL library.
!----
!---- By now, it has only the routines used in the geometrical source:
!---- MDNRIS
!----
!---- Note 1) the routine MERFI called by mdnris are set as private
!---- as it is not used directly in Shadow.
!----
!---- Note 2) the error message routines in imsl have
!---- been removed due to incompatibilities with f95
!---- They are: UERSET, UERTST, UGETIO, USPKD.
!----
!---- Example of use: see test_math_imsl
!
!
public :: mdnris, zrpoly, dbcevl, pnpoly
private :: merfi
private :: zrpqlb, zrpqlc, zrpqld, zrpqle, zrpqlf, zrpqlg, zrpqlh, zrpqli
Contains
!!C +++
!!C REAL FUNCTION WRAN (ISEED)
!!C
!!C This function is simply a wrapper around the "real" (intrinsic) random
!!C number generator, RAN, supplied by all FTN compilers. It servers two
!!C purposes: filter out "bad" random numbers (== 0 in SHADOW) and allow
!!C SHADOW to read its random numbers out of a text file, so we can get
!!C the results across platforms.
!!C
!!C 1/23/92 added the option to use the RAND function (IBM RS600
!!C specfically) in the absence of a RAN function. Also, when the 6000
!!C compiler becomes able to handle the CALL EXIT, the STOP should be
!!C removed. (*update*).
!!C
!!C ISEED: the seed to give RAN.
!!C
!!C ---
REAL(KIND=SKR) FUNCTION WRAN (ISEED)
implicit none
INTEGER(KIND=SKI) :: ISEED,K
INTEGER(KIND=SKI),dimension(1) :: iseed2
INTEGER(KIND=SKI) :: first=1,wran_counter=0
real(kind=skr) :: XX
!first=1
!!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!! Uncomment this part is for the built-in f95 random generator
!!
!!
!write(*,*) " "
!write(*,*) "WRAN: first: ",first
!write(*,*) "WRAN: wran_counter: ",wran_counter
if (first.eq.1) then
first = 0
CALL init_random_seed(iseed)
end if
CALL RANDOM_NUMBER(WRAN)
!!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!! Uncomment for Penelope random number generator
!!
!!
!! wran = RAND_PENELOPE(1.0D0)
!!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!!
!! This part is for calling the C generator using the wranc.c wrapper
!! This part produces the same results than Shadow 2.x
!!
!! todo: MUST BE COMMENTED IN WINDOWS
!! WRAN = WRANC(ISEED)
!!
wran_counter=wran_counter+1
RETURN
END FUNCTION WRAN
SUBROUTINE init_random_seed(iseed)
INTEGER(kind=ski),intent(in) :: iseed
call init_random_seed_standard(iseed)
! TODO: to be written initializer for penelope random generator
! call init_random_seed_penelope(iseed)
END SUBROUTINE
SUBROUTINE init_random_seed_standard(iseed)
INTEGER(kind=ski),intent(in) :: iseed
INTEGER(kind=ski) :: i, clock
!watch out the kind here !
INTEGER :: n
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
!print *,"n=",n
ALLOCATE(seed(n))
!CALL RANDOM_SEED(GET = oldseed)
!print *,"INIT: old seed=",seed
IF (iseed.eq.0) then
CALL SYSTEM_CLOCK(COUNT=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
print *,"INIT_RANDOM_SEED: random seed initialised using system clock"
ELSE
seed = iseed
END IF
!print *,"INIT: seed=",seed
CALL RANDOM_SEED(PUT = seed)
DEALLOCATE(seed)
END SUBROUTINE
!
! this is an implementation of the complex square root.
!
! ideally, the Fortran compiler implements it, but it gives me
! "invalid memory reference" whan calling sqrt(FH*FH_BAR) in
! subroutine crystal in windows with gfortran 4.8.0 20130302
!
! the implementation follows the formula posted by Didier Piau in:
! http://math.stackexchange.com/questions/44406/how-do-i-get-the-square-root-of-a-complex-number
complex(kind=skx) function mysqrt(z)
COMPLEX(KIND=skx) :: z,zout
real(kind=skr) :: r,zr,zi
r = abs(z)
if (abs(z+r) .eq. 0) then
print*,'mysqrt: warning calculation using intrinsic function'
mysqrt = cdsqrt(z)
else
mysqrt = sqrt(r) * (z+r)/abs(z+r)
endif
end function mysqrt
! C+++
! C SUBROUTINE ROTATE
! C
! C PURPOSE: GENERATE THE EULERIAN ANGLES FROM THE ELECTRON COORDINATES TO
! C THE SHADOW REFERENCE FRAME.
! C
! C----
SUBROUTINE ROTATE (VIN,PSI,THETA,PHI,VOUT)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
implicit none
!DIMENSION VIN(3), VOUT(3)
real(kind=skr),dimension(3) :: vin,vout
real(kind=skr) :: psi,theta,phi
real(kind=skr) :: cospsi,costhe,cosphi,sinpsi,sinthe,sinphi
!C
!C To avoid useles recalculations
!C
COSPSI = COS (PSI)
COSTHE = COS (THETA)
COSPHI = COS (PHI)
SINPSI = SIN (PSI)
SINTHE = SIN (THETA)
SINPHI = SIN (PHI)
VOUT(1)= (COSPSI*COSPHI-COSTHE*SINPHI*SINPSI)*VIN(1)+&
(-SINPSI*COSPHI-COSTHE*SINPHI*COSPSI)*VIN(2)+&
(SINTHE*SINPHI)*VIN(3)
VOUT(2)= (COSPSI*SINPHI+COSTHE*COSPHI*SINPSI)*VIN(1)+&
(-SINPSI*SINPHI+COSTHE*COSPHI*COSPSI)*VIN(2)+&
(-SINTHE*COSPHI)*VIN(3)
VOUT(3)= (SINTHE*SINPSI)*VIN(1)+(SINTHE*COSPSI)*VIN(2) &
+COSTHE*VIN(3)
END SUBROUTINE ROTATE
! C+++
! C SUBROUTINE SPL_INT
! C
! C PURPOSE TO INTERPOLATE THE VALUE Y FOR A POINT X, USING THE ARRAY
! C G(5,N) FROM THE PROGRAM CUBSPL.
! C
! C INPUT G(5,N) AS FROM CUBSPL.
! C N, THE # OF DATA POINTS IN G(5,N).
! C X, THE POINT WHERE YOU WANT THE INTERPOLATION TO BE MADE.
! C
! C OUTPUT Y, THE INTERPOLATED VALUE.
! C IER =1 FOR ERROR, =0 OTHERWISE.
! C
! C---
SUBROUTINE SPL_INT(G,N,X,Y,IER)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
REAL(KIND=SKR),dimension(5,N),intent(in) :: G
REAL(KIND=SKR), intent(in) :: X
INTEGER(KIND=SKI), intent(in) :: N
REAL(KIND=SKR), intent(out) :: Y
REAL(KIND=SKR) :: Z
!REAL(KIND=SKR) G(5,N), X, Y, Z
INTEGER(KIND=SKI) :: I, IER
real(kind=skr) :: gmin,gmax
GMAX = MAX(G(1,1),G(1,N))
GMIN = MIN(G(1,1),G(1,N))
IF ((X .LT. GMIN) .OR. (X .GT. GMAX)) THEN
! please note that an error here for BM or WIGGLER may be due
! to the use of an not-updated SRSPEC SRANG and SRDISTR
WRITE(6,*) 'SPL_INT: x is outside the interpolation range.'
WRITE(6,*) 'X, GMIN, GMAX: ',X,GMIN,GMAX
IER = 1
RETURN
ELSE IF (X .EQ. G(1,N)) THEN
I = N-1
GO TO 10
END IF
I = 0
21 IF (G(1,I+1) .LE. X) THEN
I = I + 1
GOTO 21
END IF
10 Z = X - G(1,I)
Y = G(2,I) + Z*(G(3,I) + Z*(G(4,I) + Z*G(5,I)))
IER = 0
RETURN
END SUBROUTINE SPL_INT
! C+++
! C SUBROUTINE LIN_INT
! C
! C PURPOSE TO INTERPOLATE LINEARLY THE VALUE Y FOR A POINT X,
! C USING ONLY THE ELEMENTS G(1,N) and G(2,N) OF THE ARRAY
! C G(5,N) FROM THE PROGRAM CUBSPL.
! C
! C INPUT G(5,N) AS FROM CUBSPL.
! C N, THE # OF DATA POINTS IN G(5,N).
! C X, THE POINT WHERE YOU WANT THE INTERPOLATION TO BE MADE.
! C
! C OUTPUT Y, THE INTERPOLATED VALUE.
! C IER =1 FOR ERROR, =0 OTHERWISE.
! C
! C---
SUBROUTINE LIN_INT(G,N,X,Y,IER)
REAL(KIND=SKR),dimension(5,N),intent(in) :: G
REAL(KIND=SKR), intent(in) :: X
INTEGER(KIND=SKI), intent(in) :: N
REAL(KIND=SKR), intent(out) :: Y
REAL(KIND=SKR) :: Z
INTEGER(KIND=SKI) :: I, IER
real(kind=skr) :: gmin,gmax
GMAX = MAX(G(1,1),G(1,N))
GMIN = MIN(G(1,1),G(1,N))
IF ((X .LT. GMIN) .OR. (X .GT. GMAX)) THEN
! please note that an error here for BM or WIGGLER may be due
! to the use of an not-updated SRSPEC SRANG and SRDISTR
WRITE(6,*) 'LIN_INT: x is outside the interpolation range.'
WRITE(6,*) 'X, GMIN, GMAX: ',X,GMIN,GMAX
IER = 1
RETURN
ELSE IF (X .EQ. G(1,N)) THEN
I = N-1
GO TO 10
END IF
I = 0
21 IF (G(1,I+1) .LE. X) THEN
I = I + 1
GOTO 21
END IF
10 Z = X - G(1,I)
IF (I .EQ. N) THEN
WRITE(6,*) 'LIN_INT: End point. Set previous one'
I = I-1
END IF
!cubic spline Y = G(2,I) + Z*(G(3,I) + Z*(G(4,I) + Z*G(5,I)))
!liner interpolation
Y = G(2,I) + Z* (G(2,I+1) -G(2,I))/(G(1,I+1)-G(1,I))
IER = 0
RETURN
END SUBROUTINE LIN_INT
!C ++++
!C
!C scalar multiplication
!C ----
SUBROUTINE SCALAR( V1,ARG,V2)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1
real(kind=skr), intent(in) :: arg
real(kind=skr),dimension(3),intent(out) :: v2
!DIMENSION V1(3),V2(3)
V2(1) = V1(1)*ARG
V2(2) = V1(2)*ARG
V2(3) = V1(3)*ARG
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(V2(1)).LT.1.0E-31) V2(1) = 0.0D0
IF (ABS(V2(2)).LT.1.0E-31) V2(2) = 0.0D0
IF (ABS(V2(3)).LT.1.0E-31) V2(3) = 0.0D0
END SUBROUTINE SCALAR
! C ++++
! C
! C scalar product
! C ----
SUBROUTINE DOT (V1,V2,RES)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION V1(3),V2(3)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1,v2
real(kind=skr), intent(out) :: res
RES = V1(1)*V2(1) + V1(2)*V2(2) + V1(3)*V2(3)
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(RES).LT.1.0E-31) RES = 0.0D0
RETURN
END SUBROUTINE DOT
! C ++++
! C
! C vector product : vres = v1 x v2
! C ----
SUBROUTINE CROSS (V1,V2,VRES)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1,v2
real(kind=skr),dimension(3),intent(out) :: vres
integer(KIND=SKI) :: m_flag
character(len=sklen) :: m_warning
real(kind=skr) :: ttest
!DIMENSION V1(3),V2(3),VRES (3)
M_FLAG = 0
VRES(1) = V1(2)*V2(3) - V1(3)*V2(2)
VRES(2) = - ( V1(1)*V2(3) - V1(3)*V2(1) )
VRES(3) = V1(1)*V2(2) - V1(2)*V2(1)
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(VRES(1)).LT.1.0E-31) VRES(1) = 0.0D0
IF (ABS(VRES(2)).LT.1.0E-31) VRES(2) = 0.0D0
IF (ABS(VRES(3)).LT.1.0E-31) VRES(3) = 0.0D0
TTEST = VRES(1)*VRES(1) + VRES(2)*VRES(2) + VRES(3)*VRES(3)
IF (TTEST.LT.1.0E-31) THEN
M_FLAG = 1
M_WARNING = 'Error in CROSS: product is zero.'
END IF
END SUBROUTINE CROSS
! C ++++
! C
! C vector product returning M_FLAG: vres = v1 x v2
! C ----
!TODO: This is the same as CROSS() but also returning M_FLAG, to avoid
! defining it as a global variable. To be fixed...
SUBROUTINE CROSS_M_FLAG (V1,V2,VRES,M_FLAG)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1,v2
real(kind=skr),dimension(3),intent(out) :: vres
integer(KIND=SKI) :: m_flag
character(len=sklen) :: m_warning
real(kind=skr) :: ttest
M_FLAG = 0
VRES(1) = V1(2)*V2(3) - V1(3)*V2(2)
VRES(2) = - ( V1(1)*V2(3) - V1(3)*V2(1) )
VRES(3) = V1(1)*V2(2) - V1(2)*V2(1)
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(VRES(1)).LT.1.0E-31) VRES(1) = 0.0D0
IF (ABS(VRES(2)).LT.1.0E-31) VRES(2) = 0.0D0
IF (ABS(VRES(3)).LT.1.0E-31) VRES(3) = 0.0D0
TTEST = VRES(1)*VRES(1) + VRES(2)*VRES(2) + VRES(3)*VRES(3)
IF (TTEST.LT.1.0E-31) THEN
M_FLAG = 1
M_WARNING = 'Error in CROSS: product is zero.'
END IF
END SUBROUTINE CROSS_M_FLAG
! C ++++
! C
! C vector normalization
! C ----
SUBROUTINE NORM (V1,V2)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION V1(3),V2(3)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1
real(kind=skr),dimension(3),intent(out) :: v2
real(kind=skr) :: rnorm
RNORM = V1(1)**2 + V1(2)**2 + V1(3)**2
RNORM = SQRT(RNORM)
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(RNORM).LT.1.0E-31) RNORM = 0.0D0
IF (RNORM.NE.0.0D0) THEN
RNORM = 1/RNORM
V2(1) = V1(1)*RNORM
V2(2) = V1(2)*RNORM
V2(3) = V1(3)*RNORM
END IF
RETURN
END SUBROUTINE NORM
! C ++++
! C
! C generate a vector vres = p2 - p1 ( P1 -> P2 )
! C ----
SUBROUTINE VECTOR (P1,P2,VRES)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION P1(3),P2(3),VRES(3)
implicit none
real(kind=skr),dimension(3),intent(in) :: p1,p2
real(kind=skr),dimension(3),intent(out) :: vres
integer(KIND=SKI) :: i
DO I=1,3
VRES(I) = P2(I) - P1(I)
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(VRES(I)).LT.1.0E-31) VRES(I) = 0.0D0
END DO
END SUBROUTINE VECTOR
! C ++++
! C
! C generate a versor
! C
! C ----
SUBROUTINE VERSOR (P1,P2,VRES)
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION P1(3),P2(3),VRES(3)
implicit none
real(kind=skr),dimension(3),intent(in) :: p1,p2
real(kind=skr),dimension(3),intent(out) :: vres
integer(KIND=SKI) :: I
real(kind=skr) :: rnorm
!C
!C **** CHECK FOR RNORM.EQ.0 SOMEBODY ******
!C
RNORM = .0D0
DO I=1,3
RNORM = RNORM + ( P1(I) - P2(I) )*( P1(I) - P2(I) )
END DO
RNORM = SQRT(RNORM)
DO I=1,3
VRES(I) = (P2(I)-P1(I))/RNORM
END DO
END SUBROUTINE VERSOR
! C ++++
! C
! C project v1 onto v2
! C ----
SUBROUTINE PROJ (V1,V2,VRES)
implicit none
real(kind=skr),dimension(3),intent(in) :: v1,v2
real(kind=skr),dimension(3),intent(out) :: vres
real(kind=skr) :: rnorm,scalar1,prod
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION V1(3),V2(3),VRES(3)
RNORM = V2(1)**2 + V2(2)**2 + V2(3)**2
!C
!C If the numbers are *very* small, zero them out. This is not done
!C for VMS, since it seems to work out fine. Why mess with something
!C that already works.
!C
IF (ABS(RNORM).LT.1.0E-31) RNORM = 0.0D0
IF (RNORM.EQ.0.0D0) THEN
VRES(1) = 0.0D0
VRES(2) = 0.0D0
VRES(3) = 0.0D0
ELSE
! srio renames SCALAR to scalar1
scalar1 = V1(1)*V2(1) + V1(2)*V2(2) + V1(3)*V2(3)
PROD = scalar1/RNORM
VRES(1) = V2(1)*PROD
VRES(2) = V2(2)*PROD
VRES(3) = V2(3)*PROD
END IF
END SUBROUTINE PROJ
! C ++++
! C
! C Generates the sum of two vectors
! C ----
SUBROUTINE vSUM (P1,P2,RES)
implicit none
real(kind=skr),dimension(3),intent(in) :: p1,p2
real(kind=skr),dimension(3),intent(out) :: res
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!DIMENSION P1(3),P2(3),RES(3)
RES(1) = P1(1) + P2(1)
RES(2) = P1(2) + P2(2)
RES(3) = P1(3) + P2(3)
END SUBROUTINE vSUM
! C ++++
! C
! C Vector from a line to a point; line is specified by
! C H0 (starting), VH (direction); point is P0, output is DIS
! C -----
SUBROUTINE VDIST (P0, H0, VH, DIS)
implicit none
real(kind=skr),dimension(3),intent(in) :: p0, h0, vh
real(kind=skr),dimension(3),intent(out) :: dis
real(kind=skr),dimension(3) :: vt, t_vh
!IMPLICIT REAL(KIND=SKR) (A-H, O-Z)
!DIMENSION P0(3), H0(3), VH(3), DIS(3)
!DIMENSION VT(3), T_VH(3)
CALL VECTOR(H0, P0, VT)
CALL PROJ(VT, VH, T_VH)
CALL VECTOR(T_VH, VT, DIS)
RETURN
END SUBROUTINE VDIST
! C ++++
! C
! C This subroutine returns the value of the arctangent between 0-2*PI
! C ----
SUBROUTINE ATAN_2 (SINE,COSINE,ANGLE)
implicit none
real(kind=skr),intent(in) :: sine,cosine
real(kind=skr),intent(out) :: angle
real(kind=skr) :: arg
!IMPLICIT REAL(KIND=SKR) (A-E,G-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (F,I-N)
!moved to global_definitions DATA PI /3.141592653589793238462643D0/
IF (COSINE.EQ.0.0D0.AND.SINE.EQ.0.0D0) THEN
ANGLE = 0.0D0
RETURN
ELSE
END IF
!C
!C Check if cosine is 0
!C
IF (COSINE.NE.0) THEN
ARG = SINE/COSINE
ANGLE = ATAN (ABS(ARG))
!C
!C First quadrant: sine > 0, cosine > 0
!C
IF (SINE.GT.0.0D0.AND.COSINE.GT.0.0D0) THEN
ANGLE = ANGLE
!C
!C Second quadrant: sine > 0, cosine < 0
!C
ELSE IF (SINE.GT.0.0D0.AND.COSINE.LT.0.0D0) THEN
ANGLE = PI - ANGLE
!C
!C Third quadrant: sine < 0, cosine < 0
!C
ELSE IF (SINE.LT.0.0D0.AND.COSINE.LT.0.0D0) THEN
ANGLE = ANGLE + PI
!C
!C Fourth quadrant: sine < 0, cosine > 0
!C
ELSE IF (SINE.LT.0.0D0.AND.COSINE.GT.0.0D0) THEN
ANGLE = 2*PI - ANGLE
!C
!C Divide by zero cases
!C
ELSE IF (SINE.EQ.0.0D0.AND.COSINE.GT.0.0D0) THEN
ANGLE = 0.0D0
ELSE IF (SINE.EQ.0.0D0.AND.COSINE.LT.0.0D0) THEN
ANGLE = PI
ELSE
END IF
ELSE IF (SINE.GT.0.0D0) THEN
ANGLE = PI/2
ELSE IF (SINE.LT.0.0D0) THEN
ANGLE = 1.5D0*PI
END IF
END SUBROUTINE ATAN_2
! C +++
! C SUBROUTINE GAUSS
! C
! C PURPOSE Generate a bivariate normal distribution with
! C indipendent sigmas and a correlation.
! C This will simulate a SR source of given emittance
! C specified by the two sigmas. If dist < > 0, then
! C the outputs will be correlated.
! C
! C INPUT sigma1, sigma2, distance
! C
! C OUTPUT X,X1 two binormal variate
! C
! C ALGORITHM As described by R.H.Rubinstein, in Simulation and
! C the MonteCarlo method, Wiley, NY 1981
! C
! C
! C ---
SUBROUTINE GAUSS (S,SPRIM,DD,X,XPRIM,IS)
implicit none
real(kind=skr), intent(in) :: s, sprim, dd
real(kind=skr), intent(out) :: x, xprim
integer(kind=ski), intent(out) :: is
real(kind=skr) :: c11,c22,c21,r1,r2,z1,z2
!IMPLICIT REAL(KIND=SKR) (A-H,O-Z)
!IMPLICIT INTEGER(KIND=SKI) (I-N)
!integer(KIND=SKI) :: is
! moved to shadow_globaldefinitions DATA TWOPI /6.283185307179586467925287D0/
!C
!C Initialize by computing the covariance matrix
!C
! 10 C11 = SQRT (DD**2*SPRIM**2+S**2)
C11 = SQRT (DD**2*SPRIM**2+S**2)
IF (C11.NE.0.0D0) THEN
C21 = DD*SPRIM**2/C11
C22 = S*SPRIM/C11
ELSE
C21 = 0.0D0
C22 = 0.0D0
END IF
!C
!C Entry for computation
!C Generates first the two normal variates with sigma=1 (Box & Muller)
!C
! 20 CONTINUE
R1 = WRAN(IS)
R2 = WRAN(IS)
Z1 = SQRT(-2*LOG(R1))*COS(TWOPI*R2)
Z2 = SQRT(-2*LOG(R1))*SIN(TWOPI*R2)
!C
!C generates now the new varaites
!C
X = Z1*C11
XPRIM = Z1*C21 + Z2*C22
!C
!C Completed
!C
RETURN
END SUBROUTINE GAUSS
!------------------------------------------------------------------------------------------------
! C +++
! C SUBROUTINE BINORMAL
! C
! C PURPOSE Generate a bivariate normal distribution with
! C independent sigmas and a correlation
! C
! C INPUT sigma1, sigma2, rho (correlation)
! C
! C OUTPUT X,X1 two binormal variate
! C
! C ALGORITHM compute the Cholesky decomposition
! C
! C TEST use emittance_test preprocessor
! C
! C [email protected] 20140610: written, based on gauss()
! C
! C
! C ---
SUBROUTINE BINORMAL (sigma1,sigma2,rho,x1,x2,IS)
implicit none
real(kind=skr), intent(in) :: sigma1, sigma2, rho
real(kind=skr), intent(out) :: x1, x2
integer(kind=ski), intent(out) :: is
real(kind=skr) :: r1,r2,z1,z2
real(kind=skr) :: U11,U12,U21,U22
!C
!C the covariance matrix is:
!C
! [ c11 c12 ] [ sigma1^2 rho*sigma1*sigma2 ]
! [ c21 c22 ] = [ rho*sigma1*sigma2 sigma2^2 ]
!
!
! C11 = sigma1**2
! C22 = sigma2**2
! C21 = rho*sigma1*sigma2
!
! calculate U bu Cholesky decomposition of C
!
! in Mathematica:
! cov = {{S1^2, rho S1 S2}, {rho S1 S2, S2^2}}
! Uc = FullSimplify[CholeskyDecomposition[cov]]
! {{Sqrt[S1^2], (rho S1 S2)/Sqrt[S1^2]}, {0, Sqrt[ S2 (S2 - rho Conjugate[rho] Conjugate[S2])]}}
!
U11 = sigma1
U21 = sigma2*rho
U12 = 0.0
U22 = sigma2 * sqrt( 1.0-rho**2 )
!C
!C Entry for computation
!C Generates first the two normal variates with sigma=1 (Box & Muller)
!C
R1 = WRAN(IS)
R2 = WRAN(IS)
Z1 = SQRT(-2*LOG(R1))*COS(TWOPI*R2)
Z2 = SQRT(-2*LOG(R1))*SIN(TWOPI*R2)
!C
!C generates now the new variates
!C
x1 = Z1*U11 + Z2*U12
x2 = Z1*U21 + Z2*U22
RETURN
END SUBROUTINE BINORMAL
!------------------------------------------------------------------------------------------------
! C
! C SUBROUTINE GNORMAL
! C
! C This subroutine give us a value following the gaussian
! C distribution law. We initialize the subroutine (flag negative)
! C calling it with ARG the minimum and the maximun of the interval
! C in which we want the result. After that, we call again the
! C subroutine with a flag no negative to have the result.
! C
! C
! C
SUBROUTINE GNORMAL (ARG,ISEED,IFLAG)
implicit none
integer(kind=ski), intent(in) :: iflag
integer(kind=ski), intent(inout) :: iseed
real(kind=skr), intent(out) :: arg
real(kind=skr) :: ymin=0.0,ymax=0.0 ! initialization implies "SAVE"
real(kind=skr) :: yval
integer(kind=ski) :: ierr
!SAVE YMIN, YMAX
IF (IFLAG.LT.0) THEN
IF (IFLAG.EQ.-2) CALL GNORFUNC (ARG,YMIN)
IF (IFLAG.EQ.-1) CALL GNORFUNC (ARG,YMAX)
ELSE
YVAL = YMIN + WRAN(ISEED)*(YMAX-YMIN)
CALL MDNRIS(YVAL,ARG,IERR)
! IF (IERR.NE.0) CALL MSSG &
! ('Error from GNORMAL','Value outside the interval',IERR)
IF (IERR.NE.0) print *,'GNORMAL: Error from MDNRIS.'
END IF
RETURN
End Subroutine gnormal
! C ++++
! C Subroutine GNORFUNC (Normal or gaussian probability distribution
! C function).
! C ----
SUBROUTINE GNORFUNC (Y,P)
implicit none
real(KIND=SKR),intent(in) :: y
real(KIND=SKR),intent(out) :: p
real(KIND=SKR) :: y0
Y0 =-Y/DSQRT(2.0D0)
P = 0.5D0*ERFC(Y0)
RETURN
END SUBROUTINE gnorfunc
! C ++++
! C FUNCTION ERFC (Complemented Error Fuction)
! C Reference: See Numerical Recipes in Fortran, Cambridge U. Press
! C (1989) for a description of this and following subroutines.
! C ----
REAL(KIND=SKR) FUNCTION ERFC(X)
implicit none
real(KIND=SKR),intent(in) :: x
IF(X.LT.0D0) THEN
ERFC=1.0D0+GAMMP(0.5D0,X**2)
ELSE
ERFC=GAMMQ(0.5D0,X**2)
ENDIF
RETURN
END FUNCTION erfc
! C
! C
! C
REAL(KIND=SKR) FUNCTION GAMMP(A,X)
implicit none
real(KIND=SKR),intent(in) :: a,x
real(KIND=SKR) :: gamser,gln,gammcf
IF(X.LT.0.0D0.OR.A.LE.0.0D0) THEN
!PAUSE
print *,"PAUSE statement executed. Hit Return to continue"
read (*,*)
END IF
IF(X.LT.A+1.0D0) THEN
CALL GSER(GAMSER,A,X,GLN)
ELSE
CALL GCF(GAMMCF,A,X,GLN)
GAMMP=1.0D0-GAMMCF
ENDIF
RETURN
End Function gammp
! C
! C
! C
REAL(KIND=SKR) FUNCTION GAMMQ (A,X)
implicit none
real(KIND=SKR),intent(in) :: a,x
real(KIND=SKR) :: gamser,gln,gammcf
IF(X.LT.0.0D0.OR.A.LE.0.0D0) THEN
!PAUSE
print *,"PAUSE statement executed. Hit Return to continue"
read (*,*)
END IF
IF(X.LT.A+1.0D0) THEN
CALL GSER(GAMSER,A,X,GLN)
GAMMQ=1.0D0-GAMSER
ELSE
CALL GCF(GAMMCF,A,X,GLN)
GAMMQ=GAMMCF
ENDIF
RETURN
End Function gammq
! C
! C
! C
SUBROUTINE GCF (GAMMCF,A,X,GLN)
implicit none
!implicit real(KIND=SKR) (a-h,o-z)
!implicit integer(KIND=SKI) (i-n)
real(kind=skr) :: gammcf, a, x, gln
real(kind=skr) :: gold, a0, a1, b0, b1, fac, an, ana, anf, g
integer(kind=ski) :: n,itmax=100
real(kind=skr) :: eps=3.0d-7
!PARAMETER (ITMAX=100,EPS=3.D-7)
GLN=GAMMLN(A)
GOLD=0.D0
A0=1.D0
A1=X
B0=0.D0
B1=1.D0
FAC=1.D0
DO 11 N=1,ITMAX
AN=DBLE(N)
ANA=AN-A
A0=(A1+A0*ANA)*FAC
B0=(B1+B0*ANA)*FAC
ANF=AN*FAC
A1=X*A0+ANF*A1
B1=X*B0+ANF*B1