Skip to content

Commit a0afe61

Browse files
Merge pull request #126 from computationalmodelling/dmi-nn-working
Dmi nn working
2 parents d1fddd6 + bfe74fd commit a0afe61

31 files changed

+3329
-444
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
*.pdf
77
*.tmp
88
*.so
9-
/local/
10-
9+
local/
10+
*~
11+
*.bak
12+
.DS_Store
13+
.pytest_cache/
1114
# ignore automatically generated cython files
1215
fidimag/atomistic/lib/clib.c
1316
fidimag/common/lib/common_clib.c

examples/micromagnetic/box_real_time/box_sim_fidimag.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ def run_from_ipython():
161161
if args.preview:
162162
if run_from_ipython():
163163
matplotlib.use('nbagg')
164-
print 'Using Backend: NBAgg'
164+
print('Using Backend: NBAgg')
165165
else:
166166
matplotlib.use('TkAgg')
167-
print 'Using Backend: TkAgg'
167+
print('Using Backend: TkAgg')
168168

169169
import matplotlib.pyplot as plt
170170
from mpl_toolkits.axes_grid1 import make_axes_locatable
@@ -266,7 +266,7 @@ def irregular_state(pos):
266266
unit_length=1e-9,
267267
)
268268
else:
269-
print 'Using Periodic Boundary Conditions!'
269+
print('Using Periodic Boundary Conditions!')
270270
mesh = CuboidMesh(nx=nx,
271271
ny=ny,
272272
nz=nz,
@@ -307,7 +307,7 @@ def irregular_state(pos):
307307
sim.add(UniaxialAnisotropy(args.k_u, axis=(0, 0, 1)))
308308

309309
if args.Demag:
310-
print 'Using Demag!'
310+
print('Using Demag!')
311311
sim.add(Demag())
312312

313313
# -------------------------------------------------------------------------
@@ -344,20 +344,20 @@ def irregular_state(pos):
344344

345345

346346
# Debug Information -------------------------------------------------------
347-
print 'Simulating a {} x {} x {} box'.format(args.box_length,
347+
print(('Simulating a {} x {} x {} box'.format(args.box_length,
348348
args.box_width,
349-
args.box_thickness)
350-
print 'Number of elements in x-y-z directions: ', nx, ny, nz
351-
print 'Finite differences size in x-y-z directions: ', dx, dy, dz
349+
args.box_thickness)))
350+
print(('Number of elements in x-y-z directions: ', nx, ny, nz))
351+
print(('Finite differences size in x-y-z directions: ', dx, dy, dz))
352352

353-
print 'Saturation Magnetisation: {} A / m'.format(args.Ms)
354-
print 'Exchange constant: {} J m**-1'.format(args.A)
355-
print 'DMI constant: {} * 1e-3 J m**-2'.format(args.D)
353+
print(('Saturation Magnetisation: {} A / m'.format(args.Ms)))
354+
print(('Exchange constant: {} J m**-1'.format(args.A)))
355+
print(('DMI constant: {} * 1e-3 J m**-2'.format(args.D)))
356356
if args.k_u:
357-
print 'Anisotropy constant: {} J m**-3'.format(args.k_u)
357+
print(('Anisotropy constant: {} J m**-3'.format(args.k_u)))
358358
if args.B:
359-
print 'Zeeman field: (0, 0, {}) A / m'.format(args.B / mu0)
360-
print '--------------------------------------'
359+
print(('Zeeman field: (0, 0, {}) A / m'.format(args.B / mu0)))
360+
print('--------------------------------------')
361361

362362
# -------------------------------------------------------------------------
363363

@@ -439,9 +439,9 @@ def irregular_state(pos):
439439
# Now run the simulation printing the energy
440440
for time in times:
441441
if not run_from_ipython():
442-
print 'Time: ', time, ' s'
443-
print 'Total energy: ', sim.compute_energy(), ' J'
444-
print '\n'
442+
print('Time: ', time, ' s')
443+
print('Total energy: ', sim.compute_energy(), ' J')
444+
print('\n')
445445
sim.run_until(time)
446446

447447
# Update the vector data for the plot (the spins do not move

examples/micromagnetic/domain_wall_simple_demag/dw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def excite_system_K(mesh, Hx=2000):
8989

9090
ts = np.linspace(0, 5e-9, 501)
9191
for t in ts:
92-
print 'time', t
92+
print('time', t)
9393
sim.run_until(t)
9494

9595
def excite_system_D(mesh, Hx=2000):
@@ -123,7 +123,7 @@ def excite_system_D(mesh, Hx=2000):
123123

124124
ts = np.linspace(0, 5e-9, 101)
125125
for t in ts:
126-
print 'time', t
126+
print('time', t)
127127
sim.run_until(t)
128128
#sim.save_vtk()
129129

examples/micromagnetic/dw_stt/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def excite_system(mesh):
170170
ts = np.linspace(0, 5e-9, 501)
171171

172172
for t in ts:
173-
print 'time', t
173+
print('time', t)
174174
sim.driver.run_until(t)
175175
sim.save_vtk()
176176
sim.save_m()

examples/micromagnetic/skyrmion_stt/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def excite_system(mesh):
6464

6565
ts = np.linspace(0, 0.5e-9, 101)
6666
for t in ts:
67-
print 'time', t
67+
print('time', t)
6868
sim.run_until(t)
6969
sim.save_vtk()
7070
#sim.save_m()

examples/micromagnetic/vortex/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def gaussian_fun(t):
101101
ts = np.linspace(0, 1e-9, 501)
102102

103103
for t in ts:
104-
print 'time', t
105-
print 'length:', sim.spin_length()[0:200]
104+
print('time', t)
105+
print('length:', sim.spin_length()[0:200])
106106
sim.run_until(t)
107107
sim.save_vtk()
108108

fidimag/atomistic/anisotropy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def setup(self, mesh, spin, mu_s, mu_s_inv):
5454
super(Anisotropy, self).setup(mesh, spin, mu_s, mu_s_inv)
5555

5656
self._Ku = helper.init_scalar(self.Ku, self.mesh)
57-
self._axis = helper.init_vector(self.axis, self.mesh, True)
57+
self._axis = helper.init_vector(self.axis, self.mesh, norm=True)
5858

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

fidimag/atomistic/monte_carlo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def create_tablewriter(self):
8181
self.saver.update_entity_order()
8282

8383
def set_m(self, m0=(1, 0, 0), normalise=True):
84-
self.spin[:] = helper.init_vector(m0, self.mesh, normalise)
84+
self.spin[:] = helper.init_vector(m0, self.mesh, norm=normalise)
8585

8686
# TODO: carefully checking and requires to call set_mu first
8787

fidimag/common/driver_base.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ def run_until(self, t):
193193
self.data_saver.save()
194194

195195
def relax(self, dt=10e-12, stopping_dmdt=0.01, max_steps=1000,
196-
save_m_steps=100, save_vtk_steps=100
197-
):
196+
save_m_steps=100, save_vtk_steps=100,
197+
printing=True):
198198
"""
199199
Evolve the system until meeting the `dmdt` < `stopping_dmdt` criteria.
200200
@@ -221,11 +221,12 @@ def relax(self, dt=10e-12, stopping_dmdt=0.01, max_steps=1000,
221221
self.save_m()
222222

223223
dmdt = self.compute_dmdt(_dt)
224-
print("#{:<4} t={:<8.3g} dt={:.3g} max_dmdt={:.3g}".format(
225-
self.step, # incremented in self.run_until (called above)
226-
self.t,
227-
_dt,
228-
dmdt / self._dmdt_factor))
224+
if printing:
225+
print("#{:<4} t={:<8.3g} dt={:.3g} max_dmdt={:.3g}".format(
226+
self.step, # incremented in self.run_until (called above)
227+
self.t,
228+
_dt,
229+
dmdt / self._dmdt_factor))
229230
if dmdt < stopping_dmdt * self._dmdt_factor:
230231
break
231232

fidimag/common/gradient_descent.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from __future__ import division
2+
import numpy as np
3+
import fidimag.extensions.clib as clib
4+
# import fidimag.common.constant as const
5+
6+
from .minimiser_base import MinimiserBase
7+
8+
9+
class GradientDescentMinimiser(MinimiserBase):
10+
"""
11+
A simple minimisation algorithm, where the evolution of the magnetisation
12+
follows the system's torque
13+
14+
CHECK:
15+
16+
h = (m_i + H) / || (m_i + H) ||
17+
m_i+1 = m_i + alpha * (h_i - m_i)
18+
"""
19+
20+
def __init__(self, mesh, spin,
21+
magnetisation, magnetisation_inv, field, pins,
22+
interactions,
23+
name,
24+
data_saver,
25+
use_jac=False,
26+
integrator=None
27+
):
28+
29+
# Inherit from the base minimiser class
30+
super(SimpleMinimiser, self).__init__(mesh, spin,
31+
magnetisation, magnetisation_inv,
32+
field,
33+
pins,
34+
interactions,
35+
name,
36+
data_saver
37+
)
38+
39+
self.t = 1e-4
40+
self._alpha = 0.1
41+
self._alpha_field = self._alpha * np.ones_like(self.spin)
42+
self.spin_last = np.zeros_like(spin)
43+
self._new_spin = np.zeros_like(spin)
44+
45+
@property
46+
def alpha(self):
47+
"""
48+
Returns the array with the spatially dependent Gilbert damping
49+
per mesh/lattice site
50+
"""
51+
return self._alpha
52+
53+
@alpha.setter
54+
def set_alpha(self, value):
55+
"""
56+
"""
57+
self._alpha = value
58+
self._alpha_field = value * np.ones_like(self.spin)
59+
60+
def run_step(self):
61+
62+
self.spin_last[:] = self.spin[:]
63+
self.update_effective_field()
64+
65+
self._new_spin[self._material] = (self.spin + self.field)[self._material]
66+
clib.normalise_spin(self._new_spin, self._pins, self.n)
67+
68+
self._new_spin[self._material] = (self.spin_last +
69+
self._alpha_field * (self._new_spin - self.spin_last))[self._material]
70+
self.spin[:] = self._new_spin[:]
71+
clib.normalise_spin(self.spin, self._pins, self.n)
72+
73+
def minimise(self, stopping_dm=1e-2, max_steps=2000,
74+
save_data_steps=10, save_m_steps=None, save_vtk_steps=None,
75+
log_steps=1000
76+
):
77+
"""
78+
79+
"""
80+
81+
self.step = 0
82+
83+
# Only update site with magnetisation > 0 which are not pinned
84+
self._material = np.logical_and(np.repeat(self._magnetisation, 3) > 0.0,
85+
np.repeat(1 - self._pins, 3).astype(np.bool))
86+
87+
self.spin_last[:] = self.spin[:]
88+
self.compute_effective_field()
89+
while self.step < max_steps:
90+
91+
self.run_step()
92+
93+
max_dm = (self.spin - self.spin_last).reshape(-1, 3) ** 2
94+
max_dm = np.max(np.sqrt(np.sum(max_dm, axis=1)))
95+
96+
if self.step % log_steps == 0:
97+
print("#max_tau={:<8.3g} max_dm={:<10.3g} counter={}".format(
98+
np.max(np.abs(self.tau)),
99+
max_dm, self.step))
100+
101+
if max_dm < stopping_dm and self.step > 0:
102+
print("FINISHED AT: max_tau={:<8.3g} max_dm={:<10.3g} counter={}".format(
103+
np.max(np.abs(self.tau)),
104+
max_dm, self.step))
105+
106+
self.compute_effective_field()
107+
self.data_saver.save()
108+
109+
break
110+
111+
if self.step % save_data_steps == 0:
112+
# update field before saving data
113+
self.compute_effective_field()
114+
self.data_saver.save()
115+
116+
if (save_vtk_steps is not None) and (self.step % save_vtk_steps == 0):
117+
self.save_vtk()
118+
if (save_m_steps is not None) and (self.step % save_m_steps == 0):
119+
self.save_m()
120+
121+
self.step += 1

0 commit comments

Comments
 (0)