@@ -183,6 +183,168 @@ def magnet_cuboid_Bfield(
183
183
B /= 4 * np .pi
184
184
return B
185
185
186
+ # # def axis_string_to_int(s):
187
+ # # if s == 'x':
188
+ # # return 0
189
+ # # elif s == 'y':
190
+ # # return 1
191
+ # # elif s == 'z':
192
+ # # return 2
193
+ # # else:
194
+ # # raise Exception('Sorry, input needs to be "x", "y" or "z"')
195
+
196
+
197
+ # # def rotate(array, initial_axis, final_axis, abs):
198
+ # # """
199
+ # # returns adapted array after a coorinate transformation, where initial_axis is rotated to final_axis
200
+
201
+ # # Args:
202
+ # # array (ndarray, dim (n,3)): array where columns are exchanged according to rotation.
203
+ # # initial_axis (string): axis that gets rotated from ('x', 'y', or 'z')
204
+ # # final_axis (string): axis that gets rotated into ('x', 'y', or 'z')
205
+
206
+ # # Returns:
207
+ # # ndarray, dim (n,3): new array with exchanged axes
208
+
209
+ # # """
210
+ # # assert array.ndim == 2 and array.shape[1] == 3, 'wrong dimensions in input array'
211
+
212
+ # # initial_axis = axis_string_to_int(initial_axis)
213
+ # # final_axis = axis_string_to_int(final_axis)
214
+
215
+ # # if initial_axis == final_axis:
216
+ # # return array
217
+ # # else:
218
+ # # array_new = np.copy(array)
219
+ # # array_new[:,final_axis] = array[:,initial_axis]
220
+ # # if abs:
221
+ # # array_new[:,initial_axis] = array[:,final_axis]
222
+ # # else:
223
+ # # array_new[:,initial_axis] = -array[:,final_axis]
224
+
225
+ # # return array_new
226
+
227
+
228
+ # # #####################
229
+ # # # load NN
230
+
231
+ # # import torch
232
+ # # import torch.nn as nn
233
+ # # import sys
234
+ # # from NN.networks import PINN_network
235
+
236
+ # # # path_weights_B = '/home/stipsitzm/23_Hippie/hippie_magnetics/results_demag_one/B_global/model_multiplicative_field_correction_0_SiLU_MIN-1e-5.pth'
237
+ # # path_weights_B = 'NN/model_multiplicative_field_correction_0_SiLU_MIN-1e-5.pth'
238
+
239
+ # # # # networks for global fields (one network for whole space)
240
+ # # B_model = PINN_network(6, activation=nn.SiLU(), final_activation=nn.SiLU(),defined_with_dropout=True) # (a,b,Chi, x,y,z) -> (B_x, B_y, B_z)
241
+ # # B_model.load_state_dict(torch.load(path_weights_B, map_location=torch.device('cpu')))
242
+
243
+ # # #######################
244
+
245
+
246
+ # # def eval_NN(dimensions, observers, susceptibilities):
247
+
248
+ # # dimensions_normalized = dimensions.T / dimensions[:,2]
249
+ # # dimensions_normalized = dimensions_normalized.T
250
+ # # dimensions_normalized[:,2] = susceptibilities
251
+ # # dimension_batch = torch.tensor(dimensions_normalized, dtype=torch.float32)
252
+
253
+ # # observers_normalized = np.abs(observers) / dimensions
254
+ # # xyz_batch = torch.tensor(observers_normalized, dtype=torch.float32)
255
+
256
+ # # res = B_model(dimension_batch, xyz_batch)
257
+
258
+ # # return res
259
+
260
+
261
+ # # def magnet_cuboid_Bfield_NN(
262
+ # # observers: np.ndarray,
263
+ # # dimensions: np.ndarray,
264
+ # # polarizations: np.ndarray,
265
+ # # susceptibilities: np.ndarray,
266
+ # # ):
267
+ # # """B-field of homogeneously magnetized cuboids in Cartesian Coordinates.
268
+
269
+ # # The cuboids sides are parallel to the coordinate axes. The geometric centers of the
270
+ # # cuboids lie in the origin. The output is proportional to the polarization magnitude
271
+ # # and independent of the length units chosen for observers and dimensions.
272
+
273
+ # # Parameters
274
+ # # ----------
275
+ # # observers: ndarray, shape (n,3)
276
+ # # Observer positions (x,y,z) in Cartesian coordinates.
277
+
278
+ # # dimensions: ndarray, shape (n,3)
279
+ # # Length of cuboids sides.
280
+
281
+ # # polarizations: ndarray, shape (n,3)
282
+ # # Magnetic polarization vectors.
283
+
284
+ # # susceptibilities: ndarray, shape (n,)
285
+ # # Magnetic susceptibilities
286
+
287
+ # # Returns
288
+ # # -------
289
+ # # B-Field: ndarray, shape (n,3)
290
+ # # B-field generated by Cuboids at observer positions.
291
+
292
+ # # """
293
+
294
+ # # polarizations_x = np.copy(polarizations)
295
+ # # polarizations_x[:,1] = 0
296
+ # # polarizations_x[:,2] = 0
297
+ # # polarizations_y = np.copy(polarizations)
298
+ # # polarizations_y[:,0] = 0
299
+ # # polarizations_y[:,2] = 0
300
+ # # polarizations_z = np.copy(polarizations)
301
+ # # polarizations_z[:,0] = 0
302
+ # # polarizations_z[:,1] = 0
303
+
304
+ # # Bx = magnet_cuboid_Bfield(observers, dimensions, polarizations_x)
305
+ # # By = magnet_cuboid_Bfield(observers, dimensions, polarizations_y)
306
+ # # Bz = magnet_cuboid_Bfield(observers, dimensions, polarizations_z)
307
+
308
+
309
+
310
+ # # # z-polarization
311
+ # # res_z = eval_NN(dimensions, observers, susceptibilities)
312
+ # # res_z = res_z.detach().numpy()
313
+
314
+
315
+ # # # x-polarization
316
+ # # dimensions_rotate_x = rotate(dimensions, 'x', 'z', True)
317
+ # # observers_rotate_x = rotate(observers, 'x', 'z', True)
318
+
319
+ # # res_x = eval_NN(dimensions_rotate_x, observers_rotate_x, susceptibilities)
320
+ # # res_x = res_x.detach().numpy()
321
+ # # res_x = rotate(res_x, 'z', 'x', True)
322
+
323
+ # # # y-polarization
324
+ # # dimensions_rotate_y = rotate(dimensions, 'y', 'z', True)
325
+ # # observers_rotate_y = rotate(observers, 'y', 'z', True)
326
+
327
+ # # res_y = eval_NN(dimensions_rotate_y, observers_rotate_y, susceptibilities)
328
+ # # res_y = res_y.detach().numpy()
329
+ # # res_y = rotate(res_y, 'z', 'y', True)
330
+
331
+ # # result = Bx * res_x + By * res_y + Bz * res_z
332
+
333
+ # # return result
334
+
335
+ # # # observers = np.array([[0,0,1], [1,2,3]])
336
+ # # # dimensions = np.array([[1,1,1], [1,2,3]])
337
+ # # # polarizations = np.array([[0,0,3], [1,1,1]])
338
+ # # # susceptibilities = np.array([1,1])
339
+
340
+ # # # res_NN = magnet_cuboid_Bfield_NN(observers, dimensions, polarizations, susceptibilities)
341
+ # # # res = magnet_cuboid_Bfield(observers, dimensions, polarizations)
342
+
343
+ # # # print('res NN', res_NN)
344
+ # # # print('res', res)
345
+
346
+ # # # print('res NN norm', np.linalg.norm(res_NN))
347
+ # # # print('res norm', np.linalg.norm(res))
186
348
187
349
def BHJM_magnet_cuboid (
188
350
field : str ,
@@ -262,3 +424,84 @@ def BHJM_magnet_cuboid(
262
424
raise ValueError ( # pragma: no cover
263
425
"`output_field_type` must be one of ('B', 'H', 'M', 'J'), " f"got { field !r} "
264
426
)
427
+
428
+
429
+
430
+ # # # def BHJM_magnet_cuboid_NN(
431
+ # # # field: str,
432
+ # # # observers: np.ndarray,
433
+ # # # dimension: np.ndarray,
434
+ # # # polarization: np.ndarray,
435
+ # # # ) -> np.ndarray:
436
+ # # # """
437
+ # # # - translate cuboid core field to BHJM
438
+ # # # - treat special cases
439
+ # # # """
440
+
441
+ # # # RTOL_SURFACE = 1e-15 # relative distance tolerance to be considered on surface
442
+
443
+ # # # check_field_input(field)
444
+
445
+ # # # pol_x, pol_y, pol_z = polarization.T
446
+ # # # a, b, c = np.abs(dimension.T) / 2
447
+ # # # x, y, z = observers.T
448
+
449
+ # # # # allocate for output
450
+ # # # BHJM = polarization.astype(float)
451
+
452
+ # # # # SPECIAL CASE 1: polarization = (0,0,0)
453
+ # # # mask_pol_not_null = ~(
454
+ # # # (pol_x == 0) * (pol_y == 0) * (pol_z == 0)
455
+ # # # ) # 2x faster than np.all()
456
+
457
+ # # # # SPECIAL CASE 2: 0 in dimension
458
+ # # # mask_dim_not_null = (a * b * c).astype(bool)
459
+
460
+ # # # # SPECIAL CASE 3: observer lies on-edge/corner
461
+ # # # # EPSILON to account for numerical imprecision when e.g. rotating
462
+ # # # # /a /b /c to account for the missing scaling (EPSILON is large when
463
+ # # # # a is e.g. EPSILON itself)
464
+
465
+ # # # # on-surface is not a special case
466
+ # # # mask_surf_x = abs(x_dist := abs(x) - a) < RTOL_SURFACE * a # on surface
467
+ # # # mask_surf_y = abs(y_dist := abs(y) - b) < RTOL_SURFACE * b # on surface
468
+ # # # mask_surf_z = abs(z_dist := abs(z) - c) < RTOL_SURFACE * c # on surface
469
+
470
+ # # # # inside-outside
471
+ # # # mask_inside_x = x_dist < RTOL_SURFACE * a
472
+ # # # mask_inside_y = y_dist < RTOL_SURFACE * b
473
+ # # # mask_inside_z = z_dist < RTOL_SURFACE * c
474
+ # # # mask_inside = mask_inside_x & mask_inside_y & mask_inside_z
475
+
476
+ # # # # on edge (requires on-surface and inside-outside)
477
+ # # # mask_xedge = mask_surf_y & mask_surf_z & mask_inside_x
478
+ # # # mask_yedge = mask_surf_x & mask_surf_z & mask_inside_y
479
+ # # # mask_zedge = mask_surf_x & mask_surf_y & mask_inside_z
480
+ # # # mask_not_edge = ~(mask_xedge | mask_yedge | mask_zedge)
481
+
482
+ # # # mask_gen = mask_pol_not_null & mask_dim_not_null & mask_not_edge
483
+
484
+ # # # if field == "J":
485
+ # # # BHJM[~mask_inside] = 0
486
+ # # # return BHJM
487
+
488
+ # # # if field == "M":
489
+ # # # BHJM[~mask_inside] = 0
490
+ # # # return BHJM / MU0
491
+
492
+ # # # BHJM *= 0 # return (0,0,0) for all special cases
493
+ # # # BHJM[mask_gen] = magnet_cuboid_Bfield_NN(
494
+ # # # observers=observers[mask_gen],
495
+ # # # dimensions=dimension[mask_gen],
496
+ # # # polarizations=polarization[mask_gen],
497
+ # # # )
498
+ # # # if field == "B":
499
+ # # # return BHJM
500
+
501
+ # # # if field == "H":
502
+ # # # BHJM[mask_inside] -= polarization[mask_inside]
503
+ # # # return BHJM / MU0
504
+
505
+ # # # raise ValueError( # pragma: no cover
506
+ # # # "`output_field_type` must be one of ('B', 'H', 'M', 'J'), " f"got {field!r}"
507
+ # # # )
0 commit comments