Skip to content

Commit 8d09ae0

Browse files
committed
Expose Complex FFTW arrays to Python in Dipolar structs
1 parent ce9c10a commit 8d09ae0

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

fidimag/common/dipolar/dipolar.pyx

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ import ctypes
44
cimport numpy as np
55
np.import_array()
66

7-
8-
9-
10-
11-
cdef pointer_to_numpy_array_float64(void * ptr, np.npy_intp size):
12-
'''Convert c pointer to numpy array.
13-
The memory will be freed as soon as the ndarray is deallocated.
14-
'''
15-
cdef extern from "numpy/arrayobject.h":
16-
void PyArray_ENABLEFLAGS(np.ndarray arr, int flags)
17-
cdef np.ndarray[np.float64_t, ndim=1] arr = \
18-
np.PyArray_SimpleNewFromData(1, &size, np.NPY_FLOAT64, ptr)
19-
PyArray_ENABLEFLAGS(arr, np.NPY_OWNDATA)
20-
return arr
21-
227
cdef extern from "dipolar.h":
238

249
# used for demag
@@ -33,6 +18,18 @@ cdef extern from "dipolar.h":
3318
double *tensor_yy
3419
double *tensor_yz
3520
double *tensor_zz
21+
complex *Nxx
22+
complex *Nxy
23+
complex *Nxz
24+
complex *Nyy
25+
complex *Nyz
26+
complex *Nzz
27+
complex *Hx
28+
complex *Hy
29+
complex *Hz
30+
complex *Mx
31+
complex *My
32+
complex *Mz
3633

3734
fft_demag_plan * create_plan()
3835
void finalize_plan(fft_demag_plan * plan)
@@ -51,8 +48,11 @@ cdef class FFTDemag(object):
5148
cdef int total_length
5249
cdef np.float64_t[:] tensor_xx_p, tensor_xy_p, tensor_xz_p, tensor_yy_p, \
5350
tensor_yz_p, tensor_zz_p
51+
cdef np.complex128_t[:] Nxx_p, Nxy_p, Nxz_p, Nyy_p, Nyz_p, Nzz_p, Hx_p, \
52+
Hy_p, Hz_p, Mx_p, My_p, Mz_p
5453
cdef public np.ndarray tensor_xx, tensor_xy, tensor_xz, tensor_yy, \
55-
tensor_yz, tensor_zz
54+
tensor_yz, tensor_zz, Nxx, Nxy, Nxz, Nyy, Nyz, Nzz, \
55+
Mx, My, Mz, Hx, Hy, Hz
5656
#tensor_type could be 'dipolar', 'demag' or '2d_pbc'
5757
def __cinit__(self, dx, dy, dz, nx, ny, nz, tensor_type='dipolar'):
5858
self._c_plan = create_plan()
@@ -75,6 +75,35 @@ cdef class FFTDemag(object):
7575
self.total_length = int(self._c_plan.total_length)
7676
self.tensor_xx_p = <np.float64_t[:self.total_length]> self._c_plan.tensor_xx
7777
self.tensor_xx = np.asarray(self.tensor_xx_p)
78+
79+
self.Nxx_p = <np.complex128_t[:self.total_length]> self._c_plan.Nxx
80+
self.Nxx = np.asarray(self.Nxx_p)
81+
self.Nxy_p = <np.complex128_t[:self.total_length]> self._c_plan.Nxy
82+
self.Nxy = np.asarray(self.Nxy_p)
83+
self.Nxz_p = <np.complex128_t[:self.total_length]> self._c_plan.Nxz
84+
self.Nxz = np.asarray(self.Nxz_p)
85+
self.Nyy_p = <np.complex128_t[:self.total_length]> self._c_plan.Nyy
86+
self.Nyy = np.asarray(self.Nyy_p)
87+
self.Nyz_p = <np.complex128_t[:self.total_length]> self._c_plan.Nyz
88+
self.Nyz = np.asarray(self.Nyz_p)
89+
self.Nzz_p = <np.complex128_t[:self.total_length]> self._c_plan.Nzz
90+
self.Nzz = np.asarray(self.Nzz_p)
91+
92+
self.Mx_p = <np.complex128_t[:self.total_length]> self._c_plan.Mx
93+
self.Mx = np.asarray(self.Mx_p)
94+
self.My_p = <np.complex128_t[:self.total_length]> self._c_plan.My
95+
self.My = np.asarray(self.My_p)
96+
self.Mz_p = <np.complex128_t[:self.total_length]> self._c_plan.Mz
97+
self.Mz = np.asarray(self.Mz_p)
98+
99+
self.Hx_p = <np.complex128_t[:self.total_length]> self._c_plan.Hx
100+
self.Hx = np.asarray(self.Hx_p)
101+
self.Hy_p = <np.complex128_t[:self.total_length]> self._c_plan.Hy
102+
self.Hy = np.asarray(self.Hy_p)
103+
self.Hz_p = <np.complex128_t[:self.total_length]> self._c_plan.Hz
104+
self.Hz = np.asarray(self.Hz_p)
105+
106+
78107
self.tensor_xy_p = <np.float64_t[:self.total_length]> self._c_plan.tensor_xy
79108
self.tensor_xy = np.asarray(self.tensor_xy_p)
80109
self.tensor_xz_p = <np.float64_t[:self.total_length]> self._c_plan.tensor_xz
@@ -86,6 +115,7 @@ cdef class FFTDemag(object):
86115
self.tensor_zz_p = <np.float64_t[:self.total_length]> self._c_plan.tensor_zz
87116
self.tensor_zz = np.asarray(self.tensor_zz_p)
88117

118+
89119
def print_tensor(self):
90120
for k in range(self._c_plan.lenz):
91121
for j in range(self._c_plan.leny):

0 commit comments

Comments
 (0)