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
|
/*
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 <stdlib.h>
#include <sstream>
#include <string>
#include "DMRG.h"
void CheMPS2::DMRG::saveMPS(const std::string name, TensorT ** MPSlocation, SyBookkeeper * BKlocation, bool isConverged) const{
//The hdf5 file
hid_t file_id = H5Fcreate(name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
//Whether the MPS was converged or not
hid_t group_id = H5Gcreate(file_id, "/Convergence", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hsize_t dimarray = 1; //One integer
hid_t dataspace_id = H5Screate_simple(1, &dimarray, NULL);
hid_t dataset_id = H5Dcreate(group_id, "Converged_yn", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
int toWrite = (isConverged)?1:0;
H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toWrite);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
H5Gclose(group_id);
//The current virtual dimensions
for (int bound=0; bound<=BKlocation->gL(); bound++){
for (int N=BKlocation->gNmin(bound); N<=BKlocation->gNmax(bound); N++){
for (int TwoS=BKlocation->gTwoSmin(bound,N); TwoS<=BKlocation->gTwoSmax(bound,N); TwoS+=2){
for (int Irrep=0; Irrep<BKlocation->getNumberOfIrreps(); Irrep++){
std::stringstream sstream;
sstream << "/VirtDim_" << bound << "_" << N << "_" << TwoS << "_" << Irrep;
hid_t group_id2 = H5Gcreate(file_id, sstream.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hsize_t dimarray2 = 1; //One integer
hid_t dataspace_id2 = H5Screate_simple(1, &dimarray2, NULL);
hid_t dataset_id2 = H5Dcreate(group_id2, "Value", H5T_STD_I32LE, dataspace_id2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
int toWrite2 = BKlocation->gCurrentDim(bound,N,TwoS,Irrep);
H5Dwrite(dataset_id2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toWrite2);
H5Dclose(dataset_id2);
H5Sclose(dataspace_id2);
H5Gclose(group_id2);
}
}
}
}
//The MPS
for (int site=0; site<BKlocation->gL(); site++){
std::stringstream sstream;
sstream << "/MPS_" << site;
hid_t group_id3 = H5Gcreate(file_id, sstream.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hsize_t dimarray3 = MPSlocation[site]->gKappa2index(MPSlocation[site]->gNKappa()); //An array of doubles
hid_t dataspace_id3 = H5Screate_simple(1, &dimarray3, NULL);
hid_t dataset_id3 = H5Dcreate(group_id3, "Values", H5T_IEEE_F64LE, dataspace_id3, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id3, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, MPSlocation[site]->gStorage());
H5Dclose(dataset_id3);
H5Sclose(dataspace_id3);
H5Gclose(group_id3);
}
H5Fclose(file_id);
}
void CheMPS2::DMRG::loadDIM(const std::string name, SyBookkeeper * BKlocation){
//The hdf5 file
hid_t file_id = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
//The current virtual dimensions
for (int bound=0; bound<=BKlocation->gL(); bound++){
for (int N=BKlocation->gNmin(bound); N<=BKlocation->gNmax(bound); N++){
for (int TwoS=BKlocation->gTwoSmin(bound,N); TwoS<=BKlocation->gTwoSmax(bound,N); TwoS+=2){
for (int Irrep=0; Irrep<BKlocation->getNumberOfIrreps(); Irrep++){
std::stringstream sstream;
sstream << "/VirtDim_" << bound << "_" << N << "_" << TwoS << "_" << Irrep;
hid_t group_id2 = H5Gopen(file_id, sstream.str().c_str(), H5P_DEFAULT);
hid_t dataset_id2 = H5Dopen(group_id2, "Value", H5P_DEFAULT);
int toRead;
H5Dread(dataset_id2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toRead);
BKlocation->SetDim(bound, N, TwoS, Irrep, toRead);
H5Dclose(dataset_id2);
H5Gclose(group_id2);
}
}
}
}
H5Fclose(file_id);
}
void CheMPS2::DMRG::loadMPS(const std::string name, TensorT ** MPSlocation, bool * isConverged){
//The hdf5 file
hid_t file_id = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
//Whether the MPS was converged or not
hid_t group_id = H5Gopen(file_id, "/Convergence", H5P_DEFAULT);
hid_t dataset_id = H5Dopen(group_id, "Converged_yn", H5P_DEFAULT);
int toRead;
H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toRead);
isConverged[0] = (toRead==0)?false:true;
H5Dclose(dataset_id);
H5Gclose(group_id);
//The MPS
for (int site=0; site<L; site++){
std::stringstream sstream;
sstream << "/MPS_" << site;
hid_t group_id3 = H5Gopen(file_id, sstream.str().c_str(), H5P_DEFAULT);
hid_t dataset_id3 = H5Dopen(group_id3, "Values", H5P_DEFAULT);
H5Dread(dataset_id3, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, MPSlocation[site]->gStorage());
H5Dclose(dataset_id3);
H5Gclose(group_id3);
}
H5Fclose(file_id);
}
void CheMPS2::DMRG::deleteStoredMPS(){
std::stringstream thestream;
thestream << "rm " << CheMPS2::DMRG_MPS_storage_prefix << "*.h5";
int info = system(thestream.str().c_str());
std::cout << "Info on DMRG::MPS rm call to system: " << info << std::endl;
}
|