-
Notifications
You must be signed in to change notification settings - Fork 5
/
Util.pyx
67 lines (57 loc) · 2.55 KB
/
Util.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# cython: profile=False
# filename: Util.pyx
import numpy as np
cimport numpy as np
import cython
from libc.math cimport ceil, floor, sqrt
FTYPE = np.float32
ctypedef np.float32_t FTYPE_t
cdef FTYPE_t cround(FTYPE_t x):
return ceil(x - 0.5) if x < 0. else floor(x + 0.5)
@cython.boundscheck(False) # turn off bounds-checking for entire function
def min_img_vec(np.ndarray[FTYPE_t, ndim=1] x, np.ndarray[FTYPE_t, ndim=1] y, np.ndarray[FTYPE_t, ndim=1] img, bint periodic=True):
cdef np.ndarray[FTYPE_t, ndim=1] dx = np.empty(3, dtype=FTYPE)
cdef int i
for i in range(3):
dx[i] = x[i] - y[i]
if(periodic):
dx[i] -= cround(dx[i] / img[i]) * img[i]
return dx
#put x into the same image as y
@cython.boundscheck(False) # turn off bounds-checking for entire function
def same_img(np.ndarray[FTYPE_t, ndim=1] x, np.ndarray[FTYPE_t, ndim=1] y, np.ndarray[FTYPE_t, ndim=1] img):
cdef FTYPE_t dx
cdef int i
for i in range(3):
dx = x[i] - y[i]
x[i] -= cround(dx / img[i]) * img[i]
return x
@cython.boundscheck(False) # turn off bounds-checking for entire function
def min_img(np.ndarray[FTYPE_t, ndim=1] x, np.ndarray[FTYPE_t, ndim=1] img, bint periodic=True):
cdef int i
for i in range(3):
x[i] -= floor(x[i] / img[i]) * img[i]
return x
@cython.boundscheck(False) # turn off bounds-checking for entire function
cpdef FTYPE_t min_img_dist_sq(np.ndarray[FTYPE_t, ndim=1] x, np.ndarray[FTYPE_t, ndim=1] y, np.ndarray[FTYPE_t, ndim=1] img, bint periodic=True):
cdef FTYPE_t dx
cdef FTYPE_t dist = 0
cdef int i
for i in range(3):
dx = x[i] - y[i]
if(periodic):
dx -= cround(dx / img[i]) * img[i]
dist += dx * dx
return dist
def min_img_dist(np.ndarray[FTYPE_t, ndim=1] x, np.ndarray[FTYPE_t, ndim=1] y, np.ndarray[FTYPE_t, ndim=1] img, bint periodic=True):
return sqrt(min_img_dist_sq(x, y, img, periodic))
@cython.boundscheck(False) # turn off bounds-checking for entire function
cpdef double norm3(np.ndarray[FTYPE_t, ndim=1] x):
return sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2])
def spec_force_inner_loop(np.ndarray[FTYPE_t, ndim=1] w, np.ndarray[FTYPE_t, ndim=1] basis_out,
np.ndarray[FTYPE_t, ndim=2] grad, np.ndarray[FTYPE_t, ndim=1] force,
np.ndarray[FTYPE_t, ndim=1] r):
for i in range(w.shape[0]):
for j in range(r.shape[0]):
force[j] = force[j] + w[i] * basis_out[i] * r[j]
grad[i,j] = basis_out[i] * r[j] + grad[i,j]