Skip to content

Commit 1abcdcc

Browse files
author
davidcorteso
committed
Refactor magnetisation variable
These commits update the energy classes to properly update variables dependent on Ms or mu_s, which were not updated when set_Ms or set_mu_s were called. The inverse magnetisation array was also modified as a field-like array of 3 * n elements (will change this in the next commit)
1 parent 35de180 commit 1abcdcc

21 files changed

+87
-60
lines changed

fidimag/atomistic/anisotropy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def __init__(self, Ku, axis=(1, 0, 0), name='Anisotropy'):
5050
self.axis = axis
5151
self.jac = True
5252

53-
def setup(self, mesh, spin, mu_s):
54-
super(Anisotropy, self).setup(mesh, spin, mu_s)
53+
def setup(self, mesh, spin, mu_s, mu_s_inv):
54+
super(Anisotropy, self).setup(mesh, spin, mu_s, mu_s_inv)
5555

5656
self._Ku = helper.init_scalar(self.Ku, self.mesh)
5757
self._axis = helper.init_vector(self.axis, self.mesh, True)
@@ -85,8 +85,8 @@ def __init__(self, Kc, name='CubicAnisotropy'):
8585
self.jac = True
8686

8787

88-
def setup(self, mesh, spin, mu_s):
89-
super(CubicAnisotropy, self).setup(mesh, spin, mu_s)
88+
def setup(self, mesh, spin, mu_s, mu_s_inv):
89+
super(CubicAnisotropy, self).setup(mesh, spin, mu_s, mu_s_inv)
9090
self._Kc = helper.init_scalar(self.Kc, self.mesh)
9191

9292
def compute_field(self, t=0, spin=None):

fidimag/atomistic/atomistic_driver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ def set_default_options(self, gamma=1, mu_s=1, alpha=0.1):
133133
# When we create the simulation, mu_s is set to the default value. This
134134
# is overriden when calling the set_mu_s method from the Simulation
135135
# class or when setting mu_s directly (property)
136+
# David Tue 19 Jun 2018: Not a very clear thing to do, we must set a
137+
# WARNING
136138
self._mu_s[:] = mu_s
139+
self._mu_s_inv[:] = 1. / mu_s
140+
137141
self.mu_s_const = mu_s
138142
self.gamma = gamma
139143
self.do_precession = True

fidimag/atomistic/demag.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, name='Demag'):
3737
self.name = name
3838
self.jac = True
3939

40-
def setup(self, mesh, spin, mu_s):
40+
def setup(self, mesh, spin, mu_s, mu_s_inv):
4141
self.mesh = mesh
4242
self.dx = mesh.dx
4343
self.dy = mesh.dy
@@ -46,6 +46,7 @@ def setup(self, mesh, spin, mu_s):
4646
self.ny = mesh.ny
4747
self.nz = mesh.nz
4848
self.spin = spin
49+
self.mu_s = mu_s
4950
self.n = mesh.n
5051
self.field = np.zeros(3 * self.n, dtype=np.float)
5152
unit_length = mesh.unit_length
@@ -55,6 +56,8 @@ def setup(self, mesh, spin, mu_s):
5556
self.scale = 1e-7 / unit_length**3
5657

5758
# could be wrong, needs carefully tests!!!
59+
# David Tue 19 Jun 2018: This variable is updated in the SIM class in
60+
# case mu_s changes
5861
self.mu_s_scale = mu_s * self.scale
5962

6063
self.demag = clib.FFTDemag(self.dx, self.dy, self.dz,

fidimag/atomistic/demag_full.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def __init__(self, name='DemagFull'):
1818
self.name = name
1919
self.jac = True
2020

21-
def setup(self, mesh, spin, mu_s):
22-
super(DemagFull, self).setup(mesh, spin, mu_s)
21+
def setup(self, mesh, spin, mu_s, mu_s_inv):
22+
super(DemagFull, self).setup(mesh, spin, mu_s, mu_s_inv)
2323

2424
unit_length = mesh.unit_length
2525
self.mu_s_scale = np.zeros(mesh.n, dtype=np.float)

fidimag/atomistic/demag_hexagonal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, name='DemagHexagonal'):
5151
self.name = name
5252
self.jac = True
5353

54-
def setup(self, mesh, spin, mu_s):
54+
def setup(self, mesh, spin, mu_s, mu_s_inv):
5555

