Skip to content

Commit 35e446c

Browse files
committed
Write tests for restructured skyrmion number function as per point 2 in the pull request. TDD time!
1 parent 140ce71 commit 35e446c

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

fidimag/common/skyrmion_number_test.py

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from fidimag.common.skyrmion_number import skyrmion_number_centre_slice
1+
from fidimag.common.skyrmion_number import skyrmion_number_slice
22
from fidimag.common.skyrmion_number import skyrmion_number_lee
33
import copy
44
import fidimag
55
import numpy as np
6+
import pytest
67

78

89
def skyrmion_centre_z(mesh):
@@ -177,15 +178,25 @@ def test_skyrmion_number_monolayer():
177178
clibSk = testCase.microSim.skyrmion_number()
178179
print("CLib calculates the skyrmion number as: {:1.2f}.".format(clibSk))
179180

180-
centreSk = testCase.microSim.skyrmion_number_centre_slice()
181+
bottomSk = testCase.microSim.skyrmion_number_slice(at="bottom")
182+
print("I calculate the skyrmion number at the bottom as: {:1.2f}."
183+
.format(bottomSk))
184+
185+
centreSk = testCase.microSim.skyrmion_number_slice(at="centre")
181186
print("I calculate the skyrmion number at the centre as: {:1.2f}."
182187
.format(centreSk))
183188

189+
topSk = testCase.microSim.skyrmion_number_slice(at="top")
190+
print("I calculate the skyrmion number at the top as: {:1.2f}."
191+
.format(topSk))
192+
184193
leeSk = testCase.microSim.skyrmion_number_lee()
185194
print("I calculate the 3D skyrmion number as: {:1.2f}.".format(leeSk))
186195

187196
assert abs(clibSk - 0) < testCase.aTol
197+
assert abs(bottomSk - clibSk) < testCase.aTol
188198
assert abs(centreSk - 1) < testCase.aTol
199+
assert abs(topSk - 0) < testCase.aTol
189200
assert abs(leeSk - 1 / float(testCase.microSim.mesh.nz)) < testCase.aTol
190201

191202
# Atomistic simulation
@@ -195,16 +206,26 @@ def test_skyrmion_number_monolayer():
195206
clibSk = testCase.atomSim.skyrmion_number()
196207
print("CLib calculates the skyrmion number as: {:1.2f}.".format(clibSk))
197208

198-
centreSk = testCase.atomSim.skyrmion_number_centre_slice()
209+
bottomSk = testCase.microSim.skyrmion_number_slice(at="bottom")
210+
print("I calculate the skyrmion number at the bottom as: {:1.2f}."
211+
.format(bottomSk))
212+
213+
centreSk = testCase.microSim.skyrmion_number_slice(at="centre")
199214
print("I calculate the skyrmion number at the centre as: {:1.2f}."
200215
.format(centreSk))
201216

202-
leeSk = testCase.atomSim.skyrmion_number_lee()
217+
topSk = testCase.microSim.skyrmion_number_slice(at="top")
218+
print("I calculate the skyrmion number at the top as: {:1.2f}."
219+
.format(topSk))
220+
221+
leeSk = testCase.microSim.skyrmion_number_lee()
203222
print("I calculate the 3D skyrmion number as: {:1.2f}.".format(leeSk))
204223

205224
assert abs(clibSk - 0) < testCase.aTol
225+
assert abs(bottomSk - clibSk) < testCase.aTol
206226
assert abs(centreSk - 1) < testCase.aTol
207-
assert abs(leeSk - 1 / float(testCase.atomSim.mesh.nz)) < testCase.aTol
227+
assert abs(topSk - 0) < testCase.aTol
228+
assert abs(leeSk - 1 / float(testCase.microSim.mesh.nz)) < testCase.aTol
208229

209230

210231
def test_skyrmion_number_multilayer():
@@ -229,15 +250,25 @@ def test_skyrmion_number_multilayer():
229250
clibSk = testCase.microSim.skyrmion_number()
230251
print("CLib calculates the skyrmion number as: {:1.2f}.".format(clibSk))
231252

