-
Notifications
You must be signed in to change notification settings - Fork 0
/
abscab.f90
2422 lines (2020 loc) · 93.7 KB
/
abscab.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module abscab
use mod_cel
use mod_compsum
use, intrinsic :: ieee_arithmetic, only: IEEE_VALUE, IEEE_SIGNALING_NAN
implicit none
!> vacuum magnetic permeability in Vs/Am (CODATA-2018)
real(wp), parameter :: MU_0 = 1.25663706212e-6_wp
!> vacuum magnetic permeability, divided by pi
real(wp), parameter :: MU_0_BY_PI = MU_0 / PI
!> vacuum magnetic permeability, divided by 2 pi
real(wp), parameter :: MU_0_BY_2_PI = MU_0 / (2.0_wp * PI)
!> vacuum magnetic permeability, divided by 4 pi
real(wp), parameter :: MU_0_BY_4_PI = MU_0 / (4.0_wp * PI)
contains
!------------ A_z of straight wire segment
!> Compute the normalized axial component of magnetic vector potential of straight wire segment,
!> evaluated along axis of wire segment (rho = 0).
!> This is a special case for points away from the wire ("far-field") for zP < -1 or zP >= 2.
!>
!> @param zP normalized axial coordinate of evaluation location; must not be in [0, 1] (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_ax_f(zP)
real(wp) :: sws_A_z_ax_f
real(wp) :: zP
sws_A_z_ax_f = atanh(1.0_wp / (abs(zP) + abs(1.0_wp - zP)))
end function ! sws_A_z_ax_f
!> Compute the normalized axial component of magnetic vector potential of straight wire segment,
!> evaluated along axis of wire segment (rhoP = 0).
!> This is a special case for points close to the wire ("near-field") for -1 <= zP < 2.
!>
!> @param zP normalized axial coordinate of evaluation location; must not be in [0, 1] (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_ax_n(zP)
real(wp) :: sws_A_z_ax_n
real(wp) :: zP
! Two negative signs must be able to cancel each other here!
sws_A_z_ax_n = sign(1.0_wp, zP) * log(zP / (zP - 1.0_wp)) / 2.0_wp
end function ! sws_A_z_ax_n
!> Compute the normalized axial component of magnetic vector potential of straight wire segment,
!> evaluated along axis of wire segment (rho = 0).
!>
!> @param zP normalized axial coordinate of evaluation location; must not be in [0, 1] (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_ax(zP)
real(wp) :: sws_A_z_ax
real(wp) :: zP
if (zP .lt. -1.0_wp .or. zP .ge. 2.0_wp) then
sws_A_z_ax = sws_A_z_ax_f(zP)
else
sws_A_z_ax = sws_A_z_ax_n(zP)
end if
end function ! sws_A_z_ax
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment,
!> evaluated radially along the endpoints of the wire segment (zP = 0 or zP = 1).
!> This is a special case for points away from the wire ("far-field") for rhoP > 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location; must not be zero (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_rad_f(rhoP)
real(wp) :: sws_A_z_rad_f
real(wp) :: rhoP
sws_A_z_rad_f = atanh(1.0_wp / (rhoP + hypot(rhoP, 1.0_wp)))
end function ! sws_A_z_rad_f
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment,
!> evaluated radially along the endpoints of the wire segment (zP = 0 or zP = 1).
!> This is a special case for points close to the wire ("near-field") for rhoP <= 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location; must not be zero (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_rad_n(rhoP)
real(wp) :: sws_A_z_rad_n
real(wp) :: rhoP
real(wp) :: cat, sat, rc, num, den
cat = 1.0_wp / hypot(rhoP, 1.0_wp) ! cos(atan(...) )
sat = sin(atan(rhoP) / 2.0_wp) ! sin(atan(...)/2)
rc = rhoP * cat
num = rc + 1.0_wp + cat
den = rc + 2.0_wp * sat * sat
sws_A_z_rad_n = log(num / den) / 2.0_wp
end function ! sws_A_z_rad_n
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment,
!> evaluated radially along the endpoints of the wire segment (zP = 0 or zP = 1).
!>
!> @param rhoP normalized radial coordinate of evaluation location; must not be zero (on wire segment)
!> @return normalized axial component of magnetic vector potential
function sws_A_z_rad(rhoP)
real(wp) :: sws_A_z_rad
real(wp) :: rhoP
if (rhoP .gt. 1.0_wp) then
sws_A_z_rad = sws_A_z_rad_f(rhoP)
else
sws_A_z_rad = sws_A_z_rad_n(rhoP)
end if
end function ! sws_A_z_rad
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment.
!> This formulation is useful for points away from the wire ("far-field")
!> at rhoP >= 1 or zP <= -1 or zP > 2.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized axial component of magnetic vector potential
function sws_A_z_f(rhoP, zP)
real(wp) :: sws_A_z_f
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: r_i, r_f
r_i = hypot(rhoP, zP)
r_f = hypot(rhoP, 1.0_wp - zP)
sws_A_z_f = atanh(1.0_wp / (r_i + r_f))
end function ! sws_A_z_f
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment.
!> This formulation is useful for points close to the wire ("near-field")
!> at rhoP < 1 and -1 < zP <= 2.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized axial component of magnetic vector potential
function sws_A_z_n(rhoP, zP)
real(wp) :: sws_A_z_n
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: omz, r_i, r_f, alpha, sinAlphaHalf, &
beta, sinBetaHalf, Ri_zP, Rf_p_zM1, n
omz = 1.0_wp - zP
r_i = hypot(rhoP, zP)
r_f = hypot(rhoP, omz)
alpha = atan2(rhoP, zP)
sinAlphaHalf = sin(alpha / 2.0_wp)
beta = atan2(rhoP, omz)
sinBetaHalf = sin(beta / 2.0_wp)
Ri_zP = r_i * sinAlphaHalf * sinAlphaHalf ! 0.5 * (r_i - z')
Rf_p_zM1 = r_f * sinBetaHalf * sinBetaHalf ! 0.5 * (r_f - (1 - z'))
n = Ri_zP + Rf_p_zM1
sws_A_z_n = (log(1.0_wp + n) - log(n)) / 2.0_wp
end function ! sws_A_z_n
!------------ B_phi of straight wire segment
!> Compute the normalized tangential component of the magnetic field of a straight wire segment,
!> evaluated radially along the endpoints of the wire segment (zP = 0 or zP = 1).
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @return normalized tangential component of magnetic field
function sws_B_phi_rad(rhoP)
real(wp) :: sws_B_phi_rad
real(wp) :: rhoP
sws_B_phi_rad = 1.0_wp / (rhoP * hypot(rhoP, 1.0_wp))
end function ! sws_B_phi_rad
!> Compute the normalized tangential component of the magnetic field of a straight wire segment.
!> This formulation is useful for points away from the wire ("far-field")
!> at rhoP >= 1 or zP <= 0 or zP >= 1 or rhoP/(1-zP) >= 1 or rhoP/zP >= 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic field
function sws_B_phi_f(rhoP, zP)
real(wp) :: sws_B_phi_f
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: omz, r_i, r_f, num, den
omz = 1.0_wp - zP
r_i = hypot(rhoP, zP)
r_f = hypot(rhoP, omz)
num = rhoP * (1.0_wp/r_i + 1.0_wp/r_f)
den = rhoP * rhoP - zP * omz + r_i * r_f
sws_B_phi_f = num / den
end function ! sws_B_phi_f
!> Compute the normalized tangential component of the magnetic field of a straight wire segment.
!> This formulation is useful for points close to the wire ("near-field")
!> at rhoP < 1 and 0 < zP < 1 and rhoP/(1-zP) < 1 and rhoP/zP < 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic field
function sws_B_phi_n(rhoP, zP)
real(wp) :: sws_B_phi_n
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: omz, r_i, r_f, num, den, rfb_omza, &
alpha, sinAlphaHalf, beta, sinBetaHalf
omz = 1.0_wp - zP
r_i = hypot(rhoP, zP)
r_f = hypot(rhoP, omz)
num = rhoP * (1.0_wp/r_i + 1.0_wp/r_f)
alpha = atan2(rhoP, zP)
sinAlphaHalf = sin(alpha / 2.0_wp)
beta = atan2(rhoP, omz)
sinBetaHalf = sin(beta / 2.0_wp)
! r_f * sin^2(beta/2) + (1 - z') * sin^2(alpha/2)
rfb_omza = r_f * sinBetaHalf * sinBetaHalf &
+ omz * sinAlphaHalf * sinAlphaHalf
! r_i * r_f - z' * (1 - z')
! = r_i * r_f - r_i * (1 - z') + r_i * (1 - z') - z' * (1 - z')
! = r_i * r_f - r_i * r_f * cos(beta)
! + r_i * (1 - z') + (1 - z') * r_i * cos(alpha)
! = r_i * r_f * (1 - cos(beta))
! + r_i * (1 - z') * (1 - cos(alpha))
! = 2 * r_i * [ r_f * sin^2(beta/2) + (1 - z') * sin^2(alpha/2) ]
den = rhoP * rhoP + 2.0_wp * r_i * rfb_omza
sws_B_phi_n = num / den
end function ! sws_B_phi_n
!------------ A_phi of circular wire loop
!> Compute the normalized tangential component of the magnetic vector potential of a circular wire loop.
!> This formulation is useful for points away from the wire ("far-field")
!> at rhoP < 1/2 or rhoP > 2 or |zP| >= 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic vector potential
function cwl_A_phi_f(rhoP, zP)
real(wp) :: cwl_A_phi_f
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: sqrt_kCSqNum, sqrt_kCSqDen, kC, kSq, &
kCp1, arg1, arg2, C
sqrt_kCSqNum = hypot(zP, 1.0_wp - rhoP)
sqrt_kCSqDen = hypot(zP, 1.0_wp + rhoP)
kC = sqrt_kCSqNum / sqrt_kCSqDen
kSq = 4.0_wp * rhoP / (sqrt_kCSqDen * sqrt_kCSqDen)
kCp1 = 1.0_wp + kC
arg1 = 2.0_wp * sqrt(kC) / kCp1
arg2 = 2.0_wp / (kCp1 * kCp1 * kCp1)
C = cel(arg1, 1.0_wp, 0.0_wp, arg2)
cwl_A_phi_f = kSq/sqrt_kCSqDen * C
end function ! cwl_A_phi_f
!> Compute the normalized tangential component of the magnetic vector potential of a circular wire loop.
!> This formulation is useful for points close to the wire ("near-field")
!> at 1/2 <= rhoP <= 2 and |zP| < 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic vector potential
function cwl_A_phi_n(rhoP, zP)
real(wp) :: cwl_A_phi_n
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: rhoP_m_1, n, m, num, den, kCSq, prefac, celPart
rhoP_m_1 = rhoP - 1.0_wp
n = zP / rhoP_m_1
m = 1.0_wp + 2.0_wp / rhoP_m_1
num = n * n + 1.0_wp
den = n * n + m * m
kCSq = num / den
prefac = 1.0_wp / (abs(rhoP - 1.0_wp) * sqrt(den))
celPart = cel(sqrt(kCSq), 1.0_wp, -1.0_wp, 1.0_wp)
cwl_A_phi_n = prefac * celPart;
end function ! cwl_A_phi_n
!> Compute the normalized tangential component of the magnetic vector potential of a circular wire loop.
!> This formulation is useful for points along rhoP=1 with |zP| < 1.
!>
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic vector potential
function cwl_A_phi_v(zP)
real(wp) :: cwl_A_phi_v
real(wp) :: zP
real(wp) :: absZp, kCInv
absZp = abs(zP)
! 1/k_c
kCInv = sqrt(4.0_wp + zP * zP) / absZp
cwl_A_phi_v = cel(kCInv, 1.0_wp, 1.0_wp, -1.0_wp) / absZp
end function ! cwl_A_phi_v
!------------ B_rho of circular wire loop
!> Compute the normalized radial component of the magnetic field of a circular wire loop.
!> This formulation is useful for points away from the wire ("far-field")
!> at rhoP < 1/2 or rhoP > 2 or |zP| >= 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized radial component of magnetic field
function cwl_B_rho_f(rhoP, zP)
real(wp) :: cwl_B_rho_f
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: sqrt_kCSqNum, sqrt_kCSqDen, kCSqNum, kCSqDen, &
kCSq, kC, D, kCp1, arg1, arg2, C, prefac
sqrt_kCSqNum = hypot(zP, 1.0_wp - rhoP)
sqrt_kCSqDen = hypot(zP, 1.0_wp + rhoP)
kCSqNum = sqrt_kCSqNum * sqrt_kCSqNum
kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen
kCSq = kCSqNum / kCSqDen
kC = sqrt(kCSq)
D = cel(kC, 1.0_wp, 0.0_wp, 1.0_wp)
kCp1 = 1.0_wp + kC
arg1 = 2.0_wp * sqrt(kC) / kCp1
arg2 = 2.0_wp / (kCp1 * kCp1 * kCp1)
C = cel(arg1, 1.0_wp, 0.0_wp, arg2)
prefac = 4.0_wp * rhoP / (kCSqDen * sqrt_kCSqDen * kCSqNum)
cwl_B_rho_f = prefac * zP * (D - C)
end function ! cwl_B_rho_f
!> Compute the normalized radial component of the magnetic field of a circular wire loop.
!> This formulation is useful for points close to the wire ("near-field")
!> at 1/2 <= rhoP <= 2 and |zP| < 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized radial component of magnetic field
function cwl_B_rho_n(rhoP, zP)
real(wp) :: cwl_B_rho_n
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: rhoP_m_1, rd2, n, m, &
sqrt_kCSqNum, sqrt_kCSqDen, kCSqNum, kCSqDen, kC, &
D, kCp1, arg1, arg2, C, zP_rd5, prefac
rhoP_m_1 = rhoP - 1.0_wp
rd2 = rhoP_m_1 * rhoP_m_1
n = zP / rhoP_m_1
m = 1.0_wp + 2.0_wp / rhoP_m_1
sqrt_kCSqNum = hypot(n, 1.0_wp)
sqrt_kCSqDen = hypot(n, m)
kCSqNum = sqrt_kCSqNum * sqrt_kCSqNum
kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen
kC = sqrt_kCSqNum / sqrt_kCSqDen
D = cel(kC, 1.0_wp, 0.0_wp, 1.0_wp)
kCp1 = 1.0_wp + kC
arg1 = 2.0_wp * sqrt(kC) / kCp1
arg2 = 2.0_wp / (kCp1 * kCp1 * kCp1)
C = arg2 * cel(arg1, 1.0_wp, 0.0_wp, 1.0_wp)
! z' / |rho' - 1|^5
zP_rd5 = zP / (abs(rhoP_m_1) * rd2 * rd2)
prefac = 4.0_wp * rhoP / (kCSqDen * sqrt_kCSqDen * kCSqNum)
cwl_B_rho_n = prefac * zP_rd5 * (D - C)
end function ! cwl_B_rho_n
!> Compute the normalized radial component of the magnetic field of a circular wire loop.
!> This formulation is useful for points along rhoP=1 with |zP| < 1.
!>
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized radial component of magnetic field
function cwl_B_rho_v(zP)
real(wp) :: cwl_B_rho_v
real(wp) :: zP
real(wp) :: zPSq, kCSq, kC, K, E
zPSq = zP * zP
kCSq = 1.0_wp / (1.0_wp + 4.0_wp / zPSq)
kC = sqrt(kCSq)
K = cel(kC, 1.0_wp, 1.0_wp, 1.0_wp)
E = cel(kC, 1.0_wp, 1.0_wp, kCSq)
cwl_B_rho_v = sign(kC / 2.0_wp * ((2.0_wp / zPSq + 1.0_wp) * E - K), zP)
end function ! cwl_B_rho_v
!------------ B_z of circular wire loop
!> Compute the normalized vertical component of the magnetic field of a circular wire loop.
!> This formulation is useful for certain points away from the wire ("far-field")
!> at rhoP < 1/2 or (rhoP <= 2 and |zP| >= 1).
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized vertical component of magnetic field
function cwl_B_z_f1(rhoP, zP)
real(wp) :: cwl_B_z_f1
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: sqrt_kCSqNum, sqrt_kCSqDen, kC, K, E, D, prefac, comb
sqrt_kCSqNum = hypot(zP, 1.0_wp - rhoP)
sqrt_kCSqDen = hypot(zP, 1.0_wp + rhoP)
kC = sqrt_kCSqNum / sqrt_kCSqDen
K = cel(kC, 1.0_wp, 1.0_wp, 1.0_wp)
E = cel(kC, 1.0_wp, 1.0_wp, kC * kC)
D = cel(kC, 1.0_wp, 0.0_wp, 1.0_wp)
prefac = 1.0_wp / (sqrt_kCSqDen * sqrt_kCSqNum * sqrt_kCSqNum)
comb = E - 2.0_wp * K + 2.0_wp * D
cwl_B_z_f1 = prefac * (E + rhoP * comb)
end function ! cwl_B_z_f1
!> Compute the normalized vertical component of the magnetic field of a circular wire loop.
!> This formulation is useful for certain other points away from the wire ("far-field")
!> at rhoP > 2.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized vertical component of magnetic field
function cwl_B_z_f2(rhoP, zP)
real(wp) :: cwl_B_z_f2
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: sqrt_kCSqNum, sqrt_kCSqDen, kC, kCSq, &
zPSqP1, rhoPSq, t1, t2, a, b, &
prefac, cdScale, E, D, kCP1, arg1, arg2, C
sqrt_kCSqNum = hypot(zP, 1.0_wp - rhoP)
sqrt_kCSqDen = hypot(zP, 1.0_wp + rhoP)
kC = sqrt_kCSqNum / sqrt_kCSqDen
kCSq = kC * kC
zPSqP1 = zP * zP + 1.0_wp
rhoPSq = rhoP * rhoP
t1 = zPSqP1 / rhoPSq + 1.0_wp
t2 = 2.0_wp / rhoP
! a is sqrt_kCSqDen normalized to rho'^2
! b is sqrt_kCSqNum normalized to rho'^2
! a == (z'^2 + (1 + rho')^2) / rho'^2 = (z'^2 + 1)/rho'^2 + 1 + 2/rho'
! b == (z'^2 + (1 - rho')^2) / rho'^2 = (z'^2 + 1)/rho'^2 + 1 - 2/rho'
a = t1 + t2
b = t1 - t2
! 1/prefac = sqrt( z'^2 + (1 + rho')^2) * (z'^2 + (1 - rho')^2)
! = sqrt((z'^2 + (1 + rho')^2) / rho'^2) * (z'^2 + (1 - rho')^2) / rho'^2 * rho'^3
! = sqrt(a) * b * rho'^3
prefac = 1.0_wp / (sqrt(a) * b * rhoPSq * rhoP)
cdScale = 1.0_wp + (2.0_wp + zPSqP1 / rhoP) / rhoP
E = cel(kC, 1.0_wp, 1.0_wp, kCSq)
D = cel(kC, 1.0_wp, 0.0_wp, 1.0_wp)
kCP1 = 1.0_wp + kC
arg1 = 2.0_wp * sqrt(kC) / kCP1
arg2 = 2.0_wp / (kCP1 * kCP1 * kCP1)
C = arg2 * cel(arg1, 1.0_wp, 0.0_wp, 1.0_wp)
! use C - D for (2 * D - E)/kSq
cwl_B_z_f2 = prefac * (E + 4.0_wp * (C - D) / cdScale)
end function ! cwl_B_z_f2
!> Compute the normalized vertical component of the magnetic field of a circular wire loop.
!> This formulation is useful for points close to the wire ("near-field")
!> at 1/2 <= rhoP <= 2, but not rhoP=1, and |zP| <= 1.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized vertical component of magnetic field
function cwl_B_z_n(rhoP, zP)
real(wp) :: cwl_B_z_n
real(wp) :: rhoP
real(wp) :: zP
real(wp) :: rp1, n, m, prefac, &
sqrt_kCSqNum, sqrt_kCSqDen, kCSqDen, kC
rp1 = rhoP - 1.0_wp
n = zP / rp1
m = 1.0_wp + 2.0_wp / rp1
sqrt_kCSqNum = hypot(n, 1.0_wp)
sqrt_kCSqDen = hypot(n, m)
kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen
kC = sqrt_kCSqNum / sqrt_kCSqDen
prefac = 1.0_wp / (abs(rp1) * rp1 * rp1 * kCSqDen * sqrt_kCSqDen)
cwl_B_z_n = prefac * cel(kC, kC * kC, 1.0_wp + rhoP, 1.0_wp - rhoP)
end function ! cwl_B_z_n
!> Compute the normalized vertical component of the magnetic field of a circular wire loop.
!> This formulation is useful for points along rhoP=1 with |zP| <= 1.
!>
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized vertical component of magnetic field
function cwl_B_z_v(zP)
real(wp) :: cwl_B_z_v
real(wp) :: zP
real(wp) :: kCSq, kC, f, prefac
kCSq = zP * zP / (4.0_wp + zP * zP)
kC = sqrt(kCSq)
f = zP * zP + 4.0_wp
prefac = 1.0_wp / (f * sqrt(f))
cwl_B_z_v = prefac * cel(kC, kCSq, 2.0_wp, 0.0_wp)
end function ! cwl_B_z_v
! --------------------------------------------------
!> Compute the normalized axial component of the magnetic vector potential of a straight wire segment.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized axial component of magnetic vector potential
function straightWireSegment_A_z(rhoP, zP)
real(wp) :: straightWireSegment_A_z
real(wp) :: rhoP
real(wp) :: zP
if (rhoP .eq. 0.0_wp) then
if (zP .lt. 0.0_wp .or. zP .gt. 1.0_wp) then
straightWireSegment_A_z = sws_A_z_ax(zP)
else
write(*,*) "evaluation locations on the wire segment (rho'=", &
rhoP, " z'=", zP, ") are not allowed"
straightWireSegment_A_z = IEEE_VALUE(straightWireSegment_A_z, &
IEEE_SIGNALING_NAN)
end if
else if (zP .eq. 0.0_wp .or. zP .eq. 1.0_wp) then
straightWireSegment_A_z = sws_A_z_rad(rhoP)
else if (rhoP .ge. 1.0_wp .or. zP .le. -1.0_wp .or. zP .gt. 2.0_wp) then
straightWireSegment_A_z = sws_A_z_f(rhoP, zP)
else
straightWireSegment_A_z = sws_A_z_n(rhoP, zP)
end if
end function ! straightWireSegment_A_z
!> Compute the normalized tangential component of the magnetic field of a straight wire segment.
!>
!> @param rhoP normalized radial coordinate of evaluation location
!> @param zP normalized axial coordinate of evaluation location
!> @return normalized tangential component of magnetic field
function straightWireSegment_B_phi(rhoP, zP)
real(wp) :: straightWireSegment_B_phi
real(wp) :: rhoP
real(wp) :: zP
if (rhoP .eq. 0.0_wp) then
if (zP .lt. 0.0_wp .or. zP .gt. 1.0_wp) then
straightWireSegment_B_phi = 0.0_wp
else
write(*,*) "evaluation locations on the wire segment (rho'=", &
rhoP, " z'=", zP, ") are not allowed"
straightWireSegment_B_phi = IEEE_VALUE(straightWireSegment_B_phi, &
IEEE_SIGNALING_NAN)
end if
else if (zP .eq. 0.0_wp .or. zP .eq. 1.0_wp) then
straightWireSegment_B_phi = sws_B_phi_rad(rhoP)
else if (rhoP .ge. zP .or. rhoP .ge. 1.0_wp - zP .or. &
zP .lt. 0.0_wp .or. zP .gt. 1.0_wp) then
straightWireSegment_B_phi = sws_B_phi_f(rhoP, zP)
else
straightWireSegment_B_phi = sws_B_phi_n(rhoP, zP)
end if
end function ! straightWireSegment_B_phi
!> Geometric part of magnetic vector potential computation for circular wire
!> loop at rho'=1, z'=0 (normalized coordinates). This routine selects special
!> case routines to get the most accurate formulation for given evaluation
!> coordinates.
!>
!> @param rhoP normalized radial evaluation position
!> @param zP normalized vertical evaluation position
!> @return A_phi: toroidal component of magnetic vector potential: geometric
!> part (no mu0*I/pi factor included)
function circularWireLoop_A_phi(rhoP, zP)
real(wp) :: circularWireLoop_A_phi
real(wp) :: rhoP
real(wp) :: zP
if (rhoP .eq. 0.0_wp) then
circularWireLoop_A_phi = 0.0_wp
else if (rhoP .lt. 0.5_wp .or. rhoP .gt. 2.0_wp .or. &
abs(zP) .ge. 1.0_wp) then
circularWireLoop_A_phi = cwl_A_phi_f(rhoP, zP)
else if (rhoP .ne. 1.0_wp) then
circularWireLoop_A_phi = cwl_A_phi_n(rhoP, zP)
else
if (zP .ne. 0.0_wp) then
circularWireLoop_A_phi = cwl_A_phi_v(zP)
else
write(*,*) "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined"
circularWireLoop_A_phi = IEEE_VALUE(circularWireLoop_A_phi, &
IEEE_SIGNALING_NAN)
end if
end if
end function ! circularWireLoop_A_phi
!> Geometric part of radial magnetic field computation for circular wire loop at
!> rho'=1, z'=0 (normalized coordinates). This routine selects special case
!> routines to get the most accurate formulation for given evaluation
!> coordinates.
!>
!> @param rhoP normalized radial evaluation position
!> @param zP normalized vertical evaluation position
!> @return B_rho: radial component of magnetic field: geometric part (no
!> mu0*I/(pi*a) factor included)
function circularWireLoop_B_rho(rhoP, zP)
real(wp) :: circularWireLoop_B_rho
real(wp) :: rhoP
real(wp) :: zP
if (rhoP .eq. 0.0_wp .or. zP .eq. 0.0_wp) then
if (rhoP .ne. 1.0_wp) then
circularWireLoop_B_rho = 0.0_wp
else
write(*,*) "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined"
circularWireLoop_B_rho = IEEE_VALUE(circularWireLoop_B_rho, &
IEEE_SIGNALING_NAN)
end if
else if (rhoP .lt. 0.5_wp .or. rhoP .gt. 2.0_wp .or. &
abs(zP) .ge. 1.0_wp) then
circularWireLoop_B_rho = cwl_B_rho_f(rhoP, zP)
else if (rhoP .ne. 1.0_wp) then
circularWireLoop_B_rho = cwl_B_rho_n(rhoP, zP)
else
circularWireLoop_B_rho = cwl_B_rho_v(zP)
end if
end function ! circularWireLoop_B_rho
!> Geometric part of vertical magnetic field computation for circular wire loop
!> at rho'=1, z'=0 (normalized coordinates). This routine selects special case
!> routines to get the most accurate formulation for given evaluation
!> coordinates.
!>
!> @param rhoP normalized radial evaluation position
!> @param zP normalized vertical evaluation position
!> @return B_z: vertical component of magnetic field: geometric part (no
!> mu0*I/(pi*a) factor included)
function circularWireLoop_B_z(rhoP, zP)
real(wp) :: circularWireLoop_B_z
real(wp) :: rhoP
real(wp) :: zP
if (rhoP .lt. 0.5_wp .or. &
(rhoP .le. 2.0_wp .and. abs(zP) .gt. 1.0_wp)) then
circularWireLoop_B_z = cwl_B_z_f1(rhoP, zP)
else if (rhoP .gt. 2.0_wp) then
circularWireLoop_B_z = cwl_B_z_f2(rhoP, zP)
else if (rhoP .ne. 1.0_wp) then
circularWireLoop_B_z = cwl_B_z_n(rhoP, zP)
else
if (zP .ne. 0.0_wp) then
circularWireLoop_B_z = cwl_B_z_v(zP)
else
write(*,*) "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined"
circularWireLoop_B_z = IEEE_VALUE(circularWireLoop_B_z, &
IEEE_SIGNALING_NAN)
end if
end if
end function ! circularWireLoop_B_z
! --------------------------------------------------
!> Compute the magnetic vector potential of a circular wire loop.
!>
!> @param center [3: x, y, z] origin of loop (in meters)
!> @param normal [3: x, y, z] normal vector of loop (in meters); will be
!> normalized internally
!> @param radius radius of the wire loop (in meters)
!> @param current loop current (in A)
!> @param evalPos [3: x, y, z][nEvalPos] evaluation locations (in meters)
!> @param vectorPotential [3: A_x, A_y, A_z][nEvalPos] Cartesian components of the magnetic
!> vector potential evaluated at the given locations (in Tm); has to be allocated on entry
subroutine vectorPotentialCircularFilament(center, normal, radius, current, &
nEvalPos, evalPos, vectorPotential)
real(wp), intent(in), dimension(3) :: center
real(wp), intent(in), dimension(3) :: normal
real(wp), intent(in) :: radius
real(wp), intent(in) :: current
integer, intent(in) :: nEvalPos
real(wp), intent(in), dimension(3, nEvalPos) :: evalPos
real(wp), intent(out), dimension(3, nEvalPos) :: vectorPotential
integer :: idxEval
real(wp) :: aPrefactor, nLen2, nLen, eX, eY, eZ, &
r0x, r0y, r0z, alignedZ, zP, &
rParallelX, rParallelY, rParallelZ, &
rPerpX, rPerpY, rPerpZ, &
alignedRSq, alignedR, eRX, eRY, eRZ, &
rhoP, aPhi, ePhiX, ePhiY, ePhiZ
if (.not. ieee_is_finite(radius) .or. radius .le. 0.0_wp) then
print *, "radius must be finite and positive, but is ", radius
return
end if
aPrefactor = MU_0_BY_PI * current
! squared length of normal vector
nLen2 = normal(1) * normal(1) + normal(2) * normal(2) + normal(3) * normal(3)
if (nLen2 .eq. 0.0_wp) then
print *, "length of normal vector must not be zero"
return
end if
! length of normal vector
nLen = sqrt(nLen2)
! unit normal vector of wire loop
eX = normal(1) / nLen
eY = normal(2) / nLen
eZ = normal(3) / nLen
do idxEval = 1, nEvalPos
! vector from center of wire loop to eval pos
r0x = evalPos(1, idxEval) - center(1)
r0y = evalPos(2, idxEval) - center(2)
r0z = evalPos(3, idxEval) - center(3)
! z position along normal of wire loop
alignedZ = eX * r0x + eY * r0y + eZ * r0z
! normalized z component of evaluation location in coordinate system of wire loop
zP = alignedZ / radius
! r0 projected onto axis of wire loop
rParallelX = alignedZ * eX
rParallelY = alignedZ * eY
rParallelZ = alignedZ * eZ
! vector perpendicular to axis of wire loop, pointing at evaluation pos
rPerpX = r0x - rParallelX
rPerpY = r0y - rParallelY
rPerpZ = r0z - rParallelZ
! perpendicular distance squared between evalPos and axis of wire loop
alignedRSq = rPerpX * rPerpX + rPerpY * rPerpY + rPerpZ * rPerpZ
! prevent division-by-zero when computing radial unit vector
! A_phi is zero anyway on-axis --> no contribution expected
if (alignedRSq .gt. 0.0_wp) then
! perpendicular distance between evalPos and axis of wire loop
alignedR = sqrt(alignedRSq)
! unit vector in radial direction
eRX = rPerpX / alignedR
eRY = rPerpY / alignedR
eRZ = rPerpZ / alignedR
! normalized rho component of evaluation location in coordinate system of wire loop
rhoP = alignedR / radius
! compute tangential component of magnetic vector potential, including current and mu_0
aPhi = aPrefactor * circularWireLoop_A_phi(rhoP, zP)
! compute cross product between e_z and e_rho to get e_phi
ePhiX = eRY * eZ - eRZ * eY
ePhiY = eRZ * eX - eRX * eZ
ePhiZ = eRX * eY - eRY * eX
! add contribution from wire loop to result
vectorPotential(1, idxEval) = aPhi * ePhiX
vectorPotential(2, idxEval) = aPhi * ePhiY
vectorPotential(3, idxEval) = aPhi * ePhiZ
end if ! alignedRSq .gt. 0.0
end do ! idxEval = 1, nEvalPos
end subroutine ! vectorPotentialCircularFilament
!> Compute the magnetic field of a circular wire loop.
!>
!> @param center [3: x, y, z] origin of loop (in meters)
!> @param normal [3: x, y, z] normal vector of loop (in meters); will be
!> normalized internally
!> @param radius radius of the wire loop (in meters)
!> @param current loop current (in A)
!> @param evalPos [3: x, y, z][nEvalPos] evaluation locations (in meters)
!> @param magneticField [3: B_x, B_y, B_z][nEvalPos] Cartesian components of the magnetic
!> field evaluated at the given locations (in T); has to be allocated on entry
subroutine magneticFieldCircularFilament(center, normal, radius, current, &
nEvalPos, evalPos, magneticField)
real(wp), intent(in), dimension(3) :: center
real(wp), intent(in), dimension(3) :: normal
real(wp), intent(in) :: radius
real(wp), intent(in) :: current
integer, intent(in) :: nEvalPos
real(wp), intent(in), dimension(3, nEvalPos) :: evalPos
real(wp), intent(out), dimension(3, nEvalPos) :: magneticField
integer :: idxEval
real(wp) :: bPrefactor, nLen2, nLen, eX, eY, eZ, &
r0x, r0y, r0z, alignedZ, zP, &
rParallelX, rParallelY, rParallelZ, &
rPerpX, rPerpY, rPerpZ, &
alignedRSq, alignedR, eRX, eRY, eRZ, &
rhoP, bRho, bZ, ePhiX, ePhiY, ePhiZ
if (.not. ieee_is_finite(radius) .or. radius .le. 0.0_wp) then
print *, "radius must be finite and positive, but is ", radius
return
end if
bPrefactor = MU_0_BY_PI * current / radius
! squared length of normal vector
nLen2 = normal(1) * normal(1) + normal(2) * normal(2) + normal(3) * normal(3)
if (nLen2 .eq. 0.0_wp) then
print *, "length of normal vector must not be zero"
return
end if
! length of normal vector
nLen = sqrt(nLen2)
! unit normal vector of wire loop
eX = normal(1) / nLen
eY = normal(2) / nLen
eZ = normal(3) / nLen
do idxEval = 1, nEvalPos
! vector from center of wire loop to eval pos
r0x = evalPos(1, idxEval) - center(1)
r0y = evalPos(2, idxEval) - center(2)
r0z = evalPos(3, idxEval) - center(3)
! z position along normal of wire loop
alignedZ = eX * r0x + eY * r0y + eZ * r0z
! normalized z component of evaluation location in coordinate system of wire loop
zP = alignedZ / radius
! r0 projected onto axis of wire loop
rParallelX = alignedZ * eX
rParallelY = alignedZ * eY
rParallelZ = alignedZ * eZ
! vector perpendicular to axis of wire loop, pointing at evaluation pos
rPerpX = r0x - rParallelX
rPerpY = r0y - rParallelY
rPerpZ = r0z - rParallelZ
! perpendicular distance squared between evalPos and axis of wire loop
alignedRSq = rPerpX * rPerpX + rPerpY * rPerpY + rPerpZ * rPerpZ
if (alignedRSq .gt. 0.0_wp) then
! radial unit vector is only defined if evaluation pos is off-axis
! perpendicular distance between evalPos and axis of wire loop
alignedR = sqrt(alignedRSq)
! unit vector in radial direction
eRX = rPerpX / alignedR
eRY = rPerpY / alignedR
eRZ = rPerpZ / alignedR
! normalized rho component of evaluation location in coordinate system of wire loop
rhoP = alignedR / radius
! compute radial component of normalized magnetic field
! and scale by current and mu_0
bRho = bPrefactor * circularWireLoop_B_rho(rhoP, zP)
! add contribution from B_rho of wire loop to result
magneticField(1, idxEval) = bRho * eRX
magneticField(2, idxEval) = bRho * eRY
magneticField(3, idxEval) = bRho * eRZ
else
rhoP = 0.0_wp
end if
! compute vertical component of normalized magnetic field
! and scale by current and mu_0
bZ = bPrefactor * circularWireLoop_B_z(rhoP, zP)
! add contribution from B_z of wire loop to result
magneticField(1, idxEval) = magneticField(1, idxEval) + bZ * eX
magneticField(2, idxEval) = magneticField(2, idxEval) + bZ * eY
magneticField(3, idxEval) = magneticField(3, idxEval) + bZ * eZ
end do ! idxEval = 1, nEvalPos
end subroutine ! magneticFieldCircularFilament
!> Compute the magnetic vector potential of a polygon filament
!> at a number of evaluation locations.
!>
!> @param vertices [3: x, y, z][numVertices] points along polygon; in m
!> @param current current along polygon; in A
!> @param evalPos [3: x, y, z][numEvalPos] evaluation locations; in m
!> @param vectorPotential [3: x, y, z][numEvalPos] target array for magnetic vector potential at evaluation locations; in Tm
!> @param idxSourceStart first index in {@code vertices} to take into account
!> @param idxSourceEnd last index in {@code vertices} to take into account
!> @param idxEvalStart first index in {@code evalPos} to take into account
!> @param idxEvalEnd last index in {@code evalPos} to take into account
!> @param useCompensatedSummation if true, use Kahan-Babuska compensated summation to compute the superposition
!> of the contributions from the polygon vertices; otherwise, use standard += summation
subroutine kernelVectorPotentialPolygonFilament ( &
vertices, current, evalPos, vectorPotential, &
idxSourceStart, idxSourceEnd, idxEvalStart, idxEvalEnd, &
useCompensatedSummation)
real(wp), intent(in), dimension(3, *) :: vertices
real(wp), intent(in) :: current
real(wp), intent(in), dimension(3, *) :: evalPos
real(wp), intent(out), dimension(3, *) :: vectorPotential
integer, intent(in) :: idxSourceStart
integer, intent(in) :: idxSourceEnd
integer, intent(in) :: idxEvalStart
integer, intent(in) :: idxEvalEnd
logical, intent(in) :: useCompensatedSummation
real(wp) :: aPrefactor, x_i, y_i, z_i, x_f, y_f, z_f, &
dx, dy, dz, l2, l, eX, eY, eZ, r0x, r0y, r0z, &
alignedZ, zP, rPerpX, rPerpY, rPerpZ, &
alignedR, rhoP, aParallel
integer :: istat, idxEval, numEvalPos, idxSource
real(wp), dimension(:,:), allocatable :: aXSum, aYSum, aZSum
aPrefactor = MU_0_BY_2_PI * current
! setup compensated summation
if (useCompensatedSummation) then
numEvalPos = idxEvalEnd - idxEvalStart + 1
! need three values (s, cs, ccs) per eval pos --> see mod_compsum
allocate(aXSum(3, numEvalPos), &
aYSum(3, numEvalPos), &
aZSum(3, numEvalPos), stat=istat)
if (istat .ne. 0) then
print *, "failed to allocate compensated summation buffers: stat=", istat
return
end if
! initialize target array to zero
aXSum(:,:) = 0.0_wp
aYSum(:,:) = 0.0_wp
aZSum(:,:) = 0.0_wp
end if ! useCompensatedSummation
x_i = vertices(1, idxSourceStart)
y_i = vertices(2, idxSourceStart)
z_i = vertices(3, idxSourceStart)
do idxSource = idxSourceStart, idxSourceEnd
x_f = vertices(1, idxSource + 1)
y_f = vertices(2, idxSource + 1)
z_f = vertices(3, idxSource + 1)
! vector from start to end of i:th wire segment
dx = x_f - x_i
dy = y_f - y_i
dz = z_f - z_i
! squared length of wire segment
l2 = dx * dx + dy * dy + dz * dz