-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_schema_keywords.py
299 lines (249 loc) · 13 KB
/
test_schema_keywords.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Tests the DQM compute dispatch module
"""
import pytest
try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError
from qcelemental.models import Molecule
# qcng: from qcengine.procedures.manybody import ManyBodyComputer
from qcmanybody.computer import ManyBodyComputer
from qcmanybody.models import BsseEnum, ManyBodyInput, ManyBodyResultProperties
@pytest.fixture(scope="function")
def mbe_data():
henehh = Molecule(symbols=["He", "Ne", "H", "H"], fragments=[[0], [1], [2, 3]], geometry=[0, 0, 0, 2, 0, 0, 0, 1, 0, 0, -1, 0])
return {
"specification": {
"specification": {
"model": {
"method": "hf",
"basis": "cc-pvdz",
},
"driver": "energy",
"program": "psi4",
},
"keywords": {
"bsse_type": "cp",
},
"driver": "gradient",
},
"molecule": henehh,
}
@pytest.fixture(scope="function")
def mbe_data_multilevel():
henehh = Molecule(symbols=["He", "Ne", "H", "H"], fragments=[[0], [1], [2, 3]], geometry=[0, 0, 0, 2, 0, 0, 0, 1, 0, 0, -1, 0])
return {
"specification": {
"specification": {
"(auto)": { #p4-hf-dz": {
"model": {
"method": "hf",
"basis": "cc-pvdz",
},
"driver": "energy",
"program": "psi4",
},
"p4-mp2-dz": {
"model": {
"method": "mp2",
"basis": "cc-pvdz",
},
"driver": "energy",
"program": "psi4",
},
"c4-hf-dz": {
"model": {
"method": "hf",
"basis": "cc-pvdz",
},
"driver": "energy",
"program": "cfour",
},
"c4-ccsd-tz": {
"model": {
"method": "ccsd",
"basis": "cc-pvtz",
},
"driver": "energy",
"program": "cfour",
},
},
"keywords": {
"bsse_type": "nocp",
},
"driver": "gradient",
},
"molecule": henehh,
}
@pytest.mark.parametrize("driver,kws,ans", [
pytest.param("energy", {}, False),
pytest.param("energy", {"return_total_data": True}, True),
pytest.param("energy", {"return_total_data": False}, False),
pytest.param("gradient", {}, True),
pytest.param("hessian", {}, True),
pytest.param("hessian", {"return_total_data": True}, True),
pytest.param("hessian", {"return_total_data": False}, False),
])
def test_mbe_rtd(mbe_data, driver, kws, ans):
mbe_data["specification"]["driver"] = driver
mbe_data["specification"]["keywords"] = kws
input_model = ManyBodyInput(**mbe_data)
comp_model = ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert comp_model.driver == driver
assert comp_model.return_total_data == ans
@pytest.mark.parametrize("kws,ans", [
pytest.param({}, [3, {3: "(auto)"}, [[1, 2, 3]] ]),
pytest.param({"max_nbody": 3}, [3, {3: "(auto)"}, [[1, 2, 3]] ]),
pytest.param({"max_nbody": 2}, [2, {2: "(auto)"}, [[1, 2]] ]),
pytest.param({"max_nbody": 1}, [1, {1: "(auto)"}, [[1]] ]),
# TODO? when supersystem_ie_only=T, nbodies_per_mc_level and levels isn't really accurate
pytest.param({"supersystem_ie_only": True}, [3, {3: "(auto)"}, [[1, 2, 3]] ]),
pytest.param({"supersystem_ie_only": False, "max_nbody": 2}, [2, {2: "(auto)"}, [[1, 2]] ]),
pytest.param({"supersystem_ie_only": True, "max_nbody": 3}, [3, {3: "(auto)"}, [[1, 2, 3]] ]),
pytest.param({"levels": {3: "mp2"}}, [3, {3: "mp2"}, [[1, 2, 3]] ]),
pytest.param({"levels": {1: "mp2", 3: "ccsd"}}, [3, {1: "mp2", 3: "ccsd"}, [[1], [2, 3]] ]),
pytest.param({"levels": {2: "ccsd", 3: "mp2"}}, [3, {2: "ccsd", 3: "mp2"}, [[1, 2], [3]] ]),
pytest.param({"levels": {2: "ccsd"}}, [2, {2: "ccsd"}, [[1, 2]] ]),
pytest.param({"levels": {2: "ccsd", 1: "ccsd(t)"}}, [2, {1: "ccsd(t)", 2: "ccsd"}, [[1], [2]] ]),
pytest.param({"levels": {1: "ccsd(t)"}}, [1, {1: "ccsd(t)"}, [[1]] ]),
pytest.param({"levels": {"supersystem": "hf", 1: "ccsd(t)"}}, [1, {1: "ccsd(t)", "supersystem": "hf"}, [[1], ["supersystem"]] ]),
pytest.param({"levels": {2: "ccsd(t)", "supersystem": "hf"}}, [2, {2: "ccsd(t)", "supersystem": "hf"}, [[1, 2], ["supersystem"]] ]),
pytest.param({"max_nbody": 3, "levels": {3: "mp2"}}, [3, {3: "mp2"}, [[1, 2, 3]] ]),
pytest.param({"max_nbody": 3, "levels": {1: "mp2", 3: "ccsd"}}, [3, {1: "mp2", 3: "ccsd"}, [[1], [2, 3]] ]),
pytest.param({"max_nbody": 3, "levels": {2: "ccsd", 3: "mp2"}}, [3, {2: "ccsd", 3: "mp2"}, [[1, 2], [3]] ]),
pytest.param({"max_nbody": 2, "levels": {2: "ccsd"}}, [2, {2: "ccsd"}, [[1, 2]] ]),
pytest.param({"max_nbody": 2, "levels": {2: "ccsd", 1: "ccsd(t)"}}, [2, {1: "ccsd(t)", 2: "ccsd"}, [[1], [2]] ]),
pytest.param({"max_nbody": 1, "levels": {1: "ccsd(t)"}}, [1, {1: "ccsd(t)"}, [[1]] ]),
pytest.param({"max_nbody": 1, "levels": {"supersystem": "hf", 1: "ccsd(t)"}}, [1, {1: "ccsd(t)", "supersystem": "hf"}, [[1], ["supersystem"]] ]),
pytest.param({"max_nbody": 2, "levels": {2: "ccsd(t)", "supersystem": "hf"}}, [2, {2: "ccsd(t)", "supersystem": "hf"}, [[1, 2], ["supersystem"]] ]),
pytest.param({"levels": {2: 'scf/sto-3g', 1: 'mp2/sto-3g'}}, [2, {1: 'mp2/sto-3g', 2: 'scf/sto-3g'}, [[1], [2]] ]),
pytest.param({"levels": {1: 'mp2/sto-3g', 'supersystem': 'scf/sto-3g'}}, [1, {1: 'mp2/sto-3g', 'supersystem': 'scf/sto-3g'}, [[1], ["supersystem"]] ]),
pytest.param({"levels": {'supersystem': 'scf/sto-3g', 1: 'mp2/sto-3g'}}, [1, {1: 'mp2/sto-3g', 'supersystem': 'scf/sto-3g'}, [[1], ["supersystem"]] ]),
#pytest.param({}, [ , {}, [] ]),
])
def test_mbe_level_bodies(mbe_data, kws, ans):
mbe_data["specification"]["keywords"] = kws
input_model = ManyBodyInput(**mbe_data)
comp_model = ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert comp_model.nfragments == 3
assert comp_model.max_nbody == ans[0]
assert list(comp_model.levels.items()) == list(ans[1].items()) # compare as OrderedDict
assert comp_model.nbodies_per_mc_level == ans[2]
@pytest.mark.parametrize("kws,ans", [
pytest.param({"levels": {5: "hi"}}, [5, {5: "hi"}, [[1, 2, 3, 4, 5]] ]),
pytest.param({"levels": {5: "hi", 4: "md"}}, [5, {4: "md", 5: "hi"}, [[1, 2, 3, 4], [5]] ]),
pytest.param({"levels": {5: "hi", 3: "md"}}, [5, {3: "md", 5: "hi"}, [[1, 2, 3], [4, 5]] ]),
pytest.param({"levels": {5: "hi", 2: "md"}}, [5, {2: "md", 5: "hi"}, [[1, 2], [3, 4, 5]] ]),
pytest.param({"levels": {5: "hi", 1: "md"}}, [5, {1: "md", 5: "hi"}, [[1], [2, 3, 4, 5]] ]),
pytest.param({"levels": {4: "md", 3: "lo", 5: "hi"}}, [5, {3: "lo", 4: "md", 5: "hi"}, [[1, 2, 3], [4], [5]] ]),
pytest.param({"levels": {4: "md", 1: "lo", 5: "hi"}}, [5, {1: "lo", 4: "md", 5: "hi"}, [[1], [2, 3, 4], [5]] ]),
pytest.param({"levels": {4: "md", 2: "lo", 5: "hi"}}, [5, {2: "lo", 4: "md", 5: "hi"}, [[1, 2], [3, 4], [5]] ]),
pytest.param({"levels": {3: "md", 1: "lo", 5: "hi"}}, [5, {1: "lo", 3: "md", 5: "hi"}, [[1], [2, 3], [4, 5]] ]),
pytest.param({"levels": {3: "md", 1: "lo", 4: "hi"}}, [4, {1: "lo", 3: "md", 4: "hi"}, [[1], [2, 3], [4]] ]),
])
def test_mbe_level_5mer(mbe_data, kws, ans):
he3ne2 = Molecule(symbols=["He", "He", "He", "Ne", "Ne"], fragments=[[0], [1], [2], [3], [4]], geometry=[0, 0, 0, 2, 0, 0, -2, 0, 0, 0, 2, 0, 0, -2, 0])
mbe_data["molecule"] = he3ne2
mbe_data["specification"]["keywords"] = kws
input_model = ManyBodyInput(**mbe_data)
comp_model = ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert comp_model.nfragments == 5
assert comp_model.max_nbody == ans[0]
assert list(comp_model.levels.items()) == list(ans[1].items()) # compare as OrderedDict
assert comp_model.nbodies_per_mc_level == ans[2]
@pytest.mark.parametrize("kws,errmsg", [
pytest.param({"max_nbody": -2}, "should be between 1 and 3"),
pytest.param({"max_nbody": -1}, "should be between 1 and 3"),
pytest.param({"max_nbody": 4}, "should be between 1 and 3"),
# fails on v1 b/c val 2 coerced to a str pytest.param({"levels": {1: 2, 3: "mp2", 2: "ccsd"}}, "asdf"), # `1: 2 is old syntax and doesn't pass typing
pytest.param({"max_nbody": 1, "supersystem_ie_only": True}, "Cannot skip intermediate n-body jobs"),
# [3, supersystem]
# the two below could be healed up with a more sophisticated nbodies_per_mc_level building. Right now they violate the CORE == COMPUTER consistency checks
pytest.param({"levels": {3: "ccsd", 2: "ccsd"}}, "Cannot have duplicate model chemistries in levels"), #[3, {2: "ccsd", 3: "ccsd"}, [[1, 2, 3]] ]),
pytest.param({"max_nbody": 3, "levels": {3: "ccsd", 2: "ccsd"}}, "Cannot have duplicate model chemistries in levels"), #[3, {2: "ccsd", 3: "ccsd"}, [[1, 2, 3]] ]),
])
def test_mbe_level_fails(mbe_data, kws, errmsg):
mbe_data["specification"]["keywords"] = kws
# v2: with pytest.raises(Exception):
with pytest.raises(ValidationError) as e:
input_model = ManyBodyInput(**mbe_data)
ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert errmsg in str(e.value), e.value
@pytest.mark.parametrize("kws,ans", [
pytest.param({}, [BsseEnum.cp]),
pytest.param({"bsse_type": "CP"}, [BsseEnum.cp]),
pytest.param({"bsse_type": "nocp"}, [BsseEnum.nocp]),
pytest.param({"bsse_type": ["vmfc"]}, [BsseEnum.vmfc]),
pytest.param({"bsse_type": ["vmfc", "nocp"]}, [BsseEnum.vmfc, BsseEnum.nocp]),
pytest.param({"bsse_type": ["ssFC", "none"]}, [BsseEnum.cp, BsseEnum.nocp]),
pytest.param({"bsse_type": ["ssfc", "cp"]}, [BsseEnum.cp]),
pytest.param({"bsse_type": ["ssfc", "vmfc", "nocp", "cp"]}, [BsseEnum.cp, BsseEnum.vmfc, BsseEnum.nocp]),
pytest.param({"bsse_type": "mbE"}, [BsseEnum.nocp]),
pytest.param({"bsse_type": ["ssfc", "cp"]}, [BsseEnum.cp]),
pytest.param({"bsse_type": "mycp"}, "error"),
pytest.param({"bsse_type": ["CP", "mycp"]}, "error"),
])
def test_mbe_bsse_type(mbe_data, kws, ans):
mbe_data["specification"]["keywords"] = kws
if ans == "error":
with pytest.raises(ValidationError) as e:
input_model = ManyBodyInput(**mbe_data)
assert "not a valid enumeration member; permitted: 'nocp', 'cp', 'vmfc'" in str(e.value)
return
input_model = ManyBodyInput(**mbe_data)
comp_model = ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert comp_model.bsse_type == ans, f"{comp_model=} != {ans}"
@pytest.mark.parametrize("kws,ans", [
pytest.param({}, False),
pytest.param({"max_nbody": 3, "supersystem_ie_only": True}, True),
pytest.param({"supersystem_ie_only": False}, False),
pytest.param({"max_nbody": 1, "supersystem_ie_only": True},
"Cannot skip intermediate n-body jobs when max_nbody=1 != nfragments"),
pytest.param({"bsse_type": "vmFC", "supersystem_ie_only": True},
"Cannot skip intermediate n-body jobs when VMFC in bsse_type"),
pytest.param({"bsse_type": ["cp", "Vmfc"], "supersystem_ie_only": True},
"Cannot skip intermediate n-body jobs when VMFC in bsse_type"),
])
def test_mbe_sie(mbe_data, kws, ans):
mbe_data["specification"]["keywords"] = kws
if isinstance(ans, str):
input_model = ManyBodyInput(**mbe_data)
with pytest.raises(ValidationError) as e:
ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert ans in str(e.value)
return
input_model = ManyBodyInput(**mbe_data)
comp_model = ManyBodyComputer.from_manybodyinput(input_model, build_tasks=False)
assert comp_model.supersystem_ie_only == ans
@pytest.mark.parametrize("kws,ans", [
pytest.param({
"cp_corrected_total_energy_through_2_body": 2,
"cp_corrected_total_energy": 4,
}, 2),
pytest.param({
"calcinfo_nfr": 3,
"cp_corrected_total_energy_through_2_body": 2,
"cp_corrected_total_energy_through_51_body": 50,
"cp_corrected_total_energy": 5,
}, 4),
pytest.param({
"an_interloper": 3,
"cp_corrected_total_energy_through_2_body": 2,
"cp_corrected_total_energy": 4,
}, "Field names not allowed"),
pytest.param({
"an_interloper": 3,
"cp_corrected_total_energy_through_2_body": 2,
"cp_corrected_total_energy_through_50_body": 50,
"calcinfo_nfrrrrrrrrrrr": 3,
}, "Field names not allowed"),
])
def test_mbproperties_expansion(kws, ans):
if isinstance(ans, str):
with pytest.raises(ValidationError) as e:
input_model = ManyBodyResultProperties(**kws)
assert ans in str(e.value)
return
input_model = ManyBodyResultProperties(**kws)
assert len(input_model.dict()) == ans