41#include "accumulators/AutoUpdateAccumulators.hpp"
46#include "collision_detection/CollisionDetection.hpp"
59#include "system/System.hpp"
66#include <boost/mpi/collectives/all_reduce.hpp>
68#ifdef ESPRESSO_CALIPER
69#include <caliper/cali.h>
72#ifdef ESPRESSO_VALGRIND
86#ifdef ESPRESSO_WALBERLA
87#ifdef ESPRESSO_WALBERLA_STATIC_ASSERT
88#error "waLberla headers should not be visible to the ESPResSo core"
93volatile std::sig_atomic_t
ctrl_C = 0;
104 assert(m_protocol !=
nullptr);
111 auto &system = get_system();
112 auto &cell_structure = *system.cell_structure;
113 auto &box_geo = *system.box_geo;
115 m_protocol = std::move(protocol);
116 update_box_params(box_geo, system.get_sim_time());
117 system.propagation->recalc_forces =
true;
122 auto &system = get_system();
123 auto &cell_structure = *system.cell_structure;
124 auto &box_geo = *system.box_geo;
125 m_protocol =
nullptr;
127 system.propagation->recalc_forces =
true;
143#ifdef ESPRESSO_ROTATION
148#ifdef ESPRESSO_ROTATION
153#ifdef ESPRESSO_ROTATION
158#ifdef ESPRESSO_ROTATION
172#ifdef ESPRESSO_ROTATION
176#ifdef ESPRESSO_STOKESIAN_DYNAMICS
182 throw std::runtime_error(
"Unknown value for integ_switch");
188 for (
auto &p : cell_structure->local_particles()) {
189 used_propagations |= p.propagation();
192 used_propagations |= propagation->default_propagation;
194 used_propagations = boost::mpi::all_reduce(
::comm_cart, used_propagations,
196 propagation->used_propagations = used_propagations;
199void System::System::integrator_sanity_checks()
const {
200 auto const thermo_switch = thermostat->thermo_switch;
201 if (time_step <= 0.) {
207 <<
"The steepest descent integrator is incompatible with thermostats";
213 "currently active combination of thermostats";
225 nptiso->coulomb_dipole_sanity_checks(*
this);
226 }
catch (std::runtime_error
const &err) {
237#ifdef ESPRESSO_STOKESIAN_DYNAMICS
243 if (lb.is_solver_set() and (propagation->used_propagations &
246 if (thermostat->lb ==
nullptr) {
250 if (bonded_ias->get_n_thermalized_bonds() >= 1 and
251 (thermostat->thermalized_bond ==
nullptr or
254 <<
"Thermalized bonds require the thermalized_bond thermostat";
257#ifdef ESPRESSO_ROTATION
258 for (
auto const &p : cell_structure->local_particles()) {
260 if (p.can_rotate() and not p.is_virtual() and
261 (p.propagation() & (SYSTEM_DEFAULT | ROT_EULER | ROT_LANGEVIN |
262 ROT_BROWNIAN | ROT_STOKESIAN)) == 0) {
264 <<
"Rotating particles must have a rotation propagation mode enabled";
270#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
272 for (
auto const &p : cell_structure->local_particles()) {
273 if (p.stoner_wohlfarth_is_enabled()) {
274 runtimeErrorMsg() <<
"The thermal Stoner-Wohlfarth model requires the "
275 "Langevin thermostat";
283#ifdef ESPRESSO_WALBERLA
286 if (time_step <= 0.) {
290 auto const eps =
static_cast<double>(std::numeric_limits<float>::epsilon());
291 if ((tau - time_step) / (tau + time_step) < -eps)
292 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
293 ") must be >= MD time_step (" +
294 std::to_string(time_step) +
")");
295 auto const factor = tau / time_step;
296 if (std::fabs(std::round(factor) - factor) / factor > eps)
297 throw std::invalid_argument(method +
" tau (" + std::to_string(tau) +
298 ") must be an integer multiple of the "
300 std::to_string(time_step) +
"). Factor is " +
301 std::to_string(factor));
311 auto const tol = agrid / 1E6;
312 if ((lattice_left - geo_left).norm2() > tol or
313 (lattice_right - geo_right).norm2() > tol) {
315 <<
"left ESPResSo: [" << geo_left <<
"], "
316 <<
"left waLBerla: [" << lattice_left <<
"]"
318 <<
"right ESPResSo: [" << geo_right <<
"], "
319 <<
"right waLBerla: [" << lattice_right <<
"]"
320 <<
"\nfor method: " << method;
321 throw std::runtime_error(
322 "waLBerla and ESPResSo disagree about domain decomposition.");
328#ifdef ESPRESSO_CALIPER
329 CALI_CXX_MARK_FUNCTION;
333 *system.
box_geo, cell_structure.get_le_pos_offset_at_last_resort());
334 if (cell_structure.check_resort_required(offset)) {
345#ifdef ESPRESSO_CALIPER
346 CALI_CXX_MARK_FUNCTION;
353 auto const kT = thermostat.kT;
355#ifdef ESPRESSO_VIRTUAL_SITES
366#ifdef ESPRESSO_ROTATION
372#ifdef ESPRESSO_ROTATION
382#ifdef ESPRESSO_ROTATION
388#ifdef ESPRESSO_ROTATION
395#ifdef ESPRESSO_ROTATION
417#ifdef ESPRESSO_STOKESIAN_DYNAMICS
422 *thermostat.stokesian, time_step, kT);
433#ifdef ESPRESSO_CALIPER
434 CALI_CXX_MARK_FUNCTION;
440#ifdef ESPRESSO_VIRTUAL_SITES
451#ifdef ESPRESSO_ROTATION
457#ifdef ESPRESSO_ROTATION
467#ifdef ESPRESSO_ROTATION
473#ifdef ESPRESSO_ROTATION
496#ifdef ESPRESSO_CALIPER
497 CALI_CXX_MARK_FUNCTION;
499 auto &propagation = *this->propagation;
500#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
501 auto const has_vs_rel = [&propagation]() {
502 return propagation.used_propagations &
508#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
509 auto const has_vs_com = [&propagation]() {
510 return propagation.used_propagations &
514#ifdef ESPRESSO_BOND_CONSTRAINT
515 auto const n_rigid_bonds = bonded_ias->get_n_rigid_bonds();
519 propagation.update_default_propagation(thermostat->thermo_switch);
520 update_used_propagations();
521 on_integration_start();
530 propagation.recalc_forces)) {
531#ifdef ESPRESSO_CALIPER
532 CALI_MARK_BEGIN(
"Initial Force Calculation");
534 thermostat->lb_coupling_deactivate();
536#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
541#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
548 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
553#ifdef ESPRESSO_ROTATION
558#ifdef ESPRESSO_CALIPER
559 CALI_MARK_END(
"Initial Force Calculation");
563 thermostat->lb_coupling_activate();
569 int n_verlet_updates = 0;
573 auto const singleton_mode =
comm_cart.size() == 1;
574 auto caught_sigint =
false;
575 auto caught_error =
false;
577 auto lb_active =
false;
578 auto ek_active =
false;
580 lb_active = lb.is_solver_set();
581 ek_active = ek.is_ready_for_propagation();
583 auto const calc_md_steps_per_tau = [
this](
double tau) {
584 return static_cast<int>(std::round(tau / time_step));
587#ifdef ESPRESSO_VALGRIND
588 CALLGRIND_START_INSTRUMENTATION;
591#ifdef ESPRESSO_CALIPER
592 CALI_CXX_MARK_LOOP_BEGIN(integration_loop,
"Integration loop");
594 int integrated_steps = 0;
595 for (
int step = 0; step < n_steps; step++) {
596#ifdef ESPRESSO_CALIPER
597 CALI_CXX_MARK_LOOP_ITERATION(integration_loop, step);
600#ifdef ESPRESSO_BOND_CONSTRAINT
603 cell_structure->ghost_particles());
606 lees_edwards->update_box_params(*box_geo, sim_time);
612 sim_time += time_step;
615 cell_structure->for_each_local_particle(
616 [&kernel](
Particle &p) { kernel(p); });
620 if (not has_npt_enabled())
626 thermostat->philox_counter_increment();
628#ifdef ESPRESSO_BOND_CONSTRAINT
635#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
638 if (has_npt_enabled()) {
639 cell_structure->update_ghosts_and_resort_particle(
646#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
649 if (has_npt_enabled()) {
650 cell_structure->update_ghosts_and_resort_particle(
662 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
664#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
670#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
671 if (thermostat->lb and
683 cell_structure->for_each_local_particle(
684 [&kernel](
Particle &p) { kernel(p); });
686#ifdef ESPRESSO_BOND_CONSTRAINT
694 if (lb_active and ek_active) {
696 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
697 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
699 if (md_steps_per_lb_step != md_steps_per_ek_step) {
701 <<
"LB and EK are active but with different time steps.";
704 assert(lb.is_gpu() == ek.is_gpu());
705 assert(propagation.lb_skipped_md_steps ==
706 propagation.ek_skipped_md_steps);
708 propagation.lb_skipped_md_steps += 1;
709 propagation.ek_skipped_md_steps += 1;
710 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
711 propagation.lb_skipped_md_steps = 0;
712 propagation.ek_skipped_md_steps = 0;
713#ifdef ESPRESSO_CALIPER
714 CALI_MARK_BEGIN(
"lb_propagation");
717 lb.ghost_communication_vel();
718#ifdef ESPRESSO_CALIPER
719 CALI_MARK_END(
"lb_propagation");
720 CALI_MARK_BEGIN(
"ek_propagation");
723#ifdef ESPRESSO_CALIPER
724 CALI_MARK_END(
"ek_propagation");
727 }
else if (lb_active) {
728 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
729 propagation.lb_skipped_md_steps += 1;
730 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
731 propagation.lb_skipped_md_steps = 0;
732#ifdef ESPRESSO_CALIPER
733 CALI_MARK_BEGIN(
"lb_propagation");
736#ifdef ESPRESSO_CALIPER
737 CALI_MARK_END(
"lb_propagation");
740 }
else if (ek_active) {
741 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
742 propagation.ek_skipped_md_steps += 1;
743 if (propagation.ek_skipped_md_steps >= md_steps_per_ek_step) {
744 propagation.ek_skipped_md_steps = 0;
745#ifdef ESPRESSO_CALIPER
746 CALI_MARK_BEGIN(
"ek_propagation");
749#ifdef ESPRESSO_CALIPER
750 CALI_MARK_END(
"ek_propagation");
754 if (lb_active and (propagation.used_propagations &
756 thermostat->lb->rng_increment();
759#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
760 if (thermostat->lb and
762#ifdef ESPRESSO_CALIPER
763 CALI_MARK_BEGIN(
"lb_tracers_propagation");
766 lb.ghost_communication_vel();
769#ifdef ESPRESSO_CALIPER
770 CALI_MARK_END(
"lb_tracers_propagation");
775#ifdef ESPRESSO_COLLISION_DETECTION
776 collision_detection->handle_collisions();
778 bond_breakage->process_queue(*
this);
789 if (singleton_mode and ctrl_C == 1) {
790 caught_sigint =
true;
796 lb.ghost_communication();
798 lees_edwards->update_box_params(*box_geo, sim_time);
799#ifdef ESPRESSO_CALIPER
800 CALI_CXX_MARK_LOOP_END(integration_loop);
803#ifdef ESPRESSO_VALGRIND
804 CALLGRIND_STOP_INSTRUMENTATION;
807#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
812#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
819 cell_structure->update_verlet_stats(n_steps, n_verlet_updates);
822 if (has_npt_enabled()) {
823 synchronize_npt_state();
833#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
834 if (boost::mpi::all_reduce(
::comm_cart, not cell_structure->use_verlet_list,
835 std::logical_or<>())) {
836 cell_structure->use_verlet_list =
false;
839 return integrated_steps;
843 bool update_accumulators) {
844 assert(n_steps >= 0);
850 if (not cell_structure->is_verlet_skin_set()) {
852 cell_structure->set_verlet_skin_heuristic();
861 if (not update_accumulators or n_steps == 0) {
862 return integrate(n_steps, reuse_forces);
865 for (
int i = 0; i < n_steps;) {
869 std::min((n_steps - i), auto_update_accumulators->next_update());
871 auto const local_retval = integrate(steps, reuse_forces);
874 std::remove_const_t<
decltype(local_retval)> global_retval;
875 boost::mpi::all_reduce(
comm_cart, local_retval, global_retval,
877 if (global_retval < 0) {
878 return global_retval;
883 (*auto_update_accumulators)(
comm_cart, steps);
893 propagation->recalc_forces =
true;
894 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.
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)
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.
bool steepest_descent_step(const ParticleRange &particles)
Steepest descent integrator.
void stokesian_dynamics_step_1(ParticleRangeStokesian const &particles, StokesianThermostat const &stokesian, double time_step, double kT)
void run_magnetodynamics(CellStructure &cell_structure, Thermostat::Thermostat const &thermostat)
Run magnetodynamics update for local virtual particles.
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 NpT isotropic for MTK approach.
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 for Andersen method.
void velocity_verlet_npt_Andersen_step_1(ParticleRangeNPT const &particles, IsotropicNptThermostat const &npt_iso, double time_step, System::System &system)
Special propagator for NpT isotropic for 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 for Andersen method.