@@ -112,194 +112,21 @@ def compute_energy(self):
112
112
113
113
114
114
class TimeZeeman (Zeeman ):
115
- """
116
- The time dependent external field, also can vary with space
117
-
118
- The function time_fun must be a function which takes two arguments:
119
-
120
- def time_fun(pos, t):
121
- x, y, z = pos
122
- # compute Bx, By, Bz as a function of x y, z and t.
123
- Bx = ...
124
- By = ...
125
- Bz = ...
126
- return (Bx, By, Bz)
127
-
128
- Extra arguments can be passed to the function to allow more
129
- general code. These must be set when initialising the TimeZeeman class. For
130
- example:
131
-
132
- freq = 10e9
133
-
134
- def time_fun(pos, t, frequency):
135
- x, y, z = pos
136
- if x < 50:
137
- return (0.0, 0.0, np.sin(frequency*t))
138
- else:
139
- return (0.0, 0.0, 0.0)
140
-
141
- zee = TimeZeeman(time_fun, extra_args=[freq])
142
-
143
- These arguments are then passed into the time_fun code function in
144
- order.
145
115
146
116
"""
147
-
148
- def __init__ (self , time_fun , extra_args = [], name = 'TimeZeeman' ):
149
- self .time_fun = time_fun
150
- self .name = name
151
- self .jac = True
152
- self .extra_args = extra_args
153
-
154
- def setup (self , mesh , spin , Ms ):
155
- self .mesh = mesh
156
- self .spin = spin
157
- self .n = mesh .n
158
-
159
- self .Ms = Ms
160
- self .Ms_long = np .zeros (3 * mesh .n )
161
-
162
- # TODO: Check if it is necessary to define a 3D matrix for
163
- # the Ms vectors. Maybe there is a way that uses less memory
164
- # (see the calculation in the *compute_energy* function)
165
- self .Ms_long .shape = (3 , - 1 )
166
- for i in range (mesh .n ):
167
- self .Ms_long [:, i ] = Ms [i ]
168
-
169
- self .Ms_long .shape = (- 1 ,)
170
- self .field = np .zeros (3 * self .n )
171
-
172
- def compute_field (self , t = 0 , spin = None ):
173
- self .field [:] = helper .init_vector (self .time_fun ,
174
- self .mesh ,
175
- False ,
176
- t , * self .extra_args )
177
- return self .field
178
-
179
-
180
- class TimeZeemanSimple (Zeeman ):
181
-
182
- """
183
- Time Dependent Zeeman Interaction with no spatial dependence.
184
-
185
- The function time_fun must be a function which takes one argument:
186
-
187
- def time_fun(t):
188
- x, y, z = pos
189
- # compute Bx, By, Bz as a function of x y, z and t.
190
- Bx = ...
191
- By = ...
192
- Bz = ...
193
- return (Bx, By, Bz)
194
-
195
-
196
- Extra arguments can be passed to the function to allow more
197
- general code. These must be set when initialising the TimeZeeman class. For
198
- example:
199
-
200
- freq = 10e9
201
-
202
- def time_fun(t, frequency):
203
- return (0.0, 0.0, np.sin(frequency*t))
204
-
205
- zee = SimpleTimeZeeman(time_fun, extra_args=[freq])
206
-
207
- These arguments are then passed into the time_fun code function in
208
- order.
209
-
210
-
211
- """
212
-
213
- def __init__ (self , time_fun , extra_args = [], name = 'TimeZeemanFast' ):
214
- self .time_fun = time_fun
215
- self .name = name
216
- self .jac = True
217
- self ._v = np .zeros (3 )
218
- self .extra_args = extra_args
219
-
220
- def setup (self , mesh , spin , Ms ):
221
- self .mesh = mesh
222
- self .spin = spin
223
- self .n = mesh .n
224
-
225
- self .Ms = Ms
226
- self .Ms_long = np .zeros (3 * mesh .n )
227
-
228
- # TODO: Check if it is necessary to define a 3D matrix for
229
- # the Ms vectors. Maybe there is a way that uses less memory
230
- # (see the calculation in the *compute_energy* function)
231
- self .Ms_long .shape = (3 , - 1 )
232
- for i in range (mesh .n ):
233
- self .Ms_long [:, i ] = Ms [i ]
234
-
235
- self .Ms_long .shape = (- 1 ,)
236
- self .field = np .zeros (3 * self .n )
237
-
238
- def compute_field (self , t = 0 , spin = None ):
239
- v = self .time_fun (t , * self .extra_args )
240
- self .field [0 ::3 ] = v [0 ]
241
- self .field [1 ::3 ] = v [1 ]
242
- self .field [2 ::3 ] = v [2 ]
243
- #self.field[::3] = v
244
- return self .field
245
-
246
-
247
- class TimeZeemanFast (Zeeman ):
248
-
249
- """
250
- The time dependent external field, also can vary with space. This uses
251
- an unsafe setting function. Should not be used except by advanced users.
252
-
253
- The function must handle coordinates
254
-
255
- The function time_fun must be a function which takes three or more arguments. The time variable is always passed in as the first argument
256
- in params.
257
-
258
- e.g.
259
-
260
- from libc.math cimport sin
261
-
262
- def fast_sin_init(mesh, double[:] field, *params):
263
- t, axis, Bmax, fc = params
264
- for i in range(mesh.n):
265
- x, y, z = mesh.coordinates[i]
266
- if x < 10:
267
- field[3*i+0] = Bmax * axis[0] * sin(fc*t)
268
- field[3*i+1] = Bmax * axis[1] * sin(fc*t)
269
- field[3*i+2] = Bmax * axis[2] * sin(fc*t)
270
-
271
- Add the function to a user Cython module in fidimag/user/ and recompile.
272
-
117
+ The time dependent external field, also can vary with space
273
118
"""
274
119
275
- def __init__ (self , time_fun , extra_args = [], name = 'TimeZeemanFast' ):
120
+ def __init__ (self , H0 , time_fun , extra_args = [], name = 'TimeZeeman' ):
121
+ self .H0 = H0
276
122
self .time_fun = time_fun
277
123
self .name = name
278
124
self .jac = True
279
- self .extra_args = extra_args
280
125
281
126
def setup (self , mesh , spin , Ms ):
282
- self .mesh = mesh
283
- self .spin = spin
284
- self .n = mesh .n
285
-
286
- self .Ms = Ms
287
- self .Ms_long = np .zeros (3 * mesh .n )
288
-
289
- # TODO: Check if it is necessary to define a 3D matrix for
290
- # the Ms vectors. Maybe there is a way that uses less memory
291
- # (see the calculation in the *compute_energy* function)
292
- self .Ms_long .shape = (3 , - 1 )
293
- for i in range (mesh .n ):
294
- self .Ms_long [:, i ] = Ms [i ]
295
-
296
- self .Ms_long .shape = (- 1 ,)
297
- self .field = np .zeros (3 * self .n )
127
+ super (TimeZeeman , self ).setup (mesh , spin , Ms )
128
+ self .H_init = self .field .copy ()
298
129
299
130
def compute_field (self , t = 0 , spin = None ):
300
- helper .init_vector_func_fast (self .time_fun ,
301
- self .mesh ,
302
- self .field ,
303
- False ,
304
- t , * self .extra_args )
305
- return self .field
131
+ self .field [:] = self .H_init [:] * self .time_fun (t , * extra_args )
132
+ return self .field
0 commit comments