3
0
mirror of https://github.com/triqs/dft_tools synced 2024-10-31 19:23:45 +01:00

Clean old boost and old mc_tools code (v1)

This commit is contained in:
Olivier Parcollet 2014-10-12 19:32:45 +02:00
parent edd1ff4529
commit dddae19853
4 changed files with 84 additions and 479 deletions

View File

@ -0,0 +1,59 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011-2014 by 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/>.
*
******************************************************************************/
#include "./common.hpp"
#include <triqs/arrays.hpp>
#include <iostream>
//#include <triqs/arrays/h5/simple_read_write.hpp>
using namespace triqs::arrays;
using namespace triqs;
int main(int argc, char **argv) {
try {
array<long,2> A (2,3),B,vc;
array<double,2> D (2,3), D2;
array<long,2> Af(FORTRAN_LAYOUT),Bf(FORTRAN_LAYOUT),vf(FORTRAN_LAYOUT);
for (int i =0; i<2; ++i)
for (int j=0; j<3; ++j)
A(i,j) = 10*i+ j;
Af = A;
std::cout<<" A= "<<A<<std::endl;
std::cout<<" Arange(0,1),range(1,3) = "<< A(range(),range(1,3))<<std::endl;
h5::file file("ess2.h5", H5F_ACC_TRUNC);
h5::group top(file);
h5_write(top,"A",A);
h5_write(top,"A_slice",A(range(),range(1,3)));
//h5_read (top, "A",B); std::cout<< "B = "<< B<<std::endl;
//h5_read (top, "Af",Bf); std::cout<< "Bf = "<< Bf<<std::endl;
//h5_read (top, "D",D2); std::cout<< "D = "<< D2<<std::endl;
}
catch(std::exception const& err) { std::cout<<err.what()<<std::endl;}
}

View File

@ -20,71 +20,51 @@
*
******************************************************************************/
#include "./common.hpp"
#include <triqs/arrays/array.hpp>
#include <triqs/arrays/matrix.hpp>
#include <iostream>
using std::cout; using std::endl;
using namespace triqs::arrays;
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
namespace BLL=boost::lambda;
struct S {
double x,y;
int i;
S(): x(0), y(0), i(0) {}
S(double x0, double y0, int i0): x(x0), y(y0), i(i0) {}
double x = 0, y = 0;
int i = 0;
};
double my_fun(double x, double y) { return x+y;}
std::ostream & operator << (std::ostream & out, S const & s) { return out<<"("<<s.x<<" "<<s.y<<" "<<s.i<<")";}
//std::ostream & operator << (std::ostream & out, S const & s) { return out<<std::make_tuple(s.x,s.y,s.i);}
std::ostream &operator<<(std::ostream &out, S const &s) { return out << "(" << s.x << " " << s.y << " " << s.i << ")"; }
int main(int argc, char **argv) {
array<S, 2> A(2, 2);
array<S,2> A(2,2);
S s0{1.0, 2.0, 3};
int p = 0;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j, ++p) A(i, j) = S{double(i), double(j), p};
S s0(1,2,3);
int p=0;
for (int i =0; i<2; ++i)
for (int j=0; j<2; ++j,++p)
A(i,j) = S(i,j,p);
std::cout<<" A "<<A<<A(range(),0)<<std::endl;
std::cout << " A " << A << A(range(), 0) << std::endl;
A() = s0;
std::cout<<" A "<<A<<A(range(),0)<<std::endl;
std::cout << " A " << A << A(range(), 0) << std::endl;
array< array<double,1>, 2> AA(2,2);
array<array<double, 1>, 2> AA(2, 2);
array<double,1> A0(2);
for (int i =0; i<2; ++i)
A0(i) = i;
array<double, 1> A0(2);
for (int i = 0; i < 2; ++i) A0(i) = i;
AA()=A0;
AA() = A0;
std::cout<<" AA "<<AA<<std::endl;
std::cout<<AA(0,0)<<std::endl;
std::cout << " AA " << AA << std::endl;
std::cout << AA(0, 0) << std::endl;
// even more : a matrix of functions !
matrix< boost::function<double(double) > > F (2,2);
matrix<std::function<double(double)>> F(2, 2);
for (int i =0; i<2; ++i)
for (int j=0; j<2; ++j,++p)
F(i,j) = BLL::bind(&my_fun, BLL::_1, i+j);
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
auto s = i + j;
F(i, j) = [s](int i) { return i + s; };
}
for (int i =0; i<2; ++i)
for (int j=0; j<2; ++j,++p)
std::cout<<F(i,j)(0)<<std::endl;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) std::cout << F(i, j)(0) << std::endl;
return 0;
}

