Skip to content

Commit 40eb470

Browse files
committed
Refactor SimpleDemag so that it is a subclass of Energy
1 parent a6e8c2a commit 40eb470

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

fidimag/micro/simple_demag.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
2+
from .energy import Energy
23

34

4-
class SimpleDemag(object):
5+
class SimpleDemag(Energy):
56
"""
67
Demagnetising field for thin films in the i-direction.
78
Hj = Hk = 0 and Hi = - Mi.
@@ -12,29 +13,12 @@ def __init__(self, Nx=0, Ny=0.5, Nz=0.5, name='SimpleDemag'):
1213
field_strength is Ms by default
1314
1415
"""
15-
16+
1617
self.Nx = Nx
1718
self.Ny = Ny
1819
self.Nz = Nz
19-
2020
self.name = name
2121

22-
def setup(self, mesh, spin, Ms):
23-
self.mesh = mesh
24-
self.spin = spin
25-
self.n = mesh.n
26-
27-
self.Ms = Ms
28-
self.Ms_long = np.zeros(3 * mesh.n)
29-
30-
self.Ms_long.shape = (3, -1)
31-
for i in range(mesh.n):
32-
self.Ms_long[:, i] = Ms[i]
33-
34-
self.Ms_long.shape = (-1,)
35-
self.field = np.zeros(3 * self.n)
36-
#self.field[:] = helper.init_vector(self.H0, self.mesh)
37-
3822

3923
def compute_field(self, t=0):
4024
self.spin.shape = (3,-1)
@@ -49,9 +33,7 @@ def compute_field(self, t=0):
4933
def compute_energy(self):
5034

5135
mu_0 = 4*np.pi*1e-7
52-
53-
sf = 0.5* self.field * self.spin * self.Ms_long * mu_0
54-
55-
energy = -np.sum(sf)
36+
sf = -0.5 * self.field * self.spin * mu_0
37+
energy = np.sum(sf.reshape(-1, 3), axis=1) * self.Ms
5638

5739
return energy * self.mesh.cellsize

0 commit comments

Comments
 (0)