1 /* Copyright (c) 2015  Gerald Knizia
2  *
3  * This file is part of the IboView program (see: http://www.iboview.org)
4  *
5  * IboView is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * IboView is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with bfint (LICENSE). If not, see http://www.gnu.org/licenses/
16  *
17  * Please see IboView documentation in README.txt for:
18  * -- A list of included external software and their licenses. The included
19  *    external software's copyright is not touched by this agreement.
20  * -- Notes on re-distribution and contributions to/further development of
21  *    the IboView software
22  */
23 
24 #ifndef CT_RHF_FOCKBUILD_H
25 #define CT_RHF_FOCKBUILD_H
26 
27 #include <string>
28 #include "CtBasisSet.h"
29 #include "CtAtomSet.h"
30 #include "CtMatrix.h"
31 // #include "CtDma.h"
32 #include "CtDftGrid.h" // for grid params.
33 #include "CtDftFunc.h"
34 #include "CtFockBuild.h"
35 #include "CtRhfOptions.h"
36 #include "CtTiming.h"
37 #include "CtIo.h"
38 
39 namespace ct {
40 
41 enum FFockBuilderFlags {
42    FOCKBUILD_ClosedOnly //
43 };
44 
45 
46 struct FFockComponentBuilder : public FIntrusivePtrDest
47 {
48    explicit FFockComponentBuilder(FLog &Log_, FTimerSet *pTimers_=0) : Energy(0.), m_Log(Log_), m_pTimers(pTimers_) {};
49 
50    virtual void Init(FWfDecl const &WfDecl_, FBasisSet *pOrbBasis_, FAtomSet const &Atoms_, FHfOptions const &Options_, FMemoryStack &Mem) = 0;
51    // Accumulate fock matrix contribution to FockC/FockO.
52    // Notes:
53    //   - OrbC and OrbO are supposed to have their respective occupation numbers absorbed,
54    //     such that the closed/open densities are obtained by DenC := OrbC x OrbC.T and DenO := OrbO x OrbO.T
55    //   - Both orbitals and Fock matrices are expressed in terms of pBasis. pBasis may differ from pOrbBasis given in Init().
56    virtual void AccFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FMemoryStack &Mem);
57    virtual ~FFockComponentBuilder() = 0;
58 
59    virtual void AccGradient(FMatrixView Gradient, FMatrixView COccC, FMatrixView COccO, FMemoryStack &Mem);
60 
61    double Energy;
62    virtual void PrintEnergyContribs();
63 
64    // returns true if this fock builder did anything here. Default implementation returns false (command ignored---no grids)
65    virtual bool SwitchToRefineGrid(FDftGridParams const &NewGridParams);
66 protected:
67    FLog
68       &m_Log;
69    FTimerSet
70       *m_pTimers;
71    FWfDecl
72       m_WfDecl;
73 private:
74    FFockComponentBuilder(FFockComponentBuilder const &); // not implemented
75    void operator = (FFockComponentBuilder const &); // not implemented
76 };
77 typedef boost::intrusive_ptr<FFockComponentBuilder>
78    FFockComponentBuilderPtr;
79 
80 
81 typedef std::vector<FFockComponentBuilderPtr>
82    FFockComponentBuilderList;
83 void BuildFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FFockComponentBuilderList &BuilderList, FMemoryStack &Mem);
84 
85 
86 
87 struct FDfJkOptions
88 {
FDfJkOptionsFDfJkOptions89    FDfJkOptions() : fExchFactor(1.0) {}
90    double fExchFactor;
91 };
92 
93 
94 // build both DF Coulomb and Exchange
95 struct FFockComponentBuilderDfJk : public FFockComponentBuilder
96 {
97    FFockComponentBuilderDfJk(FDfJkOptions const &Options, FLog &Log_, FTimerSet *pTimers_);
98    void Init(FWfDecl const &WfDecl_, FBasisSet *pOrbBasis_, FAtomSet const &Atoms_, FHfOptions const &Options_, FMemoryStack &Mem);
99    void AccFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FMemoryStack &Mem);
100    void AccGradient(FMatrixView Gradient, FMatrixView COccC, FMatrixView COccO, FMemoryStack &Mem);
101    void PrintEnergyContribs();
102    ~FFockComponentBuilderDfJk();
103 public:
104    FDfJkOptions
105       Options;
106    FAtomSet const
107       *pAtoms;
108    FBasisSetPtr
109       pFitBasis;
110    FBasisSet
111       *pOrbBasis;
112    FMatrixView
113       Jcd;
114    size_t
115       nFit;
116    double
117       EnergyCoulomb, EnergyExch, EnergyXc;
118 };
119 
120 
121 // build DF Coulomb only
122 struct FFockComponentBuilderDfCoul : public FFockComponentBuilder
123 {
124    FFockComponentBuilderDfCoul(FDfJkOptions const &Options, FLog &Log_);
125    void Init(FWfDecl const &WfDecl_, FBasisSet *pOrbBasis_, FAtomSet const &Atoms_, FHfOptions const &Options_, FMemoryStack &Mem);
126    void AccFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FMemoryStack &Mem);
127    ~FFockComponentBuilderDfCoul();
128 public:
129    FDfJkOptions
130       Options;
131    FAtomSet const
132       *pAtoms;
133    FBasisSetPtr
134       pFitBasis;
135    FBasisSet
136       *pOrbBasis;
137    FMatrixView
138       Jcd;
139    size_t
140       nFit;
141 };
142 
143 
144 // build df-coulomb and auxiliary expanded xc. Cache all integrals in memory.
145 // struct FFockComponentBuilderDfCoulXcCachedImpl;
146 struct FFockComponentBuilderDfCoulXcCached : public FFockComponentBuilder
147 {
148    FFockComponentBuilderDfCoulXcCached(FDfJkOptions const &Options, FDftGridParams const &GridParams_, std::string const &XcFunctionalName, FLog &Log_, FTimerSet *pTimers_);
149    void Init(FWfDecl const &WfDecl_, FBasisSet *pOrbBasis_, FAtomSet const &Atoms_, FHfOptions const &Options_, FMemoryStack &Mem);
150    void AccFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FMemoryStack &Mem);
151    void AccGradient(FMatrixView Gradient, FMatrixView COccC, FMatrixView COccO, FMemoryStack &Mem);
152    ~FFockComponentBuilderDfCoulXcCached();
153    void PrintEnergyContribs();
154    bool SwitchToRefineGrid(FDftGridParams const &NewGridParams);
155 protected:
156    std::string
157       XcFunctionalName;
158    FDfJkOptions
159       JkOptions;
160    FAtomSet const
161       *pAtoms;
162    FBasisSetPtr
163       pFitBasis;
164    FBasisSet
165       *pOrbBasis;
166    FRawBasis
167       *pOrbBasisRaw,
168       *pFitBasisRaw;
169    FMatrixView
170       Jcd;
171    size_t
172       nAo,
173       nFit;
174    FDftGridParams
175       GridParams;
176    FDftGridPtr
177       pDftGrid;
178    FXcFunctionalPtr
179       pXcFn;
180    TArray<double>
181       // format: nFit x [(nAo * (nAo+1))/2].
182       // This version is for *small* cases and stores *all* integrals.
183       Int3ixStorage;
184    FMatrixView
185       Int3ix;
186 //    FFockComponentBuilderDfCoulXcCachedImpl
187 //       *p;
188    TArray<double>
189       m_LastDensity;
190    bool
191       AuxExpandXc;
192 
193    double
194       EnergyCoulomb, EnergyXc, fElecTotal, fElecTotalAnalytic;
195    void Make1ixDensity(FMatrixView Jgamma, FMatrixView AuxDen, FMatrixView COccC, FMatrixView COccO, FMemoryStack &Mem);
196 private:
197    FFockComponentBuilderDfCoulXcCached(FFockComponentBuilderDfCoulXcCached const&); // not implemented
198    void operator = (FFockComponentBuilderDfCoulXcCached const &); // not implemented
199 };
200 
201 
202 
203 
204 // build IAO local exchange
205 struct FFockComponentBuilderDfLx : public FFockComponentBuilder
206 {
207    FFockComponentBuilderDfLx(FDfJkOptions const &Options_, FMatrixView S1_, FMatrixView S1cd_, FLog &Log_);
208    void Init(FWfDecl const &WfDecl_, FBasisSet *pOrbBasis_, FAtomSet const &Atoms_, FHfOptions const &Options_, FMemoryStack &Mem);
209    void AccFock(FMatrixView &FockC, FMatrixView &FockO, FBasisSet *pBasis, FMatrixView const &COccC, FMatrixView const &COccO, uint Flags, FMemoryStack &Mem);
210    ~FFockComponentBuilderDfLx();
211 public:
212    FDfJkOptions
213       Options;
214    FAtomSet const
215       *pAtoms;
216    FBasisSetPtr
217       pFitBasis,
218       pMinBasis,
219       pIaoBasis;
220    FBasisSet
221       *pOrbBasis;
222    FMatrixView
223       Jcd,  // cholesky decomposition of full JKFIT basis (may not be required, depending on the mode of use)
224       S1,   // overlap matrix of orbital basis
225       S1cd; // choleksy decomposition of orbital basis.
226 };
227 
228 
229 double *FormIntMNF(ir::FRawShell const &ShF,
230    ir::FIntegralKernel *pIntKernel, FRawBasis const *pOrbBasisA, FRawBasis const *pOrbBasisB, FRawBasis const *pFitBasis, FMemoryStack &Mem, FMatrixView ScrDen, double fThr);
231 double *FormIntMNF(ir::FRawShell const &ShF,
232    ir::FIntegralKernel *pIntKernel, FRawBasis const *pOrbBasis, FRawBasis const *pFitBasis, FMemoryStack &Mem, FMatrixView ScrDen, double fThr);
233 
234 
235 } // namespace ct
236 
237 #endif // CT_RHF_FOCKBUILD_H
238