1
"""
2
test_get_psi_results.py
3
"""
4 0
import sys
5 0
import os
6 0
import pytest
7

8
# define location of input files for testing
9 0
mydir = os.path.dirname(os.path.abspath(__file__))
10

11
# import functions to aid testing
12 0
sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers'))
13 0
from helper import *
14

15 0
from quanformer.get_psi_results import *
16

17
# -----------------------
18

19 0
def test_initiate_dict():
20 0
    d = initiate_dict()
21 0
    assert d['package'] == 'Psi4'
22 0
    assert d['missing'] == False
23

24

25 0
def test_get_conf_data():
26 0
    props = get_conf_data({}, 'spe',
27
        os.path.join(mydir, 'data_tests', 'timer.dat'),
28
        os.path.join(mydir, 'data_tests', 'output_spe.dat'))
29 0
    assert props['time'] == 847.0
30 0
    assert props['basis'] == 'cc-pVTZ'
31 0
    assert props['method'] == 'mp2'
32 0
    assert props['finalEnergy'] == -463.3016772141739
33 0
    assert props['finalSCSEnergy'] == -465.19422272924896
34

35

36
# TODO
37
#def test_set_conf_data():
38
#    mol = read_mol(os.path.join(mydir, 'data_tests', 'methane_title-1.0.sdf'))
39
#    {'package': 'Psi4', 'missing': False, 'time': 847.0, 'basis': 'cc-pVTZ', 'method': 'mp2', 'finalEnergy': -463.3016772141739, 'finalSCSEnergy': -465.19422272924896}
40
#    set_conf_data(mol, props, 'spe')
41

42

43 0
def test_check_title():
44 0
    mol = read_mol(os.path.join(mydir, 'data_tests', 'methane_title-1.0.sdf'))
45 0
    mol = check_title(
46
        mol, os.path.join(mydir, 'data_tests', 'methane_title-1.0.sdf'))
47 0
    assert mol.GetTitle() == 'methane_title10'
48

49

50 0
def test_get_psi_time():
51 0
    time = get_psi_time(os.path.join(mydir, 'data_tests', 'timer.dat'))
52 0
    assert time == 847.00
53

54

55 0
def test_process_psi_out_spe():
56 0
    spe_dict = process_psi_out(
57
        os.path.join(mydir, 'data_tests', 'output_spe.dat'), {}, 'spe')
58
    # {'basis': 'def2-tzvp', 'method': 'b3lyp-d3mbj', 'finalEnergy': -466.37497616456733}
59
    # actual energy: -463.3016772141738784
60
    # scs final: -465.1942227292489633
61 0
    assert spe_dict['basis'] == 'cc-pVTZ'
62 0
    assert spe_dict['method'] == 'mp2'
63 0
    assert spe_dict['finalEnergy'] == pytest.approx(-463.3016772141738784,
64
                                                    0.000000000001)
65 0
    assert spe_dict['finalSCSEnergy'] == pytest.approx(-465.1942227292489633,
66
                                                       0.000000000001)
67

68

69 0
def test_process_psi_out_hess():
70 0
    hess_dict = process_psi_out(
71
        os.path.join(mydir, 'data_tests', 'output_hess.dat'), {}, 'hess')
72 0
    assert hess_dict['basis'] == 'def2-sv(p)'
73 0
    assert hess_dict['method'] == 'mp2'
74 0
    hess = hess_dict['hessian']
75 0
    assert hess.shape == (36, 36)
76 0
    assert hess[1, 1] == 0.77147127082666
77

78

79 0
def test_process_psi_out_opt():
80 0
    opt_dict = process_psi_out(
81
        os.path.join(mydir, 'data_tests', 'output_opt.dat'), {}, 'opt')
82
    # initial energy: -582.148922080397
83
    # final energy: -582.1568394053036
84
    # scs final: -582.0904465352028865
85 0
    assert opt_dict['basis'] == 'def2-SV(P)'
86 0
    assert opt_dict['method'] == 'mp2'
87 0
    assert opt_dict['numSteps'] == '8'
88 0
    assert opt_dict['initEnergy'] == pytest.approx(-582.148922080397,
89
                                                   0.000000000001)
90 0
    assert opt_dict['finalEnergy'] == pytest.approx(-582.1568394053036,
91
                                                    0.000000000001)
92 0
    assert opt_dict['finalSCSEnergy'] == pytest.approx(-582.0904465352028865,
93
                                                       0.000000000001)
94 0
    assert opt_dict['coords'][0] == -0.0238533448
95 0
    assert len(opt_dict['coords']) == 69
96

97

98 0
def test_process_psi_out_two():
99
    # TODO what happens if passed in psi4 output with opt-->hess, or opt-->spe
100 0
    pass
101

102

103 0
def test_getPsiOne():
104 0
    infile = os.path.join(mydir,  'data_tests', 'methane_c2p.sdf')
105 0
    outfile = os.path.join(mydir, 'data_tests', 'methane_c2p-210.sdf')
106 0
    psiout = os.path.join(mydir,  'data_tests', 'methane', '1', 'output.dat')
107 0
    timeout = os.path.join(mydir, 'data_tests', 'methane', '1', 'timer.dat')
108 0
    mol, props = getPsiOne(infile, outfile, 'opt', psiout, timeout)
109 0
    assert oechem.OEHasSDData(
110
        mol, "QM Psi4 Opt. Runtime (sec) mp2/def2-sv(p)") == True
