forked from SebWouters/CheMPS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest11.cpp.in
191 lines (169 loc) · 7.28 KB
/
test11.cpp.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
CheMPS2: a spin-adapted implementation of DMRG for ab initio quantum chemistry
Copyright (C) 2013-2018 Sebastian Wouters
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iostream>
#include <math.h>
#include "Initialize.h"
#include "DMRG.h"
#include "FCI.h"
#include "MPIchemps2.h"
using namespace std;
int main(void){
#ifdef CHEMPS2_MPI_COMPILATION
CheMPS2::MPIchemps2::mpi_init();
#endif
CheMPS2::Initialize::Init();
//The Hamiltonian: 1D Hubbard model
const int L = 10;
const int Group = 0;
const double U = 2.0;
const double T = -1.0;
int * irreps = new int[L];
for (int cnt=0; cnt<L; cnt++){ irreps[cnt] = 0; }
//The Hamiltonian initializes all its matrix elements to 0.0
CheMPS2::Hamiltonian * Ham = new CheMPS2::Hamiltonian(L, Group, irreps);
delete [] irreps;
for (int cnt=0; cnt<L; cnt++){ Ham->setVmat(cnt,cnt,cnt,cnt,U); }
for (int cnt=0; cnt<L-1; cnt++){ Ham->setTmat(cnt,cnt+1,T); }
//The targeted state
const int TwoS = 5;
const int N = 9;
const int Irrep = 0;
CheMPS2::Problem * Prob = new CheMPS2::Problem(Ham, TwoS, N, Irrep);
//The convergence scheme
CheMPS2::ConvergenceScheme * OptScheme = new CheMPS2::ConvergenceScheme(2);
//OptScheme->setInstruction(instruction, DSU(2), Econvergence, maxSweeps, noisePrefactor);
OptScheme->setInstruction(0, 30, 1e-10, 3, 0.1);
OptScheme->setInstruction(1, 1000, 1e-10, 10, 0.0);
//Run ground state calculation
CheMPS2::DMRG * theDMRG = new CheMPS2::DMRG(Prob, OptScheme);
const double EnergyDMRG = theDMRG->Solve();
const bool do_3rdm = true;
theDMRG->calc_rdms_and_correlations(do_3rdm);
#ifdef CHEMPS2_MPI_COMPILATION
if ( CheMPS2::MPIchemps2::mpi_rank() == MPI_CHEMPS2_MASTER )
#endif
{
theDMRG->getCorrelations()->Print();
}
//Get a diagonal block of the 4-RDM
const int ham_orbz = 3;
double * dmrg_diag_4rdm = new double[ L*L*L*L*L*L ];
theDMRG->Symm4RDM( dmrg_diag_4rdm, ham_orbz, ham_orbz, false );
//Calculate FCI reference energy
double EnergyFCI = 0.0;
double RMSerror2DM = 0.0;
double RMSerror3DM = 0.0;
double RMSerror4DM = 0.0;
#ifdef CHEMPS2_MPI_COMPILATION
if ( CheMPS2::MPIchemps2::mpi_rank() == MPI_CHEMPS2_MASTER )
#endif
{
const int Nel_up = ( N + TwoS ) / 2;
const int Nel_down = ( N - TwoS ) / 2;
const double maxMemWorkMB = 10.0;
const int FCIverbose = 1;
CheMPS2::FCI * theFCI = new CheMPS2::FCI(Ham, Nel_up, Nel_down, Irrep, maxMemWorkMB, FCIverbose);
double * inoutput = new double[theFCI->getVecLength(0)];
theFCI->FillRandom(theFCI->getVecLength(0), inoutput);
inoutput[ theFCI->LowestEnergyDeterminant() ] = 1.0;
EnergyFCI = theFCI->GSDavidson(inoutput);
theFCI->CalcSpinSquared(inoutput);
const int L = Ham->getL();
double * RDMspace = new double[ L*L*L*L*L*L ];
theFCI->Fill2RDM(inoutput, RDMspace);
for (int orb1=0; orb1<L; orb1++){
for (int orb2=0; orb2<L; orb2++){
for (int orb3=0; orb3<L; orb3++){
for (int orb4=0; orb4<L; orb4++){
const double difference = RDMspace[orb1 + L*(orb2 + L*(orb3 + L*orb4))]
- theDMRG->get2DM()->getTwoDMA_HAM(orb1, orb2, orb3, orb4);
RMSerror2DM += difference * difference;
}
}
}
}
theFCI->Fill3RDM(inoutput, RDMspace);
for (int orb1=0; orb1<L; orb1++){
for (int orb2=0; orb2<L; orb2++){
for (int orb3=0; orb3<L; orb3++){
for (int orb4=0; orb4<L; orb4++){
for (int orb5=0; orb5<L; orb5++){
for (int orb6=0; orb6<L; orb6++){
const double difference = RDMspace[orb1 + L*(orb2 + L*(orb3 + L*(orb4 + L*(orb5 + L * orb6))))]
- theDMRG->get3DM()->get_ham_index(orb1, orb2, orb3, orb4, orb5, orb6);
RMSerror3DM += difference * difference;
}
}
}
}
}
}
double * fci_diag_4rdm = new double[ L*L*L*L*L*L ];
theFCI->Diag4RDM( inoutput, RDMspace, ham_orbz, fci_diag_4rdm );
for (int orb1=0; orb1<L; orb1++){
for (int orb2=0; orb2<L; orb2++){
for (int orb3=0; orb3<L; orb3++){
for (int orb4=0; orb4<L; orb4++){
for (int orb5=0; orb5<L; orb5++){
for (int orb6=0; orb6<L; orb6++){
const double difference = fci_diag_4rdm[orb1 + L*(orb2 + L*(orb3 + L*(orb4 + L*(orb5 + L * orb6))))]
- 0.5 * dmrg_diag_4rdm[orb1 + L*(orb2 + L*(orb3 + L*(orb4 + L*(orb5 + L * orb6))))];
RMSerror4DM += difference * difference;
}
}
}
}
}
}
delete [] fci_diag_4rdm;
delete [] RDMspace;
delete [] inoutput;
delete theFCI;
RMSerror2DM = sqrt(RMSerror2DM);
RMSerror3DM = sqrt(RMSerror3DM);
RMSerror4DM = sqrt(RMSerror4DM);
cout << "Frobenius norm of the difference of the DMRG and FCI 2-RDM = " << RMSerror2DM << endl;
cout << "Frobenius norm of the difference of the DMRG and FCI 3-RDM = " << RMSerror3DM << endl;
cout << "Frobenius norm of the difference of the DMRG and FCI diag(4-RDM) for fixed orbital " << ham_orbz << " = " << RMSerror4DM << endl;
cout << "******************************************************************" << endl;
}
#ifdef CHEMPS2_MPI_COMPILATION
CheMPS2::MPIchemps2::broadcast_array_double( &EnergyFCI, 1, MPI_CHEMPS2_MASTER );
CheMPS2::MPIchemps2::broadcast_array_double( &RMSerror2DM, 1, MPI_CHEMPS2_MASTER );
CheMPS2::MPIchemps2::broadcast_array_double( &RMSerror3DM, 1, MPI_CHEMPS2_MASTER );
CheMPS2::MPIchemps2::broadcast_array_double( &RMSerror4DM, 1, MPI_CHEMPS2_MASTER );
#endif
//Clean up DMRG
delete [] dmrg_diag_4rdm;
if (CheMPS2::DMRG_storeMpsOnDisk){ theDMRG->deleteStoredMPS(); }
if (CheMPS2::DMRG_storeRenormOptrOnDisk){ theDMRG->deleteStoredOperators(); }
delete theDMRG;
delete OptScheme;
delete Prob;
delete Ham;
//Check succes
const bool success = (( fabs( EnergyDMRG - EnergyFCI ) < 1e-8 ) && ( RMSerror2DM < 1e-3 ) && ( RMSerror3DM < 1e-3 ) && ( RMSerror4DM < 1e-3 )) ? true : false;
#ifdef CHEMPS2_MPI_COMPILATION
CheMPS2::MPIchemps2::mpi_finalize();
#endif
cout << "================> Did test 11 succeed : ";
if (success){
cout << "yes" << endl;
return 0; //Success
}
cout << "no" << endl;
return 7; //Fail
}