Skip to content

Commit

Permalink
Introduce metropolis_step and after_cycle_duties functions in mc_generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoemi09 committed Nov 21, 2024
1 parent 5ad2e42 commit 1a1ca4d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
34 changes: 21 additions & 13 deletions c++/triqs/mc_tools/mc_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,11 @@ namespace triqs::mc_tools {
for (std::int64_t i = 0; i < params.cycle_length; i++) {
if (triqs::signal_handler::received()) throw triqs::signal_handler::exception{};
// Metropolis step
double r = moves_.attempt();
if (rng_() < std::min(1.0, r)) {
sign_ *= moves_.accept();
} else {
moves_.reject();
}
metropolis_step();
++config_id_;
}
// after cycle duties
params.after_cycle_duty();
if (params.enable_calibration) moves_.calibrate(params.comm);
if (params.enable_measures) {
nmeasures_done_++;
for (auto &m : measures_aux_) m();
measures_.accumulate(sign_);
}
after_cycle_duties(params);
} catch (triqs::signal_handler::exception const &) {
// current cycle is interrupted, simulation is stopped below
std::cerr << fmt::format("[Rank {}] Signal caught in mc_generic::run: Stopping the simulation.\n", rank);
Expand Down Expand Up @@ -287,6 +276,25 @@ namespace triqs::mc_tools {
if (params.enable_measures) report_(3) << measures_.report();
}

template <DoubleOrComplex MCSignType> void mc_generic<MCSignType>::metropolis_step() {
double r = moves_.attempt();
if (rng_() < std::min(1.0, r)) {
sign_ *= moves_.accept();
} else {
moves_.reject();
}
}

template <DoubleOrComplex MCSignType> void mc_generic<MCSignType>::after_cycle_duties(run_param_t const &params) {
params.after_cycle_duty();
if (params.enable_calibration) moves_.calibrate(params.comm);
if (params.enable_measures) {
nmeasures_done_++;
for (auto &m : measures_aux_) m();
measures_.accumulate(sign_);
}
}

// Explicit template instantiations.
template class mc_generic<double>;
template class mc_generic<std::complex<double>>;
Expand Down
6 changes: 6 additions & 0 deletions c++/triqs/mc_tools/mc_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ namespace triqs::mc_tools {
// Print simulation info.
void print_sim_info(run_param_t const &params, std::int64_t cycle_counter);

// Do a single Metropolis accept/reject step.
void metropolis_step();

// Perform after cycle duties.
void after_cycle_duties(run_param_t const &params);

private:
random_generator rng_;
move_set<MCSignType> moves_;
Expand Down

0 comments on commit 1a1ca4d

Please sign in to comment.