Skip to content

Commit ed68830

Browse files
Consolidate vector mathematics into a single C++ file native/c_vectormath.cpp. Make some of these external C++ declarations available in other modules by using a Cython pxd file, vectormath.pxd.
Co-Authored-By: David Ignacio Cortee <[email protected]>
1 parent 8307070 commit ed68830

22 files changed

+164
-227
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ CPPFLAGS = -fPIC -g -Iinclude/ \
2020
-lfftw3_omp -lfftw3 \
2121
-lsundials_cvodes -lsundials_nvecserial -lsundials_nvecopenmp \
2222
-lblas -llapack \
23-
-fopenmp
23+
-fopenmp \
24+
-std=c++14
2425

2526
LDFLAGS = -shared
2627
SOURCES = $(shell echo native/src/*.cpp)

fidimag/atomistic/a_clib.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# distutils: language = c++
2-
32
import numpy
43
cimport numpy as np
4+
cimport fidimag.common.vectormath as vmath
55
np.import_array()
66

7+
8+
79
cdef extern from "a_random.h":
810
ctypedef struct mt19937_state:
911
pass
@@ -97,8 +99,6 @@ cdef extern from "a_clib.h":
9799

98100
# -------------------------------------------------------------------------
99101

100-
void normalise(double *m, int *pins, int n)
101-
102102
# used for sllg
103103
void llg_rhs_dw_c(double *m, double *h, double *dm, double *T, double *alpha,
104104
double *mu_s_inv, int *pins, double *eta, int n,
@@ -272,7 +272,7 @@ def compute_demag_full(double [:] spin,
272272

273273
def normalise_spin(np.ndarray[double, ndim=1, mode="c"] spin,
274274
np.ndarray[int, ndim=1, mode="c"] pins, n):
275-
normalise(&spin[0], &pins[0], n)
275+
vmath.normalise(&spin[0], &pins[0], n)
276276

277277
def compute_llg_rhs_dw(double [:] dm,
278278
double [:] spin,

fidimag/common/neb_method/nebm_clib.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# distutils: language = c++
22

3+
cimport fidimag.common.vectormath as vmath
4+
35
cdef extern from "c_nebm_lib.h":
46
void compute_tangents_C(double *tangents,
57
double *y,
@@ -23,7 +25,7 @@ cdef extern from "c_nebm_lib.h":
2325
int n_images,
2426
int n_dofs_image)
2527

26-
void normalise(double * a, int n)
28+
2729
void normalise_images_C(double * y, int n_images, int n_dofs_image)
2830
void normalise_spins_C(double * y, int n_images, int n_dofs_image)
2931
double compute_distance_cartesian(double * A, double * B, int n_dofs_image,
@@ -112,7 +114,7 @@ def compute_effective_force(double [:] G,
112114
)
113115

114116
def normalise_clib(double [:] a, n):
115-
normalise(&a[0], n)
117+
vmath.normalise(&a[0], n)
116118

117119
def project_images(double [:] vector,
118120
double [:] y,

fidimag/common/sundials/cvode.pyx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cimport openmp
77
np.import_array() # don't remove or you'll segfault
88
from libc.string cimport memcpy
99
import sys
10+
from fidimag.common cimport vectormath
1011

1112

1213
cdef extern from "sundials/sundials_types.h":
@@ -16,7 +17,7 @@ cdef extern from "sundials/sundials_types.h":
1617
cdef extern from "sundials/sundials_nvector.h":
1718
cdef struct _generic_N_Vector:
1819
void *content
19-
20+
2021
ctypedef _generic_N_Vector *N_Vector
2122
N_Vector N_VNew_Serial(long int vec_length)
2223
N_Vector N_VNew_OpenMP(long int vec_length, int num_threads)
@@ -170,16 +171,10 @@ cdef extern from "sundials/sundials_iterative.h":
170171
int PREC_LEFT
171172
int PREC_RIGHT
172173
int PREC_BOTH
173-
174+
174175
int MODIFIED_GS
175176
int CLASSICAL_GS
176177

177-
178-
179-
cdef extern from "a_clib.h":
180-
void normalise(double * m, int nxyz)
181-
182-
183178
cdef struct cv_userdata:
184179
void * rhs_fun
185180
void * y

fidimag/common/vectormath.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cdef extern from "c_vectormath.h":
2+
void normalise(double *m, int *pins, int n)
3+
void normalise(double * a, int n)

native/include/a_clib.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,11 @@
44
#include <fftw3.h>
55
#include <math.h>
66
#include<omp.h>
7-
87
#include "a_random.h"
8+
#include "c_vectormath.h"
99

1010
#define WIDE_PI 3.1415926535897932384626433832795L
1111

12-
// ----------------------------------------------------------------------------
13-
/* 3 components for the cross product calculations */
14-
15-
inline double cross_x(double a0, double a1, double a2, double b0, double b1,
16-
double b2) {
17-
return a1 * b2 - a2 * b1;
18-
}
19-
inline double cross_y(double a0, double a1, double a2, double b0, double b1,
20-
double b2) {
21-
return a2 * b0 - a0 * b2;
22-
}
23-
inline double cross_z(double a0, double a1, double a2, double b0, double b1,
24-
double b2) {
25-
return a0 * b1 - a1 * b0;
26-
}
27-
2812
// ----------------------------------------------------------------------------
2913
// From exch.c
3014

@@ -101,8 +85,6 @@ void compute_px_py_c(double *spin, int nx, int ny, int nz, double *px,
10185
// ----------------------------------------------------------------------------
10286
// From sllg.c
10387

104-
void normalise(double *m, int *pins, int n);
105-
10688
void llg_rhs_dw_c(double * m, double * h, double * dm, double * T, double * alpha,
10789
double * mu_s_inv, int *pins, double * eta, int n, double gamma,
10890
double dt);

native/include/c_clib.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,6 @@
44
#include<omp.h>
55
#define WIDE_PI 3.1415926535897932384626433832795L
66

7-
// ----------------------------------------------------------------------------
8-
9-
/* 3 components for the cross product calculations */
10-
inline double cross_x(double a0, double a1, double a2, double b0, double b1,
11-
double b2) {
12-
return a1 * b2 - a2 * b1;
13-
}
14-
inline double cross_y(double a0, double a1, double a2, double b0, double b1,
15-
double b2) {
16-
return a2 * b0 - a0 * b2;
17-
}
18-
inline double cross_z(double a0, double a1, double a2, double b0, double b1,
19-
double b2) {
20-
return a0 * b1 - a1 * b0;
21-
}
22-
23-
inline void normalise(double *m, int *pins, int n){
24-
int i, j, k;
25-
double mm;
26-
for (int id = 0; id < n; id++) {
27-
i = 3*id;
28-
j = i + 1;
29-
k = j + 1;
30-
31-
if (pins[id]>0) continue;
32-
33-
mm = sqrt(m[i] * m[i] + m[j] * m[j] + m[k] * m[k]);
34-
if(mm > 0) {
35-
mm = 1 / mm;
36-
m[i] *= mm;
37-
m[j] *= mm;
38-
m[k] *= mm;
39-
}
40-
}
41-
}
42-
437
// ----------------------------------------------------------------------------
448
// From: llg.c
459

native/include/c_dipolar.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#include<math.h>
22
#include<complex>
33
#include<fftw3.h>
4+
#include "c_vectormath.h"
45
//#include<omp.h>
56

67
#define WIDE_PI 3.1415926535897932384626433832795L
78

8-
inline double cross_x(double a0, double a1, double a2, double b0, double b1, double b2) { return a1*b2 - a2*b1; }
9-
inline double cross_y(double a0, double a1, double a2, double b0, double b1, double b2) { return a2*b0 - a0*b2; }
10-
inline double cross_z(double a0, double a1, double a2, double b0, double b1, double b2) { return a0*b1 - a1*b0; }
11-
129

1310
enum Type_Nij {
1411
Tensor_xx, Tensor_yy, Tensor_zz, Tensor_xy, Tensor_xz, Tensor_yz

native/include/c_nebm_lib.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
#include "math.h"
22
#define WIDE_PI 3.1415926535897932384626433832795L
33

4-
5-
void cross_product(double * output, double * A, double * B);
6-
7-
double dot_product(double * A, double * B, int n);
8-
9-
double compute_norm(double * a, int n);
10-
11-
void normalise(double * a, int n);
12-
134
void compute_tangents_C(double * ys, double * energy,
145
double * tangents, int image_num, int nodes
156
);

native/include/m_clib.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#define MU0 1.25663706143591728850e-6
66
#define MU0_INV 795774.71545947669074
77

8-
double cross_x(double a0, double a1, double a2, double b0, double b1, double b2);
9-
double cross_y(double a0, double a1, double a2, double b0, double b1, double b2);
10-
double cross_z(double a0, double a1, double a2, double b0, double b1, double b2);
11-
128
void compute_exch_field_micro(double * m, double * field, double * energy, double * Ms_inv,
139
double A, double dx, double dy, double dz, int n, int *ngbs);
1410

0 commit comments

Comments
 (0)