Skip to content

Commit 3967e6e

Browse files
committed
Add TimeZeemanFast to handle Cython functions
1 parent 74cc41e commit 3967e6e

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

fidimag/micro/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .demag import Demag
33
from .exchange import UniformExchange
44
from .exchange_rkky import ExchangeRKKY
5-
from .zeeman import Zeeman, TimeZeeman, SimpleTimeZeeman
5+
from .zeeman import Zeeman, TimeZeeman, SimpleTimeZeeman, TimeZeemanFast
66
from .anisotropy import UniaxialAnisotropy
77
from .dmi import DMI
88
from .baryakhtar import LLBar, LLBarFull

fidimag/micro/zeeman.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fidimag.common.helper as helper
55
import inspect
66

7+
78
class Zeeman(object):
89

910
"""
@@ -153,10 +154,10 @@ def setup(self, mesh, spin, Ms):
153154
self.field = np.zeros(3 * self.n)
154155

155156
def compute_field(self, t=0, spin=None):
156-
self.field[:] = helper.init_vector(self.time_fun,
157-
self.mesh,
158-
False,
159-
t)
157+
self.field[:] = helper.init_vector_func_fast(self.time_fun,
158+
self.mesh,
159+
False,
160+
t)
160161
return self.field
161162

162163

@@ -182,6 +183,7 @@ def __init__(self, time_fun, name='TimeZeeman'):
182183
self.time_fun = time_fun
183184
self.name = name
184185
self.jac = True
186+
self._v = np.zeros(3)
185187

186188
def setup(self, mesh, spin, Ms):
187189
self.mesh = mesh
@@ -206,4 +208,58 @@ def compute_field(self, t=0, spin=None):
206208
self.field[0::3] = v[0]
207209
self.field[1::3] = v[1]
208210
self.field[2::3] = v[2]
209-
return self.field
211+
#self.field[::3] = v
212+
return self.field
213+
214+
215+
class TimeZeemanFast(Zeeman):
216+
217+
"""
218+
The time dependent external field, also can vary with space
219+
220+
The function time_fun must be a function which takes two arguments:
221+
222+
def time_fun(mesh, t):
223+
x, y, z = pos
224+
# compute Bx, By, Bz as a function of x y, z and t.
225+
Bx = ...
226+
By = ...
227+
Bz = ...
228+
return (Bx, By, Bz)
229+
230+
Add the function to the user modules and recompile.
231+
232+
233+
"""
234+
235+
def __init__(self, time_fun, extra_args, name='TimeZeeman'):
236+
self.time_fun = time_fun
237+
self.name = name
238+
self.jac = True
239+
self.extra_args = extra_args
240+
241+
def setup(self, mesh, spin, Ms):
242+
self.mesh = mesh
243+
self.spin = spin
244+
self.n = mesh.n
245+
246+
self.Ms = Ms
247+
self.Ms_long = np.zeros(3 * mesh.n)
248+
249+
# TODO: Check if it is necessary to define a 3D matrix for
250+
# the Ms vectors. Maybe there is a way that uses less memory
251+
# (see the calculation in the *compute_energy* function)
252+
self.Ms_long.shape = (3, -1)
253+
for i in range(mesh.n):
254+
self.Ms_long[:, i] = Ms[i]
255+
256+
self.Ms_long.shape = (-1,)
257+
self.field = np.zeros(3 * self.n)
258+
259+
def compute_field(self, t=0, spin=None):
260+
helper.init_vector_func_fast(self.time_fun,
261+
self.mesh,
262+
self.field,
263+
False,
264+
t, *self.extra_args)
265+
return self.field

0 commit comments

Comments
 (0)