forked from SRHudson/Oculus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oculus.h
8327 lines (6573 loc) · 363 KB
/
oculus.h
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
#ifdef HAVE_CONFIGF
#include "config.f"
#else
#define HAVE_NAG
#endif
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
module oculus
implicit none
! constants; 25 Mar 15;
REAL, parameter :: zero = 0.0
REAL, parameter :: one = 1.0
REAL, parameter :: two = 2.0
REAL, parameter :: three = 3.0
REAL, parameter :: four = 4.0
REAL, parameter :: five = 5.0
REAL, parameter :: six = 6.0
REAL, parameter :: seven = 7.0
REAL, parameter :: eight = 8.0
REAL, parameter :: nine = 9.0
REAL, parameter :: ten = 10.0
REAL, parameter :: twelve = 12.0
REAL, parameter :: hundred = 100.0
REAL, parameter :: thousand = 1000.0
REAL, parameter :: pi = 3.141592653589793238462643383279502884197
REAL, parameter :: pi2 = pi * two
REAL, parameter :: goldenmean = 1.618033988749895 ! golden mean = ( one + sqrt(five) ) / two ;
REAL, parameter :: small = 1.0e-15 ! this is near the lsode machine precision limit
REAL, parameter :: half = one / two
REAL, parameter :: third = one / three
REAL, parameter :: quart = one / four
REAL, parameter :: fifth = one / five
REAL, parameter :: sixth = one / six
! structures; 25 Mar 15;
type biotsavart
LOGICAL :: LB, LA, LL
INTEGER :: N, ixyz, ifail
REAL :: tol, x, y, z, Bx, By, Bz, Ax, Ay, Az, length
end type biotsavart
type(biotsavart) :: bsfield
!$OMP threadprivate(bsfield)
type magneticaxis
INTEGER :: Nfp, Ntor, maxits, its, Lallocated, nbfield(0:1)
REAL :: R, Z, phi, odetol, tol, error, residue, iota, rzf(0:2,0:31)
REAL :: tangent(1:2,1:2), wr(1:2), wi(1:2), vr(1:2,1:2), vi(1:2,1:2)
REAL , allocatable :: Ri(:), Zi(:), Rnc(:), Rns(:), Znc(:), Zns(:)
end type magneticaxis
type(magneticaxis) :: axis
!$OMP threadprivate(axis)
type homoclinictangle
INTEGER :: Nfp, xits, maxits, hits, ilobe(1:2), Lallocated, maxilobe, nbfield(0:1)
REAL :: odetol, xtol, htol, ltol, R, Z, phi, dU, dS, xerror, herror, lerror(1:2), residue
REAL :: tangent(1:2,1:2), wr(1:2), wi(1:2), vr(1:2,1:2), vi(1:2,1:2)
REAL, allocatable :: hpoints(:,:,:)
end type homoclinictangle
type(homoclinictangle) :: tangle
!$OMP threadprivate(tangle)
type extremizingcurve
INTEGER :: Nfp, Ntor, emethod, Lrz, maxits, its, nbfield(0:1), Lallocated, itau, ibfield
REAL :: etol, ftol, err, sN, odetol, tauend, dtau, FF, epsilon
REAL, allocatable :: Rnc(:), Rns(:), Znc(:), Zns(:)
REAL, allocatable :: iRnc(:,:), iRns(:,:), iZnc(:,:), iZns(:,:) ! to be deleted; 28 Aug 15;
REAL, allocatable :: tt(:), ct(:,:), st(:,:), trig(:), rr(:), rd(:), zz(:), zd(:)
CHARACTER :: init
end type extremizingcurve
type(extremizingcurve) :: curve
!$OMP threadprivate(curve)
#ifdef FH00AA
type backgroundcoords
INTEGER :: Nfp, pplobe, nsep(1:2), Nx, Ny, Nh, Lallocated, nbfield(0:1)
INTEGER, allocatable :: ii(:), jj(:)
REAL :: odetol, svdtol, sigma
REAL :: wr(1:2), vr(1:2,1:2)
REAL, allocatable :: sep(:,:,:), hi(:)
end type backgroundcoords
type(backgroundcoords) :: bcoords
!$OMP threadprivate(bcoords)
#endif
type coordinates
INTEGER :: Lrad, Nfp, Mpol, Ntor, mn, mm
INTEGER, allocatable :: im(:), in(:)
REAL , allocatable :: ss(:), Rbc(:,:), Rbs(:,:), Zbc(:,:), Zbs(:,:)
REAL , allocatable :: Xbc(:,:,:), Xbs(:,:,:), Ybc(:,:,:), Ybs(:,:,:)
end type coordinates
type(coordinates) :: rzmn
!$OMP threadprivate(rzmn)
type vectorpotential
INTEGER :: Nfp, Lrad, Mpol, Ntor, mn, Nt, Nz, Ntz
INTEGER, allocatable :: im(:), in(:)
REAL , allocatable :: ss(:), gBtc(:,:,:), gBts(:,:,:), gBzc(:,:,:), gBzs(:,:,:), gBstz(:,:,:)
end type vectorpotential
type(vectorpotential) :: Atzmn
!$OMP threadprivate(Atzmn)
type irrationalsurface
INTEGER :: Nfp, Mpol, Ntor, Mits, Nxdof, Npts, Nc, Nk
INTEGER, allocatable :: im(:), in(:)
REAL :: odetol, irrtol
REAL , allocatable :: rcm(:), rsm(:), tcm(:), tsm(:), sap(:,:), tap(:,:), xdof(:), err(:), derr(:,:)
end type irrationalsurface
type(irrationalsurface) :: isurface
!$OMP threadprivate(isurface)
type qfminsurface
INTEGER :: Nfp, pp, qq, nn, mm, Ntor, Mpol, Np, qNp, Nd, qNd, Lrestart, Id, mn, ok
INTEGER, allocatable :: im(:), in(:)
REAL :: rr, nu, odetol, offset, pqtol
REAL , allocatable :: t(:,:), r(:,:), n(:), X(:,:), Y(:,:), Rbc(:), Rbs(:), Zbc(:), Zbs(:)
end type qfminsurface
type(qfminsurface) :: qfms
!$OMP threadprivate(qfms)
type pressurerelax
LOGICAL, allocatable :: F(:,:,:)
INTEGER :: Nfp, NR, Np, NZ, ifb, ii, Ntime, Ldiff, Lode, Lcontrol
REAL :: Dp, dtime, kpara, kperp, Rmin, Rmax, Zmin, Zmax, odetol, lB(-1:1)
INTEGER, allocatable :: I(:,:,:,:), J(:,:,:,:)
REAL , allocatable :: s(:,:,:), p(:,:,:), B(:,:,:,:), x(:,:,:,:), y(:,:,:,:)
end type pressurerelax
type(pressurerelax) :: pressure
!$OMP threadprivate(pressure)
type transformdata
INTEGER :: Nfp, Ppts, Lallocated, nbfield(0:1)
REAL :: Ra, Za, R, Z, phi, iota, odetol
REAL , allocatable :: RZ(:,:)
end type transformdata
type(transformdata) :: transform
!$OMP threadprivate(transform)
type bnormal
INTEGER :: cmn, Nfp, Mpol, Ntor, mn, Nt, Nz, Ntz
INTEGER, allocatable :: cim(:), cin(:), im(:), in(:)
REAL :: tol, Itor, Gpol
REAL , allocatable :: Rcc(:), Rcs(:), Zcc(:), Zcs(:), gBi(:), gBc(:), gBs(:)
end type bnormal
type(bnormal) :: bn
!$OMP threadprivate(bn)
type rzcoordsdata
INTEGER :: Nfp, Ppts, Lallocated
REAL :: R, Z, odetol
REAL , allocatable :: RZ(:,:)
end type rzcoordsdata
type(rzcoordsdata) :: rzdata
!$OMP threadprivate(rzdata)
type poincaredata
INTEGER :: Nfp, Ppts, idirection, iLyapunov, flparameter
INTEGER :: Lallocated, ipts, nbfield(0:1)
REAL :: phi, R, Z, odetol, Ltol, Lyapunov, phistart
REAL , allocatable :: RZ(:,:), Ly(:)
end type poincaredata
type(poincaredata) :: poincare
!$OMP threadprivate(poincare)
! miscellaneous internal varibles; 25 Mar 15;
! internal copy of "ifail"'s
INTEGER :: ibs00aa
INTEGER :: iga00aa, iec00aa, iho00aa, itr00aa, ipp00aa, ibc00aa, iqf00aa, iad00aa, iaa00aa, iaa00ba
INTEGER :: ibn00aa
#ifdef FH00AA
INTEGER :: ifh00aa
!$OMP threadprivate(ifh00aa)
#endif
INTEGER :: iir00aa, irz00aa
INTEGER :: itangent ! input required for user-supplied subroutine bfield ; 19 Jun 15;
LOGICAL :: Lbfieldok ! logical flag: check user-supplied subroutine bfield was correctly executed; 19 Jun 15;
INTEGER :: nbfield(0:1) ! counts how many times the subroutine bfield is called; 19 Jun 15;
REAL :: actiongradient
INTEGER :: izeta, iteta, fNtor, ipip, niter(0:1)
REAL :: lxx(1:2), lff(1:2) ! user-termination of C05PBF; 19 Jun 15;
!$OMP threadprivate(ibs00aa, iga00aa, iec00aa, iho00aa, itr00aa, ipp00aa, ibc00aa, iqf00aa, iad00aa, iaa00aa, iaa00ba)
!$OMP threadprivate(ibn00aa, iir00aa, irz00aa)
!$OMP threadprivate(itangent, Lbfieldok, nbfield, actiongradient, izeta, iteta, fNtor, ipip, niter, lxx, lff)
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
contains
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
#ifdef HAVE_NAG
subroutine bf00aa( zeta, RZ, BRZ ) ! format constrained by the NAG ode integration routines.
implicit none
INTEGER, parameter :: Node = 6
#else
subroutine bf00aa( Node, zeta, RZ, BRZ ) ! format constrained by the lsode integration routines.
implicit none
INTEGER, intent(in) :: Node
#endif
REAL , intent(in) :: zeta, RZ(1:Node)
REAL , intent(out) :: BRZ(1:Node)
INTEGER :: ifail
REAL :: RpZ(1:3), dBRpZ(1:3,0:3), TM(1:2,1:2)
BRZ(1:Node) = zero ! default intent out; 25 Mar 15;
if( .not.Lbfieldok ) return
RpZ(1:3) = (/ RZ(1), zeta, RZ(2) /)
nbfield(itangent) = nbfield(itangent) + 1 ; ifail = -9 ; call bfield( RpZ(1:3), itangent, dBRpZ(1:3,0:3), ifail )
if( ifail.ne.0 ) then ; Lbfieldok = .false. ; return
endif
if( abs(dBRpZ(2,0)).lt.small ) then
write(0,'("bf00aa : ! bfield ! : ifail.eq.0, but |B^z| < small ; divide-by-zero error ;")')
write(0,'("bf00aa : ! bfield ! : B("es23.15","es23.15","es23.15")=("es23.15","es23.15","es23.15")")') &
RpZ(1:3), dBRpZ(1:3,0)
Lbfieldok = .false. ; return
endif
BRZ(1:2) = (/ dBRpZ(1,0), dBRpZ(3,0) /) / dBRpZ(2,0) ! normalize to toroidal field; 25 Mar 15;
if( itangent.eq.0 ) return
TM(1,1) = ( dBRpZ(1,1) - BRZ(1) * dBRpZ(2,1) ) / dBRpZ(2,0)
TM(1,2) = ( dBRpZ(1,3) - BRZ(1) * dBRpZ(2,3) ) / dBRpZ(2,0)
TM(2,1) = ( dBRpZ(3,1) - BRZ(2) * dBRpZ(2,1) ) / dBRpZ(2,0)
TM(2,2) = ( dBRpZ(3,3) - BRZ(2) * dBRpZ(2,3) ) / dBRpZ(2,0)
BRZ(3) = TM(1,1) * RZ(3) + TM(1,2) * RZ(5) ! tangent map obtained by matrix multiplication;
BRZ(4) = TM(1,1) * RZ(4) + TM(1,2) * RZ(6)
BRZ(5) = TM(2,1) * RZ(3) + TM(2,2) * RZ(5)
BRZ(6) = TM(2,1) * RZ(4) + TM(2,2) * RZ(6)
9999 continue
return
end subroutine bf00aa
#ifdef HAVE_NAG
subroutine bf00ab( zeta, RZ, BRZ ) ! format constrained by the NAG ode integration routines.
implicit none
INTEGER, parameter :: Node = 5
#else
subroutine bf00ab( Node, zeta, RZ, BRZ ) ! format constrained by the lsode integration routines.
implicit none
INTEGER, intent(in) :: Node
#endif
REAL , intent(in) :: zeta, RZ(1:Node)
REAL , intent(out) :: BRZ(1:Node)
INTEGER :: ifail, jfail
REAL :: RpZa(1:3), RpZb(1:3), dBRpZa(1:3,0:3), dBRpZb(1:3,0:3), dR, dZ, length
BRZ(1:Node) = zero ! default intent out; 25 Mar 15;
if( .not.Lbfieldok ) return
RpZa(1:3) = (/ RZ(1), zeta, RZ(2) /)
RpZb(1:3) = (/ RZ(3), zeta, RZ(4) /)
nbfield(itangent) = nbfield(itangent) + 1 ; ifail = -9 ; call bfield( RpZa(1:3), itangent, dBRpZa(1:3,0:3), ifail )
nbfield(itangent) = nbfield(itangent) + 1 ; jfail = -9 ; call bfield( RpZb(1:3), itangent, dBRpZb(1:3,0:3), jfail )
if( ifail.ne.0 .or. jfail.ne.0 ) then ; Lbfieldok = .false. ; return
endif
if( abs(dBRpZa(2,0)).lt.small ) then
write(0,'("bf00ab : ! bfield ! : ifail.eq.0, but |B^z| < small ; divide-by-zero error ;")')
write(0,'("bf00ab : ! bfield ! : B("es23.15","es23.15","es23.15")=("es23.15","es23.15","es23.15")")') &
RpZa(1:3), dBRpZa(1:3,0)
Lbfieldok = .false. ; return
endif
if( abs(dBRpZb(2,0)).lt.small ) then
write(0,'("bf00ab : ! bfield ! : ifail.eq.0, but |B^z| < small ; divide-by-zero error ;")')
write(0,'("bf00ab : ! bfield ! : B("es23.15","es23.15","es23.15")=("es23.15","es23.15","es23.15")")') &
RpZb(1:3), dBRpZb(1:3,0)
Lbfieldok = .false. ; return
endif
BRZ(1:2) = (/ dBRpZa(1,0), dBRpZa(3,0) /) / dBRpZa(2,0) ! normalize to toroidal field; 25 Mar 15;
BRZ(3:4) = (/ dBRpZb(1,0), dBRpZb(3,0) /) / dBRpZb(2,0) ! normalize to toroidal field; 25 Mar 15;
dR = RpZb(1) - RpZa(1)
dZ = RpZb(3) - RpZa(3) ; length = dR*dR + dZ*dZ
FATAL(bf00ab, length.lt.small, divide by zero in bf00ab -- request soft fail)
BRZ(5) = ( dR * ( BRZ(4)-BRZ(2) ) - dZ * ( BRZ(3)-BRZ(1) ) ) / length
9999 continue
return
end subroutine bf00ab
#ifdef HAVE_NAG
subroutine bf00ac( phi, RZ, BRZ ) ! format constrained by the NAG ode integration routines.
implicit none
INTEGER, parameter :: Node = 2
#else
subroutine bf00ac( Node, phi, RZ, BRZ ) ! format constrained by the lsode integration routines.
implicit none
INTEGER, intent(in) :: Node
#endif
REAL , intent(in) :: phi, RZ(1:Node)
REAL , intent(out) :: BRZ(1:Node)
INTEGER :: ibfield
REAL :: RpZ(1:3), dBRpZ(1:3,0:3), Bphi, BB
BRZ(1:Node) = zero ! default intent out; 25 Mar 15;
if( .not.Lbfieldok ) return
nbfield(itangent) = nbfield(itangent) + 1
RpZ(1:3) = (/ RZ(1), phi, RZ(2) /) ; ibfield = -9 ; call bfield( RpZ(1:3), itangent, dBRpZ(1:3,0:3), ibfield )
Bphi = dBRpZ(2,0) ; BB = sqrt( dBRpZ(1,0)**2 + (RpZ(1)*dBRpZ(2,0))**2 + dBRpZ(3,0)**2 )
if( ibfield.ne.0 .or. abs(Bphi).lt.small .or. BB.lt.small ) then ; Lbfieldok = .false. ; return
endif
BRZ(1:2) = (/ dBRpZ(1,0), dBRpZ(3,0) /) / Bphi
return
end subroutine bf00ac
#ifdef HAVE_NAG
subroutine bf00ba( time, RZp, BRZp ) ! format constrained by the NAG ode integration routines.
implicit none
INTEGER, parameter :: Node = 7
#else
subroutine bf00ba( Node, time, RZp, BRZp ) ! format constrained by the lsode integration routines.
implicit none
INTEGER, intent(in) :: Node
#endif
REAL , intent(in) :: time, RZp(1:Node)
REAL , intent(out) :: BRZp(1:Node)
INTEGER :: ifail
REAL :: RpZ(1:3), dBRpZ(1:3,0:3), TM(1:2,1:2), denominator
BRZp(1:Node) = zero ! default intent out; 25 Mar 15;
if( .not.Lbfieldok ) return
RpZ(1:3) = (/ RZp(1), RZp(3), RZp(2) /)
nbfield(itangent) = nbfield(itangent) + 1 ; ifail = -9 ; call bfield( RpZ(1:3), itangent, dBRpZ(1:3,0:3), ifail )
if( ifail.ne.0 ) then ; Lbfieldok = .false. ; return
endif
if( poincare%flparameter.eq.0 ) then ; denominator = dBRpZ(2,0)
else ; denominator = sqrt( dBRpZ(1,0)**2 + (RpZ(1)*dBRpZ(2,0))**2 + dBRpZ(3,0)**2 )
endif
if( abs(denominator).lt.small ) then
write(0,'("bf00ba : ! bfield ! : ifail.eq.0, but |B^z| or |B| < small ; divide-by-zero error ;")')
write(0,'("bf00ba : ! bfield ! : B("es23.15","es23.15","es23.15")=("es23.15","es23.15","es23.15")")') &
RpZ(1:3), dBRpZ(1:3,0)
Lbfieldok = .false. ; return
endif
BRZp(1:3) = (/ dBRpZ(1,0), dBRpZ(3,0), dBRpZ(2,0) /) / denominator
if( itangent.eq.0 ) return
TM(1,1) = ( dBRpZ(1,1) - BRZp(1) * dBRpZ(2,1) ) / dBRpZ(2,0)
TM(1,2) = ( dBRpZ(1,3) - BRZp(1) * dBRpZ(2,3) ) / dBRpZ(2,0)
TM(2,1) = ( dBRpZ(3,1) - BRZp(2) * dBRpZ(2,1) ) / dBRpZ(2,0)
TM(2,2) = ( dBRpZ(3,3) - BRZp(2) * dBRpZ(2,3) ) / dBRpZ(2,0)
BRZp(4) = TM(1,1) * RZp(4) + TM(1,2) * RZp(6) ! tangent map obtained by matrix multiplication;
BRZp(5) = TM(1,1) * RZp(5) + TM(1,2) * RZp(7) ! only correct if \phi is integration parameter;
BRZp(6) = TM(2,1) * RZp(4) + TM(2,2) * RZp(6)
BRZp(7) = TM(2,1) * RZp(5) + TM(2,2) * RZp(7)
9999 continue
return
end subroutine bf00ba
#ifdef HAVE_NAG
subroutine pf00aa( zeta, st, Bst ) ! format constrained by the NAG ode integration routines.
implicit none
INTEGER, parameter :: Node = 6
#else
subroutine pf00aa( Node, zeta, st, Bst ) ! format constrained by the lsode integration routines.
implicit none
INTEGER, intent(in) :: Node
#endif
REAL , intent(in) :: zeta, st(1:Node)
REAL , intent(out) :: Bst(1:Node)
INTEGER :: ifail, Lderiv, id
REAL :: stz(1:3), dRpZ(1:3,0:3,0:3), sqrtg, dBRpZ(1:3,0:3), dBstz(1:3,0:3)
REAL :: det, VM(1:3,1:3), TM(1:2,1:2), ddet, DM(1:3,1:3), DB(1:3,1:3)
Bst(1:Node) = zero ; stz(1:3) = zero ; dRpZ(1:3,0:3,0:3) = zero ; dBRpZ(1:3,0:3) = zero ; dBstz(1:3,0:3) = zero
if( .not.Lbfieldok ) goto 9999
Lderiv = itangent + 1 ; stz(1:3) = (/ st(1), st(2), zeta /)
call bc00ab( rzmn, stz(1:3), Lderiv, dRpZ(1:3,0:3,0:3), sqrtg, ifail )
if( ifail.ne.0 ) then ; Lbfieldok = .false. ; goto 9999
endif
nbfield(itangent) = nbfield(itangent) + 1 ; ifail = -9
call bfield( dRpZ(1:3,0,0), itangent, dBRpZ(1:3,0:3), ifail )
if( ifail.ne.0 ) then ; Lbfieldok = .false. ; goto 9999
endif
if( abs(dBRpZ(2,0)).lt.small ) then
write(0,'("pf00aa : ! bfield ! : ifail.eq.0, but |B^z| < small ; divide-by-zero error ;")')
write(0,'("pf00aa : ! bfield ! : B("es23.15","es23.15","es23.15")=("es23.15","es23.15","es23.15")")') &
dRpZ(1:3,0,0), dBRpZ(1:3,0)
Lbfieldok = .false. ; goto 9999
endif
VM(1,1:3) = (/ - dRpZ(3,2, 0), dRpZ(1,3, 0) * dRpZ(3,2, 0) - dRpZ(1,2, 0) * dRpZ(3,3, 0), + dRpZ(1,2, 0) /)
VM(2,1:3) = (/ + dRpZ(3,1, 0), dRpZ(1,1, 0) * dRpZ(3,3, 0) - dRpZ(1,3, 0) * dRpZ(3,1, 0), - dRpZ(1,1, 0) /)
VM(3,1:3) = (/ zero , dRpZ(1,2, 0) * dRpZ(3,1, 0) - dRpZ(1,1, 0) * dRpZ(3,2, 0), zero /)
dBstz(1:3,0) = matmul( VM(1:3,1:3), dBRpZ(1:3,0) )
Bst(1:2) = (/ dBstz(1,0), dBstz(2,0) /) / dBstz(3,0)
Bst(1) = Bst(1) - actiongradient
if( itangent.eq.0 ) goto 9999
DB(1,1:3) = (/ dBRpZ(1,1), dBRpZ(1,2), dBRpZ(1,3) /)
DB(2,1:3) = (/ dBRpZ(2,1), dBRpZ(2,2), dBRpZ(2,3) /)
DB(3,1:3) = (/ dBRpZ(3,1), dBRpZ(3,2), dBRpZ(3,3) /)
do id = 1, 3
DM(1,1:3) = (/ - dRpZ(3,2,id), dRpZ(1,3,id) * dRpZ(3,2, 0) - dRpZ(1,2,id) * dRpZ(3,3, 0) &
+ dRpZ(1,3, 0) * dRpZ(3,2,id) - dRpZ(1,2, 0) * dRpZ(3,3,id), + dRpZ(1,2,id) /)
DM(2,1:3) = (/ + dRpZ(3,1,id), dRpZ(1,1,id) * dRpZ(3,3, 0) - dRpZ(1,3,id) * dRpZ(3,1, 0) &
+ dRpZ(1,1, 0) * dRpZ(3,3,id) - dRpZ(1,3, 0) * dRpZ(3,1,id), - dRpZ(1,1,id) /)
DM(3,1:3) = (/ zero , dRpZ(1,2,id) * dRpZ(3,1, 0) - dRpZ(1,1,id) * dRpZ(3,2, 0) &
+ dRpZ(1,2, 0) * dRpZ(3,1,id) - dRpZ(1,1, 0) * dRpZ(3,2,id), zero /)
dBstz(1:3,id) = matmul( DM(1:3,1:3), dBRpZ(1:3,0) ) + matmul( VM(1:3,1:3), matmul( DB(1:3,1:3), dRpZ(1:3,id,0) ) )
enddo
TM(1,1) = ( dBstz(1,1) - dBstz(1,0) * dBstz(3,1) / dBstz(3,0) ) / dBstz(3,0)
TM(1,2) = ( dBstz(1,2) - dBstz(1,0) * dBstz(3,2) / dBstz(3,0) ) / dBstz(3,0)
TM(2,1) = ( dBstz(2,1) - dBstz(2,0) * dBstz(3,1) / dBstz(3,0) ) / dBstz(3,0)
TM(2,2) = ( dBstz(2,2) - dBstz(2,0) * dBstz(3,2) / dBstz(3,0) ) / dBstz(3,0)
Bst(3) = TM(1,1) * st(3) + TM(1,2) * st(5) ! tangent map obtained by matrix multiplication;
Bst(4) = TM(1,1) * st(4) + TM(1,2) * st(6) - one
Bst(5) = TM(2,1) * st(3) + TM(2,2) * st(5)
Bst(6) = TM(2,1) * st(4) + TM(2,2) * st(6)
9999 continue
! write(0,'("pf00aa : "2x" : stz="3es13.5" ; RpZ="3es13.5" ; BRpZ="3es13.5" ; Bstz="3es13.5" ;")') &
! stz(1:3), dRpZ(1:3,0,0), dBRpZ(1:3,0), Bstz(1:3)
return
end subroutine pf00aa
!latex \newpage \section{Biot-Savart subroutines}
!latex In this section are described subroutines for computing the magnetic field produced by a current distribution.
!latex \subroutine{bs00aa}{compute the magnetic field produced by a filamentary current loop of arbitrary shape;}
!latex \bi
!latex \item[1.] Given a closed (i.e. periodic), one-dimensional loop embedded in three-dimensional space,
!latex with position described by $\bar {\bf x}(t) \equiv \bar x(t) {\bf i} + \bar y(t) {\bf j} + \bar z(t) {\bf k}$,
!latex with the arbitrary curve parameter $t\in[0,2\pi]$, and $\bar {\bf x}(t+2\pi) = \bar {\bf x}(t)$,
!latex assumed to carry unit current, i.e. $I=1$,
!latex the magnetic field at ${\bf x} \equiv x {\bf i} + y {\bf j} + z {\bf k}$ is given by the Biot-Savart integral,
!latex \be {\bf B} \equiv \int_{\cal C} \frac{d{\bf l}\times {\bf r}}{r^3}, \label{eq:BiotSavart}
!latex \ee
!latex where ${\bf r}\equiv {\bf x}-\bar{\bf x}$.
!latex \item[2.] In component form, \Eqn{BiotSavart} is
!latex \be B^x & \equiv & \int_0^{2\pi} \frac{\dot {\bar y} (z-\bar z) - \dot {\bar z} (y-\bar y) }{r^3} dt, \\
!latex B^y & \equiv & \int_0^{2\pi} \frac{\dot {\bar z} (x-\bar x) - \dot {\bar x} (z-\bar z) }{r^3} dt, \\
!latex B^z & \equiv & \int_0^{2\pi} \frac{\dot {\bar x} (y-\bar y) - \dot {\bar y} (x-\bar x) }{r^3} dt,
!latex \ee
!latex where $\dot {\bar x} \equiv d\bar x/dt$, etc.
!latex \item[3.] The magnetic vector potential is
!latex \be {\bf A} \equiv \int_{\cal C} \frac{d{\bf l}}{r}. \label{eq:BiotSavartA}
!latex \ee
!latex \item[4.] The total length of the curve is
!latex \be L \equiv \int_{\cal C} d l , \label{eq:BiotSavartL}
!latex \ee
!latex where $dl \equiv (\dot {\bar x}^2 + \dot {\bar y}^2 + \dot {\bar z}^2 )^{1/2}$.
!latex \item[5.] The user must supply a subroutine, \verb+iccoil+, that returns $\bar x$, $\bar y$ \& $\bar z$,
!latex and $d \bar x /dt$, $d \bar y /dt$ \& $d \bar z /dt$, given $t$:
!latex \verb+subroutine iccoil( t, x(0:1), y(0:1), z(0:1), ifail )+ \\
!latex where \verb+t+, \verb+x(0:1)+, \verb+y(0:1)+ and \verb+z(0:1)+ are real and ifail is an integer,
!latex \verb+x(0)+$\equiv \bar x(t)$ and \verb+x(1)+$\equiv \dot {\bar x}(t)$, and similarly for $y$ and $z$.
!latex \item[6.] The integration is performed using \nag{www.nag.co.uk/numeric/FL/manual19/pdf/D01/d01ajf_fl19.pdf}{D01AJF}.
!latex (This routine is based upon the {\footnotesize QUADPACK} routine QAGS, which is freely available.)
!latex \item[7.] \underline{\bf The user must include} \\ \\
!latex \verb+use oculus, only : biotsavart, bs00aa+ \\ \\
!latex \verb+type(biotsavart) :: bsfield+ \\ \\ in their source that calls \verb+bs00aa+,
!latex where \verb+biotsavart+ is a derived type (i.e. structure) that contains both the required input and output information.
!latex The variable name, \verb+bsfield+, is arbitrary.
!latex \item[8.] \underline{\bf Required inputs}
!latex \item[ ] \verb+bsfield%x : real ;+
!latex \item[ ] \verb+bsfield%y : real ;+
!latex \item[ ] \verb+bsfield%z : real ;+
!latex \bi
!latex \item[i.] position ${\bf x} \equiv x \, {\bf i} + y \, {\bf j} + z \, {\bf k}$ at which magnetic field is required;
!latex \ei
!latex \item[ ] \verb+bsfield%tol : real ;+
!latex \item[ ] \verb+bsfield%N : integer ;+
!latex \bi
!latex \item[i.] integration accuracy parameters provided to \verb+D01AJF+; (\verb+EPSABS=tol+, \verb+EPSREL=zero+ and \verb+LW=4*N+);
!latex \ei
!latex \item[ ] \verb+bsfield%LB : logical ;+
!latex \bi
!latex \item[i.] set \verb+LB = .true.+ to compute the magnetic field;
!latex \ei
!latex \item[ ] \verb+bsfield%LA : logical ;+
!latex \bi
!latex \item[i.] set \verb+LA = .true.+ to compute the magnetic vector potential;
!latex \ei
!latex \item[ ] \verb+bsfield%LL : logical ;+
!latex \bi
!latex \item[i.] set \verb+LL = .true.+ to compute the length of the curve;
!latex \ei
!latex \item[9.] \underline{\bf Execution}
!latex \item[ ] \verb+call bs00aa( bsfield, ifail )+
!latex \item[10.] \underline{\bf Outputs}
!latex \item[ ] \verb+bsfield%Bx : real ;+
!latex \item[ ] \verb+bsfield%By : real ;+
!latex \item[ ] \verb+bsfield%Bz : real ;+
!latex \bi
!latex \item[i.] only if \verb+LB = .true.+;
!latex \ei
!latex \item[ ] \verb+bsfield%Ax : real ;+
!latex \item[ ] \verb+bsfield%Ay : real ;+
!latex \item[ ] \verb+bsfield%Az : real ;+
!latex \bi
!latex \item[i.] only if \verb+LA = .true.+;
!latex \ei
!latex \item[ ] \verb+bsfield%length : real ;+
!latex \bi
!latex \item[i.] only if \verb+LL = .true.+;
!latex \ei
!latex \item[ ] \verb+ifail : integer ;+
!latex \bi
!l!tex \item[i.] on output:
!latex \item[] \verb+ifail=0+ : normal execution;
!latex \item[] \verb+ifail=1+ : input error;
!latex \item[] \verb+ifail=2+ : \verb+D01AJF+ encountered a divide-by-zero;
!latex this is only possible if $\exists t \in [0,2\pi]$ such that ${\bf x}=\bar{\bf x}(t)$.
!latex \ei
!latex \ei
subroutine bs00aa( lbsfield, ifail )
implicit none
type(biotsavart) :: lbsfield
INTEGER :: ifail
INTEGER :: ixyz, id01ajf, LRwork, LIwork, astat
INTEGER, allocatable :: Iwork(:)
REAL :: lowlimit, upplimit, epsabs, epsrel
REAL :: abserrbx, abserrby, abserrbz, abserrax, abserray, abserraz, abserr
REAL , allocatable :: Rwork(:)
#ifndef HAVE_NAG
INTEGER :: neval, last
#endif
ibs00aa = ifail
CHECKINPUT( bs00aa, lbsfield%tol.le.small, 9999 )
CHECKINPUT( bs00aa, lbsfield%N .le. 0, 9999 )
bsfield%x = lbsfield%x
bsfield%y = lbsfield%y
bsfield%z = lbsfield%z
lbsfield%Bx = zero
lbsfield%By = zero
lbsfield%Bz = zero
lbsfield%Ax = zero
lbsfield%Ay = zero
lbsfield%Az = zero
lbsfield%length = zero
abserrbx = - one ; abserrby = - one ; abserrbz = - one
abserrax = - one ; abserray = - one ; abserraz = - one
abserr = - one
LRwork = lbsfield%N * 4
SALLOCATE(Rwork,(1:LRwork),zero)
LIwork = lbsfield%N
SALLOCATE(Iwork,(1:LIwork),zero)
do ixyz = 1, 3
bsfield%ixyz = ixyz
if( lbsfield%LB ) then ! compute magnetic field; 09 Nov 15;
id01ajf = 1 ; lowlimit = zero ; upplimit = pi2 ; epsabs = lbsfield%tol ; epsrel = zero
select case( ixyz)
#ifdef HAVE_NAG
case( 1 )
call D01AJF( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%Bx, abserrbx, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
case( 2 )
call D01AJF( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%By, abserrby, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
case( 3 )
call D01AJF( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%Bz, abserrbz, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
#else
case( 1 )
call DQAGS( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%Bx, abserrbx, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
case( 2 )
call DQAGS( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%By, abserrby, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
case( 3 )
call DQAGS( bs00bx, lowlimit, upplimit, epsabs, epsrel, lbsfield%Bz, abserrbz, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
#endif
end select
if( id01ajf.ne.0 ) ibs00aa = 2
endif
if( lbsfield%LA ) then ! compute magnetic vector potential; 09 Nov 15;
id01ajf = 1 ; lowlimit = zero ; upplimit = pi2 ; epsabs = lbsfield%tol ; epsrel = zero
select case( ixyz)
#ifdef HAVE_NAG
case( 1 )
call D01AJF( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Ax, abserrax, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
case( 2 )
call D01AJF( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Ay, abserray, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
case( 3 )
call D01AJF( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Az, abserraz, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
#else
case( 1 )
call DQAGS( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Ax, abserrbx, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
case( 2 )
call DQAGS( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Ay, abserrby, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
case( 3 )
call DQAGS( bs00ax, lowlimit, upplimit, epsabs, epsrel, lbsfield%Az, abserrbz, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
#endif
end select
if( id01ajf.ne.0 ) ibs00aa = 2
endif
enddo ! end of do ixyz; 13 Oct 15;
if( lbsfield%LL ) then ! compute length; 09 Nov 15;
lowlimit = zero ; upplimit = pi2 ; epsabs = lbsfield%tol ; epsrel = zero
#ifdef HAVE_NAG
id01ajf = 1
call D01AJF( bs00lx, lowlimit, upplimit, epsabs, epsrel, lbsfield%length, abserr, Rwork(1:LRwork), LRwork, &
Iwork(1:LIwork), LIwork, id01ajf )
#else
call DQAGS( bs00lx, lowlimit, upplimit, epsabs, epsrel, lbsfield%length, abserr, neval, id01ajf, LIwork, LRwork, &
last, Iwork(1:LIwork), Rwork(1:LRwork) )
#endif
if( id01ajf.ne.0 ) ibs00aa = 2
endif
DALLOCATE(Iwork)
DALLOCATE(Rwork)
! lbsfield%its = bsfield%its
!ibs00aa = 0 ! success; 02 Jun 15;
9999 continue
if( ibs00aa.ne. 0 ) write(0,9000) ibs00aa, lbsfield%tol, lbsfield%x, lbsfield%y, lbsfield%z, &
abserrbx, abserrby, abserrbz
9000 format("bs00aa : d01ajf "i3" : tol="es9.1" ; (x,y,z) = ("f23.15" ,"f23.15" ,"f23.15" ) ; " &
:"dB(x,y,z) = ("es23.15" ,"es23.15" ,"es23.15" ) ; "i9" ; ")
ifail = ibs00aa
return
end subroutine bs00aa
REAL function bs00bx( tt )
implicit none
REAL :: tt
REAL :: xx(0:1), yy(0:1), zz(0:1), dx, dy, dz, rr, r3
INTEGER :: ifail
ifail = 0
call iccoil( tt, xx(0:1), yy(0:1), zz(0:1), ifail )
dx = xx(0) - bsfield%x
dy = yy(0) - bsfield%y
dz = zz(0) - bsfield%z
rr = sqrt( dx**2 + dy**2 + dz**2 ) ; r3 = rr**3
if( rr.lt.small ) then ; ibs00aa = 2 ; bs00bx = ten**6 ; return
endif
select case( bsfield%ixyz)
case( 1 ) ; bs00bx = ( dy * zz(1) - dz * yy(1) ) / r3
case( 2 ) ; bs00bx = ( dz * xx(1) - dx * zz(1) ) / r3
case( 3 ) ; bs00bx = ( dx * yy(1) - dy * xx(1) ) / r3
end select
! bsfield%its = bsfield%its + 1
return
end function bs00bx
REAL function bs00ax( tt )
implicit none
REAL :: tt
REAL :: xx(0:1), yy(0:1), zz(0:1), dx, dy, dz, rr
INTEGER :: ifail
ifail = 0
call iccoil( tt, xx(0:1), yy(0:1), zz(0:1), ifail )
dx = xx(0) - bsfield%x
dy = yy(0) - bsfield%y
dz = zz(0) - bsfield%z
rr = sqrt( dx**2 + dy**2 + dz**2 )
if( rr.lt.small ) then ; ibs00aa = 2 ; bs00ax = ten**6 ; return
endif
select case( bsfield%ixyz)
case( 1 ) ; bs00ax = xx(1) / rr
case( 2 ) ; bs00ax = yy(1) / rr
case( 3 ) ; bs00ax = zz(1) / rr
end select
! bsfield%its = bsfield%its + 1
return
end function bs00ax
REAL function bs00lx( tt )
implicit none
REAL :: tt
REAL :: xx(0:1), yy(0:1), zz(0:1)
INTEGER :: ifail
ifail = 0
call iccoil( tt, xx(0:1), yy(0:1), zz(0:1), ifail )
bs00lx = sqrt( xx(1)**2 + yy(1)**2 + zz(1)**2 )
return
end function bs00lx
!latex \newpage \section{``cylindrical'' subroutines}
!latex In this section are described subroutines that do not depend explicitly on a pre-defined, `background', toroidal coordinate framework.
!latex \subroutine{ga00aa}{find the magnetic axis;}
!latex \bi
!latex \item[1.] Iterative fieldline tracing methods are used to find the magnetic axis,
!latex defined as the magnetic fieldline that closes on itself after a toroidal distance of $\Delta \p = 2\pi$/\verb+Nfp+,
!latex i.e. ${\bf x}(\Delta\p)={\bf x}(0)$,
!latex where \verb+Nfp+ is the field periodicity.
!latex \item[ *] The fieldline mapping is defined by integrating along the magnetic field,
!latex and is constructed numerically in cylindrical coordinates by integrating the o.d.e.'s
!latex \be \frac{dR(\p)}{d\p} & = & \frac{B^R(R,\p,Z)}{B^\p(R,\p,Z)} \equiv \dot R(R,\p,Z), \label{eq:BR} \\
!latex \frac{dZ(\p)}{d\p} & = & \frac{B^Z(R,\p,Z)}{B^\p(R,\p,Z)} \equiv \dot Z(R,\p,Z), \label{eq:BZ}
!latex \ee
!latex from an initial, user-supplied starting point, $(R_0,0,Z_0)$.
!latex The toroidal angle, $\p$, is used as the integration parameter, and so $B^\p$ cannot be zero.
!latex Upon request, this routine will be modified in order to follow field lines in regions where $B^\p=0$.
!latex \item[ *] A Newton-iterative method is used to find the zero of
!latex \be {\bf f}\left(\begin{array}{c}R_0\\Z_0\end{array}\right) \equiv \left(\begin{array}{c}R_1-R_0\\Z_1-Z_0\end{array}\right)
!latex \ee
!latex where $R_1 \equiv R(\Delta\p)$ and $Z_1 \equiv Z(\Delta\p)$.
!latex \item[ *] Given an initial guess, ${\bf x}\equiv(R_0, Z_0)^T$,
!latex a better guess for the location of the axis, $(R_0,Z_0)^T+(\delta R,\delta Z)^T$, is given by the linear approximation
!latex \be {\bf f}\left(\begin{array}{c}R_0+\delta R_0\\Z_0+\delta Z_0\end{array}\right) = {\bf f}\left(\begin{array}{c}R_0\\Z_0\end{array}\right)
!latex + \underbrace{
!latex \left( \begin{array}{lcl}\partial_{R_0}R_1 -1&, & \partial_{Z_0}R_1 \\
!latex \partial_{R_0}Z_1&, & \partial_{Z_0}Z_1-1\end{array} \right)}_{\nabla {\bf f}} \cdot
!latex \left(\begin{array}{c}\delta R_0\\ \delta Z_0\end{array}\right) + {\cal O}(\delta^2) = 0,
!latex \ee
!latex and the correction is given by $\delta{\bf x} = - (\nabla {\bf f})^{-1} \cdot {\bf f}({\bf x})$.
!latex \item[ *] The derivatives, $\partial_{R_0}R_1$, $\partial_{Z_0}R_1$, etc. are determined by fieldline integration,
!latex \be \frac{d}{d\p} \left( \begin{array}{cc}\partial_{R_0}R(\p), & \partial_{Z_0}R(\p) \\
!latex \partial_{R_0}Z(\p), & \partial_{Z_0}Z(\p) \end{array}\right) =
!latex \left( \begin{array}{cc}\partial_{R }\dot R, & \partial_{Z }\dot R\\
!latex \partial_{R }\dot Z, & \partial_{Z }\dot Z \end{array}\right) \cdot
!latex \left( \begin{array}{cc}\partial_{R_0}R(\p), & \partial_{Z_0}R(\p) \\
!latex \partial_{R_0}Z(\p), & \partial_{Z_0}Z(\p) \end{array}\right),
!latex \ee
!latex from an initial starting point being the identity matrix,
!latex \be \left( \begin{array}{cc}\partial_{R_0}R( 0), & \partial_{Z_0}R( 0) \\ \partial_{R_0}Z( 0), & \partial_{Z_0}Z( 0)
!latex \end{array}\right) =
!latex \left( \begin{array}{cc} 1, & 0 \\ 0,& 1
!latex \end{array}\right).
!latex \ee
!l!tex i.e. $\partial_{R_0}R_1(0)=1$, $\partial_{Z_0}R_1(0)=0$, $\partial_{R_0}Z_1(0)=0$ and $\partial_{Z_0}Z_1(0)=1$.
!latex \item[ *] The iterative search is enabled by \nag{http://www.nag.co.uk/numeric/FL/manual19/pdf/C05/c05pbf_fl19.pdf}{C05PBF}.
!latex \item[ *] If \verb+ifail=-4+, then instead the axis search is provided by
!latex \nag{http://www.nag.co.uk/numeric/FL/manual19/pdf/C05/c05nbf_fl19.pdf}{C05NBF}, which does not require derivatives.
!latex Note that for this option, the tangent map, the transform on axis, and the residue will not be calculated.
!latex \item[ *] The above definition of the magnetic axis does not have a unique solution:
!latex an $\iotabar=1/1$ fieldline also satisfies this definition,
!latex as does the $\iotabar=2/1$, $3/1$, etc., as also does the ``X'' point at the separatrix.
!latex Furthermore, during a sawteeth cycle, the $\iotabar=1/1$ fieldline and the original magnetic axis swap places.
!latex If there is a continuous family of ``magnetic axes", e.g. there exists an intact $q=1$ surface,
!latex then $\nabla {\bf f}$ will not be invertible (unless singular value decomposition methods are used).
!latex Thus, this routine should be used with care.
!latex Which closed field line that \verb+ga00aa+ locates is determined by the initial guess provided.
!l!tex Only the ``true'' magnetic axis should be located with \verb+ga00aa+.
!l!tex Frequently, the coordinate harmonics of the magnetic axis returned by \verb+ga00aa+
!l!tex will be used as the coordinate axis of toroidal ``chaotic'' coordinates.
!l!tex Other routines will be provided for locating the $X$ point etc.
!l!tex \item[ *] The definition of the ``true'' magnetic axis may be made precise by examining the Fourier harmonic content of the closed fieldline,
!l!tex and defining the true magnetic axis may be that closed fieldline with minimal Fourier content.
!latex \item[ *] The returned information includes:
!latex \bi
!latex \item[i.] the Fourier representation of $R(\p)$ and $Z(\p)$;
!latex \item[ii.] the tangent-mapping near the axis, which allows the rotational-transform on axis to be determined;
!latex \item[iii.] Greene's residue [Greene, \link{dx.doi.org/10.1063/1.524170}{J. Math. Phys. 20, 1183 (1979)}]
!latex calculated at the magnetic axis (determines stability).
!latex \ei
!latex \item[2.] \underline{\bf The user must include} \\ \\
!latex \verb+use oculus, only : magneticaxis, ga00aa+ \\ \\
!latex \verb+type(magneticaxis) :: axis+ \\ \\ in their source that calls \verb+ga00aa+,
!latex where \verb+axis+ is a derived type (i.e. structure) that contains both the required input and output information.
!latex The variable name, \verb+axis+, is arbitrary.
!latex \item[3.] \underline{\bf Required inputs}
!latex \item[ ] \verb+axis%Nfp : integer ;+
!latex \bi
!latex \item[i.] the toroidal periodicity of the magnetic field, e.g. \verb+Nfp=1+;
!latex \ei
!latex \item[ ] \verb+axis%Ntor : integer ;+
!latex \bi
!latex \item[i.] the desired Fourier resolution of the magnetic axis,
!latex \item[ii.] if it is not required to have a Fourier decomposition of the magnetic axis, or the magnetic field is axisymmetric, choose \verb+Ntor=0+;
!latex \ei
!latex \item[ ] \verb+axis%R : real ;+
!latex \bi
!latex \item[i.] guess for the $R$ location of the magnetic axis on the $\phi=0$ plane;
!latex \ei
!latex \item[ ] \verb+axis%Z : real ;+
!latex \bi
!latex \item[i.] guess for the $Z$ location of the magnetic axis on the $\phi=0$ plane;
!latex \ei
!latex \item[ ] \verb+axis%maxits : integer ;+
!latex \bi
!latex \item[i.] max. iterations allowed in search;
!latex \item[ii.] e.g. \verb+maxits=16+;
!latex \ei
!latex \item[ ] \verb+axis%tol : real ;+
!latex \bi
!latex \item[i.] required accuracy to which the position of the magnetic axis on the $\p=0$ plane is required
!latex \item[ii.] e.g. \verb+tol=1.0e-06+;
!latex \ei
!latex \item[ ] \verb+axis%odetol : real ;+
!latex \bi
!latex \item[i.] o.d.e. integration tolerance;
!latex \item[ii.] e.g. \verb+odetol=1.0e-08+;
!latex \ei
!latex \item[ ] \verb+ifail : integer ;+
!latex \bi
!latex \item[i.] if \verb+ifail.ge. 1+ : there is no screen output, {\em except} if there is an input error;
!latex \item[ii.] if \verb+ifail.le. 0+ : a one-line summary is provided, giving the $(R,Z)$ location of the axis, $\iotabar_{axis}$, etc.
!latex \item[iii.] if \verb+ifail.le.-1+ : the Fourier harmonics of the magnetic axis are displayed;
!latex \item[iv.] if \verb+ifail.le.-2+ : the eigenvalues and eigenvectors of the tangent map at the axis are displayed;
!latex \item[iv.] if \verb+ifail.le.-3+ : information detailing the progress of the iterative search is provided;
!latex \item[iv.] if \verb+ifail.le.-4+ : C05NBF (which does not require derivatives) is used instead of C05PBF;
!latex \ei
!latex \item[4.] \underline{\bf Execution}
!latex \item[ ] \verb+call ga00aa( axis, ifail )+
!latex \item[5.] \underline{\bf Outputs}
!latex \item[ ] \verb+axis%R : real ;+
!latex \bi
!latex \item[i.] updated;
!latex \ei
!latex \item[ ] \verb+axis%Z : real ;+
!latex \bi
!latex \item[i.] updated;
!latex \ei
!latex \item[ ] \verb+axis%tangent(1:2,1:2) : real ;+
!latex \bi
!latex \item[i.] the tangent mapping at axis;
!latex \item[ii.] if the eigenvalues of the tangent map are imaginary, e.g. $\lambda\equiv\alpha+\beta i$,
!latex then the rotational-transform on axis satisfies $\tan(||\iotabar||)=\beta/\alpha$, where $||\iotabar||\equiv\iotabar \mod 2\pi$.
!latex \item[iii.] if the eigenvalues of the tangent map are real, then the eigenvalues give the direction of the stable and unstable manifolds.
!latex \ei
!latex \item[ ] \verb+axis%wr(1:2) : real ;+
!latex \item[ ] \verb+axis%wi(1:2) : real ;+
!latex \item[ ] \verb+axis%vr(1:2,1:2) : real ;+
!latex \item[ ] \verb+axis%vi(1:2,1:2) : real ;+
!latex \bi
!latex \item[i.] the eigenvalues and eigenvectors of the tangent mapping at axis;
!latex \ei
!latex \item[ ] \verb+axis%iota : real ;+
!latex \bi
!latex \item[i.] rotational-transform on axis
!latex \item[ii.] will only be meaningful if axis is stable, which is indicated by both (i) the sign of residue,
!latex and (ii) whether the eigenvalues and eigenvectors are real or imaginary.
!latex \ei
!latex \item[ ] \verb+axis%Lallocated : integer ;+
!latex \bi
!latex \item[i.] if \verb+Lallocated=1+, the \verb+Ri+, \verb+Zi+, \verb+Rnc+, \verb+Rns+, \verb+Zns+, \verb+Znc+ have been allocated;
!latex \item[ii.] if \verb+Lallocated=0+, the \verb+Ri+, \verb+Zi+, \verb+Rnc+, \verb+Rns+, \verb+Zns+, \verb+Znc+ have {\bf not} been allocated;
!latex \ei
!latex \item[ ] \verb+axis%Ri(0:4*Ntor) : real ;+
!latex \bi
!latex \item[i.] the magnetic axis, $R(i\Delta\varphi)$, for $i=0,4*$\verb+Ntor+, where $\Delta\varphi=\Delta\p/(4*$\verb+Ntor+$)$;
!latex \item[ii.] \verb+Ri+ is allocated internally; if on input \verb+Ri+ is already allocated it will first be deallocated; similarly for \verb+Zi+
!latex \ei
!latex \item[ ] \verb+axis%Zi(0:4*Ntor) : real ;+
!latex \bi
!latex \item[i.] the magnetic axis, $Z(i\Delta\varphi)$, for $i=0,4*$\verb+Ntor+, where $\Delta\varphi=\Delta\p/(4*$\verb+Ntor+$)$;
!latex \ei
!latex \item[ ] \verb+axis%Rnc(0:Ntor) : real ;+
!latex \bi
!latex \item[i.] the Fourier harmonics, $R(\phi)=\sum_n [R_{n,c} \cos(-n\phi)+R_{n,s} \sin(-n\phi)]$;
!latex \item[ii.] \verb+Rnc+ is allocated internally; if on input \verb+Rnc+ is already allocated it will first be deallocated;
!latex similarly for \verb+Zns+, \verb+Rns+ and \verb+Znc+.