Skip to content

Commit d42ce88

Browse files
author
Weiwei Wang
committed
make guiding center more flexible
1 parent b4d4ef1 commit d42ce88

File tree

5 files changed

+494
-502
lines changed

5 files changed

+494
-502
lines changed

examples/micromagnetic/dw_stt_cpp/main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def excite_system(mesh, beta=0.0):
120120
# The simulation will run for 5 ns and save
121121
# 500 snapshots of the system in the process
122122
ts = np.linspace(0, 0.5e-9, 21)
123-
123+
124124
xs=[]
125125
thetas=[]
126126

@@ -154,15 +154,14 @@ def plot():
154154
z_an, theta_an = analytical(ts)
155155

156156
fig = plt.figure(figsize=(5, 2.6))
157-
157+
158158
gs = gridspec.GridSpec(1, 2, width_ratios=[4, 4])
159159
ax0 = plt.subplot(gs[0])
160160
ax0.plot(ts*1e9,xs,'.')
161161
ax0.plot(ts*1e9, z_an, '-')
162162
plt.ylabel(r'DW shift')
163163
plt.xlabel(r'Time (ns)')
164164

165-
166165
ax1 = plt.subplot(gs[1])
167166
ax1.plot(ts*1e9,theta,'.')
168167
ax1.plot(ts*1e9, theta_an, '-')
@@ -173,7 +172,6 @@ def plot():
173172
analytical(ts)
174173

175174
plt.savefig('xs_theta.pdf')
176-
177175

178176
if __name__ == '__main__':
179177

@@ -189,4 +187,3 @@ def plot():
189187

190188
excite_system(mesh, beta=0.16)
191189
plot()
192-

fidimag/atomistic/lib/clib.h

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,101 @@
11
#ifndef __CLIB__
22
#define __CLIB__
33

4-
#include<math.h>
5-
#include<complex.h>
6-
#include<fftw3.h>
4+
#include <complex.h>
5+
#include <fftw3.h>
6+
#include <math.h>
77
//#include<omp.h>
88

9-
#include"fidimag_random.h"
9+
#include "fidimag_random.h"
1010

1111
#define WIDE_PI 3.1415926535897932384626433832795L
1212

1313
/* 3 components for the cross product calculations */
14-
inline double cross_x(double a0, double a1, double a2,
15-
double b0, double b1, double b2) { return a1 * b2 - a2 * b1; }
16-
inline double cross_y(double a0, double a1, double a2,
17-
double b0, double b1, double b2) { return a2 * b0 - a0 * b2; }
18-
inline double cross_z(double a0, double a1, double a2,
19-
double b0, double b1, double b2) { return a0 * b1 - a1 * b0; }
20-
21-
void compute_exch_field(double *spin, double *field, double *energy,
22-
double Jx, double Jy, double Jz,
23-
int *ngbs, int n);
24-
25-
26-
void compute_exch_field_spatial(double *spin, double *field, double *energy,double *J,
27-
int *ngbs, int n);
14+
inline double cross_x(double a0, double a1, double a2, double b0, double b1,
15+
double b2) {
16+
return a1 * b2 - a2 * b1;
17+
}
18+
inline double cross_y(double a0, double a1, double a2, double b0, double b1,
19+
double b2) {
20+
return a2 * b0 - a0 * b2;
21+
}
22+
inline double cross_z(double a0, double a1, double a2, double b0, double b1,
23+
double b2) {
24+
return a0 * b1 - a1 * b0;
25+
}
26+
27+
void compute_exch_field(double *spin, double *field, double *energy, double Jx,
28+
double Jy, double Jz, int *ngbs, int n);
29+
30+
void compute_exch_field_spatial(double *spin, double *field, double *energy,
31+
double *J, int *ngbs, int n);
2832

2933
double compute_exch_energy(double *spin, double Jx, double Jy, double Jz,
30-
int nx, int ny, int nz,
31-
int xperiodic, int yperiodic);
34+
int nx, int ny, int nz, int xperiodic,
35+
int yperiodic);
3236

33-
void compute_anis(double *spin, double *field, double *energy,
34-
double *Ku, double *axis, int n);
37+
void compute_anis(double *spin, double *field, double *energy, double *Ku,
38+
double *axis, int n);
3539

36-
void dmi_field_bulk(double *spin, double *field, double *energy,
37-
double *D, int *ngbs, int n);
40+
void dmi_field_bulk(double *spin, double *field, double *energy, double *D,
41+
int *ngbs, int n);
3842

3943
void dmi_field_interfacial_atomistic(double *spin, double *field,
40-
double *energy, double D, int *ngbs,
41-
int n, int nneighbours,
42-
double *DMI_vec);
44+
double *energy, double D, int *ngbs, int n,
45+
int nneighbours, double *DMI_vec);
4346

