CheMPS2
DMRG.h
1 /*
2  CheMPS2: a spin-adapted implementation of DMRG for ab initio quantum chemistry
3  Copyright (C) 2013-2016 Sebastian Wouters
4 
5  This program 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; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef DMRG_CHEMPS2_H
21 #define DMRG_CHEMPS2_H
22 
23 #include <string>
24 
25 #include "Options.h"
26 #include "Problem.h"
27 #include "TensorT.h"
28 #include "TensorX.h"
29 #include "TensorL.h"
30 #include "SyBookkeeper.h"
31 #include "TensorF1.h"
32 #include "TensorF0.h"
33 #include "TensorS0.h"
34 #include "TensorS1.h"
35 #include "TensorOperator.h"
36 #include "TensorQ.h"
37 #include "TensorO.h"
38 #include "Tensor3RDM.h"
39 #include "TensorGYZ.h"
40 #include "TensorKM.h"
41 #include "TwoDM.h"
42 #include "ThreeDM.h"
43 #include "Correlations.h"
44 #include "Heff.h"
45 #include "Sobject.h"
46 #include "ConvergenceScheme.h"
47 #include "MyHDF5.h"
48 
49 //For the timings of the different parts of DMRG
50 #define CHEMPS2_TIME_S_JOIN 0
51 #define CHEMPS2_TIME_S_SOLVE 1
52 #define CHEMPS2_TIME_S_SPLIT 2
53 #define CHEMPS2_TIME_TENS_TOTAL 3
54 #define CHEMPS2_TIME_TENS_ALLOC 4
55 #define CHEMPS2_TIME_TENS_FREE 5
56 #define CHEMPS2_TIME_DISK_WRITE 6
57 #define CHEMPS2_TIME_DISK_READ 7
58 #define CHEMPS2_TIME_TENS_CALC 8
59 #define CHEMPS2_TIME_VECLENGTH 9
60 
61 namespace CheMPS2{
82  class DMRG{
83 
84  public:
85 
87 
91  DMRG(Problem * Probin, ConvergenceScheme * OptSchemeIn, const bool makechkpt=CheMPS2::DMRG_storeMpsOnDisk, const string tmpfolder=CheMPS2::defaultTMPpath);
92 
94  virtual ~DMRG();
95 
97 
98  double Solve();
99 
101  void PreSolve();
102 
105 
107 
109  void calc_rdms_and_correlations( const bool do_3rdm, const bool disk_3rdm = false );
110 
112 
113  TwoDM * get2DM(){ return the2DM; }
114 
116 
117  ThreeDM * get3DM(){ return the3DM; }
118 
120 
124  void Symm4RDM( double * output, const int ham_orb1, const int ham_orb2, const bool last_case );
125 
127 
128  Correlations * getCorrelations(){ return theCorr; }
129 
131 
133  double getSpecificCoefficient(int * coeff) const;
134 
136 
140  double getFCIcoefficient(int * alpha, int * beta, const bool mpi_chemps2_master_only=true) const;
141 
143  void deleteStoredMPS();
144 
146  void deleteStoredOperators();
147 
149 
150  void activateExcitations(const int maxExcIn);
151 
153 
154  void newExcitation(const double EshiftIn);
155 
157  static void PrintLicense();
158 
159  private:
160 
161  //Setup the DMRG SyBK and MPS (in separate function to allow pushbacks and recreations for excited states)
162  void setupBookkeeperAndMPS();
163 
165  string MPSstoragename;
166 
167  //The optimization scheme for the DMRG sweeps (externally allocated, filled and deleted)
168  ConvergenceScheme * OptScheme;
169 
170  //Whether or not the MPS was loaded from disk to memory at the start
171  bool loadedMPS;
172 
173  //Integer to distinguish storage between different calculations
174  int thePID;
175 
176  //Pointer to the Problem --> constructed and destructed outside of this class
177  Problem * Prob;
178 
179  //The number of orbitals: copied here so that the DMRG destructor doesn't depend on whether Prob still exists
180  int L;
181 
182  //Minimum energy encountered during all performed micro-iterations (as opposed to the 2DM/edge energy)
183  double TotalMinEnergy;
184 
185  //Minimum energy encountered during the micro-iterations of the last performed sweep (as opposed to the 2DM/edge energy)
186  double LastMinEnergy;
187 
188  //Max. discarded weight of last sweep
189  double MaxDiscWeightLastSweep;
190 
191  //Symmetry information object
192  SyBookkeeper * denBK;
193 
194  //The MPS
195  TensorT ** MPS;
196 
197  //The TwoDM
198  TwoDM * the2DM;
199 
200  //The ThreeDM
201  ThreeDM * the3DM;
202 
203  //The Correlations
204  Correlations * theCorr;
205 
206  //Whether or not allocated
207  int * isAllocated;
208 
209  //TensorL's
210  TensorL *** Ltensors;
211 
212  //TensorX's
213  TensorX ** Xtensors;
214 
215  //TensorF0's
216  TensorF0 **** F0tensors;
217 
218  //TensorF1's
219  TensorF1 **** F1tensors;
220 
221  //TensorS0's
222  TensorS0 **** S0tensors;
223 
224  //TensorS1's
225  TensorS1 **** S1tensors;
226 
227  //ABCD-tensors
228  TensorOperator **** Atensors;
229  TensorOperator **** Btensors;
230  TensorOperator **** Ctensors;
231  TensorOperator **** Dtensors;
232 
233  //TensorQ's
234  TensorQ *** Qtensors;
235 
236  //Tensors required for the 3-RDM calculation
237  Tensor3RDM ***** tensor_3rdm_a_J0_doublet;
238  Tensor3RDM ***** tensor_3rdm_a_J1_doublet;
239  Tensor3RDM ***** tensor_3rdm_a_J1_quartet;
240  Tensor3RDM ***** tensor_3rdm_b_J0_doublet;
241  Tensor3RDM ***** tensor_3rdm_b_J1_doublet;
242  Tensor3RDM ***** tensor_3rdm_b_J1_quartet;
243  Tensor3RDM ***** tensor_3rdm_c_J0_doublet;
244  Tensor3RDM ***** tensor_3rdm_c_J1_doublet;
245  Tensor3RDM ***** tensor_3rdm_c_J1_quartet;
246  Tensor3RDM ***** tensor_3rdm_d_J0_doublet;
247  Tensor3RDM ***** tensor_3rdm_d_J1_doublet;
248  Tensor3RDM ***** tensor_3rdm_d_J1_quartet;
249 
250  //Tensors required for the Correlations calculation
251  TensorGYZ ** Gtensors;
252  TensorGYZ ** Ytensors;
253  TensorGYZ ** Ztensors;
254  TensorKM ** Ktensors;
255  TensorKM ** Mtensors;
256 
257  // Sweeps
258  double sweepleft( const bool change, const int instruction, const bool am_i_master );
259  double sweepright( const bool change, const int instruction, const bool am_i_master );
260  double solve_site( const int index, const double dvdson_rtol, const double noise_level, const int virtual_dimension, const bool am_i_master, const bool moving_right, const bool change );
261 
262  //Load and save functions
263  void MY_HDF5_WRITE_BATCH(const hid_t file_id, const int number, Tensor ** batch, const long long totalsize, const std::string tag);
264  void MY_HDF5_READ_BATCH( const hid_t file_id, const int number, Tensor ** batch, const long long totalsize, const std::string tag);
265  void OperatorsOnDisk(const int index, const bool movingRight, const bool store);
266  string tempfolder;
267 
268  void saveMPS(const std::string name, TensorT ** MPSlocation, SyBookkeeper * BKlocation, bool isConverged) const;
269  void loadDIM(const std::string name, SyBookkeeper * BKlocation);
270  void loadMPS(const std::string name, TensorT ** MPSlocation, bool * isConverged);
271  bool makecheckpoints;
272 
273  //Helper functions for making the boundary operators
274  void updateMovingRight(const int index);
275  void updateMovingLeft(const int index);
276  void deleteTensors(const int index, const bool movingRight);
277  void allocateTensors(const int index, const bool movingRight);
278  void updateMovingRightSafe(const int cnt);
279  void updateMovingRightSafeFirstTime(const int cnt);
280  void updateMovingRightSafe2DM(const int cnt);
281  void updateMovingLeftSafe(const int cnt);
282  void updateMovingLeftSafeFirstTime(const int cnt);
283  void updateMovingLeftSafe2DM(const int cnt);
284  void deleteAllBoundaryOperators();
285 
286  // Helper functions for making the 3-RDM boundary operators
287  void update_safe_3rdm_operators( const int boundary );
288  void allocate_3rdm_operators( const int boundary );
289  void update_3rdm_operators( const int boundary );
290  void delete_3rdm_operators( const int boundary );
291 
292  // Helper functions for making the symmetrized 4-RDM
293  void solve_fock( const int dmrg_orb1, const int dmrg_orb2, const double alpha, const double beta );
294  static void solve_fock_update_helper( const int index, const int dmrg_orb1, const int dmrg_orb2, const bool moving_right, TensorT ** new_mps, TensorT ** old_mps, SyBookkeeper * new_bk, SyBookkeeper * old_bk, TensorO ** overlaps, TensorL ** regular, TensorL ** trans );
295  static void left_normalize( TensorT * left_mps, TensorT * right_mps );
296  static void right_normalize( TensorT * left_mps, TensorT * right_mps );
297  void symm_4rdm_helper( double * output, const int ham_orb1, const int ham_orb2, const double alpha, const double beta, const bool add, const double factor );
298 
299  //Helper functions for making the Correlations boundary operators
300  void update_correlations_tensors(const int siteindex);
301 
302  //The storage and functions to handle excited states
303  int nStates;
304  bool Exc_activated;
305  int maxExc;
306  double * Exc_Eshifts;
307  TensorT *** Exc_MPSs;
308  SyBookkeeper ** Exc_BKs;
309  TensorO *** Exc_Overlaps;
310  double ** prepare_excitations(Sobject * denS);
311  void cleanup_excitations(double ** VeffTilde) const;
312  void calcVeffTilde(double * result, Sobject * currentS, int state_number);
313  void calc_overlaps( const bool moving_right );
314 
315  // Performance counters
316  double timings[ CHEMPS2_TIME_VECLENGTH ];
317  long long num_double_write_disk;
318  long long num_double_read_disk;
319  void print_tensor_update_performance() const;
320 
321  };
322 }
323 
324 #endif
void Symm4RDM(double *output, const int ham_orb1, const int ham_orb2, const bool last_case)
Obtain the symmetrized 4-RDM terms 0.5 * ( Gamma4_ijkl,pqrt + Gamma4_ijkt,pqrl ) with l and t fixed...
Definition: DMRGfock.cpp:41
void deleteStoredMPS()
Call "rm " + CheMPS2::DMRG_MPS_storage_prefix + "*.h5".
Definition: DMRGmpsio.cpp:164
Definition: CASPT2.h:42
DMRG(Problem *Probin, ConvergenceScheme *OptSchemeIn, const bool makechkpt=CheMPS2::DMRG_storeMpsOnDisk, const string tmpfolder=CheMPS2::defaultTMPpath)
Constructor.
Definition: DMRG.cpp:36
Correlations * getCorrelations()
Get the pointer to the Correlations.
Definition: DMRG.h:128
TwoDM * get2DM()
Get the pointer to the 2-RDM.
Definition: DMRG.h:113
double Solve()
Solver.
Definition: DMRG.cpp:199
double getFCIcoefficient(int *alpha, int *beta, const bool mpi_chemps2_master_only=true) const
Get a specific FCI coefficient. The arrays alpha and beta contain the alpha and beta occupation numbe...
void PreSolve()
Reconstruct the renormalized operators when you overwrite the matrix elements with Prob->setMxElement...
Definition: DMRG.cpp:188
void newExcitation(const double EshiftIn)
Push back current calculation and set everything up to calculate a (new) excitation.
Definition: DMRG.cpp:395
static void PrintLicense()
Print the license.
double getSpecificCoefficient(int *coeff) const
Get a specific FCI coefficient. The array coeff contains the occupation numbers of the L Hamiltonian ...
void activateExcitations(const int maxExcIn)
Activate the necessary storage and machinery to handle excitations.
Definition: DMRG.cpp:384
virtual ~DMRG()
Destructor.
Definition: DMRG.cpp:142
ThreeDM * get3DM()
Get the pointer to the 3-RDM.
Definition: DMRG.h:117
void calc_rdms_and_correlations(const bool do_3rdm, const bool disk_3rdm=false)
Calculate the reduced density matrices and correlations. Afterwards the MPS is again in LLLLLLLC gaug...
void deleteStoredOperators()
Call "rm " + tempfolder + "/" + CheMPS2::DMRG_OPERATOR_storage_prefix + string(thePID) + "*...
void calc2DMandCorrelations()
Calculate the 2-RDM and correlations. Afterwards the MPS is again in LLLLLLLC gauge.
Definition: DMRG.h:104