-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_examples.py
251 lines (210 loc) · 9.64 KB
/
test_examples.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
import pprint
from qcelemental.testing import compare_values
try:
from .addons import uusing
except ImportError:
# for runnning file as script
from qcmanybody.tests.addons import uusing
# * for anyone replicating this, note that Psi4 MP2 is density-fitted, and NWChem CCSD is conventional.
# * Snippets refer to an imminent publication
@uusing("psi4")
@uusing("nwchem")
@uusing("qcengine")
def test_core_interface_example_1_show_running():
# Snippet 1
from qcelemental.models import Molecule
mol = Molecule(symbols=["ne", "ne", "ne"],
geometry=[[0,0,0],[0,0,2],[0,0,4]],
fragments=[[0], [1], [2]])
# Snippet 2
from qcmanybody import ManyBodyCore
mbc = ManyBodyCore(molecule=mol,
bsse_type=["cp", "vmfc"],
levels={1: "ccsd/cc-pvtz",
2: "mp2/cc-pvdz",
3: "mp2/cc-pvdz"},
return_total_data=True,
supersystem_ie_only=False,
embedding_charges=None)
# Running
import qcengine as qcng
from qcelemental.models import AtomicInput
from qcmanybody.utils import delabeler
calculation_results = {}
for chem, label, imol in mbc.iterate_molecules():
mc, real, bas = delabeler(label)
print(f"{label=} --> {mc=} {real=} {bas=}")
if chem == "mp2/cc-pvdz":
program = "psi4"
atin = AtomicInput(molecule=imol,
driver="gradient",
model={"method": "mp2", "basis": "cc-pvdz"})
elif chem == "ccsd/cc-pvtz":
program = "nwchem"
atin = AtomicInput(molecule=imol,
driver="gradient",
model={"method": "ccsd", "basis": "cc-pvtz"},
keywords={"ccsd__thresh": 1.e-7})
atres = qcng.compute(atin, program, raise_error=True)
calculation_results[label] = {
"energy": atres.properties.return_energy,
"gradient": atres.properties.return_gradient,
}
# Snippet 3 (partial; calculation_results generated by QC during iterate_molecules above)
final_results = mbc.analyze(calculation_results)
print(final_results["ret_energy"])
#> -384.1286
# Checking & Printing
assert compare_values([[0., 0., 2.832], [0., 0., 0.], [0., 0., -2.832]], final_results["ret_gradient"], atol=1.e-3, label="cp 3b tot G")
assert compare_values(-384.1286, final_results["ret_energy"], atol=1.e-4, label="cp 3b tot E")
pprint.pprint(final_results, width=200)
# Uncomment below to force fail and see `final_results` printing
# assert 0
def test_core_interface_example_2_show_processing():
# Snippet 1
from qcelemental.models import Molecule
mol = Molecule(symbols=["ne", "ne", "ne"],
geometry=[[0,0,0],[0,0,2],[0,0,4]],
fragments=[[0], [1], [2]])
# Snippet 2
from qcmanybody import ManyBodyCore
mbc = ManyBodyCore(molecule=mol,
bsse_type=["cp", "vmfc"],
levels={1: "ccsd/cc-pvtz",
2: "mp2/cc-pvdz",
3: "mp2/cc-pvdz"},
return_total_data=True,
supersystem_ie_only=False,
embedding_charges=None)
# Snippet 3 (filled out; generated by test_highlevel_interface_example below)
calculation_results = {
'["ccsd/cc-pvtz", [1], [1]]': {'energy': -128.818536, 'gradient': [[0., 0., 0.]]},
'["ccsd/cc-pvtz", [2], [2]]': {'energy': -128.818536, 'gradient': [[0., 0., 0.]]},
'["ccsd/cc-pvtz", [3], [3]]': {'energy': -128.818536, 'gradient': [[0., 0., 0.]]},
'["mp2/cc-pvdz", [1, 2, 3], [1, 2, 3]]': {
'energy': -383.723156,
'gradient': [[ 0. , 0. , 2.820548],
[ 0. , -0. , -0. ],
[-0. , 0. , -2.820548]]},
'["mp2/cc-pvdz", [1, 2], [1, 2, 3]]': {
'energy': -256.245775,
'gradient': [[ 0. , 0. , 2.686851],
[ 0. , -0. , -2.693556],
[ 0. , 0. , 0.006705]]},
'["mp2/cc-pvdz", [1, 2], [1, 2]]': {
'energy': -256.234927,
'gradient': [[-0. , 0. , 2.714538],
[ 0. , 0. , -2.714538]]},
'["mp2/cc-pvdz", [1, 3], [1, 2, 3]]': {
'energy': -257.356268,
'gradient': [[ 0. , 0. , 0.013743],
[-0. , 0. , 0. ],
[ 0. , 0. , -0.013743]]},
'["mp2/cc-pvdz", [1, 3], [1, 3]]': {
'energy': -257.346398,
'gradient': [[-0. , 0. , 0.019847],
[ 0. , 0. , -0.019847]]},
'["mp2/cc-pvdz", [1], [1, 2, 3]]': {
'energy': -128.682013,
'gradient': [[ 0. , -0. , -0.005872],
[-0. , 0. , 0.005266],
[ 0. , 0. , 0.000607]]},
'["mp2/cc-pvdz", [1], [1, 2]]': {
'energy': -128.6814438,
'gradient': [[ 0. , -0. , -0.005224],
[ 0. , 0. , 0.005224]]},
'["mp2/cc-pvdz", [1], [1, 3]]': {
'energy': -128.677532,
'gradient': [[ 0. , 0. , -0.000551],
[ 0. , -0. , 0.000551]]},
'["mp2/cc-pvdz", [2, 3], [1, 2, 3]]': {
'energy': -256.245775,
'gradient': [[ 0. , 0. , -0.006705],
[ 0. , 0. , 2.693556],
[-0. , 0. , -2.686851]]},
'["mp2/cc-pvdz", [2, 3], [2, 3]]': {
'energy': -256.234927,
'gradient': [[-0. , 0. , 2.714538],
[ 0. , -0. , -2.714538]]},
'["mp2/cc-pvdz", [2], [1, 2, 3]]': {
'energy': -128.686110,
'gradient': [[ 0. , 0. , -0.004979],
[ 0. , -0. , -0. ],
[-0. , 0. , 0.004979]]},
'["mp2/cc-pvdz", [2], [1, 2]]': {
'energy': -128.681444,
'gradient': [[ 0. , 0. , -0.005224],
[ 0. , -0. , 0.005224]]},
'["mp2/cc-pvdz", [2], [2, 3]]': {
'energy': -128.681444,
'gradient': [[ 0. , 0. , -0.005224],
[ 0. , -0. , 0.005224]]},
'["mp2/cc-pvdz", [3], [1, 2, 3]]': {
'energy': -128.682013,
'gradient': [[ 0. , 0. , -0.000607],
[-0. , -0. , -0.005266],
[ 0. , 0. , 0.005872]]},
'["mp2/cc-pvdz", [3], [1, 3]]': {
'energy': -128.677532,
'gradient': [[ 0. , -0. , -0.000551],
[ 0. , 0. , 0.000551]]},
'["mp2/cc-pvdz", [3], [2, 3]]': {
'energy': -128.681444,
'gradient': [[ 0. , -0. , -0.005224],
[ 0. , 0. , 0.005224]]}
}
final_results = mbc.analyze(calculation_results)
print(final_results["ret_energy"])
#> -384.1286
# Checking & Printing
assert compare_values([[0., 0., 2.832], [0., 0., 0.], [0., 0., -2.832]], final_results["ret_gradient"], atol=1.e-3, label="cp 3b tot G")
assert compare_values(-384.1286, final_results["ret_energy"], atol=1.e-4, label="cp 3b tot E")
pprint.pprint(final_results, width=200)
# Uncomment below to force fail and see `ret` printing
# assert 0
@uusing("psi4")
@uusing("nwchem")
@uusing("qcengine")
def test_highlevel_interface_example():
# Snippet 1
from qcelemental.models import Molecule
mol = Molecule(symbols=["ne", "ne", "ne"],
geometry=[[0,0,0],[0,0,2],[0,0,4]],
fragments=[[0], [1], [2]])
# Snippet 4
from qcmanybody import ManyBodyComputer
from qcmanybody.models import ManyBodyInput
manybodyinput = ManyBodyInput(**{
"molecule": mol,
"specification": {
"driver": "gradient",
"keywords": {
"bsse_type": ["cp", "vmfc"],
"levels": {1: "nwc-ccsd/tz", 3: "p4-mp2/dz"},
},
"specification": {
"nwc-ccsd/tz": {
"program": "nwchem",
"model": {"method": "ccsd", "basis": "cc-pvtz"},
"driver": "energy",
"keywords": {"ccsd__thresh": 1.e-7}},
"p4-mp2/dz": {
"program": "psi4",
"model": {"method": "mp2", "basis": "cc-pvdz"},
"driver": "energy"},
}}})
ret = ManyBodyComputer.from_manybodyinput(manybodyinput)
print(ret.return_result)
#> [[0., 0., 2.832], [0., 0., 0.], [0., 0., -2.832]]
print(ret.properties.cp_corrected_total_energy_through_3_body)
#> -384.1286
# Checking & Printing
assert compare_values([[0., 0., 2.832], [0., 0., 0.], [0., 0., -2.832]], ret.return_result, atol=1.e-3, label="cp 3b tot G")
assert compare_values(-384.1286, ret.properties.cp_corrected_total_energy_through_3_body, atol=1.e-4, label="cp 3b tot E")
pprint.pprint(ret.dict(), width=200)
# Uncomment below to force fail and see `ret` printing
# assert 0
if __name__ == "__main__":
test_core_interface_example_1_show_running()
test_core_interface_example_2_show_processing()
test_highlevel_interface_example()