1#!/usr/bin/env python
2#
3# Author: Qiming Sun <osirpt.sun@gmail.com>
4#
5
6'''
7This example shows how to save the DF integrals in one calculation and use the
8integrals in another one.  This can be particularly useful in all-electron PBC
9calculations.  The time-consuming DF integral generation can be done once and
10reused many times.
11'''
12
13import tempfile
14from pyscf import gto, scf, df
15from pyscf.pbc import gto as pgto
16from pyscf.pbc import dft as pdft
17
18tmpf = tempfile.NamedTemporaryFile()
19file_to_save_df_ints = tmpf.name
20print('DF integral is saved in %s' % file_to_save_df_ints)
21
22#
23# Save the density fitting integrals in a file.
24# After calling .density_fit() function for SCF object, attribute .with_df
25# will be created for the SCF object.  If with_df._cderi_to_save is specified,
26# the DF integrals will be saved in this file.
27#
28mol = gto.M(atom='N 0 0 0; N 0 0 1.2', basis='ccpvdz')
29mf = scf.RHF(mol).density_fit(auxbasis='weigend')
30mf.with_df._cderi_to_save = file_to_save_df_ints
31mf.kernel()
32
33#
34# The DF integral file can be used in an separated calculation.
35# Attribute with_df._cderi is the filename which saves the DF integrals.  This
36# is a read-only file, integrals in this file will not be modified in the
37# You can feed integrals using ._cderi (HDF5) file from any source/program or
38# an early calculation.
39#
40mf = scf.RHF(mol).density_fit()
41mf.with_df._cderi = file_to_save_df_ints
42mf.kernel()
43
44#
45# Attribute with_df._cderi also can be a Numpy array.
46# In the following example, df.incore.cholesky_eri function generates the DF
47# integral tensor in a numpy array.
48#
49int3c = df.incore.cholesky_eri(mol, auxbasis='ahlrichs')
50mf = scf.RHF(mol).density_fit()
51mf.with_df._cderi = int3c
52mf.kernel()
53
54
55#
56# Same mechanism can be used in PBC system.  Generating DF integrals under PBC
57# is very expensive.  These DF integrals can be generated once in a file and
58# reused later in another calculation.
59#
60
61cell = pgto.Cell()
62cell.atom='''
63C 0.000000000000   0.000000000000   0.000000000000
64C 1.685068664391   1.685068664391   1.685068664391
65'''
66cell.basis = '6-31g'
67cell.a = '''
680.000000000, 3.370137329, 3.370137329
693.370137329, 0.000000000, 3.370137329
703.370137329, 3.370137329, 0.000000000'''
71cell.unit = 'B'
72cell.mesh = [10]*3
73#cell.verbose = 4
74cell.build()
75
76# Using density fitting for all-electron calculation
77mf = pdft.RKS(cell).density_fit(auxbasis='ahlrichs')
78# Saving DF integrals in file
79mf.with_df._cderi_to_save = file_to_save_df_ints
80mf.kernel()
81
82# Using DF integrals in a separated calculation by specifying with_df._cderi
83# attribute.
84mf.with_df._cderi = file_to_save_df_ints
85mf.kernel()
86