| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2018, 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 various population analyses (MPA, LPA, CSPA) in cclib""" |
| 9 |
|
| 10 |
from __future__ import print_function |
| 11 |
|
| 12 |
import sys |
| 13 |
import os |
| 14 |
import logging |
| 15 |
import unittest |
| 16 |
|
| 17 |
import numpy |
| 18 |
|
| 19 |
from cclib.method import CSPA, LPA, MPA, OPA |
| 20 |
from cclib.method.calculationmethod import MissingAttributeError |
| 21 |
from cclib.parser import Gaussian |
| 22 |
|
| 23 |
sys.path.insert(1, "..") |
| 24 |
|
| 25 |
from ..test_data import getdatafile |
| 26 |
|
| 27 |
|
| 28 |
class PopulationTest(unittest.TestCase): |
| 29 |
"""Generic population method tests.""" |
| 30 |
|
| 31 |
methods = (CSPA, LPA, MPA, OPA) |
| 32 |
|
| 33 |
def parse(self): |
| 34 |
self.data, self.logfile = getdatafile(Gaussian, "basicGaussian09", ["dvb_un_sp.log"]) |
| 35 |
|
| 36 |
def calculate(self, method_class): |
| 37 |
if not hasattr(self, 'data'): |
| 38 |
self.parse() |
| 39 |
self.analysis = method_class(self.data) |
| 40 |
self.analysis.logger.setLevel(0) |
| 41 |
self.analysis.calculate() |
| 42 |
|
| 43 |
def testmissingrequiredattributes(self): |
| 44 |
"""Is an error raised when required attributes are missing?""" |
| 45 |
for missing_attribute in MPA.required_attrs: |
| 46 |
self.parse() |
| 47 |
delattr(self.data, missing_attribute) |
| 48 |
for method_class in self.methods: |
| 49 |
with self.assertRaises(MissingAttributeError): |
| 50 |
self.calculate(method_class) |
| 51 |
|
| 52 |
def testmissingoverlaps(self): |
| 53 |
"""Is an error raised when no overlaps are available?""" |
| 54 |
self.parse() |
| 55 |
for overlap_attribute in MPA.overlap_attributes: |
| 56 |
if hasattr(self.data, overlap_attribute): |
| 57 |
delattr(self.data, overlap_attribute) |
| 58 |
for method_class in self.methods: |
| 59 |
if method_class.overlap_attributes: |
| 60 |
with self.assertRaises(MissingAttributeError): |
| 61 |
self.calculate(method_class) |
| 62 |
|
| 63 |
|
| 64 |
class GaussianMPATest(unittest.TestCase): |
| 65 |
"""Mulliken Population Analysis test""" |
| 66 |
|
| 67 |
def setUp(self): |
| 68 |
self.data, self.logfile = getdatafile(Gaussian, "basicGaussian09", ["dvb_un_sp.log"]) |
| 69 |
self.analysis = MPA(self.data) |
| 70 |
self.analysis.logger.setLevel(0) |
| 71 |
self.analysis.calculate() |
| 72 |
|
| 73 |
def testsumcharges(self): |
| 74 |
"""Do the Mulliken charges sum up to the total formal charge?""" |
| 75 |
formalcharge = sum(self.data.atomnos) - self.data.charge |
| 76 |
totalpopulation = sum(self.analysis.fragcharges) |
| 77 |
self.assertAlmostEqual(totalpopulation, formalcharge, delta=1.0e-3) |
| 78 |
|
| 79 |
def testsumspins(self): |
| 80 |
"""Do the Mulliken spins sum up to the total formal spin?""" |
| 81 |
formalspin = self.data.homos[0] - self.data.homos[1] |
| 82 |
totalspin = sum(self.analysis.fragspins) |
| 83 |
self.assertAlmostEqual(totalspin, formalspin, delta=1.0e-3) |
| 84 |
|
| 85 |
|
| 86 |
class GaussianLPATest(unittest.TestCase): |
| 87 |
"""Lowdin Population Analysis test""" |
| 88 |
|
| 89 |
def setUp(self): |
| 90 |
self.data, self.logfile = getdatafile(Gaussian, "basicGaussian09", ["dvb_un_sp.log"]) |
| 91 |
self.analysis = LPA(self.data) |
| 92 |
self.analysis.logger.setLevel(0) |
| 93 |
self.analysis.calculate() |
| 94 |
|
| 95 |
def testsumcharges(self): |
| 96 |
"""Do the Lowdin charges sum up to the total formal charge?""" |
| 97 |
formalcharge = sum(self.data.atomnos) - self.data.charge |
| 98 |
totalpopulation = sum(self.analysis.fragcharges) |
| 99 |
self.assertAlmostEqual(totalpopulation, formalcharge, delta=0.001) |
| 100 |
|
| 101 |
def testsumspins(self): |
| 102 |
"""Do the Lowdin spins sum up to the total formal spin?""" |
| 103 |
formalspin = self.data.homos[0] - self.data.homos[1] |
| 104 |
totalspin = sum(self.analysis.fragspins) |
| 105 |
self.assertAlmostEqual(totalspin, formalspin, delta=1.0e-3) |
| 106 |
|
| 107 |
|
| 108 |
class GaussianCSPATest(unittest.TestCase): |
| 109 |
"""C-squared Population Analysis test""" |
| 110 |
|
| 111 |
def setUp(self): |
| 112 |
self.data, self.logfile = getdatafile(Gaussian, "basicGaussian09", ["dvb_un_sp.log"]) |
| 113 |
self.analysis = CSPA(self.data) |
| 114 |
self.analysis.logger.setLevel(0) |
| 115 |
self.analysis.calculate() |
| 116 |
|
| 117 |
def testsumcharges(self): |
| 118 |
"""Do the CSPA charges sum up to the total formal charge?""" |
| 119 |
formalcharge = sum(self.data.atomnos) - self.data.charge |
| 120 |
totalpopulation = sum(self.analysis.fragcharges) |
| 121 |
self.assertAlmostEqual(totalpopulation, formalcharge, delta=1.0e-3) |
| 122 |
|
| 123 |
def testsumspins(self): |
| 124 |
"""Do the CSPA spins sum up to the total formal spin?""" |
| 125 |
formalspin = self.data.homos[0] - self.data.homos[1] |
| 126 |
totalspin = sum(self.analysis.fragspins) |
| 127 |
self.assertAlmostEqual(totalspin, formalspin, delta=1.0e-3) |
| 128 |
|
| 129 |
|
| 130 |
tests = [GaussianMPATest, GaussianLPATest, GaussianCSPATest] |
| 131 |
|
| 132 |
|
| 133 |
if __name__ == "__main__": |
| 134 |
for test in tests: |
| 135 |
thistest = unittest.makeSuite(test) |
| 136 |
unittest.TextTestRunner(verbosity=2).run(thistest) |