Debian Codegraph @

test/data/testSP.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2019, the cclib development team
4 #
5 # This file is part of cclib (http://cclib.github.io) and is distributed under
6 # the terms of the BSD 3-Clause License.
7
8 """Test single point logfiles in cclib."""
9
10 import os
11 import unittest
12
13 import numpy
14
15 from common import get_minimum_carbon_separation
16
17 from skip import skipForParser
18 from skip import skipForLogfile
19
20
21 __filedir__ = os.path.realpath(os.path.dirname(__file__))
22
23
24 class GenericSPTest(unittest.TestCase):
25 """Generic restricted single point unittest"""
26
27 # Molecular mass of DVB in mD, and expected precision.
28 molecularmass = 130078.25
29 mass_precision = 0.10
30
31 # In STO-3G, H has 1, C has 5 (1 S and 4 SP).
32 nbasisdict = {1:1, 6:5}
33
34 # Approximate B3LYP energy of dvb after SCF in STO-3G.
35 b3lyp_energy = -10365
36
37 # Overlap first two atomic orbitals.
38 overlap01 = 0.24
39
40 # Generally, one criteria for SCF energy convergence.
41 num_scf_criteria = 1
42
43 def testnatom(self):
44 """Is the number of atoms equal to 20?"""
45 self.assertEqual(self.data.natom, 20)
46
47 def testatomnos(self):
48 """Are the atomnos correct?"""
49
50 # The nuclear charges should be integer values in a NumPy array.
51 self.assertTrue(numpy.alltrue([numpy.issubdtype(atomno, numpy.signedinteger)
52 for atomno in self.data.atomnos]))
53 self.assertEqual(self.data.atomnos.dtype.char, 'i')
54
55 self.assertEqual(self.data.atomnos.shape, (20,) )
56 self.assertEqual(sum(self.data.atomnos == 6) + sum(self.data.atomnos == 1), 20)
57
58 @skipForParser('DALTON', 'DALTON has a very low accuracy for the printed values of all populations (2 decimals rounded in a weird way), so let it slide for now')
59 @skipForLogfile('Jaguar/basicJaguar7', 'We did not print the atomic partial charges in the unit tests for this version')
60 @skipForLogfile('Molpro/basicMolpro2006', "These tests were run a long time ago and since we don't have access to Molpro 2006 anymore, we can skip this test (it is tested in 2012)")
61 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
62 def testatomcharges(self):
63 """Are atomcharges (at least Mulliken) consistent with natom and sum to zero?"""
64 for type in set(['mulliken'] + list(self.data.atomcharges.keys())):
65 charges = self.data.atomcharges[type]
66 self.assertEqual(len(charges), self.data.natom)
67 self.assertAlmostEqual(sum(charges), 0.0, delta=0.001)
68
69 def testatomcoords(self):
70 """Are the dimensions of atomcoords 1 x natom x 3?"""
71 expected_shape = (1, self.data.natom, 3)
72 self.assertEqual(self.data.atomcoords.shape, expected_shape)
73
74 def testatomcoords_units(self):
75 """Are atomcoords consistent with Angstroms?"""
76 min_carbon_dist = get_minimum_carbon_separation(self.data)
77 dev = abs(min_carbon_dist - 1.34)
78 self.assertTrue(dev < 0.03, "Minimum carbon dist is %.2f (not 1.34)" % min_carbon_dist)
79
80 @skipForParser('Molcas','The parser is still being developed so we skip this test')
81 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
82 def testcharge_and_mult(self):
83 """Are the charge and multiplicity correct?"""
84 self.assertEqual(self.data.charge, 0)
85 self.assertEqual(self.data.mult, 1)
86
87 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
88 def testnbasis(self):
89 """Is the number of basis set functions correct?"""
90 count = sum([self.nbasisdict[n] for n in self.data.atomnos])
91 self.assertEqual(self.data.nbasis, count)
92
93 @skipForParser('ADF', 'ADF parser does not extract atombasis')
94 @skipForLogfile('Jaguar/basicJaguar7', 'Data file does not contain enough information. Can we make a new one?')
95 @skipForParser('Molcas','The parser is still being developed so we skip this test')
96 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
97 def testatombasis(self):
98 """Are the indices in atombasis the right amount and unique?"""
99 all = []
100 for i, atom in enumerate(self.data.atombasis):
101 self.assertEqual(len(atom), self.nbasisdict[self.data.atomnos[i]])
102 all += atom
103 # Test if there are as many indices as atomic orbitals.
104 self.assertEqual(len(all), self.data.nbasis)
105 # Check if all are different (every orbital indexed once).
106 self.assertEqual(len(set(all)), len(all))
107
108 @skipForParser('GAMESS', 'atommasses not implemented yet')
109 @skipForParser('GAMESSUK', 'atommasses not implemented yet')
110 @skipForParser('Jaguar', 'atommasses not implemented yet')
111 @skipForParser('Molcas','The parser is still being developed so we skip this test')
112 @skipForParser('Molpro', 'atommasses not implemented yet')
113 @skipForParser('NWChem', 'atommasses not implemented yet')
114 @skipForLogfile('Psi4/basicPsi4.0b5', 'atommasses not implemented yet')
115 @skipForParser('QChem', 'atommasses not implemented yet')
116 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
117 def testatommasses(self):
118 """Do the atom masses sum up to the molecular mass?"""
119 mm = 1000*sum(self.data.atommasses)
120 msg = "Molecule mass: %f not %f +- %fmD" % (mm, self.molecularmass, self.mass_precision)
121 self.assertAlmostEqual(mm, self.molecularmass, delta=self.mass_precision, msg=msg)
122
123 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
124 def testcoreelectrons(self):
125 """Are the coreelectrons all 0?"""
126 ans = numpy.zeros(self.data.natom, 'i')
127 numpy.testing.assert_array_equal(self.data.coreelectrons, ans)
128
129 @skipForParser('Molcas','The parser is still being developed so we skip this test')
130 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
131 def testnormalisesym(self):
132 """Did this subclass overwrite normalisesym?"""
133 # https://stackoverflow.com/a/8747890
134 self.logfile.normalisesym("A")
135
136 @skipForParser('Molcas','The parser is still being developed so we skip this test')
137 @skipForParser('Molpro', '?')
138 @skipForParser('ORCA', 'ORCA has no support for symmetry yet')
139 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
140 def testsymlabels(self):
141 """Are all the symmetry labels either Ag/u or Bg/u?"""
142 sumwronglabels = sum([x not in ['Ag', 'Bu', 'Au', 'Bg'] for x in self.data.mosyms[0]])
143 self.assertEqual(sumwronglabels, 0)
144
145 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
146 def testhomos(self):
147 """Is the index of the HOMO equal to 34?"""
148 numpy.testing.assert_array_equal(self.data.homos, numpy.array([34],"i"), "%s != array([34],'i')" % numpy.array_repr(self.data.homos))
149
150 def testscfvaluetype(self):
151 """Are scfvalues and its elements the right type??"""
152 self.assertEqual(type(self.data.scfvalues),type([]))
153 self.assertEqual(type(self.data.scfvalues[0]),type(numpy.array([])))
154
155 def testscfenergy(self):
156 """Is the SCF energy within the target?"""
157 self.assertAlmostEqual(self.data.scfenergies[-1], self.b3lyp_energy, delta=40, msg="Final scf energy: %f not %i +- 40eV" %(self.data.scfenergies[-1], self.b3lyp_energy))
158
159 def testscftargetdim(self):
160 """Do the scf targets have the right dimensions?"""
161 self.assertEqual(self.data.scftargets.shape, (len(self.data.scfvalues), len(self.data.scfvalues[0][0])))
162
163 def testscftargets(self):
164 """Are correct number of SCF convergence criteria being parsed?"""
165 self.assertEqual(len(self.data.scftargets[0]), self.num_scf_criteria)
166
167 def testlengthmoenergies(self):
168 """Is the number of evalues equal to nmo?"""
169 if hasattr(self.data, "moenergies"):
170 self.assertEqual(len(self.data.moenergies[0]), self.data.nmo)
171
172 def testtypemoenergies(self):
173 """Is moenergies a list containing one numpy array?"""
174 if hasattr(self.data, "moenergies"):
175 self.assertIsInstance(self.data.moenergies, list)
176 self.assertIsInstance(self.data.moenergies[0], numpy.ndarray)
177
178 @skipForParser('DALTON', 'mocoeffs not implemented yet')
179 @skipForLogfile('Jaguar/basicJaguar7', 'Data file does not contain enough information. Can we make a new one?')
180 def testdimmocoeffs(self):
181 """Are the dimensions of mocoeffs equal to 1 x nmo x nbasis?"""
182 if hasattr(self.data, "mocoeffs"):
183 self.assertIsInstance(self.data.mocoeffs, list)
184 self.assertEqual(len(self.data.mocoeffs), 1)
185 self.assertEqual(self.data.mocoeffs[0].shape,
186 (self.data.nmo, self.data.nbasis))
187
188 @skipForParser('DALTON', 'mocoeffs not implemented yet')
189 @skipForLogfile('Jaguar/basicJaguar7', 'Data file does not contain enough information. Can we make a new one?')
190 def testfornoormo(self):
191 """Do we have NOs or MOs?"""
192 self.assertTrue(
193 hasattr(self.data, "nocoeffs") or hasattr(self.data, "mocoeffs")
194 )
195
196 def testdimnoccnos(self):
197 """Is the length of nooccnos equal to nmo?"""
198 if hasattr(self.data, "nooccnos"):
199 self.assertIsInstance(self.data.nooccnos, numpy.ndarray)
200 self.assertEqual(len(self.data.nooccnos), self.data.nmo)
201
202 def testdimnocoeffs(self):
203 """Are the dimensions of nocoeffs equal to nmo x nmo?"""
204 if hasattr(self.data, "nocoeffs"):
205 self.assertIsInstance(self.data.nocoeffs, numpy.ndarray)
206 self.assertEqual(
207 self.data.nocoeffs.shape, (self.data.nmo, self.data.nmo)
208 )
209
210 @skipForParser('DALTON', 'To print: **INTEGRALS\n.PROPRI')
211 @skipForParser('Molcas','The parser is still being developed so we skip this test')
212 @skipForParser('Psi3', 'Psi3 does not currently have the option to print the overlap matrix')
213 @skipForParser('Psi4', 'Psi4 does not currently have the option to print the overlap matrix')
214 @skipForParser('QChem', 'QChem cannot print the overlap matrix')
215 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
216 def testaooverlaps(self):
217 """Are the dims and values of the overlap matrix correct?"""
218
219 self.assertEqual(self.data.aooverlaps.shape, (self.data.nbasis, self.data.nbasis))
220
221 # The matrix is symmetric.
222 row = self.data.aooverlaps[0,:]
223 col = self.data.aooverlaps[:,0]
224 self.assertEqual(sum(col - row), 0.0)
225
226 # All values on diagonal should be exactly zero.
227 for i in range(self.data.nbasis):
228 self.assertEqual(self.data.aooverlaps[i,i], 1.0)
229
230 # Check some additional values that don't seem to move around between programs.
231 self.assertAlmostEqual(self.data.aooverlaps[0, 1], self.overlap01, delta=0.01)
232 self.assertAlmostEqual(self.data.aooverlaps[1, 0], self.overlap01, delta=0.01)
233 self.assertEqual(self.data.aooverlaps[3,0], 0.0)
234 self.assertEqual(self.data.aooverlaps[0,3], 0.0)
235
236 @skipForParser('Molcas','The parser is still being developed so we skip this test')
237 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
238 def testoptdone(self):
239 """There should be no optdone attribute set."""
240 self.assertFalse(hasattr(self.data, 'optdone'))
241
242 @skipForParser('Gaussian', 'Logfile needs to be updated')
243 @skipForParser('Jaguar', 'No dipole moments in the logfile')
244 @skipForParser('Molcas','The parser is still being developed so we skip this test')
245 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
246 def testmoments(self):
247 """Does the dipole and possible higher molecular moments look reasonable?"""
248
249 # The reference point is always a vector, but not necessarily the
250 # origin or center of mass. In this case, however, the center of mass
251 # is at the origin, so we now what to expect.
252 reference = self.data.moments[0]
253 self.assertEqual(len(reference), 3)
254 for x in reference:
255 self.assertEqual(x, 0.0)
256
257 # Length and value of dipole moment should always be correct (zero for this test).
258 dipole = self.data.moments[1]
259 self.assertEqual(len(dipole), 3)
260 for d in dipole:
261 self.assertAlmostEqual(d, 0.0, places=7)
262
263 # If the quadrupole is there, we can expect roughly -50B for the XX moment,
264 # -50B for the YY moment and and -60B for the ZZ moment.
265 if len(self.data.moments) > 2:
266 quadrupole = self.data.moments[2]
267 self.assertEqual(len(quadrupole), 6)
268 self.assertAlmostEqual(quadrupole[0], -50, delta=2.5)
269 self.assertAlmostEqual(quadrupole[3], -50, delta=2.5)
270 self.assertAlmostEqual(quadrupole[5], -60, delta=3)
271
272 # If the octupole is there, it should have 10 components and be zero.
273 if len(self.data.moments) > 3:
274 octupole = self.data.moments[3]
275 self.assertEqual(len(octupole), 10)
276 for m in octupole:
277 self.assertAlmostEqual(m, 0.0, delta=0.001)
278
279 # The hexadecapole should have 15 elements, an XXXX component of around -1900 Debye*ang^2,
280 # a YYYY component of -330B and a ZZZZ component of -50B.
281 if len(self.data.moments) > 4:
282 hexadecapole = self.data.moments[4]
283 self.assertEqual(len(hexadecapole), 15)
284 self.assertAlmostEqual(hexadecapole[0], -1900, delta=90)
285 self.assertAlmostEqual(hexadecapole[10], -330, delta=11)
286 self.assertAlmostEqual(hexadecapole[14], -50, delta=2.5)
287
288 # The are 21 unique 32-pole moments, and all are zero in this test case.
289 if len(self.data.moments) > 5:
290 moment32 = self.data.moments[5]
291 self.assertEqual(len(moment32), 21)
292 for m in moment32:
293 self.assertEqual(m, 0.0)
294
295 @skipForParser('ADF', 'Does not support metadata yet')
296 @skipForParser('GAMESSUK', 'Does not support metadata yet')
297 @skipForParser('Molcas','The parser is still being developed so we skip this test')
298 @skipForParser('Molpro', 'Does not support metadata yet')
299 @skipForParser('NWChem', 'Does not support metadata yet')
300 @skipForParser('ORCA', 'Does not support metadata yet')
301 @skipForParser('Psi3', 'Does not support metadata yet')
302 @skipForParser('Psi4', 'Does not support metadata yet')
303 @skipForParser('QChem', 'Does not support metadata yet')
304 @skipForParser('Turbomole','The parser is still being developed so we skip this test')
305 def testmetadata(self):
306 """Does metadata have expected keys and values?"""
307 self.assertTrue(hasattr(self.data, "metadata"))
308 if self.logfile.logname not in ['ORCA', 'Psi']:
309 self.assertIn("basis_set", self.data.metadata)
310 if self.logfile.logname == 'ORCA':
311 self.assertIn("input_file_name", self.data.metadata)
312 self.assertIn("input_file_contents", self.data.metadata)
313 self.assertIn("methods", self.data.metadata)
314 self.assertIn("package", self.data.metadata)
315 self.assertIn("package_version", self.data.metadata)
316
317 class ADFSPTest(GenericSPTest):
318 """Customized restricted single point unittest"""
319
320 # ADF only prints up to 0.1mD per atom, so the precision here is worse than 0.1mD.
321 mass_precision = 0.3
322
323 foverlap00 = 1.00003
324 foverlap11 = 1.02672
325 foverlap22 = 1.03585
326 num_scf_criteria = 2
327 b3lyp_energy = -140
328
329 def testfoverlaps(self):
330 """Are the dims and values of the fragment orbital overlap matrix correct?"""
331
332 self.assertEqual(self.data.fooverlaps.shape, (self.data.nbasis, self.data.nbasis))
333
334 # The matrix is symmetric.
335 row = self.data.fooverlaps[0,:]
336 col = self.data.fooverlaps[:,0]
337 self.assertEqual(sum(col - row), 0.0)
338
339 # Although the diagonal elements are close to zero, the SFOs
340 # are generally not normalized, so test for a few specific values.
341 self.assertAlmostEqual(self.data.fooverlaps[0, 0], self.foverlap00, delta=0.0001)
342 self.assertAlmostEqual(self.data.fooverlaps[1, 1], self.foverlap11, delta=0.0001)
343 self.assertAlmostEqual(self.data.fooverlaps[2, 2], self.foverlap22, delta=0.0001)
344
345 class GaussianSPTest(GenericSPTest):
346 """Customized restricted single point unittest"""
347
348 num_scf_criteria = 3
349
350 class JaguarSPTest(GenericSPTest):
351 """Customized restricted single point unittest"""
352
353 num_scf_criteria = 2
354
355 class Jaguar7SPTest(JaguarSPTest):
356 """Customized restricted single point unittest"""
357
358 # Jaguar prints only 10 virtual MOs by default. Can we re-run with full output?
359 def testlengthmoenergies(self):
360 """Is the number of evalues equal to the number of occ. MOs + 10?"""
361 self.assertEqual(len(self.data.moenergies[0]), self.data.homos[0]+11)
362
363 class MolcasSPTest(GenericSPTest):
364 """Customized restricted single point unittest"""
365
366 num_scf_criteria = 4
367
368 class MolproSPTest(GenericSPTest):
369 """Customized restricted single point unittest"""
370
371 num_scf_criteria = 2
372
373 class NWChemKSSPTest(GenericSPTest):
374 """Customized restricted single point unittest"""
375
376 num_scf_criteria = 3
377
378 class PsiSPTest(GenericSPTest):
379 """Customized restricted single point HF/KS unittest"""
380
381 num_scf_criteria = 2
382
383 class OrcaSPTest(GenericSPTest):
384 """Customized restricted single point unittest"""
385
386 # Orca has different weights for the masses
387 molecularmass = 130190
388
389 num_scf_criteria = 3
390
391 class TurbomoleSPTest(GenericSPTest):
392 """Customized restricted single point unittest"""
393
394 num_scf_criteria = 2
395
396
397 if __name__=="__main__":
398
399 import sys
400 sys.path.insert(1, os.path.join(__filedir__, ".."))
401
402 from test_data import DataSuite
403 suite = DataSuite(['SP'])
404 suite.testall()