@@ -2,13 +2,25 @@ import numpy
22cimport numpy as np
33np.import_array()
44
5- cdef extern from " clib.h" :
6- void initial_random(int seed)
7- double single_random()
8- void gauss_random_vec(double * x, int n)
9- 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)
5+ cdef extern from " fidimag_random.h" :
6+ ctypedef struct mt19937_state:
7+ pass
8+
9+ mt19937_state * create_mt19937_state()
10+ void finalize_mt19937_state(mt19937_state * state)
11+
12+ void initial_rng_mt19973(mt19937_state * state, int seed)
13+ double random_double_half_open(mt19937_state * state)
14+ void gauss_random_vector(mt19937_state * state, double * x, int n)
15+ void uniform_random_sphere(mt19937_state * state, double * spin, int n)
1116
17+ cdef extern from " time.h" :
18+ ctypedef int time_t
19+ time_t time(time_t * timer)
20+
21+ cdef extern from " clib.h" :
22+ void run_step_mc(mt19937_state * state, double * spin, double * new_spin, int * ngbs, int * nngbs,
23+ double J, double J1, double D, double D1, double * h, double Kc, int n, double T, int hexagnoal_mesh)
1224 double skyrmion_number(double * spin, double * charge,
1325 int nx, int ny, int nz, int * ngbs)
1426
@@ -264,31 +276,6 @@ def normalise_spin(np.ndarray[double, ndim=1, mode="c"] spin,
264276 normalise(& spin[0 ], & pins[0 ], n)
265277
266278
267- def init_random (seed ):
268- initial_random(seed)
269-
270- def random_number_array (np.ndarray[double , ndim = 1 , mode = " c" ] v):
271-
272- cdef int n = len (v)
273-
274- gauss_random_vec(& v[0 ], n)
275-
276- def random_spin_uniform_sphere (np.ndarray[double , ndim = 1 , mode = " c" ] v, n ):
277-
278- random_spin_uniform(& v[0 ], n)
279-
280- def random_number ():
281- cdef double res = single_random()
282- return res
283-
284- def run_mc_step (np.ndarray[double , ndim = 1 , mode = " c" ] spin,
285- np.ndarray[double , ndim = 1 , mode = " c" ] new_spin,
286- np.ndarray[int , ndim = 2 , mode = " c" ] ngbs,
287- J , D , np.ndarray[double , ndim = 1 , mode = " c" ] h,
288- n , T ):
289- run_step_mc(& spin[0 ], & new_spin[0 ], & ngbs[0 ,0 ], J, D, & h[0 ], n, T)
290-
291-
292279def compute_llg_rhs_dw (np.ndarray[double , ndim = 1 , mode = " c" ] dm,
293280 np.ndarray[double , ndim = 1 , mode = " c" ] spin,
294281 np.ndarray[double , ndim = 1 , mode = " c" ] field,
@@ -298,3 +285,64 @@ def compute_llg_rhs_dw(np.ndarray[double, ndim=1, mode="c"] dm,
298285 np.ndarray[double , ndim = 1 , mode = " c" ] eta,
299286 np.ndarray[int , ndim = 1 , mode = " c" ] pin, n , gamma , dt ):
300287 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)
288+
289+
290+ cdef class rng_mt19937(object ):
291+ cdef mt19937_state * _c_state
292+ cdef public int seed
293+ def __init__ (self , seed = None ):
294+ if seed:
295+ self .seed = int (seed)
296+ else :
297+ self .seed = time(NULL )
298+
299+ self ._c_state = create_mt19937_state()
300+ if self ._c_state is NULL :
301+ raise MemoryError ()
302+
303+ initial_rng_mt19973(self ._c_state, self .seed)
304+
305+ def set_seed (self , seed ):
306+ self .seed = int (seed)
307+ initial_rng_mt19973(self ._c_state, self .seed)
308+
309+ def random (self ):
310+ """
311+ return a random number in [0,1)
312+ """
313+ return random_double_half_open(self ._c_state)
314+
315+ def fill_vector_gaussian (self , np.ndarray[np.float64_t , ndim = 1 ] vector):
316+ gauss_random_vector(self ._c_state, & vector[0 ], vector.shape[0 ])
317+
318+ def fill_vector_uniform_sphere (self , np.ndarray[double , ndim = 1 , mode = " c" ] spin, n ):
319+ uniform_random_sphere(self ._c_state,& spin[0 ], n)
320+
321+ def __dealloc__ (self ):
322+ if self ._c_state is not NULL :
323+ finalize_mt19937_state(self ._c_state)
324+ self ._c_state = NULL
325+
326+ cdef class monte_carlo(object ):
327+ cdef mt19937_state * _c_state
328+
329+ def __init__ (self , seed = - 1 ):
330+
331+ self ._c_state = create_mt19937_state()
332+ if self ._c_state is NULL :
333+ raise MemoryError ()
334+
335+ initial_rng_mt19973(self ._c_state, seed)
336+
337+ def set_seed (self , seed ):
338+ initial_rng_mt19973(self ._c_state, seed)
339+
340+ def run_step (self ,np.ndarray[double , ndim = 1 , mode = " c" ] spin,
341+ np.ndarray[double , ndim = 1 , mode = " c" ] new_spin,
342+ np.ndarray[int , ndim = 2 , mode = " c" ] ngbs,
343+ np.ndarray[int , ndim = 2 , mode = " c" ] nngbs,
344+ J , J1 , D , D1 , np.ndarray[double , ndim = 1 , mode = " c" ] h,
345+ Kc , n , T , hexagnoal_mesh ):
346+
347+ 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)
348+
0 commit comments