Skip to content

Commit 3879b2a

Browse files
committed
Add regression tests for energy classes to check they hold a refernece to the correct value of the Ms/mu_s array rather than a copy/etc. This should stop anyone changing this in future as all of these tests will fail.
1 parent a5971d6 commit 3879b2a

File tree

1 file changed

+170
-12
lines changed

1 file changed

+170
-12
lines changed

tests/test_regression_referencing.py

Lines changed: 170 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@
33

44
import fidimag
55

6-
def setup_fixture_micro():
6+
def setup_fixture_micro(driver='llg'):
77
mesh = fidimag.common.CuboidMesh(nx=10, ny=10, nz=10,
88
dx=1, dy=1, dz=1,
99
unit_length=1e-9)
10-
sim = fidimag.micro.Sim(mesh)
10+
sim = fidimag.micro.Sim(mesh, driver=driver)
1111
return mesh, sim
1212

13-
14-
def setup_fixture_atomistic():
15-
mesh = fidimag.common.CuboidMesh(nx=10, ny=10, nz=10,
16-
dx=1, dy=1, dz=1,
17-
unit_length=1e-9)
18-
sim = fidimag.atomistic.Sim(mesh)
19-
return mesh, sim
20-
21-
22-
def test_exchange_Ms_regression_micro():
13+
def test_Ms_regression_Exch_micro():
2314
mesh, sim = setup_fixture_micro()
2415
A1 = 2e-11
2516
Ms1 = 8.5e5
@@ -30,3 +21,170 @@ def test_exchange_Ms_regression_micro():
3021
sim.set_m(Ms2)
3122
assert exch.Ms is sim._magnetisation, "The Ms in the Exchange Micro class is not a reference to Ms in the Sim class"
3223
assert exch.Ms_inv is sim._magnetisation_inv, "The Ms_inv in the Exchange Micro class is not a reference to Ms_inv in the Sim Class"
24+
25+
def test_Ms_regression_Demag_micro():
26+
mesh, sim = setup_fixture_micro()
27+
Ms1 = 8.5e5
28+
Ms2 = 9.0e5
29+
sim.set_m(Ms1)
30+
demag = fidimag.micro.Demag()
31+
sim.add(demag)
32+
sim.set_m(Ms2)
33+
assert demag.Ms is sim._magnetisation, "The Ms in the Demag Micro class is not a reference to Ms in the Sim class"
34+
assert demag.Ms_inv is sim._magnetisation_inv, "The Ms_inv in the Demag Micro class is not a reference to Ms_inv in the Sim Class"
35+
36+
def test_Ms_regression_SimpleDemag_micro():
37+
mesh, sim = setup_fixture_micro()
38+
Ms1 = 8.5e5
39+
Ms2 = 9.0e5
40+
sim.set_m(Ms1)
41+
demag = fidimag.micro.SimpleDemag()
42+
sim.add(demag)
43+
sim.set_m(Ms2)
44+
assert demag.Ms is sim._magnetisation, "The Ms in the SimpleDemag Micro class is not a reference to Ms in the Sim class"
45+
assert demag.Ms_inv is sim._magnetisation_inv, "The Ms_inv in the SimpleDemag Micro class is not a reference to Ms_inv in the Sim Class"
46+
47+
def test_Ms_regression_UniaxialAnisotropy_micro():
48+
mesh, sim = setup_fixture_micro()
49+
Ku = 2e-11
50+
Ms1 = 8.5e5
51+
Ms2 = 9.0e5
52+
sim.set_Ms(Ms1)
53+
anis = fidimag.micro.UniaxialAnisotropy(Ku)
54+
sim.add(anis)
55+
sim.set_Ms(Ms2)
56+
assert anis.Ms is sim._magnetisation, "The Ms in the Exchange Micro class is not a reference to Ms in the Sim class"
57+
assert anis.Ms_inv is sim._magnetisation_inv, "The Ms_inv in the UniaxialAnisotropy Micro class is not a reference to Ms_inv in the Sim Class"
58+
59+
def test_exchange_Ms_regression_UniaxialAnisotropy():
60+
mesh, sim = setup_fixture_micro()
61+
Ku = 2e-11
62+
Ms1 = 8.5e5
63+
Ms2 = 9.0e5
64+
sim.set_Ms(Ms1)
65+
anis = fidimag.micro.UniaxialAnisotropy(Ku)
66+
sim.add(anis)
67+
sim.set_Ms(Ms2)
68+
assert anis.Ms is sim._magnetisation, "The Ms in the Exchange Micro class is not a reference to Ms in the Sim class"
69+
assert anis.Ms_inv is sim._magnetisation_inv, "The Ms_inv in the UniaxialAnisotropy Micro class is not a reference to Ms_inv in the Sim Class"
70+
71+
def test_llg_Ms_regression():
72+
mesh, sim = setup_fixture_micro(driver='llg')
73+
Ku = 2e-11
74+
Ms1 = 8.5e5
75+
Ms2 = 9.0e5
76+
sim.set_Ms(Ms1)
77+
print(sim.driver._Ms)
78+
sim.set_Ms(Ms2)
79+
print(sim.driver._Ms)
80+
assert sim.driver._Ms is sim._magnetisation
81+
82+
def test_llg_stt_Ms_regression():
83+
mesh, sim = setup_fixture_micro(driver='llg_stt')
84+
Ku = 2e-11
85+
Ms1 = 8.5e5
86+
Ms2 = 9.0e5
87+
sim.set_Ms(Ms1)
88+
print(sim.driver._Ms)
89+
sim.set_Ms(Ms2)
90+
print(sim.driver._Ms)
91+
assert sim.driver._Ms is sim._magnetisation
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
def setup_fixture_atomistic(driver='llg'):
106+
mesh = fidimag.common.CuboidMesh(nx=10, ny=10, nz=10,
107+
dx=1, dy=1, dz=1,
108+
unit_length=1e-9)
109+
sim = fidimag.atomistic.Sim(mesh, driver=driver)
110+
return mesh, sim
111+
112+
def test_Ms_regression_Exch_atom():
113+
mesh, sim = setup_fixture_atomistic()
114+
A1 = 2e-11
115+
Ms1 = 8.5e5
116+
Ms2 = 9.0e5
117+
sim.set_mu_s(Ms1)
118+
exch = fidimag.atomistic.UniformExchange(A1)
119+
sim.add(exch)
120+
sim.set_mu_s(Ms2)
121+
assert exch.mu_s is sim._magnetisation, "The Ms in the Exchange Micro class is not a reference to Ms in the Sim class"
122+
assert exch.mu_s_inv is sim._magnetisation_inv, "The Ms_inv in the Exchange Micro class is not a reference to Ms_inv in the Sim Class"
123+
124+
def test_Ms_regression_Demag_atom():
125+
mesh, sim = setup_fixture_atomistic()
126+
Ms1 = 8.5e5
127+
Ms2 = 9.0e5
128+
sim.set_mu_s(Ms1)
129+
demag = fidimag.atomistic.Demag()
130+
sim.add(demag)
131+
sim.set_mu_s(Ms2)
132+
assert demag.mu_s is sim._magnetisation, "The Ms in the Demag Micro class is not a reference to Ms in the Sim class"
133+
assert demag.mu_s_inv is sim._magnetisation_inv, "The Ms_inv in the Demag Micro class is not a reference to Ms_inv in the Sim Class"
134+
135+
def test_Ms_regression_DemagFull_atomistic():
136+
mesh, sim = setup_fixture_atomistic()
137+
Ms1 = 8.5e5
138+
Ms2 = 9.0e5
139+
sim.set_mu_s(Ms1)
140+
demag = fidimag.atomistic.DemagFull()
141+
sim.add(demag)
142+
sim.set_mu_s(Ms2)
143+
assert demag.mu_s is sim._magnetisation, "The Ms in the SimpleDemag Micro class is not a reference to Ms in the Sim class"
144+
assert demag.mu_s_inv is sim._magnetisation_inv, "The Ms_inv in the SimpleDemag Micro class is not a reference to Ms_inv in the Sim Class"
145+
146+
def test_Ms_regression_Anisotropy_atom():
147+
mesh, sim = setup_fixture_atomistic()
148+
Ku = 2e-11
149+
Ms1 = 8.5e5
150+
Ms2 = 9.0e5
151+
sim.set_mu_s(Ms1)
152+
anis = fidimag.atomistic.Anisotropy(Ku)
153+
sim.add(anis)
154+
sim.set_mu_s(Ms2)
155+
assert anis.mu_s is sim._magnetisation, "The Ms in the Exchange atomistic class is not a reference to Ms in the Sim class"
156+
assert anis.mu_s_inv is sim._magnetisation_inv, "The mu_s_inv in the Anisotropy atomistic class is not a reference to mu_s_inv in the Sim Class"
157+
158+
def test_exchange_Ms_regression_CubicAnisotropy_atom():
159+
mesh, sim = setup_fixture_atomistic()
160+
Ku = 2e-11
161+
Ms1 = 8.5e5
162+
Ms2 = 9.0e5
163+
sim.set_mu_s(Ms1)
164+
anis = fidimag.atomistic.CubicAnisotropy(Ku)
165+
sim.add(anis)
166+
sim.set_mu_s(Ms2)
167+
assert anis.mu_s is sim._magnetisation, "The Ms in the Exchange atomistic class is not a reference to Ms in the Sim class"
168+
assert anis.mu_s_inv is sim._magnetisation_inv, "The mu_s_inv in the CubicAnisotropy atomistic class is not a reference to Ms_inv in the Sim Class"
169+
170+
def test_llg_mu_s_regression_atom():
171+
mesh, sim = setup_fixture_atomistic(driver='llg')
172+
Ku = 2e-11
173+
Ms1 = 8.5e5
174+
Ms2 = 9.0e5
175+
sim.set_mu_s(Ms1)
176+
print(sim.driver._mu_s)
177+
sim.set_mu_s(Ms2)
178+
print(sim.driver._mu_s)
179+
assert sim.driver._mu_s is sim._magnetisation
180+
181+
def test_llg_stt_mu_s_regression_atom():
182+
mesh, sim = setup_fixture_atomistic(driver='llg_stt')
183+
Ku = 2e-11
184+
Ms1 = 8.5e5
185+
Ms2 = 9.0e5
186+
sim.set_mu_s(Ms1)
187+
print(sim.driver._mu_s)
188+
sim.set_mu_s(Ms2)
189+
print(sim.driver._mu_s)
190+
assert sim.driver._mu_s is sim._magnetisation

0 commit comments

Comments
 (0)