40#include "accumulators/AutoUpdateAccumulators.hpp"
45#include "collision_detection/CollisionDetection.hpp"
57#include "system/System.hpp"
67#include <boost/mpi/collectives/all_reduce.hpp>
69#ifdef ESPRESSO_CALIPER
70#include <caliper/cali.h>
73#ifdef ESPRESSO_VALGRIND
88#ifdef ESPRESSO_WALBERLA
89#ifdef ESPRESSO_WALBERLA_STATIC_ASSERT
90#error "waLberla headers should not be visible to the ESPResSo core"
95volatile std::sig_atomic_t
ctrl_C = 0;
106 assert(m_protocol !=
nullptr);
113 auto &system = get_system();
114 auto &cell_structure = *system.cell_structure;
115 auto &box_geo = *system.box_geo;
117 m_protocol = std::move(protocol);
118 update_box_params(box_geo, system.get_sim_time());
119 system.propagation->recalc_forces =
true;
124 auto &system = get_system();
125 auto &cell_structure = *system.cell_structure;
126 auto &box_geo = *system.box_geo;
127 m_protocol =
nullptr;
129 system.propagation->recalc_forces =
true;
145#ifdef ESPRESSO_ROTATION
150#ifdef ESPRESSO_ROTATION
155#ifdef ESPRESSO_ROTATION
160#ifdef ESPRESSO_ROTATION
174#ifdef ESPRESSO_ROTATION
178#ifdef ESPRESSO_STOKESIAN_DYNAMICS
184 throw std::runtime_error(
"Unknown value for integ_switch");
190 for (
auto &p : cell_structure->local_particles()) {
191 used_propagations |= p.propagation();
194 used_propagations |= propagation->default_propagation;
196 used_propagations = boost::mpi::all_reduce(
::comm_cart, used_propagations,
198 propagation->used_propagations = used_propagations;
199 propagation->recalc_used_propagations =
false;
202void System::System::integrator_sanity_checks()
const {
203 auto const thermo_switch = thermostat->thermo_switch;
204 if (time_step <= 0.) {
210 <<
"The steepest descent integrator is incompatible with thermostats";
216 "currently active combination of thermostats";
228 nptiso->coulomb_dipole_sanity_checks(*
this);
229 }
catch (std::runtime_error
const &err) {
240#ifdef ESPRESSO_STOKESIAN_DYNAMICS
246 if (lb.is_solver_set() and (propagation->used_propagations &
249 if (thermostat->lb ==
nullptr) {
253 if (bonded_ias->get_n_thermalized_bonds() >= 1 and
254 (thermostat->thermalized_bond ==
nullptr or
257 <<
"Thermalized bonds require the thermalized_bond thermostat";
260#ifdef ESPRESSO_ROTATION
261 for (
auto const &p : cell_structure->local_particles()) {
263 if (p.can_rotate() and not p.is_virtual() and
264 (p.propagation() & (SYSTEM_DEFAULT | ROT_EULER | ROT_LANGEVIN |
265 ROT_BROWNIAN | ROT_STOKESIAN)) == 0) {
267 <<
"Rotating particles must have a rotation propagation mode enabled";
273#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
274#ifdef ESPRESSO_EXTERNAL_FORCES
275 if (propagation->used_propagations &
277 for (
auto const &p : cell_structure->local_particles()) {
279 if ((p.propagation() & TRANS_VS_CENTER_OF_MASS) and
280 p.has_fixed_coordinates()) {
287#ifdef ESPRESSO_BOND_CONSTRAINT
288 if (bonded_ias->get_n_rigid_bonds()) {
290 for (
auto const &p : cell_structure->local_particles()) {
291 if (p.propagation() & TRANS_VS_CENTER_OF_MASS) {
292 for (
auto const bond : p.bonds()) {
293 if (std::holds_alternative<RigidBond>(
294 *bonded_ias->at(bond.bond_id()))) {
305#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
307 for (
auto const &p : cell_structure->local_particles()) {
308 if (p.stoner_wohlfarth_is_enabled()) {
309 runtimeErrorMsg() <<
"The thermal Stoner-Wohlfarth model requires the "
310 "Langevin thermostat";
318#ifdef ESPRESSO_WALBERLA
321 if (time_step <= 0.) {
325 auto const eps =
static_cast<double>(std::numeric_limits<float>::epsilon());
326 if ((tau - time_step) / (tau + time_step) < -eps)
327 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
328 ") must be >= MD time_step (" +
329 std::to_string(time_step) +
")");
330 auto const factor = tau / time_step;
331 if (std::fabs(std::round(factor) - factor) / factor > eps)
332 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
333 ") must be an integer multiple of the "
335 std::to_string(time_step) +
"). Factor is " +
336 std::to_string(factor));
346 auto const tol = agrid / 1E6;
347 if ((lattice_left - geo_left).norm2() > tol or
348 (lattice_right - geo_right).norm2() > tol) {
349 std::stringstream error_msg;
350 error_msg <<
"waLBerla and ESPResSo disagree about domain decomposition"
352 <<
"left ESPResSo: [" << geo_left <<
"], "
353 <<
"left waLBerla: [" << lattice_left <<
"]"
355 <<
"right ESPResSo: [" << geo_right <<
"], "
356 <<
"right waLBerla: [" << lattice_right <<
"]"
357 <<
"\nfor method: " << method;
358 throw std::runtime_error(error_msg.str());
364#ifdef ESPRESSO_CALIPER
365 CALI_CXX_MARK_FUNCTION;
369 *system.
box_geo, cell_structure.get_le_pos_offset_at_last_resort());
370 if (cell_structure.check_resort_required(offset)) {
381#ifdef ESPRESSO_CALIPER
382 CALI_CXX_MARK_FUNCTION;
389 auto const kT = thermostat.kT;
391#ifdef ESPRESSO_VIRTUAL_SITES
402#ifdef ESPRESSO_ROTATION
408#ifdef ESPRESSO_ROTATION
418#ifdef ESPRESSO_ROTATION
424#ifdef ESPRESSO_ROTATION
431#ifdef ESPRESSO_ROTATION
453#ifdef ESPRESSO_STOKESIAN_DYNAMICS
470#ifdef ESPRESSO_CALIPER
471 CALI_CXX_MARK_FUNCTION;
477#ifdef ESPRESSO_VIRTUAL_SITES
488#ifdef ESPRESSO_ROTATION
494#ifdef ESPRESSO_ROTATION
504#ifdef ESPRESSO_ROTATION
510#ifdef ESPRESSO_ROTATION
533#ifdef ESPRESSO_CALIPER
534 CALI_CXX_MARK_FUNCTION;
536 auto &propagation = *this->propagation;
537#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
538 auto const has_vs_rel = [&propagation]() {
539 return propagation.used_propagations &
545#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
546 auto const has_vs_com = [&propagation]() {
547 return propagation.used_propagations &
551#ifdef ESPRESSO_BOND_CONSTRAINT
552 auto const n_rigid_bonds = bonded_ias->get_n_rigid_bonds();
556 propagation.update_default_propagation(thermostat->thermo_switch);
557 update_used_propagations();
558 on_integration_start();
567 propagation.recalc_forces)) {
568#ifdef ESPRESSO_CALIPER
569 CALI_MARK_BEGIN(
"Initial Force Calculation");
571 thermostat->lb_coupling_deactivate();
573#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
578#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
585 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
590#ifdef ESPRESSO_ROTATION
595#ifdef ESPRESSO_CALIPER
596 CALI_MARK_END(
"Initial Force Calculation");
600 thermostat->lb_coupling_activate();
606 int n_verlet_updates = 0;
610 auto const singleton_mode =
comm_cart.size() == 1;
611 auto caught_sigint =
false;
612 auto caught_error =
false;
614 auto lb_active =
false;
615 auto ek_active =
false;
617 lb_active = lb.is_solver_set();
618 ek_active = ek.is_ready_for_propagation();
620 auto const calc_md_steps_per_tau = [
this](
double tau) {
621 return static_cast<int>(std::round(tau / time_step));
624#ifdef ESPRESSO_VALGRIND
625 CALLGRIND_START_INSTRUMENTATION;
628#ifdef ESPRESSO_CALIPER
629 CALI_CXX_MARK_LOOP_BEGIN(integration_loop,
"Integration loop");
631 int integrated_steps = 0;
632 for (
int step = 0; step < n_steps; step++) {
633#ifdef ESPRESSO_CALIPER
634 CALI_CXX_MARK_LOOP_ITERATION(integration_loop, step);
637#ifdef ESPRESSO_BOND_CONSTRAINT
640 cell_structure->ghost_particles());
643 lees_edwards->update_box_params(*box_geo, sim_time);
649 sim_time += time_step;
652 cell_structure->for_each_local_particle(
653 [&kernel](
Particle &p) { kernel(p); });
657 if (not has_npt_enabled())
663 thermostat->philox_counter_increment();
665#ifdef ESPRESSO_BOND_CONSTRAINT
672#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
675 if (has_npt_enabled()) {
676 cell_structure->update_ghosts_and_resort_particle(
683#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
686 if (has_npt_enabled()) {
687 cell_structure->update_ghosts_and_resort_particle(
699 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
701#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
702 integrate_magnetodynamics();
707#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
708 if (thermostat->lb and
720 cell_structure->for_each_local_particle(
721 [&kernel](
Particle &p) { kernel(p); });
723#ifdef ESPRESSO_BOND_CONSTRAINT
731 if (lb_active and ek_active) {
733 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
734 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
736 if (md_steps_per_lb_step != md_steps_per_ek_step) {
738 <<
"LB and EK are active but with different time steps.";
741 assert(lb.is_gpu() == ek.is_gpu());
742 assert(propagation.lb_skipped_md_steps ==
743 propagation.ek_skipped_md_steps);
745 propagation.lb_skipped_md_steps += 1;
746 propagation.ek_skipped_md_steps += 1;
747 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
748 propagation.lb_skipped_md_steps = 0;
749 propagation.ek_skipped_md_steps = 0;
750#ifdef ESPRESSO_CALIPER
751 CALI_MARK_BEGIN(
"lb_propagation");
754 lb.ghost_communication_vel();
755#ifdef ESPRESSO_CALIPER
756 CALI_MARK_END(
"lb_propagation");
758#ifdef ESPRESSO_CALIPER
759 CALI_MARK_BEGIN(
"ek_propagation");
762#ifdef ESPRESSO_CALIPER
763 CALI_MARK_END(
"ek_propagation");
766 }
else if (lb_active) {
767 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
768 propagation.lb_skipped_md_steps += 1;
769 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
770 propagation.lb_skipped_md_steps = 0;
771#ifdef ESPRESSO_CALIPER
772 CALI_MARK_BEGIN(
"lb_propagation");
775#ifdef ESPRESSO_CALIPER
776 CALI_MARK_END(
"lb_propagation");
779 }
else if (ek_active) {
780 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
781 propagation.ek_skipped_md_steps += 1;
782 if (propagation.ek_skipped_md_steps >= md_steps_per_ek_step) {
783 propagation.ek_skipped_md_steps = 0;
784#ifdef ESPRESSO_CALIPER
785 CALI_MARK_BEGIN(
"ek_propagation");
788#ifdef ESPRESSO_CALIPER
789 CALI_MARK_END(
"ek_propagation");
793 if (lb_active and (propagation.used_propagations &
795 thermostat->lb->rng_increment();
798#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
799 if (thermostat->lb and
801#ifdef ESPRESSO_CALIPER
802 CALI_MARK_BEGIN(
"lb_tracers_propagation");
805 lb.ghost_communication_vel();
808#ifdef ESPRESSO_CALIPER
809 CALI_MARK_END(
"lb_tracers_propagation");
814#ifdef ESPRESSO_COLLISION_DETECTION
815 cell_structure->clear_new_bonds();
816 collision_detection->handle_collisions();
817 cell_structure->rebuild_bond_list();
819 bond_breakage->process_queue(*
this);
830 if (singleton_mode and ctrl_C == 1) {
831 caught_sigint =
true;
837 lb.ghost_communication();
839 lees_edwards->update_box_params(*box_geo, sim_time);
840#ifdef ESPRESSO_CALIPER
841 CALI_CXX_MARK_LOOP_END(integration_loop);
844#ifdef ESPRESSO_VALGRIND
845 CALLGRIND_STOP_INSTRUMENTATION;
848#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
853#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
860 cell_structure->update_verlet_stats(n_steps, n_verlet_updates);
863 if (has_npt_enabled()) {
864 synchronize_npt_state();
874 if (boost::mpi::all_reduce(
::comm_cart, not cell_structure->use_verlet_list,
875 std::logical_or<>())) {
876 cell_structure->use_verlet_list =
false;
878 return integrated_steps;
882 bool update_accumulators) {
883 assert(n_steps >= 0);
889 if (not cell_structure->is_verlet_skin_set()) {
891 cell_structure->set_verlet_skin_heuristic();
900 if (not update_accumulators or n_steps == 0) {
901 return integrate(n_steps, reuse_forces);
904 for (
int i = 0; i < n_steps;) {
908 std::min((n_steps - i), auto_update_accumulators->next_update());
910 auto const local_retval = integrate(steps, reuse_forces);
913 std::remove_const_t<
decltype(local_retval)> global_retval;
914 boost::mpi::all_reduce(
comm_cart, local_retval, global_retval,
916 if (global_retval < 0) {
917 return global_retval;
922 (*auto_update_accumulators)(
comm_cart, steps);
932 propagation->recalc_forces =
true;
933 lees_edwards->update_box_params(*box_geo, sim_time);
@ INTEG_METHOD_NPT_ISO_AND
@ INTEG_METHOD_STEEPEST_DESCENT
@ INTEG_METHOD_SYMPLECTIC_EULER
@ INTEG_METHOD_NPT_ISO_MTK
Data structures for bonded interactions.
This file contains everything related to the global cell structure / cell system.
void lees_edwards_update(double pos_offset, double shear_velocity)
Update the Lees-Edwards parameters of the box geometry for the current simulation time.
Describes a cell structure / cell system.
void for_each_local_particle(ParticleUnaryOp &&f, bool parallel=true) const
Run a kernel on all local particles.
ParticleRange local_particles() const
void update_box_params(BoxGeometry &box_geo, double sim_time)
Update the Lees-Edwards parameters of the box geometry for the current simulation time.
void set_protocol(std::shared_ptr< ActiveProtocol > protocol)
Set a new Lees-Edwards protocol.
void unset_protocol()
Delete the currently active Lees-Edwards protocol.
ParticleRangeFiltered< Predicate > filter(Predicate pred) const
void update_default_propagation(int thermo_switch)
bool should_propagate_with(Particle const &p, int mode) const
RAII guard for signal handling.
std::shared_ptr< StokesianDynamics > stokesian_dynamics
void update_used_propagations()
Update the global propagation bitmask.
void set_sim_time(double value)
Set sim_time.
int integrate_with_signal_handler(int n_steps, int reuse_forces, bool update_accumulators)
std::shared_ptr< SteepestDescent > steepest_descent
int integrate(int n_steps, int reuse_forces)
Integrate equations of motion.
std::shared_ptr< Thermostat::Thermostat > thermostat
std::shared_ptr< CellStructure > cell_structure
std::shared_ptr< BoxGeometry > box_geo
void vs_com_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
int check_runtime_errors(boost::mpi::communicator const &comm)
Count runtime errors on all nodes.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
static bool integrator_step_1(CellStructure &cell_structure, Propagation const &propagation, System::System &system, double time_step)
Calls the hook for propagation kernels before the force calculation.
static void resort_particles_if_needed(System::System &system)
static void integrator_step_2(CellStructure &cell_structure, Propagation const &propagation, System::System &system, double time_step)
void walberla_agrid_sanity_checks(std::string method, Utils::Vector3d const &geo_left, Utils::Vector3d const &geo_right, Utils::Vector3d const &lattice_left, Utils::Vector3d const &lattice_right, double agrid)
void walberla_tau_sanity_checks(std::string method, double tau, double time_step)
Molecular dynamics integrator.
#define INTEG_ERROR_RUNTIME
#define INTEG_ERROR_SIGINT
#define INTEG_REUSE_FORCES_NEVER
recalculate forces unconditionally (mostly used for timing)
#define INTEG_REUSE_FORCES_ALWAYS
do not recalculate forces (mostly when reading checkpoints with forces)
void brownian_dynamics_rotator(BrownianThermostat const &brownian, Particle &p, double time_step, double kT)
void brownian_dynamics_propagator(BrownianThermostat const &brownian, Particle &p, double time_step, double kT)
void lb_tracers_propagate(CellStructure &cell_structure, LB::Solver const &lb, double time_step)
void lb_tracers_add_particle_force_to_fluid(CellStructure &cell_structure, BoxGeometry const &box_geo, LocalBox const &local_box, LB::Solver &lb)
@ DATA_PART_PROPERTIES
Particle::p.
Utils::Vector3d verlet_list_offset(BoxGeometry const &box, double pos_offset_at_last_resort)
double get_shear_velocity(double time, ActiveProtocol const &protocol)
Calculation of current velocity.
double get_pos_offset(double time, ActiveProtocol const &protocol)
@ TRANS_VS_CENTER_OF_MASS
@ TRANS_LB_MOMENTUM_EXCHANGE
volatile std::sig_atomic_t ctrl_C
Various procedures concerning interactions between particles.
Exports for the NpT code.
void correct_velocity_shake(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias)
Correction of current velocities using RATTLE algorithm.
void save_old_position(const ParticleRange &particles, const ParticleRange &ghost_particles)
copy current position
void correct_position_shake(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap const &bonded_ias)
Propagate velocity and position while using SHAKE algorithm for bond constraint.
void vs_relative_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
void convert_initial_torques(const ParticleRange &particles)
Convert torques to the body-fixed frame before the integration loop.
This file contains all subroutines required to process rotational motion.
See for the Stokesian dynamics method used here.
void stokesian_dynamics_step_1(ParticleRangeStokesian const &particles, StokesianDynamics const &integrator, StokesianThermostat const &stokesian, double time_step, double kT)
Struct holding all information for one particle.
void symplectic_euler_rotator_2(Particle &, double)
void symplectic_euler_rotator_1(Particle &p, double time_step)
void symplectic_euler_propagator_2(Particle &, double)
Final integration step of the Symplectic Euler integrator For symplectic Euler, there is no second st...
void symplectic_euler_propagator_1(Particle &p, double time_step)
Propagate the velocities and positions.
void velocity_verlet_rotator_1(Particle &p, double time_step)
void velocity_verlet_propagator_2(Particle &p, double time_step)
Final integration step of the Velocity Verlet integrator.
void velocity_verlet_propagator_1(Particle &p, double time_step)
Propagate the velocities and positions.
void velocity_verlet_rotator_2(Particle &p, double time_step)
void velocity_verlet_npt_MTK_step_1(ParticleRangeNPT const &particles, IsotropicNptThermostat const &npt_iso, double time_step, System::System &system)
Special propagator for velocity Verlet NpT with the Andersen method.
void velocity_verlet_npt_MTK_step_2(ParticleRangeNPT const &particles, double time_step, System::System &system)
Final integration step of the velocity Verlet NpT integrator with the MTK method.
void velocity_verlet_npt_Andersen_step_1(ParticleRangeNPT const &particles, IsotropicNptThermostat const &npt_iso, double time_step, System::System &system)
Special propagator for velocity Verlet NpT with the Andersen method.
void velocity_verlet_npt_Andersen_step_2(ParticleRangeNPT const &particles, double time_step, System::System &system)
Final integration step of the velocity Verlet NpT integrator with the Andersen method.