| 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 Moments method in cclib""" |
| 9 |
|
| 10 |
import unittest |
| 11 |
|
| 12 |
import numpy as np |
| 13 |
from numpy.testing import assert_equal, assert_almost_equal |
| 14 |
|
| 15 |
from cclib.method import Moments |
| 16 |
|
| 17 |
from six import add_move, MovedModule |
| 18 |
add_move(MovedModule('mock', 'mock', 'unittest.mock')) |
| 19 |
from six.moves import mock |
| 20 |
|
| 21 |
|
| 22 |
class TestIdealizedInputs(unittest.TestCase): |
| 23 |
linear_dipole_attrs = { |
| 24 |
'atomcoords': np.array([[[-1, 0, 0], [ 1, 0, 0]]]), |
| 25 |
'atomcharges': {'mulliken': [-1, 1]}, |
| 26 |
'atomnos': [1, 1] |
| 27 |
} |
| 28 |
|
| 29 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 30 |
def test_dipole_moment(self, mock): |
| 31 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 32 |
|
| 33 |
x = Moments(mock).calculate()[1] |
| 34 |
assert_almost_equal(x / 4.80320425, [2, 0, 0]) |
| 35 |
|
| 36 |
@unittest.skip("This does not pass for some reason.") |
| 37 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 38 |
def test_nonzero_quadrupole_moment(self, mock): |
| 39 |
mock.atomcoords = np.array([[ |
| 40 |
[-1, 0, 0], |
| 41 |
[0, 0, 0], |
| 42 |
[1, 0, 0]]]) |
| 43 |
# The periods are for Python2. |
| 44 |
mock.atomcharges = {'mulliken': [1/2., -1., 1/2.]} |
| 45 |
mock.atomnos = np.ones(mock.atomcoords.shape[1]) |
| 46 |
|
| 47 |
x = Moments(mock).calculate() |
| 48 |
self.assertEqual(np.count_nonzero(x[1]), 0) |
| 49 |
assert_almost_equal(x[2] / 4.80320423, [1, 0, 0, -0.5, 0, -0.5]) |
| 50 |
|
| 51 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 52 |
def test_zero_moments(self, mock): |
| 53 |
mock.atomcoords = np.array([[ |
| 54 |
[-2, 0, 0], |
| 55 |
[-1, 0, 0], |
| 56 |
[0, 0, 0], |
| 57 |
[1, 0, 0], |
| 58 |
[2, 0, 0]]]) |
| 59 |
# The periods are for Python2. |
| 60 |
mock.atomcharges = {'mulliken': [-1/8., 1/2., -3/4., 1/2., -1/8.]} |
| 61 |
mock.atomnos = np.ones(mock.atomcoords.shape[1]) |
| 62 |
|
| 63 |
x = Moments(mock).calculate() |
| 64 |
self.assertEqual(np.count_nonzero(x[1]), 0) |
| 65 |
self.assertEqual(np.count_nonzero(x[2]), 0) |
| 66 |
|
| 67 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 68 |
def test_invariant_to_origin_dislacement(self, mock): |
| 69 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 70 |
|
| 71 |
x = Moments(mock).calculate(origin=[0, 0, 0])[1] |
| 72 |
y = Moments(mock).calculate(origin=[1, 1, 1])[1] |
| 73 |
assert_equal(x, y) |
| 74 |
|
| 75 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 76 |
def test_variant_to_origin_dislacement(self, mock): |
| 77 |
attrs = dict(self.linear_dipole_attrs, **{ |
| 78 |
'atomcharges': {'mulliken': [-1, 2]} |
| 79 |
}) |
| 80 |
mock.configure_mock(**attrs) |
| 81 |
|
| 82 |
x = Moments(mock).calculate(origin=[0, 0, 0])[1] |
| 83 |
y = Moments(mock).calculate(origin=[1, 1, 1])[1] |
| 84 |
self.assertFalse(np.array_equal(x, y)) |
| 85 |
|
| 86 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 87 |
def test_origin_at_center_of_nuclear_charge(self, mock): |
| 88 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 89 |
|
| 90 |
x = Moments(mock).calculate()[0] |
| 91 |
assert_equal(x, [0, 0, 0]) |
| 92 |
|
| 93 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 94 |
def test_origin_at_center_of_mass(self, mock): |
| 95 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 96 |
mock.atommasses = np.ones(mock.atomcoords.shape[1]) |
| 97 |
|
| 98 |
x = Moments(mock).calculate(origin='mass')[0] |
| 99 |
assert_equal(x, [0, 0, 0]) |
| 100 |
|
| 101 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 102 |
def test_user_provided_origin(self, mock): |
| 103 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 104 |
|
| 105 |
x = Moments(mock).calculate(origin=[1, 1, 1]) |
| 106 |
assert_almost_equal(x[0], [1, 1, 1]) |
| 107 |
|
| 108 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 109 |
def test_user_provided_masses(self, mock): |
| 110 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 111 |
|
| 112 |
x = Moments(mock).calculate(masses=[1, 3], origin='mass') |
| 113 |
assert_almost_equal(x[0], [0.5, 0, 0]) |
| 114 |
|
| 115 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 116 |
def test_not_providing_masses(self, mock): |
| 117 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 118 |
# Replace with the refex version when Python2 is dropped. |
| 119 |
# with self.assertRaisesRegex(ValueError, 'masses'): |
| 120 |
with self.assertRaises(ValueError): |
| 121 |
Moments(mock).calculate(origin='mass') |
| 122 |
|
| 123 |
@mock.patch('cclib.parser.ccData', spec=True) |
| 124 |
def test_results_storing(self, mock): |
| 125 |
mock.configure_mock(**self.linear_dipole_attrs) |
| 126 |
mock.atomcharges.update({'lowdin': [-0.5, 0.5]}) |
| 127 |
|
| 128 |
m = Moments(mock) |
| 129 |
m.calculate(population='mulliken') |
| 130 |
m.calculate(population='lowdin') |
| 131 |
|
| 132 |
a, b = m.results['mulliken'][1], m.results['lowdin'][1] |
| 133 |
self.assertFalse(np.array_equal(a, b)) |
| 134 |
|
| 135 |
|
| 136 |
if __name__ == '__main__': |
| 137 |
suite = unittest.makeSuite(TestIdealizedInputs) |
| 138 |
unittest.TextTestRunner(verbosity=2).run(suite) |