Skip to content

Commit f70a244

Browse files
author
Weiwei Wang
committed
clean the cython version of sllg class
1 parent 4fd0d97 commit f70a244

File tree

3 files changed

+0
-283
lines changed

3 files changed

+0
-283
lines changed

fidimag/atomistic/lib/clib.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,5 @@ void compute_px_py_c(double *spin, int nx, int ny, int nz,
8484
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
8585
double *mu_s_inv, int *pins, double *eta, int n, double gamma, double dt);
8686

87-
//=========================================================
88-
//=========================================================
89-
//used for sode
90-
typedef struct {
91-
int n;
92-
93-
double dt;
94-
double T;
95-
double gamma;
96-
double *mu_s;
97-
double coeff;
98-
double Q;
99-
100-
double theta;
101-
double theta1;
102-
double theta2;
103-
104-
double *dm1;
105-
double *dm2;
106-
double *eta;
107-
108-
} ode_solver;
109-
110-
void init_solver(ode_solver *s, double k_B, double theta,
111-
int n, double dt, double gamma);
112-
113-
ode_solver *create_ode_plan(void);
114-
115-
void finalize_ode_plan(ode_solver *plan);
116-
117-
void run_step1(ode_solver *s, double *m, double *h, double *m_pred, double *T,
118-
double *alpha, double *mu_s_inv, int *pins);
119-
120-
void run_step2(ode_solver *s, double *m_pred, double *h, double *m, double *T,
121-
double *alpha, double *mu_s_inv, int *pins);
12287

12388
#endif

fidimag/atomistic/lib/clib.pyx

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,6 @@ cdef extern from "clib.h":
7171

7272
# used for sllg
7373
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)
74-
75-
ctypedef struct ode_solver:
76-
pass
77-
78-
ode_solver *create_ode_plan()
79-
80-
void init_solver(ode_solver *s, double k_B, double theta,
81-
int n, double dt, double gamma)
82-
83-
void finalize_ode_plan(ode_solver *plan)
84-
85-
void run_step1(ode_solver *s, double *m, double *h,
86-
double *m_pred, double *T, double *alpha,
87-
double *mu_s_inv, int *pins)
88-
89-
void run_step2(ode_solver *s, double *m_pred,
90-
double *h, double *m, double *T,
91-
double *alpha, double *mu_s_inv, int *pins)
9274

9375