232-
centreSk = testCase.microSim.skyrmion_number_centre_slice()
253+
bottomSk = testCase.microSim.skyrmion_number_slice(at="bottom")
254+
print("I calculate the skyrmion number at the bottom as: {:1.2f}."
255+
.format(bottomSk))
256+
257+
centreSk = testCase.microSim.skyrmion_number_slice(at="centre")
233258
print("I calculate the skyrmion number at the centre as: {:1.2f}."
234259
.format(centreSk))
235260

261+
topSk = testCase.microSim.skyrmion_number_slice(at="top")
262+
print("I calculate the skyrmion number at the top as: {:1.2f}."
263+
.format(topSk))
264+
236265
leeSk = testCase.microSim.skyrmion_number_lee()
237266
print("I calculate the 3D skyrmion number as: {:1.2f}.".format(leeSk))
238267

239268
assert abs(clibSk - 1) < testCase.aTol
269+
assert abs(bottomSk - clibSk) < testCase.aTol
240270
assert abs(centreSk - 1) < testCase.aTol
271+
assert abs(topSk - 1) < testCase.aTol
241272
assert abs(leeSk - 1) < testCase.aTol
242273

243274
# Atomistic simulation
@@ -247,19 +278,64 @@ def test_skyrmion_number_multilayer():
247278
clibSk = testCase.atomSim.skyrmion_number()
248279
print("CLib calculates the skyrmion number as: {:1.2f}.".format(clibSk))
249280

250-
centreSk = testCase.atomSim.skyrmion_number_centre_slice()
281+
bottomSk = testCase.microSim.skyrmion_number_slice(at="bottom")
282+
print("I calculate the skyrmion number at the bottom as: {:1.2f}."
283+
.format(bottomSk))
284+
285+
centreSk = testCase.microSim.skyrmion_number_slice(at="centre")
251286
print("I calculate the skyrmion number at the centre as: {:1.2f}."
252287
.format(centreSk))
253288

289+
topSk = testCase.microSim.skyrmion_number_slice(at="top")
290+
print("I calculate the skyrmion number at the top as: {:1.2f}."
291+
.format(topSk))
292+
254293
leeSk = testCase.atomSim.skyrmion_number_lee()
255294
print("I calculate the 3D skyrmion number as: {:1.2f}.".format(leeSk))
256295

257296
assert abs(clibSk - 1) < testCase.aTol
297+
assert abs(bottomSk - clibSk) < testCase.aTol
258298
assert abs(centreSk - 1) < testCase.aTol
299+
assert abs(topSk - 1) < testCase.aTol
259300
assert abs(leeSk - 1) < testCase.aTol
260301

261302

303+
def test_skyrmion_number_slice_errors():
304+
"""
305+
Tests skyrmion number slice function for error conditions as described in
306+
its docstring.
307+
308+
Returns nothing.
309+
"""
310+
311+
testCase = Case()
312+
313+
print("Test that an error is raised when no arguments are passed (due to "
314+
"argument checks in the function).")
315+
with pytest.raises(ValueError):
316+
testCase.microSim.skyrmion_number_slice()
317+
318+
print("Test that an out of bounds Z-index raises an error.")
319+
with pytest.raises(ValueError):
320+
testCase.microSim.skyrmion_number_slice(zIndex=-1)
321+
with pytest.raises(ValueError):
322+
testCase.microSim.skyrmion_number_slice(\
323+
zIndex=testCase.microSim.mesh.nz)
324+
325+
print("Test that a Z-index in bounds does not raise an error.")
326+
testCase.microSim.skyrmion_number_slice(zIndex=0)
327+
testCase.microSim.skyrmion_number_slice(\
328+
zIndex=testCase.microSim.mesh.nz - 1)
329+
330+
print("Test that an invalid 'at' parameter raises an error.")
331+
with pytest.raises(ValueError):
332+
testCase.microSim.skyrmion_number_slice(at="center")
333+
with pytest.raises(ValueError):
334+
testCase.microSim.skyrmion_number_slice(at="middle")
335+
336+
262337
# Run tests if this script was executed.
263338
if __name__ == "__main__":
264339
test_skyrmion_number_monolayer()
265340
test_skyrmion_number_multilayer()
341+
test_skyrmion_number_slice_errors()

0 commit comments

Comments
 (0)