Skip to content

Commit

Permalink
return abs value of vector, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjonesBSU committed Dec 19, 2023
1 parent fce9ad5 commit bf87674
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmeutils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_backbone_vector(coordinates):
_, _, V = np.linalg.svd(centered_coordinates)
# The first principal component (V[0]) is the vec of the best-fit line
direction_vector = V[0]
return direction_vector
return np.abs(direction_vector)


def get_plane_normal(points):
Expand Down
19 changes: 19 additions & 0 deletions cmeutils/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import numpy as np
import pytest
from base_test import BaseTest
from mbuild.lib.recipes import Alkane

from cmeutils.geometry import (
angle_between_vectors,
get_backbone_vector,
get_plane_normal,
moit,
radial_grid_positions,
Expand All @@ -14,6 +16,23 @@


class TestGeometry(BaseTest):
def test_backbone_vector(self):
with pytest.raises(ValueError):
coordinates = np.array([1, 1, 1])
get_backbone_vector(coordinates)

z_coords = np.array([[0, 0, 1], [0, 0, 2], [0, 0, 3]])
backbone = get_backbone_vector(z_coords)
assert np.allclose(backbone, np.array([0, 0, 1]), atol=1e-5)

x_coords = np.array([[1, 0, 0], [2, 0, 0], [3, 0, 0]])
backbone = get_backbone_vector(x_coords)
assert np.allclose(backbone, np.array([1, 0, 0]), atol=1e-5)

mb_chain = Alkane(n=20)
chain_backbone = get_backbone_vector(mb_chain.xyz)
assert np.allclose(chain_backbone, np.array([0, 1, 0]), atol=1e-2)

def test_moit(self):
_moit = moit(points=[(-1, 0, 0), (1, 0, 0)], masses=[1, 1])
assert np.array_equal(_moit, np.array([0, 2.0, 2.0]))
Expand Down

0 comments on commit bf87674

Please sign in to comment.