v2DM-DOCI  1.0
Tools.cpp
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Copyright (C) 2014-2015 Ward Poelmans
5  *
6  * This file is part of v2DM-DOCI.
7  *
8  * v2DM-DOCI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Foobar is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @END LICENSE
22  */
23 
24 #include <fstream>
25 #include <hdf5.h>
26 #include "include.h"
27 
28 #include "PotentialReducation.h"
29 #include "BoundaryPoint.h"
30 #include "Hamiltonian.h"
31 
32 using namespace doci2DM;
33 
34 int Tools::getspDimension(std::string filename)
35 {
36  hid_t file_id, group_id, attribute_id;
37  herr_t status;
38  int spdim = 0; // to avoid uninitalized errors
39 
40  file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
41  HDF5_STATUS_CHECK(file_id);
42 
43  group_id = H5Gopen(file_id, "/integrals", H5P_DEFAULT);
44  HDF5_STATUS_CHECK(group_id);
45 
46  attribute_id = H5Aopen(group_id, "sp_dim", H5P_DEFAULT);
47  HDF5_STATUS_CHECK(attribute_id);
48 
49  status = H5Aread(attribute_id, H5T_NATIVE_INT, &spdim);
50  HDF5_STATUS_CHECK(status);
51 
52  status = H5Aclose(attribute_id);
53  HDF5_STATUS_CHECK(status);
54 
55  return spdim;
56 }
57 
58 int Tools::getNumberOfParticles(std::string filename)
59 {
60  hid_t file_id, group_id, attribute_id;
61  herr_t status;
62  int nelectrons = 0; // to avoid uninitalized errors
63 
64  file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
65  HDF5_STATUS_CHECK(file_id);
66 
67  group_id = H5Gopen(file_id, "/Data", H5P_DEFAULT);
68  HDF5_STATUS_CHECK(group_id);
69 
70  attribute_id = H5Dopen(group_id, "nelectrons", H5P_DEFAULT);
71  HDF5_STATUS_CHECK(attribute_id);
72 
73  status = H5Dread(attribute_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nelectrons);
74  HDF5_STATUS_CHECK(status);
75 
76  status = H5Dclose(attribute_id);
77  HDF5_STATUS_CHECK(status);
78 
79  return nelectrons;
80 }
81 
82 double Tools::getNuclearRepulEnergy(std::string filename)
83 {
84  hid_t file_id, group_id, attribute_id;
85  herr_t status;
86  double nuclrep = 0; // to avoid uninitalized errors
87 
88  file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
89  HDF5_STATUS_CHECK(file_id);
90 
91  group_id = H5Gopen(file_id, "/integrals", H5P_DEFAULT);
92  HDF5_STATUS_CHECK(group_id);
93 
94  attribute_id = H5Aopen(group_id, "nuclear_repulsion_energy", H5P_DEFAULT);
95  HDF5_STATUS_CHECK(attribute_id);
96 
97  status = H5Aread(attribute_id, H5T_NATIVE_DOUBLE, &nuclrep);
98  HDF5_STATUS_CHECK(status);
99 
100  status = H5Aclose(attribute_id);
101  HDF5_STATUS_CHECK(status);
102 
103  return nuclrep;
104 }
105 
106 
107 void Tools::scan_all(const TPM &rdm, const CheMPS2::Hamiltonian &ham)
108 {
109  const int L = rdm.gL();
110 
111  std::function<double(int,int)> getT = [&ham] (int a, int b) -> double { return ham.getTmat(a,b); };
112  std::function<double(int,int,int,int)> getV = [&ham] (int a, int b, int c, int d) -> double { return ham.getVmat(a,b,c,d); };
113 
114  PotentialReduction mymethod(ham);
115 
116  auto orig_ham = mymethod.getHam();
117 
118  for(int k_in=0;k_in<L;k_in++)
119  for(int l_in=k_in+1;l_in<L;l_in++)
120  if(ham.getOrbitalIrrep(k_in) == ham.getOrbitalIrrep(l_in))
121  {
122  std::fstream fs;
123  std::string filename = getenv("SAVE_H5_PATH");
124  filename += "/orbs-scan-" + std::to_string(k_in) + "-" + std::to_string(l_in) + ".txt";
125  fs.open(filename, std::fstream::out | std::fstream::trunc);
126 
127  fs.precision(10);
128 
129  fs << "# theta\trot\trot+v2dm" << std::endl;
130 
131  auto found = rdm.find_min_angle(k_in,l_in,0.3,getT,getV);
132 
133  std::cout << "Min:\t" << k_in << "\t" << l_in << "\t" << found.first << "\t" << found.second << std::endl;
134 
135  std::cout << "######################" << std::endl;
136 
137 
138  int Na = 100;
139 //#pragma omp parallel for
140  for(int a=0;a<=Na;a++)
141  {
142  double theta = 1.0*M_PI/(1.0*Na) * a - M_PI/2.0;
143 
144  mymethod.getHam() = orig_ham;
145  mymethod.getRDM() = rdm;
146  mymethod.getHam().rotate(k_in, l_in, theta, getT, getV);
147 
148  double new_en = mymethod.evalEnergy();
149 
150  mymethod.Run();
151 
152 //#pragma omp critical
153  fs << theta << "\t" << new_en << "\t" << mymethod.evalEnergy() << std::endl;
154  }
155 
156  fs.close();
157  }
158 }
159 
160 void Tools::scan_all_bp(const TPM &rdm, const CheMPS2::Hamiltonian &ham)
161 {
162  const int L = rdm.gL();
163 
164  std::function<double(int,int)> getT = [&ham] (int a, int b) -> double { return ham.getTmat(a,b); };
165  std::function<double(int,int,int,int)> getV = [&ham] (int a, int b, int c, int d) -> double { return ham.getVmat(a,b,c,d); };
166 
167  BoundaryPoint method(ham);
168 
169  const auto orig_ham = method.getHam();
170 
171  for(int k_in=0;k_in<L;k_in++)
172  for(int l_in=k_in+1;l_in<L;l_in++)
173  if(ham.getOrbitalIrrep(k_in) == ham.getOrbitalIrrep(l_in))
174  {
175  std::fstream fs;
176  std::string filename = getenv("SAVE_H5_PATH");
177  filename += "/orbs-scan-" + std::to_string(k_in) + "-" + std::to_string(l_in) + ".txt";
178  fs.open(filename, std::fstream::out | std::fstream::trunc);
179 
180  fs.precision(10);
181 
182  fs << "# theta\trot\trot+v2dm" << std::endl;
183 
184  auto found = rdm.find_min_angle(k_in,l_in,0.3,getT,getV);
185 
186  std::cout << "Min:\t" << k_in << "\t" << l_in << "\t" << found.first << "\t" << found.second << std::endl;
187 
188  std::cout << "######################" << std::endl;
189 
190 
191  int Na = 100;
192 #pragma omp parallel for
193  for(int a=0;a<=Na;a++)
194  {
195  double theta = 1.0*M_PI/(1.0*Na) * a - M_PI/2.0;
196 
197  BoundaryPoint mymethod(ham);
198  mymethod.set_tol_PD(1e-7);
199 
200 // mymethod.set_output(false);
201 
202  mymethod.getHam() = orig_ham;
203  mymethod.getRDM() = rdm;
204  mymethod.getHam().rotate(k_in, l_in, theta, getT, getV);
205 
206  double new_en = mymethod.evalEnergy();
207 
208  mymethod.Run();
209 
210 #pragma omp critical
211  fs << theta << "\t" << new_en << "\t" << mymethod.evalEnergy() << std::endl;
212  }
213 
214  fs.close();
215  }
216 }
217 
218 
219 /* vim: set ts=3 sw=3 expandtab :*/
static void scan_all(const TPM &rdm, const CheMPS2::Hamiltonian &ham)
Definition: Tools.cpp:107
int getOrbitalIrrep(const int nOrb) const
Get an orbital irrep number.
double getVmat(const int index1, const int index2, const int index3, const int index4) const
Get a Vmat element.
void rotate(int, int, double, std::function< double(int, int)> &, std::function< double(int, int, int, int)> &)
Definition: TPM.cpp:1684
double getTmat(const int index1, const int index2) const
Get a Tmat element.
std::pair< double, bool > find_min_angle(int k, int l, double start_angle, std::function< double(int, int)> &T, std::function< double(int, int, int, int)> &V) const
Definition: TPM.cpp:1577
static int getNumberOfParticles(std::string filename)
Definition: Tools.cpp:58
#define HDF5_STATUS_CHECK(status)
Definition: helpers.cpp:30
static int getspDimension(std::string filename)
Definition: Tools.cpp:34
static void scan_all_bp(const TPM &rdm, const CheMPS2::Hamiltonian &ham)
Definition: Tools.cpp:160
double evalEnergy() const
static double getNuclearRepulEnergy(std::string filename)
Definition: Tools.cpp:82
int gL() const
Definition: TPM.cpp:115