@@ -25,47 +25,43 @@ def normalise(a):
25
25
26
26
27
27
def init_vector (m0 , mesh , norm = False , * args ):
28
-
29
28
n = mesh .n
30
-
31
- spin = np .zeros ((n , 3 ))
29
+ field = np .zeros ((n , 3 ))
32
30
33
31
if isinstance (m0 , list ) or isinstance (m0 , tuple ):
34
- spin [:, :] = m0
35
- spin = np .reshape (spin , 3 * n , order = 'C' )
32
+ field [:, :] = m0
33
+ field = np .reshape (field , 3 * n , order = 'C' )
36
34
37
35
elif hasattr (m0 , '__call__' ):
36
+ # Check only once that the function returns appropriately...
37
+ v = m0 (mesh .coordinates [0 ], * args )
38
+ if len (v ) != 3 :
39
+ raise Exception (
40
+ 'The length of the value in init_vector method must be 3.' )
38
41
for i in range (n ):
39
- v = m0 (mesh .coordinates [i ], * args )
40
- if len (v ) != 3 :
41
- raise Exception (
42
- 'The length of the value in init_vector method must be 3.' )
43
- spin [i , :] = v [:]
44
- spin = np .reshape (spin , 3 * n , order = 'C' )
42
+ field [i , :] = m0 (mesh .coordinates [i ], * args )
43
+ field = np .reshape (field , 3 * n , order = 'C' )
45
44
46
45
elif isinstance (m0 , np .ndarray ):
47
46
if m0 .shape == (3 , ):
48
- spin [:] = m0 # broadcasting
47
+ field [:] = m0 # broadcasting
49
48
else :
50
- spin .shape = (- 1 )
51
- spin [:] = m0 # overwriting the whole thing
52
-
53
- spin .shape = (- 1 ,)
54
-
49
+ field .shape = (- 1 )
50
+ field [:] = m0 # overwriting the whole thing
51
+ field .shape = (- 1 ,)
55
52
if norm :
56
- normalise (spin )
53
+ normalise (field )
57
54
58
- return spin
55
+ return field
59
56
60
57
61
58
def init_scalar (value , mesh , * args ):
62
-
63
59
n = mesh .n
64
-
65
60
mesh_v = np .zeros (n )
66
61
67
62
if isinstance (value , (int , float )):
68
63
mesh_v [:] = value
64
+
69
65
elif hasattr (value , '__call__' ):
70
66
for i in range (n ):
71
67
mesh_v [i ] = value (mesh .coordinates [i ], * args )
0 commit comments