1#!/usr/bin/env python
2# Copyright 2014-2018 The PySCF Developers. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import unittest
17import numpy
18from pyscf import lib
19from pyscf.pbc import gto as pgto
20from pyscf.pbc import scf as pscf
21from pyscf.pbc.scf import stability
22
23L = 4
24n = 15
25cell = pgto.Cell()
26cell.build(unit = 'B',
27           verbose = 5,
28           output = '/dev/null',
29           a = ((L,0,0),(0,L,0),(0,0,L)),
30           mesh = [n,n,n],
31           atom = [['He', (L/2.-.5,L/2.,L/2.-.5)],
32                   ['He', (L/2.   ,L/2.,L/2.+.5)]],
33           basis = { 'He': [[0, (0.8, 1.0)],
34                            [0, (1.0, 1.0)],
35                            [0, (1.2, 1.0)]]})
36
37numpy.random.seed(4)
38kpts = numpy.random.random((1,3))
39
40def tearDownModule():
41    global cell
42    cell.stdout.close()
43    del cell
44
45
46class KnownValues(unittest.TestCase):
47    def test_hf_stability(self):
48        mf = pscf.RHF(cell, exxdiv='ewald').run(conv_tol=1e-12)
49        mo_i, mo_e = mf.stability(internal=True, external=True)
50        self.assertAlmostEqual(abs(mf.mo_coeff-mo_i).max(), 0, 9)
51
52    def test_khf_stability(self):
53        kmf = pscf.KRHF(cell, kpts, exxdiv='ewald').run(conv_tol=1e-12)
54        mo_i, mo_e = kmf.stability(internal=True, external=True)
55        self.assertAlmostEqual(abs(kmf.mo_coeff[0]-mo_i[0]).max(), 0, 9)
56
57        hop2, hdiag2 = stability._gen_hop_rhf_external(kmf)
58        self.assertAlmostEqual(lib.fp(hdiag2), 18.528134783454508, 7)
59        self.assertAlmostEqual(lib.fp(hop2(hdiag2)), 108.99683506471919, 5)
60
61    def test_uhf_stability(self):
62        umf = pscf.UHF(cell, exxdiv='ewald').run(conv_tol=1e-12)
63        mo_i, mo_e = umf.stability(internal=True, external=True)
64        self.assertAlmostEqual(abs(umf.mo_coeff[0]-mo_i[0]).max(), 0, 9)
65        self.assertAlmostEqual(abs(umf.mo_coeff[1]-mo_i[1]).max(), 0, 9)
66
67    def test_kuhf_stability(self):
68        kumf = pscf.KUHF(cell, kpts, exxdiv='ewald').run(conv_tol=1e-12)
69        mo_i, mo_e = kumf.stability(internal=True, external=True)
70        self.assertAlmostEqual(abs(kumf.mo_coeff[0][0]-mo_i[0][0]).max(), 0, 9)
71        self.assertAlmostEqual(abs(kumf.mo_coeff[1][0]-mo_i[1][0]).max(), 0, 9)
72
73        hop2, hdiag2 = stability._gen_hop_uhf_external(kumf)
74        self.assertAlmostEqual(lib.fp(hdiag2), 10.977759629315884, 7)
75        self.assertAlmostEqual(lib.fp(hop2(hdiag2)), 86.425042652868, 5)
76
77    def test_rotate_mo(self):
78        numpy.random.seed(4)
79        def occarray(nmo, nocc):
80            occ = numpy.zeros(nmo)
81            occ[:nocc] = 2
82            return occ
83        mo_coeff = [numpy.random.random((8,8)),
84                    numpy.random.random((8,7)),
85                    numpy.random.random((8,8))]
86        mo_occ = [occarray(8, 3), occarray(7, 3), occarray(8, 2)]
87        dx = numpy.random.random(15+12+12)
88        mo1 = stability._rotate_mo(mo_coeff, mo_occ, dx)
89        self.assertAlmostEqual(lib.fp(mo1[0]), 1.1090134286653903, 12)
90        self.assertAlmostEqual(lib.fp(mo1[1]), 1.0665953580532537, 12)
91        self.assertAlmostEqual(lib.fp(mo1[2]), -5.008202013953201, 12)
92
93if __name__ == "__main__":
94    print("Full Tests for stability")
95    unittest.main()
96
97