| 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 |
"""Test the Nuclear method in cclib""" |
| 9 |
|
| 10 |
import sys |
| 11 |
import os |
| 12 |
import re |
| 13 |
import logging |
| 14 |
import unittest |
| 15 |
|
| 16 |
import numpy as np |
| 17 |
|
| 18 |
from cclib.method import Nuclear |
| 19 |
from cclib.parser import ccData |
| 20 |
from cclib.parser import DALTON |
| 21 |
from cclib.parser import Gaussian |
| 22 |
from cclib.parser import QChem |
| 23 |
from cclib.parser import utils |
| 24 |
|
| 25 |
sys.path.insert(1, "..") |
| 26 |
|
| 27 |
from ..test_data import getdatafile |
| 28 |
|
| 29 |
|
| 30 |
class NuclearTest(unittest.TestCase): |
| 31 |
|
| 32 |
def test_stoichiometry(self): |
| 33 |
"""Testing stoichoimetry generation.""" |
| 34 |
data = ccData() |
| 35 |
|
| 36 |
def check(atomnos, formula, charge=0): |
| 37 |
data.natom = len(atomnos) |
| 38 |
data.atomnos = np.array(atomnos) |
| 39 |
data.atomcoords = np.zeros((data.natom, 3)) |
| 40 |
data.charge = charge |
| 41 |
self.assertEqual(Nuclear(data).stoichiometry(), formula) |
| 42 |
|
| 43 |
# Basics and permutations. |
| 44 |
check([], "") |
| 45 |
check([6, 1, 6, 1, 1, 1], "C2H4") |
| 46 |
check([1, 1, 1, 6, 1, 6], "C2H4") |
| 47 |
|
| 48 |
# Charges. |
| 49 |
check([8], "O", charge=0) |
| 50 |
check([8], "O(+1)", charge=1) |
| 51 |
check([8], "O(-1)", charge=-1) |
| 52 |
check([8], "O(+2)", charge=2) |
| 53 |
check([8], "O(+9)", charge=9) |
| 54 |
|
| 55 |
# Element counts. |
| 56 |
check([6, 1], "CH") |
| 57 |
check([6] * 60, "C60") |
| 58 |
|
| 59 |
# Test the Hill system. |
| 60 |
check([8, 1, 1], "H2O") |
| 61 |
check([6, 8, 8, 1, 1], "CH2O2") |
| 62 |
check([16, 16, 8, 8], "O2S2") |
| 63 |
|
| 64 |
def test_repulsion_energy(self): |
| 65 |
"""Testing nuclear repulsion energy for one logfile where it is printed.""" |
| 66 |
|
| 67 |
data, logfile = getdatafile(QChem, "basicQChem4.2", ["water_mp4sdq.out"]) |
| 68 |
nuclear = Nuclear(data) |
| 69 |
nuclear.logger.setLevel(logging.ERROR) |
| 70 |
|
| 71 |
with open(logfile.filename) as f: |
| 72 |
output = f.read() |
| 73 |
line = re.search('Nuclear Repulsion Energy = .* hartrees', output).group() |
| 74 |
nre = float(line.split()[4]) |
| 75 |
nre = utils.convertor(nre, 'Angstrom', 'bohr') |
| 76 |
self.assertAlmostEqual(nuclear.repulsion_energy(), nre, places=7) |
| 77 |
|
| 78 |
@unittest.skipIf(sys.version_info < (3, 0), "The periodictable package doesn't work in Python2.") |
| 79 |
def test_principal_moments_of_inertia(self): |
| 80 |
"""Testing principal moments of inertia and the principal axes for one |
| 81 |
logfile where it is printed. |
| 82 |
""" |
| 83 |
|
| 84 |
data, logfile = getdatafile(DALTON, "basicDALTON-2015", ["dvb_sp_hf.out"]) |
| 85 |
nuclear = Nuclear(data) |
| 86 |
nuclear.logger.setLevel(logging.ERROR) |
| 87 |
|
| 88 |
ref_pmoi = [] |
| 89 |
ref_axes = [] |
| 90 |
with open(logfile.filename) as f: |
| 91 |
for line in f: |
| 92 |
if line.strip() == "Principal moments of inertia (u*A**2) and principal axes": |
| 93 |
next(f) |
| 94 |
next(f) |
| 95 |
for _ in range(3): |
| 96 |
tokens = [float(x) for x in next(f).split()[1:]] |
| 97 |
ref_pmoi.append(tokens[0]) |
| 98 |
ref_axes.append(tokens[1:]) |
| 99 |
pmoi, axes = nuclear.principal_moments_of_inertia() |
| 100 |
np.testing.assert_allclose(pmoi, ref_pmoi, rtol=0, atol=1.0e-4) |
| 101 |
# The phases of the eigenvectors may be different, but they |
| 102 |
# are still orthonormal within each set. |
| 103 |
np.testing.assert_allclose(np.abs(axes), np.abs(ref_axes), rtol=0, atol=1.0e-4) |
| 104 |
|
| 105 |
@unittest.skipIf(sys.version_info < (3, 0), "The periodictable package doesn't work in Python2.") |
| 106 |
def test_rotational_constants(self): |
| 107 |
"""Testing rotational constants for two logfiles where they are |
| 108 |
printed. |
| 109 |
""" |
| 110 |
|
| 111 |
data, logfile = getdatafile(DALTON, "basicDALTON-2015", ["dvb_sp_hf.out"]) |
| 112 |
nuclear = Nuclear(data) |
| 113 |
nuclear.logger.setLevel(logging.ERROR) |
| 114 |
|
| 115 |
with open(logfile.filename) as f: |
| 116 |
for line in f: |
| 117 |
if line.strip() == "Rotational constants": |
| 118 |
while line.split() != ['A', 'B', 'C']: |
| 119 |
line = next(f) |
| 120 |
line = next(f) |
| 121 |
ref_mhz = [float(x) for x in next(f).split()[:-1]] |
| 122 |
ref_invcm = [float(x) for x in next(f).split()[:-1]] |
| 123 |
rotconsts_ghz = nuclear.rotational_constants('ghz') |
| 124 |
rotconsts_invcm = nuclear.rotational_constants('invcm') |
| 125 |
np.testing.assert_allclose(rotconsts_ghz * 1.0e3, ref_mhz, rtol=0, atol=1.0e-4) |
| 126 |
np.testing.assert_allclose(rotconsts_invcm, ref_invcm, rtol=0, atol=1.0e-4) |
| 127 |
|
| 128 |
data, logfile = getdatafile(Gaussian, "basicGaussian16", ["dvb_sp.out"]) |
| 129 |
nuclear = Nuclear(data) |
| 130 |
nuclear.logger.setLevel(logging.ERROR) |
| 131 |
|
| 132 |
with open(logfile.filename) as f: |
| 133 |
for line in f: |
| 134 |
if "Rotational constants (GHZ):" in line: |
| 135 |
ref_ghz = [float(x) for x in line.split()[3:]] |
| 136 |
rotconsts_ghz = nuclear.rotational_constants('ghz') |
| 137 |
np.testing.assert_allclose(rotconsts_ghz, ref_ghz, rtol=0, atol=1.0e-5) |
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
unittest.TextTestRunner(verbosity=2).run(unittest.makeSuite(NuclearTest)) |