forked from duartegroup/autodE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_values.py
137 lines (87 loc) · 2.89 KB
/
test_values.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
import pytest
from autode.units import ang, ha, ha_per_ang, ha_per_a0, ev
from autode.values import (
ValueArray,
Gradient,
Coordinate,
Coordinates,
MomentOfInertia,
ForceConstant,
_to,
)
class TmpValues(ValueArray):
implemented_units = [ha, ev]
def __repr__(self):
return ""
def test_base_arr():
tmp_values = TmpValues(np.arange(2))
assert tmp_values.units is None
tmp_values = TmpValues(np.arange(2), units=ha)
assert tmp_values.units == ha
for item in (None, "a", 0, np.zeros(2)):
# These are not the same! != calls __ne__
assert not tmp_values == item
assert tmp_values != item
def test_unit_retention():
vals = TmpValues(np.array([0.1]), units=ev)
assert vals.units == ev
# Initialising an array from something with units should not default to the
# default unit type (Hartrees for energies)
vals1 = TmpValues(vals)
assert vals1.units == ev
def test_coordinate():
coord = Coordinate(0.0, 0.0, 0.0)
assert coord.units == ang
assert "coord" in repr(coord).lower()
assert coord is not None
# Equality defaults to np.allclose
assert coord == np.zeros(3)
def test_coordinates():
arr = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.1]])
coords = Coordinates(arr)
assert coords.units == ang
# Coordinates should reshape into n_atoms x 3
coords = Coordinates(arr.flatten())
assert coords.shape == (2, 3)
assert "coord" in repr(coords).lower()
def test_moi():
moi = MomentOfInertia(np.zeros(shape=(3, 3)))
assert "i" in repr(moi).lower()
def test_gradients():
# Default gradient units are Ha Å^-1
gradients = Gradient(np.arange(3, dtype="f8"))
assert gradients.units == ha_per_ang
assert "grad" in repr(gradients).lower()
gradients_ha_a0 = gradients.to(ha_per_a0)
# Energy per bohr is smaller than per angstrom..
assert all(
g1 - g2 <= 0
for g1, g2 in zip(gradients_ha_a0.flatten(), gradients.flatten())
)
class Unit:
conversion = 1.0
aliases = ["unit"]
def lower(self) -> str:
return "unit"
class InvalidValue(float):
implemented_units = [Unit]
units = Unit()
def test_to_unsupported():
with pytest.raises(ValueError):
_ = _to(InvalidValue(), Unit(), inplace=True)
def test_inplace_modification():
x = Gradient([[1.0, 1.0, 1.0]], units="Ha / Å")
return_value = x.to_("eV / Å")
assert return_value is None
assert not np.allclose(x, np.ones(shape=(1, 3)))
def test_copy_conversion():
x = Gradient([[1.0, 1.0, 1.0]], units="Ha / Å")
y = x.to("eV / Å")
assert not np.allclose(x, y)
assert np.allclose(x, np.ones(shape=(1, 3)))
def test_force_constant():
fc = ForceConstant(0.1)
assert "force" in repr(fc).lower()
# should be able to convert to Ha/a0^2 without any problems
_ = fc.to("Ha/a0^2")