View File

@ -1,171 +0,0 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011 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 MC_MEASURE1_H
#define MC_MEASURE1_H
#include <boost/mpi.hpp>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/concept_check.hpp>
#include <map>
#include <triqs/utility/exceptions.hpp>
namespace triqs {
namespace mc_tools {
namespace mpi=boost::mpi;
using std::size_t;
//--------------------------------------------------------------------
namespace details {
template<typename MCSignType>
class measure_base {
public:
virtual ~measure_base(){}
virtual void accumulate(MCSignType signe)=0;
virtual void collect_results(mpi::communicator const & c)=0;
virtual uint64_t count() const=0;
};
//--------------------------------------------------------------------
template <class X, typename MCSignType> struct IsMeasure {
BOOST_CONCEPT_USAGE(IsMeasure)
{
i.accumulate(MCSignType(1.0));
i.collect_results (*c);
}
private:
X i; boost::mpi::communicator const * c;
};
//--------------------------------------------------------------------
template<typename MCSignType, typename MeasureType>
class measure_impl : public measure_base<MCSignType> {
BOOST_CONCEPT_ASSERT((IsMeasure<MeasureType,MCSignType>));
void operator=(measure_impl const &); // forbidden
protected:
boost::shared_ptr<MeasureType> ptr;
uint64_t count_;
public:
measure_impl(MeasureType * _ptr):measure_base<MCSignType>(), ptr(_ptr),count_(0) {}
measure_impl(boost::shared_ptr<MeasureType> sptr) : measure_base<MCSignType>(), ptr(sptr),count_(0) {}
measure_impl( measure_impl const & X):measure_base<MCSignType>(X), ptr( new MeasureType (*X.ptr.get())),count_(X.count_) {}
virtual ~measure_impl(){}
virtual void accumulate(MCSignType signe){ count_++; ptr->accumulate(signe); }
virtual void collect_results(mpi::communicator const & c) { ptr->collect_results (c); }
virtual uint64_t count() const { return count_;}
};
}//details namespace
//--------------------------------------------------------------------
template<typename MCSignType>
class mcmeasure {
boost::shared_ptr< details::measure_base<MCSignType> > impl_;
public :
template<typename MeasureType>
mcmeasure ( MeasureType * p) : impl_( new details::measure_impl<MCSignType,MeasureType> (p)) {}
template<typename MeasureType>
mcmeasure ( boost::shared_ptr<MeasureType> sptr) : impl_( new details::measure_impl<MCSignType,MeasureType> (sptr)) {}
mcmeasure() {}
void accumulate(MCSignType const & signe) { assert(impl_); impl_->accumulate(signe);}
void collect_results(boost::mpi::communicator const & communicator) { assert(impl_); impl_-> collect_results(communicator);}
uint64_t count() const { assert(impl_); return impl_->count();}
// Recover the measure in its original type
/* template <typename T> T const & get() const { assert(impl_);
details::measure_impl<MCSignType,T> * res = dynamic_cast< details::measure_impl <MCSignType,T> *> (impl_.get());
if (!res) TRIQS_RUNTIME_ERROR<<"Internal error in measure_set : Measure is not of the required type";
return res.get();
} */
};
//--------------------------------------------------------------------
template<typename MCSignType>
class measure_set : public std::map<std::string, mcmeasure<MCSignType> > {
typedef std::map<std::string, mcmeasure<MCSignType> > BaseType;
typedef mcmeasure<MCSignType> measure_type;
public :
typedef typename BaseType::iterator iterator;
typedef typename BaseType::const_iterator const_iterator;
/**
* Register the Measure M with a name
* WARNING : the pointer is deleted automatically by the class at destruction.
*/
template<typename T>
void insert (T * && M, std::string const & name) {
if (has(name)) TRIQS_RUNTIME_ERROR <<"measure_set : insert : measure '"<<name<<"' already inserted";
BaseType::insert(std::make_pair(name, measure_type (M)));
}
template<typename T>
void insert(boost::shared_ptr<T> ptr, std::string const & name) {
if (has(name)) TRIQS_RUNTIME_ERROR <<"measure_set : insert : measure '"<<name<<"' already inserted";
BaseType::insert(std::make_pair(name, measure_type(ptr)));
}
bool has(std::string const & name) const { return BaseType::find(name) != BaseType::end(); }
measure_type & operator[](std::string const & name) {
if (!has(name))
throw std::out_of_range("No result found with the name: " + name);
return BaseType::find(name)->second;
}
measure_type const & operator[](std::string const & name) const {
if (!has(name))
throw std::out_of_range("No result found with the name: " + name);
return BaseType::find(name)->second;
}
///
void accumulate( MCSignType & signe) { for (iterator it= this->begin(); it != this->end(); ++it) it->second.accumulate(signe); }
///
//size_t size() const {return name_to_mes.size(); }
///
std::vector<std::string> names() const {
std::vector<std::string> res;
for (const_iterator it= this->begin(); it !=this->end(); ++it) res.push_back(it->first);
return res;
}
// gather result for all measure, on communicator c
void collect_results (boost::mpi::communicator const & c ) {
for (typename BaseType::iterator it = this->begin(); it != this->end(); ++it) it->second.collect_results(c);
}
};
}}// end namespace
#endif

