forked from rallured/PyXFocus_old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPytranCode.f95~
More file actions
1903 lines (1748 loc) · 55.8 KB
/
PytranCode.f95~
File metadata and controls
1903 lines (1748 loc) · 55.8 KB
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
!Factorial Function
real*8 function factorial(n)
implicit none
integer, intent(in) :: n
real*8 :: Ans
integer :: i
Ans = 1
do i=1,n
Ans = Ans * dble(i)
end do
factorial = Ans
end function factorial
!Function to compute radial Zernike polynomial at radius rho of order n,m
real*8 function radialpoly(rho,n,m)
!Declarations
implicit none
real*8, intent(in) :: rho
integer, intent(in) :: n,m
real*8 :: output
real*8 :: const, factorial
integer :: j
!n,m are assumed to be valid (m>=n, n-abs(m) is even)
!Following Python module convention, negative m indicates sinusoidal Zernike
output = 0.
if (rho<=1) then
do j = 0,(n-abs(m))/2
const = (-1)**j*factorial(n-j)/factorial(j)/factorial((n+m)/2-j)/factorial((n-m)/2-j)
output = output + const*rho**(n-2*j)
end do
end if
radialpoly = output
end function radialpoly
!This function makes use of the q recursive method to compute a Zernike
!Radial polynomial
recursive function fastradpoly(rho,n,m) result(output)
!Declarations
implicit none
real*8, intent(in) :: rho,n,m
!integer, intent(in) :: n,m
real*8 :: output,r2,r4,h1,h2,h3,tempm
!Handle rho=0
if (rho==0) then
if (m==0) then
output = (-1.)**(n/2)
return
else
output = 0.
return
end if
end if
!Three possibilities, m=n, m=n-2, m<n-2
if (m==n) then
output = rho**n
return
else if (m==n-2) then
output = n*rho**n - (n-1)*rho**(n-2)
return
else
tempm = n-4
r4 = rho**n
r2 = n*rho**n - (n-1)*rho**(n-2)
do while(tempm >= m)
h3 = -4*(tempm+2)*(tempm+1)/(n+tempm+2)/(n-tempm)
h2 = h3*(n+tempm+4)*(n-tempm-2)/4./(tempm+3) + (tempm+2)
h1 = .5*(tempm+4)*(tempm+3) - (tempm+4)*h2 + h3*(n+tempm+6)*(n-tempm-4)/8.
output = h1*r4 + (h2 + h3/rho**2)*r2
if (tempm == m) then
return
end if
r4 = r2
r2 = output
tempm = tempm - 2
end do
end if
end function fastradpoly
!This function makes use of the q recursive method to compute a Zernike
!Radial polynomial DERIVATIVE
recursive function fastradder(rho,n,m) result(output)
!Declarations
implicit none
real*8, intent(in) :: rho
real*8, intent(in) :: n,m
real*8 :: output,r2,r4,h1,h2,h3,fastradpoly,tempm
!Handle rho=0
if (rho==0) then
if (m==1) then
output = (-1.)**((n-1)/2)*(n+1)/2
return
else
output = 0.
return
end if
end if
!Three possibilities, m=n, m=n-2, m<n-2
if (m==n) then
output = n*rho**(n-1)
return
else if (m==n-2) then
output = n*(n*rho**(n-1)) - (n-1)*(n-2)*rho**(n-3)
return
else
tempm = n-4
r4 = n*rho**(n-1)
r2 = n*(n*rho**(n-1)) - (n-1)*(n-2)*rho**(n-3)
do while(tempm >= m)
h3 = -4*(tempm+2)*(tempm+1)/(n+tempm+2)/(n-tempm)
h2 = h3*(n+tempm+4)*(n-tempm-2)/4./(tempm+3) + (tempm+2)
h1 = .5*(tempm+4)*(tempm+3) - (tempm+4)*h2 + h3*(n+tempm+6)*(n-tempm-4)/8.
!Might need to literally copy and paste fastradpoly here to save on function call...
output = h1*r4 + (h2 + h3/rho**2)*r2 - 2*h3*fastradpoly(rho,n,tempm+2)/rho**3
if (tempm == m) then
return
end if
r4 = r2
r2 = output
tempm = tempm - 2
end do
end if
end function fastradder
!This function will return a vector of Zernike polynomial evaluations for
!a point (rho,theta) and a number N of standard Zernike polynomials
!Start at n=0, and loop up in radial order
!For each radial order, start at Znn, and recursively calculate down
!Adding azimuthal dependence is easy, just note what overall index you're at
!and add sinmt if odd and cosmt if even, where Z1 = 1, so Z2=2rcost
!Still need special handling of rho=0
subroutine zernset(rho,theta,rorder,aorder,znum,polyout,derrho,dertheta)
!Declarations
implicit none
integer, intent(in) :: znum
integer, intent(in) :: rorder(znum),aorder(znum)
real*8, intent(in) :: rho,theta
real*8, intent(out) :: polyout(znum),derrho(znum),dertheta(znum)
real*8, allocatable :: rnm(:,:),rprime(:,:)
integer :: i,j,radnum,tznum
real*8 :: n,m,mm,h1,h2,h3,norm
real*8 :: fastradpoly,fastradder,zernthetader,zernrhoder,zernike
!Allocate temp output array for full sets of radial polynomials
!Will slice off unused terms at the end
tznum = 1
radnum = 1
do while(tznum < znum)
tznum = tznum + (radnum + 1)
radnum = radnum + 1
end do
!print *, tznum, " ", radnum
!Allocate radnum by radnum arrays for radial polynomials
!This is much larger than necessary, but for practical purposes this should be ok
allocate(rnm(radnum,radnum))
allocate(rprime(radnum,radnum))
!Compute radial polynomials and derivatives
do i=1,radnum
n = dble(i)-1
do j=int(n)+1,1,-2
m = dble(j)-1
!Handle case rho=0
if (rho==0) then
if (m==1) then
rprime(i,j) = (-1.)**((n-1)/2)*(n+1)/2
rnm(i,j) = 0.
else if (m==0) then
rprime(i,j) = 0.
rnm(i,j) = (-1.)**(n/2)
else
rprime(i,j) = 0.
rnm(i,j) = 0.
end if
!Handle general case
else
if (n==m) then
rnm(i,j) = rho**n
rprime(i,j) = n*rho**(n-1)
else if (m==n-2) then
rnm(i,j) = n*rnm(i,i) - (n-1)*rnm(i-2,i-2)
rprime(i,j) = n*rprime(i,i) - (n-1)*rprime(i-2,i-2)
else
h3 = -4*(m+2)*(m+1)/(n+m+2)/(n-m)
h2 = h3*(n+m+4)*(n-m-2)/4./(m+3) + (m+2)
h1 = .5*(m+4)*(m+3) - (m+4)*h2 + h3*(n+m+6)*(n-m-4)/8.
rnm(i,j) = h1*rnm(i,j+4) + (h2+h3/rho**2)*rnm(i,j+2)
rprime(i,j) = h1*rprime(i,j+4) + (h2+h3/rho**2)*rprime(i,j+2) - 2*h3/rho**3*rnm(i,j+2)
end if
end if
!print *, i-1, " ", j-1
!print *, rnm(i,j), " ", fastradpoly(rho,dble(i-1),dble(j-1))
!print *, rprime(i,j), " ", fastradder(rho,dble(i-1),dble(j-1))
end do
end do
!Radial terms are computed. Now construct Zernike, Zernderrho, and Zerndertheta
!Use standard order up to znum
do i=1,znum
!Rorder and Aorder are passed to this function
!Simply construct if statement on m, and compute all three!
n = rorder(i)
mm = aorder(i)
m = abs(mm)
norm = sqrt(2*(n+1))
if (mm<0) then
polyout(i) = norm * rnm(int(n)+1,int(m)+1) * sin(m*theta)
derrho(i) = norm * rprime(int(n)+1,int(m)+1) * sin(m*theta)
dertheta(i) = norm * rnm(int(n)+1,int(m)+1) * cos(m*theta) * m
else if (mm>0) then
polyout(i) = norm * rnm(int(n)+1,int(m)+1) * cos(m*theta)
derrho(i) = norm * rprime(int(n)+1,int(m)+1) * cos(m*theta)
dertheta(i) = -norm * rnm(int(n)+1,int(m)+1) * sin(m*theta) * m
else
polyout(i) = norm*sqrt(0.5)*rnm(int(n)+1,int(m)+1)
derrho(i) = norm*sqrt(0.5)*rprime(int(n)+1,int(m)+1)
dertheta(i) = 0.
end if
!print *, dertheta(i), " ",zernthetader(rho,theta,int(n),int(mm))
end do
end subroutine zernset
!Function to compute full Zernike polynomial at radius rho and angle theta of order n,m
!Following Python Zernike mod conventions
real*8 function zernike(rho,theta,n,m)
!Declarations
implicit none
real*8, intent(in) :: rho,theta
integer, intent(in) :: n,m
real*8 :: fastradpoly,rnm,znm,norm
!Compute radial polynomial
rnm = fastradpoly(rho,dble(n),abs(dble(m)))
!Compute full Zernike polynomial
norm = sqrt(2*(dble(n)+1))
if (m<0) then
znm = norm * rnm * sin(m*theta)
else if (m>0) then
znm = norm * rnm * cos(m*theta)
else
znm = norm*sqrt(0.5)*rnm
end if
!Return Zernike polynomial
zernike = znm
end function zernike
!Function to compute radial polynomial derivative
real*8 function radialder(rho,n,m)
!Declarations
implicit none
real*8, intent(in) :: rho
integer, intent(in) :: n,m
real*8 :: output
real*8 :: const, factorial
integer :: j
!n,m are assumed to be valid (m>=n, n-abs(m) is even)
!Following Python module convention, negative m indicates sinusoidal Zernike
output = 0.
if (rho<=1) then
do j = 0,(n-abs(m))/2
const = (-1)**j*factorial(n-j)/factorial(j)/factorial((n+m)/2-j)/factorial((n-m)/2-j)
output = output + const*(n-2*j)*rho**(n-2*j-1)
end do
end if
radialder = output
end function radialder
!Function to compute partial Zernike derivative wrt rho
real*8 function zernrhoder(rho,theta,n,m)
!Declarations
implicit none
real*8, intent(in) :: rho,theta
integer, intent(in) :: n,m
real*8 :: fastradder, rnm, norm, znm
!Compute radial polynomial
rnm = fastradder(rho,dble(n),abs(dble(m)))
!Compute full Zernike polynomial
norm = sqrt(2*(dble(n)+1))
if (m<0) then
znm = norm * rnm * sin(abs(m)*theta)
else if (m>0) then
znm = norm * rnm * cos(m*theta)
else
znm = norm*sqrt(0.5)*rnm
end if
!Return Zernike polynomial
zernrhoder = znm
end function zernrhoder
!Function to compute partial Zernike derivative wrt theta
real*8 function zernthetader(rho,theta,n,m)
!Declarations
implicit none
real*8, intent(in) :: rho,theta
integer, intent(in) :: n,m
real*8 :: rnm, znm, fastradpoly, norm
!Compute radial polynomial
rnm = fastradpoly(rho,dble(n),abs(dble(m)))
!Compute full Zernike polynomial
norm = sqrt(2*(dble(n)+1))
if (m<0) then
znm = norm * rnm * abs(m) * cos(abs(m)*theta)
else if (m>0) then
znm = -norm * rnm * m * sin(m*theta)
else
znm = 0.
end if
!Return Zernike polynomial
zernthetader = znm
end function zernthetader
!Function to trace set of rays to Zernike surface
!Inputs are position and cosines of rays
!Array of Zernike coefficients
!Output is position of rays at surface
!and new cosines after reflection
subroutine tracezern(x,y,z,l,m,n,ux,uy,uz,num,coeff,rorder,aorder,arrsize,rad)
!Declarations
implicit none
integer, intent(in) :: arrsize, num
real*8, intent(in) :: coeff(arrsize),rad
real*8, intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer, intent(in) :: rorder(arrsize),aorder(arrsize)
integer :: i,c
real*8 :: F,Frho,Ftheta,Frhox,Frhoy,Fthetax,Fthetay,Fx,Fy,Fz,Fp,rho,theta,delta,t
real*8 :: zern(arrsize),rhoder(arrsize),thetader(arrsize)
real*8 :: zernike,zernrhoder,zernthetader
real*8 :: dum
!Loop through each individual ray, trace to surface, and reflect
!Establish convergence criteria (surface function should be < 1.e-12)
!$omp parallel do private(t,delta,rho,theta,F,Frho,Ftheta,Frhox,Frhoy,Fthetax,Fthetay,Fx,Fy,Fz,Fp,i,c,zern,rhoder,thetader)
do i=1,num
t = 0.
delta = 100.
!count = 0
!print *, x(i), y(i), z(i)
!print *, l(i), m(i), n(i)
!read *, dum
do while (abs(delta) > 1.e-10)
!count = count + 1
!Convert to cylindrical coordinates
rho = sqrt(x(i)**2+y(i)**2)
theta = atan2(y(i),x(i))
!Compute surface function and derivatives
F = z(i)
Frho = 0.
Ftheta = 0.
call zernset(rho/rad,theta,rorder,aorder,arrsize,zern,rhoder,thetader)
!print *, zern
!read *, dum
do c=1,arrsize
!F = F - coeff(c)*zernike(rho/rad,theta,rorder(c),aorder(c))
!Frho = Frho - coeff(c)*zernrhoder(rho/rad,theta,rorder(c),aorder(c))/rad
!Ftheta = Ftheta - coeff(c)*zernthetader(rho/rad,theta,rorder(c),aorder(c))
F = F - coeff(c)*zern(c)
Frho = Frho - coeff(c)*rhoder(c)/rad
Ftheta = Ftheta - coeff(c)*thetader(c)
!print *, rho/rad, " ", theta, " ", rorder(c), " ", aorder(c)
!print *, zern(c), rhoder(c)
!print *, coeff(c)
!print *, F
!read *, dum
end do
!print *, rho/rad
!print *, theta
!print *, zernrhoder(rho/rad,theta,rorder(c),aorder(c))
!print *, zernthetader(rho/rad,theta,rorder(c),aorder(c))
!Convert back to cartesian coordinates
!print *, Frho, Ftheta
!read *, dum
Frhox = (x(i)/rho) * Frho
Frhoy = (y(i)/rho) * Frho
Fthetax = (-y(i)/rho) * Ftheta/rho
Fthetay = (x(i)/rho) * Ftheta/rho
!if (rho==0) then
! Frhox = Frho
! Frhoy = 0.
! Fthetax = 0.
! Fthetay = 0.
!end if
Fx = Frhox + Fthetax
Fy = Frhoy + Fthetay
Fz = 1.
!print *, Fx, Fy, Fz
!print *, l(i), m(i), n(i)
!read *, dum
!Compute delta and new ray position
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
!print *, 'Fp:' , Fp
!read *, dum
delta = -F/Fp
!print *, F, Fp, delta
!read *, dum
x(i) = x(i) + l(i)*delta
y(i) = y(i) + m(i)*delta
z(i) = z(i) + n(i)*delta
!Keep track of total length ray is traced
t = t + delta
end do
!print *, "Number of iterations: ", count
!We have converged, do any vignetting and compute surface normal
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
end do
!$omp end parallel do
end subroutine tracezern
!Trace a Zernike standard phase surface as in ZEMAX
subroutine zernphase(opd,x,y,z,l,m,n,ux,uy,uz,num,coeff,rorder,aorder,arrsize,rad,wave)
!Declarations
implicit none
integer, intent(in) :: arrsize, num
real*8, intent(in) :: coeff(arrsize),rad,wave
real*8, intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num),opd(num)
integer, intent(in) :: rorder(arrsize),aorder(arrsize)
real*8 :: zern(arrsize),rhoder(arrsize),thetader(arrsize)
real*8 :: F,Frho,Ftheta,Frhox,Frhoy,Fthetax,Fthetay,Fx,Fy
real*8 :: pi,theta,rho,dum
integer :: i,c
pi = acos(-1.)
do i=1,num
!Compute the set of Zernike polynomials
rho = sqrt(x(i)**2+y(i)**2)
theta = atan2(y(i),x(i))
call zernset(rho/rad,theta,rorder,aorder,arrsize,zern,rhoder,thetader)
!Sum derivatives
F = 0.
Frho = 0.
Ftheta = 0.
do c=1,arrsize
F = F + coeff(c)*zern(c)
Frho = Frho + coeff(c)*rhoder(c)/rad
Ftheta = Ftheta + coeff(c)*thetader(c)
end do
!Convert to Cartesian coordinates
Frhox = (x(i)/rho) * Frho
Frhoy = (y(i)/rho) * Frho
Fthetax = (-y(i)/rho) * Ftheta/rho
Fthetay = (x(i)/rho) * Ftheta/rho
Fx = Frhox + Fthetax
Fy = Frhoy + Fthetay
!print *, rho,theta,Frho, Ftheta,Fx,Fy,wave
!read *, dum
!Add appropriate perturbations to ray wavevector
l(i) = l(i) + Fx*wave!/2/pi
m(i) = m(i) + Fy*wave!/2/pi
n(i) = sign(sqrt(1.-l(i)**2-m(i)**2),n(i))
opd(i) = opd(i) + F*wave
end do
end subroutine zernphase
!Function to trace set of rays to Zernike surface
!Inputs are position and cosines of rays
!Array of Zernike coefficients
!Output is position of rays at surface
!and new cosines after reflection
subroutine tracezernrot(x,y,z,l,m,n,ux,uy,uz,num,coeff1,rorder1,aorder1,arrsize1,coeff2,rorder2,aorder2,arrsize2,rad,rot)
!Declarations
implicit none
integer, intent(in) :: arrsize1, arrsize2, num
real*8, intent(in) :: coeff1(arrsize1),coeff2(arrsize2),rad,rot
real*8, intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer, intent(in) :: rorder1(arrsize1),aorder1(arrsize1)
integer, intent(in) :: rorder2(arrsize2),aorder2(arrsize2)
integer :: i,c
real*8 :: F,Frho,Ftheta,Frhox,Frhoy,Fthetax,Fthetay,Fx,Fy,Fz,Fp,rho,theta,delta,t
real*8 :: zern1(arrsize1),rhoder1(arrsize1),thetader1(arrsize1)
real*8 :: zern2(arrsize2),rhoder2(arrsize2),thetader2(arrsize2)
real*8 :: zernike,zernrhoder,zernthetader
real*8 :: dum
!Loop through each individual ray, trace to surface, and reflect
!Establish convergence criteria (surface function should be < 1.e-12)
!$omp parallel do private(t,delta,rho,theta,F,Frho,Ftheta,Frhox,Frhoy,Fthetax,Fthetay,Fx,Fy,Fz,Fp,i,c,zern1,zern2)
do i=1,num
t = 0.
delta = 100.
!count = 0
!print *, x(i), y(i), z(i)
!print *, l(i), m(i), n(i)
!read *, dum
do while (abs(delta) > 1.e-10)
!count = count + 1
!Convert to cylindrical coordinates
rho = sqrt(x(i)**2+y(i)**2)
theta = atan2(y(i),x(i))
!Compute surface function and derivatives
F = z(i)
Frho = 0.
Ftheta = 0.
call zernset(rho/rad,theta,rorder1,aorder1,arrsize1,zern1,rhoder1,thetader1)
!print *, zern1(1)
!read *, dum
do c=1,arrsize1
!F = F - coeff(c)*zernike(rho/rad,theta,rorder(c),aorder(c))
!Frho = Frho - coeff(c)*zernrhoder(rho/rad,theta,rorder(c),aorder(c))/rad
!Ftheta = Ftheta - coeff(c)*zernthetader(rho/rad,theta,rorder(c),aorder(c))
F = F - coeff1(c)*zern1(c)
Frho = Frho - coeff1(c)*rhoder1(c)/rad
Ftheta = Ftheta - coeff1(c)*thetader1(c)
!print *, rho/rad, " ", theta, " ", rorder(c), " ", aorder(c)
!print *, zern(c), rhoder(c)
!print *, coeff(c)
!print *, F
!read *, dum
end do
call zernset(rho/rad,theta+rot,rorder2,aorder2,arrsize2,zern2,rhoder2,thetader2)
!print *, zern2(1)
do c=1,arrsize2
F = F - coeff2(c)*zern2(c)
Frho = Frho - coeff2(c)*rhoder2(c)/rad
Ftheta = Ftheta - coeff2(c)*thetader2(c)
end do
!print *, rho/rad
!print *, theta
!print *, zernrhoder(rho/rad,theta,rorder(c),aorder(c))
!print *, zernthetader(rho/rad,theta,rorder(c),aorder(c))
!Convert back to cartesian coordinates
!print *, Frho, Ftheta
!read *, dum
Frhox = (x(i)/rho) * Frho
Frhoy = (y(i)/rho) * Frho
Fthetax = (-y(i)/rho) * Ftheta/rho
Fthetay = (x(i)/rho) * Ftheta/rho
!if (rho==0) then
! Frhox = Frho
! Frhoy = 0.
! Fthetax = 0.
! Fthetay = 0.
!end if
Fx = Frhox + Fthetax
Fy = Frhoy + Fthetay
Fz = 1.
!print *, Fx, Fy, Fz
!print *, l(i), m(i), n(i)
!read *, dum
!Compute delta and new ray position
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
!print *, 'Fp:' , Fp
!read *, dum
delta = -F/Fp
!print *, F, Fp, delta
!read *, dum
x(i) = x(i) + l(i)*delta
y(i) = y(i) + m(i)*delta
z(i) = z(i) + n(i)*delta
!Keep track of total length ray is traced
t = t + delta
end do
!print *, "Number of iterations: ", count
!We have converged, do any vignetting and compute surface normal
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
end do
!$omp end parallel do
end subroutine tracezernrot
!This function computes a Legendre polynomial of order n
real*8 function legendre(x,n)
!Declarations
implicit none
real*8, intent(in) :: x
integer, intent(in) :: n
integer :: i
real*8 :: factorial,x2
if (abs(x) > 1.) then
x2 = x/abs(x)
else
x2 = x
end if
legendre = 0.
if (n==0) then
legendre = 1.
else
do i=0,floor(real(n)/2)
legendre = legendre + (-1)**(i)*factorial(2*n-2*i)/factorial(i)/factorial(n-i)/factorial(n-2*i)/2**n*x2**(n-2*i)
end do
end if
end function legendre
!This function computes a Legendre polynomial first derivative of order n
real*8 function legendrep(x,n)
!Declarations
implicit none
real*8, intent(in) :: x
integer, intent(in) :: n
integer :: i
real*8 :: factorial
legendrep = 0.
if (n==0) then
legendrep = 0.
else if (n==1) then
legendrep = 1.
else if (x==0. .and. mod(n,2)==0) then
legendrep = 0.
else
do i=0,floor(real(n)/2)
legendrep = legendrep + (-1)**(i)*factorial(2*n-2*i)/factorial(i)/factorial(n-i)/factorial(n-2*i)/2**n*(n-2*i)*x**(n-2*i-1)
end do
end if
if (abs(x) > 1.) then
legendrep = 0.
end if
end function legendrep
!This function rotates a vector in a right handed fashion
!axis is 1,2,3 for x,y,z axis rotation
subroutine rotatevector(x,y,z,theta,axis)
!Declarations
real*8, intent(inout) :: x,y,z,theta
integer, intent(in) :: axis
real*8 :: output(3)
!real*8, dimension(3) :: rotatevector
if (axis==1) then
output(1) = x
output(2) = cos(theta)*y-sin(theta)*z
output(3) = sin(theta)*y+cos(theta)*z
else if (axis==2) then
output(1) = cos(theta)*x+sin(theta)*z
output(2) = y
output(3) = -sin(theta)*x+cos(theta)*z
else
output(1) = cos(theta)*x-sin(theta)*y
output(2) = sin(theta)*x+cos(theta)*y
output(3) = z
end if
x = output(1)
y = output(2)
z = output(3)
end subroutine rotatevector
!This function rotates a vector in a right handed fashion
!axis is given by input ux,uy,uz
subroutine rotateaxis(x,y,z,theta,ux,uy,uz)
!Declarations
real*8, intent(inout) :: x,y,z
real*8, intent(inout) :: theta,ux,uy,uz
real*8 :: output(3),s,c,mag
!real*8, dimension(3) :: rotatevector
!Ensure axis is normalized
mag = sqrt(ux**2 + uy**2 + uz**2)
ux = ux/mag
uy = uy/mag
uz = uz/mag
c = cos(theta)
s = sin(theta)
output(1) = (c+ux**2*(1-c))*x + (ux*uy*(1-c)-uz*s)*y + (ux*uz*(1-c)+uy*s)*z
output(2) = (uy*ux*(1-c)+uz*s)*x + (c+uy**2*(1-c))*y + (uy*uz*(1-c)-ux*s)*z
output(3) = (uz*ux*(1-c)-uy*s)*x + (uz*uy*(1-c)+ux*s)*y + (c+uz**2*(1-c))*z
x = output(1)
y = output(2)
z = output(3)
end subroutine rotateaxis
!Function to reflect about local surface normal
!Assume ray cosines point into surface, surface normal points out of surface
!If i = -incident, then reflected ray is 2(i.n)n-i
subroutine reflect(l,m,n,ux,uy,uz,num)
!Declarations
integer, intent(in) :: num
real*8, intent(inout) :: l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8 :: dot
integer :: i
!Loop through rays and reflect about normal
!$omp parallel do private(i,dot)
do i=1,num
!Compute dot of incident with normal
dot = ux(i)*l(i) + uy(i)*m(i) + uz(i)*n(i)
!Compute reflected direction
l(i) = l(i) - 2*dot*ux(i)
m(i) = m(i) - 2*dot*uy(i)
n(i) = n(i) - 2*dot*uz(i)
end do
!$omp end parallel do
end subroutine reflect
!Refract from one index to another
subroutine refract(l,m,n,ux,uy,uz,num,n1,n2)
!Declarations
implicit none
integer, intent(in) :: num
real*8, intent(in) :: n1,n2
real*8, intent(inout) :: l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
real*8 :: dot,t1,t2,alpha,cx,cy,cz
!Loop through rays
!$omp parallel do private(dot,t1,t2,cx,cy,cz,alpha)
do i=1,num
!Ensure normal vector is pointing into second index (dot product should be positive)
dot = l(i)*ux(i) + m(i)*uy(i) + n(i)*uz(i)
if (dot < 0) then
ux(i) = -ux(i)
uy(i) = -uy(i)
uz(i) = -uz(i)
dot = -dot
end if
!Compute Snell's law
t1 = acos(dot)
t2 = asin((n1/n2)*sin(t1))
!Compute cross product
cx = uy(i)*n(i)-m(i)*uz(i)
cy = l(i)*uz(i)-ux(i)*n(i)
cz = ux(i)*m(i)-l(i)*uy(i)
!Rotate about cross vector an angle t2-t1
call rotateaxis(l(i),m(i),n(i),t2-t1,cx,cy,cz)
!Normalize
alpha = sqrt(l(i)**2 + m(i)**2 + n(i)**2)
l(i) = l(i)/alpha
m(i) = m(i)/alpha
n(i) = n(i)/alpha
end do
!$omp end parallel do
end subroutine refract
!Coordinate system transform, translations are done first, then rotations in XYZ order
!Rotations act to rotate a surface via the right hand rule
subroutine transform(x,y,z,l,m,n,ux,uy,uz,num,tx,ty,tz,rx,ry,rz)
!Declarations
integer, intent(in) :: num
real*8, intent(inout) :: tx,ty,tz,rx,ry,rz
real*8, intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
!Loop through rays
!$omp parallel do
do i=1,num
!Perform translation
x(i) = x(i) + tx
y(i) = y(i) + ty
z(i) = z(i) + tz
!Perform x rotation
call rotatevector(x(i),y(i),z(i),rx,1)
call rotatevector(l(i),m(i),n(i),rx,1)
call rotatevector(ux(i),uy(i),uz(i),rx,1)
!Perform y rotation
call rotatevector(x(i),y(i),z(i),ry,2)
call rotatevector(l(i),m(i),n(i),ry,2)
call rotatevector(ux(i),uy(i),uz(i),ry,2)
!Perform z rotation
call rotatevector(x(i),y(i),z(i),rz,3)
call rotatevector(l(i),m(i),n(i),rz,3)
call rotatevector(ux(i),uy(i),uz(i),rz,3)
end do
!$omp end parallel do
end subroutine transform
!Coordinate system transform, rotations in ZYX order, then translations
!Rotations act to rotate a surface via the right hand rule
!This is meant to invert the transform routine
subroutine itransform(x,y,z,l,m,n,ux,uy,uz,num,tx,ty,tz,rx,ry,rz)
!Declarations
integer, intent(in) :: num
real*8, intent(inout) :: tx,ty,tz,rx,ry,rz
real*8, intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
!Loop through rays
!$omp parallel do
do i=1,num
!Perform z rotation
call rotatevector(x(i),y(i),z(i),-rz,3)
call rotatevector(l(i),m(i),n(i),-rz,3)
call rotatevector(ux(i),uy(i),uz(i),-rz,3)
!Perform y rotation
call rotatevector(x(i),y(i),z(i),-ry,2)
call rotatevector(l(i),m(i),n(i),-ry,2)
call rotatevector(ux(i),uy(i),uz(i),-ry,2)
!Perform x rotation
call rotatevector(x(i),y(i),z(i),-rx,1)
call rotatevector(l(i),m(i),n(i),-rx,1)
call rotatevector(ux(i),uy(i),uz(i),-rx,1)
!Perform translation
x(i) = x(i) - tx
y(i) = y(i) - ty
z(i) = z(i) - tz
end do
!$omp end parallel do
end subroutine itransform
!Trace rays to local XY plane
subroutine flat(x,y,z,l,m,n,ux,uy,uz,num)
!Declarations
integer, intent(in) :: num
real*8, intent(in) :: l(num),m(num),n(num)
real*8, intent(inout) :: x(num),y(num),z(num),ux(num),uy(num),uz(num)
integer :: i
real*8 :: dummy
!Loop through rays
!$omp parallel do private(delta)
do i=1,num
delta = -z(i)/n(i)
z(i) = 0.
x(i) = x(i) + delta*l(i)
y(i) = y(i) + delta*m(i)
ux(i) = 0.
uy(i) = 0.
uz(i) = 1.
!print *, x(i),y(i),z(i)
!print *, l(i),m(i),n(i)
!print *, ux(i),uy(i),uz(i)
!read *, dummy
end do
!$omp end parallel do
end subroutine flat
!Trace rays to local XY plane
subroutine flatOPD(x,y,z,l,m,n,ux,uy,uz,opd,num,nr)
!Declarations
integer, intent(in) :: num
real*8, intent(in) :: l(num),m(num),n(num),nr
real*8, intent(inout) :: x(num),y(num),z(num),ux(num),uy(num),uz(num),opd(num)
integer :: i
!Loop through rays
!$omp parallel do private(delta)
do i=1,num
delta = -z(i)/n(i)
z(i) = 0.
x(i) = x(i) + delta*l(i)
y(i) = y(i) + delta*m(i)
ux(i) = 0.
uy(i) = 0.
uz(i) = 1.
opd(i) = opd(i) + delta*nr
end do
!$omp end parallel do
end subroutine flatOPD
!Trace rays to spherical surface, center assumed to be at origin
!Intersection taken to be the closest point to ray
subroutine tracesphere(x,y,z,l,m,n,ux,uy,uz,num,rad)
!Declarations
implicit none
integer, intent(in) :: num
real*8, intent(in) :: rad
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
real*8 :: mago, dotol, determinant, d1, d2
!Loop through rays
!$omp parallel do private(dotol,mago,determinant,d1,d2)
do i=1,num
!Compute dot product
dotol= l(i)*x(i) + m(i)*y(i) + n(i)*z(i)
mago = x(i)**2. + y(i)**2. + z(i)**2.
!Compute distance to move rays
determinant = dotol**2 - mago + rad**2
!If ray does not intersect, set position and cosine vector to NaN
if (determinant < 0) then
x(i) = 0.
y(i) = 0.
z(i) = 0.
l(i) = 0.
m(i) = 0.
n(i) = 0.
else
d1 = -dotol + sqrt(determinant)
d2 = -dotol - sqrt(determinant)
if (abs(d2) < abs(d1)) then
d1 = d2
end if
x(i) = x(i) + d1*l(i)
y(i) = y(i) + d1*m(i)
z(i) = z(i) + d1*n(i)
end if
!Compute surface normal, just normalized position vector
mago = sqrt(x(i)**2 + y(i)**2 + z(i)**2)
ux(i) = x(i)/mago
uy(i) = y(i)/mago
uz(i) = z(i)/mago
end do
!$omp end parallel do
end subroutine tracesphere
!Trace rays to spherical surface, center assumed to be at origin
!Intersection taken to be the closest point to ray
subroutine tracesphereOPD(opd,x,y,z,l,m,n,ux,uy,uz,num,rad,nr)
!Declarations
implicit none
integer, intent(in) :: num
real*8, intent(in) :: rad,nr
real*8 , intent(inout) :: opd(num),x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
real*8 :: mago, dotol, determinant, d1, d2
!Loop through rays
!$omp parallel do private(dotol,mago,determinant,d1,d2)
do i=1,num
!Compute dot product
dotol= l(i)*x(i) + m(i)*y(i) + n(i)*z(i)
mago = x(i)**2. + y(i)**2. + z(i)**2.
!Compute distance to move rays
determinant = dotol**2 - mago + rad**2
!If ray does not intersect, set position and cosine vector to NaN
if (determinant < 0) then
x(i) = 0.
y(i) = 0.
z(i) = 0.
l(i) = 0.
m(i) = 0.
n(i) = 0.
else
d1 = -dotol + sqrt(determinant)
d2 = -dotol - sqrt(determinant)
if (abs(d2) < abs(d1)) then
d1 = d2
end if
x(i) = x(i) + d1*l(i)
y(i) = y(i) + d1*m(i)
z(i) = z(i) + d1*n(i)
opd(i) = opd(i) + d1*nr
end if
!Compute surface normal, just normalized position vector
mago = sqrt(x(i)**2 + y(i)**2 + z(i)**2)
ux(i) = x(i)/mago
uy(i) = y(i)/mago
uz(i) = z(i)/mago
end do
!$omp end parallel do
end subroutine tracesphereOPD
!Traces onto a cylinder
!Center is assumed to be at origin, y axis is cylindrical axis
subroutine tracecyl(x,y,z,l,m,n,ux,uy,uz,num,rad)
!Declarations
implicit none
integer, intent(in) :: num
real*8, intent(in) :: rad
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
integer :: i
real*8 :: a,b,c,mag,d1,d2,det,dum
!$omp parallel do private(a,b,c,det,mag,d1,d2)
!Compute a,b,c terms in quadratic solution for distance to move rays
do i=1,num
a = l(i)**2 + n(i)**2
b = 2*(x(i)*l(i)+z(i)*n(i))
c = x(i)**2 + z(i)**2 - rad**2
!Compute determinant, if < 0, set ray to 0's
det = b**2 - 4*a*c
if (det < 0) then