ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dmrg_sim.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2013-2013 by Michele Dolfi <dolfim@phys.ethz.ch>
7  *
8  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef ALPS_MPS_OPTIM_DMRG_SIM_HPP
28 #define ALPS_MPS_OPTIM_DMRG_SIM_HPP
29 
30 #include <cmath>
31 #include <iterator>
32 #include <iostream>
33 
34 #include <boost/shared_ptr.hpp>
35 
36 #include "dmrg/sim/sim.h"
37 #include "dmrg/optimize/optimize.h"
38 #include <alps/parser/xmlstream.h>
39 
40 template <class Matrix, class SymmGroup>
41 class dmrg_sim : public sim<Matrix, SymmGroup> {
42 
45  typedef typename base::status_type status_type;
47 
48  using base::mps;
49  using base::mpo;
50  using base::parms;
52  using base::stop_callback;
53  using base::init_sweep;
54  using base::init_site;
55  using base::rfile;
56 
57 public:
58 
60  : base(parms_)
61  { }
62 
63  void run()
64  {
65  int meas_each = parms["measure_each"];
66  int chkp_each = parms["chkp_each"];
67 
68  /// MPO creation
70  if (parms["use_compressed"])
71  mpoc.compress(1e-12);
72 
73  /// Optimizer initialization
74  boost::shared_ptr<opt_base_t> optimizer;
75  if (init_sweep < parms["nsweeps"]) {
76  if (parms["optimization"] == "singlesite")
78  (mps, mpoc, parms, stop_callback, init_site) );
79  else if(parms["optimization"] == "twosite")
81  (mps, mpoc, parms, stop_callback, init_site) );
82  else
83  throw std::runtime_error("Don't know this optimizer");
84  }
85 
86  measurements_type always_measurements = this->iteration_measurements(init_sweep);
87 
88  try {
89  bool stopped = false;
90  for (int sweep=init_sweep; sweep < parms["nsweeps"]; ++sweep) {
91  // TODO: introduce some timings
92 
93  optimizer->sweep(sweep, Both);
95 
96  if ((sweep+1) % meas_each == 0 || (sweep+1) == parms["nsweeps"])
97  {
98  /// write iteration results
99  {
100  storage::archive ar(rfile, "w");
101  ar[results_archive_path(sweep) + "/parameters"] << parms;
102  ar[results_archive_path(sweep) + "/results"] << optimizer->iteration_results();
103  // ar[results_archive_path(sweep) + "/results/Runtime/mean/value"] << std::vector<double>(1, elapsed_sweep + elapsed_measure);
104  }
105 
106  /// measure observables specified in 'always_measure'
107  if (always_measurements.size() > 0)
108  this->measure(this->results_archive_path(sweep) + "/results/", always_measurements);
109  }
110 
111  /// write checkpoint
112  stopped = stop_callback();
113  if (stopped || (sweep+1) % chkp_each == 0 || (sweep+1) == parms["nsweeps"])
114  checkpoint_simulation(mps, sweep, -1);
115 
116  if (stopped) break;
117  }
118  } catch (dmrg::time_limit const& e) {
119  maquis::cout << e.what() << " checkpointing partial result." << std::endl;
120  checkpoint_simulation(mps, e.sweep(), e.site());
121 
122  {
123  storage::archive ar(rfile, "w");
124  ar[results_archive_path(e.sweep()) + "/parameters"] << parms;
125  ar[results_archive_path(e.sweep()) + "/results"] << optimizer->iteration_results();
126  // ar[results_archive_path(e.sweep()) + "/results/Runtime/mean/value"] << std::vector<double>(1, elapsed_sweep + elapsed_measure);
127  }
128  throw e;
129  }
130  }
131 
133  {
135  }
136 
137 private:
138  using base::results_archive_path; // The following function is an overload, not the virtual function
139  std::string results_archive_path(int sweep) const
140  {
141  status_type status;
142  status["sweep"] = sweep;
143  return base::results_archive_path(status);
144  }
145 
146  using base::checkpoint_simulation; // The following function is an overload, not the virtual function
147  void checkpoint_simulation(MPS<Matrix, SymmGroup> const& state, int sweep, int site)
148  {
149  status_type status;
150  status["sweep"] = sweep;
151  status["site"] = site;
152  return base::checkpoint_simulation(state, status);
153  }
154 };
155 
156 
157 #endif
Model< Matrix, SymmGroup >::measurements_type measurements_type
Definition: sim.h:77
virtual void measure(std::string archive_path, measurements_type &meas)
Definition: sim.hpp:161
DmrgParameters parms
Definition: sim.h:89
MPS< Matrix, SymmGroup > mps
Definition: sim.h:101
Definition: optimize.h:84
measurements_type iteration_measurements(int sweep)
Definition: sim.hpp:112
Definition: sim.h:69
void run()
Definition: dmrg_sim.hpp:63
time_stopper stop_callback
Definition: sim.h:97
Definition: mpo.h:36
std::map< std::string, int > status_type
Definition: sim.h:78
virtual void checkpoint_simulation(MPS< Matrix, SymmGroup > const &state, status_type const &)
Definition: sim.hpp:131
dmrg_sim(DmrgParameters &parms_)
Definition: dmrg_sim.hpp:59
optimization base class
virtual std::string results_archive_path(status_type const &) const
Definition: sim.hpp:147
Definition: mps.h:40
std::string rfile
Definition: sim.h:95
int init_sweep
Definition: sim.h:91
static void sync()
Definition: storage.h:245
int init_site
Definition: sim.h:91
measurements_type all_measurements
Definition: sim.h:103
MPO< Matrix, SymmGroup > mpo
Definition: sim.h:102
declaration of simulation class
MPO< Matrix, SymmGroup > mpoc
Definition: sim.h:102