Skip to content

Commit b8ffb8c

Browse files
Merge pull request #135 from computationalmodelling/demag-2d
Demag 2d
2 parents b06edc1 + 6d2cfdf commit b8ffb8c

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

fidimag/atomistic/hexagonal_mesh.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ def init_grid(self):
258258
vertex_counter = 0
259259
vertices = [] # list of tuples of coordinates
260260
hexagons = [] # list of tuples of vertices
261+
self.corners = []
261262
for j in range(self.ny):
262263
for i in range(self.nx):
263264
index = self._index(i, j)
264265
x, y = self.coordinates[index][0], self.coordinates[index][1]
265266
# self.radius is the inradius while self.h/2 is the circumradius
266267
corners = self.hexagon_corners(x, y, self.h * 0.5)
268+
self.corners.append(corners)
267269
hexagon = []
268270
# We'll go through the corners in a counter-clockwise direction.
269271
# For each corner, we think about if it's a "new" vertex, or
@@ -328,6 +330,8 @@ def init_grid(self):
328330
hexagon.append(vertex_counter)
329331
vertex_counter += 1
330332
hexagons.append(hexagon)
333+
334+
self.corners = np.array(self.corners)
331335
return np.array(vertices), np.array(hexagons)
332336

333337
def index(self, i, j):

fidimag/micro/demag.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,25 @@ def setup(self, mesh, spin, Ms, Ms_inv):
5050
asymptotic_radius = options['asymptotic_radius']
5151
if 'dipolar_radius' in options:
5252
dipolar_radius = options['dipolar_radius']
53-
if 'tensor_file_name' in options:
54-
tensor_file_name = options['tensor_file_name']
55-
56-
if tensor_file_name:
57-
if not (os.path.exists(tensor_file_name+'.npz')):
58-
self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
59-
else:
60-
npzfile = np.load(tensor_file_name+'.npz')
61-
geo_info = npzfile['geo']
62-
geo_arr = np.array([self.nx,self.ny, self.nz, self.dx, self.dy, self.dz], dtype=np.float)
63-
#print 'info', geo_info
64-
if not np.allclose(geo_arr, geo_info):
65-
self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
66-
else:
67-
tensors = npzfile['tensors']
68-
geo_arr = np.array([self.nx, self.ny, self.nz, self.dx, self.dy, self.dz], dtype=np.float)
69-
np.savez(tensor_file_name+'.npz', geo=geo_arr, tensors=tensors)
70-
71-
else:
72-
self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
53+
#if 'tensor_file_name' in options:
54+
# tensor_file_name = options['tensor_file_name']
55+
56+
#if tensor_file_name:
57+
#if not (os.path.exists(tensor_file_name+'.npz')):
58+
# self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
59+
#else:
60+
# npzfile = np.load(tensor_file_name+'.npz')
61+
# geo_info = npzfile['geo']
62+
# geo_arr = np.array([self.nx,self.ny, self.nz, self.dx, self.dy, self.dz], dtype=np.float)
63+
# #print 'info', geo_info
64+
# if not np.allclose(geo_arr, geo_info):
65+
# self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
66+
# else:
67+
# tensors = npzfile['tensors']
68+
#geo_arr = np.array([self.nx, self.ny, self.nz, self.dx, self.dy, self.dz], dtype=np.float)
69+
#np.savez(tensor_file_name+'.npz', geo=geo_arr, tensors=tensors)
70+
71+
self.demag.compute_tensors_2dpbc(tensors, pbc_2d_error, sample_repeat_nx, sample_repeat_ny, dipolar_radius)
7372

7473
#print tensors
7574
self.demag.fill_demag_tensors(tensors)

tests/test_demag_libraries.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
we only use cuboid meshes, so we only have the FFT approach
55
for the dipolar field)
66
"""
7-
7+
import fidimag
88
from fidimag.atomistic import Sim
99
from fidimag.common import CuboidMesh
1010
from fidimag.atomistic.hexagonal_mesh import HexagonalMesh
@@ -222,6 +222,35 @@ def test_hexagonal_demags_2D():
222222
assert (DemagFull_energy - demag_2fft_energy) < 1e-10
223223

224224

225+
226+
def test_demag_2d_pbc():
227+
"""
228+
Attempt to check that demag with 2d_pbc option does
229+
not give a nonsensical answer.
230+
"""
231+
A=1.3e-11
232+
Ms=8.6e5
233+
n = 40
234+
d = 2.5
235+
236+
mesh = fidimag.common.CuboidMesh(nx=n, ny=n, nz=1, dx=d, dy=d, dz=d, unit_length=1e-9, periodicity=(True, True, False))
237+
sim = fidimag.micro.Sim(mesh, name="pbc_2d_bug")
238+
sim.set_Ms(Ms)
239+
sim.set_m((0, 0, 1.0), normalise=True)
240+
demag = fidimag.micro.Demag(pbc_2d=True)
241+
242+
sim.add(demag)
243+
sim.compute_effective_field(0)
244+
245+
assert not np.isnan(demag.demag.tensor_xx).any()
246+
assert not np.isnan(demag.demag.tensor_xy).any()
247+
assert not np.isnan(demag.demag.tensor_xz).any()
248+
assert not np.isnan(demag.demag.tensor_yy).any()
249+
assert not np.isnan(demag.demag.tensor_yz).any()
250+
assert not np.isnan(demag.demag.tensor_zz).any()
251+
assert not np.isnan(sim.field).any(), "NaN in demag array"
252+
assert 2 == 1
253+
225254
if __name__ == '__main__':
226255
test_hexagonal_demags_1Dchain()
227256
test_cuboid_demags_1Dchain()

0 commit comments

Comments
 (0)