ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/system/System.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config/config.hpp"
21
22#include "System.hpp"
23#include "System.impl.hpp"
24
25#include "BoxGeometry.hpp"
26#include "LocalBox.hpp"
27#include "PropagationMode.hpp"
28#include "accumulators/AutoUpdateAccumulators.hpp"
34#include "collision_detection/CollisionDetection.hpp"
35#include "communication.hpp"
37#include "errorhandling.hpp"
39#include "npt.hpp"
40#include "particle_node.hpp"
42#include "thermostat.hpp"
43#include "virtual_sites/com.hpp"
45
46#include <utils/Vector.hpp>
48
49#include <boost/mpi/collectives/all_reduce.hpp>
50
51#include <algorithm>
52#include <cstddef>
53#include <functional>
54#include <memory>
55#include <stdexcept>
56#include <utility>
57
58namespace System {
59
60static std::shared_ptr<System> instance = System::create();
61
62std::shared_ptr<System> System::create() {
63 auto handle = std::make_shared<System>(Private());
64 handle->initialize();
65 return handle;
66}
67
69 box_geo = std::make_shared<BoxGeometry>();
70 local_geo = std::make_shared<LocalBox>();
71 cell_structure = std::make_shared<CellStructure>(*box_geo);
72#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
73 cell_structure->set_kokkos_handle(::kokkos_handle);
74#endif
75 propagation = std::make_shared<Propagation>();
76 bonded_ias = std::make_shared<BondedInteractionsMap>();
77 thermostat = std::make_shared<Thermostat::Thermostat>();
78 nonbonded_ias = std::make_shared<InteractionsNonBonded>();
79 comfixed = std::make_shared<ComFixed>();
80 galilei = std::make_shared<Galilei>();
81 oif_global = std::make_shared<OifGlobal>();
82 immersed_boundaries = std::make_shared<ImmersedBoundaries>();
83#ifdef ESPRESSO_COLLISION_DETECTION
84 collision_detection =
85 std::make_shared<CollisionDetection::CollisionDetection>();
86#endif
87 bond_breakage = std::make_shared<BondBreakage::BondBreakage>();
88 lees_edwards = std::make_shared<LeesEdwards::LeesEdwards>();
89 auto_update_accumulators =
90 std::make_shared<Accumulators::AutoUpdateAccumulators>();
91 constraints = std::make_shared<Constraints::Constraints>();
92#ifdef ESPRESSO_NPT
93 nptiso = std::make_shared<NptIsoParameters>();
94 npt_inst_pressure = std::make_shared<InstantaneousPressure>();
95#endif
96 reinit_thermo = true;
97 time_step = -1.;
98 sim_time = 0.;
99 force_cap = 0.;
100 min_global_cut = inactive_cutoff;
101}
102
103void System::initialize() {
104 auto handle = shared_from_this();
105 cell_structure->bind_system(handle);
106 lees_edwards->bind_system(handle);
107 immersed_boundaries->bind_system(handle);
108 bonded_ias->bind_system(handle);
109 thermostat->bind_system(handle);
110 nonbonded_ias->bind_system(handle);
111 oif_global->bind_system(handle);
112 immersed_boundaries->bind_system(handle);
113#ifdef ESPRESSO_COLLISION_DETECTION
114 collision_detection->bind_system(handle);
115#endif
116 auto_update_accumulators->bind_system(handle);
117 constraints->bind_system(handle);
118#ifdef ESPRESSO_CUDA
119 gpu.bind_system(handle);
120 gpu.initialize();
121#endif
122 lb.bind_system(handle);
123 ek.bind_system(handle);
124}
125
126void reset_system() { instance.reset(); }
127
128void set_system(std::shared_ptr<System> new_instance) {
129 instance = new_instance;
130}
131
133
134void System::set_time_step(double value) {
135 if (value <= 0.)
136 throw std::domain_error("time_step must be > 0.");
137 if (lb.is_solver_set()) {
138 lb.veto_time_step(value);
139 }
140 if (ek.is_solver_set()) {
141 ek.veto_time_step(value);
142 }
143 time_step = value;
144 on_timestep_change();
145}
146
147void System::check_kT(double value) const {
148 if (lb.is_solver_set()) {
149 lb.veto_kT(value);
150 }
151 if (ek.is_solver_set()) {
152 ek.veto_kT(value);
153 }
154}
155
156void System::set_force_cap(double value) {
157 force_cap = value;
158 propagation->recalc_forces = true;
159}
160
161void System::set_min_global_cut(double value) {
162 min_global_cut = value;
163 on_verlet_skin_change();
164}
165
167 if (topology == CellStructureType::REGULAR) {
168 if (cell_structure->decomposition_type() == CellStructureType::REGULAR) {
169 // get fully connected info from existing regular decomposition
170 auto &old_regular_decomposition =
171 dynamic_cast<RegularDecomposition const &>(
172 std::as_const(*cell_structure).decomposition());
173 cell_structure->set_regular_decomposition(
174 get_interaction_range(),
175 old_regular_decomposition.fully_connected_boundary());
176 } else { // prev. decomposition is not a regular decomposition
177 cell_structure->set_regular_decomposition(get_interaction_range(), {});
178 }
179 } else if (topology == CellStructureType::NSQUARE) {
180 cell_structure->set_atom_decomposition();
181 } else {
182 assert(topology == CellStructureType::HYBRID);
183 /* Get current HybridDecomposition to extract n_square_types */
184 auto &old_hybrid_decomposition = dynamic_cast<HybridDecomposition const &>(
185 std::as_const(*cell_structure).decomposition());
186 cell_structure->set_hybrid_decomposition(
187 old_hybrid_decomposition.get_cutoff_regular(),
188 old_hybrid_decomposition.get_n_square_types());
189 }
190}
191
193 set_cell_structure_topology(cell_structure->decomposition_type());
194}
195
196void System::on_boxl_change(bool skip_method_adaption) {
197 update_local_geo();
198 rebuild_cell_structure();
199
200 /* Now give methods a chance to react to the change in box length */
201 if (not skip_method_adaption) {
202 lb.on_boxl_change();
203 ek.on_boxl_change();
204#ifdef ESPRESSO_ELECTROSTATICS
205 coulomb.on_boxl_change();
206#endif
207#ifdef ESPRESSO_DIPOLES
208 dipoles.on_boxl_change();
209#endif
210 }
211 constraints->on_boxl_change();
212}
213
214void System::veto_boxl_change(bool skip_particle_checks) const {
215 if (not skip_particle_checks) {
216 auto const n_part = boost::mpi::all_reduce(
217 ::comm_cart, cell_structure->local_particles().size(), std::plus<>());
218 if (n_part > 0ul) {
219 throw std::runtime_error(
220 "Cannot reset the box length when particles are present");
221 }
222 }
223 constraints->veto_boxl_change();
224 lb.veto_boxl_change();
225 ek.veto_boxl_change();
226}
227
229 update_local_geo();
230 lb.on_node_grid_change();
231 ek.on_node_grid_change();
232#ifdef ESPRESSO_ELECTROSTATICS
233 coulomb.on_node_grid_change();
234#endif
235#ifdef ESPRESSO_DIPOLES
236 dipoles.on_node_grid_change();
237#endif
238 rebuild_cell_structure();
239}
240
242#ifdef ESPRESSO_ELECTROSTATICS
243 coulomb.on_periodicity_change();
244#endif
245
246#ifdef ESPRESSO_DIPOLES
247 dipoles.on_periodicity_change();
248#endif
249
250#ifdef ESPRESSO_STOKESIAN_DYNAMICS
251 if (propagation->integ_switch == INTEG_METHOD_SD) {
252 if (box_geo->periodic(0u) or box_geo->periodic(1u) or box_geo->periodic(2u))
253 runtimeErrorMsg() << "Stokesian Dynamics requires periodicity "
254 << "(False, False, False)\n";
255 }
256#endif
257 on_verlet_skin_change();
258}
259
262 lb.on_cell_structure_change();
263 ek.on_cell_structure_change();
264#ifdef ESPRESSO_ELECTROSTATICS
265 coulomb.on_cell_structure_change();
266#endif
267#ifdef ESPRESSO_DIPOLES
268 dipoles.on_cell_structure_change();
269#endif
270}
271
272void System::on_thermostat_param_change() { reinit_thermo = true; }
273
275 rebuild_cell_structure();
276#ifdef ESPRESSO_ELECTROSTATICS
277 coulomb.on_coulomb_change();
278#endif
279#ifdef ESPRESSO_DIPOLES
280 dipoles.on_dipoles_change();
281#endif
282 on_short_range_ia_change();
283}
284
286 lb.on_temperature_change();
287 ek.on_temperature_change();
288}
289
291 lb.on_timestep_change();
292 ek.on_timestep_change();
293 on_thermostat_param_change();
294}
295
297 rebuild_cell_structure();
298 propagation->recalc_forces = true;
299}
300
302 nonbonded_ias->recalc_maximal_cutoffs();
303 rebuild_cell_structure();
304 on_thermostat_param_change();
305 propagation->recalc_forces = true;
306}
307
309#ifdef ESPRESSO_ELECTROSTATICS
310 coulomb.on_coulomb_change();
311#endif
312 on_short_range_ia_change();
313}
314
316#ifdef ESPRESSO_DIPOLES
317 dipoles.on_dipoles_change();
318#endif
319 on_short_range_ia_change();
320}
321
322void System::on_constraint_change() { propagation->recalc_forces = true; }
323
325 propagation->recalc_forces = true;
326}
327
329 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
330 propagation->recalc_forces = true;
331}
332
334 if (cell_structure->decomposition_type() == CellStructureType::HYBRID) {
335 cell_structure->set_resort_particles(Cells::RESORT_GLOBAL);
336 } else {
337 cell_structure->set_resort_particles(Cells::RESORT_LOCAL);
338 }
339#ifdef ESPRESSO_ELECTROSTATICS
340 coulomb.on_particle_change();
341#endif
342#ifdef ESPRESSO_DIPOLES
343 dipoles.on_particle_change();
344#endif
345 propagation->recalc_forces = true;
346
347 /* the particle information is no longer valid */
349#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
350 cell_structure->clear_local_properties();
351#endif
352}
353
355#ifdef ESPRESSO_ELECTROSTATICS
356 coulomb.on_particle_change();
357#endif
358}
359
361#ifdef ESPRESSO_VIRTUAL_SITES
362#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
363 vs_relative_update_particles(*cell_structure, *box_geo);
364#endif
365#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
366 vs_com_update_particles(*cell_structure, *box_geo);
367#endif
368 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
369#endif
370
371#ifdef ESPRESSO_ELECTROSTATICS
372 if (has_icc_enabled()) {
373#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
374 rebuild_aosoa();
375#endif
376 update_icc_particles();
377 }
378#endif
379
380 // Here we initialize volume conservation
381 // This function checks if the reference volumes have been set and if
382 // necessary calculates them
383 immersed_boundaries->init_volume_conservation(*cell_structure);
384}
385
387 /* Prepare particle structure: Communication step: number of ghosts and ghost
388 * information */
389 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
390 update_dependent_particles();
391
392#ifdef ESPRESSO_ELECTROSTATICS
393 coulomb.on_observable_calc();
394#endif
395
396#ifdef ESPRESSO_DIPOLES
397 dipoles.on_observable_calc();
398#endif
399
401#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
402 rebuild_aosoa();
403#endif
404}
405
406#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
408#ifdef ESPRESSO_COLLISION_DETECTION
409 auto const collision_detection_cutoff = collision_detection->cutoff();
410#else
411 auto const collision_detection_cutoff = inactive_cutoff;
412#endif
413
414 VerletCriterion<> const verlet_criterion{*this,
415 cell_structure->get_verlet_skin(),
416 get_interaction_range(),
417 coulomb.cutoff(),
418 dipoles.cutoff(),
419 collision_detection_cutoff};
420
421 update_cabana_state(*cell_structure, verlet_criterion,
422 get_interaction_range(), propagation->integ_switch);
423}
424#endif // ESPRESSO_SHARED_MEMORY_PARALLELISM
425
426void System::on_lees_edwards_change() { lb.on_lees_edwards_change(); }
427
433
435 auto max_cut = inactive_cutoff;
436 max_cut = std::max(max_cut, get_min_global_cut());
437 max_cut = std::max(max_cut, coulomb.cutoff());
438 max_cut = std::max(max_cut, dipoles.cutoff());
439 if (::communicator.size > 1) {
440 // If there is just one node, the bonded cutoff can be omitted
441 // because bond partners are always on the local node.
442 max_cut = std::max(max_cut, bonded_ias->maximal_cutoff());
443 }
444 max_cut = std::max(max_cut, nonbonded_ias->maximal_cutoff());
445
446#ifdef ESPRESSO_COLLISION_DETECTION
447 max_cut = std::max(max_cut, collision_detection->cutoff());
448#endif
449 return max_cut;
450}
451
453 try {
454#ifdef ESPRESSO_ELECTROSTATICS
455 coulomb.sanity_checks();
456#endif
457#ifdef ESPRESSO_DIPOLES
458 dipoles.sanity_checks();
459#endif
460 } catch (std::runtime_error const &err) {
461 runtimeErrorMsg() << err.what();
462 return true;
463 }
464 return false;
465}
466
468 auto const max_cut = maximal_cutoff();
469 auto const verlet_skin = cell_structure->get_verlet_skin();
470 /* Consider skin only if there are actually interactions */
471 return (max_cut > 0.) ? max_cut + verlet_skin : inactive_cutoff;
472}
473
475 box_geo->set_length(box_l);
476 on_boxl_change();
477}
478
480 // sanity checks
481 integrator_sanity_checks();
482 long_range_interactions_sanity_checks();
483 lb.sanity_checks();
484 ek.sanity_checks();
485
486#ifdef ESPRESSO_NPT
487 if (propagation->integ_switch == INTEG_METHOD_NPT_ISO_AND ||
488 propagation->integ_switch == INTEG_METHOD_NPT_ISO_MTK) {
489 npt_ensemble_init(propagation->recalc_forces);
490 }
491#endif
492
493 /* Prepare the thermostat */
494 if (reinit_thermo) {
495 thermostat->recalc_prefactors(time_step);
496 reinit_thermo = false;
497 propagation->recalc_forces = true;
498 }
499
501#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
502 cell_structure->clear_local_properties();
503#endif
504
505#ifdef ESPRESSO_ADDITIONAL_CHECKS
506 if (!Utils::Mpi::all_compare(::comm_cart, cell_structure->use_verlet_list)) {
507 runtimeErrorMsg() << "Nodes disagree about use of verlet lists.";
508 }
509#ifdef ESPRESSO_ELECTROSTATICS
510 {
511 auto const &actor = coulomb.impl->solver;
512 if (not Utils::Mpi::all_compare(::comm_cart, static_cast<bool>(actor)) or
513 (actor and not Utils::Mpi::all_compare(::comm_cart, (*actor).index())))
514 runtimeErrorMsg() << "Nodes disagree about Coulomb long-range method";
515 }
516#endif
517#ifdef ESPRESSO_DIPOLES
518 {
519 auto const &actor = dipoles.impl->solver;
520 if (not Utils::Mpi::all_compare(::comm_cart, static_cast<bool>(actor)) or
521 (actor and not Utils::Mpi::all_compare(::comm_cart, (*actor).index())))
522 runtimeErrorMsg() << "Nodes disagree about dipolar long-range method";
523 }
524#endif
525#endif /* ESPRESSO_ADDITIONAL_CHECKS */
526
527 on_observable_calc();
528}
529
530/**
531 * @brief Returns the ghost flags required for running pair
532 * kernels for the global state, e.g. the force calculation.
533 * @return Required data parts;
534 */
536 /* Position and Properties are always requested. */
538
539 if (lb.is_solver_set())
540 data_parts |= Cells::DATA_PART_MOMENTUM;
541
542 if (thermostat->thermo_switch & THERMO_DPD)
543 data_parts |= Cells::DATA_PART_MOMENTUM;
544
545 if (thermostat->thermo_switch & THERMO_BOND) {
546 data_parts |= Cells::DATA_PART_MOMENTUM;
547 data_parts |= Cells::DATA_PART_BONDS;
548 }
549
550#ifdef ESPRESSO_COLLISION_DETECTION
551 if (not collision_detection->is_off()) {
552 data_parts |= Cells::DATA_PART_BONDS;
553 }
554#endif
555
556 return data_parts;
557}
558
559#ifdef ESPRESSO_NPT
561 return (propagation->integ_switch == INTEG_METHOD_NPT_ISO_AND) or
562 (propagation->integ_switch == INTEG_METHOD_NPT_ISO_MTK);
563}
564#endif
565
567#ifdef ESPRESSO_NPT
568 if (has_npt_enabled()) {
569 return &npt_inst_pressure->p_vir;
570 }
571#endif
572 return nullptr;
573}
574
575#ifdef ESPRESSO_COLLISION_DETECTION
577 return not collision_detection->is_off();
578}
579#endif
580
581} // namespace System
CellStructureType
Cell structure topology.
@ NSQUARE
Atom decomposition (N-square).
@ HYBRID
Hybrid decomposition.
@ REGULAR
Regular decomposition.
@ INTEG_METHOD_NPT_ISO_AND
@ INTEG_METHOD_SD
@ INTEG_METHOD_NPT_ISO_MTK
@ THERMO_BOND
@ THERMO_DPD
Vector implementation and trait types for boost qvm interoperability.
Data structures for bonded interactions.
Hybrid decomposition cell system.
static LocalBox make_regular_decomposition(Utils::Vector3d const &box_l, Utils::Vector3i const &node_index, Utils::Vector3i const &node_grid)
Definition LocalBox.hpp:69
Main system class.
double get_interaction_range() const
Get the interaction range.
double maximal_cutoff() const
Calculate the maximal cutoff of all interactions.
void on_constraint_change()
Called every time a constraint is changed.
unsigned get_global_ghost_flags() const
Returns the ghost flags required for running pair kernels for the global state, e....
void on_boxl_change(bool skip_method_adaption=false)
Called when the box length has changed.
void set_box_l(Utils::Vector3d const &box_l)
Change the box dimensions.
void set_force_cap(double value)
Set force_cap.
void update_dependent_particles()
Update particles with properties depending on other particles, namely virtual sites and ICC charges.
void on_lb_boundary_conditions_change()
Called when the LB boundary conditions change (geometry, slip velocity, or both).
void veto_boxl_change(bool skip_particle_checks=false) const
void set_time_step(double value)
Set time_step.
void on_particle_change()
Called every time a particle property changes.
bool long_range_interactions_sanity_checks() const
Check electrostatic and magnetostatic methods are properly initialized.
void on_particle_charge_change()
Called every time a particle charge changes.
void on_particle_local_change()
Called every time a particle local property changes.
void on_observable_calc()
called before calculating observables, i.e.
void check_kT(double value) const
Veto temperature change.
static std::shared_ptr< System > create()
Utils::Vector3d * get_npt_virial() const
void set_min_global_cut(double value)
Set min_global_cut.
bool has_npt_enabled() const
void set_cell_structure_topology(CellStructureType topology)
Change cell structure topology.
void rebuild_cell_structure()
Rebuild cell lists.
bool has_collision_detection_enabled() const
Returns true if the particles are to be considered for short range interactions.
void vs_com_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition com.cpp:105
std::shared_ptr< KokkosHandle > kokkos_handle
Communicator communicator
boost::mpi::communicator comm_cart
The communicator.
constexpr double inactive_cutoff
Special cutoff value for an inactive interaction.
Definition config.hpp:44
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
ICC is a method that allows to take into account the influence of arbitrarily shaped dielectric inter...
@ DATA_PART_MOMENTUM
Particle::m.
@ DATA_PART_PROPERTIES
Particle::p.
@ DATA_PART_BONDS
Particle::bonds.
@ DATA_PART_POSITION
Particle::r.
System & get_system()
static std::shared_ptr< System > instance
void set_system(std::shared_ptr< System > new_instance)
bool all_compare(boost::mpi::communicator const &comm, T const &value)
Compare values on all nodes.
Exports for the NpT code.
void invalidate_fetch_cache()
Invalidate the fetch cache for get_particle_data.
void clear_particle_node()
Invalidate particle_node.
Particles creation and deletion.
void vs_relative_update_particles(CellStructure &cell_structure, BoxGeometry const &box_geo)
Definition relative.cpp:121
ESPRESSO_ATTR_ALWAYS_INLINE void update_cabana_state(CellStructure &cell_structure, auto const &verlet_criterion, double const pair_cutoff, auto const integ_switch)
Utils::Vector3i calc_node_index() const
Calculate the node index in the Cartesian topology.
Utils::Vector3i node_grid
Regular decomposition cell system.
Routines to thermalize the center of mass and distance of a particle pair.