View File

@ -1,263 +0,0 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011 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_MOVE_SET1_H
#define TRIQS_TOOLS_MC_MOVE_SET1_H
#include <boost/mpi.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include "random_generator.hpp"
#include <boost/concept_check.hpp>
#include <triqs/utility/report_stream.hpp>
namespace triqs { namespace mc_tools {
namespace mpi=boost::mpi;
template<typename MCSignType> class move_set;
namespace details {
template<typename MCSignType>
struct move_base {
virtual ~move_base(){}
virtual MCSignType Try()=0;
virtual MCSignType Accept()=0;
virtual void Reject()=0;
virtual double acceptance_probability(mpi::communicator const & c) const=0;// on master,0 on nodes
virtual void print (triqs::utility::report_stream &, mpi::communicator const &, std::string name, std::string decal) const =0;
};
//--------------------------------------------------------------------
template <class X, typename Y> struct IsMove {
BOOST_CONCEPT_USAGE(IsMove)
{
Y r = i.Try();
r = i.Accept();
i.Reject();
}
private:
X i;
};
//--------------------------------------------------------------------
template<typename MoveType>
void print_move (MoveType const &,triqs::utility::report_stream &,mpi::communicator const &,std::string name,std::string decal){}
template<typename MCSignType>
void print_move (move_set<MCSignType> const & M, triqs::utility::report_stream & report, mpi::communicator const & c, std::string name, std::string decal);
//--------------------------------------------------------------------
template<typename MCSignType, typename MoveType>
class move_impl : boost::noncopyable, public move_base<MCSignType> {
BOOST_CONCEPT_ASSERT((IsMove<MoveType, MCSignType>));
private:
boost::shared_ptr<MoveType> ptr; // ptr to the move
uint64_t NProposed,NAccepted; // Statistics
void operator=(move_impl const &); // forbidden
public:
move_impl(MoveType * move_ptr):move_base<MCSignType>(),ptr(move_ptr),NProposed(0),NAccepted(0) {}
move_impl(boost::shared_ptr<MoveType> sptr):move_base<MCSignType>(),ptr(sptr),NProposed(0),NAccepted(0) {}
move_impl(move_impl const & M):
move_base<MCSignType>(),ptr(new MoveType (*M.ptr.get())),
NProposed(M.NProposed),NAccepted(M.NAccepted){}
virtual ~move_impl(){}
virtual MCSignType Try(){ NProposed++; return ptr->Try();}
virtual MCSignType Accept() { NAccepted++; return ptr->Accept(); }
virtual void Reject() { ptr->Reject();}
const MoveType * get() const { return ptr.get();}
/// Acceptance Probability on the master and 0 on the nodes
virtual double acceptance_probability(mpi::communicator const & c) const {
uint64_t nacc_tot=0, nprop_tot=1;
mpi::reduce(c, NAccepted, nacc_tot, std::plus<uint64_t>(), 0);
mpi::reduce(c, NProposed, nprop_tot, std::plus<uint64_t>(), 0);
return nacc_tot/static_cast<double>(nprop_tot);
}
virtual void print (triqs::utility::report_stream & report, mpi::communicator const & c, std::string name, std::string decal) const {
report<< decal <<"Acceptance probability of move : "<<name<<" : "<<acceptance_probability(c)<<std::endl;
print_move(*get(),report,c,name,decal);
}
};
}
//--------------------------------------------------------------------
/**
* A vector of (moves, PropositionProbability), which is also a move itself
*/
template<typename MCSignType>
class move_set {
boost::ptr_vector<details::move_base<MCSignType> > moves_;
std::vector<std::string> names_;
details::move_base<MCSignType> * current;
size_t current_move_number;
random_generator & RNG;
std::vector<double> Proba_Moves, Proba_Moves_Acc_Sum;
public:
///
move_set(random_generator & R): RNG(R) { Proba_Moves.push_back(0); }
/**
* Add move M with its probability of being proposed.
* NB : the PropositionProbability needs to be >0 but does not need to be
* normalized. Normalization is automatically done with all the added moves
* before starting the run
*
* WARNING : the pointer is deleted automatically by the MC class at destruction.
*/
template <typename MoveType>
void add (MoveType * && M, std::string name, double PropositionProbability) {
moves_.push_back(new details::move_impl<MCSignType,MoveType>(M));
assert(PropositionProbability >=0);
Proba_Moves.push_back(PropositionProbability);
names_.push_back(name);
normaliseProba();// ready to run after each add !
}
template <typename MoveType>
void add (boost::shared_ptr<MoveType> sptr, std::string name, double PropositionProbability) {
moves_.push_back(new details::move_impl<MCSignType,MoveType>(sptr));
assert(PropositionProbability >=0);
Proba_Moves.push_back(PropositionProbability);
names_.push_back(name);
normaliseProba();// ready to run after each add !
}
//-----------------
/**
* - Picks up one of the move at random (weighted by their proposition probability),
* - Call Try method of that move
* - Returns the metropolis ratio R (see move concept).
* The sign ratio returned by the try method of the move is kept.
*/
double Try() {
assert( Proba_Moves_Acc_Sum.size()>0);
// Choice of move with its probability
double proba = RNG();assert(proba>=0);
//std::cerr<<" Size of proba_moves_acc"<< Proba_Moves_Acc_Sum.size()<<std::endl;
// for (unsigned int u = 0; u<Proba_Moves_Acc_Sum.size(); ++u) std::cerr<<"PROBA-- "<<u << " "<<Proba_Moves_Acc_Sum[u]<<std::endl;
current_move_number =0; while (proba >= Proba_Moves_Acc_Sum[current_move_number] ) { current_move_number++;}
//std::cerr << "curren move #"<<current_move_number<< " "<<proba<<std::endl;
assert(current_move_number>0); assert(current_move_number<=this->size());
current_move_number--;
current = &moves_[current_move_number];
#ifdef DEBUG
std::cerr << "*******************************************************"<< std::endl;
std::cerr << "Name of the proposed move: " << name_of_currently_selected() << std::endl;
std::cerr <<" Proposition probability = "<<proba<<std::endl;
#endif
MCSignType rate_ratio = current->Try();
if (!std::isfinite(std::abs(rate_ratio)))
TRIQS_RUNTIME_ERROR<<"Monte Carlo Error : the rate is not finite in move "<<name_of_currently_selected();
double abs_rate_ratio = std::abs(rate_ratio);
#ifdef DEBUG
std::cerr << " Metropolis ratio " << rate_ratio<<". Abs(Metropolis ratio) " <<abs_rate_ratio << std::endl;
#endif
assert ((abs_rate_ratio>=0));
try_sign_ratio = ( abs_rate_ratio> 1.e-14 ? rate_ratio/abs_rate_ratio : 1); // keep the sign
return abs_rate_ratio;
}
//-----------------
/**
* Accept the move previously selected and tried.
* Returns the Sign computed as, if M is the move :
* Sign = sign (M.Try()) * M.Accept()
*/
MCSignType Accept() {
MCSignType accept_sign_ratio = current->Accept();
// just make sure that accept_sign_ratio is a sign!
assert(std::abs(std::abs(accept_sign_ratio)-1.0) < 1.e-10);
#ifdef DEBUG
std::cerr.setf(std::ios::scientific, std::ios::floatfield);
std::cerr<<" ... Move accepted"<<std::endl;
std::cerr<<" try_sign_ratio = " << try_sign_ratio <<std::endl;
std::cerr<<" accept_sign_ratio = "<< accept_sign_ratio <<std::endl;
std::cerr<<" their product = "<< try_sign_ratio* accept_sign_ratio <<std::endl;
#endif
return try_sign_ratio * accept_sign_ratio;
}
//-----------------
/** Reject the move
* Call the Reject() method of the move previously selected
*/
void Reject() {
#ifdef DEBUG
std::cerr<<" ... Move rejected"<<std::endl;
#endif
current->Reject();
}
//-----------------
/// Number of moves registered in the move_set
size_t size() const { return moves_.size();}
//-----------------
/// Pretty printing of the acceptance probability of the moves.
void print (triqs::utility::report_stream & report, mpi::communicator const & c, std::string name="", std::string decal="") const {
report <<decal<<"Move set : "<<name <<std::endl;
for (unsigned int u =0; u< size(); ++u)
moves_[u].print(report,c,names_[u],decal+std::string(" "));
}
protected:
MCSignType try_sign_ratio;
void normaliseProba() {
if (moves_.size() ==0) TRIQS_RUNTIME_ERROR<<" no moves registered";
// Computes the normalised accumulated probability
double acc = 0;
Proba_Moves_Acc_Sum.clear();
for (unsigned int u = 0; u<Proba_Moves.size(); ++u) acc+=Proba_Moves[u];assert(acc>0);
for (unsigned int u = 0; u<Proba_Moves.size(); ++u) Proba_Moves_Acc_Sum.push_back(Proba_Moves[u]/acc);
// to accumulation
for (unsigned int u = 1; u<Proba_Moves_Acc_Sum.size(); ++u) Proba_Moves_Acc_Sum[u] += Proba_Moves_Acc_Sum[u-1];
assert(std::abs(Proba_Moves_Acc_Sum[Proba_Moves_Acc_Sum.size()-1] -1)<1.e-13);
//std::cerr<<" NOam Size of proba_moves_acc"<< Proba_Moves_Acc_Sum.size()<<std::endl;
//for (unsigned int u = 0; u<Proba_Moves_Acc_Sum.size(); ++u) std::cerr<<"PROBA "<<u <<Proba_Moves_Acc_Sum[u]<<std::endl;
Proba_Moves_Acc_Sum[Proba_Moves_Acc_Sum.size()-1] += 0.001;
// I shift the last proba acc so that even if random number in onecycle is 1 it is below that bound
assert(Proba_Moves_Acc_Sum.size()==this->size()+1);
}
/// for debug only
std::string name_of_currently_selected() const { return names_[current_move_number];}
};// class move_set
namespace details { // specialization for pretty print of move_set.
template<typename MCSignType>
inline void print_move (move_set<MCSignType> const & M, triqs::utility::report_stream & report, mpi::communicator const & c, std::string name, std::string decal) {
M.print(report,c,name,decal);
}
}
}
}// end namespace
#endif