111 0
    assert oechem.OEGetSDData(
112
        mol, "QM Psi4 Opt. Runtime (sec) mp2/def2-sv(p)") == '2.22'
113 0
    assert oechem.OEHasSDData(
114
        mol, "QM Psi4 Final Opt. Energy (Har) mp2/def2-sv(p)") == True
115 0
    assert oechem.OEGetSDData(
116
        mol, "QM Psi4 Final Opt. Energy (Har) mp2/def2-sv(p)"
117
    ) == '-40.2897657783697'
118 0
    assert oechem.OEHasSDData(
119
        mol, "QM Psi4 Initial Opt. Energy (Har) mp2/def2-sv(p)") == True
120 0
    assert oechem.OEGetSDData(
121
        mol, "QM Psi4 Initial Opt. Energy (Har) mp2/def2-sv(p)"
122
    ) == '-40.289556302848'
123 0
    assert oechem.OEHasSDData(mol, "QM Psi4 Opt. Steps mp2/def2-sv(p)") == True
124 0
    assert oechem.OEGetSDData(mol, "QM Psi4 Opt. Steps mp2/def2-sv(p)") == '3'
125 0
    os.remove(outfile)
126

127

128 0
def test_get_psi_results_exists():
129 0
    infile = os.path.join(mydir,'data_tests','methane_c2p.sdf')
130 0
    with pytest.raises(FileExistsError):
131 0
        m, b = get_psi_results(infile, infile, calctype='opt', psiout="output.dat", timeout="timer.dat")
132 0
    assert True
133

134 0
def test_get_psi_results_calctype():
135 0
    infile = os.path.join(mydir,'data_tests','methane_c2p.sdf')
136 0
    outfile = os.path.join(mydir,'data_tests','methane_c2p-210.sdf')
137 0
    with pytest.raises(ValueError):
138 0
        m, b = get_psi_results(infile, outfile, calctype='blah', psiout="output.dat", timeout="timer.dat")
139 0
    assert True
140

141

142 0
def test_get_psi_results_spe():
143
    # TODO
144 0
    pass
145

146

147 0
def test_get_psi_results_hess():
148 0
    infile = os.path.join(mydir, 'data_tests', 'carbon-222.sdf')
149 0
    outfile = os.path.join(mydir, 'data_tests', 'carbon_hess-222.sdf')
150 0
    m, b = get_psi_results(infile, outfile, 'hess', "output.dat", "timer.dat")
151
    # check the method and basis set returned
152 0
    assert m == 'mp2'
153 0
    assert b == 'def2-tzvp'
154

155
    # check tag of Hessian calculation time for one conformer
156 0
    mols = read_mol(outfile, True)
157 0
    conf = list(next(mols).GetConfs())[0]  # only conf of the s1 mol
158 0
    assert oechem.OEGetSDData(
159
        conf, 'QM Psi4 Hessian Runtime (sec) mp2/def2-tzvp') == '253.0'
160

161
    # check a few values of the Hessian matrix for each conformer
162 0
    hpickle = os.path.join(mydir, 'data_tests', 'carbon_hess-222.hess.pickle')
163 0
    hdict = pickle.load(open(hpickle, 'rb'))
164
    # mol s1, conf 1, row 10, column 23
165 0
    assert hdict['s1'][1][9][22] == 0.00065859359798
166
    # mol t1, conf 1, row 3, column 9
167 0
    assert hdict['t1'][1][2][8] == -0.42670095298438
168

169
    # clean up
170 0
    os.remove(outfile)
171 0
    os.remove(hpickle)
172

173

174 0
def test_get_psi_results_opt():
175 0
    infile = os.path.join(mydir, 'data_tests', 'gbi-200.sdf')
176 0
    outfile = os.path.join(mydir, 'data_tests', 'gbi-210.sdf')
177 0
    m, b = get_psi_results(infile, outfile, 'opt', "output.dat", "timer.dat")
178 0
    assert m == 'mp2'
179 0
    assert b == 'def2-SV(P)'
180

181
    # read the single mol (from generator) and get its three conformers
182
    # confs 2 and 4 failed opt so we should have data from confs 1 3 5
183 0
    mol = read_mol(outfile, True)
184 0
    confs = list(next(mol).GetConfs())
185 0
    assert len(confs) == 3
186

187
    # get one conf and check its tag data
188 0
    conf = confs[1]
189
    # handy way to get all tags and data (.GetTag(), .GetValue())
190
    #print([x.GetValue() for x in list(oechem.OEGetSDDataPairs(conf))])
191 0
    assert oechem.OEGetSDData(
192
        conf, 'QM Psi4 Opt. Runtime (sec) mp2/def2-SV(P)') == '2635.0'
193 0
    assert oechem.OEGetSDData(
194
        conf, 'QM Psi4 Final Opt. Energy (Har) mp2/def2-SV(P)'
195
    ) == '-582.1570265488717'
196 0
    assert oechem.OEGetSDData(conf,
197
                              'QM Psi4 Opt. Steps mp2/def2-SV(P)') == '21'
198 0
    assert oechem.OEGetSDData(
199
        conf, 'QM Psi4 Initial Opt. Energy (Har) mp2/def2-SV(P)'
200
    ) == '-582.146838702714'
201 0
    os.remove(outfile)
202

203

204
# test manually without pytest
205 0
if 0:
206
    #test_getPsiOne()
207
    #test_get_psi_results_none()
208
    test_get_psi_results_hess()

Read our documentation on viewing source code .

Loading