forked from rallured/PyXFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woltsurf.f95~
639 lines (599 loc) · 20.3 KB
/
woltsurf.f95~
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
include 'specialFunctions.f95'
!This function traces to a Wolter I primary mirror
!Defined by Van Speybroeck prescription
!For WFS test, use flat to get rays close so they find correct intersection
!Surface should be placed at common focus with z+ pointing toward mirrors
subroutine wolterprimary(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,psi)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,psi
real*8 :: alpha,thetah,thetap,p,d,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum
integer :: i
!Compute Van Speybroeck parameters
alpha = .25*atan(r0/z0)
thetah = 2*(1+2*psi)/(1+psi) * alpha
thetap = 2*psi/(1+psi) * alpha
p = z0*tan(4*alpha)*tan(thetap)
d = z0*tan(4*alpha)*tan(4*alpha-thetah)
e = cos(4*alpha)*(1+tan(4*alpha)*tan(thetah))
Fz = 2*p
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fp)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-8)
F = 2*p*z(i) + p**2 + 4*e**2*p*d/(e**2-1) - x(i)**2 - y(i)**2
Fx = -2.*x(i)
Fy = -2.*y(i)
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
!print *, x(i),y(i),z(i)
!print *, F
!print * ,delt
!read *, dum
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine wolterprimary
!This function traces to a Wolter I secondary mirror
!Defined by Van Speybroeck prescription
!For WFS test, use flat to get rays close so they find correct intersection
!Surface should be placed at common focus with z+ pointing toward mirrors
subroutine woltersecondary(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,psi)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,psi
real*8 :: alpha,thetah,thetap,p,d,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum
integer :: i
!Compute Van Speybroeck parameters
alpha = .25*atan(r0/z0)
thetah = 2*(1+2*psi)/(1+psi) * alpha
thetap = 2*psi/(1+psi) * alpha
p = z0*tan(4*alpha)*tan(thetap)
d = z0*tan(4*alpha)*tan(4*alpha-thetah)
e = cos(4*alpha)*(1+tan(4*alpha)*tan(thetah))
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fz,Fp)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-8)
F = e**2*(d+z(i))**2 - z(i)**2 - x(i)**2 - y(i)**2
Fx = -2.*x(i)
Fy = -2.*y(i)
Fz = 2*e**2*(d+z(i)) - 2*z(i)
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
!print *, x(i),y(i),z(i)
!print *, F, Fx, Fy, Fz
!print * ,delt
!read *, dum
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine woltersecondary
!This function traces to a Wolter I primary mirror with sinusoidal perturbation
!Defined by Van Speybroeck prescription
!For WFS test, use flat to get rays close so they find correct intersection
!Surface should be placed at common focus with z+ pointing toward mirrors
subroutine woltersine(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,amp,freq)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,amp,freq
real*8 :: alpha,thetah,thetap,p,d,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,rad
integer :: i
!Compute Van Speybroeck parameters
alpha = .25*atan(r0/z0)
thetah = 3.*alpha
thetap = alpha
p = z0*tan(4*alpha)*tan(thetap)
d = z0*tan(4*alpha)*tan(4*alpha-thetah)
e = cos(4*alpha)*(1+tan(4*alpha)*tan(thetah))
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fz,Fp,rad)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-10)
rad = sqrt(x(i)**2+y(i)**2) + amp*sin(2*acos(-1.)*freq*z(i))
F = 2*p*z(i) + p**2 + 4*e**2*p*d/(e**2-1) - rad**2
Fx = -2.*x(i)
Fy = -2.*y(i)
Fz = 2.*p - 2*rad*amp*2*acos(-1.)*freq*cos(2*acos(-1.)*freq*z(i))
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
!print *, x(i),y(i),z(i)
!print *, F
!print * ,delt
!read *, dum
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine woltersine
!Construct a paraboloid as in Wolter but with Legendre-Legendre
!deformations. Define Legendre and Legendre derivative functions.
!Pass in coeff, axial order, and azimuthal order as in Zemax implementation
subroutine wolterprimLL(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,zmax,zmin,dphi,coeff,axial,az,cnum)
!Declarations
implicit none
integer, intent(in) :: num,cnum
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,zmax,zmin,dphi,coeff(cnum)
integer, intent(in) :: axial(cnum),az(cnum)
real*8 :: alpha,thetah,thetap,p,d,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,rad,add,addx,addy,addz,G
integer :: i,a
real*8 :: pi,legendre,legendrep,zarg,targ,ang
!Compute Van Speybroeck parameters
pi = acos(-1.)
alpha = .25*atan(r0/z0)
thetah = 3.*alpha
thetap = alpha
p = z0*tan(4*alpha)*tan(thetap)
d = z0*tan(4*alpha)*tan(4*alpha-thetah)
e = cos(4*alpha)*(1+tan(4*alpha)*tan(thetah))
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fz,Fp,rad,ang,zarg,targ,add,addx,addy,addz,a,G)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-10)
ang = atan2(y(i),x(i))
zarg = (z(i)-((zmax+zmin)/2.)) / ((zmax-zmin)/2.)
targ = 2*ang/dphi
!Compute Legendre additive terms
add = 0.
addx = 0.
addy = 0.
addz = 0.
do a=1,cnum
add = add + coeff(a)*legendre(zarg,axial(a))*legendre(targ,az(a))
addx = addx - coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(y(i)/(y(i)**2+x(i)**2))
addy = addy + coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(x(i)/(y(i)**2+x(i)**2))
addz = addz + coeff(a)*legendrep(zarg,axial(a))*legendre(targ,az(a))*2/(zmax-zmin)
end do
G = sqrt(x(i)**2+y(i)**2) + add
F = -(G**2 - p**2 - 2*p*z(i) - 4*e**2*p*d/(e**2-1))
Fx = -2*G*(x(i)/sqrt(x(i)**2+y(i)**2)+addx)
Fy = -2*G*(y(i)/sqrt(x(i)**2+y(i)**2)+addy)
Fz = 2*p - 2*G*addz
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!Set rays outside mirror definition to NaN
!if (abs(zarg)>1 .or. abs(targ) > 1) then
! x(i) = 0.
! y(i) = 0.
! z(i) = 0.
!end if
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine wolterprimLL
!Construct a hyperboloid as in Wolter but with Legendre-Legendre
!deformations. Define Legendre and Legendre derivative functions.
!Pass in coeff, axial order, and azimuthal order as in Zemax implementation
subroutine woltersecLL(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,psi,zmax,zmin,dphi,coeff,axial,az,cnum)
!Declarations
implicit none
integer, intent(in) :: num,cnum
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,psi,zmax,zmin,dphi,coeff(cnum)
integer, intent(in) :: axial(cnum),az(cnum)
real*8 :: alpha,thetah,thetap,p,d,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,rad,add,addx,addy,addz,G
integer :: i,a
real*8 :: pi,legendre,legendrep,zarg,targ,ang
!Compute Van Speybroeck parameters
pi = acos(-1.)
alpha = .25*atan(r0/z0)
thetah = 2*(1+2*psi)/(1+psi) * alpha
thetap = 2*psi/(1+psi) * alpha
p = z0*tan(4*alpha)*tan(thetap)
d = z0*tan(4*alpha)*tan(4*alpha-thetah)
e = cos(4*alpha)*(1+tan(4*alpha)*tan(thetah))
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fz,Fp,rad,ang,zarg,targ,add,addx,addy,addz,a,G)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-8)
ang = atan2(y(i),x(i))
zarg = (z(i)-((zmax+zmin)/2.)) / ((zmax-zmin)/2.)
targ = 2*ang/dphi
!print *, x(i),y(i),z(i)
!read *, dum
!Compute Legendre additive terms
add = 0.
addx = 0.
addy = 0.
addz = 0.
do a=1,cnum
add = add + coeff(a)*legendre(zarg,axial(a))*legendre(targ,az(a))
addx = addx - coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(y(i)/(y(i)**2+x(i)**2))
addy = addy + coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(x(i)/(y(i)**2+x(i)**2))
addz = addz + coeff(a)*legendrep(zarg,axial(a))*legendre(targ,az(a))*2/(zmax-zmin)
end do
G = sqrt(x(i)**2+y(i)**2) + add
F = -(G**2 - e**2*(d+z(i))**2 + z(i)**2)
Fx = -2*G*(x(i)/sqrt(x(i)**2+y(i)**2)+addx)
Fy = -2*G*(y(i)/sqrt(x(i)**2+y(i)**2)+addy)
Fz = 2*e**2*(d+z(i)) - 2*z(i) - 2*G*addz
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
!print *, G, F, Fx, Fy, Fz
!print *, l(i), m(i), n(i), delt
!read *, dum
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
!print *, x(i),y(i),z(i)
!print *, F
!print * ,delt
!read *, dum
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!Set rays outside mirror definition to NaN
!if (abs(zarg)>1 .or. abs(targ) > 1) then
! x(i) = 0.
! y(i) = 0.
! z(i) = 0.
!end if
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine woltersecLL
!This function traces to a Wolter-Schwarzschild primary mirror
!Defined by Van Speybroeck prescription
!Surface should be placed at common focus with z+ pointing toward mirrors
!If ray is within inner radius of mirror (defined by betas), it will be
!traced to minimum z position
!Code in Python wrapper must vignette such rays
subroutine wsprimary(x,y,z,l,m,n,ux,uy,uz,num,alpha,z0,psi)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: alpha,z0,psi
real*8 :: k,kterm,dbdx,dbdy,beta,betas,ff,g,r
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,Fb
integer :: i, flag, c
!Compute Chase parameters
betas = 4*alpha
ff = z0/cos(betas)
g = ff / psi
k = tan(betas/2)**2
!Loop through rays and trace to mirror
!$omp parallel do private(i,delt,F,Fx,Fy,Fz,Fp,Fb,kterm,beta,dbdx,dbdy,flag,r)
do i=1,num
delt = 100.
c = 0
do while(abs(delt)>1.e-8)
beta = asin(sqrt(x(i)**2 + y(i)**2)/ff)
flag = 0
if (beta<=betas) then
beta = betas
flag = 1
kterm = 0.
else
kterm = (1/k)*tan(beta/2)**2 - 1
end if
F = -z(i) - ff*sin(betas/2)**2 + &
ff**2*sin(beta)**2/(4*ff*sin(betas/2)**2) + &
g*cos(beta/2)**4*(kterm)**(1-k)
Fb = ff**2*sin(beta)*cos(beta)/(2*ff*sin(betas/2)**2) - &
2*g*cos(beta/2)**3*sin(beta/2)*(kterm)**(1-k) + &
g*(1-k)*cos(beta/2)*sin(beta/2)*(kterm)**(-k)*(1/k)
Fz = -1.
if (flag==1) then
r = sqrt(x(i)**2 + y(i)**2)
Fb = ff**2*sin(betas)*cos(betas)/(2*ff*sin(betas/2)**2) + &
g*(1-k)*cos(betas/2)*sin(betas/2)*(1/k)
F = F + (r - ff*sin(betas))*z(i)/(r**2+z(i)**2)*Fb
Fz = Fz + (r-ff*sin(betas))*(r**2-z(i)**2)/(r**2+z(i)**2)**2*Fb
!print *, Fb, F, Fz
!read *, dum
end if
dbdx = x(i)/sqrt(1-(x(i)**2+y(i)**2)/ff**2)/ff/sqrt(x(i)**2+y(i)**2)
dbdy = y(i)/sqrt(1-(x(i)**2+y(i)**2)/ff**2)/ff/sqrt(x(i)**2+y(i)**2)
Fx = Fb * dbdx
Fy = Fb * dbdy
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
!print *, x(i),y(i),z(i)
!print *, F, Fx, Fy, Fz
!print *, kterm, Fb, flag,k,tan(beta/2)**2
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
if (c > 10) then
delt = 0.
l(i) = 0.
m(i) = 0.
n(i) = 0.
end if
c = c + 1
!read *, dum
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = -Fx/Fp
uy(i) = -Fy/Fp
uz(i) = -Fz/Fp
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine wsprimary
!This function traces to a Wolter-Schwarzschild secondary mirror
!Defined by Van Speybroeck prescription
!Surface should be placed at common focus with z+ pointing toward mirrors
!If ray is within inner radius of mirror (defined by betas), it will be
!traced to minimum z position
!Code in Python wrapper must vignette such rays
subroutine wssecondary(x,y,z,l,m,n,ux,uy,uz,num,alpha,z0,psi)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: alpha,z0,psi
real*8 :: k,kterm,dbdx,dbdy,dbdz,dadb,beta,betas,ff,g,d,a
real*8 :: gam,dbdzs,dadbs
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,Fb
integer :: i, flag, c
!Compute Chase parameters
betas = 4*alpha
ff = z0/cos(betas)
g = ff / psi
k = tan(betas/2)**2
!Loop through rays and trace to mirror
!$omp parallel do private(i,delt,F,Fx,Fy,Fz,Fp,Fb,kterm,beta,dbdx,dbdy,dbdz,flag,c,a,dadbs,dbdzs,gam,dadb)
do i=1,num
delt = 100.
c = 0
do while(abs(delt)>1.e-8)
beta = atan2(sqrt(x(i)**2 + y(i)**2),z(i))
flag = 0
if (beta<=betas) then
beta = betas
kterm = 0
a = 1/ff
flag = 1
else
kterm = (1/k)*tan(beta/2)**2 - 1
a = (1-cos(beta))/(1-cos(betas))/ff + &
(1+cos(beta))/(2*g)*(kterm)**(1+k)
end if
F = -z(i) + cos(beta)/a
!Add corrective term to F if beta was < betas
if (flag==1) then
Fb = 0.
dadbs = sin(betas)/ff/(1-cos(betas)) + &
(k+1)*(cos(betas)+1)*tan(betas/2)/cos(betas/2)**2/2/g/k
dbdzs = -sin(betas)**2/sqrt(x(i)**2+y(i)**2)
gam = (-ff*sin(betas)-ff**2*cos(betas)*dadbs)*dbdzs
F = F + gam*(z(i)-sqrt(x(i)**2+y(i)**2)/tan(betas))
Fx = -2./tan(betas)*x(i)/sqrt(x(i)**2+y(i)**2)
Fy = -2./tan(betas)*y(i)/sqrt(x(i)**2+y(i)**2)
Fz = gam - 1.
!print *, x(i), y(i), z(i)
!print *, F, Fx, Fy, Fz
!print *, delt
!read *, dum
!Otherwise, business as usual
else
dadb = sin(beta)/ff/(1-cos(betas)) - &
sin(beta)/(2*g)*(kterm)**(1+k) + &
(k+1)*(cos(beta)+1)*tan(beta/2)*kterm**k/2/g/k/(cos(beta/2)**2)
Fb = -sin(beta)/a - cos(beta)/a**2*dadb
dbdx = x(i)*z(i)/(x(i)**2+y(i)**2+z(i)**2)/sqrt(x(i)**2+y(i)**2)
dbdy = y(i)*z(i)/(x(i)**2+y(i)**2+z(i)**2)/sqrt(x(i)**2+y(i)**2)
dbdz = -sqrt(x(i)**2+y(i)**2)/(x(i)**2+y(i)**2+z(i)**2)
Fx = Fb * dbdx
Fy = Fb * dbdy
Fz = -1. + Fb*dbdz
end if
!We have derivatives, now compute the iteration
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
!print *, x(i), y(i), z(i)
!print *, F, Fx, Fy, Fz
!print *, delt
!read *, dum
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
if (c > 10) then
delt = 0.
l(i) = 0.
m(i) = 0.
n(i) = 0.
end if
!print *, x(i),y(i),z(i)
!print *, F
!print * ,delt
!read *, dum
c = c+1
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!print *, F, Fx, Fy, Fz
!read *, dum
end do
!$omp end parallel do
end subroutine wssecondary
!Intersection with SPO Cone
subroutine spoCone(x,y,z,l,m,n,ux,uy,uz,num,R0,tg)
!Declarations
implicit none
integer, intent(in) :: num
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: R0,tg
! real*8 :: k,kterm,dbdx,dbdy,dbdz,dadb,beta,betas,ff,g,d,a
! real*8 :: gam,dbdzs,dadbs
! real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,Fb
integer :: i
real*8 :: A,B,C,sl,det,t1,t2
!Loop through rays and trace to mirror
sl = tan(tg)
!$omp parallel do private(i,A,B,C,det,t1,t2)
do i=1,num
!Solve quadratic equation for ray advancement distance
A = n(i)**2*sl**2 - m(i)**2 - l(i)**2
B = 2*n(i)*sl*R0 + 2*z(i)*sl**2*n(i) - 2*x(i)*l(i) - 2*y(i)*m(i)
C = R0**2 + 2*sl*R0*z(i) + z(i)**2*sl**2 - x(i)**2 - y(i)**2
det = B**2 - 4*A*C
if (det .ge. 0) then
t1 = (-B + sqrt(det))/(2*A)
t2 = (-B - sqrt(det))/(2*A)
if (abs(t2) < abs(t1)) then
t1 = t2
end if
!Set new ray position
x(i) = x(i) + t1*l(i)
y(i) = y(i) + t1*m(i)
z(i) = z(i) + t1*n(i)
!Set up surface normal
ux(i) = -x(i)/sqrt(x(i)**2+y(i)**2)*cos(tg)
uy(i) = -y(i)/sqrt(x(i)**2+y(i)**2)*cos(tg)
uz(i) = sin(tg)!*abs(z(i))/z(i)
else
l(i) = 0.
m(i) = 0.
n(i) = 0.
end if
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!print *, F, Fx, Fy, Fz
!read *, dum
end do
!$omp end parallel do
end subroutine spoCone
!Construct an ellipsoid primary but with Legendre-Legendre
!deformations. Define Legendre and Legendre derivative functions.
!Pass in coeff, axial order, and azimuthal order as in Zemax implementation
subroutine ellipsoidWoltLL(x,y,z,l,m,n,ux,uy,uz,num,r0,z0,psi,S,zmax,zmin,dphi,coeff,axial,az,cnum)
!Declarations
implicit none
integer, intent(in) :: num,cnum
real*8 , intent(inout) :: x(num),y(num),z(num),l(num),m(num),n(num),ux(num),uy(num),uz(num)
real*8, intent(in) :: r0,z0,psi,S,zmax,zmin,dphi,coeff(cnum)
integer, intent(in) :: axial(cnum),az(cnum)
real*8 :: P,ff,bq,cq,aa,bb,e
real*8 :: F,Fx,Fy,Fz,Fp,delt,dum,rad,add,addx,addy,addz,G
integer :: i,a
real*8 :: pi,legendre,legendrep,zarg,targ,ang,dummy,zfoc
!Compute telescope parameters
pi = acos(-1.)
P = R0/sin((psi*asin(R0/z0)-asin(R0/S))/(1+psi))
ff = (S+P)/2.
bq = -(R0**2+(ff-P)**2+ff**2)
cq = ff**2*(ff-P)**2
aa = sqrt((-bq+sqrt(bq**2-4*cq))/2.)
bb = sqrt(aa**2-ff**2)
e = ff/aa
zfoc = ff-P+z0
!Loop through rays and trace to mirror
!$omp parallel do private(delt,F,Fx,Fy,Fz,Fp,rad,ang,zarg,targ,add,addx,addy,addz,a,G)
do i=1,num
delt = 100.
do while(abs(delt)>1.e-10)
ang = atan2(y(i),x(i))
zarg = (z(i)-((zmax+zmin)/2.)) / ((zmax-zmin)/2.)
targ = 2*ang/dphi
!Compute Legendre additive terms
add = 0.
addx = 0.
addy = 0.
addz = 0.
do a=1,cnum
add = add + coeff(a)*legendre(zarg,axial(a))*legendre(targ,az(a))
addx = addx - coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(y(i)/(y(i)**2+x(i)**2))
addy = addy + coeff(a)*legendre(zarg,axial(a))*legendrep(targ,az(a))*(2/dphi)*(x(i)/(y(i)**2+x(i)**2))
addz = addz + coeff(a)*legendrep(zarg,axial(a))*legendre(targ,az(a))*2/(zmax-zmin)
end do
G = sqrt(x(i)**2+y(i)**2) + add
F = (z(i)-zfoc)**2/aa**2 + G**2/bb**2 - 1.
Fx = 2*G/bb**2*(x(i)/sqrt(x(i)**2+y(i)**2)+addx)
Fy = 2*G/bb**2*(y(i)/sqrt(x(i)**2+y(i)**2)+addy)
Fz = 2*(z(i)-zfoc)/aa**2 + (2*G/bb**2)*(addz)
Fp = Fx*l(i) + Fy*m(i) + Fz*n(i)
delt = -F/Fp
!print *, 'Position: ',x(i), y(i), z(i)
!print *, 'Function: ', F, Fx, Fy, Fz
!print *, 'Change: ',delt, z0,aa
!print *, z(i)-zfoc
!read *, dummy
x(i) = x(i) + l(i)*delt
y(i) = y(i) + m(i)*delt
z(i) = z(i) + n(i)*delt
end do
Fp = sqrt(Fx*Fx+Fy*Fy+Fz*Fz)
ux(i) = Fx/Fp
uy(i) = Fy/Fp
uz(i) = Fz/Fp
!Set rays outside mirror definition to NaN
!if (abs(zarg)>1 .or. abs(targ) > 1) then
! x(i) = 0.
! y(i) = 0.
! z(i) = 0.
!end if
!print *, x(i),y(i),z(i)
!print *, ux(i),uy(i),uz(i)
!read *, dum
end do
!$omp end parallel do
end subroutine ellipsoidWoltLL