Skip to content

Commit dd2f329

Browse files
author
davidcorteso
committed
Refactored calculation of fields
The mu_s_inv or Ms_inv (from the _magnetisation_inv variable) was set to an n-array once more (instead of a field-like 3 * n array). This leaves unotuched the Micromagnetic classes. The major change is that we moved the full calculation of the atomistic fields to the corresponding C classes, instead of multiplying the fields by 1/mu_s in the Python classes. This required massive update of the Cython files. Also cleaned up the code to fit 80 cols
1 parent 1abcdcc commit dd2f329

File tree

13 files changed

+224
-124
lines changed

13 files changed

+224
-124
lines changed

fidimag/atomistic/anisotropy.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ def compute_field(self, t=0, spin=None):
6565

6666
clib.compute_anisotropy(m,
6767
self.field,
68+
self.mu_s_inv,
6869
self.energy,
6970
self._Ku,
7071
self._axis,
7172
self.n
7273
)
7374

74-
return self.field * self.mu_s_inv
75+
return self.field
7576

7677

7778
class CubicAnisotropy(Energy):
@@ -84,7 +85,6 @@ def __init__(self, Kc, name='CubicAnisotropy'):
8485
self.name = name
8586
self.jac = True
8687

87-
8888
def setup(self, mesh, spin, mu_s, mu_s_inv):
8989
super(CubicAnisotropy, self).setup(mesh, spin, mu_s, mu_s_inv)
9090
self._Kc = helper.init_scalar(self.Kc, self.mesh)
@@ -96,9 +96,10 @@ def compute_field(self, t=0, spin=None):
9696
m = self.spin
9797

9898
clib.compute_anisotropy_cubic(m,
99-
self.field,
100-
self.energy,
101-
self._Kc,
102-
self.n)
99+
self.field,
100+
self.mu_s_inv,
101+
self.energy,
102+
self._Kc,
103+
self.n)
103104

104-
return self.field * self.mu_s_inv
105+
return self.field

