ALPSCore reference
mcbase.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
8 #include <alps/mc/mcbase.hpp>
9 
10 namespace alps {
11 
12  mcbase::mcbase(parameters_type const & parms, std::size_t seed_offset)
13  : parameters(parms)
14  , random(std::size_t(parameters["SEED"]) + seed_offset)
15  {
17  }
18 
20  return parameters.define<long>("SEED", 42, "PRNG seed");
21  }
22 
23  void mcbase::save(std::string const & filename) const {
24  alps::hdf5::archive ar(filename, "w");
25  ar["/simulation/realizations/0/clones/0"] << *this;
26  }
27 
28  void mcbase::load(std::string const & filename) {
29  alps::hdf5::archive ar(filename);
30  ar["/simulation/realizations/0/clones/0"] >> *this;
31  }
32 
33  bool mcbase::run(boost::function<bool ()> const & stop_callback) {
34  bool stopped = false;
35  while(!(stopped = stop_callback()) && fraction_completed() < 1.) {
36  update();
37  measure();
38  }
39  return !stopped;
40  }
41 
42  // implement a nice keys(m) function
44  result_names_type names;
46  names.push_back(it->first);
47  return names;
48  }
49 
51  return result_names_type();
52  }
53 
55  return collect_results(result_names());
56  }
57 
59  results_type partial_results;
60  for(result_names_type::const_iterator it = names.begin(); it != names.end(); ++it){
61  partial_results.insert(*it, measurements[*it].result());
62  }
63  return partial_results;
64  }
65 
66  void mcbase::save(alps::hdf5::archive & ar) const {
67  ar["/parameters"] << parameters;
68  ar["measurements"] << measurements;
69  ar["checkpoint"] << random;
70  }
71 
73  ar["/parameters"] >> parameters;
74  ar["measurements"] >> measurements;
75  ar["checkpoint"] >> random;
76  }
77 
78 }
virtual void measure()=0
observable_collection_type measurements
Definition: mcbase.hpp:58
Parse sectioned INI file or HDF5 or command line, provide the results as dictionary.
Definition: params.hpp:84
bool run(boost::function< bool()> const &stop_callback)
Definition: mcbase.cpp:33
void save(std::string const &filename) const
Definition: mcbase.cpp:23
result_names_type unsaved_result_names() const
Definition: mcbase.cpp:50
std::vector< std::string > result_names_type
Definition: mcbase.hpp:30
params & define(const std::string &name, const std::string &descr)
Defines a parameter; returns false on error, and records the error in the object. ...
static void listen()
Definition: signal.cpp:54
alps::random01 random
Definition: mcbase.hpp:57
virtual double fraction_completed() const =0
STL namespace.
void insert(std::string const &name, std::shared_ptr< T > ptr)
Definition: wrapper_set.cpp:47
void load(std::string const &filename)
Definition: mcbase.cpp:28
results_type collect_results() const
Definition: mcbase.cpp:54
static parameters_type & define_parameters(parameters_type &parameters)
Definition: mcbase.cpp:19
result_names_type result_names() const
Definition: mcbase.cpp:43
mcbase(parameters_type const &parms, std::size_t seed_offset=0)
Definition: mcbase.cpp:12
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
virtual void update()=0
Functor-predicate: is it time to stop?
parameters_type parameters
Definition: mcbase.hpp:55
std::map< std::string, std::shared_ptr< T > >::const_iterator const_iterator
Definition: wrapper_set.hpp:33