Skip to content

Commit 4499c63

Browse files
authored
Merge pull request #113 from computationalmodelling/minimiser_sd
Minimiser sd
2 parents 2c19405 + c3fc080 commit 4499c63

20 files changed

+15122
-157
lines changed

doc/ipynb/steepest_descent.ipynb

Lines changed: 6195 additions & 0 deletions
Large diffs are not rendered by default.

fidimag/atomistic/lib/clib.pyx

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -93,90 +93,91 @@ cdef extern from "clib.h":
9393
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha, double *mu_s_inv, int *pins, double *eta, int n, double gamma, double dt)
9494

9595

96+
9697
# -----------------------------------------------------------------------------
9798
# -----------------------------------------------------------------------------
9899

99100

100-
def compute_skyrmion_number(np.ndarray[double, ndim=1, mode="c"] spin,
101-
np.ndarray[double, ndim=1, mode="c"] charge,
101+
def compute_skyrmion_number(double [:] spin,
102+
double [:] charge,
102103
nx, ny, nz,
103-
np.ndarray[int, ndim=2, mode="c"] ngbs,
104+
int [:, :] ngbs,
104105
n_ngbs
105106
):
106107

107108
return skyrmion_number(&spin[0], &charge[0], nx, ny, nz, &ngbs[0,0], n_ngbs)
108109

109110

110-
def compute_skyrmion_number_BergLuscher(np.ndarray[double, ndim=1, mode="c"] spin,
111-
np.ndarray[double, ndim=1, mode="c"] charge,
112-
nx, ny, nz,
113-
np.ndarray[int, ndim=2, mode="c"] ngbs,
114-
n_ngbs
115-
):
111+
def compute_skyrmion_number_BergLuscher(double [:] spin,
112+
double [:] charge,
113+
nx, ny, nz,
114+
int [:, :] ngbs,
115+
n_ngbs
116+
):
116117

117118
return skyrmion_number_BergLuscher(&spin[0], &charge[0], nx, ny, nz, &ngbs[0,0], n_ngbs)
118119

119-
def compute_RxRy(np.ndarray[double, ndim=1, mode="c"] spin,
120-
nx, ny, nz, nx_start=0, nx_stop=-1, ny_start=0, ny_stop=-1):
120+
def compute_RxRy(double [:] spin,
121+
nx, ny, nz, nx_start=0, nx_stop=-1, ny_start=0, ny_stop=-1):
121122

122123
res = numpy.array([0.0,0.0])
123124
if nx_stop < 0 or nx_stop > nx:
124125
nx_stop = nx
125126
if ny_stop < 0 or ny_stop > ny:
126127
ny_stop = ny
127128

128-
cdef np.ndarray[double, ndim=1, mode="c"] R = res
129+
cdef double [:] R = res
129130

130131
compute_guiding_center(&spin[0], nx, ny, nz, nx_start, nx_stop, ny_start, ny_stop, &R[0])
131132

132133
return res[0], res[1]
133134

134-
def compute_px_py(np.ndarray[double, ndim=1, mode="c"] spin,
135+
def compute_px_py(double [:] spin,
135136
nx,ny,nz,
136-
np.ndarray[double, ndim=1, mode="c"] px,
137-
np.ndarray[double, ndim=1, mode="c"] py):
137+
double[:] px,
138+
double[:] py):
138139

139140
compute_px_py_c(&spin[0], nx, ny, nz, &px[0], &py[0])
140141

141142
# -------------------------------------------------------------------------
142143

143-
def compute_exchange_field(np.ndarray[double, ndim=1, mode="c"] spin,
144-
np.ndarray[double, ndim=1, mode="c"] field,
145-
np.ndarray[double, ndim=1, mode="c"] energy,
146-
Jx, Jy, Jz,
147-
np.ndarray[int, ndim=2, mode="c"] ngbs,
148-
n, n_ngbs
149-
):
144+
def compute_exchange_field(double [:] spin,
145+
double [:] field,
146+
double [:] energy,
147+
Jx, Jy, Jz,
148+
int [:, :] ngbs,
149+
n, n_ngbs
150+
):
150151

151152
compute_exch_field(&spin[0], &field[0], &energy[0], Jx, Jy, Jz,
152153
&ngbs[0, 0], n, n_ngbs)
153154

154-
def compute_exchange_field_spatial(np.ndarray[double, ndim=1, mode="c"] spin,
155-
np.ndarray[double, ndim=1, mode="c"] field,
156-
np.ndarray[double, ndim=1, mode="c"] energy,
157-
np.ndarray[double, ndim=2, mode="c"] J,
158-
np.ndarray[int, ndim=2, mode="c"] ngbs,
159-
n, n_ngbs):
155+
def compute_exchange_field_spatial(double [:] spin,
156+
double [:] field,
157+
double [:] energy,
158+
double [:, :] J,
159+
int [:, :] ngbs,
160+
n, n_ngbs):
160161

161162
compute_exch_field_spatial(&spin[0], &field[0], &energy[0],&J[0,0],&ngbs[0, 0], n, n_ngbs)
162163

163164

164-
def compute_exchange_energy(np.ndarray[double, ndim=1, mode="c"] spin,
165+
def compute_exchange_energy(double [:] spin,
165166
Jx, Jy, Jz, nx, ny, nz, xperiodic,yperiodic):
166167

167168
return compute_exch_energy(&spin[0], Jx, Jy, Jz,
168169
nx, ny, nz,
169170
xperiodic, yperiodic)
170171

171172

172-
def compute_full_exchange_field(np.ndarray[double, ndim=1, mode="c"] spin,
173-
np.ndarray[double, ndim=1, mode="c"] field,
174-
np.ndarray[double, ndim=1, mode="c"] energy,
175-
np.ndarray[double, ndim=1, mode="c"] J,
176-
np.ndarray[int, ndim=2, mode="c"] ngbs,
173+
def compute_full_exchange_field(double [:] spin,
174+
double [:] field,
175+
double [:] energy,
176+
double [:] J,
177+
int [:, :] ngbs,
177178
n, n_ngbs, n_shells,
178-
np.ndarray[int, ndim=1, mode="c"] n_ngbs_shell,
179-
np.ndarray[int, ndim=1, mode="c"] sum_ngbs_shell
179+
int [:] n_ngbs_shell,
180+
int [:] sum_ngbs_shell
180181
):
181182

182183
compute_full_exch_field(&spin[0], &field[0], &energy[0], &J[0],
@@ -195,22 +196,22 @@ def compute_anisotropy_cubic(double [:] spin, double [:] field, double [:] energ
195196

196197
# -----------------------------------------------------------------------------
197198

198-
def compute_dmi_field(np.ndarray[double, ndim=1, mode="c"] spin,
199-
np.ndarray[double, ndim=1, mode="c"] field,
200-
np.ndarray[double, ndim=1, mode="c"] energy,
201-
np.ndarray[double, ndim=2, mode="c"] D,
202-
np.ndarray[int, ndim=2, mode="c"] ngbs,
199+
def compute_dmi_field(double [:] spin,
200+
double [:] field,
201+
double [:] energy,
202+
double [:, :] D,
203+
int [:, :] ngbs,
203204
n, n_ngbs):
204205
dmi_field_bulk(&spin[0], &field[0], &energy[0], &D[0,0], &ngbs[0, 0], n, n_ngbs)
205206

206207

207-
def compute_dmi_field_interfacial(np.ndarray[double, ndim=1, mode="c"] spin,
208-
np.ndarray[double, ndim=1, mode="c"] field,
209-
np.ndarray[double, ndim=1, mode="c"] energy,
208+
def compute_dmi_field_interfacial(double [:] spin,
209+
double [:] field,
210+
double [:] energy,
210211
D,
211-
np.ndarray[int, ndim=2, mode="c"] ngbs,
212+
int [:, :] ngbs,
212213
n, n_ngbs, n_ngbs_dmi,
213-
np.ndarray[double, ndim=1, mode="c"] DMI_vec,
214+
double [:] DMI_vec,
214215
):
215216
dmi_field_interfacial_atomistic(&spin[0], &field[0], &energy[0],
216217
D, &ngbs[0, 0], n,
@@ -225,12 +226,12 @@ def compute_dmi_energy(np.ndarray[double, ndim=1, mode="c"] spin,
225226

226227
# -------------------------------------------------------------------------
227228

228-
def compute_demag_full(np.ndarray[double, ndim=1, mode="c"] spin,
229-
np.ndarray[double, ndim=1, mode="c"] field,
230-
np.ndarray[double, ndim=1, mode="c"] energy,
231-
np.ndarray[double, ndim=2, mode="c"] coords,
232-
np.ndarray[double, ndim=1, mode="c"] mu_s,
233-
np.ndarray[double, ndim=1, mode="c"] mu_s_scale,
229+
def compute_demag_full(double [:] spin,
230+
double [:] field,
231+
double [:] energy,
232+
double [:, :] coords,
233+
double [:] mu_s,
234+
double [:] mu_s_scale,
234235
n
235236
):
236237
demag_full(&spin[0], &field[0], &energy[0],
@@ -242,14 +243,14 @@ def normalise_spin(np.ndarray[double, ndim=1, mode="c"] spin,
242243
np.ndarray[int, ndim=1, mode="c"] pins, n):
243244
normalise(&spin[0], &pins[0], n)
244245

245-
def compute_llg_rhs_dw(np.ndarray[double, ndim=1, mode="c"] dm,
246-
np.ndarray[double, ndim=1, mode="c"] spin,
247-
np.ndarray[double, ndim=1, mode="c"] field,
248-
np.ndarray[double, ndim=1, mode="c"] T,
249-
np.ndarray[double, ndim=1, mode="c"] alpha,
250-
np.ndarray[double, ndim=1, mode="c"] mu_s_inv,
251-
np.ndarray[double, ndim=1, mode="c"] eta,
252-
np.ndarray[int, ndim=1, mode="c"] pin, n, gamma, dt):
246+
def compute_llg_rhs_dw(double [:] dm,
247+
double [:] spin,
248+
double [:] field,
249+
double [:] T,
250+
double [:] alpha,
251+
double [:] mu_s_inv,
252+
double [:] eta,
253+
int [:] pin, n, gamma, dt):
253254
llg_rhs_dw_c(&spin[0], &field[0], &dm[0], &T[0], &alpha[0], &mu_s_inv[0], &pin[0], &eta[0], n, gamma, dt)
254255

255256

@@ -307,12 +308,15 @@ cdef class monte_carlo(object):
307308
def set_seed(self, seed):
308309
initial_rng_mt19973(self._c_state, seed)
309310

310-
def run_step(self,np.ndarray[double, ndim=1, mode="c"] spin,
311-
np.ndarray[double, ndim=1, mode="c"] new_spin,
312-
np.ndarray[int, ndim=2, mode="c"] ngbs,
313-
np.ndarray[int, ndim=2, mode="c"] nngbs,
311+
def run_step(self,
312+
double [:] spin,
313+
double [:] new_spin,
314+
int [:, :] ngbs,
315+
int [:, :] nngbs,
314316
n_ngbs,
315317
J, J1, D, D1, np.ndarray[double, ndim=1, mode="c"] h,
316318
Kc, n, T, hexagonal_mesh):
317319

318-
run_step_mc(self._c_state, &spin[0], &new_spin[0], &ngbs[0,0], &nngbs[0,0], n_ngbs, J, J1, D, D1, &h[0], Kc, n, T, hexagonal_mesh)
320+
run_step_mc(self._c_state, &spin[0], &new_spin[0],
321+
&ngbs[0,0], &nngbs[0,0], n_ngbs,
322+
J, J1, D, D1, &h[0], Kc, n, T, hexagonal_mesh)

fidimag/atomistic/sim.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from .sllg import SLLG
33
from .llg_stt import LLG_STT
44
from .llg_stt_cpp import LLG_STT_CPP
5+
from fidimag.common.steepest_descent import SteepestDescent
6+
# from .minimiser import Minimiser
57

68
import fidimag.common.skyrmion_number
79
import fidimag.common.helper as helper
@@ -13,7 +15,9 @@
1315
KNOWN_DRIVERS = {'llg': LLG,
1416
'sllg': SLLG,
1517
'llg_stt': LLG_STT,
16-
'llg_stt_cpp': LLG_STT_CPP
18+
'llg_stt_cpp': LLG_STT_CPP,
19+
'steepest_descent': SteepestDescent,
20+
# 'minimiser': Minimiser
1721
}
1822

1923

fidimag/common/driver_base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,6 @@ def save_m(self, ZIP=False):
261261
except OSError:
262262
pass
263263

264-
265-
266264
def save_skx(self):
267265
"""
268266
Save the skyrmion number density (sk number per mesh site)

fidimag/common/lib/common_clib.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ inline double cross_z(double a0, double a1, double a2, double b0, double b1,
2121
return a0 * b1 - a1 * b0;
2222
}
2323

24+
inline void normalise(double *m, int *pins, int n){
25+
int i, j, k;
26+
double mm;
27+
for (int id = 0; id < n; id++) {
28+
i = 3*id;
29+
j = i + 1;
30+
k = j + 1;
31+
32+
if (pins[id]>0) continue;
33+
34+
mm = sqrt(m[i] * m[i] + m[j] * m[j] + m[k] * m[k]);
35+
if(mm > 0) {
36+
mm = 1 / mm;
37+
m[i] *= mm;
38+
m[j] *= mm;
39+
m[k] *= mm;
40+
}
41+
}
42+
}
43+
2444
// ----------------------------------------------------------------------------
2545
// From: llg.c
2646

@@ -44,5 +64,16 @@ void llg_stt_rhs(double *restrict dm_dt, double *restrict m, double *restrict h,
4464
void llg_stt_cpp(double *restrict dm_dt, double *restrict m, double *restrict h, double *restrict p, double *restrict alpha,
4565
int *restrict pins, double *restrict a_J, double beta, double gamma, int n);
4666

67+
// ----------------------------------------------------------------------------
68+
// From steepest_descent.c
69+
70+
void sd_update_spin (double *spin, double *spin_last, double *magnetisation,
71+
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
72+
int* pins, int n);
73+
74+
void sd_compute_step (double *spin, double *spin_last, double *magnetisation, double *field,
75+
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
76+
int *pins, int n, int counter, double tmin, double tmax);
77+
4778

4879
#endif

0 commit comments

Comments
 (0)