17
17
class Demag (Energy ):
18
18
19
19
def __init__ (self , name = 'Demag' , pbc_2d = False ,
20
- pbc_options = default_options ):
20
+ pbc_options = default_options , calc_every = 0 ):
21
21
self .name = name
22
22
self .oommf = True
23
23
self .pbc_2d = pbc_2d
24
24
self .pbc_options = pbc_options
25
25
self .jac = False
26
+ self .calc_every = calc_every
26
27
27
28
def setup (self , mesh , spin , Ms , Ms_inv ):
28
29
super (Demag , self ).setup (mesh , spin , Ms , Ms_inv )
29
30
30
31
if self .pbc_2d is True :
31
-
32
32
self .demag = clib .FFTDemag (self .dx , self .dy , self .dz ,
33
33
self .nx , self .ny , self .nz , tensor_type = '2d_pbc' )
34
-
35
34
nxyz = self .nx * self .ny * self .nz
36
35
tensors = np .zeros (6 * nxyz , dtype = np .float )
37
-
38
36
pbc_2d_error = 1e-10
39
37
sample_repeat_nx = - 1
40
38
sample_repeat_ny = - 1
41
39
asymptotic_radius = 32.0
42
40
dipolar_radius = 10000.0
43
- tensor_file_name = ''
44
-
41
+ tensor_file_name = None
45
42
options = self .pbc_options
46
43
47
44
if 'sample_repeat_nx' in options :
@@ -56,9 +53,8 @@ def setup(self, mesh, spin, Ms, Ms_inv):
56
53
if 'tensor_file_name' in options :
57
54
tensor_file_name = options ['tensor_file_name' ]
58
55
59
- if len ( tensor_file_name ) > 0 :
56
+ if tensor_file_name :
60
57
if not (os .path .exists (tensor_file_name + '.npz' )):
61
-
62
58
self .demag .compute_tensors_2dpbc (tensors , pbc_2d_error , sample_repeat_nx , sample_repeat_ny , dipolar_radius )
63
59
else :
64
60
npzfile = np .load (tensor_file_name + '.npz' )
@@ -69,7 +65,6 @@ def setup(self, mesh, spin, Ms, Ms_inv):
69
65
self .demag .compute_tensors_2dpbc (tensors , pbc_2d_error , sample_repeat_nx , sample_repeat_ny , dipolar_radius )
70
66
else :
71
67
tensors = npzfile ['tensors' ]
72
-
73
68
geo_arr = np .array ([self .nx , self .ny , self .nz , self .dx , self .dy , self .dz ], dtype = np .float )
74
69
np .savez (tensor_file_name + '.npz' , geo = geo_arr , tensors = tensors )
75
70
@@ -84,19 +79,34 @@ def setup(self, mesh, spin, Ms, Ms_inv):
84
79
self .demag = clib .FFTDemag (self .dx , self .dy , self .dz ,
85
80
self .nx , self .ny , self .nz ,
86
81
tensor_type = 'demag' )
82
+ if not self .calc_every :
83
+ self .compute_field = self .compute_field_every
84
+ else :
85
+ self .count = 0
86
+ self .compute_field = self .compute_field_periodically
87
87
88
-
89
-
90
- def compute_field (self , t = 0 , spin = None ):
88
+ def compute_field_every (self , t = 0 , spin = None ):
91
89
if spin is not None :
92
90
m = spin
93
91
else :
94
92
m = self .spin
95
-
96
93
self .demag .compute_field (m , self .Ms , self .field )
97
-
98
94
return self .field
99
95
96
+ def compute_field_periodically (self , t = 0 , spin = None ):
97
+ if spin is not None :
98
+ m = spin
99
+ else :
100
+ m = self .spin
101
+
102
+ if not (self .count % self .calc_every == 0 ):
103
+ self .count += 1
104
+ return self .field
105
+ else :
106
+ self .count += 1
107
+ self .demag .compute_field (m , self .Ms , self .field )
108
+ return self .field
109
+
100
110
def compute_exact (self ):
101
111
field = np .zeros (3 * self .mesh .n )
102
112
self .demag .compute_exact (self .spin , self .Ms , field )
0 commit comments