40#include "accumulators/AutoUpdateAccumulators.hpp"
46#include "collision_detection/CollisionDetection.hpp"
58#include "system/System.hpp"
68#include <boost/mpi/collectives/all_reduce.hpp>
70#ifdef ESPRESSO_CALIPER
71#include <caliper/cali.h>
74#ifdef ESPRESSO_VALGRIND
89#ifdef ESPRESSO_WALBERLA
90#ifdef ESPRESSO_WALBERLA_STATIC_ASSERT
91#error "waLberla headers should not be visible to the ESPResSo core"
96volatile std::sig_atomic_t
ctrl_C = 0;
107 assert(m_protocol !=
nullptr);
114 auto &system = get_system();
115 auto &cell_structure = *system.cell_structure;
116 auto &box_geo = *system.box_geo;
118 m_protocol = std::move(protocol);
119 update_box_params(box_geo, system.get_sim_time());
120 system.propagation->recalc_forces =
true;
125 auto &system = get_system();
126 auto &cell_structure = *system.cell_structure;
127 auto &box_geo = *system.box_geo;
128 m_protocol =
nullptr;
130 system.propagation->recalc_forces =
true;
146#ifdef ESPRESSO_ROTATION
151#ifdef ESPRESSO_ROTATION
156#ifdef ESPRESSO_ROTATION
161#ifdef ESPRESSO_ROTATION
175#ifdef ESPRESSO_ROTATION
179#ifdef ESPRESSO_STOKESIAN_DYNAMICS
185 throw std::runtime_error(
"Unknown value for integ_switch");
191 for (
auto &p : cell_structure->local_particles()) {
192 used_propagations |= p.propagation();
195 used_propagations |= propagation->default_propagation;
197 used_propagations = boost::mpi::all_reduce(
::comm_cart, used_propagations,
199 propagation->used_propagations = used_propagations;
200 propagation->recalc_used_propagations =
false;
203void System::System::integrator_sanity_checks()
const {
204 auto const thermo_switch = thermostat->thermo_switch;
205 if (time_step <= 0.) {
211 <<
"The steepest descent integrator is incompatible with thermostats";
217 "currently active combination of thermostats";
229 nptiso->coulomb_dipole_sanity_checks(*
this);
230 }
catch (std::runtime_error
const &err) {
241#ifdef ESPRESSO_STOKESIAN_DYNAMICS
247 if (lb.is_solver_set() and (propagation->used_propagations &
250 if (thermostat->lb ==
nullptr) {
254 if (bonded_ias->get_n_thermalized_bonds() >= 1 and
255 (thermostat->thermalized_bond ==
nullptr or
258 <<
"Thermalized bonds require the thermalized_bond thermostat";
261#ifdef ESPRESSO_ROTATION
262 for (
auto const &p : cell_structure->local_particles()) {
264 if (p.can_rotate() and not p.is_virtual() and
265 (p.propagation() & (SYSTEM_DEFAULT | ROT_EULER | ROT_LANGEVIN |
266 ROT_BROWNIAN | ROT_STOKESIAN)) == 0) {
268 <<
"Rotating particles must have a rotation propagation mode enabled";
274#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
275#ifdef ESPRESSO_EXTERNAL_FORCES
276 if (propagation->used_propagations &
278 for (
auto const &p : cell_structure->local_particles()) {
280 if ((p.propagation() & TRANS_VS_CENTER_OF_MASS) and
281 p.has_fixed_coordinates()) {
288#ifdef ESPRESSO_BOND_CONSTRAINT
289 if (bonded_ias->get_n_rigid_bonds()) {
291 for (
auto const &p : cell_structure->local_particles()) {
292 if (p.propagation() & TRANS_VS_CENTER_OF_MASS) {
293 for (
auto const bond : p.bonds()) {
294 if (std::holds_alternative<RigidBond>(
295 *bonded_ias->at(bond.bond_id()))) {
306#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
308 for (
auto const &p : cell_structure->local_particles()) {
309 if (p.stoner_wohlfarth_is_enabled()) {
310 runtimeErrorMsg() <<
"The thermal Stoner-Wohlfarth model requires the "
311 "Langevin thermostat";
319#ifdef ESPRESSO_WALBERLA
322 if (time_step <= 0.) {
326 auto const eps =
static_cast<double>(std::numeric_limits<float>::epsilon());
327 if ((tau - time_step) / (tau + time_step) < -eps)
328 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
329 ") must be >= MD time_step (" +
330 std::to_string(time_step) +
")");
331 auto const factor = tau / time_step;
332 if (std::fabs(std::round(factor) - factor) / factor > eps)
333 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
334 ") must be an integer multiple of the "
336 std::to_string(time_step) +
"). Factor is " +
337 std::to_string(factor));
347 auto const tol = agrid / 1E6;
348 if ((lattice_left - geo_left).norm2() > tol or
349 (lattice_right - geo_right).norm2() > tol) {
350 std::stringstream error_msg;
351 error_msg <<
"waLBerla and ESPResSo disagree about domain decomposition"
353 <<
"left ESPResSo: [" << geo_left <<
"], "
354 <<
"left waLBerla: [" << lattice_left <<
"]"
356 <<
"right ESPResSo: [" << geo_right <<
"], "
357 <<
"right waLBerla: [" << lattice_right <<
"]"
358 <<
"\nfor method: " << method;
359 throw std::runtime_error(error_msg.str());
365#ifdef ESPRESSO_CALIPER
366 CALI_CXX_MARK_FUNCTION;
370 *system.
box_geo, cell_structure.get_le_pos_offset_at_last_resort());
371 if (cell_structure.check_resort_required(offset)) {
382#ifdef ESPRESSO_CALIPER
383 CALI_CXX_MARK_FUNCTION;
390 auto const kT = thermostat.kT;
392#ifdef ESPRESSO_VIRTUAL_SITES
403#ifdef ESPRESSO_ROTATION
409#ifdef ESPRESSO_ROTATION
419#ifdef ESPRESSO_ROTATION
425#ifdef ESPRESSO_ROTATION
432#ifdef ESPRESSO_ROTATION
454#ifdef ESPRESSO_STOKESIAN_DYNAMICS
471#ifdef ESPRESSO_CALIPER
472 CALI_CXX_MARK_FUNCTION;
478#ifdef ESPRESSO_VIRTUAL_SITES
489#ifdef ESPRESSO_ROTATION
495#ifdef ESPRESSO_ROTATION
505#ifdef ESPRESSO_ROTATION
511#ifdef ESPRESSO_ROTATION
534#ifdef ESPRESSO_CALIPER
535 CALI_CXX_MARK_FUNCTION;
537 auto &propagation = *this->propagation;
538#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
539 auto const has_vs_rel = [&propagation]() {
540 return propagation.used_propagations &
546#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
547 auto const has_vs_com = [&propagation]() {
548 return propagation.used_propagations &
552#ifdef ESPRESSO_BOND_CONSTRAINT
553 auto const n_rigid_bonds = bonded_ias->get_n_rigid_bonds();
557 propagation.update_default_propagation(thermostat->thermo_switch);
558 update_used_propagations();
559 on_integration_start();
568 propagation.recalc_forces)) {
569#ifdef ESPRESSO_CALIPER
570 CALI_MARK_BEGIN(
"Initial Force Calculation");
572 thermostat->lb_coupling_deactivate();
574#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
579#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
586 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
591#ifdef ESPRESSO_ROTATION
596#ifdef ESPRESSO_CALIPER
597 CALI_MARK_END(
"Initial Force Calculation");
601 thermostat->lb_coupling_activate();
607 int n_verlet_updates = 0;
611 auto const singleton_mode =
comm_cart.size() == 1;
612 auto caught_sigint =
false;
613 auto caught_error =
false;
615 auto lb_active =
false;
616 auto ek_active =
false;
618 lb_active = lb.is_solver_set();
619 ek_active = ek.is_ready_for_propagation();
621 auto const calc_md_steps_per_tau = [
this](
double tau) {
622 return static_cast<int>(std::round(tau / time_step));
625#ifdef ESPRESSO_VALGRIND
626 CALLGRIND_START_INSTRUMENTATION;
629#ifdef ESPRESSO_CALIPER
630 CALI_CXX_MARK_LOOP_BEGIN(integration_loop,
"Integration loop");
632 int integrated_steps = 0;
633 for (
int step = 0; step < n_steps; step++) {
634#ifdef ESPRESSO_CALIPER
635 CALI_CXX_MARK_LOOP_ITERATION(integration_loop, step);
638#ifdef ESPRESSO_BOND_CONSTRAINT
641 cell_structure->ghost_particles());
644 lees_edwards->update_box_params(*box_geo, sim_time);
650 sim_time += time_step;
653 cell_structure->for_each_local_particle(
654 [&kernel](
Particle &p) { kernel(p); });
658 if (not has_npt_enabled())
664 thermostat->philox_counter_increment();
666#ifdef ESPRESSO_BOND_CONSTRAINT
673#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
676 if (has_npt_enabled()) {
677 cell_structure->update_ghosts_and_resort_particle(
684#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
687 if (has_npt_enabled()) {
688 cell_structure->update_ghosts_and_resort_particle(
700 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
702#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
703 integrate_magnetodynamics();
708#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
709 if (thermostat->lb and
721 cell_structure->for_each_local_particle(
722 [&kernel](
Particle &p) { kernel(p); });
724#ifdef ESPRESSO_BOND_CONSTRAINT
732 if (lb_active and ek_active) {
734 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
735 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
737 if (md_steps_per_lb_step != md_steps_per_ek_step) {
739 <<
"LB and EK are active but with different time steps.";
742 assert(lb.is_gpu() == ek.is_gpu());
743 assert(propagation.lb_skipped_md_steps ==
744 propagation.ek_skipped_md_steps);
746 propagation.lb_skipped_md_steps += 1;
747 propagation.ek_skipped_md_steps += 1;
748 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
749 propagation.lb_skipped_md_steps = 0;
750 propagation.ek_skipped_md_steps = 0;
751#ifdef ESPRESSO_CALIPER
752 CALI_MARK_BEGIN(
"lb_propagation");
755 lb.ghost_communication_vel();
756#ifdef ESPRESSO_CALIPER
757 CALI_MARK_END(
"lb_propagation");
759#ifdef ESPRESSO_CALIPER
760 CALI_MARK_BEGIN(
"ek_propagation");
763#ifdef ESPRESSO_CALIPER
764 CALI_MARK_END(
"ek_propagation");
767 }
else if (lb_active) {
768 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
769 propagation.lb_skipped_md_steps += 1;
770 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
771 propagation.lb_skipped_md_steps = 0;
772#ifdef ESPRESSO_CALIPER
773 CALI_MARK_BEGIN(
"lb_propagation");
776#ifdef ESPRESSO_CALIPER
777 CALI_MARK_END(
"lb_propagation");
780 }
else if (ek_active) {
781 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
782 propagation.ek_skipped_md_steps += 1;
783 if (propagation.ek_skipped_md_steps >= md_steps_per_ek_step) {
784 propagation.ek_skipped_md_steps = 0;
785#ifdef ESPRESSO_CALIPER
786 CALI_MARK_BEGIN(
"ek_propagation");
789#ifdef ESPRESSO_CALIPER
790 CALI_MARK_END(
"ek_propagation");
794 if (lb_active and (propagation.used_propagations &
796 thermostat->lb->rng_increment();
799#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
800 if (thermostat->lb and
802#ifdef ESPRESSO_CALIPER
803 CALI_MARK_BEGIN(
"lb_tracers_propagation");
806 lb.ghost_communication_vel();
809#ifdef ESPRESSO_CALIPER
810 CALI_MARK_END(
"lb_tracers_propagation");
815#ifdef ESPRESSO_COLLISION_DETECTION
816 cell_structure->clear_new_bonds();
817 collision_detection->handle_collisions();
818 cell_structure->rebuild_bond_list();
820 bond_breakage->process_queue(*
this);
831 if (singleton_mode and ctrl_C == 1) {
832 caught_sigint =
true;
838 lb.ghost_communication();
840 lees_edwards->update_box_params(*box_geo, sim_time);
841#ifdef ESPRESSO_CALIPER
842 CALI_CXX_MARK_LOOP_END(integration_loop);
845#ifdef ESPRESSO_VALGRIND
846 CALLGRIND_STOP_INSTRUMENTATION;
849#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
854#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
861 cell_structure->update_verlet_stats(n_steps, n_verlet_updates);
864 if (has_npt_enabled()) {
865 synchronize_npt_state();
875 if (boost::mpi::all_reduce(
::comm_cart, not cell_structure->use_verlet_list,
876 std::logical_or<>())) {
877 cell_structure->use_verlet_list =
false;
879 return integrated_steps;
883 bool update_accumulators) {
884 assert(n_steps >= 0);
890 if (not cell_structure->is_verlet_skin_set()) {
892 cell_structure->set_verlet_skin_heuristic();
901 if (not update_accumulators or n_steps == 0) {
902 return integrate(n_steps, reuse_forces);
905 for (
int i = 0; i < n_steps;) {
909 std::min((n_steps - i), auto_update_accumulators->next_update());
911 auto const local_retval = integrate(steps, reuse_forces);
914 std::remove_const_t<
decltype(local_retval)> global_retval;
915 boost::mpi::all_reduce(
comm_cart, local_retval, global_retval,
917 if (global_retval < 0) {
918 return global_retval;
923 (*auto_update_accumulators)(
comm_cart, steps);
933 propagation->recalc_forces =
true;
934 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(Callable &&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.
void walberla_tau_sanity_checks(std::string const &method, double tau, double time_step)
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 const &method, Utils::Vector3d const &geo_left, Utils::Vector3d const &geo_right, Utils::Vector3d const &lattice_left, Utils::Vector3d const &lattice_right, double agrid)
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.