44-
void demag_full(double *spin, double *field, double *energy,
45-
double *coords,
47+
void demag_full(double *spin, double *field, double *energy, double *coords,
4648
double *mu_s, double *mu_s_scale, int n);
4749

48-
double dmi_energy(double *spin, double D, int nx, int ny, int nz,
49-
int xperiodic, int yperiodic);
50+
double dmi_energy(double *spin, double D, int nx, int ny, int nz, int xperiodic,
51+
int yperiodic);
5052

51-
void llg_rhs(double * dm_dt, double * spin, double * h, double *alpha,
52-
int *pins, double gamma, int n, int do_precession,
53-
double default_c);
53+
void llg_rhs(double *dm_dt, double *spin, double *h, double *alpha, int *pins,
54+
double gamma, int n, int do_precession, double default_c);
5455

55-
void llg_rhs_jtimes(double *jtn, double *m, double *h,
56-
double *mp, double *hp, double *alpha, int *pins,
57-
double gamma, int n, int do_precession, double default_c);
56+
void llg_rhs_jtimes(double *jtn, double *m, double *h, double *mp, double *hp,
57+
double *alpha, int *pins, double gamma, int n,
58+
int do_precession, double default_c);
5859

59-
void llg_s_rhs(double * dm_dt, double * spin, double * h, double *alpha,
60-
double *chi, double gamma, int n);
60+
void llg_s_rhs(double *dm_dt, double *spin, double *h, double *alpha,
61+
double *chi, double gamma, int n);
6162

63+
void compute_stt_field_c(double *spin, double *field, double *jx, double *jy,
64+
double *jz, double dx, double dy, double dz, int *ngbs,
65+
int n);
6266

63-
void compute_stt_field_c(double *spin, double *field, double *jx, double *jy, double *jz,
64-
double dx, double dy, double dz, int *ngbs, int n);
65-
66-
void llg_stt_rhs(double *dm_dt, double *m, double *h,
67-
double *h_stt, double *alpha,
68-
double beta, double u0, double gamma, int n);
69-
70-
void llg_stt_cpp(double *dm_dt, double *m, double *h, double *p,
71-
double *alpha, int *pins, double *a_J, double beta, double gamma, int n);
67+
void llg_stt_rhs(double *dm_dt, double *m, double *h, double *h_stt,
68+
double *alpha, double beta, double u0, double gamma, int n);
7269

70+
void llg_stt_cpp(double *dm_dt, double *m, double *h, double *p, double *alpha,
71+
int *pins, double *a_J, double beta, double gamma, int n);
7372

7473
void normalise(double *m, int *pins, int n);
7574

76-
double skyrmion_number(double *spin, double *charge,
77-
int nx, int ny, int nz, int *ngbs);
75+
double skyrmion_number(double *spin, double *charge, int nx, int ny, int nz,
76+
int *ngbs);
7877

79-
double skyrmion_number_BergLuscher(double *spin, double *charge,
80-
int nx, int ny, int nz, int *ngbs);
78+
double skyrmion_number_BergLuscher(double *spin, double *charge, int nx, int ny,
79+
int nz, int *ngbs);
8180

82-
void compute_guiding_center(double *spin, int nx, int ny, int nz, double *res);
81+
void compute_guiding_center(double *spin, int nx, int ny, int nz, int nx_start,
82+
int nx_stop, int ny_start, int ny_stop,
83+
double *res);
8384

84-
void compute_px_py_c(double *spin, int nx, int ny, int nz,
85-
double *px, double *py);
85+
void compute_px_py_c(double *spin, int nx, int ny, int nz, double *px,
86+
double *py);
8687

8788
//======================================================================
8889

89-
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
90-
double *mu_s_inv, int *pins, double *eta, int n, double gamma, double dt);
90+
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
91+
double *mu_s_inv, int *pins, double *eta, int n, double gamma,
92+
double dt);
9193

9294
//======================================================================
9395

94-
void run_step_mc(mt19937_state *state, double *spin, double *new_spin, int *ngbs, int *nngbs, double J, double J1, double D, double D1, double *h, double Kc, int n, double T, int hexagnoal_mesh);
95-
96+
void run_step_mc(mt19937_state *state, double *spin, double *new_spin,
97+
int *ngbs, int *nngbs, double J, double J1, double D,
98+
double D1, double *h, double Kc, int n, double T,
99+
int hexagnoal_mesh);
96100

97101
#endif

fidimag/atomistic/lib/clib.pyx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ np.import_array()
55
cdef extern from "fidimag_random.h":
66
ctypedef struct mt19937_state:
77
pass
8-
8+
99
mt19937_state *create_mt19937_state()
1010
void finalize_mt19937_state(mt19937_state *state)
1111

@@ -19,7 +19,7 @@ cdef extern from "time.h":
1919
time_t time(time_t *timer)
2020

