| 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 wfx writer.""" |
| 9 |
|
| 10 |
import os |
| 11 |
import unittest |
| 12 |
import numpy as np |
| 13 |
|
| 14 |
import cclib |
| 15 |
|
| 16 |
from cclib.io.filewriter import MissingAttributeError |
| 17 |
from cclib.io.wfxwriter import _section |
| 18 |
from cclib.io.wfxwriter import _list_format |
| 19 |
|
| 20 |
__filedir__ = os.path.dirname(__file__) |
| 21 |
__filepath__ = os.path.realpath(__filedir__) |
| 22 |
__datadir__ = os.path.join(__filepath__, "..", "..") |
| 23 |
|
| 24 |
|
| 25 |
class WFXTest(unittest.TestCase): |
| 26 |
|
| 27 |
def test_missing_attribute_error(self): |
| 28 |
"""Check if MissingAttributeError is raised as expected.""" |
| 29 |
fpath = os.path.join(__datadir__, |
| 30 |
"data/GAMESS/basicGAMESS-US2017/C_bigbasis.out") |
| 31 |
required_attrs = ('atomcoords', 'atomnos', 'gbasis', 'charge', |
| 32 |
'homos', 'mult', 'mocoeffs') |
| 33 |
for attr in required_attrs: |
| 34 |
data = cclib.io.ccread(fpath) |
| 35 |
delattr(data, attr) |
| 36 |
|
| 37 |
# WFX files cannot be written if required attrs are missing. |
| 38 |
with self.assertRaises(MissingAttributeError): |
| 39 |
cclib.io.wfxwriter.WFXWriter(data) |
| 40 |
|
| 41 |
def test_no_of_prims(self): |
| 42 |
"""Check if number of primitives are calculated correctly.""" |
| 43 |
num_orb = {'s':1, 'p':3, 'd':6, 'f':10, 'g':15, 'h':21} |
| 44 |
gamessdir = os.path.join(__datadir__, |
| 45 |
"data/GAMESS/basicGAMESS-US2017") |
| 46 |
filenames = ["C_bigbasis.out", "dvb_un_sp.out"] |
| 47 |
filepaths = [os.path.join(gamessdir, fn) for fn in filenames] |
| 48 |
|
| 49 |
for fpath in filepaths: |
| 50 |
data = cclib.io.ccread(fpath) |
| 51 |
wfx = cclib.io.wfxwriter.WFXWriter(data) |
| 52 |
|
| 53 |
no_prims_writer = wfx._no_of_prims() |
| 54 |
no_prims_ccdata = 0 |
| 55 |
for atom in data.gbasis: |
| 56 |
for prims in atom: |
| 57 |
no_prims_ccdata += num_orb[prims[0].lower()]\ |
| 58 |
* len(prims[1]) |
| 59 |
|
| 60 |
self.assertEqual(no_prims_writer, no_prims_ccdata) |
| 61 |
|
| 62 |
def test_mo_normalization(self): |
| 63 |
"""Check if MO section is printed correctly.""" |
| 64 |
fpath = os.path.join(__datadir__, |
| 65 |
"data/GAMESS/basicGAMESS-US2017/C_bigbasis.out") |
| 66 |
data = cclib.io.ccread(fpath) |
| 67 |
wfx = cclib.io.wfxwriter.WFXWriter(data) |
| 68 |
|
| 69 |
normalized_mocoeffs = wfx._normalized_mocoeffs() |
| 70 |
if len(data.homos) > 1: |
| 71 |
self.assertEqual(len(normalized_mocoeffs), wfx._no_electrons()) |
| 72 |
else: |
| 73 |
self.assertEqual(len(normalized_mocoeffs), wfx._no_of_mos()) |
| 74 |
self.assertEqual(len(normalized_mocoeffs[0]), wfx._no_of_prims()) |
| 75 |
|
| 76 |
def test_mo_normalization_dat(self): |
| 77 |
"""Check if MOs are normalized as expected.""" |
| 78 |
fpath = os.path.join(__datadir__, |
| 79 |
"data/GAMESS/basicGAMESS-US2017/dvb_sp.out") |
| 80 |
data = cclib.io.ccread(fpath) |
| 81 |
wfx = cclib.io.wfxwriter.WFXWriter(data) |
| 82 |
|
| 83 |
normalized_mocoeffs_wfx = wfx._normalized_mocoeffs() |
| 84 |
datfile = os.path.join(__datadir__, |
| 85 |
"data/GAMESS/basicGAMESS-US2017/dvb_sp.dat") |
| 86 |
|
| 87 |
with open(datfile) as file: |
| 88 |
content = iter(file.readlines()) |
| 89 |
|
| 90 |
normalized_mocoeffs_dat = [] |
| 91 |
|
| 92 |
line = next(content) |
| 93 |
while 'TOP OF INPUT FILE FOR BADER' not in line: |
| 94 |
line = next(content) |
| 95 |
|
| 96 |
while 'END OF INPUT FILE FOR BADER' not in line: |
| 97 |
if 'MO' in line and 'OCC NO' in line and 'ORB. ENERGY' in line: |
| 98 |
line = next(content) |
| 99 |
mo = [] |
| 100 |
while 'MO' not in line and 'END DATA' not in line: |
| 101 |
mo.extend([float(x) for x in line.rsplit()]) |
| 102 |
line = next(content) |
| 103 |
normalized_mocoeffs_dat.append(mo) |
| 104 |
else: |
| 105 |
line = next(content) |
| 106 |
|
| 107 |
for mo1, mo2 in zip(normalized_mocoeffs_dat, normalized_mocoeffs_wfx): |
| 108 |
for coeff1, coeff2 in zip(mo1, mo2): |
| 109 |
self.assertTrue(-0.09 <= abs(coeff1) - abs(coeff2) <= 0.09) |
| 110 |
|
| 111 |
|
| 112 |
def test_programs(self): |
| 113 |
"""Check other programs against reference data.""" |
| 114 |
ref_file = "data/GAMESS/basicGAMESS-US2017/dvb_sp.out" |
| 115 |
# Skipping ORCA test until https://github.com/cclib/cclib/pull/394 is |
| 116 |
# merged. |
| 117 |
programs = { |
| 118 |
#'ORCA': "data/ORCA/basicORCA4.0/dvb_sp.out", |
| 119 |
'NWChem': "data/NWChem/basicNWChem6.5/dvb_sp_hf.out", |
| 120 |
'Psi4': "data/Psi4/basicPsi4-1.0/dvb_sp_rhf.out", |
| 121 |
'GAMESS-UK': "data/GAMESS-UK/basicGAMESS-UK8.0/dvb_sp_hf.out", |
| 122 |
'Firefly': "data/GAMESS/basicFirefly8.0/dvb_sp.out", |
| 123 |
} |
| 124 |
fpath_ref = os.path.join(__datadir__, ref_file) |
| 125 |
data_ref = cclib.io.ccread(fpath_ref) |
| 126 |
wfx_ref = cclib.io.wfxwriter.WFXWriter(data_ref) |
| 127 |
norm_mat_ref = wfx_ref._norm_mat()[0] |
| 128 |
mos_ref = wfx_ref._no_of_mos() |
| 129 |
|
| 130 |
for name in programs: |
| 131 |
fpath_prog = os.path.join(__datadir__, programs[name]) |
| 132 |
data_prog = cclib.io.ccread(fpath_prog) |
| 133 |
wfx_prog = cclib.io.wfxwriter.WFXWriter(data_prog) |
| 134 |
|
| 135 |
norm_mat_prog = wfx_prog._norm_mat()[0] |
| 136 |
mos_prog = wfx_prog._no_of_mos() |
| 137 |
|
| 138 |
# Check if normalization matrix are matching. |
| 139 |
self.assertAlmostEqual(max(np.array(norm_mat_prog) - |
| 140 |
np.array(norm_mat_ref)), 0, places=5) |
| 141 |
# Check if number of orbitals to be printed are as expected. |
| 142 |
self.assertEqual(mos_prog, mos_ref) |
| 143 |
|
| 144 |
def test_section_printing(self): |
| 145 |
"""Check if wfx section printing works as expected.""" |
| 146 |
float_section = _section('Test Section', 123.456) |
| 147 |
expected = ['<Test Section>', ' 123.456', '</Test Section>'] |
| 148 |
self.assertEqual(float_section, expected) |
| 149 |
|
| 150 |
list_section = _section('Test Section', ['1', '2']) |
| 151 |
expected = ['<Test Section>', '1', '2', '</Test Section>'] |
| 152 |
self.assertEqual(list_section, expected) |
| 153 |
|
| 154 |
str_section = _section('Test Section', 'Test Data') |
| 155 |
expected = ['<Test Section>', ' Test Data', '</Test Section>'] |
| 156 |
self.assertEqual(str_section, expected) |
| 157 |
|
| 158 |
def test_list_format(self): |
| 159 |
"""Check if list formatting works as expected.""" |
| 160 |
odd_list = _list_format([1, 2, 3], 2, '%8.1E') |
| 161 |
odd_expected = [' 1.0E+00 2.0E+00', ' 3.0E+00'] |
| 162 |
self.assertEqual(odd_list, odd_expected) |
| 163 |
even_list = _list_format([1, 2, 3, 4], 2, '%8.1E') |
| 164 |
even_expected = [' 1.0E+00 2.0E+00', ' 3.0E+00 4.0E+00'] |
| 165 |
self.assertEqual(even_list, even_expected) |
| 166 |
|
| 167 |
|
| 168 |
if __name__ == "__main__": |
| 169 |
unittest.main() |