v2DM-DOCI  1.0
doci2.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 <chrono>
26 #include <getopt.h>
27 #include <mpi.h>
28 
29 #include "include.h"
30 #include "BoundaryPoint.h"
31 #include "PotentialReducation.h"
32 // from CheMPS2
33 #include "Hamiltonian.h"
34 
35 #include "SimulatedAnnealing.h"
36 
37 int main(int argc,char **argv)
38 {
39  using std::cout;
40  using std::endl;
41  using namespace doci2DM;
43 
44  cout.precision(10);
45 
46  std::string integralsfile = "mo-integrals.h5";
47  bool bp = false;
48  bool pr = false;
49  bool read_startpoint = false;
50  std::string startinput;
51 
52  struct option long_options[] =
53  {
54  {"integrals", required_argument, 0, 'i'},
55  {"start", required_argument, 0, 's'},
56  {"boundary-point", no_argument, 0, 'b'},
57  {"potential-reduction", no_argument, 0, 'p'},
58  {"help", no_argument, 0, 'h'},
59  {0, 0, 0, 0}
60  };
61 
62  int i,j;
63 
64  while( (j = getopt_long (argc, argv, "hi:bps:", long_options, &i)) != -1)
65  switch(j)
66  {
67  case 'h':
68  case '?':
69  cout << "Usage: " << argv[0] << " [OPTIONS]\n"
70  "\n"
71  " -i, --integrals=integrals-file Set the input integrals file\n"
72  " -b, --boundary-point Use the boundary point method as solver (default)\n"
73  " -p, --potential-reduction Use the potential reduction method as solver\n"
74  " -h, --help Display this help\n"
75  "\n";
76  return 0;
77  break;
78  case 'i':
79  integralsfile = optarg;
80  break;
81  case 's':
82  startinput = optarg;
83  read_startpoint = true;
84  break;
85  case 'b':
86  bp = true;
87  break;
88  case 'p':
89  pr = true;
90  break;
91  }
92 
93  if(!bp && !pr)
94  {
95  std::cout << "Tell me what to do..." << std::endl;
96  return 0;
97  }
98 
99  cout << "Reading: " << integralsfile << endl;
100 
101  // make sure we have a save path, even if it's not specify already
102  // This will not overwrite an already set SAVE_H5_PATH
103  setenv("SAVE_H5_PATH", "./", 0);
104 
105  cout << "Using save path: " << getenv("SAVE_H5_PATH") << endl;
106 
107  MPI_Init(&argc,&argv);
108 
109  SimulatedAnnealing opt(CheMPS2::Hamiltonian::CreateFromH5(integralsfile));
110 
111  if(bp)
112  opt.UseBoundaryPoint();
113  else if(pr)
114  opt.UsePotentialReduction();
115 
116  auto& ham = opt.getHam();
117 
118  const auto L = ham.getL(); //dim sp hilbert space
119  const auto N = ham.getNe(); //nr of particles
120 
121  cout << "Starting with L=" << L << " N=" << N << endl;
122  auto start = std::chrono::high_resolution_clock::now();
123 
124  opt.Set_start_temp(0.1);
125  opt.Set_delta_temp(0.99);
126  opt.Set_max_angle(1.3);
127  opt.Set_delta_angle(0.999);
128 
129 // opt.get_Optimal_Unitary().loadU("uni-test.h5");
130 // opt.calc_new_energy();
131 
132  if(bp)
133  try {
134  opt.getMethod_BP().set_use_prev_result(true);
135  } catch (const std::bad_cast err)
136  {
137  std::cerr << "Shit just hit the fan!\t" << err.what() << std::endl;
138  }
139 
140  if(! startinput.empty())
141  opt.getMethod().getRDM().ReadFromFile(startinput.c_str());
142  else
143  opt.calc_energy();
144 
145  opt.optimize_mpi();
146 
147  auto end = std::chrono::high_resolution_clock::now();
148 
149  cout << "Total Runtime: " << std::fixed << std::chrono::duration_cast<std::chrono::duration<double,std::ratio<1>>>(end-start).count() << " s" << endl;
150 
151  int rank;
152  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
153  if(rank == 0)
154  {
155  std::stringstream h5_name1;
156  h5_name1 << getenv("SAVE_H5_PATH") << "/rdm.h5";
157  opt.getMethod().getRDM().WriteToFile(h5_name1.str().c_str());
158 
159  std::stringstream h5_name2;
160  h5_name2 << getenv("SAVE_H5_PATH") << "/optimale-uni.h5";
161  opt.get_Optimal_Unitary().saveU(h5_name2.str());
162  }
163 
164  MPI_Finalize();
165 
166  return 0;
167 }
168 
169 /* vim: set ts=3 sw=3 expandtab :*/
static Hamiltonian CreateFromH5(const string filename)
int main(int argc, char **argv)
Definition: doci2.cpp:37