Skip to content

Commit

Permalink
rename calc_bonds
Browse files Browse the repository at this point in the history
  • Loading branch information
hmacdope committed Jan 2, 2025
1 parent 5c0997d commit c96e7a3
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 203 deletions.
12 changes: 6 additions & 6 deletions distopia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from .pydistopia import (
calc_bonds_ortho,
calc_bonds_no_box,
calc_bonds_triclinic,
calc_distances_ortho,
calc_distances_no_box,
calc_distances_triclinic,
calc_angles_no_box,
calc_angles_ortho,
calc_angles_triclinic,
Expand All @@ -18,9 +18,9 @@
)

__all__ = [
'calc_bonds_ortho',
'calc_bonds_no_box',
'calc_bonds_triclinic',
'calc_distances_ortho',
'calc_distances_no_box',
'calc_distances_triclinic',
'calc_angles_no_box',
'calc_angles_ortho',
'calc_angles_triclinic',
Expand Down
18 changes: 9 additions & 9 deletions distopia/pydistopia.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ cdef extern from "distopia.h" namespace "distopia" nogil:
int GetNFloatLanes();
int GetNDoubleLanes();

void CalcBondsNoBox[T](
void CalcDistancesNoBox[T](
const T *coords0,
const T *coords1,
size_t n,
T *output
)
void CalcBondsOrtho[T](
void CalcDistancesOrtho[T](
const T *coords0,
const T *coords1,
size_t n,
const T *box,
T *output
)
void CalcBondsTriclinic[T](
void CalcDistancesTriclinic[T](
const T *coords0,
const T *coords1,
size_t n,
Expand Down Expand Up @@ -168,7 +168,7 @@ def _check_shapes(*args):

@cython.boundscheck(False)
@cython.wraparound(False)
def calc_bonds_no_box(floating[:, ::1] coords0,
def calc_distances_no_box(floating[:, ::1] coords0,
floating[:, ::1] coords1,
results=None):
"""Calculate pairwise distances between coords0 and coords1 with no periodic boundary conditions
Expand Down Expand Up @@ -203,14 +203,14 @@ def calc_bonds_no_box(floating[:, ::1] coords0,

results_view = results

CalcBondsNoBox(& coords0[0][0], & coords1[0][0], nvals, & results_view[0])
CalcDistancesNoBox(& coords0[0][0], & coords1[0][0], nvals, & results_view[0])

return np.array(results)


@cython.boundscheck(False)
@cython.wraparound(False)
def calc_bonds_ortho(floating[:, ::1] coords0,
def calc_distances_ortho(floating[:, ::1] coords0,
floating[:, ::1] coords1,
floating[::1] box,
floating[::1] results=None):
Expand Down Expand Up @@ -250,14 +250,14 @@ def calc_bonds_ortho(floating[:, ::1] coords0,

results_view = results

CalcBondsOrtho(& coords0[0][0], & coords1[0][0], nvals, & box[0], & results_view[0])
CalcDistancesOrtho(& coords0[0][0], & coords1[0][0], nvals, & box[0], & results_view[0])

return np.array(results)


@cython.boundscheck(False)
@cython.wraparound(False)
def calc_bonds_triclinic(floating[:, ::1] coords0,
def calc_distances_triclinic(floating[:, ::1] coords0,
floating[:, ::1] coords1,
floating[:, ::1] box,
floating[::1] results=None):
Expand Down Expand Up @@ -296,7 +296,7 @@ def calc_bonds_triclinic(floating[:, ::1] coords0,

results_view = results

CalcBondsTriclinic(& coords0[0][0], & coords1[0][0], nvals, & box[0][0], & results_view[0])
CalcDistancesTriclinic(& coords0[0][0], & coords1[0][0], nvals, & box[0][0], & results_view[0])

return np.array(results)

Expand Down
42 changes: 21 additions & 21 deletions distopia/tests/test_distopia.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def result_shim(self, make_arr, N, dtype):
@pytest.mark.parametrize("box", ([10, 10, 10], [100, 20, 10]))
@pytest.mark.parametrize("N", (0, 10, 1000, 10000))
@pytest.mark.parametrize("use_result_buffer", (True, False))
def test_calc_bonds_ortho_all_zero(self, N, box, use_result_buffer, dtype):
def test_calc_distances_ortho_all_zero(self, N, box, use_result_buffer, dtype):
c0 = self.arange_input(N, dtype)
c1 = self.arange_input(N, dtype)
result_buffer = self.result_shim(use_result_buffer, N, dtype)
box = np.asarray(box, dtype=dtype)
result = distopia.calc_bonds_ortho(
result = distopia.calc_distances_ortho(
c0, c1, box, results=result_buffer
)
assert_allclose(result, np.zeros(N))
Expand All @@ -45,11 +45,11 @@ def test_calc_bonds_ortho_all_zero(self, N, box, use_result_buffer, dtype):
@pytest.mark.parametrize("dtype", (np.float32, np.float64))
@pytest.mark.parametrize("N", (0, 10, 1000, 10000))
@pytest.mark.parametrize("use_result_buffer", (True, False))
def test_calc_bonds_nobox_all_zero(self, N, use_result_buffer, dtype):
def test_calc_distances_nobox_all_zero(self, N, use_result_buffer, dtype):
c0 = self.arange_input(N, dtype)
c1 = self.arange_input(N, dtype)
result_buffer = self.result_shim(use_result_buffer, N, dtype)
result = distopia.calc_bonds_no_box(c0, c1, results=result_buffer)
result = distopia.calc_distances_no_box(c0, c1, results=result_buffer)
assert_allclose(result, np.zeros(N))
# check dtype of result
assert result.dtype == dtype
Expand All @@ -58,21 +58,21 @@ def test_calc_bonds_nobox_all_zero(self, N, use_result_buffer, dtype):
@pytest.mark.parametrize("dtype", (np.float32, np.float64))
@pytest.mark.parametrize("N", (0, 10, 1000, 10000))
@pytest.mark.parametrize("use_result_buffer", (True, False))
def test_calc_bonds_triclinic_all_zero(self, N, use_result_buffer, dtype):
def test_calc_distances_triclinic_all_zero(self, N, use_result_buffer, dtype):
c0 = self.arange_input(N, dtype)
c1 = self.arange_input(N, dtype)
result_buffer = self.result_shim(use_result_buffer, N, dtype)
box = np.asarray([[30, 0, 0], [-2.6146722, 29.885841, 0], [-10.260604, 9.402112, 26.576687]], dtype=dtype)
result = distopia.calc_bonds_triclinic(c0, c1, box, results=result_buffer)
result = distopia.calc_distances_triclinic(c0, c1, box, results=result_buffer)
assert_allclose(result, np.zeros(N))

def test_calc_bonds_inplace_results(self):
def test_calc_distances_inplace_results(self):
N = 100
dtype = np.float32
c0 = self.arange_input(N, dtype)
c1 = self.arange_input(N, dtype) + 1
result_buffer = np.empty(N, dtype=dtype)
result = distopia.calc_bonds_no_box(c0, c1, results=result_buffer)
result = distopia.calc_distances_no_box(c0, c1, results=result_buffer)
assert_allclose(result, result_buffer)
assert_allclose(result, np.linalg.norm(c0 - c1, axis=1))

Expand All @@ -83,27 +83,27 @@ def test_no_box_bad_result_or_input_shape(self):
c0 = np.zeros(6, dtype=np.float32).reshape(2, 3)
c1 = np.zeros(6, dtype=np.float32).reshape(2, 3)
with pytest.raises(ValueError, match="results must be"):
distopia.calc_bonds_no_box(c0, c1, results=np.empty(1, dtype=np.float32))
distopia.calc_distances_no_box(c0, c1, results=np.empty(1, dtype=np.float32))
with pytest.raises(ValueError, match="All input arrays must"):
distopia.calc_bonds_no_box(c0, c1[:-1])
distopia.calc_distances_no_box(c0, c1[:-1])

def test_ortho_bad_result_or_input_shape(self):
c0 = np.zeros(6, dtype=np.float32).reshape(2, 3)
c1 = np.zeros(6, dtype=np.float32).reshape(2, 3)
box = np.array([10, 10, 10], dtype=np.float32)
with pytest.raises(ValueError, match="results must be"):
distopia.calc_bonds_ortho(c0, c1, box, results=np.empty(1, dtype=np.float32))
distopia.calc_distances_ortho(c0, c1, box, results=np.empty(1, dtype=np.float32))
with pytest.raises(ValueError, match="All input arrays must"):
distopia.calc_bonds_ortho(c0, c1[:-1], box)
distopia.calc_distances_ortho(c0, c1[:-1], box)

def test_triclinic_bad_result_or_input_shape(self):
c0 = np.zeros(6, dtype=np.float32).reshape(2, 3)
c1 = np.zeros(6, dtype=np.float32).reshape(2, 3)
box = np.array([[10, 0, 0], [0, 10, 0], [0, 0, 10]], dtype=np.float32)
with pytest.raises(ValueError, match="results must be"):
distopia.calc_bonds_triclinic(c0, c1, box, results=np.empty(1, dtype=np.float32))
distopia.calc_distances_triclinic(c0, c1, box, results=np.empty(1, dtype=np.float32))
with pytest.raises(ValueError, match="All input arrays must"):
distopia.calc_bonds_triclinic(c0, c1[:-1], box)
distopia.calc_distances_triclinic(c0, c1[:-1], box)



Expand Down Expand Up @@ -371,13 +371,13 @@ def test_bonds(self, box_bonds, dtype, positions):
a, b, c, d = convert_ndarray(a, b, c, d, dtype=dtype)
box_bonds = convert_ndarray(box_bonds, dtype=dtype)

dists = distopia.calc_bonds_no_box(a, b)
assert_equal(len(dists), 4, err_msg="calc_bonds results have wrong length")
dists_pbc = distopia.calc_bonds_ortho(a, b, box_bonds)
dists = distopia.calc_distances_no_box(a, b)
assert_equal(len(dists), 4, err_msg="calc_distances results have wrong length")
dists_pbc = distopia.calc_distances_ortho(a, b, box_bonds)
#tests 0 length
assert_almost_equal(dists[0], 0.0, self.prec, err_msg="Zero length calc_bonds fail")
assert_almost_equal(dists[0], 0.0, self.prec, err_msg="Zero length calc_distances fail")
assert_almost_equal(dists[1], 1.7320508075688772, self.prec,
err_msg="Standard length calc_bonds fail") # arbitrary length check
err_msg="Standard length calc_distances fail") # arbitrary length check
# PBC checks, 2 without, 2 with
assert_almost_equal(dists[2], 11.0, self.prec,
err_msg="PBC check #1 w/o box") # pbc check 1, subtract single box length
Expand All @@ -392,9 +392,9 @@ def test_bonds(self, box_bonds, dtype, positions):
@pytest.mark.parametrize("dtype", (np.float32, np.float64))
def test_bonds_triclinic(self, triclinic_box, dtype, positions):
a, b, c, d = positions
dists = distopia.calc_bonds_triclinic(a, b, triclinic_box)
dists = distopia.calc_distances_triclinic(a, b, triclinic_box)
reference = np.array([0.0, 1.7320508, 1.4142136, 2.82842712])
assert_almost_equal(dists, reference, self.prec, err_msg="calc_bonds with triclinic box failed")
assert_almost_equal(dists, reference, self.prec, err_msg="calc_distances with triclinic box failed")



Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## API documentation for the `distopia` python layer

::: distopia.calc_bonds_no_box
::: distopia.calc_distances_no_box
:docstring:

::: distopia.calc_bonds_ortho
::: distopia.calc_distances_ortho
:docstring:

::: distopia.calc_bonds_triclinic
::: distopia.calc_distances_triclinic
:docstring:

::: distopia.calc_angles_no_box
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import distopia
N = 10000
coordinates0 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
coordinates1 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
result = distopia.calc_bonds_no_box(coordinates0, coordinates1)
result = distopia.calc_distances_no_box(coordinates0, coordinates1)

# alternatively we can pass in a buffer to use for the results.
buffer = np.empty(N, dtype=np.float32)
result = distopia.calc_bonds_no_box(coordinates0, coordinates1, results=buffer)
result = distopia.calc_distances_no_box(coordinates0, coordinates1, results=buffer)
```

### Orthorhombic periodic boundary conditions
Expand All @@ -38,7 +38,7 @@ N = 10000
coordinates0 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
coordinates1 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
box = np.asarray([10, 10, 10]).astype(np.float32)
result = distopia.calc_bonds_ortho(coordinates0, coordinates1, box)
result = distopia.calc_distances_ortho(coordinates0, coordinates1, box)
```

### Triclinic periodic boundary conditions
Expand All @@ -53,7 +53,7 @@ N = 10000
coordinates0 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
coordinates1 = np.random.rand(3 * N).reshape(N, 3).astype(np.float32)
box = np.asarray([[10, 0, 0], [0, 10, 0], [0, 0, 10]]).astype(np.float32)
result = distopia.calc_bonds_triclinic(coordinates0, coordinates1, box)
result = distopia.calc_distances_triclinic(coordinates0, coordinates1, box)
```

### Note
Expand Down
12 changes: 6 additions & 6 deletions libdistopia/include/distopia.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@


namespace distopia {
template <typename T> void CalcBondsNoBox(const T *a, const T *b, int n, T *out);
template <typename T> void CalcBondsOrtho(const T *a, const T *b, int n, const T *box, T *out);
template <typename T> void CalcBondsTriclinic(const T *a, const T *b, int n, const T *box, T *out);
template <typename T> void CalcDistancesNoBox(const T *a, const T *b, int n, T *out);
template <typename T> void CalcDistancesOrtho(const T *a, const T *b, int n, const T *box, T *out);
template <typename T> void CalcDistancesTriclinic(const T *a, const T *b, int n, const T *box, T *out);
template <typename T> void CalcAnglesNoBox(const T *a, const T *b, const T *c, int n, T *out);
template <typename T> void CalcAnglesOrtho(const T *a, const T *b, const T *c, int n, const T *box, T *out);
template <typename T> void CalcAnglesTriclinic(const T *a, const T *b, const T *c, int n, const T *box, T *out);
Expand All @@ -31,9 +31,9 @@ namespace distopia {
template <typename T> void CalcSelfDistanceArrayNoBox(const T *a, int n, T *out);
template <typename T> void CalcSelfDistanceArrayOrtho(const T *a, int n, const T *box, T *out);
template <typename T> void CalcSelfDistanceArrayTriclinic(const T *a, int n, const T *box, T *out);
template <typename T> void CalcBondsNoBoxIdx(const T *coords, const int *a_idx, const int *b_idx, int n, T *out);
template <typename T> void CalcBondsOrthoIdx(const T *coords, const int *a_idx, const int *b_idx, int n, const T *box, T *out);
template <typename T> void CalcBondsTriclinicIdx(const T *coords, const int *a_idx, const int *b_idx, int n, const T *box, T *out);
template <typename T> void CalcDistancesNoBoxIdx(const T *coords, const int *a_idx, const int *b_idx, int n, T *out);
template <typename T> void CalcDistancesOrthoIdx(const T *coords, const int *a_idx, const int *b_idx, int n, const T *box, T *out);
template <typename T> void CalcDistancesTriclinicIdx(const T *coords, const int *a_idx, const int *b_idx, int n, const T *box, T *out);
template <typename T> void CalcAnglesNoBoxIdx(const T *coords, const int *a_idx, const int *b_idx, const int *c_idx, int n, T *out);
template <typename T> void CalcAnglesOrthoIdx(const T *coords, const int *a_idx, const int *b_idx, const int *c_idx, int n, const T *box, T *out);
template <typename T> void CalcAnglesTriclinicIdx(const T *coords, const int *a_idx, const int *b_idx, const int *c_idx, int n, const T *box, T *out);
Expand Down
Loading

0 comments on commit c96e7a3

Please sign in to comment.