v2DM-DOCI  1.0
doci_sdp.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 <iostream>
25 #include <getopt.h>
26 #include <cassert>
27 #include <signal.h>
28 
29 #include "include.h"
30 #include "PotentialReducation.h"
31 #include "LocalMinimizer.h"
32 
33 // from CheMPS2
34 #include "Hamiltonian.h"
35 
36 // if set, the signal has been given to stop the calculation and write current step to file
37 sig_atomic_t stopping = 0;
38 sig_atomic_t stopping_min = 0;
39 
40 void stopcalcsignal(int sig);
41 void stopminsignal(int sig);
42 
43 
44 int main(int argc, char **argv)
45 {
46  using std::cout;
47  using std::endl;
48  using namespace doci2DM;
50 
51  cout.precision(10);
52 
53  std::string integralsfile = "mo-integrals.h5";
54  std::string unitary;
55  bool random = false;
56  bool localmini = false;
57  bool scan = false;
58 
59  struct option long_options[] =
60  {
61  {"integrals", required_argument, 0, 'i'},
62  {"unitary", required_argument, 0, 'u'},
63  {"random", no_argument, 0, 'r'},
64  {"scan", no_argument, 0, 's'},
65  {"local-minimizer", no_argument, 0, 'l'},
66  {"help", no_argument, 0, 'h'},
67  {0, 0, 0, 0}
68  };
69 
70  int i,j;
71 
72  while( (j = getopt_long (argc, argv, "d:rlhi:u:s", long_options, &i)) != -1)
73  switch(j)
74  {
75  case 'h':
76  case '?':
77  cout << "Usage: " << argv[0] << " [OPTIONS]\n"
78  "\n"
79  " -i, --integrals=integrals-file Set the input integrals file\n"
80  " -u, --unitary=unitary-file Use the unitary matrix in this file\n"
81  " -r, --random Perform a random unitary transformation on the Hamiltonian\n"
82  " -l, --local-minimizer Use the local minimizer\n"
83  " -h, --help Display this help\n"
84  "\n";
85  return 0;
86  break;
87  case 'i':
88  integralsfile = optarg;
89  break;
90  case 'u':
91  unitary = optarg;
92  break;
93  case 'r':
94  random = true;
95  break;
96  case 'l':
97  localmini = true;
98  break;
99  case 's':
100  scan = true;
101  break;
102  }
103 
104  cout << "Reading: " << integralsfile << endl;
105 
106  // make sure we have a save path, even if it's not specify already
107  // This will not overwrite an already set SAVE_H5_PATH
108  setenv("SAVE_H5_PATH", "./", 0);
109 
110  cout << "Using save path: " << getenv("SAVE_H5_PATH") << endl;
111 
112  auto ham = CheMPS2::Hamiltonian::CreateFromH5(integralsfile);
113 
114  const auto L = ham.getL(); //nr of particles
115  const auto N = ham.getNe(); //nr of particles
116 
117  cout << "Starting with L=" << L << " N=" << N << endl;
118 
119  if(!unitary.empty() && !localmini)
120  {
121  cout << "Reading transform: " << unitary << endl;
122 
123  simanneal::OrbitalTransform orbtrans(ham);
124 
125  orbtrans.get_unitary().loadU(unitary);
126  orbtrans.fillHamCI(ham);
127  }
128 
129  const simanneal::OptIndex opt(ham);
130 
131  if(random)
132  {
134  X.fill_random();
136  X.print_unitary();
137  simanneal::OrbitalTransform orbtrans(ham);
138  orbtrans.update_unitary(X, false);
139  std::string filename = getenv("SAVE_H5_PATH");
140  filename += "/random-start-unitary.h5";
141  orbtrans.get_unitary().saveU(filename);
142  orbtrans.fillHamCI(ham);
143  }
144 
145  PotentialReduction method(ham);
146 
147  // set up everything to handle SIGALRM
148  struct sigaction act;
149  act.sa_flags = 0;
150  act.sa_handler = &stopcalcsignal;
151 
152  sigset_t blockset;
153  sigemptyset(&blockset); // we don't block anything in the handler
154  act.sa_mask = blockset;
155 
156  sigaction(SIGALRM, &act, 0);
157 
158  act.sa_handler = &stopminsignal;
159 
160  sigaction(SIGUSR1, &act, 0);
161 
162  if(localmini)
163  {
164  LocalMinimizer minimize(ham);
165 
166  if(!unitary.empty())
167  {
168  cout << "Starting local minimizer from: " << unitary << endl;
169  minimize.getOrbitalTf().get_unitary().loadU(unitary);
170  }
171 
172  minimize.UsePotentialReduction();
173 
174  minimize.set_conv_crit(1e-6);
175 
176  minimize.Minimize();
177 
178  cout << "Bottom is " << minimize.get_energy() << endl;
179 
180  method = minimize.getMethod_PR();
181  ham = minimize.getHam();
182  } else
183  method.Run();
184 
185  cout << "The optimal energy is " << method.evalEnergy() << std::endl;
186 
187  if(scan)
188  Tools::scan_all(method.getRDM(), ham);
189 
190  std::string h5_name = getenv("SAVE_H5_PATH");
191  h5_name += "/optimal-rdm.h5";
192 
193  method.getRDM().WriteToFile(h5_name);
194 
195  h5_name = getenv("SAVE_H5_PATH");
196  h5_name += "/optimal-ham.h5";
197 
198  ham.save2(h5_name);
199 
200  return 0;
201 }
202 
203 void stopcalcsignal(int sig)
204 {
205  stopping=1;
206 }
207 
208 void stopminsignal(int sig)
209 {
210  stopping_min=1;
211 }
212 
213 /* vim: set ts=3 sw=3 expandtab :*/
static Hamiltonian CreateFromH5(const string filename)
void update_unitary(double *step)
void stopcalcsignal(int sig)
Definition: doci_sdp.cpp:203
static void scan_all(const TPM &rdm, const CheMPS2::Hamiltonian &ham)
Definition: Tools.cpp:107
void fillHamCI(CheMPS2::Hamiltonian &HamCI)
void stopminsignal(int sig)
Definition: doci_sdp.cpp:208
sig_atomic_t stopping_min
Definition: doci_sdp.cpp:38
int main(int argc, char **argv)
Definition: doci_sdp.cpp:44
UnitaryMatrix & get_unitary()
void WriteToFile(hid_t &group_id) const
Definition: TPM.cpp:304
sig_atomic_t stopping
Definition: doci_sdp.cpp:37