5656
if mesh.mesh_type != 'hexagonal':
5757
raise Exception('This interaction is only defined'
@@ -89,6 +89,8 @@ def setup(self, mesh, spin, mu_s):
8989
self.scale = 1e-7 / unit_length ** 3
9090

9191
# could be wrong, needs carefully tests!!!
92+
# David Tue 19 Jun 2018: this variable is upated in the sim class
93+
# in case mu_s changes
9294
self.mu_s_scale = mu_s * self.scale
9395

9496
# This seems not to be necessary

fidimag/atomistic/dmi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def __init__(self, D, name='DMI', dmi_type='bulk'):
8181

8282
self.jac = True
8383

84-
def setup(self, mesh, spin, mu_s):
85-
super(DMI, self).setup(mesh, spin, mu_s)
84+
def setup(self, mesh, spin, mu_s, mu_s_inv):
85+
super(DMI, self).setup(mesh, spin, mu_s, mu_s_inv)
8686

8787
if self.mesh_type == 'hexagonal':
8888
self.n_ngbs_dmi = 6

fidimag/atomistic/energy.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Energy(object):
1212
1313
"""
1414

15-
def setup(self, mesh, spin, mu_s):
15+
def setup(self, mesh, spin, mu_s, mu_s_inv):
1616
self.mesh = mesh
1717
self.dx = mesh.dx * mesh.unit_length
1818
self.dy = mesh.dy * mesh.unit_length
@@ -30,16 +30,8 @@ def setup(self, mesh, spin, mu_s):
3030

3131
self.field = np.zeros(3 * self.n, dtype=np.float)
3232
self.energy = np.zeros(mesh.n, dtype=np.float)
33-
self.mu_s_inv = np.zeros(3 * self.n, dtype=np.float)
3433

35-
self.mu_s_inv.shape = (-1, 3)
36-
for i in range(mesh.n):
37-
if self.mu_s[i] == 0.0:
38-
self.mu_s_inv[i, :] = 0
39-
else:
40-
self.mu_s_inv[i, :] = 1.0 / self.mu_s[i]
41-
42-
self.mu_s_inv.shape = (-1,)
34+
self.mu_s_inv = mu_s_inv
4335

4436
self.xperiodic, self.yperiodic = (mesh.periodicity[0],
4537
mesh.periodicity[1])

fidimag/atomistic/exchange.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def __init__(self, J, name='Exchange'):
8686
self.name = name
8787
self.jac = False
8888

89-
def setup(self, mesh, spin, mu_s):
90-
super(Exchange, self).setup(mesh, spin, mu_s)
89+
def setup(self, mesh, spin, mu_s, mu_s_inv):
90+
super(Exchange, self).setup(mesh, spin, mu_s, mu_s_inv)
9191

9292
# Uniform exchange ----------------------------------------------------
9393
if isinstance(self.J, (int, float)):

fidimag/atomistic/sim.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ def __init__(self, mesh, name='unnamed', driver='llg',
6464

6565
# Magnetic moments definitions:
6666
# self._mu_s = np.zeros(self.n, dtype=np.float)
67+
# David: Be careful to change these references to the common
68+
# magnetisation array
6769
self._mu_s = self._magnetisation
68-
self._mu_s_inv = np.zeros(self.n, dtype=np.float)
70+
# Remember this is a 3 * n array:
71+
self._mu_s_inv = self._magnetisation_inv
6972

7073
# This is only for old C files using the xperiodic variable
7174
(self.xperiodic,
@@ -158,10 +161,12 @@ def mu_s_profile(r):
158161

159162
self._mu_s[:] = helper.init_scalar(value, self.mesh)
160163
nonzero = 0
164+
self._mu_s_inv.shape = (-1, 3)
161165
for i in range(self.n):
162166
if self._mu_s[i] > 0.0:
163167
self._mu_s_inv[i] = 1.0 / self._mu_s[i]
164168
nonzero += 1
169+
self._mu_s_inv.shape = (-1,)
165170

166171
# We moved this variable to the micro_driver class
167172
self.n_nonzero = nonzero
@@ -177,6 +182,19 @@ def mu_s_profile(r):
177182
# for the LLG STT in the drivers
178183
self.driver.mu_s_const = np.max(self._mu_s)
179184

185+
# David Tue 19 Jun 2018
186+
# Since the scaling variables in the HexagonalDemag class depend on
187+
# mu_s, the safest way to avoid breaking the code is to update the
188+
# variables here in case mu_s changes. Same for normal demag
189+
if self.mesh.mesh_type == 'hexagonal':
190+
for inter in self.interactions:
191+
if isinstance(inter, fidimag.atomistic.DemagHexagonal):
192+
inter.mu_s_scale = self._mu_s * self.scale
193+
inter.scalar2cuboid(inter.mu_s_scale, inter.mu_s_scale_c)
194+
195+
if isinstance(inter, fidimag.atomistic.Demag):
196+
inter.mu_s_scale = self._mu_s * self.scale
197+
180198
mu_s = property(get_mu_s, set_mu_s)
181199

182200
def skyrmion_number(self, method='FiniteSpinChirality'):

fidimag/atomistic/zeeman.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, B0, name='Zeeman'):
5858
self.name = name
5959
self.jac = False
6060

61-
def setup(self, mesh, spin, mu_s):
61+
def setup(self, mesh, spin, mu_s, mu_s_inv):
6262
self.mesh = mesh
6363
self.spin = spin
6464
self.n = mesh.n

0 commit comments

Comments
 (0)