19#ifndef REACTION_METHODS_REACTION_ALGORITHM_HPP
20#define REACTION_METHODS_REACTION_ALGORITHM_HPP
37#include <unordered_map>
50 boost::mpi::communicator
const &m_comm;
54 boost::mpi::communicator
const &comm,
int seed,
double kT,
58 m_generator(
Random::mt19937(
std::seed_seq({seed, seed, seed}))),
59 m_normal_distribution(0.0, 1.0), m_uniform_real_distribution(0.0, 1.0),
60 m_exponential_distribution(1.) {
62 throw std::domain_error(
"Invalid value for 'kT'");
65 throw std::domain_error(
"Invalid value for 'exclusion_range'");
73 std::vector<std::shared_ptr<SingleReaction>>
reactions;
98 if (new_volume <= 0.) {
99 throw std::domain_error(
"Invalid value for 'volume'");
107 for (
auto const &item : map) {
108 auto const type = item.first;
109 auto const exclusion_radius = item.second;
110 if (exclusion_radius < 0.) {
111 throw std::domain_error(
"Invalid excluded_radius value for type " +
112 std::to_string(type) +
": radius " +
113 std::to_string(exclusion_radius));
115 max_exclusion_range =
116 std::max(max_exclusion_range, 2. * exclusion_radius);
119 m_max_exclusion_range = max_exclusion_range;
126 if (m_reaction_constraint != ReactionConstraint::SLAB_Z) {
127 throw std::runtime_error(
"no slab constraint is currently active");
129 return {m_slab_start_z, m_slab_end_z};
134 void add_reaction(std::shared_ptr<SingleReaction>
const &new_reaction);
149 std::vector<std::tuple<int, int>>
hidden{};
150 std::vector<std::tuple<int, Utils::Vector3d, Utils::Vector3d>>
moved{};
158 throw std::runtime_error(
"No chemical reaction is currently under way");
233 std::uniform_int_distribution<int> uniform_int_dist(0, maxint - 1);
234 return uniform_int_dist(m_generator);
242 std::mt19937 m_generator;
243 std::normal_distribution<double> m_normal_distribution;
244 std::uniform_real_distribution<double> m_uniform_real_distribution;
245 std::exponential_distribution<double> m_exponential_distribution;
247 std::unordered_map<int, int>
250 int create_particle(
int p_type);
251 void hide_particle(
int p_id,
int p_type)
const;
252 void check_exclusion_range(
int p_id,
int p_type);
253 auto get_random_uniform_number() {
254 return m_uniform_real_distribution(m_generator);
256 auto get_random_logarithmic_number() {
257 return m_exponential_distribution(m_generator);
259 auto get_random_velocity_vector() {
261 m_normal_distribution(m_generator),
262 m_normal_distribution(m_generator)};
265 enum class ReactionConstraint {
NONE, CYL_Z, SLAB_Z };
266 ReactionConstraint m_reaction_constraint = ReactionConstraint::NONE;
267 double m_cyl_radius = -10.0;
268 double m_cyl_x = -10.0;
269 double m_cyl_y = -10.0;
270 double m_slab_start_z = -10.0;
271 double m_slab_end_z = -10.0;
272 double m_max_exclusion_range = 0.;
274 Particle *get_real_particle(
int p_id)
const;
275 Particle *get_local_particle(
int p_id)
const;
Vector implementation and trait types for boost qvm interoperability.
Base class for reaction ensemble methods.
double make_reaction_mc_move_attempt_logarithmic(int reaction_id, double ln_bf, double E_pot_old, double E_pot_new)
Accept or reject a reaction MC move made by create_new_trial_state based on a logarithmic probability...
void add_reaction(std::shared_ptr< SingleReaction > const &new_reaction)
Adds a reaction to the reaction system.
double calculate_potential_energy() const
Compute the system potential energy.
int m_tried_configurational_MC_moves
void make_reaction_attempt(::ReactionMethods::SingleReaction const &reaction, ParticleChanges &bookkeeping)
Carry out a chemical reaction and save the old system state.
Utils::Vector3d get_random_position_in_box()
Writes a random position inside the central box into the provided array.
void set_slab_constraint(double slab_start_z, double slab_end_z)
bool particle_inside_exclusion_range_touched
int i_random(int maxint)
draws a random integer from the uniform distribution in the range [0,maxint-1]
virtual ~ReactionAlgorithm()=default
std::unordered_map< int, double > charges_of_types
void set_cyl_constraint(double center_x, double center_y, double radius)
std::unordered_map< int, double > exclusion_radius_per_type
void setup_bookkeeping_of_empty_pids()
Cleans the list of empty pids and searches for empty pid in the system.
void delete_reaction(int reaction_id)
void displacement_mc_move(int type, int n_particles)
Carry out displacement MC moves for particles of a given type.
void update_volume()
Automatically sets the volume which is used by the reaction ensemble to the volume of a cuboid box.
void set_exclusion_radius_per_type(std::unordered_map< int, double > const &map)
void restore_old_system_state()
Restore last valid system state.
auto get_exclusion_range() const
bool all_reactant_particles_exist(SingleReaction const ¤t_reaction) const
Checks whether all particles exist for the provided reaction.
ReactionAlgorithm(boost::mpi::communicator const &comm, int seed, double kT, double exclusion_range, std::unordered_map< int, double > const &exclusion_radius_per_type)
auto & make_new_system_state()
Open new handle for system state tracking.
std::shared_ptr< ParticleChanges > m_system_changes
void clear_old_system_state()
Clear last valid system state.
bool is_reaction_under_way() const
void delete_particle(int p_id)
Deletes the particle with the given p_id and stores the id if the deletion created a hole in the part...
int m_accepted_configurational_MC_moves
std::vector< std::shared_ptr< SingleReaction > > reactions
double get_acceptance_rate_configurational_moves() const
Utils::Vector2d get_slab_constraint_parameters() const
double exclusion_range
Hard sphere radius.
void set_volume(double new_volume)
std::optional< double > create_new_trial_state(int reaction_id)
Carry out a reaction MC move and calculate the new potential energy.
bool make_displacement_mc_move_attempt(int type, int n_particles)
Attempt displacement MC moves for particles of a given type.
bool neighbor_search_order_n
auto const & get_old_system_state() const
std::vector< int > m_empty_p_ids_smaller_than_max_seen_particle
Communicator communicator
Random number generation using Philox.
Struct holding all information for one particle.
std::vector< int > created
std::unordered_map< int, int > old_particle_numbers
std::vector< std::tuple< int, int > > hidden
std::vector< std::tuple< int, int > > changed
std::vector< std::tuple< int, Utils::Vector3d, Utils::Vector3d > > moved