Skip to content

Commit a655f7b

Browse files
author
Weiwei Wang
committed
improve mc
1 parent 48e9eb1 commit a655f7b

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

examples/mc/skx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def run(mesh):
1717

1818
mc = MonteCarlo(mesh, name='test1')
1919
mc.set_m(init_m)
20-
mc.set_options(H=[0,0,2e-2], J=50.0, D=0.18*50, T=10)
21-
mc.run(steps=100)
20+
mc.set_options(H=[0,0,2e-2], J=50.0, D=0.18*50, T=20.0)
21+
mc.run(steps=200)
2222

2323

2424
if __name__=='__main__':

fidimag/atomistic/lib/clib.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#define WIDE_PI 3.1415926535897932384626433832795L
1010

11+
double single_random(void);
12+
void gauss_random_vec(double *x, int n);
13+
void random_spin_uniform(double *spin, int n);
14+
1115
/* 3 components for the cross product calculations */
1216
inline double cross_x(double a0, double a1, double a2,
1317
double b0, double b1, double b2) { return a1 * b2 - a2 * b1; }
@@ -84,5 +88,9 @@ void compute_px_py_c(double *spin, int nx, int ny, int nz,
8488
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
8589
double *mu_s_inv, int *pins, double *eta, int n, double gamma, double dt);
8690

91+
//======================================================================
92+
void run_step_mc(double *spin, double *new_spin, int *ngbs, double J, double D, double *h, int n, double T);
93+
94+
8795

8896
#endif

fidimag/atomistic/lib/clib.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cdef extern from "clib.h":
77
double single_random()
88
void gauss_random_vec(double *x, int n)
99
void random_spin_uniform(double *spin, int n)
10+
void run_step_mc(double *spin, double *new_spin, int *ngbs, double J, double D, double *h, int n, double T)
1011

1112
double skyrmion_number(double *spin, double *charge,
1213
int nx, int ny, int nz, int *ngbs)
@@ -267,6 +268,13 @@ def random_number():
267268
cdef double res = single_random()
268269
return res
269270

271+
def run_mc_step(np.ndarray[double, ndim=1, mode="c"] spin,
272+
np.ndarray[double, ndim=1, mode="c"] new_spin,
273+
np.ndarray[int, ndim=2, mode="c"] ngbs,
274+
J, D, np.ndarray[double, ndim=1, mode="c"] h,
275+
n, T):
276+
run_step_mc(&spin[0], &new_spin[0], &ngbs[0,0], J, D, &h[0], n, T)
277+
270278

271279
def compute_llg_rhs_dw(np.ndarray[double, ndim=1, mode="c"] dm,
272280
np.ndarray[double, ndim=1, mode="c"] spin,

fidimag/atomistic/lib/mc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ void uniform_random_sphere(double *phi, double *ct, int n){
77
}
88
}
99

10-
1110
/*
1211
* n is the total spin number
1312
*/
@@ -80,7 +79,6 @@ void run_step_mc(double *spin, double *new_spin, int *ngbs, double J, double D,
8079

8180
random_spin_uniform(&new_spin[0], n);
8281

83-
8482
double ed, r;
8583
int update=0;
8684
for(int i=0;i<n;i++){

fidimag/atomistic/mc.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, mesh, name='unnamed'):
1717
self.n_nonzero = self.n
1818

1919

20-
self.ngs = mesh.neighbours
20+
self.ngbs = mesh.neighbours
2121

2222
self._mu_s = np.zeros(self.n, dtype=np.float)
2323
self.spin = np.ones(3 * self.n, dtype=np.float)
@@ -51,12 +51,19 @@ def set_options(self, J=50.0, D=0, H=None, seed=100, T=10.0):
5151

5252
def create_tablewriter(self):
5353

54-
self.saver = DataSaver(self, self.name + '.txt')
54+
entities = {
55+
'step': {'unit': '<>',
56+
'get': lambda sim: sim.step,
57+
'header': 'step'},
58+
'm': {'unit': '<>',
59+
'get': lambda sim: sim.compute_average(),
60+
'header': ('m_x', 'm_y', 'm_z')},
61+
'skx_num':{'unit': '<>',
62+
'get': lambda sim: sim.skyrmion_number(),
63+
'header': 'skx_num'}
64+
}
5565

56-
self.saver.entities['skx_num'] = {
57-
'unit': '<>',
58-
'get': lambda sim: sim.skyrmion_number(),
59-
'header': 'skx_num'}
66+
self.saver = DataSaver(self, self.name + '.txt', entities=entities)
6067

6168
self.saver.update_entity_order()
6269

@@ -130,7 +137,8 @@ def run(self, steps=1000, save_m_steps=100, save_vtk_steps=100):
130137

131138
for step in range(1, steps + 1):
132139
self.step = step
133-
140+
clib.run_mc_step(self.spin, self.random_spin, self.ngbs,
141+
self.J, self.D, self._H, self.n, self.T)
134142

135143
self.saver.save()
136144

@@ -141,7 +149,7 @@ def run(self, steps=1000, save_m_steps=100, save_vtk_steps=100):
141149
if step % save_m_steps == 0:
142150
self.save_m()
143151

144-
print(step)
152+
print("step=%d, skyrmion number=%0.4g"%(self.step, self.skyrmion_number()))
145153

146154

147155

0 commit comments

Comments
 (0)