3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-25 13:53:40 +01:00

mc_tools : add precomputation (first draft)

- add precomputation as auxiliary measure,
computed before the accumulation of measures.
This commit is contained in:
Olivier Parcollet 2013-06-14 17:16:18 +02:00
parent 76d74ce0da
commit f7b88b839c
2 changed files with 186 additions and 7 deletions

View File

@ -24,6 +24,7 @@
#include <triqs/utility/timer.hpp>
#include <triqs/utility/report_stream.hpp>
#include <triqs/parameters/parameters.hpp>
#include "./mc_measure_aux_set.hpp"
#include "./mc_measure_set.hpp"
#include "./mc_move_set.hpp"
#include "./mc_basic_step.hpp"
@ -46,7 +47,7 @@ namespace triqs { namespace mc_tools {
std::function<bool()> AfterCycleDuty = std::function<bool()>() ) :
RandomGenerator(Random_Name, Random_Seed),
AllMoves(RandomGenerator),
AllMeasures(),
AllMeasures(),AllMeasuresAux(),
report(&std::cout, Verbosity),
Length_MC_Cycle(Length_Cycle),
NWarmIterations(N_Warmup_Cycles),
@ -64,7 +65,7 @@ namespace triqs { namespace mc_tools {
//RandomGenerator(P["Random_Generator_Name"]), P.value_or_default("Random_Seed",1)),
report(&std::cout,int(P["Verbosity"])),
AllMoves(RandomGenerator),
AllMeasures(),
AllMeasures(),AllMeasuresAux(),
Length_MC_Cycle(long(P["Length_Cycle"])), /// NOT NICE THIS EXPLICIT CAST : no unsigned in parameters, really ??
NWarmIterations(long(P["N_Warmup_Cycles"])),
NCycles(long(P["N_Cycles"])),
@ -92,7 +93,16 @@ namespace triqs { namespace mc_tools {
AllMeasures.insert(std::forward<MeasureType>(M), name);
}
/// get the average sign (to be called after collect_results)
/**
* Register the precomputation
*/
/*template<typename MeasureAuxType>
MeasureAuxType * add_measure_aux(MeasureAuxType && M, std::string name) {
static_assert( !std::is_pointer<MeasureAuxType>::value, "add_measure_aux in mc_generic takes ONLY values !");
AllMeasuresAux.insert(std::forward<MeasureAuxType>(M), name);
}
*/
/// get the average sign (to be called after collect_results)
MCSignType average_sign() const { return sign_av; }
/// get the current percents done
@ -116,6 +126,7 @@ namespace triqs { namespace mc_tools {
if (thermalized()) {
nmeasures++;
sum_sign += sign;
AllMeasuresAux.compute_all();
AllMeasures.accumulate(sign);
}
// recompute fraction done
@ -156,11 +167,14 @@ namespace triqs { namespace mc_tools {
}
// do not use direcly, use the free function it is simpler to call...
template<typename MeasureType> MeasureType & get_measure(std::string const & name) { return AllMeasures.template get<MeasureType> (name); }
template<typename MeasureType> MeasureType const & get_measure(std::string const & name) const { return AllMeasures.template get<MeasureType> (name); }
template<typename MeasureType> MeasureType & get_measure(std::string const & name) { return AllMeasures.template get_measure<MeasureType> (name); }
template<typename MeasureType> MeasureType const & get_measure(std::string const & name) const { return AllMeasures.template get_measure<MeasureType> (name); }
template<typename MeasureAuxType> MeasureAuxType * get_measure_aux(std::string const & name) { return AllMeasuresAux.template get_measure_aux<MeasureAuxType> (name); }
template<typename MeasureAuxType> MeasureAuxType const * get_measure_aux(std::string const & name) const { return AllMeasuresAux.template get_measure_aux<MeasureAuxType> (name); }
template<typename MoveType> MoveType & get_move (std::string const & name) { return AllMoves.template get<MoveType> (name); }
template<typename MoveType> MoveType const & get_move (std::string const & name) const { return AllMoves.template get<MoveType> (name); }
template<typename MoveType> MoveType & get_move (std::string const & name) { return AllMoves.template get_move<MoveType> (name); }
template<typename MoveType> MoveType const & get_move (std::string const & name) const { return AllMoves.template get_move<MoveType> (name); }
/// HDF5 interface
friend void h5_write (h5::group g, std::string const & name, mc_generic const & mc){
@ -194,6 +208,7 @@ namespace triqs { namespace mc_tools {
random_generator RandomGenerator;
move_set<MCSignType> AllMoves;
measure_set<MCSignType> AllMeasures;
measure_aux_set AllMeasuresAux;
utility::report_stream report;
uint64_t Length_MC_Cycle;/// Length of one Monte-Carlo cycle between 2 measures
uint64_t NWarmIterations, NCycles;
@ -212,6 +227,10 @@ namespace triqs { namespace mc_tools {
/// Retrieve a Measure given name and type. NB : the type is checked at runtime
template<typename M,typename T1, typename T2> M & get_measure(mc_generic<T1,T2> & s, std::string const & name) { return s.template get_measure<M> (name); }
template<typename M,typename T1, typename T2> M const & get_measure(mc_generic<T1,T2> const & s, std::string const & name) { return s.template get_measure<M> (name); }
/// Retrieve a Measure given name and type. NB : the type is checked at runtime
template<typename M,typename T1, typename T2> M * get_measure_aux(mc_generic<T1,T2> & s, std::string const & name) { return s.template get_measure_aux<M> (name); }
template<typename M,typename T1, typename T2> M const * get_measure_aux(mc_generic<T1,T2> const & s, std::string const & name) { return s.template get_measure_aux<M> (name); }
/// Retrieve a Move given name and type. NB : the type is checked at runtime
template<typename M,typename T1, typename T2> M & get_move(mc_generic<T1,T2> & s, std::string const & name) { return s.template get_move<M> (name); }

View File

@ -0,0 +1,160 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011-2013 by M. Ferrero, O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#ifndef TRIQS_TOOLS_MC_PRECOMPUTATION_H
#define TRIQS_TOOLS_MC_PRECOMPUTATION_H
#include <functional>
#include <boost/mpi.hpp>
#include <map>
#include <triqs/utility/exceptions.hpp>
#include "./impl_tools.hpp"
namespace triqs { namespace mc_tools {
// mini concept checking
template<typename T, typename Enable=void> struct is_callable: std::false_type {};
template<typename T> struct is_callable <T, decltype(std::declval<T>()())> : std::true_type {};
//--------------------------------------------------------------------
class measure_aux {
std::shared_ptr<void> impl_;
std::function<measure_aux()> clone_;
size_t hash_;
std::string type_name_;
std::function<void ( ) > call_;
public :
template<typename MeasureAuxType>
measure_aux (MeasureAuxType && p_in, bool) {
static_assert( is_callable<MeasureAuxType>::value, "This measure_aux is not callable");
MeasureAuxType *p = new typename std::remove_reference<MeasureAuxType>::type(std::forward<MeasureAuxType>(p_in));
impl_= std::shared_ptr<void> (p);
clone_ = [p]() { return MeasureAuxType(*p);} ;
hash_ = typeid(MeasureAuxType).hash_code();
type_name_ = typeid(MeasureAuxType).name();
call_ = [p]() { (*p)();};
}
// Value semantics. Everyone at the end call move = ...
measure_aux(measure_aux const &rhs) = default; //{*this = rhs;}
//measure_aux(measure_aux &rhs) {*this = rhs;} // or it will use the template = bug
measure_aux(measure_aux && rhs) = default ; //{ *this = std::move(rhs);}
measure_aux & operator = (measure_aux const & rhs) { *this = rhs.clone_(); return *this;}
#ifndef TRIQS_WORKAROUND_INTEL_COMPILER_BUGS
measure_aux & operator = (measure_aux && rhs) =default;
#else
measure_aux & operator = (measure_aux && rhs) noexcept {
using std::swap;
#define SW(X) swap(X,rhs.X)
SW(impl_); SW(hash_); SW(type_name_); SW(clone_);
SW(call_);
#undef SW
return *this;
}
#endif
void operator()(){ call_();}
template<typename MeasureAuxType> bool has_type() const { return (typeid(MeasureAuxType).hash_code() == hash_); };
template<typename MeasureAuxType> void check_type() const {
if (!(has_type<MeasureAuxType>()))
TRIQS_RUNTIME_ERROR << "Trying to retrieve a measure_aux of type "<< typeid(MeasureAuxType).name() << " from a measure_aux of type "<< type_name_;
};
template<typename MeasureAuxType> MeasureAuxType * get() { check_type<MeasureAuxType>(); return (static_cast<MeasureAuxType *>(impl_.get())); }
template<typename MeasureAuxType> MeasureAuxType const * get() const { check_type<MeasureAuxType>(); return (static_cast<MeasureAuxType const *>(impl_.get())); }
};
//--------------------------------------------------------------------
class measure_aux_set {
typedef measure_aux measure_aux_type;
std::map<std::string, measure_aux> m_map;
public :
measure_aux_set(){}
measure_aux_set(measure_aux_set const &) = default;
measure_aux_set(measure_aux_set &&) = default;
measure_aux_set& operator = (measure_aux_set const &) = default;
#ifndef TRIQS_WORKAROUND_INTEL_COMPILER_BUGS
measure_aux_set& operator = (measure_aux_set &&) = default;
#else
measure_aux_set& operator = (measure_aux_set && rhs) {
using std::swap;
swap(m_map,rhs.m_map);
return *this;
}
#endif
/**
* Register the auxiliary M with a name
*/
/*template<typename MeasureAuxType>
MeasureAuxType * insert (MeasureAuxType && M, std::string const & name) {
if (has(name)) TRIQS_RUNTIME_ERROR << "Auxiliary measure "<<name<<" already exists";
auto r = m_map.insert(std::make_pair(name, measure_aux_type (std::forward<MeasureAuxType>(M))));
return r.first;
}
template<typename MeasureAuxType>
MeasureAuxType * retrieve (std::string const & name) {
if (!has(name)) TRIQS_RUNTIME_ERROR << "Auxiliary measure "<<name<<" does not exist";
return (&get_measure_aux<MeasureAuxType>(name));
}
*/
bool has(std::string const & name) const { return m_map.find(name) != m_map.end(); }
void compute_all () { for (auto & nmp : m_map) nmp.second(); }
std::vector<std::string> names() const {
std::vector<std::string> res;
for (auto const & nmp : m_map) res.push_back(nmp.first);
return res;
}
// access to the measure_aux, given its type, with dynamical type check
template<typename MeasureAuxType>
MeasureAuxType * get_measure_aux(std::string const & name) {
auto it = m_map.find (name);
if (it == m_map.end()) { auto r = m_map.insert(std::make_pair(name, measure_aux_type(MeasureAuxType()))); return get_measure_aux<MeasureAuxType>(name);}
return it->second.template get<MeasureAuxType>();
}
/*template<typename MeasureAuxType>
MeasureAuxType const & get_measure_aux(std::string const & name) const {
auto it = m_map.find (name);
if (it == m_map.end()) TRIQS_RUNTIME_ERROR << " Measure " << name << " unknown";
return it->template get<MeasureAuxType>();
}
*/
};
}}// end namespace
#endif