@@ -116,16 +116,63 @@ def test_scalarpotential_Bfield(self):
116116 Bscalar .set_points (points )
117117 B1 = np .array (Bscalar .B ())
118118 dB1_by_dX = np .array (Bscalar .dB_by_dX ())
119+
119120 # Analytical Formula for B
120121 rphiz = [[np .sqrt (np .power (point [0 ], 2 ) + np .power (point [1 ], 2 )), np .arctan2 (point [1 ], point [0 ]), point [2 ]] for point in points ]
121122 B2 = np .array ([[0.2 * point [2 ]+ 0.8 * point [0 ], (0.1 + 0.3 * point [2 ])/ point [0 ], 0.2 * point [0 ]+ 0.3 * point [1 ]+ point [2 ]] for point in rphiz ])
123+ # Convert to Cartesian coordinates
124+ r = np .sqrt (np .power (points [:, 0 ], 2 ) + np .power (points [:, 1 ], 2 ))
125+ phi = np .arctan2 (points [:, 1 ], points [:, 0 ])
126+ z = points [:, 2 ]
127+ B2_cart = np .zeros_like (B2 )
128+ # Bx = Br cos(phi) - Bphi sin(phi)
129+ B2_cart [:, 0 ] = B2 [:, 0 ] * np .cos (phi ) - B2 [:, 1 ] * np .sin (phi )
130+ # By = Br sin(phi) + Bphi cos(phi)
131+ B2_cart [:, 1 ] = B2 [:, 0 ] * np .sin (phi ) + B2 [:, 1 ] * np .cos (phi )
132+ B2_cart [:, 2 ] = B2 [:, 2 ]
122133 dB2_by_dX = np .array ([
123134 [[0.8 * np .cos (point [1 ]), - (np .cos (point [1 ])/ point [0 ]** 2 )* (0.1 + 0.3 * point [2 ]), 0.2 * np .cos (point [1 ])- 0.3 * np .sin (point [1 ])/ point [0 ]],
124135 [0.8 * np .sin (point [1 ]), - (np .sin (point [1 ])/ point [0 ]** 2 )* (0.1 + 0.3 * point [2 ]), 0.2 * np .sin (point [1 ])+ 0.3 * np .cos (point [1 ])/ point [0 ]],
125136 [0.2 , 0.3 / point [0 ], 1 ]] for point in rphiz ])
137+ dBxdx = dB1_by_dX [:, 0 , 0 ]
138+ dBxdy = dB1_by_dX [:, 1 , 0 ]
139+ dBxdz = dB1_by_dX [:, 2 , 0 ]
140+ dBydx = dB1_by_dX [:, 0 , 1 ]
141+ dBydy = dB1_by_dX [:, 1 , 1 ]
142+ dBydz = dB1_by_dX [:, 2 , 1 ]
143+ dB1_by_dX_cyl = np .zeros_like (dB2_by_dX )
144+ dcosphidx = - points [:, 0 ]** 2 / r ** 3 + 1 / r
145+ dsinphidx = - points [:, 0 ]* points [:, 1 ]/ r ** 3
146+ dcosphidy = - points [:, 0 ]* points [:, 1 ]/ r ** 3
147+ dsinphidy = - points [:, 1 ]** 2 / r ** 3 + 1 / r
148+ Bx = B1 [:, 0 ]
149+ By = B1 [:, 1 ]
150+ # Br = Bx cos(phi) + By sin(phi)
151+ dB1_by_dX_cyl [:, 0 , 0 ] = dBxdx * np .cos (phi ) + Bx * dcosphidx + dBydx * np .sin (phi ) \
152+ + By * dsinphidx
153+ dB1_by_dX_cyl [:, 1 , 0 ] = dBxdy * np .cos (phi ) + Bx * dcosphidy + dBydy * np .sin (phi ) \
154+ + By * dsinphidy
155+ dB1_by_dX_cyl [:, 2 , 0 ] = dBxdz * np .cos (phi ) + dBydz * np .sin (phi )
156+ # Bphi = - sin(phi) Bx + cos(phi) By
157+ dB1_by_dX_cyl [:, 0 , 1 ] = - dBxdx * np .sin (phi ) - Bx * dsinphidx + dBydx * np .cos (phi ) \
158+ + By * dcosphidx
159+ dB1_by_dX_cyl [:, 1 , 1 ] = - dBxdy * np .sin (phi ) - Bx * dsinphidy + dBydy * np .cos (phi ) \
160+ + By * dcosphidy
161+ dB1_by_dX_cyl [:, 2 , 1 ] = - dBxdz * np .sin (phi ) + dBydz * np .cos (phi )
162+ dB1_by_dX_cyl [:, :, 2 ] = dB1_by_dX [:, :, 2 ]
126163 # Verify
127- assert np .allclose (B1 , B2 )
128- assert np .allclose (dB1_by_dX , dB2_by_dX )
164+ assert np .allclose (B1 , B2_cart )
165+ assert np .allclose (dB1_by_dX_cyl , dB2_by_dX )
166+
167+ # Check for divergence-free condition for dipole field
168+ # Set up magnetic field scalar potential
169+ PhiStr = "Z/(R*R + Z*Z)**(3/2)"
170+ # Set up scalar potential B
171+ Bscalar = ScalarPotentialRZMagneticField (PhiStr )
172+ Bscalar .set_points (points )
173+ dB1_by_dX = np .array (Bscalar .dB_by_dX ())
174+ divB = dB1_by_dX [:, 0 , 0 ] + dB1_by_dX [:, 1 , 1 ] + dB1_by_dX [:, 2 , 2 ]
175+ assert np .allclose (np .abs (divB ), 0 )
129176
130177 def test_circularcoil_Bfield (self ):
131178 current = 1.2e7
@@ -284,8 +331,8 @@ def test_circularcoil_Bfield_toroidal_arrangement(self):
284331 N_coils = 30
285332
286333 N_turns = 3
287- a1 = 10 / 2 * 0.0254
288- a2 = 19.983 / 2 * 0.0254
334+ a1 = 10 / 2 * 0.0254
335+ a2 = 19.983 / 2 * 0.0254
289336 r_array = np .linspace (a1 , a2 , N_turns )
290337 I_amp = 433 * (33 / N_turns )
291338
@@ -420,16 +467,16 @@ def test_Reiman(self):
420467 x = points [:, 0 ]
421468 y = points [:, 1 ]
422469 z = points [:, 2 ]
423- Bx = (y * np .sqrt (x ** 2 + y ** 2 ) + x * z * (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
424- 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 ))))) +
470+ Bx = (y * np .sqrt (x ** 2 + y ** 2 ) + x * z * (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
471+ 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 ))))) +
425472 0.06 * x * (1 - np .sqrt (x ** 2 + y ** 2 ))* ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 *
426473 np .sin (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 )))))/ (x ** 2 + y ** 2 )
427- By = (- 1. * x * np .sqrt (x ** 2 + y ** 2 ) + y * z * (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
428- 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 ))))) +
474+ By = (- 1. * x * np .sqrt (x ** 2 + y ** 2 ) + y * z * (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
475+ 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 ))))) +
429476 0.06 * y * (1 - np .sqrt (x ** 2 + y ** 2 ))* ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 *
430477 np .sin (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 )))))/ (x ** 2 + y ** 2 )
431- Bz = (- ((- 1 + np .sqrt (x ** 2 + y ** 2 ))* (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
432- 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 )))))) -
478+ Bz = (- ((- 1 + np .sqrt (x ** 2 + y ** 2 ))* (0.15 + 0.38 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 ) -
479+ 0.06 * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .cos (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 )))))) -
433480 0.06 * z * ((- 1 + np .sqrt (x ** 2 + y ** 2 ))** 2 + z ** 2 )** 2 * np .sin (np .arctan2 (y , x ) - 6 * np .arctan (z / (- 1 + np .sqrt (x ** 2 + y ** 2 )))))/ np .sqrt (x ** 2 + y ** 2 )
434481 B2 = np .array (np .vstack ((Bx , By , Bz )).T )
435482 assert np .allclose (B1 , B2 )
0 commit comments