ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tevol_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  * 2011-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 APP_DMRG_TEVOL_SIM_H
28 #define APP_DMRG_TEVOL_SIM_H
29 
30 #include <cmath>
31 #include <iterator>
32 #include <iostream>
33 #include <sys/stat.h>
34 
35 #include "dmrg/sim/sim.h"
36 #include "dmrg/evolve/te_utils.hpp"
37 
38 template <class Matrix, class SymmGroup, class TimeEvolver>
39 class tevol_sim : public sim<Matrix, SymmGroup> {
40 
42  typedef typename base::status_type status_type;
44 
45  using base::mps;
46  using base::mpo;
47  using base::lat;
48  using base::parms;
49  using base::model;
51  using base::stop_callback;
52  using base::init_sweep;
53  using base::rfile;
54 
55 public:
56  tevol_sim(DmrgParameters const & parms_, bool write_xml_)
57  : base(parms_)
58  , write_xml(write_xml_)
59  {
60  alps::oxstream out(boost::replace_last_copy(rfile, ".h5", ".xml"));
61  out << alps::header("UTF-8") << alps::stylesheet(alps::xslt_path("ALPS.xsl"));
62  out << alps::start_tag("SIMULATION") << alps::xml_namespace("xsi","http://www.w3.org/2001/XMLSchema-instance")
63  << alps::attribute("xsi:noNamespaceSchemaLocation","http://xml.comp-phys.org/2003/10/ALPS.xsd");
64  out << parms;
65  out << alps::end_tag("SIMULATION");
66  }
67 
68  void run()
69  {
70  int meas_each = parms["measure_each"];
71  int chkp_each = parms["chkp_each"];
72  int update_each = parms["update_each"];
73  int nsweeps = parms["nsweeps"];
74  int nsweeps_img = parms["nsweeps_img"];
75 
77 
78  /// compute nsteps as the min of the three above
79  int nsteps = parms["nsweeps"];
80  if (meas_each > -1)
81  nsteps = std::min(nsteps, meas_each);
82  if (chkp_each > -1)
83  nsteps = std::min(nsteps, chkp_each);
84  if (update_each > -1)
85  nsteps = std::min(nsteps, update_each);
86 
87  #define CHECK_MULTIPLICITY(var) \
88  if (var > 0 && var % nsteps != 0) \
89  throw std::runtime_error("var must be a multiple of 'nsteps'.");
90  CHECK_MULTIPLICITY(nsweeps)
91  CHECK_MULTIPLICITY(nsweeps_img)
92  CHECK_MULTIPLICITY(meas_each)
93  CHECK_MULTIPLICITY(chkp_each)
94  CHECK_MULTIPLICITY(update_each)
95  #undef CHECK_MULTIPLICITY
96 
97  TimeEvolver evolver(&parms, &mps, lat, model, init_sweep);
98 
99  int n = nsweeps / nsteps;
100  for (int i=init_sweep/nsteps; i < n; ++i) {
101  // TODO: introduce some timings
102 
103  int sweep = i*nsteps;
104  BaseParameters iteration_params = parms.iteration_params("Time", sweep);
105  if (update_each > -1 && (sweep % update_each) == 0)
106  {
107  if (iteration_params.size() > 1) { // Time will always be set
108  parms << iteration_params;
109  meas_each = parms["measure_each"];
110  chkp_each = parms["chkp_each"];
111  update_each = parms["update_each"];
112  model.update(parms);
113  mpo = make_mpo(lat, model, parms);
114  evolver = TimeEvolver(&parms, &mps, lat, model, sweep);
115  }
116  } else if (sweep == nsweeps_img) {
117  // since this is just a change in the time step, there is
118  // no need to split the hamiltonian in non-overlapping terms.
119  evolver.prepare_te_terms(sweep);
120  }
121 
122  /// time evolution
123  evolver(sweep, nsteps);
124  sweep = (i+1)*nsteps - 1;
125  iteration_params.set("Time", sweep);
126 
127  /// measurements
128  if ((sweep+1) % meas_each == 0 || (sweep+1) == parms["nsweeps"])
129  {
130  /// measure energy
131  double energy = maquis::real(expval(mps, mpo));
132  maquis::cout << "Energy " << energy << std::endl;
133 
134  /// measure observables specified in 'always_measure'
135  measurements_type always_measure = this->iteration_measurements(sweep); // todo: make measure() using const&
136  if (!parms["ALWAYS_MEASURE"].empty())
137  this->measure(this->results_archive_path(sweep) + "/results/", always_measure);
138 
139  /// write iteration results
140  {
141  storage::archive ar(rfile, "w");
142  ar[this->results_archive_path(sweep) + "/parameters"] << iteration_params;
143  ar[this->results_archive_path(sweep) + "/results"] << evolver.iteration_results();
144  ar[this->results_archive_path(sweep) + "/results/Energy/mean/value"] << std::vector<double>(1, energy);
145  // ar[this->results_archive_path(sweep) + "/results/Runtime/mean/value"] << std::vector<double>(1, elapsed_sweep + elapsed_measure);
146  }
147  }
148 
149  /// write checkpoint
150  bool stopped = stop_callback();
151  if (stopped || (sweep+1) % chkp_each == 0 || (sweep+1) == parms["nsweeps"])
152  checkpoint_simulation(mps, sweep);
153 
154  if (stopped) break;
155  }
156  }
157 
158 private:
159  using base::results_archive_path; // The following function is an overload, not the virtual function
160  std::string results_archive_path(int sweep) const
161  {
162  status_type status;
163  status["sweep"] = sweep;
164  return base::results_archive_path(status);
165  }
166 
167  using base::checkpoint_simulation; // The following function is an overload, not the virtual function
168  void checkpoint_simulation(MPS<Matrix, SymmGroup> const& state, int sweep)
169  {
170  status_type status;
171  status["sweep"] = sweep;
172  status["site"] = -1;
173  return base::checkpoint_simulation(state, status);
174  }
175 
176  bool write_xml;
177 };
178 
179 #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
MPO< Matrix, SymmGroup > make_mpo(Lattice const &lat, Model< Matrix, SymmGroup > const &model, BaseParameters &parms)
DmrgParameters parms
Definition: sim.h:89
double expval(MPS< Matrix, SymmGroup > const &mps, MPO< Matrix, SymmGroup > const &mpo, int d)
Definition: mps_mpo_ops.h:72
void run()
Definition: tevol_sim.hpp:68
MPS< Matrix, SymmGroup > mps
Definition: sim.h:101
measurements_type iteration_measurements(int sweep)
Definition: sim.hpp:112
Definition: sim.h:69
utilities for the preparation of time evolution operators
void set(std::string const &key, T const &value)
Model< Matrix, SymmGroup > model
Definition: sim.h:100
time_stopper stop_callback
Definition: sim.h:97
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
BaseParameters iteration_params(std::string const &var, std::size_t val)
tevol_sim(DmrgParameters const &parms_, bool write_xml_)
Definition: tevol_sim.hpp:56
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
#define CHECK_MULTIPLICITY(var)
Lattice lat
Definition: sim.h:99
measurements_type all_measurements
Definition: sim.h:103
MPO< Matrix, SymmGroup > mpo
Definition: sim.h:102
alps::numeric::real_type< T >::type real(T f)
Definition: bindings.hpp:38
declaration of simulation class