mcpele
1.0.0
The Monte Carlo Python Energy Landscape Explorer
|
00001 #ifndef _MCPELE_MC_H 00002 #define _MCPELE_MC_H 00003 00004 #include <cmath> 00005 #include <algorithm> 00006 #include <memory> 00007 #include <stdexcept> 00008 00009 #include "pele/array.h" 00010 #include "pele/base_potential.h" 00011 00012 namespace mcpele{ 00013 00014 class MC; 00015 00016 /* 00017 * Action 00018 */ 00019 00020 class Action { 00021 public: 00022 //Action(){std::cout<< "Action()" << "\n";} 00023 //virtual ~Action(){std::cout << "~Action()" << "\n";} 00024 virtual ~Action(){} 00025 virtual void action(pele::Array<double> &coords, double energy, bool accepted, 00026 MC* mc) =0; 00027 }; 00028 00029 /* 00030 * Accept Test 00031 */ 00032 00033 class AcceptTest{ 00034 public: 00035 //AcceptTest(){std::cout << "AcceptTest()" << "\n";} 00036 //virtual ~AcceptTest(){std::cout << "~AcceptTest()" << "\n";} 00037 virtual ~AcceptTest(){} 00038 virtual bool test(pele::Array<double> &trial_coords, double trial_energy, 00039 pele::Array<double> & old_coords, double old_energy, double temperature, 00040 MC * mc) =0; 00041 }; 00042 00043 /* 00044 * Accept Test 00045 */ 00046 00047 class ConfTest{ 00048 public: 00049 //ConfTest(){std::cout << "ConfTest()" << "\n";} 00050 //virtual ~ConfTest(){std::cout << "~ConfTest()" << "\n";} 00051 virtual ~ConfTest(){} 00052 virtual bool conf_test(pele::Array<double> &trial_coords, MC * mc) =0; 00053 }; 00054 00055 /* 00056 * Take Step 00057 */ 00058 00059 class TakeStep { 00060 public: 00061 virtual ~TakeStep() {} 00062 virtual void displace(pele::Array<double>& coords, MC* mc) = 0; 00063 virtual void report(pele::Array<double>&, const double, 00064 pele::Array<double>&, const double, const bool, MC*) {} 00065 virtual void increase_acceptance(const double) {} 00066 virtual void decrease_acceptance(const double) {} 00067 }; 00068 00083 class MC { 00084 public: 00085 typedef std::vector<std::shared_ptr<Action> > actions_t; 00086 typedef std::vector<std::shared_ptr<AcceptTest> > accept_t; 00087 typedef std::vector<std::shared_ptr<ConfTest> > conf_t; 00088 protected: 00089 std::shared_ptr<pele::BasePotential> m_potential; 00090 pele::Array<double> m_coords; 00091 pele::Array<double> m_trial_coords; 00092 actions_t m_actions; 00093 accept_t m_accept_tests; 00094 conf_t m_conf_tests; 00095 conf_t m_late_conf_tests; 00096 std::shared_ptr<TakeStep> m_take_step; 00097 size_t m_nitercount; 00098 size_t m_accept_count; 00099 size_t m_E_reject_count; 00100 size_t m_conf_reject_count; 00101 bool m_success; 00102 /*nitercount is the cumulative count, it does not get reset at the end of run*/ 00103 bool m_print_progress; 00104 public: 00105 /*need to keep these public to make them accessible to tests and actions, be careful though!*/ 00106 size_t m_niter; 00107 size_t m_neval; 00108 double m_temperature; 00109 double m_energy; 00110 double m_trial_energy; 00111 private: 00112 size_t m_report_steps; 00113 public: 00114 MC(std::shared_ptr<pele::BasePotential> potential, pele::Array<double>& coords, const double temperature); 00115 virtual ~MC() {} 00116 void one_iteration(); 00117 void run(const size_t max_iter); 00118 void set_temperature(const double T) { m_temperature = T; } 00119 double get_temperature() const { return m_temperature; } 00120 void set_report_steps(const size_t report_steps) { m_report_steps = report_steps; } 00121 size_t get_report_steps() const { return m_report_steps; } 00122 void add_action(std::shared_ptr<Action> action) { m_actions.push_back(action); } 00123 void add_accept_test(std::shared_ptr<AcceptTest> accept_test) { m_accept_tests.push_back(accept_test); } 00124 void add_conf_test(std::shared_ptr<ConfTest> conf_test) { m_conf_tests.push_back(conf_test); } 00125 void add_late_conf_test(std::shared_ptr<ConfTest> conf_test) { m_late_conf_tests.push_back(conf_test); } 00126 void set_takestep(std::shared_ptr<TakeStep> takestep) { m_take_step = takestep; } 00127 std::shared_ptr<TakeStep> get_takestep() const { return m_take_step; } 00128 void set_coordinates(pele::Array<double>& coords, double energy); 00129 double get_energy() const { return m_energy; } 00130 void reset_energy(); 00131 double get_trial_energy() const { return m_trial_energy; } 00132 pele::Array<double> get_coords() const { return m_coords.copy(); } 00133 pele::Array<double> get_trial_coords() const { return m_trial_coords.copy(); } 00134 double get_norm_coords() const { return norm(m_coords); } 00135 size_t get_naccept() const { return m_accept_count; } 00136 size_t get_nreject() const { return m_nitercount - m_accept_count; } 00137 double get_accepted_fraction() const { return static_cast<double>(m_accept_count) / 00138 static_cast<double>(m_nitercount); } 00139 double get_conf_rejection_fraction() const { return static_cast<double>(m_conf_reject_count) / 00140 static_cast<double>(m_nitercount); } 00141 double get_E_rejection_fraction() const { return static_cast<double>(m_E_reject_count) / 00142 static_cast<double>(m_nitercount); } 00143 size_t get_iterations_count() const { return m_nitercount; } 00144 size_t get_neval() const { return m_neval; } 00145 std::shared_ptr<pele::BasePotential> get_potential_ptr() { return m_potential; } 00146 bool take_step_specified() const { return m_take_step != NULL; } 00147 bool report_steps_specified() const { return get_report_steps() > 0; } 00148 void check_input(); 00149 void set_print_progress(const bool input) { m_print_progress = input; } 00150 void set_print_progress() { set_print_progress(true); } 00151 bool get_success() const { return m_success; } 00155 void abort() { m_niter = std::numeric_limits<size_t>::max(); } 00156 protected: 00157 inline double compute_energy(pele::Array<double> x) 00158 { 00159 ++m_neval; 00160 return m_potential->get_energy(x); 00161 } 00162 bool do_conf_tests(pele::Array<double> x); 00163 bool do_accept_tests(pele::Array<double> xtrial, double etrial, pele::Array<double> xold, double eold); 00164 bool do_late_conf_tests(pele::Array<double> x); 00165 void do_actions(pele::Array<double> x, double energy, bool success); 00166 void take_steps(); 00167 }; 00168 00169 }//namespace mcpele 00170 00171 #endif//#ifndef _MCPELE_MC_H