Skip to content

Commit 5ba1b49

Browse files
author
davidcorteso
committed
AtomExch: Updated doc and added more complex tests
1 parent fc28d7a commit 5ba1b49

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

fidimag/atomistic/lib/exch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void compute_exch_field_spatial(double *spin, double *field, double *energy,
172172

173173
/* Calculation of Exchange field for up to 8 shells of neighbours
174174
*
175-
* J :: Array with 8 elements: an exchange constant per shell
175+
* J :: Array with 9 elements: an exchange constant per shell
176176
* Calculation is only up to n_shells (the rest of the elements
177177
* are not used.
178178
* ngbs :: array with the neighbours

tests/test_full_exch.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_full_exch_hex_3_shells_lattice_pos():
5656
(4 + 0 + 3 + 0 + 1 + 0))
5757

5858

59-
def test_full_exch_hex_8_shells():
59+
def test_full_exch_hex_9_shells():
6060
"""
61-
Test the x component of the exchange field when using 2 shells of
61+
Test the x component of the exchange field when using 9 shells of
6262
neighbours in a 9 X 9 hexagonal lattice with square and diagonal
6363
arrangement
6464
@@ -74,7 +74,7 @@ def test_full_exch_hex_8_shells():
7474

7575
for arrang in ['square', 'diagonal']:
7676
a = 1
77-
shells = 2
77+
shells = 9
7878
mesh = HexagonalMesh(a * 0.5, nx=9, ny=9,
7979
shells=shells, alignment=arrang)
8080
sim = Sim(mesh)
@@ -93,27 +93,77 @@ def test_full_exch_hex_8_shells():
9393
assert field[3 * i] == np.sum(remove_negatives(sim.mesh.neighbours[i]))
9494

9595

96+
def test_full_exch_hex_9_shells_J_rings():
97+
"""
98+
Test the x component of the exchange field when using 9 shells of
99+
neighbours in a 11 X 11 hexagonal lattice with square and diagonal
100+
arrangement
101+
102+
We set J=1,2,3,.. for every shell and set the s_x component of the spins as
103+
the lattice site number:
104+
[0, 1, 2, 3, ... 120]
105+
106+
Since we set the s_x components as the lattice position indexes, the x
107+
component of the field is the sum of the indexes of the neighbours
108+
(assuming the neighbours indexing is correct) multiplied by J[i] where i is
109+
the shell (1, 2, ...9), i.e. the 1st shell of ngbs is multiplied by 1,
110+
the 2nd shell by 2, the 3rd shell by 3, and so on
111+
"""
112+
113+
for arrang in ['square', 'diagonal']:
114+
a = 1
115+
shells = 9
116+
mesh = HexagonalMesh(a * 0.5, nx=11, ny=11,
117+
shells=shells, alignment=arrang)
118+
sim = Sim(mesh)
119+
# Set s_x as the lattice site number
120+
sim.spin.reshape(-1, 3)[:, 0] = np.arange(len(sim.spin.reshape(-1, 3)[:, 0]))
121+
122+
# Exchange constants according to the shell
123+
Js = np.arange(1, 10)
124+
exch = Exchange(Js)
125+
sim.add(exch)
126+
127+
field = exch.compute_field()
128+
129+
# We only test for the 60th lattice site
130+
ngbs_60 = mesh.neighbours[60]
131+
sum_ngbs = mesh._sum_ngbs_shell
132+
133+
# For every shell, find the ngb indexes in that shell and multiply the
134+
# sum by the corresponding J=1, 2, 3, ...
135+
sum_rings = 0
136+
for i in range(1, shells + 1):
137+
ngbs_range = slice(sum_ngbs[i - 1], sum_ngbs[i])
138+
print('J = ', Js[i - 1], ' ngbs indexes: ', ngbs_60[ngbs_range])
139+
sum_rings += Js[i - 1] * np.sum(ngbs_60[ngbs_range])
140+
141+
assert field[3 * 60] == sum_rings
142+
143+
96144
def test_full_exch_hex_2_shells():
97145
"""
98146
Test the x component of the exchange field when using 2 shells of
99147
neighbours, comparing the field manually.
100-
This is similar than the *test_full_exch_hex_8_shells* function but
148+
This is similar than the *test_full_exch_hex_9_shells* function but
101149
here we do not assume that the neighbours indexes are correct
150+
We set J=1 for NN and J=2 for NNN
102151
"""
103152
a = 1
104153
shells = 2
105154
mesh = HexagonalMesh(a * 0.5, nx=9, ny=9,
106155
shells=shells, alignment='square')
107156
sim = Sim(mesh)
108157
sim.spin.reshape(-1, 3)[:, 0] = np.arange(len(sim.spin.reshape(-1, 3)[:, 0]))
109-
Js = np.ones(shells)
158+
Js = np.array([1., 2.])
110159
exch = Exchange(Js)
111160
sim.add(exch)
112161
field = exch.compute_field()
113-
assert field[3 * 0] == 1 + 10 + 9 + 11 + 18
114-
assert field[3 * 11] == (12 + 10 + 20 + 1 + 19 + 2) + (21 + 29 + 18 + 3)
162+
assert field[3 * 0] == 1. * (1 + 10 + 9) + 2. * (11 + 18)
163+
assert field[3 * 11] == (12 + 10 + 20 + 1 + 19 + 2) + 2. * (21 + 29 + 18 + 3)
115164

116165

117166
if __name__ == '__main__':
118167
test_full_exch_hex_2_shells()
119-
test_full_exch_hex_8_shells()
168+
test_full_exch_hex_9_shells()
169+
test_full_exch_hex_9_shells_J_rings()

0 commit comments

Comments
 (0)