2121
cdef extern from "clib.h":
22-
void run_step_mc(mt19937_state *state, double *spin, double *new_spin, int *ngbs, int *nngbs,
22+
void run_step_mc(mt19937_state *state, double *spin, double *new_spin, int *ngbs, int *nngbs,
2323
double J, double J1, double D, double D1, double *h, double Kc, int n, double T, int hexagnoal_mesh)
2424
double skyrmion_number(double *spin, double *charge,
2525
int nx, int ny, int nz, int *ngbs)
@@ -28,7 +28,8 @@ cdef extern from "clib.h":
2828
double skyrmion_number_BergLuscher(double *spin, double *charge,
2929
int nx, int ny, int nz, int *ngbs)
3030

31-
void compute_guiding_center(double *spin, int nx, int ny, int nz,
31+
void compute_guiding_center(double *spin, int nx, int ny, int nz, int nx_start,
32+
int nx_stop, int ny_start, int ny_stop,
3233
double *res)
3334

3435
void compute_px_py_c(double *spin, int nx, int ny, int nz,
@@ -107,13 +108,17 @@ def compute_skyrmion_number_BergLuscher(np.ndarray[double, ndim=1, mode="c"] spi
107108
return skyrmion_number_BergLuscher(&spin[0], &charge[0], nx, ny, nz, &ngbs[0,0])
108109

109110
def compute_RxRy(np.ndarray[double, ndim=1, mode="c"] spin,
110-
nx, ny, nz):
111+
nx, ny, nz, nx_start=0, nx_stop=-1, ny_start=0, ny_stop=-1):
111112

112113
res = numpy.array([0.0,0.0])
114+
if nx_stop < 0 or nx_stop > nx:
115+
nx_stop = nx
116+
if ny_stop < 0 or ny_stop > ny:
117+
ny_stop = ny
113118

114119
cdef np.ndarray[double, ndim=1, mode="c"] R = res
115120

116-
compute_guiding_center(&spin[0], nx, ny, nz, &R[0])
121+
compute_guiding_center(&spin[0], nx, ny, nz, nx_start, nx_stop, ny_start, ny_stop, &R[0])
117122

118123
return res[0], res[1]
119124

@@ -258,7 +263,7 @@ def compute_llg_stt_cpp(np.ndarray[double, ndim=1, mode="c"] dm_dt,
258263
np.ndarray[int, ndim=1, mode="c"] pin,
259264
np.ndarray[double, ndim=1, mode="c"] a_J,
260265
beta, gamma, n):
261-
llg_stt_cpp(&dm_dt[0], &spin[0], &field[0], &p[0],
266+
llg_stt_cpp(&dm_dt[0], &spin[0], &field[0], &p[0],
262267
&alpha[0], &pin[0], &a_J[0], beta, gamma, n)
263268

264269

@@ -286,11 +291,11 @@ cdef class rng_mt19937(object):
286291
self.seed = int(seed)
287292
else:
288293
self.seed = time(NULL)
289-
294+
290295
self._c_state = create_mt19937_state()
291296
if self._c_state is NULL:
292297
raise MemoryError()
293-
298+
294299
initial_rng_mt19973(self._c_state, self.seed)
295300

296301
def set_seed(self, seed):
@@ -302,7 +307,7 @@ cdef class rng_mt19937(object):
302307
return a random number in [0,1)
303308
"""
304309
return random_double_half_open(self._c_state)
305-
310+
306311
def fill_vector_gaussian(self, np.ndarray[np.float64_t, ndim=1] vector):
307312
gauss_random_vector(self._c_state, &vector[0], vector.shape[0])
308313

@@ -315,25 +320,24 @@ cdef class rng_mt19937(object):
315320
self._c_state = NULL
316321

317322
cdef class monte_carlo(object):
318-
cdef mt19937_state *_c_state
323+
cdef mt19937_state *_c_state
319324

320325
def __init__(self, seed=-1):
321-
326+
322327
self._c_state = create_mt19937_state()
323328
if self._c_state is NULL:
324329
raise MemoryError()
325-
330+
326331
initial_rng_mt19973(self._c_state, seed)
327332

328333
def set_seed(self, seed):
329334
initial_rng_mt19973(self._c_state, seed)
330-
335+
331336
def run_step(self,np.ndarray[double, ndim=1, mode="c"] spin,
332337
np.ndarray[double, ndim=1, mode="c"] new_spin,
333338
np.ndarray[int, ndim=2, mode="c"] ngbs,
334339
np.ndarray[int, ndim=2, mode="c"] nngbs,
335-
J, J1, D, D1, np.ndarray[double, ndim=1, mode="c"] h,
340+
J, J1, D, D1, np.ndarray[double, ndim=1, mode="c"] h,
336341
Kc, n, T, hexagnoal_mesh):
337342

338343
run_step_mc(self._c_state, &spin[0], &new_spin[0], &ngbs[0,0], &nngbs[0,0], J, J1, D, D1, &h[0], Kc, n, T, hexagnoal_mesh)
339-

0 commit comments

Comments
 (0)