-
Notifications
You must be signed in to change notification settings - Fork 0
/
abscab.h
2387 lines (2034 loc) · 80.6 KB
/
abscab.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
#ifndef ABSCAB_H
#define ABSCAB_H
// for memset()
#include <string.h>
#ifdef _OPENMP
#include <omp.h>
#else // _OPENMP
#define omp_get_max_threads() 1
#endif // _OPENMP
#include "cel.h"
#include "compsum.h"
#define min(x,y) ((x) < (y) ? (x) : (y))
/** vacuum magnetic permeability in Vs/Am (CODATA-2018) */
const double MU_0 = 1.25663706212e-6;
/** vacuum magnetic permeability, divided by pi */
const double MU_0_BY_PI = MU_0 / M_PI;
/** vacuum magnetic permeability, divided by 2 pi */
const double MU_0_BY_2_PI = MU_0 / (2.0 * M_PI);
/** vacuum magnetic permeability, divided by 4 pi */
const double MU_0_BY_4_PI = MU_0 / (4.0 * M_PI);
/////// 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
*/
double sws_A_z_ax_f(double zP) {
return atanh(1 / (fabs(zP) + fabs(1 - zP)));
}
/**
* 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
*/
double sws_A_z_ax_n(double zP) {
// Two negative signs must be able to cancel each other here!
return copysign(1.0, zP) * log(zP / (zP - 1)) / 2;
}
/**
* 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
*/
double sws_A_z_ax(double zP) {
if (zP < -1 || zP >= 2) {
return sws_A_z_ax_f(zP);
} else {
return sws_A_z_ax_n(zP);
}
}
/**
* 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
*/
double sws_A_z_rad_f(double rhoP) {
return atanh(1 / (rhoP + hypot(rhoP, 1)));
}
/**
* 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
*/
double sws_A_z_rad_n(double rhoP) {
double cat = 1 / hypot(rhoP, 1); // cos(atan(...) )
double sat = sin(atan(rhoP) / 2); // sin(atan(...)/2)
double rc = rhoP * cat;
double num = rc + 1 + cat;
double den = rc + 2 * sat * sat;
return log(num / den) / 2;
}
/**
* 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
*/
double sws_A_z_rad(double rhoP) {
if (rhoP > 1) {
return sws_A_z_rad_f(rhoP);
} else {
return sws_A_z_rad_n(rhoP);
}
}
/**
* 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
*/
double sws_A_z_f(double rhoP, double zP) {
double r_i = hypot(rhoP, zP);
double r_f = hypot(rhoP, 1 - zP);
return atanh(1 / (r_i + r_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
*/
double sws_A_z_n(double rhoP, double zP) {
double omz = 1 - zP;
double r_i = hypot(rhoP, zP);
double r_f = hypot(rhoP, omz);
double alpha = atan2(rhoP, zP);
double sinAlphaHalf = sin(alpha / 2);
double beta = atan2(rhoP, omz);
double sinBetaHalf = sin(beta / 2);
double Ri_zP = r_i * sinAlphaHalf * sinAlphaHalf; // 0.5 * (r_i - z')
double Rf_p_zM1 = r_f * sinBetaHalf * sinBetaHalf; // 0.5 * (r_f - (1 - z'))
double n = Ri_zP + Rf_p_zM1;
return (log(1 + n) - log(n)) / 2;
}
/////// 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
*/
double sws_B_phi_rad(double rhoP) {
return 1 / (rhoP * hypot(rhoP, 1));
}
/**
* 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
*/
double sws_B_phi_f(double rhoP, double zP) {
double omz = 1 - zP;
double r_i = hypot(rhoP, zP);
double r_f = hypot(rhoP, omz);
double num = rhoP * (1/r_i + 1/r_f);
double den = rhoP * rhoP - zP * omz + r_i * r_f;
return num / den;
}
/**
* 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
*/
double sws_B_phi_n(double rhoP, double zP) {
double omz = 1 - zP;
double r_i = hypot(rhoP, zP);
double r_f = hypot(rhoP, omz);
double num = rhoP * (1/r_i + 1/r_f);
double alpha = atan2(rhoP, zP);
double sinAlphaHalf = sin(alpha / 2);
double beta = atan2(rhoP, omz);
double sinBetaHalf = sin(beta / 2);
// r_f * sin^2(beta/2) + (1 - z') * sin^2(alpha/2)
double 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) ]
double den = rhoP * rhoP + 2 * r_i * rfb_omza;
return num / den;
}
///// 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
*/
double cwl_A_phi_f(double rhoP, double zP) {
double sqrt_kCSqNum = hypot(zP, 1 - rhoP);
double sqrt_kCSqDen = hypot(zP, 1 + rhoP);
double kC = sqrt_kCSqNum / sqrt_kCSqDen;
double kSq = 4 * rhoP / (sqrt_kCSqDen * sqrt_kCSqDen);
double kCp1 = 1 + kC;
double arg1 = 2 * sqrt(kC) / kCp1;
double arg2 = 2 / (kCp1 * kCp1 * kCp1);
double C = cel(arg1, 1, 0, arg2);
return kSq/sqrt_kCSqDen * C;
}
/**
* 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
*/
double cwl_A_phi_n(double rhoP, double zP) {
double rhoP_m_1 = rhoP - 1;
double n = zP / rhoP_m_1;
double m = 1 + 2 / rhoP_m_1;
double num = n * n + 1;
double den = n * n + m * m;
double kCSq = num / den;
double prefac = 1 / (fabs(rhoP - 1) * sqrt(den));
double celPart = cel(sqrt(kCSq), 1, -1, 1);
return prefac * celPart;
}
/**
* 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
*/
double cwl_A_phi_v(double zP) {
double absZp = fabs(zP);
// 1/k_c
double kCInv = sqrt(4 + zP * zP) / absZp;
return cel(kCInv, 1, 1, -1) / absZp;
}
//////// 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
*/
double cwl_B_rho_f(double rhoP, double zP) {
double sqrt_kCSqNum = hypot(zP, 1 - rhoP);
double sqrt_kCSqDen = hypot(zP, 1 + rhoP);
double kCSqNum = sqrt_kCSqNum * sqrt_kCSqNum;
double kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen;
double kCSq = kCSqNum / kCSqDen;
double kC = sqrt(kCSq);
double D = cel(kC, 1, 0, 1);
double kCp1 = 1 + kC;
double arg1 = 2 * sqrt(kC) / kCp1;
double arg2 = 2 / (kCp1 * kCp1 * kCp1);
double C = cel(arg1, 1, 0, arg2);
double prefac = 4 * rhoP / (kCSqDen * sqrt_kCSqDen * kCSqNum);
return prefac * zP * (D - C);
}
/**
* 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
*/
double cwl_B_rho_n(double rhoP, double zP) {
double rhoP_m_1 = rhoP - 1;
double rd2 = rhoP_m_1 * rhoP_m_1;
double n = zP / rhoP_m_1;
double m = 1 + 2 / rhoP_m_1;
double sqrt_kCSqNum = hypot(n, 1);
double sqrt_kCSqDen = hypot(n, m);
double kCSqNum = sqrt_kCSqNum * sqrt_kCSqNum;
double kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen;
double kC = sqrt_kCSqNum / sqrt_kCSqDen;
double D = cel(kC, 1, 0, 1);
double kCp1 = 1 + kC;
double arg1 = 2 * sqrt(kC) / kCp1;
double arg2 = 2 / (kCp1 * kCp1 * kCp1);
double C = arg2 * cel(arg1, 1, 0, 1);
// z' / |rho' - 1|^5
double zP_rd5 = zP / (fabs(rhoP_m_1) * rd2 * rd2);
double prefac = 4 * rhoP / (kCSqDen * sqrt_kCSqDen * kCSqNum);
return prefac * zP_rd5 * (D - C);
}
/**
* 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
*/
double cwl_B_rho_v(double zP) {
double zPSq = zP * zP;
double kCSq = 1 / (1 + 4 / zPSq);
double kC = sqrt(kCSq);
double K = cel(kC, 1, 1, 1);
double E = cel(kC, 1, 1, kCSq);
return copysign(kC / 2 * ((2 / zPSq + 1) * E - K), zP);
}
////// 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
*/
double cwl_B_z_f1(double rhoP, double zP) {
double sqrt_kCSqNum = hypot(zP, 1 - rhoP);
double sqrt_kCSqDen = hypot(zP, 1 + rhoP);
double kC = sqrt_kCSqNum / sqrt_kCSqDen;
double K = cel(kC, 1, 1, 1);
double E = cel(kC, 1, 1, kC * kC);
double D = cel(kC, 1, 0, 1);
double prefac = 1 / (sqrt_kCSqDen * sqrt_kCSqNum * sqrt_kCSqNum);
double comb = (E - 2 * K + 2 * D);
return prefac * (E + rhoP * comb);
}
/**
* 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
*/
double cwl_B_z_f2(double rhoP, double zP) {
double sqrt_kCSqNum = hypot(zP, 1 - rhoP);
double sqrt_kCSqDen = hypot(zP, 1 + rhoP);
double kC = sqrt_kCSqNum / sqrt_kCSqDen;
double kCSq = kC * kC;
double zPSqP1 = zP * zP + 1;
double rhoPSq = rhoP * rhoP;
double t1 = zPSqP1 / rhoPSq + 1;
double t2 = 2 / 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'
double a = t1 + t2;
double 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
double prefac = 1 / (sqrt(a) * b * rhoPSq * rhoP);
double cdScale = 1 + (2 + zPSqP1 / rhoP) / rhoP;
double E = cel(kC, 1, 1, kCSq);
double D = cel(kC, 1, 0, 1);
double kCP1 = 1 + kC;
double arg1 = 2 * sqrt(kC) / kCP1;
double arg2 = 2 / (kCP1 * kCP1 * kCP1);
double C = arg2 * cel(arg1, 1, 0, 1);
// use C - D for (2 * D - E)/kSq
return prefac * (E + 4 * (C - D) / cdScale);
}
/**
* 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
*/
double cwl_B_z_n(double rhoP, double zP) {
double rp1 = rhoP - 1;
double n = zP / rp1;
double m = 1 + 2 / rp1;
double sqrt_kCSqNum = hypot(n, 1);
double sqrt_kCSqDen = hypot(n, m);
double kCSqDen = sqrt_kCSqDen * sqrt_kCSqDen;
double kC = sqrt_kCSqNum / sqrt_kCSqDen;
double prefac = 1 / (fabs(rp1) * rp1 * rp1 * kCSqDen * sqrt_kCSqDen);
return prefac * cel(kC, kC * kC, 1 + rhoP, 1 - rhoP);
}
/**
* 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
*/
double cwl_B_z_v(double zP) {
double kCSq = zP * zP / (4 + zP * zP);
double kC = sqrt(kCSq);
double f = zP * zP + 4;
double prefac = 1 / (f * sqrt(f));
return prefac * cel(kC, kCSq, 2, 0);
}
// --------------------------------------------------
/**
* 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
*/
double straightWireSegment_A_z(double rhoP, double zP) {
if (rhoP == 0.0) {
if (zP < 0 || zP > 1) {
return sws_A_z_ax(zP);
} else {
fprintf(stderr, "evaluation locations on the wire segment (rho'=%g z'=%g) are not allowed\n", rhoP, zP);
}
} else if (zP == 0.0 || zP == 1.0) {
return sws_A_z_rad(rhoP);
} else if (rhoP >= 1.0 || zP <= -1.0 || zP > 2.0) {
return sws_A_z_f(rhoP, zP);
} else {
return sws_A_z_n(rhoP, zP);
}
}
/**
* 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
*/
double straightWireSegment_B_phi(double rhoP, double zP) {
if (rhoP == 0.0) {
if (zP < 0 || zP > 1) {
return 0.0;
} else {
fprintf(stderr, "evaluation locations on the wire segment (rho'=%g z'=%g) are not allowed\n", rhoP, zP);
}
} else if (zP == 0.0 || zP == 1.0) {
return sws_B_phi_rad(rhoP);
} else if (rhoP >= zP || rhoP >= 1 - zP || zP < 0.0 || zP > 1.0) {
return sws_B_phi_f(rhoP, zP);
} else {
return sws_B_phi_n(rhoP, zP);
}
}
/**
* 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)
*/
double circularWireLoop_A_phi(double rhoP, double zP) {
if (rhoP == 0.0) {
return 0.0;
} else if (rhoP < 0.5 || rhoP > 2.0 || fabs(zP) >= 1.0) {
return cwl_A_phi_f(rhoP, zP);
} else if (rhoP != 1.0) {
return cwl_A_phi_n(rhoP, zP);
} else {
if (zP != 0) {
return cwl_A_phi_v(zP);
} else {
fprintf(stderr, "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined\n");
}
}
}
/**
* 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)
*/
double circularWireLoop_B_rho(double rhoP, double zP) {
if (rhoP == 0.0 || zP == 0.0) {
if (rhoP != 1.0) {
return 0.0;
} else {
fprintf(stderr, "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined\n");
}
} else if (rhoP < 0.5 || rhoP > 2.0 || fabs(zP) >= 1.0) {
return cwl_B_rho_f(rhoP, zP);
} else if (rhoP != 1.0) {
return cwl_B_rho_n(rhoP, zP);
} else {
return cwl_B_rho_v(zP);
}
}
/**
* 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)
*/
double circularWireLoop_B_z(double rhoP, double zP) {
if (rhoP < 0.5 || (rhoP <= 2 && fabs(zP) > 1)) {
return cwl_B_z_f1(rhoP, zP);
} else if (rhoP > 2) {
return cwl_B_z_f2(rhoP, zP);
} else if (rhoP != 1.0) {
return cwl_B_z_n(rhoP, zP);
} else {
if (zP != 0) {
return cwl_B_z_v(zP);
} else {
fprintf(stderr, "evaluation at location of wire loop (rho' = 1, z' = 0) is not defined\n");
}
}
}
// --------------------------------------------------
/**
* 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
*/
void vectorPotentialCircularFilament(double *center, double *normal, double radius,
double current, int nEvalPos, double *evalPos, double *vectorPotential) {
if (!isfinite(radius) || radius <= 0.0) {
printf("radius must be finite and positive, but is %g\n", radius);
return;
}
double aPrefactor = MU_0_BY_PI * current;
// squared length of normal vector
double nLen2 = normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2];
if (nLen2 == 0.0) {
printf("length of normal vector must not be zero");
return;
}
// length of normal vector
double nLen = sqrt(nLen2);
// unit normal vector of wire loop
double eX = normal[0] / nLen;
double eY = normal[1] / nLen;
double eZ = normal[2] / nLen;
for (int idxEval = 0; idxEval < nEvalPos; ++idxEval) {
// vector from center of wire loop to eval pos
double r0x = evalPos[3 * idxEval + 0] - center[0];
double r0y = evalPos[3 * idxEval + 1] - center[1];
double r0z = evalPos[3 * idxEval + 2] - center[2];
// z position along normal of wire loop
double alignedZ = eX * r0x + eY * r0y + eZ * r0z;
// normalized z component of evaluation location in coordinate system of wire loop
double zP = alignedZ / radius;
// r0 projected onto axis of wire loop
double rParallelX = alignedZ * eX;
double rParallelY = alignedZ * eY;
double rParallelZ = alignedZ * eZ;
// vector perpendicular to axis of wire loop, pointing at evaluation pos
double rPerpX = r0x - rParallelX;
double rPerpY = r0y - rParallelY;
double rPerpZ = r0z - rParallelZ;
// perpendicular distance squared between evalPos and axis of wire loop
double 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 > 0.0) {
// perpendicular distance between evalPos and axis of wire loop
double alignedR = sqrt(alignedRSq);
// unit vector in radial direction
double eRX = rPerpX / alignedR;
double eRY = rPerpY / alignedR;
double eRZ = rPerpZ / alignedR;
// normalized rho component of evaluation location in coordinate system of wire loop
double rhoP = alignedR / radius;
// compute tangential component of magnetic vector potential, including current and mu_0
double aPhi = aPrefactor * circularWireLoop_A_phi(rhoP, zP);
// compute cross product between e_z and e_rho to get e_phi
double ePhiX = eRY * eZ - eRZ * eY;
double ePhiY = eRZ * eX - eRX * eZ;
double ePhiZ = eRX * eY - eRY * eX;
// add contribution from wire loop to result
vectorPotential[3 * idxEval + 0] = aPhi * ePhiX;
vectorPotential[3 * idxEval + 1] = aPhi * ePhiY;
vectorPotential[3 * idxEval + 2] = aPhi * ePhiZ;
}
}
}
/**
* 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
*/
void magneticFieldCircularFilament(double *center, double *normal, double radius,
double current, int nEvalPos, double *evalPos, double *magneticField) {
if (!isfinite(radius) || radius <= 0.0) {
printf("radius must be finite and positive, but is %g\n", radius);
return;
}
double bPrefactor = MU_0_BY_PI * current / radius;
// squared length of normal vector
double nLen2 = normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2];
if (nLen2 == 0.0) {
printf("length of normal vector must not be zero");
return;
}
// length of normal vector
double nLen = sqrt(nLen2);
// unit normal vector of wire loop
double eX = normal[0] / nLen;
double eY = normal[1] / nLen;
double eZ = normal[2] / nLen;
for (int idxEval = 0; idxEval < nEvalPos; ++idxEval) {
// vector from center of wire loop to eval pos
double r0x = evalPos[3 * idxEval + 0] - center[0];
double r0y = evalPos[3 * idxEval + 1] - center[1];
double r0z = evalPos[3 * idxEval + 2] - center[2];
// z position along normal of wire loop
double alignedZ = eX * r0x + eY * r0y + eZ * r0z;
// normalized z component of evaluation location in coordinate system of wire loop
double zP = alignedZ / radius;
// r0 projected onto axis of wire loop
double rParallelX = alignedZ * eX;
double rParallelY = alignedZ * eY;
double rParallelZ = alignedZ * eZ;
// vector perpendicular to axis of wire loop, pointing at evaluation pos
double rPerpX = r0x - rParallelX;
double rPerpY = r0y - rParallelY;
double rPerpZ = r0z - rParallelZ;
// perpendicular distance squared between evalPos and axis of wire loop
double alignedRSq = rPerpX * rPerpX + rPerpY * rPerpY + rPerpZ * rPerpZ;
double rhoP;
if (alignedRSq > 0.0) {
// radial unit vector is only defined if evaluation pos is off-axis
// perpendicular distance between evalPos and axis of wire loop
double alignedR = sqrt(alignedRSq);
// unit vector in radial direction
double eRX = rPerpX / alignedR;
double eRY = rPerpY / alignedR;
double 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
double bRho = bPrefactor * circularWireLoop_B_rho(rhoP, zP);
// add contribution from B_rho of wire loop to result
magneticField[3 * idxEval + 0] = bRho * eRX;
magneticField[3 * idxEval + 1] = bRho * eRY;
magneticField[3 * idxEval + 2] = bRho * eRZ;
} else {
rhoP = 0.0;
}
// compute vertical component of normalized magnetic field
// and scale by current and mu_0
double bZ = bPrefactor * circularWireLoop_B_z(rhoP, zP);
// add contribution from B_z of wire loop to result
magneticField[3 * idxEval + 0] += bZ * eX;
magneticField[3 * idxEval + 1] += bZ * eY;
magneticField[3 * idxEval + 2] += bZ * eZ;
}
}
// --------------------------------------------------
/**
* 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+1) index in {@code vertices} to take into account
* @param idxEvalStart first index in {@code evalPos} to take into account
* @param idxEvalEnd (last+1) 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
*/
void kernelVectorPotentialPolygonFilament(
double *vertices,
double current,
double *evalPos,
double *vectorPotential,
int idxSourceStart,
int idxSourceEnd,
int idxEvalStart,
int idxEvalEnd,
bool useCompensatedSummation) {
double aPrefactor = MU_0_BY_2_PI * current;
// setup compensated summation objects
double *aXSum;
double *aYSum;
double *aZSum;
if (useCompensatedSummation) {
int numEvalPos = idxEvalEnd - idxEvalStart;
// need three doubles (s, cs, ccs) per eval pos --> see compsum.h
int numBytesToAllocate = 3 * numEvalPos * sizeof(double);
aXSum = (double *) malloc (numBytesToAllocate);
aYSum = (double *) malloc (numBytesToAllocate);
aZSum = (double *) malloc (numBytesToAllocate);
// initialize target array to zero
memset(aXSum, 0, numBytesToAllocate);
memset(aYSum, 0, numBytesToAllocate);
memset(aZSum, 0, numBytesToAllocate);
} else {
aXSum = NULL;
aYSum = NULL;
aZSum = NULL;
// initialize target array to zero
for (int idxEval = idxEvalStart; idxEval < idxEvalEnd; ++idxEval) {
memset(vectorPotential + 3 * idxEval, 0, 3 * sizeof(double));
}
}
double x_i = vertices[3 * idxSourceStart + 0];
double y_i = vertices[3 * idxSourceStart + 1];
double z_i = vertices[3 * idxSourceStart + 2];
for (int idxSource = idxSourceStart; idxSource < idxSourceEnd; ++idxSource) {
double x_f = vertices[3 * (idxSource + 1) + 0];
double y_f = vertices[3 * (idxSource + 1) + 1];
double z_f = vertices[3 * (idxSource + 1) + 2];
// vector from start to end of i:th wire segment
double dx = x_f - x_i;
double dy = y_f - y_i;
double dz = z_f - z_i;
// squared length of wire segment
double l2 = dx * dx + dy * dy + dz * dz;
if (l2 == 0.0) {
// skip zero-length segments: no contribution
continue;
}
// length of wire segment
double l = sqrt(l2);
// unit vector parallel to wire segment
double eX = dx / l;
double eY = dy / l;
double eZ = dz / l;
for (int idxEval = idxEvalStart; idxEval < idxEvalEnd; ++idxEval) {
// vector from start of wire segment to eval pos
double r0x = evalPos[3 * idxEval + 0] - x_i;
double r0y = evalPos[3 * idxEval + 1] - y_i;
double r0z = evalPos[3 * idxEval + 2] - z_i;
// z position along axis of wire segment
double alignedZ = eX * r0x + eY * r0y + eZ * r0z;
// normalized z component of evaluation location in coordinate system of wire segment
double zP = alignedZ / l;
// vector perpendicular to axis of wire segment, pointing at evaluation pos
double rPerpX = r0x - alignedZ * eX;
double rPerpY = r0y - alignedZ * eY;
double rPerpZ = r0z - alignedZ * eZ;
// perpendicular distance between evalPos and axis of wire segment
double alignedR = sqrt(rPerpX * rPerpX + rPerpY * rPerpY + rPerpZ * rPerpZ);
// normalized rho component of evaluation location in coordinate system of wire segment
double rhoP = alignedR / l;
// compute parallel component of magnetic vector potential, including current and mu_0
double aParallel = aPrefactor * straightWireSegment_A_z(rhoP, zP);
// add contribution from wire segment to result
if (useCompensatedSummation) {
compAdd(aParallel * eX, aXSum + 3 * (idxEval - idxEvalStart));
compAdd(aParallel * eY, aYSum + 3 * (idxEval - idxEvalStart));
compAdd(aParallel * eZ, aZSum + 3 * (idxEval - idxEvalStart));
} else {
vectorPotential[3 * idxEval + 0] += aParallel * eX;
vectorPotential[3 * idxEval + 1] += aParallel * eY;
vectorPotential[3 * idxEval + 2] += aParallel * eZ;
}
}
// shift to next point
x_i = x_f;
y_i = y_f;
z_i = z_f;
}
if (useCompensatedSummation) {
// obtain compensated sums from summation objects
for (int idxEval = idxEvalStart; idxEval < idxEvalEnd; ++idxEval) {
int relIdx = 3 * (idxEval - idxEvalStart);
vectorPotential[3 * idxEval + 0] = aXSum[relIdx + 0] + aXSum[relIdx + 1] + aXSum[relIdx + 2];
vectorPotential[3 * idxEval + 1] = aYSum[relIdx + 0] + aYSum[relIdx + 1] + aYSum[relIdx + 2];
vectorPotential[3 * idxEval + 2] = aZSum[relIdx + 0] + aZSum[relIdx + 1] + aZSum[relIdx + 2];
}
free(aXSum);
free(aYSum);
free(aZSum);
}
}
/**
* Compute the magnetic vector potential of a polygon filament
* at a number of evaluation locations.
*
* @param void (*vertexSupplier)(int i, double *point): callback to put i-th current carrier polygon vertex into point as [3: x, y, z]; 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+1) index in {@code vertices} to take into account
* @param idxEvalStart first index in {@code evalPos} to take into account
* @param idxEvalEnd (last+1) 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
*/
void kernelVectorPotentialPolygonFilamentVertexSupplier(