fidimag/atomistic/dmi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def compute_field(self, t=0, spin=None):
120120
if self.dmi_type == 'bulk':
121121
clib.compute_dmi_field(m,
122122
self.field,
123+
self.mu_s_inv,
123124
self.energy,
124125
self._D,
125126
self.neighbours,
@@ -131,6 +132,7 @@ def compute_field(self, t=0, spin=None):
131132

132133
clib.compute_dmi_field_interfacial(m,
133134
self.field,
135+
self.mu_s_inv,
134136
self.energy,
135137
self.D,
136138
self.neighbours,
@@ -140,7 +142,7 @@ def compute_field(self, t=0, spin=None):
140142
self.DMI_vector
141143
)
142144

143-
return self.field * self.mu_s_inv
145+
return self.field
144146

145147
def compute_energy_direct(self):
146148
"""

fidimag/atomistic/exchange.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,23 @@ def compute_field_spatial(self, t=0, spin=None):
128128

129129
clib.compute_exchange_field_spatial(m,
130130
self.field,
131+
self.mu_s_inv,
131132
self.energy,
132133
self._J,
133134
self.neighbours,
134135
self.n,
135136
self.n_ngbs
136137
)
137138

138-
return self.field * self.mu_s_inv
139+
return self.field
139140

140141
def compute_field_uniform(self, t=0, spin=None):
141142

142143
m = spin if spin is not None else self.spin
143144

144145
clib.compute_exchange_field(m,
145146
self.field,
147+
self.mu_s_inv,
146148
self.energy,
147149
self.Jx,
148150
self.Jy,
@@ -152,13 +154,16 @@ def compute_field_uniform(self, t=0, spin=None):
152154
self.n_ngbs
153155
)
154156

155-
return self.field * self.mu_s_inv
157+
return self.field
156158

157159
def compute_field_full(self, t=0, spin=None):
158160

159161
m = spin if spin is not None else self.spin
160162

161-
clib.compute_full_exchange_field(m, self.field, self.energy,
163+
clib.compute_full_exchange_field(m,
164+
self.field,
165+
self.mu_s_inv,
166+
self.energy,
162167
self._J,
163168
self.neighbours,
164169
self.n, self.mesh.n_ngbs,
@@ -167,7 +172,7 @@ def compute_field_full(self, t=0, spin=None):
167172
self.mesh._sum_ngbs_shell
168173
)
169174

170-
return self.field * self.mu_s_inv
175+
return self.field
171176

172177

173178
class UniformExchange(Exchange):

fidimag/atomistic/lib/anis.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "clib.h"
22

33

4-
void compute_anis(double *restrict spin, double *restrict field, double *restrict energy,
5-
double *restrict Ku, double *restrict axis, int n) {
4+
void compute_anis(double *restrict spin, double *restrict field,
5+
double *restrict mu_s_inv,
6+
double *restrict energy,
7+
double *restrict Ku, double *restrict axis, int n) {
68

79
/* Remember that the magnetisation order is
810
* mx1, my1, mz1, mx2, my2, mz2, mx3,...
@@ -28,28 +30,43 @@ void compute_anis(double *restrict spin, double *restrict field, double *restric
2830

2931
energy[i] = -Ku[i] * (m_u * m_u);
3032

33+
// Scale field by 1/mu_s
34+
field[3 * i] *= mu_s_inv[i];
35+
field[3 * i + 1] *= mu_s_inv[i];
36+
field[3 * i + 2] *= mu_s_inv[i];
37+
3138
}
3239

3340
}
3441

3542

36-
void compute_anis_cubic(double *restrict spin, double *restrict field, double *restrict energy,
37-
double *restrict Kc, int n) {
43+
void compute_anis_cubic(double *restrict spin, double *restrict field,
44+
double *restrict mu_s_inv,
45+
double *restrict energy,
46+
double *restrict Kc, int n) {
3847

3948
/* Remember that the magnetisation order is
4049
* mx1, my1, mz1, mx2, my2, mz2, mx3,...
4150
* so we get the corresponding components multiplying
4251
* by 3 in every iteration.
4352
*
4453
*/
45-
#pragma omp parallel for
46-
for (int i = 0; i < n; i++) {
47-
int j = 3*i;
48-
field[j] = - 4*Kc[i]*spin[j]*spin[j]*spin[j];
49-
field[j+1] = - 4*Kc[i]*spin[j+1]*spin[j+1]*spin[j+1];
50-
field[j+2] = - 4*Kc[i]*spin[j+2]*spin[j+2]*spin[j+2];
51-
52-
energy[i] = -0.25*(field[j]*spin[j]+field[j+1]*spin[j+1]+field[j+2]*spin[j+2]) ;
54+
#pragma omp parallel for
55+
for (int i = 0; i < n; i++) {
56+
int j = 3 * i;
57+
field[j] = - 4 * Kc[i] * spin[j] * spin[j] * spin[j];
58+
field[j+1] = - 4 * Kc[i] * spin[j+1] * spin[j+1] * spin[j+1];
59+
field[j+2] = - 4 * Kc[i] * spin[j+2] * spin[j+2] * spin[j+2];
60+
61+
energy[i] = -0.25 * (field[j] * spin[j] +
62+
field[j+1] * spin[j+1] +
63+
field[j+2] * spin[j+2]
64+
);
65+
66+
// Scale field by 1/mu_s
67+
field[3 * i] *= mu_s_inv[i];
68+
field[3 * i + 1] *= mu_s_inv[i];
69+
field[3 * i + 2] *= mu_s_inv[i];
5370

5471
}
5572

fidimag/atomistic/lib/clib.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,47 @@ inline double cross_z(double a0, double a1, double a2, double b0, double b1,
2929
// ----------------------------------------------------------------------------
3030
// From exch.c
3131

32-
void compute_exch_field(double *restrict spin, double *restrict field, double *restrict energy, double Jx,
32+
void compute_exch_field(double *restrict spin, double *restrict field, double *restrict mu_s_inv,
33+
double *restrict energy, double Jx,
3334
double Jy, double Jz, int *restrict ngbs, int n, int n_ngbs);
3435

35-
void compute_exch_field_spatial(double *restrict spin, double *restrict field, double *restrict energy,
36+
void compute_exch_field_spatial(double *restrict spin, double *restrict field, double *restrict mu_s_inv,
37+
double *restrict energy,
3638
double *restrict J, int *restrict ngbs, int n, int n_ngbs);
3739

3840
double compute_exch_energy(double *restrict spin, double Jx, double Jy, double Jz,
3941
int nx, int ny, int nz, int xperiodic,
4042
int yperiodic);
4143

42-
void compute_full_exch_field(double *restrict spin, double *restrict field, double *restrict energy,
44+
void compute_full_exch_field(double *restrict spin, double *restrict field, double *restrict mu_s_inv,
45+
double *restrict energy,
4346
double *restrict J, int *restrict ngbs, int n, int n_ngbs,
4447
int n_shells, int *restrict n_ngbs_shell, int *restrict sum_ngbs_shell
4548
);
4649

4750
// -----------------------------------------------------------------------------
4851
// From anis.c
4952

50-
void compute_anis(double *restrict spin, double *restrict field, double *restrict energy, double *restrict Ku,
53+
void compute_anis(double *restrict spin, double *restrict field,
54+
double *restrict mu_s_inv,
55+
double *restrict energy, double *restrict Ku,
5156
double *restrict axis, int n);
52-
void compute_anis_cubic(double *restrict spin, double *restrict field, double *restrict energy,
53-
double *Kc, int n);
57+
58+
void compute_anis_cubic(double *restrict spin, double *restrict field,
59+
double *restrict mu_s_inv,
60+
double *restrict energy,
61+
double *Kc, int n);
5462

5563
// ----------------------------------------------------------------------------
5664
// From dmi.c
5765

58-
void dmi_field_bulk(double *restrict spin, double *restrict field, double *restrict energy, double *D,
66+
void dmi_field_bulk(double *restrict spin, double *restrict field,
67+
double *restrict mu_s_inv,
68+
double *restrict energy, double *D,
5969
int *restrict ngbs, int n, int n_ngbs);
6070

6171
void dmi_field_interfacial_atomistic(double *spin, double *field,
72+
double *mu_s_inv,
6273
double *energy, double D, int *ngbs, int n,
6374
int n_ngbs, int n_ngbs_dmi, double *DMI_vec);
6475

0 commit comments

Comments
 (0)