Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rotate_360(input_atoms, x, y, z, centered):
"""
Rotate by 360 degrees and expect that the coordinates have not
changed.
"""
func = struc.rotate_centered if centered else struc.rotate
rotated = func(input_atoms, [x, y, z])
assert type(rotated) == type(input_atoms)
assert struc.coord(rotated).shape == struc.coord(input_atoms).shape
assert np.allclose(
struc.coord(rotated), struc.coord(input_atoms), atol=1e-5
)
if centered and struc.coord(input_atoms).ndim > 1:
assert np.allclose(
struc.centroid(rotated), struc.centroid(input_atoms), atol=1e-5
)
"""
np.random.seed(random_seed)
angles = np.zeros(3)
angles[axis] = np.random.rand() * 2*np.pi
neg_angles = -angles
if as_list:
angles = angles.tolist()
neg_angles = neg_angles.tolist()
func = struc.rotate_centered if centered else struc.rotate
rotated = func(input_atoms, angles)
restored = func(rotated, neg_angles)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
print(np.max(np.abs(struc.coord(restored) - struc.coord(input_atoms))))
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
if centered and struc.coord(input_atoms).ndim > 1:
assert np.allclose(
struc.centroid(restored), struc.centroid(input_atoms), atol=1e-5
)
the same.
"""
np.random.seed(random_seed)
angles = np.zeros(3)
angles[axis] = np.random.rand() * 2*np.pi
neg_angles = -angles
if as_list:
angles = angles.tolist()
neg_angles = neg_angles.tolist()
func = struc.rotate_centered if centered else struc.rotate
rotated = func(input_atoms, angles)
restored = func(rotated, neg_angles)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
print(np.max(np.abs(struc.coord(restored) - struc.coord(input_atoms))))
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
if centered and struc.coord(input_atoms).ndim > 1:
assert np.allclose(
struc.centroid(restored), struc.centroid(input_atoms), atol=1e-5
)
def test_dihedral():
coord1 = struc.coord([-0.5,-1,0])
coord2 = struc.coord([0,0,0])
coord3 = struc.coord([1,0,0])
coord4 = struc.coord([0,0,-1])
assert struc.dihedral(coord1, coord2, coord3, coord4) \
== pytest.approx(0.5*np.pi)
np.random.seed(random_seed)
vectors = np.random.rand(*struc.coord(input_atoms).shape[-ndim:])
vectors *= 10
neg_vectors = -vectors
if as_list:
vectors = vectors.tolist()
neg_vectors = neg_vectors.tolist()
translated = struc.translate(input_atoms, vectors)
restored = struc.translate(translated, neg_vectors)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
transformed = struc.align_vectors(
input_atoms,
source_direction, target_direction,
source_position, target_position
)
restored = struc.align_vectors(
transformed,
target_direction, source_direction,
target_position, source_position
)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
neg_angles = -angles
if as_list:
angles = angles.tolist()
neg_angles = neg_angles.tolist()
func = struc.rotate_centered if centered else struc.rotate
rotated = func(input_atoms, angles)
restored = func(rotated, neg_angles)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
print(np.max(np.abs(struc.coord(restored) - struc.coord(input_atoms))))
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
if centered and struc.coord(input_atoms).ndim > 1:
assert np.allclose(
struc.centroid(restored), struc.centroid(input_atoms), atol=1e-5
)
def test_distance():
coord1 = struc.coord([0,1,1])
coord2 = struc.coord([0,2,2])
assert struc.distance(coord1, coord2) == pytest.approx(np.sqrt(2))
def test_angle():
coord1 = struc.coord([0,0,1])
coord2 = struc.coord([0,0,0])
coord3 = struc.coord([0,1,1])
assert struc.angle(coord1, coord2, coord3) == pytest.approx(0.25*np.pi)