ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 Bela Bauer <bauerb@phys.ethz.ch>
7  * Michele Dolfi <dolfim@phys.ethz.ch>
8  *
9  * This software is part of the ALPS Applications, published under the ALPS
10  * Application License; you can use, redistribute it and/or modify it under
11  * the terms of the license, either version 1 or (at your option) any later
12  * version.
13  *
14  * You should have received a copy of the ALPS Application License along with
15  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
16  * available from http://alps.comp-phys.org/.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  *****************************************************************************/
27 
28 #include <boost/algorithm/string.hpp>
29 
30 template <class Matrix, class SymmGroup>
32 : parms(parms_)
33 , init_sweep(0)
34 , init_site(-1)
35 , restore(false)
36 , dns( (parms["donotsave"] != 0) )
37 , chkpfile(boost::trim_right_copy_if(parms["chkpfile"].str(), boost::is_any_of("/ ")))
38 , rfile(parms["resultfile"].str())
39 , stop_callback(static_cast<double>(parms["run_seconds"]))
40 {
41  maquis::cout << DMRG_VERSION_STRING << std::endl;
42  storage::setup(parms);
43  dmrg_random::engine.seed(parms["seed"]);
44 
45  {
46  boost::filesystem::path p(chkpfile);
47  if (boost::filesystem::exists(p) && boost::filesystem::exists(p / "mps0.h5"))
48  {
49  storage::archive ar_in(chkpfile+"/props.h5");
50  if (ar_in.is_scalar("/status/sweep"))
51  {
52  ar_in["/status/sweep"] >> init_sweep;
53 
54  if (ar_in.is_data("/status/site") && ar_in.is_scalar("/status/site"))
55  ar_in["/status/site"] >> init_site;
56 
57  if (init_site == -1)
58  ++init_sweep;
59 
60  maquis::cout << "Restoring state." << std::endl;
61  maquis::cout << "Will start again at site " << init_site << " in sweep " << init_sweep << std::endl;
62  restore = true;
63  } else {
64  maquis::cout << "A fresh simulation will start." << std::endl;
65  }
66  }
67  }
68 
69 
70  {
71  storage::archive ar(rfile, "w");
72 
73  ar["/parameters"] << parms;
74  ar["/version"] << DMRG_VERSION_STRING;
75  }
76  if (!dns)
77  {
78  if (!boost::filesystem::exists(chkpfile))
79  boost::filesystem::create_directory(chkpfile);
80  storage::archive ar(chkpfile+"/props.h5", "w");
81 
82  ar["/parameters"] << parms;
83  ar["/version"] << DMRG_VERSION_STRING;
84  }
85 
86 
87  /// Model initialization
88  lat = Lattice(parms);
90  mpo = make_mpo(lat, model, parms);
91  all_measurements = model.measurements();
92  all_measurements << overlap_measurements<Matrix, SymmGroup>(parms);
93 
94 
95  /// MPS initialization
96  if (restore) {
97  load(chkpfile, mps);
98  } else if (!parms["initfile"].empty()) {
99  maquis::cout << "Loading init state from " << parms["initfile"] << std::endl;
100  load(parms["initfile"].str(), mps);
101  } else {
102  mps = MPS<Matrix, SymmGroup>(lat.size(), *(model.initializer(lat, parms)));
103  }
104 
105  assert(mps.length() == lat.size());
106  maquis::cout << "MPS initialization has finished...\n"; // MPS restored now
107 
108 }
109 
110 template <class Matrix, class SymmGroup>
113 {
114  measurements_type mymeas(all_measurements);
115  mymeas << overlap_measurements<Matrix, SymmGroup>(parms, sweep);
116 
117  measurements_type sweep_measurements;
118  if (!parms["ALWAYS_MEASURE"].empty())
119  sweep_measurements = meas_sublist(mymeas, parms["ALWAYS_MEASURE"]);
120 
121  return sweep_measurements;
122 }
123 
124 
125 template <class Matrix, class SymmGroup>
127 {
128 }
129 
130 template <class Matrix, class SymmGroup>
132 {
133  if (!dns) {
134  /// create chkp dir
135  if (!boost::filesystem::exists(chkpfile))
136  boost::filesystem::create_directory(chkpfile);
137  /// save state to chkp dir
138  save(chkpfile, state);
139 
140  /// save status
141  storage::archive ar(chkpfile+"/props.h5", "w");
142  ar["/status"] << status;
143  }
144 }
145 
146 template <class Matrix, class SymmGroup>
148 {
149  std::ostringstream oss;
150  oss.str("");
151 #if defined(__xlC__) || defined(__FCC_VERSION)
152  typename status_type::const_iterator match = status.find("sweep");
153  oss << "/spectrum/iteration/" << match->second;
154 #else
155  oss << "/spectrum/iteration/" << status.at("sweep");
156 #endif
157  return oss.str();
158 }
159 
160 template <class Matrix, class SymmGroup>
161 void sim<Matrix, SymmGroup>::measure(std::string archive_path, measurements_type & meas)
162 {
163  maquis::cout << "Measurements." << std::endl;
164  std::for_each(meas.begin(), meas.end(), measure_and_save<Matrix, SymmGroup>(rfile, archive_path, mps));
165 
166  // TODO: move into special measurement
167  std::vector<int> * measure_es_where = NULL;
168  entanglement_spectrum_type * spectra = NULL;
169  if (!parms["entanglement_spectra"].empty()) {
170  spectra = new entanglement_spectrum_type();
171  measure_es_where = new std::vector<int>();
172  *measure_es_where = parms.template get<std::vector<int> >("entanglement_spectra");
173  }
174  std::vector<double> entropies, renyi2;
175  if (parms["MEASURE[Entropy]"]) {
176  maquis::cout << "Calculating vN entropy." << std::endl;
177  entropies = calculate_bond_entropies(mps);
178  }
179  if (parms["MEASURE[Renyi2]"]) {
180  maquis::cout << "Calculating n=2 Renyi entropy." << std::endl;
181  renyi2 = calculate_bond_renyi_entropies(mps, 2, measure_es_where, spectra);
182  }
183 
184  {
185  storage::archive ar(rfile, "w");
186  if (entropies.size() > 0)
187  ar[archive_path + "/Entropy/mean/value"] << entropies;
188  if (renyi2.size() > 0)
189  ar[archive_path + "/Renyi2/mean/value"] << renyi2;
190  if (spectra != NULL)
191  ar[archive_path + "/Entanglement Spectra/mean/value"] << *spectra;
192  }
193 }
194 
Model< Matrix, SymmGroup >::measurements_type measurements_type
Definition: sim.h:77
void load(alps::hdf5::archive &ar, std::string const &path, TrivialGroup::charge &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t > offset=std::vector< std::size_t >())
Definition: none.h:81
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
pimpl for Model
Definition: model.h:96
static engine_t engine
Definition: random.hpp:39
std::vector< double > calculate_bond_entropies(MPS< Matrix, SymmGroup > &mps)
Definition: mps_mpo_ops.h:252
std::string chkpfile
Definition: sim.h:94
MPS< Matrix, SymmGroup > mps
Definition: sim.h:101
measurements_type iteration_measurements(int sweep)
Definition: sim.hpp:112
Model< Matrix, SymmGroup > model
Definition: sim.h:100
std::vector< std::pair< std::vector< std::string >, std::vector< double > > > entanglement_spectrum_type
Definition: mps_mpo_ops.h:183
std::map< std::string, int > status_type
Definition: sim.h:78
virtual ~sim()
Definition: sim.hpp:126
virtual void checkpoint_simulation(MPS< Matrix, SymmGroup > const &state, status_type const &)
Definition: sim.hpp:131
bool restore
Definition: sim.h:92
virtual std::string results_archive_path(status_type const &) const
Definition: sim.hpp:147
Definition: mps.h:40
bool dns
Definition: sim.h:93
std::string rfile
Definition: sim.h:95
boost::ptr_vector< measurement< Matrix, SymmGroup > > meas_sublist(boost::ptr_vector< measurement< Matrix, SymmGroup > > const &m, std::vector< std::string > const &meas_list)
Definition: measurements.h:97
bool is_data(const char *path)
Definition: archive.h:62
#define DMRG_VERSION_STRING
Definition: version.h:35
void save(alps::hdf5::archive &ar, std::string const &path, TrivialGroup::charge const &value, std::vector< std::size_t > size=std::vector< std::size_t >(), std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t > offset=std::vector< std::size_t >())
Definition: none.h:70
int init_sweep
Definition: sim.h:91
int init_site
Definition: sim.h:91
Lattice lat
Definition: sim.h:99
measurements_type all_measurements
Definition: sim.h:103
sim(DmrgParameters const &)
Definition: sim.hpp:31
std::vector< double > calculate_bond_renyi_entropies(MPS< Matrix, SymmGroup > &mps, double n, std::vector< int > *measure_es_where=NULL, entanglement_spectrum_type *spectra=NULL)
Definition: mps_mpo_ops.h:186
MPO< Matrix, SymmGroup > mpo
Definition: sim.h:102
bool is_scalar(const char *path)
Definition: archive.h:59
pimpl resolved Lattice
Definition: lattice.h:84