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#
16# Authors: Timothy Berkelbach <tim.berkelbach@gmail.com>
17#          Qiming Sun <osirpt.sun@gmail.com>
18#
19
20import unittest
21import numpy as np
22from pyscf.pbc import gto as pbcgto
23from pyscf.pbc import dft as pbcdft
24import pyscf.pbc
25pyscf.pbc.DEBUG = False
26
27L = 4.
28cell = pbcgto.Cell()
29cell.verbose = 0
30cell.a = np.eye(3)*L
31cell.atom =[['He' , ( L/2+0., L/2+0. ,   L/2+1.)],]
32cell.basis = {'He': [[0, (4.0, 1.0)], [0, (1.0, 1.0)]]}
33cell.build()
34
35def tearDownModule():
36    global cell
37    del cell
38
39
40class KnownValues(unittest.TestCase):
41#    def test_lda_grid30(self):
42#        cell = pbcgto.Cell()
43#        cell.unit = 'B'
44#        L = 10
45#        cell.a = np.diag([L]*3)
46#        cell.mesh = np.array([41]*3)
47#        cell.atom = [['He', (L/2.,L/2.,L/2.)], ]
48## these are some exponents which are not hard to integrate
49#        cell.basis = { 'He': [[0, (0.8, 1.0)],
50#                              [0, (1.0, 1.0)],
51#                              [0, (1.2, 1.0)]] }
52#        cell.verbose = 5
53#        cell.output = '/dev/null'
54#        cell.pseudo = None
55#        cell.build()
56#        mf = pbcdft.RKS(cell)
57#        mf.xc = 'LDA,VWN_RPA'
58#        mf.kpt = np.ones(3)
59#        e1 = mf.scf()
60#        self.assertAlmostEqual(e1, -2.6409616064015591, 8)
61#
62#
63#    def test_pp_RKS(self):
64#        cell = pbcgto.Cell()
65#
66#        cell.unit = 'A'
67#        cell.atom = '''
68#            Si    0.000000000    0.000000000    0.000000000;
69#            Si    0.000000000    2.715348700    2.715348700;
70#            Si    2.715348700    2.715348700    0.000000000;
71#            Si    2.715348700    0.000000000    2.715348700;
72#            Si    4.073023100    1.357674400    4.073023100;
73#            Si    1.357674400    1.357674400    1.357674400;
74#            Si    1.357674400    4.073023100    4.073023100;
75#            Si    4.073023100    4.073023100    1.357674400
76#        '''
77#        cell.basis = 'gth-szv'
78#        cell.pseudo = 'gth-pade'
79#
80#        Lx = Ly = Lz = 5.430697500
81#        cell.a = np.diag([Lx,Ly,Lz])
82#        cell.mesh = np.array([21]*3)
83#
84#        cell.verbose = 5
85#        cell.output = '/dev/null'
86#        cell.build()
87#
88#        mf = pbcdft.RKS(cell)
89#        mf.xc = 'lda,vwn'
90#        self.assertAlmostEqual(mf.scf(), -31.081616722101646, 8)
91
92
93    def test_chkfile_k_point(self):
94        cell = pbcgto.Cell()
95        cell.a = np.eye(3) * 6
96        cell.mesh = [21]*3
97        cell.unit = 'B'
98        cell.atom = '''He     2.    2.       3.
99                      He     3.    2.       3.'''
100        cell.basis = {'He': 'sto3g'}
101        cell.verbose = 0
102        cell.build()
103        mf1 = pbcdft.RKS(cell)
104        mf1.max_cycle = 1
105        mf1.kernel()
106
107        cell = pbcgto.Cell()
108        cell.a = np.eye(3) * 6
109        cell.mesh = [41]*3
110        cell.unit = 'B'
111        cell.atom = '''He     2.    2.       3.
112                       He     3.    2.       3.'''
113        cell.basis = {'He': 'ccpvdz'}
114        cell.verbose = 5
115        cell.output = '/dev/null'
116        cell.nimgs = [2,2,2]
117        cell.build()
118        mf = pbcdft.RKS(cell)
119        np.random.seed(10)
120        mf.kpt = np.random.random(3)
121        mf.max_cycle = 1
122        dm = mf.from_chk(mf1.chkfile)
123        mf.conv_check = False
124        self.assertAlmostEqual(mf.scf(dm), -4.7090816314173365, 8)
125
126    def test_density_fit(self):
127        L = 4.
128        cell = pbcgto.Cell()
129        cell.a = np.eye(3)*L
130        cell.atom =[['He' , ( L/2+0., L/2+0. ,   L/2+1.)],
131                    ['He' , ( L/2+1., L/2+0. ,   L/2+1.)]]
132        cell.basis = {'He': [[0, (4.0, 1.0)], [0, (1.0, 1.0)]]}
133        cell.build()
134        mf = pbcdft.RKS(cell).density_fit()
135        mf.kernel()
136        self.assertAlmostEqual(mf.e_tot, -4.717699891018736, 7)
137
138    def test_rsh_fft(self):
139        mf = pbcdft.RKS(cell)
140        mf.xc = 'camb3lyp'
141        mf.kernel()
142        self.assertAlmostEqual(mf.e_tot, -2.3032261128220544, 7)
143
144        mf.omega = .15
145        mf.kernel()
146        self.assertAlmostEqual(mf.e_tot, -2.3987595548455523, 7)
147
148    def test_custom_rsh_df(self):
149        mf = pbcdft.RKS(cell).density_fit()
150        mf.xc = 'camb3lyp'
151        mf.kernel()
152        self.assertAlmostEqual(mf.e_tot, -2.303232164939132, 7)
153
154        mf.omega = .15
155        mf.kernel()
156        self.assertAlmostEqual(mf.e_tot, -2.3987656490734555, 7)
157
158    def test_rsh_mdf(self):
159        mf = pbcdft.RKS(cell).mix_density_fit()
160        mf.xc = 'camb3lyp'
161        mf.kernel()
162        self.assertAlmostEqual(mf.e_tot, -2.303225896642264, 7)
163
164        mf.omega = .15
165        mf.kernel()
166        self.assertAlmostEqual(mf.e_tot, -2.398759319488945, 7)
167
168    def test_rsh_aft_high_cost(self):
169        from pyscf.pbc.df.aft import AFTDF
170        mf = pbcdft.RKS(cell)
171        mf.with_df = AFTDF(cell)
172        mf.xc = 'camb3lyp'
173        mf.kernel()
174        self.assertAlmostEqual(mf.e_tot, -2.303226113014942, 7)
175
176    def test_rsh_0d(self):
177        L = 4.
178        cell = pbcgto.Cell()
179        cell.verbose = 0
180        cell.a = np.eye(3)*L
181        cell.atom =[['He' , ( L/2+0., L/2+0. ,   L/2+1.)],]
182        cell.basis = {'He': [[0, (4.0, 1.0)], [0, (1.0, 1.0)]]}
183        cell.dimension = 0
184        cell.mesh = [60]*3
185        cell.build()
186        mf = pbcdft.RKS(cell).density_fit()
187        mf.xc = 'camb3lyp'
188        mf.omega = '0.7'
189        mf.exxdiv = None
190        mf.kernel()
191        self.assertAlmostEqual(mf.e_tot, -2.4836596871145558, 7)
192
193        mol = cell.to_mol()
194        mf1 = mol.RKS().density_fit()
195        mf1.xc = 'camb3lyp'
196        mf1.omega = '0.7'
197        mf1.kernel()
198        self.assertAlmostEqual(mf1.e_tot, mf.e_tot, 4)
199
200if __name__ == '__main__':
201    print("Full Tests for pbc.dft.rks")
202    unittest.main()
203