File: testmoments.py

package info (click to toggle)
cclib 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 55,300 kB
  • sloc: python: 23,276; makefile: 84; sh: 26
file content (136 lines) | stat: -rw-r--r-- 4,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

"""Test the Moments method in cclib"""

import unittest
from unittest import mock

import numpy as np
from numpy.testing import assert_equal, assert_almost_equal

from cclib.method import Moments
import pytest


class TestIdealizedInputs(unittest.TestCase):
    linear_dipole_attrs = {
        'atomcoords': np.array([[[-1, 0, 0], [ 1,  0, 0]]]),
        'atomcharges': {'mulliken': [-1, 1]},
        'atomnos': [1, 1]
    }
    
    @mock.patch('cclib.parser.ccData', spec=True)
    def test_dipole_moment(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)

        x = Moments(mock).calculate()[1]
        assert_almost_equal(x / 4.80320425, [2, 0, 0])

    @unittest.skip("This does not pass for some reason.")
    @mock.patch('cclib.parser.ccData', spec=True)
    def test_nonzero_quadrupole_moment(self, mock):
        mock.atomcoords = np.array([[
            [-1, 0, 0],
            [0, 0, 0],
            [1, 0, 0]]])
        # The periods are for Python2.
        mock.atomcharges = {'mulliken': [1/2., -1., 1/2.]}
        mock.atomnos = np.ones(mock.atomcoords.shape[1])

        x = Moments(mock).calculate()
        assert np.count_nonzero(x[1]) == 0
        assert_almost_equal(x[2] / 4.80320423, [1, 0, 0, -0.5, 0, -0.5])

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_zero_moments(self, mock):
        mock.atomcoords = np.array([[
            [-2, 0, 0],
            [-1, 0, 0],
            [0, 0, 0],
            [1, 0, 0],
            [2, 0, 0]]])
        # The periods are for Python2.
        mock.atomcharges = {'mulliken': [-1/8., 1/2., -3/4., 1/2., -1/8.]}
        mock.atomnos = np.ones(mock.atomcoords.shape[1])

        x = Moments(mock).calculate()
        assert np.count_nonzero(x[1]) == 0
        assert np.count_nonzero(x[2]) == 0

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_invariant_to_origin_dislacement(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)

        x = Moments(mock).calculate(origin=[0, 0, 0])[1]
        y = Moments(mock).calculate(origin=[1, 1, 1])[1]
        assert_equal(x, y)

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_variant_to_origin_dislacement(self, mock):
        attrs = dict(self.linear_dipole_attrs, **{
            'atomcharges': {'mulliken': [-1, 2]}
        })
        mock.configure_mock(**attrs)

        x = Moments(mock).calculate(origin=[0, 0, 0])[1]
        y = Moments(mock).calculate(origin=[1, 1, 1])[1]
        assert not np.array_equal(x, y)

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_origin_at_center_of_nuclear_charge(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)

        x = Moments(mock).calculate()[0]
        assert_equal(x, [0, 0, 0])

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_origin_at_center_of_mass(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)
        mock.atommasses = np.ones(mock.atomcoords.shape[1])

        x = Moments(mock).calculate(origin='mass')[0]
        assert_equal(x, [0, 0, 0])

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_user_provided_origin(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)

        x = Moments(mock).calculate(origin=[1, 1, 1])
        assert_almost_equal(x[0], [1, 1, 1])

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_user_provided_masses(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)

        x = Moments(mock).calculate(masses=[1, 3], origin='mass')
        assert_almost_equal(x[0], [0.5, 0, 0])

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_not_providing_masses(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)
        # Replace with the refex version when Python2 is dropped.
        # with self.assertRaisesRegex(ValueError, 'masses'):
        with pytest.raises(ValueError):
            Moments(mock).calculate(origin='mass')

    @mock.patch('cclib.parser.ccData', spec=True)
    def test_results_storing(self, mock):
        mock.configure_mock(**self.linear_dipole_attrs)
        mock.atomcharges.update({'lowdin': [-0.5, 0.5]})

        m = Moments(mock)
        m.calculate(population='mulliken')
        m.calculate(population='lowdin')

        a, b = m.results['mulliken'][1], m.results['lowdin'][1]
        assert not np.array_equal(a, b)


if __name__ == '__main__':
    suite = unittest.makeSuite(TestIdealizedInputs)
    unittest.TextTestRunner(verbosity=2).run(suite)