ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
integrate.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22/** \file
23 * Molecular dynamics integrator.
24 *
25 * For more information about the integrator
26 * see \ref integrate.hpp "integrate.hpp".
27 */
28
29#include "integrate.hpp"
37
38#include "BoxGeometry.hpp"
39#include "PropagationMode.hpp"
40#include "accumulators/AutoUpdateAccumulators.hpp"
45#include "cells.hpp"
46#include "collision_detection/CollisionDetection.hpp"
47#include "communication.hpp"
48#include "errorhandling.hpp"
50#include "lb/utils.hpp"
53#include "npt.hpp"
54#include "rattle.hpp"
55#include "rotation.hpp"
56#include "signalhandling.hpp"
58#include "system/System.hpp"
60#include "thermostat.hpp"
62#include "virtual_sites/com.hpp"
65
67
68#include <boost/mpi/collectives/all_reduce.hpp>
69
70#ifdef ESPRESSO_CALIPER
71#include <caliper/cali.h>
72#endif
73
74#ifdef ESPRESSO_VALGRIND
75#include <callgrind.h>
76#endif
77
78#include <algorithm>
79#include <cassert>
80#include <cmath>
81#include <csignal>
82#include <functional>
83#include <limits>
84#include <sstream>
85#include <stdexcept>
86#include <string>
87#include <utility>
88
89#ifdef ESPRESSO_WALBERLA
90#ifdef ESPRESSO_WALBERLA_STATIC_ASSERT
91#error "waLberla headers should not be visible to the ESPResSo core"
92#endif
93#endif
94
95namespace {
96volatile std::sig_atomic_t ctrl_C = 0;
97} // namespace
98
99namespace LeesEdwards {
100
101/**
102 * @brief Update the Lees-Edwards parameters of the box geometry
103 * for the current simulation time.
104 */
105void LeesEdwards::update_box_params(BoxGeometry &box_geo, double sim_time) {
106 if (box_geo.type() == BoxType::LEES_EDWARDS) {
107 assert(m_protocol != nullptr);
108 box_geo.lees_edwards_update(get_pos_offset(sim_time, *m_protocol),
109 get_shear_velocity(sim_time, *m_protocol));
110 }
111}
112
113void LeesEdwards::set_protocol(std::shared_ptr<ActiveProtocol> protocol) {
114 auto &system = get_system();
115 auto &cell_structure = *system.cell_structure;
116 auto &box_geo = *system.box_geo;
117 box_geo.set_type(BoxType::LEES_EDWARDS);
118 m_protocol = std::move(protocol);
119 update_box_params(box_geo, system.get_sim_time());
120 system.propagation->recalc_forces = true;
121 cell_structure.set_resort_particles(Cells::RESORT_LOCAL);
122}
123
125 auto &system = get_system();
126 auto &cell_structure = *system.cell_structure;
127 auto &box_geo = *system.box_geo;
128 m_protocol = nullptr;
129 box_geo.set_type(BoxType::CUBOID);
130 system.propagation->recalc_forces = true;
131 cell_structure.set_resort_particles(Cells::RESORT_LOCAL);
132}
133
134} // namespace LeesEdwards
135
137 switch (integ_switch) {
140 break;
141 case INTEG_METHOD_NVT:
143 // NOLINTNEXTLINE(bugprone-branch-clone)
144 if ((thermo_switch & THERMO_LB) and (thermo_switch & THERMO_LANGEVIN)) {
146#ifdef ESPRESSO_ROTATION
148#endif
149 } else if (thermo_switch & THERMO_LB) {
151#ifdef ESPRESSO_ROTATION
153#endif
154 } else if (thermo_switch & THERMO_LANGEVIN) {
156#ifdef ESPRESSO_ROTATION
158#endif
159 } else {
161#ifdef ESPRESSO_ROTATION
163#endif
164 }
165 break;
166 }
167#ifdef ESPRESSO_NPT
171 break;
172#endif
173 case INTEG_METHOD_BD:
175#ifdef ESPRESSO_ROTATION
177#endif
178 break;
179#ifdef ESPRESSO_STOKESIAN_DYNAMICS
180 case INTEG_METHOD_SD:
182 break;
183#endif // ESPRESSO_STOKESIAN_DYNAMICS
184 default:
185 throw std::runtime_error("Unknown value for integ_switch");
186 }
187}
188
190 int used_propagations = PropagationMode::NONE;
191 for (auto &p : cell_structure->local_particles()) {
192 used_propagations |= p.propagation();
193 }
194 if (used_propagations & PropagationMode::SYSTEM_DEFAULT) {
195 used_propagations |= propagation->default_propagation;
196 }
197 used_propagations = boost::mpi::all_reduce(::comm_cart, used_propagations,
198 std::bit_or<int>());
199 propagation->used_propagations = used_propagations;
200 propagation->recalc_used_propagations = false;
201}
202
203void System::System::integrator_sanity_checks() const {
204 auto const thermo_switch = thermostat->thermo_switch;
205 if (time_step <= 0.) {
206 runtimeErrorMsg() << "time_step not set";
207 }
208 if (propagation->integ_switch == INTEG_METHOD_STEEPEST_DESCENT) {
209 if (thermo_switch != THERMO_OFF) {
211 << "The steepest descent integrator is incompatible with thermostats";
212 }
213 }
214 if (propagation->integ_switch == INTEG_METHOD_NVT) {
215 if (thermo_switch & (THERMO_NPT_ISO | THERMO_BROWNIAN | THERMO_SD)) {
216 runtimeErrorMsg() << "The VV integrator is incompatible with the "
217 "currently active combination of thermostats";
218 }
219 }
220#ifdef ESPRESSO_NPT
221 if (propagation->used_propagations & PropagationMode::TRANS_LANGEVIN_NPT) {
222 if (thermo_switch != THERMO_NPT_ISO) {
223 runtimeErrorMsg() << "The NpT integrator requires the NpT thermostat";
224 }
225 if (box_geo->type() == BoxType::LEES_EDWARDS) {
226 runtimeErrorMsg() << "The NpT integrator cannot use Lees-Edwards";
227 }
228 try {
229 nptiso->coulomb_dipole_sanity_checks(*this);
230 } catch (std::runtime_error const &err) {
231 runtimeErrorMsg() << err.what();
232 }
233 }
234#endif
235 if (propagation->used_propagations & PropagationMode::TRANS_BROWNIAN) {
236 if (thermo_switch != THERMO_BROWNIAN) {
237 runtimeErrorMsg() << "The BD integrator requires the BD thermostat";
238 }
239 }
240 if (propagation->used_propagations & PropagationMode::TRANS_STOKESIAN) {
241#ifdef ESPRESSO_STOKESIAN_DYNAMICS
242 if (thermo_switch != THERMO_SD) {
243 runtimeErrorMsg() << "The SD integrator requires the SD thermostat";
244 }
245#endif
246 }
247 if (lb.is_solver_set() and (propagation->used_propagations &
250 if (thermostat->lb == nullptr) {
251 runtimeErrorMsg() << "The LB integrator requires the LB thermostat";
252 }
253 }
254 if (bonded_ias->get_n_thermalized_bonds() >= 1 and
255 (thermostat->thermalized_bond == nullptr or
256 (thermo_switch & THERMO_BOND) == 0)) {
258 << "Thermalized bonds require the thermalized_bond thermostat";
259 }
260#ifdef ESPRESSO_BOND_CONSTRAINT
261 if (bonded_ias->get_n_rigid_bonds() >= 1) {
262 if (not propagation->is_inertial()) {
264 << "Rigid bonds (RATTLE) require an inertial integrator "
265 "(VV or symplectic Euler); BD and SD are not supported";
266 }
267 }
268#endif
269
270#ifdef ESPRESSO_ROTATION
271 for (auto const &p : cell_structure->local_particles()) {
272 using namespace PropagationMode;
273 if (p.can_rotate() and not p.is_virtual() and
274 (p.propagation() & (SYSTEM_DEFAULT | ROT_EULER | ROT_LANGEVIN |
275 ROT_BROWNIAN | ROT_STOKESIAN)) == 0) {
277 << "Rotating particles must have a rotation propagation mode enabled";
278 break;
279 }
280 }
281#endif // ESPRESSO_ROTATION
282
283#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
284#ifdef ESPRESSO_EXTERNAL_FORCES
285 if (propagation->used_propagations &
287 for (auto const &p : cell_structure->local_particles()) {
288 using namespace PropagationMode;
289 if ((p.propagation() & TRANS_VS_CENTER_OF_MASS) and
290 p.has_fixed_coordinates()) {
291 runtimeErrorMsg() << "VS COM particles cannot be fixed in space";
292 break;
293 }
294 }
295 }
296#endif // ESPRESSO_EXTERNAL_FORCES
297#ifdef ESPRESSO_BOND_CONSTRAINT
298 if (bonded_ias->get_n_rigid_bonds()) {
299 using namespace PropagationMode;
300 for (auto const &p : cell_structure->local_particles()) {
301 if (p.propagation() & TRANS_VS_CENTER_OF_MASS) {
302 for (auto const bond : p.bonds()) {
303 if (std::holds_alternative<RigidBond>(
304 *bonded_ias->at(bond.bond_id()))) {
305 runtimeErrorMsg() << "VS COM particles cannot use rigid bonds";
306 break;
307 }
308 }
309 }
310 }
311 }
312#endif // ESPRESSO_BOND_CONSTRAINT
313#endif // ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
314
315#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
316 if ((thermo_switch & THERMO_LANGEVIN) == 0) {
317 for (auto const &p : cell_structure->local_particles()) {
318 if (p.stoner_wohlfarth_is_enabled()) {
319 runtimeErrorMsg() << "The thermal Stoner-Wohlfarth model requires the "
320 "Langevin thermostat";
321 break;
322 }
323 }
324 }
325#endif // ESPRESSO_THERMAL_STONER_WOHLFARTH
326}
327
328#ifdef ESPRESSO_WALBERLA
329void walberla_tau_sanity_checks(std::string const &method, double tau,
330 double time_step) {
331 if (time_step <= 0.) {
332 return;
333 }
334 // use float epsilon since tau may be a float
335 auto const eps = static_cast<double>(std::numeric_limits<float>::epsilon());
336 if ((tau - time_step) / (tau + time_step) < -eps)
337 throw std::invalid_argument(method + " tau (" + std::to_string(tau) +
338 ") must be >= MD time_step (" +
339 std::to_string(time_step) + ")");
340 auto const factor = tau / time_step;
341 if (std::fabs(std::round(factor) - factor) / factor > eps)
342 throw std::invalid_argument(method + " tau (" + std::to_string(tau) +
343 ") must be an integer multiple of the "
344 "MD time_step (" +
345 std::to_string(time_step) + "). Factor is " +
346 std::to_string(factor));
347}
348
349void walberla_agrid_sanity_checks(std::string const &method,
354 double agrid) {
355 // waLBerla and ESPResSo must agree on domain decomposition
356 auto const tol = agrid / 1E6;
357 if ((lattice_left - geo_left).norm2() > tol or
358 (lattice_right - geo_right).norm2() > tol) {
359 std::stringstream error_msg;
360 error_msg << "waLBerla and ESPResSo disagree about domain decomposition"
361 << "\nMPI rank " << ::this_node << ": "
362 << "left ESPResSo: [" << geo_left << "], "
363 << "left waLBerla: [" << lattice_left << "]"
364 << "\nMPI rank " << ::this_node << ": "
365 << "right ESPResSo: [" << geo_right << "], "
366 << "right waLBerla: [" << lattice_right << "]"
367 << "\nfor method: " << method;
368 throw std::runtime_error(error_msg.str());
369 }
370}
371#endif // ESPRESSO_WALBERLA
372
374#ifdef ESPRESSO_CALIPER
376#endif
377 auto &cell_structure = *system.cell_structure;
378 auto const offset = LeesEdwards::verlet_list_offset(
379 *system.box_geo, cell_structure.get_le_pos_offset_at_last_resort());
380 if (cell_structure.check_resort_required(offset)) {
381 cell_structure.set_resort_particles(Cells::RESORT_LOCAL);
382 }
383}
384
385/** @brief Calls the hook for propagation kernels before the force calculation
386 * @return whether or not to stop the integration loop early.
387 */
388static bool integrator_step_1(CellStructure &cell_structure,
389 Propagation const &propagation,
390 System::System &system, double time_step) {
391#ifdef ESPRESSO_CALIPER
393#endif
394 // steepest decent
396 return system.steepest_descent->propagate(cell_structure);
397
398 auto const &thermostat = *system.thermostat;
399 auto const kT = thermostat.kT;
400 cell_structure.for_each_local_particle([&](Particle &p) {
401#ifdef ESPRESSO_VIRTUAL_SITES
402 // virtual sites are updated later in the integration loop
403 if (p.is_virtual())
404 return;
405#endif
406 if (propagation.integ_switch == INTEG_METHOD_SYMPLECTIC_EULER) {
407 if (propagation.should_propagate_with(
409 symplectic_euler_propagator_1(p, time_step);
411 symplectic_euler_propagator_1(p, time_step);
412#ifdef ESPRESSO_ROTATION
414 symplectic_euler_rotator_1(p, time_step);
415#endif
417 symplectic_euler_propagator_1(p, time_step);
418#ifdef ESPRESSO_ROTATION
420 symplectic_euler_rotator_1(p, time_step);
421#endif
422 } else {
423 if (propagation.should_propagate_with(
425 velocity_verlet_propagator_1(p, time_step);
427 velocity_verlet_propagator_1(p, time_step);
428#ifdef ESPRESSO_ROTATION
430 velocity_verlet_rotator_1(p, time_step);
431#endif
433 velocity_verlet_propagator_1(p, time_step);
434#ifdef ESPRESSO_ROTATION
436 velocity_verlet_rotator_1(p, time_step);
437#endif
438 }
440 brownian_dynamics_propagator(*thermostat.brownian, p, time_step, kT);
441#ifdef ESPRESSO_ROTATION
443 brownian_dynamics_rotator(*thermostat.brownian, p, time_step, kT);
444#endif
445 });
446
447#ifdef ESPRESSO_NPT
451 if (propagation.integ_switch == INTEG_METHOD_NPT_ISO_AND) {
453 cell_structure.local_particles().filter(pred), *thermostat.npt_iso,
454 time_step, system);
455 } else if (propagation.integ_switch == INTEG_METHOD_NPT_ISO_MTK) {
457 cell_structure.local_particles().filter(pred), *thermostat.npt_iso,
458 time_step, system);
459 }
460 }
461#endif
462
463#ifdef ESPRESSO_STOKESIAN_DYNAMICS
468 *system.stokesian_dynamics, *thermostat.stokesian,
469 time_step, kT);
470 }
471#endif // ESPRESSO_STOKESIAN_DYNAMICS
472
473 return false;
474}
475
476static void integrator_step_2(CellStructure &cell_structure,
477 Propagation const &propagation,
479 double time_step) {
480#ifdef ESPRESSO_CALIPER
482#endif
484 return;
485
486 cell_structure.for_each_local_particle([&](Particle &p) {
487#ifdef ESPRESSO_VIRTUAL_SITES
488 // virtual sites are updated later in the integration loop
489 if (p.is_virtual())
490 return;
491#endif
492 if (propagation.integ_switch == INTEG_METHOD_SYMPLECTIC_EULER) {
493 if (propagation.should_propagate_with(
495 symplectic_euler_propagator_2(p, time_step);
497 symplectic_euler_propagator_2(p, time_step);
498#ifdef ESPRESSO_ROTATION
500 symplectic_euler_rotator_2(p, time_step);
501#endif
503 symplectic_euler_propagator_2(p, time_step);
504#ifdef ESPRESSO_ROTATION
506 symplectic_euler_rotator_2(p, time_step);
507#endif
508 } else {
509 if (propagation.should_propagate_with(
511 velocity_verlet_propagator_2(p, time_step);
513 velocity_verlet_propagator_2(p, time_step);
514#ifdef ESPRESSO_ROTATION
516 velocity_verlet_rotator_2(p, time_step);
517#endif
519 velocity_verlet_propagator_2(p, time_step);
520#ifdef ESPRESSO_ROTATION
522 velocity_verlet_rotator_2(p, time_step);
523#endif
524 }
525 });
526
527#ifdef ESPRESSO_NPT
531 if (propagation.integ_switch == INTEG_METHOD_NPT_ISO_AND) {
533 cell_structure.local_particles().filter(pred), time_step, system);
534 } else if (propagation.integ_switch == INTEG_METHOD_NPT_ISO_MTK) {
536 cell_structure.local_particles().filter(pred), time_step, system);
537 }
538 }
539#endif
540}
541
543#ifdef ESPRESSO_CALIPER
545#endif
546 auto &propagation = *this->propagation;
547#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
548 auto const has_vs_rel = [&propagation]() {
549 return propagation.used_propagations &
553 };
554#endif
555#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
556 auto const has_vs_com = [&propagation]() {
557 return propagation.used_propagations &
559 };
560#endif
561#ifdef ESPRESSO_BOND_CONSTRAINT
562 auto const n_rigid_bonds = bonded_ias->get_n_rigid_bonds();
563#endif
564
565 // Prepare particle structure and run sanity checks of all active algorithms
566 propagation.update_default_propagation(thermostat->thermo_switch);
567 update_used_propagations();
568 on_integration_start();
569
570 // If any method vetoes (e.g. P3M not initialized), immediately bail out
572 return INTEG_ERROR_RUNTIME;
573
574 // Additional preparations for the first integration step
577 propagation.recalc_forces)) {
578#ifdef ESPRESSO_CALIPER
579 CALI_MARK_BEGIN("Initial Force Calculation");
580#endif
581 thermostat->lb_coupling_deactivate();
582
583#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
584 if (has_vs_rel()) {
585 vs_relative_update_particles(*cell_structure, *box_geo);
586 }
587#endif
588#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
589 if (has_vs_com()) {
590 vs_com_update_particles(*cell_structure, *box_geo);
591 }
592#endif
593
594 // Communication step: distribute ghost positions
595 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
596
597 calculate_forces();
598
599 if (propagation.integ_switch != INTEG_METHOD_STEEPEST_DESCENT) {
600#ifdef ESPRESSO_ROTATION
601 convert_initial_torques(cell_structure->local_particles());
602#endif
603 }
604
605#ifdef ESPRESSO_CALIPER
606 CALI_MARK_END("Initial Force Calculation");
607#endif
608 }
609
610 thermostat->lb_coupling_activate();
611
613 return INTEG_ERROR_RUNTIME;
614
615 // Keep track of the number of Verlet updates (i.e. particle resorts)
616 int n_verlet_updates = 0;
617
618 // Keep track of whether an interrupt signal was caught (only in singleton
619 // mode, since signal handlers are unreliable with more than 1 MPI rank)
620 auto const singleton_mode = comm_cart.size() == 1;
621 auto caught_sigint = false;
622 auto caught_error = false;
623
624 auto lb_active = false;
625 auto ek_active = false;
626 if (propagation.integ_switch != INTEG_METHOD_STEEPEST_DESCENT) {
627 lb_active = lb.is_solver_set();
628 ek_active = ek.is_ready_for_propagation();
629 }
630 auto const calc_md_steps_per_tau = [this](double tau) {
631 return static_cast<int>(std::round(tau / time_step));
632 };
633
634#ifdef ESPRESSO_VALGRIND
636#endif
637 // Integration loop
638#ifdef ESPRESSO_CALIPER
639 CALI_CXX_MARK_LOOP_BEGIN(integration_loop, "Integration loop");
640#endif
641 int integrated_steps = 0;
642 for (int step = 0; step < n_steps; step++) {
643#ifdef ESPRESSO_CALIPER
645#endif
646
647#ifdef ESPRESSO_BOND_CONSTRAINT
648 if (n_rigid_bonds)
649 save_old_position(cell_structure->local_particles(),
650 cell_structure->ghost_particles());
651#endif
652
653 lees_edwards->update_box_params(*box_geo, sim_time);
654 bool early_exit =
655 integrator_step_1(*cell_structure, propagation, *this, time_step);
656 if (early_exit)
657 break;
658
659 sim_time += time_step;
660 if (box_geo->type() == BoxType::LEES_EDWARDS) {
661 auto const kernel = LeesEdwards::Push{*box_geo};
662 cell_structure->for_each_local_particle(
663 [&kernel](Particle &p) { kernel(p); });
664 }
665
666#ifdef ESPRESSO_NPT
667 if (not has_npt_enabled())
668#endif
669 {
671 }
672 // Propagate philox RNG counters
673 thermostat->philox_counter_increment();
674
675#ifdef ESPRESSO_BOND_CONSTRAINT
676 // Correct particle positions that participate in a rigid/constrained bond
677 if (n_rigid_bonds) {
678 correct_position_shake(*cell_structure, *box_geo, *bonded_ias);
679 }
680#endif
681
682#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
683 if (has_vs_rel()) {
684#ifdef ESPRESSO_NPT
685 if (has_npt_enabled()) {
686 cell_structure->update_ghosts_and_resort_particle(
688 }
689#endif // ESPRESSO_NPT
690 vs_relative_update_particles(*cell_structure, *box_geo);
691 }
692#endif // ESPRESSO_VIRTUAL_SITES_RELATIVE
693#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
694 if (has_vs_com()) {
695#ifdef ESPRESSO_NPT
696 if (has_npt_enabled()) {
697 cell_structure->update_ghosts_and_resort_particle(
699 }
700#endif // ESPRESSO_NPT
701 vs_com_update_particles(*cell_structure, *box_geo);
702 }
703#endif // ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
704
705 if (cell_structure->get_resort_particles() >= Cells::RESORT_LOCAL)
707
708 // Communication step: distribute ghost positions
709 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
710
711#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
712 integrate_magnetodynamics();
713#endif
714
715 calculate_forces();
716
717#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
718 if (thermostat->lb and
719 (propagation.used_propagations & PropagationMode::TRANS_LB_TRACER)) {
720 lb_tracers_add_particle_force_to_fluid(*cell_structure, *box_geo,
721 *local_geo, lb);
722 }
723#endif
724 integrator_step_2(*cell_structure, propagation, *this, time_step);
725 if (propagation.integ_switch == INTEG_METHOD_BD) {
727 }
728 if (box_geo->type() == BoxType::LEES_EDWARDS) {
729 auto const kernel = LeesEdwards::UpdateOffset{*box_geo};
730 cell_structure->for_each_local_particle(
731 [&kernel](Particle &p) { kernel(p); });
732 }
733#ifdef ESPRESSO_BOND_CONSTRAINT
734 if (n_rigid_bonds) {
735 correct_velocity_shake(*cell_structure, *box_geo, *bonded_ias);
736 }
737#endif
738
739 // propagate one-step functionalities
740 if (propagation.integ_switch != INTEG_METHOD_STEEPEST_DESCENT) {
741 if (lb_active and ek_active) {
742 // assume that they are coupled, which is not necessarily true
743 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
744 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
745
748 << "LB and EK are active but with different time steps.";
749 }
750
751 assert(lb.is_gpu() == ek.is_gpu());
752 assert(propagation.lb_skipped_md_steps ==
753 propagation.ek_skipped_md_steps);
754
755 propagation.lb_skipped_md_steps += 1;
756 propagation.ek_skipped_md_steps += 1;
757 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
758 propagation.lb_skipped_md_steps = 0;
759 propagation.ek_skipped_md_steps = 0;
760#ifdef ESPRESSO_CALIPER
761 CALI_MARK_BEGIN("lb_propagation");
762#endif
763 lb.propagate();
764 lb.ghost_communication_vel();
765#ifdef ESPRESSO_CALIPER
766 CALI_MARK_END("lb_propagation");
767#endif
768#ifdef ESPRESSO_CALIPER
769 CALI_MARK_BEGIN("ek_propagation");
770#endif
771 ek.propagate();
772#ifdef ESPRESSO_CALIPER
773 CALI_MARK_END("ek_propagation");
774#endif
775 }
776 } else if (lb_active) {
777 auto const md_steps_per_lb_step = calc_md_steps_per_tau(lb.get_tau());
778 propagation.lb_skipped_md_steps += 1;
779 if (propagation.lb_skipped_md_steps >= md_steps_per_lb_step) {
780 propagation.lb_skipped_md_steps = 0;
781#ifdef ESPRESSO_CALIPER
782 CALI_MARK_BEGIN("lb_propagation");
783#endif
784 lb.propagate();
785#ifdef ESPRESSO_CALIPER
786 CALI_MARK_END("lb_propagation");
787#endif
788 }
789 } else if (ek_active) {
790 auto const md_steps_per_ek_step = calc_md_steps_per_tau(ek.get_tau());
791 propagation.ek_skipped_md_steps += 1;
792 if (propagation.ek_skipped_md_steps >= md_steps_per_ek_step) {
793 propagation.ek_skipped_md_steps = 0;
794#ifdef ESPRESSO_CALIPER
795 CALI_MARK_BEGIN("ek_propagation");
796#endif
797 ek.propagate();
798#ifdef ESPRESSO_CALIPER
799 CALI_MARK_END("ek_propagation");
800#endif
801 }
802 }
803 if (lb_active and (propagation.used_propagations &
805 thermostat->lb->rng_increment();
806 }
807
808#ifdef ESPRESSO_VIRTUAL_SITES_INERTIALESS_TRACERS
809 if (thermostat->lb and
810 (propagation.used_propagations & PropagationMode::TRANS_LB_TRACER)) {
811#ifdef ESPRESSO_CALIPER
812 CALI_MARK_BEGIN("lb_tracers_propagation");
813#endif
814 if (lb_active) {
815 lb.ghost_communication_vel();
816 }
817 lb_tracers_propagate(*cell_structure, lb, time_step);
818#ifdef ESPRESSO_CALIPER
819 CALI_MARK_END("lb_tracers_propagation");
820#endif
821 }
822#endif
823
824#ifdef ESPRESSO_COLLISION_DETECTION
825 cell_structure->clear_new_bonds();
826 collision_detection->handle_collisions();
827 cell_structure->rebuild_bond_list();
828#endif
829 bond_breakage->process_queue(*this);
830 }
831
833
835 caught_error = true;
836 break;
837 }
838
839 // Check if SIGINT has been caught.
840 if (singleton_mode and ctrl_C == 1) {
841 caught_sigint = true;
842 break;
843 }
844
845 } // for-loop over integration steps
846 if (lb_active) {
847 lb.ghost_communication();
848 }
849 lees_edwards->update_box_params(*box_geo, sim_time);
850#ifdef ESPRESSO_CALIPER
852#endif
853
854#ifdef ESPRESSO_VALGRIND
856#endif
857
858#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
859 if (has_vs_rel()) {
860 vs_relative_update_particles(*cell_structure, *box_geo);
861 }
862#endif
863#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
864 if (has_vs_com()) {
865 vs_com_update_particles(*cell_structure, *box_geo);
866 }
867#endif
868
869 // Verlet list statistics
870 cell_structure->update_verlet_stats(n_steps, n_verlet_updates);
871
872#ifdef ESPRESSO_NPT
873 if (has_npt_enabled()) {
874 synchronize_npt_state();
875 }
876#endif
877 if (caught_sigint) {
878 ctrl_C = 0;
879 return INTEG_ERROR_SIGINT;
880 }
881 if (caught_error) {
882 return INTEG_ERROR_RUNTIME;
883 }
884 if (boost::mpi::all_reduce(::comm_cart, not cell_structure->use_verlet_list,
885 std::logical_or<>())) {
886 cell_structure->use_verlet_list = false;
887 }
888 return integrated_steps;
889}
890
892 bool update_accumulators) {
893 assert(n_steps >= 0);
894
895 // Override the signal handler so that the integrator obeys Ctrl+C
896 SignalHandler sa(SIGINT, [](int) { ctrl_C = 1; });
897
898 /* if skin wasn't set, do an educated guess now */
899 if (not cell_structure->is_verlet_skin_set()) {
900 try {
901 cell_structure->set_verlet_skin_heuristic();
902 } catch (...) {
903 if (comm_cart.rank() == 0) {
904 throw;
905 }
906 return INTEG_ERROR_RUNTIME;
907 }
908 }
909
911 return integrate(n_steps, reuse_forces);
912 }
913
914 for (int i = 0; i < n_steps;) {
915 /* Integrate to either the next accumulator update, or the
916 * end, depending on what comes first. */
917 auto const steps =
918 std::min((n_steps - i), auto_update_accumulators->next_update());
919
920 auto const local_retval = integrate(steps, reuse_forces);
921
922 // make sure all ranks exit when one rank fails
923 std::remove_const_t<decltype(local_retval)> global_retval;
924 boost::mpi::all_reduce(comm_cart, local_retval, global_retval,
925 std::plus<int>());
926 if (global_retval < 0) {
927 return global_retval; // propagate error code
928 }
929
931
932 (*auto_update_accumulators)(comm_cart, steps);
933
934 i += steps;
935 }
936
937 return 0;
938}
939
941 sim_time = value;
942 propagation->recalc_forces = true;
943 lees_edwards->update_box_params(*box_geo, sim_time);
944}
@ LEES_EDWARDS
@ INTEG_METHOD_NPT_ISO_AND
@ INTEG_METHOD_SD
@ INTEG_METHOD_STEEPEST_DESCENT
@ INTEG_METHOD_NVT
@ INTEG_METHOD_SYMPLECTIC_EULER
@ INTEG_METHOD_BD
@ INTEG_METHOD_NPT_ISO_MTK
@ THERMO_SD
@ THERMO_BROWNIAN
@ THERMO_BOND
@ THERMO_LB
@ THERMO_LANGEVIN
@ THERMO_NPT_ISO
@ THERMO_OFF
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.
BoxType type() const
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
int default_propagation
void update_default_propagation(int thermo_switch)
bool should_propagate_with(Particle const &p, int mode) const
int used_propagations
RAII guard for signal handling.
Main system class.
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.
void vs_com_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition com.cpp:131
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
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
Definition integrate.hpp:42
#define INTEG_ERROR_SIGINT
Definition integrate.hpp:43
#define INTEG_REUSE_FORCES_NEVER
recalculate forces unconditionally (mostly used for timing)
Definition integrate.hpp:49
#define INTEG_REUSE_FORCES_ALWAYS
do not recalculate forces (mostly when reading checkpoints with forces)
Definition integrate.hpp:53
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)
Definition protocols.hpp:94
volatile std::sig_atomic_t ctrl_C
Definition integrate.cpp:96
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.
Definition rattle.cpp:261
void save_old_position(const ParticleRange &particles, const ParticleRange &ghost_particles)
copy current position
Definition rattle.cpp:65
void correct_position_shake(CellStructure &cs, BoxGeometry const &box_geo, BondedInteractionsMap &bonded_ias)
Propagate velocity and position while using SHAKE algorithm for bond constraint.
Definition rattle.cpp:179
RATTLE algorithm ().
void vs_relative_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition relative.cpp:121
void convert_initial_torques(const ParticleRange &particles)
Convert torques to the body-fixed frame before the integration loop.
Definition rotation.cpp:192
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.
Definition Particle.hpp:436
constexpr auto is_virtual() const
Definition Particle.hpp:606
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.