forked from duartegroup/autodE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_constraints.py
116 lines (81 loc) · 3.13 KB
/
test_constraints.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
import pytest
from autode.constraints import Constraints, DistanceConstraints
def test_base_properties():
consts = Constraints()
assert "constraints" in str(consts).lower()
assert "constraints" in repr(consts).lower()
assert not consts.any
assert consts.distance is None
assert consts.cartesian is None
consts.update(distance={(0, 1): 1.0})
assert consts.any
assert consts.distance is not None
assert consts.cartesian is None
# Constraints should allow for setting
consts.distance = {(0, 1): 2}
assert int(consts.distance[(0, 1)]) == 2
# And be permutationally invariant
assert consts.distance[(0, 1)] == consts.distance[(1, 0)]
with pytest.raises(Exception):
consts.distance = 1
with pytest.raises(Exception):
consts.distance = {(0, 1): -1.0} # Invalid distance
with pytest.raises(Exception):
consts.distance = {0: 1.0} # Invalid atom index pair
with pytest.raises(Exception):
consts.distance = {(0, -1): 1.0} # Invalid atom index (-1)
# Non-unique pairs are skipped
consts.distance[(0,)] = 1.0
assert len(consts.distance) == 1
consts.distance[(0, 0)] = 1.0
assert len(consts.distance) == 1
with pytest.raises(Exception):
consts.distance[(0, 1)] = -1.0
# Likewise with setting cartesian constraints
consts.cartesian = [0, 1]
assert int(consts.cartesian[0]) == 0
with pytest.raises(Exception):
consts.cartesian = 5
conts2 = Constraints(cartesian=[0], distance={(0, 1): 1.1})
assert conts2.cartesian is not None and conts2.distance is not None
assert "0" in str(conts2) and "1.1" in str(conts2)
def test_multiple_update():
consts = Constraints()
consts.update(distance={(0, 1): 1.0})
consts.update(distance={(1, 0): 1.0})
assert len(consts.distance) == 1
# Non unique pairs should be skipped
consts.update(distance={(0, 0): 1.0})
assert len(consts.distance) == 1
# Cannot have negative distances
with pytest.raises(ValueError):
consts.update(distance={(0, 1): -1.0})
def test_cartesian_update():
conts = Constraints(cartesian=[0, 1])
assert conts.any
assert len(conts.cartesian) == 2
# Should only have the unique components
conts.update(cartesian=[0])
assert len(conts.cartesian) == 2
def test_clear():
# Like species.conformers setting to None should still allow future updates
consts = Constraints(distance={(0, 1): 1.0})
assert consts.distance is not None
consts.distance.clear()
assert consts.distance is None
consts.update(distance={(0, 1): 1.0})
consts.distance = None
assert consts.distance is None
consts.update(distance={(0, 1): 1.0})
assert len(consts.distance) == 1
consts.cartesian = [0, 1]
assert len(consts.cartesian) == 2
consts.cartesian = None
assert consts.cartesian is None
consts.update(cartesian=[0])
assert len(consts.cartesian) == 1
def test_copy():
constraints = DistanceConstraints({(0, 1): 1.0})
copied_constraints = constraints.copy()
constraints[(0, 1)] = 1.1
assert abs(copied_constraints[(0, 1)] - 1.0) < 1e-10