Skip to content

Commit

Permalink
Change to inplace API. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmacdope authored Nov 7, 2022
1 parent 7f99b07 commit afa8f74
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 63 deletions.
90 changes: 61 additions & 29 deletions distopia/_distopia.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ cdef extern from "distopia.h" nogil:

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_ortho_float(float[:, ::1] coords0,
float[:, ::1] coords1,
float[::1] box):
cpdef cnp.ndarray[cnp.float32_t, ndim = 1] calc_bonds_ortho_float(float[:, ::1] coords0,
float[:, ::1] coords1,
float[::1] box,
cnp.ndarray results=None):
"""Calculate pairwise distances between coords0 and coords1
Parameters
----------
coords0, coords1 : float32 array
must be same length
box : float32 array
box : float32 array
periodic boundary dimensions
results: float32 array (optional)
array to store results in, must be same size as coords0/coords1
Returns
-------
Expand All @@ -60,7 +63,8 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_ortho_float(float[:, ::1] co
cdef size_t nvals = coords0.shape[0]
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float32_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)

results_view = results

Expand All @@ -71,9 +75,10 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_ortho_float(float[:, ::1] co

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_ortho_double(double[:, ::1] coords0,
cpdef cnp.ndarray[cnp.float64_t, ndim = 1] calc_bonds_ortho_double(double[:, ::1] coords0,
double[:, ::1] coords1,
double[::1] box):
double[::1] box,
cnp.ndarray results=None):
"""Calculate pairwise distances between coords0 and coords1
Parameters
Expand All @@ -82,6 +87,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_ortho_double(double[:, ::1]
must be same length
box : float64 array
periodic boundary dimensions
results: float64 array (optional)
array to store results in, must be same size as coords0/coords1
Returns
-------
Expand All @@ -92,7 +99,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_ortho_double(double[:, ::1]
cdef size_t nvals = coords0.shape[0]
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float64_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)

results_view = results

Expand All @@ -103,14 +111,17 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_ortho_double(double[:, ::1]

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_no_box_float(float[:, ::1] coords0,
float[:, ::1] coords1):
cpdef cnp.ndarray[cnp.float32_t, ndim = 1] calc_bonds_no_box_float(float[:, ::1] coords0,
float[:, ::1] coords1,
cnp.ndarray results=None):
"""Calculate pairwise distances between coords0 and coords1
Parameters
----------
coords0, coords1 : float32 array
must be same length
results: float32 array (optional)
array to store results in, must be same size as coords0/coords1
Returns
-------
Expand All @@ -121,7 +132,8 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_no_box_float(float[:, ::1] c
cdef size_t nvals = coords0.shape[0]
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float32_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)

results_view = results

Expand All @@ -132,8 +144,9 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_no_box_float(float[:, ::1] c

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_no_box_double(double[:, ::1] coords0,
double[:, ::1] coords1):
cpdef cnp.ndarray[cnp.float64_t, ndim = 1] calc_bonds_no_box_double(double[:, ::1] coords0,
double[:, ::1] coords1,
cnp.ndarray results=None):
"""Calculate pairwise distances between coords0 and coords1
Parameters
Expand All @@ -142,6 +155,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_no_box_double(double[:, ::1]
must be same length
box : float64 array
periodic boundary dimensions
results: float64 array (optional)
array to store results in, must be same size as coords0/coords1
Returns
-------
Expand All @@ -152,7 +167,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_no_box_double(double[:, ::1]
cdef size_t nvals = coords0.shape[0]
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float64_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)

results_view = results

Expand All @@ -163,9 +179,10 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_no_box_double(double[:, ::1]

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_ortho_float(float[:, ::1] coords,
size_t[::1] idx,
float[::1] box):
cpdef cnp.ndarray[cnp.float32_t, ndim = 1] calc_bonds_idx_ortho_float(float[:, ::1] coords,
size_t[::1] idx,
float[::1] box,
cnp.ndarray results=None):
"""Calculate distances between pairs of coordinates by index
Parameters
Expand All @@ -176,6 +193,8 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_ortho_float(float[:, ::1
array of integers to calculate distances for
box : float32 array
periodic boundary dimensions
results: float32 array (optional)
array to store results in, must be half the size of idx
Returns
-------
Expand All @@ -186,7 +205,8 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_ortho_float(float[:, ::1
cdef size_t nvals = idx.shape[0] // 2 # SAFE?
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float32_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)

results_view = results

Expand All @@ -197,9 +217,10 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_ortho_float(float[:, ::1

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_ortho_double(double[:, ::1] coords,
size_t[::1] idx,
double[::1] box):
cpdef cnp.ndarray[cnp.float64_t, ndim = 1] calc_bonds_idx_ortho_double(double[:, ::1] coords,
size_t[::1] idx,
double[::1] box,
cnp.ndarray results=None):
"""Calculate distances between pairs of coordinates by index
Parameters
Expand All @@ -210,6 +231,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_ortho_double(double[:, :
array of integers to calculate distances for
box : float64 array
periodic boundary dimensions
results: float64 array (optional)
array to store results in, must be half the size of idx
Returns
-------
Expand All @@ -220,7 +243,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_ortho_double(double[:, :
cdef size_t nvals = idx.shape[0] // 2 # SAFE?
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float64_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)

results_view = results

Expand All @@ -231,8 +255,9 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_ortho_double(double[:, :

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_no_box_float(float[:, ::1] coords,
size_t[::1] idx):
cpdef cnp.ndarray[cnp.float32_t, ndim = 1] calc_bonds_idx_no_box_float(float[:, ::1] coords,
size_t[::1] idx,
cnp.ndarray results=None):
"""Calculate distances between pairs of coordinates by index
Parameters
Expand All @@ -241,7 +266,9 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_no_box_float(float[:, ::
array of coordinates
idx: int array
array of integers to calculate distances for
results: float32 array (optional)
array to store results in, must be half the size of idx
Returns
-------
distances : float32 array
Expand All @@ -251,7 +278,8 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_no_box_float(float[:, ::
cdef size_t nvals = idx.shape[0] // 2 # SAFE?
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float32_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT32, 0)

results_view = results

Expand All @@ -262,8 +290,9 @@ cpdef cnp.ndarray[cnp.float32_t, ndim=1] calc_bonds_idx_no_box_float(float[:, ::

@cython.boundscheck(False)
@cython.wraparound(False)
cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_no_box_double(double[:, ::1] coords,
size_t[::1] idx):
cpdef cnp.ndarray[cnp.float64_t, ndim = 1] calc_bonds_idx_no_box_double(double[:, ::1] coords,
size_t[::1] idx,
cnp.ndarray results=None):
"""Calculate distances between pairs of coordinates by index
Parameters
Expand All @@ -272,6 +301,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_no_box_double(double[:,
array of coordinates
idx: int array
array of integers to calculate distances for
results: float64 array (optional)
array to store results in, must be half the size of idx
Returns
-------
Expand All @@ -282,7 +313,8 @@ cpdef cnp.ndarray[cnp.float64_t, ndim=1] calc_bonds_idx_no_box_double(double[:,
cdef size_t nvals = idx.shape[0] // 2 # SAFE?
cdef cnp.npy_intp[1] dims
dims[0] = <ssize_t > nvals # FIXME truncation?
cdef cnp.ndarray[cnp.float64_t, ndim=1] results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)
if results is None:
results = cnp.PyArray_EMPTY(1, dims, cnp.NPY_FLOAT64, 0)

results_view = results

Expand Down
Loading

0 comments on commit afa8f74

Please sign in to comment.