Debian Codegraph @

test/io/testmoldenwriter.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017, 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 """Unit tests for Molden writer."""
9
10 import os
11 import unittest
12
13 import cclib
14 from cclib.io.filewriter import MissingAttributeError
15 from cclib.io.moldenwriter import MoldenReformatter
16 from cclib.io.moldenwriter import round_molden
17
18 __filedir__ = os.path.dirname(__file__)
19 __filepath__ = os.path.realpath(__filedir__)
20 __datadir__ = os.path.join(__filepath__, "..", "..")
21 __testdir__ = __filedir__
22
23 class MOLDENTest(unittest.TestCase):
24
25 def test_missing_attribute_error(self):
26 """Check if MissingAttributeError is raised as expected."""
27 fpath = os.path.join(__datadir__,
28 "data/GAMESS/basicGAMESS-US2018/dvb_un_sp.out")
29 required_attrs = ['atomcoords', 'atomnos', 'natom']
30 for attr in required_attrs:
31 data = cclib.io.ccread(fpath)
32 delattr(data, attr)
33
34 # Molden files cannot be written if required attrs are missing.
35 with self.assertRaises(MissingAttributeError):
36 cclib.io.moldenwriter.MOLDEN(data)
37
38 def test_atoms_section_size(self):
39 """Check if size of Atoms section is equal to expected."""
40 fpath = os.path.join(__datadir__,
41 "data/GAMESS/basicGAMESS-US2018/dvb_un_sp.out")
42 data = cclib.io.ccread(fpath)
43 writer = cclib.io.moldenwriter.MOLDEN(data)
44 # Check size of Atoms section.
45 self.assertEqual(len(writer._coords_from_ccdata(-1)), data.natom)
46
47 def test_atoms_section_size_with_ghost(self):
48 """Check if size of Atoms section is equal to expected with a ghost atom present."""
49 fpath = os.path.join(__datadir__,
50 "data/GAMESS/basicGAMESS-US2018/dvb_un_sp_ghost.out")
51 data = cclib.io.ccread(fpath)
52 writer = cclib.io.moldenwriter.MOLDEN(data)
53 writer.ghost = "GH"
54 # Check for the ghost atom, and make sure it has the right label.
55 self.assertIn("GH", writer._coords_from_ccdata(-1)[0])
56 # Check size of Atoms section.
57 self.assertEqual(len(writer._coords_from_ccdata(-1)), data.natom)
58
59 def test_gto_section_size(self):
60 """Check if size of GTO section is equal to expected."""
61 fpath = os.path.join(__datadir__,
62 "data/GAMESS/basicGAMESS-US2018/dvb_un_sp.out")
63 data = cclib.io.ccread(fpath)
64 writer = cclib.io.moldenwriter.MOLDEN(data)
65 # Check size of GTO section.
66 size_gto_ccdata = 0
67 for atom in data.gbasis:
68 size_gto_ccdata += 1
69 for prims in atom:
70 size_gto_ccdata += len(prims[1]) + 1
71 # Filter blank lines.
72 size_gto_writer = len(list(filter(None, writer._gto_from_ccdata())))
73 self.assertEqual(size_gto_writer, size_gto_ccdata)
74
75 def test_mo_section_size(self):
76 """Check if size of MO section is equal to expected."""
77 fpath = os.path.join(__datadir__,
78 "data/GAMESS/basicGAMESS-US2018/dvb_un_sp.out")
79 data = cclib.io.ccread(fpath)
80 writer = cclib.io.moldenwriter.MOLDEN(data)
81 # Check size of MO section.
82 size_mo_ccdata = 0
83 extra = 4 if hasattr(data, 'mosyms') else 3
84 for i in range(data.mult):
85 size_mo_ccdata += len(data.moenergies[i]) *\
86 (len(data.mocoeffs[i][0]) + extra)
87 # Filter blank lines.
88 size_mo_writer = len(list(filter(None, writer._mo_from_ccdata())))
89 self.assertEqual(size_mo_writer, size_mo_ccdata)
90
91 def test_round_molden(self):
92 """Check if Molden Style number rounding works as expected."""
93 # If the 6th digit after dot is greater than 5, but is not 7,
94 # round the number upto 6th place.
95 # Else truncate at 6th digit after dot.
96 self.assertEqual(round_molden(1), 1)
97 self.assertEqual(round_molden(-1), -1)
98 self.assertEqual(round_molden(0.999995789), 0.999995)
99 self.assertEqual(round_molden(-0.999995789), -0.999995)
100 self.assertEqual(round_molden(0.999996789), 0.999997)
101 self.assertEqual(round_molden(-0.999997789), -0.999997)
102 self.assertEqual(round_molden(0.999997789), 0.999997)
103 self.assertEqual(round_molden(-0.999998789), -0.999999)
104 self.assertEqual(round_molden(-0.999999999), -1.0)
105
106 def test_molden_cclib_diff(self):
107 """Check if file written by cclib matched file written by Molden."""
108 filenames = ['dvb_un_sp', 'C_bigbasis', 'water_mp2']
109 for fn in filenames:
110 fpath = os.path.join(__datadir__,
111 "data/GAMESS/basicGAMESS-US2018/"+fn+".out")
112 data = cclib.io.ccread(fpath)
113 cclib_out = cclib.io.moldenwriter.MOLDEN(data).generate_repr()
114 # Reformat cclib's output to remove extra spaces.
115 cclib_out_formatted = MoldenReformatter(cclib_out).reformat()
116 fpath = os.path.join(__testdir__, "data/molden5.7_"+fn+".molden")
117 with open(fpath) as handle:
118 molden_out = handle.read()
119 # Reformat Molden's output to remove extra spaces,
120 # and fix number formatting.
121 molden_out_formatted = MoldenReformatter(molden_out).reformat()
122 # Assert if reformatted files from both writers are same.
123 self.assertMultiLineEqual(molden_out_formatted,
124 cclib_out_formatted)
125
126
127 if __name__ == "__main__":
128 unittest.main()