9476
def compute_skyrmion_number(np.ndarray[double, ndim=1, mode="c"] spin,
@@ -285,89 +267,3 @@ def compute_llg_rhs_dw(np.ndarray[double, ndim=1, mode="c"] dm,
285267
np.ndarray[double, ndim=1, mode="c"] eta,
286268
np.ndarray[int, ndim=1, mode="c"] pin, n, gamma, dt):
287269
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 RK2S(object):
291-
cdef ode_solver * _c_plan
292-
cdef double dt
293-
cdef update_fun
294-
cdef np.ndarray pred_m
295-
cdef np.ndarray field
296-
cdef np.ndarray mu_s_inv
297-
cdef np.ndarray T
298-
cdef np.ndarray alpha
299-
cdef np.ndarray pins
300-
301-
cdef public double t
302-
cdef public int step
303-
cdef public np.ndarray y
304-
305-
def __cinit__(self,dt,n,gamma,k_B,theta,
306-
np.ndarray[double, ndim=1, mode="c"] mu_s_inv,
307-
np.ndarray[double, ndim=1, mode="c"] alpha,
308-
np.ndarray[double, ndim=1, mode="c"] spin,
309-
np.ndarray[double, ndim=1, mode="c"] field,
310-
np.ndarray[double, ndim=1, mode="c"] T,
311-
np.ndarray[int, ndim=1, mode="c"] pins,
312-
update_fun):
313-
314-
self.t = 0
315-
self.step = 0
316-
self.dt = dt
317-
318-
self.update_fun = update_fun
319-
self.mu_s_inv = mu_s_inv
320-
self.field = field
321-
self.T = T
322-
self.alpha = alpha
323-
self.pins = pins
324-
self.pred_m = numpy.zeros(3*n,dtype=numpy.float)
325-
self.y = numpy.zeros(3*n,dtype=numpy.float)
326-
327-
328-
self._c_plan = create_ode_plan()
329-
if self._c_plan is NULL:
330-
raise MemoryError()
331-
332-
init_solver(self._c_plan,k_B,theta,n,dt,gamma)
333-
334-
def __dealloc__(self):
335-
if self._c_plan is not NULL:
336-
finalize_ode_plan(self._c_plan)
337-
self._c_plan = NULL
338-
339-
def set_initial_value(self,np.ndarray[double, ndim=1, mode="c"] spin, t):
340-
self.t = t
341-
self.y[:] = spin[:]
342-
343-
def successful(self):
344-
#print self.spin
345-
return True
346-
347-
def run_step(self):
348-
cdef np.ndarray[double, ndim=1, mode="c"] y=self.y
349-
cdef np.ndarray[double, ndim=1, mode="c"] field=self.field
350-
cdef np.ndarray[double, ndim=1, mode="c"] pred_m=self.pred_m
351-
cdef np.ndarray[double, ndim=1, mode="c"] T=self.T
352-
cdef np.ndarray[double, ndim=1, mode="c"] alpha=self.alpha
353-
cdef np.ndarray[double, ndim=1, mode="c"] mu_s_inv=self.mu_s_inv
354-
cdef np.ndarray[int, ndim=1, mode="c"] pins = self.pins
355-
356-
#print "from cython1", self.spin,self.field,self.pred_m
357-
self.update_fun(self.y, self.t)
358-
run_step1(self._c_plan, &y[0], &field[0], &pred_m[0],
359-
&T[0], &alpha[0], &mu_s_inv[0], &pins[0])
360-
361-
self.step += 1
362-
self.t = self.step * self.dt
363-
364-
self.update_fun(self.pred_m, self.t)
365-
run_step2(self._c_plan, &pred_m[0], &field[0],
366-
&y[0], &T[0], &alpha[0], &mu_s_inv[0], &pins[0])
367-
368-
369-
def run_until(self, t):
370-
while (self.t<t):
371-
print "from cython %g"%self.t
372-
self.run_step()
373-
return 0

fidimag/atomistic/lib/sllg.c

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -42,150 +42,6 @@ void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha, do
4242
}
4343
}
4444

