ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ParticleHandle.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 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 "ParticleHandle.hpp"
23
29
30#include "core/BoxGeometry.hpp"
33#include "core/bonds.hpp"
35#include "core/exclusions.hpp"
38#include "core/propagation.hpp"
39#include "core/rotation.hpp"
43
44#include <utils/Vector.hpp>
46
47#include <boost/format.hpp>
48#include <boost/mpi/collectives/all_reduce.hpp>
49#include <boost/mpi/collectives/broadcast.hpp>
50#include <boost/mpi/communicator.hpp>
51
52#include <algorithm>
53#include <cassert>
54#include <cmath>
55#include <cstddef>
56#include <memory>
57#include <optional>
58#include <ranges>
59#include <set>
60#include <sstream>
61#include <stdexcept>
62#include <string>
63#include <string_view>
64#include <tuple>
65#include <type_traits>
66#include <vector>
67
68namespace ScriptInterface {
69namespace Particles {
70
71#ifdef ESPRESSO_ROTATION
72static auto constexpr contradicting_arguments_quat = std::to_array<
73 std::array<std::string_view, 3>>({
74 {"dip", "dipm",
75 "Setting 'dip' is sufficient as it defines the scalar dipole moment."},
76 {"quat", "director",
77 "Setting 'quat' is sufficient as it defines the director."},
78 {"dip", "quat",
79 "Setting 'dip' would overwrite 'quat'. Set 'quat' and 'dipm' instead."},
80 {"dip", "director",
81 "Setting 'dip' would overwrite 'director'. Set 'director' and "
82 "'dipm' instead."},
83});
84
85static void sanity_checks_rotation(VariantMap const &params) {
86 // if we are not constructing a particle from a checkpoint file,
87 // check the quaternion is not accidentally set twice by the user
88 if (not params.contains("__cpt_sentinel")) {
89 auto formatter =
90 boost::format("Contradicting particle attributes: '%s' and '%s'. %s");
91 for (auto const &[prop1, prop2, reason] : contradicting_arguments_quat) {
92 if (params.contains(std::string{prop1}) and
93 params.contains(std::string{prop2})) {
94 auto const err_msg = boost::str(formatter % prop1 % prop2 % reason);
95 throw std::invalid_argument(err_msg);
96 }
97 }
98 }
99}
100#endif // ESPRESSO_ROTATION
101
102#if defined(ESPRESSO_ROTATION) or defined(ESPRESSO_EXTERNAL_FORCES)
104 auto bitfield = static_cast<uint8_t>(0u);
105 if (flag[0])
106 bitfield |= static_cast<uint8_t>(1u);
107 if (flag[1])
108 bitfield |= static_cast<uint8_t>(2u);
109 if (flag[2])
110 bitfield |= static_cast<uint8_t>(4u);
111 return bitfield;
112}
113#endif
114
115#ifdef ESPRESSO_ROTATION
117 return Utils::Vector4d{{q[0], q[1], q[2], q[3]}};
118}
119
120static auto get_quaternion_safe(std::string const &name, Variant const &value) {
121 auto const q = get_value<Utils::Vector4d>(value);
122 if (q.norm2() == 0.) {
123 throw std::domain_error(error_msg(name, "must be non-zero"));
124 }
125 return Utils::Quaternion<double>{{q[0], q[1], q[2], q[3]}};
126}
127#endif // ESPRESSO_ROTATION
128
129#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
130static auto get_gamma_safe(Variant const &value) {
131#ifdef ESPRESSO_PARTICLE_ANISOTROPY
132 try {
134 } catch (...) {
135 return get_value<Utils::Vector3d>(value);
136 }
137#else // ESPRESSO_PARTICLE_ANISOTROPY
138 return get_value<double>(value);
139#endif // ESPRESSO_PARTICLE_ANISOTROPY
140}
141#endif // ESPRESSO_THERMOSTAT_PER_PARTICLE
142
143template <typename T, class F>
144T ParticleHandle::get_particle_property(F const &fun) const {
145 auto &cell_structure = get_cell_structure()->get_cell_structure();
146 auto const &comm = context()->get_comm();
147 auto const ptr = const_cast<Particle const *>(
148 get_real_particle(comm, m_pid, cell_structure));
149 std::optional<T> ret;
150 if (ptr == nullptr) {
151 ret = {};
152 } else {
153 ret = {fun(*ptr)};
154 }
155 return Utils::Mpi::reduce_optional(comm, ret);
156}
157
158template <typename T>
159T ParticleHandle::get_particle_property(T const &(Particle::*getter)()
160 const) const {
161 return get_particle_property<T>(
162 [getter](Particle const &p) { return (p.*getter)(); });
163}
164
165template <class F>
166void ParticleHandle::set_particle_property(F const &fun) const {
167 auto &cell_structure = get_cell_structure()->get_cell_structure();
168 auto const &comm = context()->get_comm();
169 auto const ptr = get_real_particle(comm, m_pid, cell_structure);
170 if (ptr != nullptr) {
171 fun(*ptr);
172 }
174}
175
176template <typename T>
177void ParticleHandle::set_particle_property(T &(Particle::*setter)(),
178 Variant const &value) const {
179 set_particle_property(
180 [&value, setter](Particle &p) { (p.*setter)() = get_value<T>(value); });
181}
182
183#ifdef ESPRESSO_EXCLUSIONS
184void ParticleHandle::set_exclusions(Variant const &value) {
185 std::vector<int> exclusion_list;
186 if (is_type<int>(value)) {
187 exclusion_list.emplace_back(get_value<int>(value));
188 } else {
190 }
191 auto &cell_structure = get_cell_structure()->get_cell_structure();
192 context()->parallel_try_catch([&]() {
193 for (auto const pid : exclusion_list) {
194 particle_exclusion_sanity_checks(m_pid, pid, cell_structure,
195 context()->get_comm());
196 }
197 });
198 set_particle_property([&](Particle const &p) {
199 for (auto const pid : p.exclusions()) {
200 local_remove_exclusion(m_pid, pid, cell_structure);
201 }
202 for (auto const pid : exclusion_list) {
203 if (!p.has_exclusion(pid)) {
204 local_add_exclusion(m_pid, pid, cell_structure);
205 }
206 }
207 });
208}
209#endif // ESPRESSO_EXCLUSIONS
210
211ParticleHandle::ParticleHandle() {
212 /* Warning: the order of particle property setters matters! Some properties
213 * override each other, e.g. quat/director/dip or dip/dipm.
214 * This is relevant during checkpointing: all particle properties are set at
215 * once, in the order specified in the following call to `add_parameters()`.
216 */
217 add_parameters({
218 {"id", AutoParameter::read_only, [this]() { return m_pid; }},
219 {"type",
220 [this](Variant const &value) {
221 auto const old_type = get_particle_property(&Particle::type);
222 auto const new_type = get_value<int>(value);
223 if (new_type < 0) {
224 throw std::domain_error(
225 error_msg("type", "must be an integer >= 0"));
226 }
227 get_system()->nonbonded_ias->make_particle_type_exist(new_type);
229 set_particle_property(&Particle::type, value);
230 },
231 [this]() { return get_particle_data(m_pid).type(); }},
232 {"pos",
233 [this](Variant const &value) {
234 auto const pos = get_value<Utils::Vector3d>(value);
235 particle_checks(m_pid, pos);
236 set_particle_pos(m_pid, pos);
237 },
238 [this]() {
239 auto const p = get_particle_data(m_pid);
240 auto const pos = p.pos();
241 auto const image_box = p.image_box();
242 return get_system()->box_geo->unfolded_position(pos, image_box);
243 }},
244 {"v",
245 [this](Variant const &value) {
246 set_particle_property(&Particle::v, value);
247 },
248 [this]() { return get_particle_data(m_pid).v(); }},
249 {"f",
250 [this](Variant const &value) {
251 set_particle_property(&Particle::force, value);
252 },
253 [this]() { return get_particle_data(m_pid).force(); }},
254 {"mass",
255#ifdef ESPRESSO_MASS
256 [this](Variant const &value) {
257 if (get_value<double>(value) <= 0.) {
258 throw std::domain_error(error_msg("mass", "must be a float > 0"));
259 }
260 set_particle_property(&Particle::mass, value);
261 },
262#else // ESPRESSO_MASS
263 [](Variant const &value) {
264 auto const default_mass = Particle().mass();
265 if (std::abs(get_value<double>(value) - default_mass) > 1e-10) {
266 throw std::runtime_error("Feature MASS not compiled in");
267 }
268 },
269#endif // ESPRESSO_MASS
270 [this]() { return get_particle_data(m_pid).mass(); }},
271 {"q",
272#ifdef ESPRESSO_ELECTROSTATICS
273 [this](Variant const &value) {
274 set_particle_property(&Particle::q, value);
275 },
276#else // ESPRESSO_ELECTROSTATICS
277 [](Variant const &value) {
278 if (get_value<double>(value) != 0.) {
279 throw std::runtime_error("Feature ELECTROSTATICS not compiled in");
280 }
281 },
282#endif // ESPRESSO_ELECTROSTATICS
283 [this]() { return get_particle_data(m_pid).q(); }},
284#ifdef ESPRESSO_DIPOLES
285 {"dip",
286 [this](Variant const &value) {
287 set_particle_property([&value](Particle &p) {
288 auto const dip = get_value<Utils::Vector3d>(value);
289 std::tie(p.quat(), p.dipm()) = convert_dip_to_quat(dip);
290 });
291 },
292 [this]() { return get_particle_data(m_pid).calc_dip(); }},
293 {"dipm",
294 [this](Variant const &value) {
295 set_particle_property(&Particle::dipm, value);
296 },
297 [this]() { return get_particle_data(m_pid).dipm(); }},
298#endif // ESPRESSO_DIPOLES
299#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
300 {"dip_fld",
301 [this](Variant const &value) {
302 set_particle_property(&Particle::dip_fld, value);
303 },
304 [this]() { return get_particle_data(m_pid).dip_fld(); }},
305#endif
306#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
307 {"magnetodynamics",
308 [this](Variant const &value) {
309 set_particle_property([&value](Particle &p) {
310 auto const dict = get_value<VariantMap>(value);
311 if (dict.contains("is_enabled"))
313 get_value<bool>(dict.at("is_enabled"));
314 if (dict.contains("sw_phi_0"))
316 get_value<double>(dict.at("sw_phi_0"));
317 if (dict.contains("sat_mag"))
319 get_value<double>(dict.at("sat_mag"));
320 if (dict.contains("anisotropy_field_inv"))
322 get_value<double>(dict.at("anisotropy_field_inv"));
323 if (dict.contains("anisotropy_energy"))
325 get_value<double>(dict.at("anisotropy_energy"));
326 if (dict.contains("sw_tau0_inv"))
328 get_value<double>(dict.at("sw_tau0_inv"));
329 if (dict.contains("sw_dt_incr"))
331 get_value<double>(dict.at("sw_dt_incr"));
332 });
333 },
334 [this]() {
335 auto const &p = get_particle_data(m_pid);
336 return VariantMap{
337 {"is_enabled", p.stoner_wohlfarth_is_enabled()},
338 {"sw_phi_0", p.stoner_wohlfarth_phi_0()},
339 {"sat_mag", p.saturation_magnetization()},
340 {"anisotropy_field_inv", p.magnetic_anisotropy_field_inv()},
341 {"anisotropy_energy", p.magnetic_anisotropy_energy()},
342 {"sw_tau0_inv", p.stoner_wohlfarth_tau0_inv()},
343 {"sw_dt_incr", p.stoner_wohlfarth_dt_incr()},
344 };
345 }},
346#endif // ESPRESSO_THERMAL_STONER_WOHLFARTH
347#ifdef ESPRESSO_ROTATION
348 {"director",
349 [this](Variant const &value) {
350 set_particle_property([&value](Particle &p) {
351 auto const director = get_value<Utils::Vector3d>(value).normalized();
353 });
354 },
355 [this]() {
356 auto const quat = get_particle_data(m_pid).quat();
358 }},
359 {"quat",
360 [this](Variant const &value) {
361 auto const quat = get_quaternion_safe("quat", value);
362 set_particle_property([&quat](Particle &p) { p.quat() = quat; });
363 },
364 [this]() { return quat2vector(get_particle_data(m_pid).quat()); }},
365 {"omega_body",
366 [this](Variant const &value) {
367 set_particle_property(&Particle::omega, value);
368 },
369 [this]() { return get_particle_data(m_pid).omega(); }},
370 {"rotation",
371 [this](Variant const &value) {
372 set_particle_property([&value](Particle &p) {
373 auto const rotation_flag =
376 });
377 },
378 [this]() {
379 auto const rotation_bits = get_particle_data(m_pid).rotation();
380 return Utils::Vector3b{{::detail::get_nth_bit(rotation_bits, 0),
381 ::detail::get_nth_bit(rotation_bits, 1),
382 ::detail::get_nth_bit(rotation_bits, 2)}};
383 }},
384 {"omega_lab",
385 [this](Variant const &value) {
386 set_particle_property([&value](Particle &p) {
387 auto const omega = get_value<Utils::Vector3d>(value);
388 p.omega() = convert_vector_space_to_body(p, omega);
389 });
390 },
391 [this]() {
392 auto &p = get_particle_data(m_pid);
393 return convert_vector_body_to_space(p, p.omega());
394 }},
395 {"torque_lab",
396 [this](Variant const &value) {
397 set_particle_property([&value](Particle &p) {
398 auto const torque = get_value<Utils::Vector3d>(value);
399 p.torque() = convert_vector_space_to_body(p, torque);
400 });
401 },
402 [this]() {
403 auto &p = get_particle_data(m_pid);
405 }},
406#endif // ESPRESSO_ROTATION
407#ifdef ESPRESSO_ROTATIONAL_INERTIA
408 {"rinertia",
409 [this](Variant const &value) {
410 set_particle_property(&Particle::rinertia, value);
411 },
412 [this]() { return get_particle_data(m_pid).rinertia(); }},
413#endif // ESPRESSO_ROTATIONAL_INERTIA
414#ifdef ESPRESSO_LB_ELECTROHYDRODYNAMICS
415 {"mu_E",
416 [this](Variant const &value) {
417 set_particle_property(&Particle::mu_E, value);
418 },
419 [this]() { return get_particle_data(m_pid).mu_E(); }},
420#endif // ESPRESSO_LB_ELECTROHYDRODYNAMICS
421#ifdef ESPRESSO_EXTERNAL_FORCES
422 {"fix",
423 [this](Variant const &value) {
424 set_particle_property([&value](Particle &p) {
425 auto const fix_flag =
428 });
429 },
430 [this]() {
431 auto const fixed = get_particle_data(m_pid).fixed();
432 return Utils::Vector3b{{::detail::get_nth_bit(fixed, 0),
433 ::detail::get_nth_bit(fixed, 1),
434 ::detail::get_nth_bit(fixed, 2)}};
435 }},
436 {"ext_force",
437 [this](Variant const &value) {
438 set_particle_property(&Particle::ext_force, value);
439 },
440 [this]() { return get_particle_data(m_pid).ext_force(); }},
441#ifdef ESPRESSO_ROTATION
442 {"ext_torque",
443 [this](Variant const &value) {
444 set_particle_property(&Particle::ext_torque, value);
445 },
446 [this]() { return get_particle_data(m_pid).ext_torque(); }},
447#endif // ESPRESSO_ROTATION
448#endif // ESPRESSO_EXTERNAL_FORCES
449#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
450 {"gamma",
451 [this](Variant const &value) {
452 set_particle_property(&Particle::gamma,
453 Variant{get_gamma_safe(value)});
454 },
455 [this]() { return get_particle_data(m_pid).gamma(); }},
456#ifdef ESPRESSO_ROTATION
457 {"gamma_rot",
458 [this](Variant const &value) {
459 set_particle_property(&Particle::gamma_rot,
460 Variant{get_gamma_safe(value)});
461 },
462 [this]() { return get_particle_data(m_pid).gamma_rot(); }},
463#endif // ESPRESSO_ROTATION
464#endif // ESPRESSO_THERMOSTAT_PER_PARTICLE
465 {"pos_folded", AutoParameter::read_only,
466 [this]() {
467 auto const &box_geo = *get_system()->box_geo;
468 return box_geo.folded_position(get_particle_data(m_pid).pos());
469 }},
470 {"lees_edwards_offset",
471 [this](Variant const &value) {
472 set_particle_property(&Particle::lees_edwards_offset, value);
473 },
474 [this]() { return get_particle_data(m_pid).lees_edwards_offset(); }},
475 {"lees_edwards_flag", AutoParameter::read_only,
476 [this]() { return get_particle_data(m_pid).lees_edwards_flag(); }},
477 {"image_box", AutoParameter::read_only,
478 [this]() {
479 auto const &box_geo = *get_system()->box_geo;
480 auto const p = get_particle_data(m_pid);
481 return box_geo.folded_image_box(p.pos(), p.image_box());
482 }},
483 {"node", AutoParameter::read_only,
484 [this]() {
485 return (context()->is_head_node()) ? get_particle_node(m_pid) : -1;
486 }},
487 {"mol_id",
488 [this](Variant const &value) {
489 auto const mol_id = get_value<int>(value);
490 if (mol_id < 0) {
491 throw std::domain_error(
492 error_msg("mol_id", "must be an integer >= 0"));
493 }
494 set_particle_property(&Particle::mol_id, Variant{mol_id});
495 },
496 [this]() { return get_particle_data(m_pid).mol_id(); }},
497#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
498 {"vs_quat",
499 [this](Variant const &value) {
500 auto const quat = get_quaternion_safe("vs_quat", value);
501 set_particle_property(
502 [&quat](Particle &p) { p.vs_relative().quat = quat; });
503 },
504 [this]() {
505 return quat2vector(get_particle_data(m_pid).vs_relative().quat);
506 }},
507 {"vs_relative",
508 [this](Variant const &value) {
510 try {
511 auto const array = get_value<std::vector<Variant>>(value);
512 if (array.size() != 3) {
513 throw 0;
514 }
515 vs_relative.distance = get_value<double>(array[1]);
516 vs_relative.to_particle_id = get_value<int>(array[0]);
517 vs_relative.rel_orientation =
518 get_quaternion_safe("vs_relative", array[2]);
519 } catch (...) {
520 throw std::invalid_argument(error_msg(
521 "vs_relative", "must take the form [id, distance, quaternion]"));
522 }
523 set_particle_property(
524 [&vs_relative](Particle &p) { p.vs_relative() = vs_relative; });
525 },
526 [this]() {
527 auto const vs_rel = get_particle_data(m_pid).vs_relative();
528 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
529 quat2vector(vs_rel.rel_orientation)}};
530 }},
531#endif // ESPRESSO_VIRTUAL_SITES_RELATIVE
532 {"propagation",
533 [this](Variant const &value) {
534 auto const propagation = get_value<int>(value);
535 if (!is_valid_propagation_combination(propagation)) {
536 throw std::domain_error(error_msg(
537 "propagation", "propagation combination not accepted: " +
538 propagation_bitmask_to_string(propagation)));
539 }
540 set_particle_property(&Particle::propagation, value);
541 },
542 [this]() { return get_particle_data(m_pid).propagation(); }},
543#ifdef ESPRESSO_ENGINE
544 {"swimming",
545 [this](Variant const &value) {
546 set_particle_property([&value](Particle &p) {
548 swim.swimming = true;
549 auto const dict = get_value<VariantMap>(value);
550 if (dict.contains("f_swim")) {
551 swim.f_swim = get_value<double>(dict.at("f_swim"));
552 }
553 if (dict.contains("is_engine_force_on_fluid")) {
554 auto const is_engine_force_on_fluid =
555 get_value<bool>(dict.at("is_engine_force_on_fluid"));
556 swim.is_engine_force_on_fluid = is_engine_force_on_fluid;
557 }
558 p.swimming() = swim;
559 });
560 },
561 [this]() {
562 auto const swim = get_particle_data(m_pid).swimming();
563 return VariantMap{
564 {"f_swim", swim.f_swim},
565 {"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
566 };
567 }},
568#endif // ESPRESSO_ENGINE
569 });
570}
571
572Variant ParticleHandle::do_call_method(std::string const &name,
573 VariantMap const &params) {
574 if (name == "set_param_parallel") {
575 auto const param_name = get_value<std::string>(params, "name");
576 if (not params.contains("value")) {
577 throw Exception("Parameter '" + param_name + "' is missing.");
578 }
579 auto const &value = params.at("value");
580 context()->parallel_try_catch(
581 [&]() { do_set_parameter(param_name, value); });
582 return {};
583 }
584 if (name == "update_params") {
585 // Set new properties
586 context()->parallel_try_catch([&]() {
587#ifdef ESPRESSO_ROTATION
589#endif
590 for (auto const &name : get_parameter_insertion_order()) {
591 if (params.contains(name) and name != "bonds") {
592 do_set_parameter(name, params.at(name));
593 }
594 }
595 });
596
597 // Set bonds
598 if (params.contains("bonds_ids")) {
599 // Remove old bonds
600 set_particle_property([&](Particle &p) { p.bonds().clear(); });
601 // Add new bonds
602 auto const bonds_ids = get_value<std::vector<int>>(params, "bonds_ids");
603 auto const bonds_partner_ids =
604 get_value<std::vector<std::vector<int>>>(params, "bonds_parts");
605 for (std::size_t i = 0; i < bonds_ids.size(); i += 1) {
606 std::vector<int> particle_ids = {m_pid};
607 std::ranges::copy(bonds_partner_ids[i],
608 std::back_inserter(particle_ids));
609 ::add_bond(*get_system(), bonds_ids[i], particle_ids);
610 get_system()->on_particle_change();
611 }
612 }
613#ifdef ESPRESSO_EXCLUSIONS
614 // set exclusions
615 if (params.contains("exclusions")) {
616 set_exclusions(params.at("exclusions"));
617 }
618#endif // ESPRESSO_EXCLUSIONS
619 return {};
620 }
621 if (name == "get_bond_by_id") {
622 if (not context()->is_head_node()) {
623 return {};
624 }
625 return get_bonded_ias()->call_method("get_bond", params);
626 }
627 if (name == "get_bonds_view") {
628 if (not context()->is_head_node()) {
629 return {};
630 }
631 auto const bond_list = get_particle_data(m_pid).bonds();
632 std::vector<std::vector<Variant>> bonds_flat;
633 for (auto const &&bond_view : bond_list) {
634 std::vector<Variant> bond_flat;
635 bond_flat.emplace_back(bond_view.bond_id());
636 for (auto const pid : bond_view.partner_ids()) {
637 bond_flat.emplace_back(pid);
638 }
639 bonds_flat.emplace_back(std::move(bond_flat));
640 }
642 }
643 if (name == "add_bond") {
644 auto const bond_id = get_value<int>(params, "bond_id");
645 auto const partner_ids = get_value<std::vector<int>>(params, "part_id");
646 std::vector<int> particle_ids = {m_pid};
647 std::ranges::copy(partner_ids, std::back_inserter(particle_ids));
648 ::add_bond(*get_system(), bond_id, particle_ids);
649 get_system()->on_particle_change();
650 } else if (name == "del_bond") {
651 set_particle_property([&params](Particle &p) {
652 auto const bond_id = get_value<int>(params, "bond_id");
653 auto const part_id = get_value<std::vector<int>>(params, "part_id");
654 auto const bond_view =
655 BondView(bond_id, {part_id.data(), part_id.size()});
656 auto &bond_list = p.bonds();
657 auto it = std::find(bond_list.begin(), bond_list.end(), bond_view);
658 if (it != bond_list.end()) {
659 bond_list.erase(it);
660 }
661 });
662 } else if (name == "delete_all_bonds") {
663 set_particle_property([&](Particle &p) { p.bonds().clear(); });
664 } else if (name == "is_valid_bond_id") {
665 auto const bond_id = get_value<int>(params, "bond_id");
666 return get_system()->bonded_ias->get_zero_based_type(bond_id) != 0;
667 }
668 if (name == "remove_particle") {
669 context()->parallel_try_catch([&]() {
670 auto &cell_structure = get_cell_structure()->get_cell_structure();
671 std::ignore =
672 get_real_particle(context()->get_comm(), m_pid, cell_structure);
673 remove_particle(m_pid);
674 });
675 } else if (name == "is_virtual") {
676 if (not context()->is_head_node()) {
677 return {};
678 }
679 return get_particle_data(m_pid).is_virtual();
680#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
681 } else if (name == "vs_auto_relate_to") {
682 if (not context()->is_head_node()) {
683 return {};
684 }
685 auto const other_pid = get_value<int>(params, "pid");
686 auto const override_cutoff_check =
687 get_value<bool>(params, "override_cutoff_check");
688 if (m_pid == other_pid) {
689 throw std::invalid_argument("A virtual site cannot relate to itself");
690 }
691 if (other_pid < 0) {
692 throw std::domain_error("Invalid particle id: " +
693 std::to_string(other_pid));
694 }
695 auto const system = get_system();
696 /* note: this code can be rewritten as parallel code, but only with a call
697 * to `cells_update_ghosts(DATA_PART_POSITION | DATA_PART_PROPERTIES)`, as
698 * there is no guarantee the virtual site has visibility of the relative
699 * particle through the ghost layer during particle creation. However,
700 * ghost updates can scramble the particle ordering in the local cells,
701 * which is an issue for checkpointing: the H5MD writer will use the
702 * scrambled ordering before writing to a checkpoint file and the
703 * non-scrambled ordering after reloading from a checkpoint file.
704 */
705 auto const &p_current = get_particle_data(m_pid);
707 auto const [quat, dist] = calculate_vs_relate_to_params(
708 p_current, p_relate_to, *system->box_geo, system->get_min_global_cut(),
710 set_parameter("vs_relative", Variant{std::vector<Variant>{
711 {other_pid, dist, quat2vector(quat)}}});
712 set_parameter("propagation",
715#endif // ESPRESSO_VIRTUAL_SITES_RELATIVE
716#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
717 } else if (name == "vs_com_relate_to") {
718 auto &cell_structure = get_cell_structure()->get_cell_structure();
719 auto const molid = get_value<int>(params, "molid");
720 auto const maybe_exists_vs = get_pid_for_vs_com(cell_structure, molid);
721 if (not context()->is_head_node()) {
722 return {};
723 }
724 if (molid < 0) {
725 throw std::domain_error("Invalid molecule id: " + std::to_string(molid));
726 }
727 if (maybe_exists_vs) {
728 throw std::runtime_error(
729 "Molecule id: " + std::to_string(molid) +
730 " is already tracked by virtual site with particle id: " +
731 std::to_string(*maybe_exists_vs));
732 }
733 set_parameter("mol_id", params.at("molid"));
734 set_parameter(
735 "propagation",
737#endif // ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
738#ifdef ESPRESSO_EXCLUSIONS
739 } else if (name == "has_exclusion") {
740 auto const other_pid = get_value<int>(params, "pid");
741 auto &cell_structure = get_cell_structure()->get_cell_structure();
742 auto const p =
743 get_real_particle(context()->get_comm(), m_pid, cell_structure);
744 if (p != nullptr) {
745 return p->has_exclusion(other_pid);
746 }
747 }
748 if (name == "add_exclusion") {
749 auto const other_pid = get_value<int>(params, "pid");
750 auto &cell_structure = get_cell_structure()->get_cell_structure();
751 context()->parallel_try_catch([&]() {
752 particle_exclusion_sanity_checks(m_pid, other_pid, cell_structure,
753 context()->get_comm());
754 });
755 local_add_exclusion(m_pid, other_pid, cell_structure);
756 get_system()->on_particle_change();
757 } else if (name == "del_exclusion") {
758 auto const other_pid = get_value<int>(params, "pid");
759 auto &cell_structure = get_cell_structure()->get_cell_structure();
760 context()->parallel_try_catch([&]() {
761 particle_exclusion_sanity_checks(m_pid, other_pid, cell_structure,
762 context()->get_comm());
763 });
764 local_remove_exclusion(m_pid, other_pid, cell_structure);
765 get_system()->on_particle_change();
766#ifdef ESPRESSO_EXCLUSIONS
767 } else if (name == "set_exclusions") {
768 set_exclusions(params.at("p_ids"));
769#endif // ESPRESSO_EXCLUSIONS
770 } else if (name == "get_exclusions") {
771 if (not context()->is_head_node()) {
772 return {};
773 }
774 auto const excl_list = get_particle_data(m_pid).exclusions();
775 return Variant{std::vector<int>{excl_list.begin(), excl_list.end()}};
776#endif // ESPRESSO_EXCLUSIONS
777#ifdef ESPRESSO_ROTATION
778 }
779 if (name == "rotate_particle") {
780 set_particle_property([&params](Particle &p) {
781 auto const axis = get_value<Utils::Vector3d>(params, "axis");
782 auto const angle = get_value<double>(params, "angle");
784 });
785 }
786 if (name == "convert_vector_body_to_space") {
788 [&params](Particle const &p) {
789 auto const vec = get_value<Utils::Vector3d>(params, "vec");
791 });
792 }
793 if (name == "convert_vector_space_to_body") {
795 [&params](Particle const &p) {
796 auto const vec = get_value<Utils::Vector3d>(params, "vec");
798 });
799#endif // ESPRESSO_ROTATION
800 }
801 return {};
802}
803
804std::size_t ParticleHandle::setup_hidden_args(VariantMap const &params) {
805 auto n_extra_args = params.size() - params.count("id");
806 if (params.contains("__cell_structure")) {
808 params, "__cell_structure");
809 so->configure(*this);
810 m_cell_structure = so;
811 --n_extra_args;
812 }
813 if (params.contains("__bonded_ias")) {
815 params, "__bonded_ias");
816 --n_extra_args;
817 }
818 return n_extra_args;
819}
820
821void ParticleHandle::do_construct(VariantMap const &params) {
822 auto const n_extra_args = setup_hidden_args(params);
823 m_pid = (params.contains("id")) ? get_value<int>(params, "id")
825
826#ifndef NDEBUG
827 if (not params.contains("id")) {
828 auto head_node_reference = m_pid;
829 boost::mpi::broadcast(context()->get_comm(), head_node_reference, 0);
830 assert(m_pid == head_node_reference && "global max_seen_pid has diverged");
831 }
832#endif
833
834 // create a new particle if extra arguments were passed
835 if (n_extra_args == 0) {
836 return;
837 }
838
839 auto const pos = get_value<Utils::Vector3d>(params, "pos");
840 context()->parallel_try_catch([&]() {
841 particle_checks(m_pid, pos);
842 auto &cell_structure = get_cell_structure()->get_cell_structure();
843 auto ptr = cell_structure.get_local_particle(m_pid);
844 if (ptr != nullptr) {
845 throw std::invalid_argument("Particle " + std::to_string(m_pid) +
846 " already exists");
847 }
848 });
849
850#ifdef ESPRESSO_ROTATION
851 context()->parallel_try_catch([&]() { sanity_checks_rotation(params); });
852#endif // ESPRESSO_ROTATION
853
854 // create a default-constructed particle
855 make_new_particle(m_pid, pos);
856
857 try {
858 context()->parallel_try_catch([&]() {
859 /* clang-format off */
860 // set particle properties (filter out read-only and deferred properties)
861 std::set<std::string_view> const skip = {
862 "pos_folded", "pos", "id", "node", "image_box", "bonds",
863#ifdef ESPRESSO_EXCLUSIONS
864 "exclusions",
865#endif // ESPRESSO_EXCLUSIONS
866 "lees_edwards_flag", "__cpt_sentinel",
867 };
868 /* clang-format on */
869 for (auto const &name : get_parameter_insertion_order()) {
870 if (params.contains(name) and not skip.contains(name)) {
871 do_set_parameter(name, params.at(name));
872 }
873 }
874 for (auto const &name : params | std::views::keys) {
875 if (not skip.contains(name) and not name.starts_with('_') and
876 not has_parameter(name)) {
877 auto error_msg = "Unknown parameter '" + name + "' for particle.";
878 std::string hint = "Hint: a feature is probably not compiled in.";
879 throw std::invalid_argument(error_msg + " " + hint);
880 }
881 }
882 if (not params.contains("type")) {
883 do_set_parameter("type", 0);
884 }
885#ifdef ESPRESSO_EXCLUSIONS
886 if (params.contains("exclusions")) {
887 do_call_method("set_exclusions", {{"p_ids", params.at("exclusions")}});
888 }
889#endif // ESPRESSO_EXCLUSIONS
890 });
891 } catch (...) {
892 remove_particle(m_pid);
893 throw;
894 }
895}
896
897} // namespace Particles
898} // namespace ScriptInterface
Vector implementation and trait types for boost qvm interoperability.
Data structures for bonded interactions.
bool add_bond(System::System &system, int bond_id, std::vector< int > const &particle_ids)
Add a bond to a particle.
Definition bonds.cpp:25
Immutable view on a bond.
Definition BondList.hpp:44
virtual boost::mpi::communicator const & get_comm() const =0
Context * context() const
Responsible context.
std::shared_ptr< BondedInteractionsMap > bonded_ias
void on_particle_change()
Called every time a particle property changes.
std::shared_ptr< BoxGeometry > box_geo
std::shared_ptr< InteractionsNonBonded > nonbonded_ias
std::vector< T > as_vector() const
Definition Vector.hpp:139
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
Definition Vector.hpp:131
std::optional< int > get_pid_for_vs_com(CellStructure &cell_structure, int mol_id)
Definition com.cpp:97
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
static uint8_t bitfield_from_flag(Utils::Vector3i const &flag)
static void sanity_checks_rotation(VariantMap const &params)
static auto get_quaternion_safe(std::string const &name, Variant const &value)
auto get_real_particle(boost::mpi::communicator const &comm, int p_id, ::CellStructure &cell_structure)
void particle_exclusion_sanity_checks(int pid1, int pid2, ::CellStructure &cell_structure, auto const &comm)
static auto get_gamma_safe(Variant const &value)
static auto quat2vector(Utils::Quaternion< double > const &q)
void local_remove_exclusion(int pid1, int pid2, ::CellStructure &cell_structure)
Locally remove an exclusion to a particle.
static auto constexpr contradicting_arguments_quat
void particle_checks(int p_id, Utils::Vector3d const &pos)
auto error_msg(std::string const &name, std::string const &reason)
void local_add_exclusion(int pid1, int pid2, ::CellStructure &cell_structure)
Locally add an exclusion to a particle.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:148
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
Definition Variant.hpp:131
System & get_system()
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
Quaternion< T > convert_director_to_quaternion(Vector< T, 3 > const &d)
Convert director to quaternion.
Vector< T, 3 > convert_quaternion_to_director(Quaternion< T > const &quat)
Convert quaternion to director.
Various procedures concerning interactions between particles.
void make_new_particle(int p_id, Utils::Vector3d const &pos)
Create a new particle and attach it to a cell.
const Particle & get_particle_data(int p_id)
Get particle data.
int get_particle_node(int p_id)
Get the MPI rank which owns the a specific particle.
void set_particle_pos(int p_id, Utils::Vector3d const &pos)
Move particle to a new position.
void remove_particle(int p_id)
Remove particle with a given identity.
int get_maximal_particle_id()
Get maximal particle id.
static auto & get_cell_structure()
void on_particle_type_change(int p_id, int old_type, int new_type)
Particles creation and deletion.
std::string propagation_bitmask_to_string(int propagation)
Convert a propagation modes bitmask to a string.
bool is_valid_propagation_combination(int propagation)
Note for developers: when enabling new propagation mode combinations, make sure every single line of ...
This file contains all subroutines required to process rotational motion.
Utils::Vector3d convert_vector_body_to_space(const Particle &p, const Utils::Vector3d &vec)
Definition rotation.hpp:58
std::pair< Utils::Quaternion< double >, double > convert_dip_to_quat(const Utils::Vector3d &dip)
convert a dipole moment to quaternions and dipolar strength
Definition rotation.hpp:109
Utils::Vector3d convert_vector_space_to_body(const Particle &p, const Utils::Vector3d &v)
Definition rotation.hpp:62
void local_rotate_particle(Particle &p, const Utils::Vector3d &axis_space_frame, const double phi)
Rotate the particle p around the NORMALIZED axis aSpaceFrame by amount phi.
Definition rotation.hpp:134
Properties of a self-propelled particle.
Definition Particle.hpp:50
bool swimming
Is the particle a swimmer.
Definition Particle.hpp:54
The following properties define, with respect to which real particle a virtual site is placed and at ...
Definition Particle.hpp:189
Struct holding all information for one particle.
Definition Particle.hpp:435
auto const & dip_fld() const
Definition Particle.hpp:568
bool has_exclusion(int pid) const
Definition Particle.hpp:648
auto const & swimming() const
Definition Particle.hpp:633
auto const & lees_edwards_offset() const
Definition Particle.hpp:486
auto const & propagation() const
Definition Particle.hpp:461
auto const & magnetic_anisotropy_energy() const
Definition Particle.hpp:554
auto const & rinertia() const
Definition Particle.hpp:572
auto const & stoner_wohlfarth_phi_0() const
Definition Particle.hpp:542
auto is_virtual() const
Definition Particle.hpp:588
auto const & mass() const
Definition Particle.hpp:492
auto const & magnetic_anisotropy_field_inv() const
Definition Particle.hpp:548
Utils::compact_vector< int > & exclusions()
Definition Particle.hpp:646
auto const & quat() const
Definition Particle.hpp:517
auto const & rotation() const
Definition Particle.hpp:498
auto const & vs_relative() const
Definition Particle.hpp:599
auto const & stoner_wohlfarth_tau0_inv() const
Definition Particle.hpp:558
auto const & q() const
Definition Particle.hpp:578
auto const & gamma() const
Definition Particle.hpp:603
auto const & gamma_rot() const
Definition Particle.hpp:606
auto const & saturation_magnetization() const
Definition Particle.hpp:544
auto const & stoner_wohlfarth_dt_incr() const
Definition Particle.hpp:562
auto const & lees_edwards_flag() const
Definition Particle.hpp:488
auto calc_dip() const
Definition Particle.hpp:535
auto const & v() const
Definition Particle.hpp:473
auto const & torque() const
Definition Particle.hpp:519
auto const & fixed() const
Definition Particle.hpp:611
auto const & ext_force() const
Definition Particle.hpp:626
auto const & omega() const
Definition Particle.hpp:521
auto const & image_box() const
Definition Particle.hpp:484
auto const & stoner_wohlfarth_is_enabled() const
Definition Particle.hpp:538
auto const & type() const
Definition Particle.hpp:458
auto const & ext_torque() const
Definition Particle.hpp:524
auto const & bonds() const
Definition Particle.hpp:468
auto const & dipm() const
Definition Particle.hpp:533
auto const & mol_id() const
Definition Particle.hpp:456
auto const & pos() const
Definition Particle.hpp:471
auto const & mu_E() const
Definition Particle.hpp:584
auto const & force() const
Definition Particle.hpp:475
Recursive variant implementation.
Definition Variant.hpp:84
Quaternion representation.
std::tuple< Utils::Quaternion< double >, double > calculate_vs_relate_to_params(Particle const &p_vs, Particle const &p_relate_to, BoxGeometry const &box_geo, double min_global_cut, bool override_cutoff_check)
Calculate the rotation quaternion and distance between two particles.