@@ -43,10 +43,32 @@ def get_susceptibilities(*sources, susceptibility=None):
4343 if src .parent is None :
4444 raise ValueError ("No susceptibility defined in any parent collection" )
4545 susceptibilities .extend (get_susceptibilities (src .parent ))
46- else :
46+ elif not hasattr (susceptibility , "__len__" ):
47+ susceptibilities .append ((susceptibility , susceptibility , susceptibility ))
48+ elif len (susceptibility ) == 3 :
4749 susceptibilities .append (susceptibility )
50+ else :
51+ raise ValueError ("susceptibility is not scalar or array fo length 3" )
4852 return susceptibilities
4953
54+ def get_H_ext (* sources , H_ext = None ):
55+ """Return a list of length (len(sources)) with H_ext values
56+ Priority is given at the source level, hovever if value is not found, it is searched up the
57+ the parent tree, if available. Sets H_ext to zero if no value is found when reached the top
58+ level of the tree"""
59+ H_exts = []
60+ for src in sources :
61+ H_ext = getattr (src , "H_ext" , None )
62+ if H_ext is None :
63+ if src .parent is None :
64+ #print("Warning: No value for H_ext defined in any parent collection. H_ext set to zero.")
65+ H_exts .append ((0.0 ,0.0 ,0.0 ))
66+ else :
67+ H_exts .extend (get_H_ext (src .parent ))
68+ else :
69+ H_exts .append (H_ext )
70+ return H_exts
71+
5072
5173def demag_tensor (
5274 src_list ,
@@ -348,7 +370,19 @@ def apply_demag(
348370 raise ValueError (
349371 "Apply_demag input collection and susceptibility must have same length."
350372 )
351- S = np .diag (np .tile (susceptibility , 3 )) # shape ii, jj
373+ susceptibility = np .reshape (
374+ susceptibility , 3 * n , order = "F"
375+ )
376+ S = np .diag (susceptibility ) # shape ii, jj
377+
378+ # set up H_ext
379+ H_ext = get_H_ext (* magnets_list )
380+ H_ext = np .array (H_ext )
381+ if len (H_ext ) != n :
382+ raise ValueError ("Apply_demag input collection and H_ext must have same length." )
383+ H_ext = np .reshape (
384+ H_ext , (3 * n , 1 ), order = "F"
385+ )
352386
353387 # set up T (3 pol unit, n cells, n positions, 3 Bxyz)
354388 with timelog ("Demagnetization tensor calculation" , min_log_time = min_log_time ):
@@ -362,7 +396,7 @@ def apply_demag(
362396 T *= magpy .mu_0
363397 T = T .swapaxes (2 , 3 ).reshape ((3 * n , 3 * n )).T # shape ii, jj
364398
365- pol_tolal = pol_magnets
399+ pol_total = pol_magnets
366400
367401 if currents_list :
368402 with timelog (
@@ -371,14 +405,14 @@ def apply_demag(
371405 pos = np .array ([src .position for src in magnets_list ])
372406 pol_currents = magpy .getB (currents_list , pos , sumup = True )
373407 pol_currents = np .reshape (pol_currents , (3 * n , 1 ), order = "F" )
374- pol_tolal += np .matmul (S , pol_currents )
408+ pol_total += np .matmul (S , pol_currents )
375409
376410 # set up Q
377411 Q = np .eye (3 * n ) - np .matmul (S , T )
378412
379413 # determine new polarization vectors
380414 with timelog ("Solving of linear system" , min_log_time = 1 ):
381- pol_new = np .linalg .solve (Q , pol_tolal )
415+ pol_new = np .linalg .solve (Q , pol_total + np . matmul ( S , H_ext ) )
382416
383417 pol_new = np .reshape (pol_new , (n , 3 ), order = "F" )
384418 # pol_new *= .4*np.pi
0 commit comments