45-
46-
47-
ode_solver *create_ode_plan(void) {
48-
49-
ode_solver *plan = (ode_solver*) malloc(sizeof(ode_solver));
50-
51-
return plan;
52-
}
53-
54-
55-
void init_solver(ode_solver *s, double k_B, double theta, int n, double dt, double gamma) {
56-
57-
s->theta = theta;
58-
s->theta1 = 1-0.5/theta;
59-
s->theta2 = 0.5/theta;
60-
61-
s->dt = dt;
62-
s->n = n;
63-
s->gamma = gamma;
64-
65-
s->Q = 2 * k_B * dt / gamma;
66-
67-
s->dm1 = (double*) malloc(3 * n * sizeof(double));
68-
s->dm2 = (double*) malloc(3 * n * sizeof(double));
69-
s->eta = (double*) malloc(3 * n * sizeof(double));
70-
71-
int i = 0;
72-
for (i = 0; i < 3 * n; i++) {
73-
s->dm1[i] = 0;
74-
s->dm2[i] = 0;
75-
s->eta[i] = 0;
76-
}
77-
78-
initial_random(2);
79-
}
80-
81-
82-
void llg_rhs_dw(ode_solver *s, double *m, double *h, double *dm, double *T, double *alpha, double *mu_s_inv, int *pins) {
83-
84-
int i, j, k;
85-
86-
double mth0, mth1, mth2;
87-
88-
int n = s->n;
89-
double *eta = &s->eta[0];
90-
double dt = s->dt;
91-
double Q=s->Q;
92-
93-
double coeff, q;
94-
double hi,hj,hk;
95-
96-
#pragma omp parallel for private(i,j,k,q,coeff,hi,hj,hk,mth0,mth1,mth2)
97-
for (int id = 0; id < n; id++) {
98-
i = 3*id;
99-
j = i+1;
100-
k = j+1;
101-
102-
if (pins[id]>0){
103-
dm[i] = 0;
104-
dm[j] = 0;
105-
dm[k] = 0;
106-
continue;
107-
}
108-
109-
110-
coeff = -s->gamma/ (1.0 + alpha[id] * alpha[id]) ;
111-
q = sqrt(Q * alpha[i] * T[id] * mu_s_inv[id]);
112-
113-
hi = h[i]*dt + eta[i]*q;
114-
hj = h[j]*dt + eta[j]*q;
115-
hk = h[k]*dt + eta[k]*q;
116-
117-
mth0 = coeff * (m[j] * hk - m[k] * hj);
118-
mth1 = coeff * (m[k] * hi - m[i] * hk);
119-
mth2 = coeff * (m[i] * hj - m[j] * hi);
120-
121-
122-
dm[i] = mth0 + alpha[i] * (m[j] * mth2 - m[k] * mth1);
123-
dm[j] = mth1 + alpha[i] * (m[k] * mth0 - m[i] * mth2);
124-
dm[k] = mth2 + alpha[i] * (m[i] * mth1 - m[j] * mth0);
125-
126-
}
127-
}
128-
129-
void run_step1(ode_solver *s, double *m, double *h, double *m_pred, double *T, double *alpha, double *mu_s_inv, int *pins) {
130-
int i;
131-
int n = s->n;
132-
double *dm1 = s->dm1;
133-
double theta = s->theta;
134-
135-
gauss_random_vec(s->eta, 3 * n);
136-
llg_rhs_dw(s, m, h, dm1, T, alpha, mu_s_inv, pins);
137-
138-
//#pragma omp parallel for private(i)
139-
for (i = 0; i < 3 * s->n; i++) {
140-
m_pred[i] = m[i] + theta * dm1[i];
141-
}
142-
143-
}
144-
145-
void run_step2(ode_solver *s, double *m_pred, double *h, double *m, double *T, double *alpha, double *mu_s_inv, int *pins) {
146-
int i, j, k;
147-
int n = s->n;
148-
double *dm1 = s->dm1;
149-
double *dm2 = s->dm2;
150-
double theta1 = s->theta1;
151-
double theta2 = s->theta2;
152-
153-
llg_rhs_dw(s, m_pred, h, dm2, T, alpha, mu_s_inv, pins);
154-
155-
//#pragma omp parallel for private(i)
156-
for (i = 0; i < 3 * n; i++) {
157-
m[i] += (theta1 * dm1[i] + theta2 * dm2[i]);
158-
}
159-
160-
double mm;
161-
#pragma omp parallel for private(i,j,k,mm)
162-
for (int id = 0; id < s->n; id++) {
163-
164-
if (pins[id]>0){
165-
continue;
166-
}
167-
168-
i= 3*id;
169-
j = i + 1;
170-
k = j + 1;
171-
172-
mm = 1.0 / sqrt(m[i] * m[i] + m[j] * m[j] + m[k] * m[k]);
173-
m[i] *= mm;
174-
m[j] *= mm;
175-
m[k] *= mm;
176-
}
177-
//we have to say that this kind of method is quite inaccurate, so in future we can try other methods
178-
179-
}
180-
181-
void finalize_ode_plan(ode_solver *plan) {
182-
free(plan->dm1);
183-
free(plan->dm2);
184-
free(plan->eta);
185-
free(plan);
186-
}
187-
188-
18945
void normalise(double *m, int n){
19046
int i, j, k;
19147
double mm;

0 commit comments

Comments
 (0)