CheMPS2
DMRGmpsio.cpp
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 #include <iostream>
21 #include <stdlib.h>
22 #include <sstream>
23 #include <string>
24 
25 #include "DMRG.h"
26 
27 void CheMPS2::DMRG::saveMPS(const std::string name, TensorT ** MPSlocation, SyBookkeeper * BKlocation, bool isConverged) const{
28 
29  //The hdf5 file
30  hid_t file_id = H5Fcreate(name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
31 
32  //Whether the MPS was converged or not
33  hid_t group_id = H5Gcreate(file_id, "/Convergence", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
34 
35  hsize_t dimarray = 1; //One integer
36  hid_t dataspace_id = H5Screate_simple(1, &dimarray, NULL);
37  hid_t dataset_id = H5Dcreate(group_id, "Converged_yn", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
38  int toWrite = (isConverged)?1:0;
39  H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toWrite);
40 
41  H5Dclose(dataset_id);
42  H5Sclose(dataspace_id);
43 
44  H5Gclose(group_id);
45 
46  //The current virtual dimensions
47  for (int bound=0; bound<=BKlocation->gL(); bound++){
48  for (int N=BKlocation->gNmin(bound); N<=BKlocation->gNmax(bound); N++){
49  for (int TwoS=BKlocation->gTwoSmin(bound,N); TwoS<=BKlocation->gTwoSmax(bound,N); TwoS+=2){
50  for (int Irrep=0; Irrep<BKlocation->getNumberOfIrreps(); Irrep++){
51 
52  std::stringstream sstream;
53  sstream << "/VirtDim_" << bound << "_" << N << "_" << TwoS << "_" << Irrep;
54  hid_t group_id2 = H5Gcreate(file_id, sstream.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
55 
56  hsize_t dimarray2 = 1; //One integer
57  hid_t dataspace_id2 = H5Screate_simple(1, &dimarray2, NULL);
58  hid_t dataset_id2 = H5Dcreate(group_id2, "Value", H5T_STD_I32LE, dataspace_id2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
59  int toWrite2 = BKlocation->gCurrentDim(bound,N,TwoS,Irrep);
60  H5Dwrite(dataset_id2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toWrite2);
61 
62  H5Dclose(dataset_id2);
63  H5Sclose(dataspace_id2);
64 
65  H5Gclose(group_id2);
66 
67  }
68  }
69  }
70  }
71 
72  //The MPS
73  for (int site=0; site<BKlocation->gL(); site++){
74 
75  std::stringstream sstream;
76  sstream << "/MPS_" << site;
77  hid_t group_id3 = H5Gcreate(file_id, sstream.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
78 
79  hsize_t dimarray3 = MPSlocation[site]->gKappa2index(MPSlocation[site]->gNKappa()); //An array of doubles
80  hid_t dataspace_id3 = H5Screate_simple(1, &dimarray3, NULL);
81  hid_t dataset_id3 = H5Dcreate(group_id3, "Values", H5T_IEEE_F64LE, dataspace_id3, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
82  H5Dwrite(dataset_id3, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, MPSlocation[site]->gStorage());
83 
84  H5Dclose(dataset_id3);
85  H5Sclose(dataspace_id3);
86 
87  H5Gclose(group_id3);
88 
89  }
90 
91 
92  H5Fclose(file_id);
93 
94 }
95 
96 void CheMPS2::DMRG::loadDIM(const std::string name, SyBookkeeper * BKlocation){
97 
98  //The hdf5 file
99  hid_t file_id = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
100 
101  //The current virtual dimensions
102  for (int bound=0; bound<=BKlocation->gL(); bound++){
103  for (int N=BKlocation->gNmin(bound); N<=BKlocation->gNmax(bound); N++){
104  for (int TwoS=BKlocation->gTwoSmin(bound,N); TwoS<=BKlocation->gTwoSmax(bound,N); TwoS+=2){
105  for (int Irrep=0; Irrep<BKlocation->getNumberOfIrreps(); Irrep++){
106 
107  std::stringstream sstream;
108  sstream << "/VirtDim_" << bound << "_" << N << "_" << TwoS << "_" << Irrep;
109  hid_t group_id2 = H5Gopen(file_id, sstream.str().c_str(), H5P_DEFAULT);
110 
111  hid_t dataset_id2 = H5Dopen(group_id2, "Value", H5P_DEFAULT);
112  int toRead;
113  H5Dread(dataset_id2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toRead);
114  BKlocation->SetDim(bound, N, TwoS, Irrep, toRead);
115  H5Dclose(dataset_id2);
116 
117  H5Gclose(group_id2);
118 
119  }
120  }
121  }
122  }
123 
124  H5Fclose(file_id);
125 
126 }
127 
128 void CheMPS2::DMRG::loadMPS(const std::string name, TensorT ** MPSlocation, bool * isConverged){
129 
130  //The hdf5 file
131  hid_t file_id = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
132 
133  //Whether the MPS was converged or not
134  hid_t group_id = H5Gopen(file_id, "/Convergence", H5P_DEFAULT);
135 
136  hid_t dataset_id = H5Dopen(group_id, "Converged_yn", H5P_DEFAULT);
137  int toRead;
138  H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &toRead);
139  isConverged[0] = (toRead==0)?false:true;
140  H5Dclose(dataset_id);
141 
142  H5Gclose(group_id);
143 
144  //The MPS
145  for (int site=0; site<L; site++){
146 
147  std::stringstream sstream;
148  sstream << "/MPS_" << site;
149  hid_t group_id3 = H5Gopen(file_id, sstream.str().c_str(), H5P_DEFAULT);
150 
151  hid_t dataset_id3 = H5Dopen(group_id3, "Values", H5P_DEFAULT);
152  H5Dread(dataset_id3, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, MPSlocation[site]->gStorage());
153  H5Dclose(dataset_id3);
154 
155  H5Gclose(group_id3);
156 
157  }
158 
159 
160  H5Fclose(file_id);
161 
162 }
163 
165 
166  std::stringstream thestream;
167  thestream << "rm " << CheMPS2::DMRG_MPS_storage_prefix << "*.h5";
168  int info = system(thestream.str().c_str());
169  std::cout << "Info on DMRG::MPS rm call to system: " << info << std::endl;
170 
171 }
172 
173 
void deleteStoredMPS()
Call "rm " + CheMPS2::DMRG_MPS_storage_prefix + "*.h5".
Definition: DMRGmpsio.cpp:164