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 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"
34#include "core/exclusions.hpp"
37#include "core/propagation.hpp"
38#include "core/rotation.hpp"
41
42#include <utils/Vector.hpp>
44
45#include <boost/format.hpp>
46#include <boost/mpi/collectives/all_reduce.hpp>
47#include <boost/mpi/collectives/broadcast.hpp>
48#include <boost/mpi/communicator.hpp>
49
50#include <algorithm>
51#include <cassert>
52#include <cmath>
53#include <cstddef>
54#include <memory>
55#include <optional>
56#include <set>
57#include <sstream>
58#include <stdexcept>
59#include <string>
60#include <tuple>
61#include <type_traits>
62#include <vector>
63
64namespace ScriptInterface {
65namespace Particles {
66
67static void particle_checks(int p_id, Utils::Vector3d const &pos) {
68 if (p_id < 0) {
69 throw std::domain_error("Invalid particle id: " + std::to_string(p_id));
70 }
71#ifndef __FAST_MATH__
72 if (std::isnan(pos[0]) or std::isnan(pos[1]) or std::isnan(pos[2]) or
73 std::isinf(pos[0]) or std::isinf(pos[1]) or std::isinf(pos[2])) {
74 throw std::domain_error("Particle position must be finite");
75 }
76#endif // __FAST_MATH__
77}
78
80 auto bitfield = static_cast<uint8_t>(0u);
81 if (flag[0])
82 bitfield |= static_cast<uint8_t>(1u);
83 if (flag[1])
84 bitfield |= static_cast<uint8_t>(2u);
85 if (flag[2])
86 bitfield |= static_cast<uint8_t>(4u);
87 return bitfield;
88}
89
90static auto error_msg(std::string const &name, std::string const &reason) {
91 std::stringstream msg;
92 msg << "attribute '" << name << "' of 'ParticleHandle' " << reason;
93 return msg.str();
94}
95
97 return Utils::Vector4d{{q[0], q[1], q[2], q[3]}};
98}
99
100static auto get_quaternion_safe(std::string const &name, Variant const &value) {
101 auto const q = get_value<Utils::Vector4d>(value);
102 if (q.norm2() == 0.) {
103 throw std::domain_error(error_msg(name, "must be non-zero"));
104 }
105 return Utils::Quaternion<double>{{q[0], q[1], q[2], q[3]}};
106}
107
108#ifdef THERMOSTAT_PER_PARTICLE
109static auto get_gamma_safe(Variant const &value) {
110#ifdef PARTICLE_ANISOTROPY
111 try {
113 } catch (...) {
114 return get_value<Utils::Vector3d>(value);
115 }
116#else // PARTICLE_ANISOTROPY
117 return get_value<double>(value);
118#endif // PARTICLE_ANISOTROPY
119}
120#endif // THERMOSTAT_PER_PARTICLE
121
122static auto get_real_particle(boost::mpi::communicator const &comm, int p_id,
123 ::CellStructure &cell_structure) {
124 if (p_id < 0) {
125 throw std::domain_error("Invalid particle id: " + std::to_string(p_id));
126 }
127 auto ptr = cell_structure.get_local_particle(p_id);
128 if (ptr != nullptr and ptr->is_ghost()) {
129 ptr = nullptr;
130 }
131 auto const n_found = boost::mpi::all_reduce(
132 comm, static_cast<int>(ptr != nullptr), std::plus<>());
133 if (n_found == 0) {
134 throw std::runtime_error("Particle with id " + std::to_string(p_id) +
135 " not found");
136 }
137 return ptr;
138}
139
140template <typename T, class F>
141T ParticleHandle::get_particle_property(F const &fun) const {
142 auto cell_structure_si = get_cell_structure();
143 auto &cell_structure = cell_structure_si->get_cell_structure();
144 auto const &comm = context()->get_comm();
145 auto const ptr = const_cast<Particle const *>(
146 get_real_particle(comm, m_pid, cell_structure));
147 std::optional<T> ret;
148 if (ptr == nullptr) {
149 ret = {};
150 } else {
151 ret = {fun(*ptr)};
152 }
153 return Utils::Mpi::reduce_optional(comm, ret);
154}
155
156template <typename T>
157T ParticleHandle::get_particle_property(T const &(Particle::*getter)()
158 const) const {
159 return get_particle_property<T>(
160 [getter](Particle const &p) { return (p.*getter)(); });
161}
162
163template <class F>
164void ParticleHandle::set_particle_property(F const &fun) const {
165 auto cell_structure_si = get_cell_structure();
166 auto &cell_structure = cell_structure_si->get_cell_structure();
167 auto const &comm = context()->get_comm();
168 auto const ptr = get_real_particle(comm, m_pid, cell_structure);
169 if (ptr != nullptr) {
170 fun(*ptr);
171 }
173}
174
175template <typename T>
176void ParticleHandle::set_particle_property(T &(Particle::*setter)(),
177 Variant const &value) const {
178 set_particle_property(
179 [&value, setter](Particle &p) { (p.*setter)() = get_value<T>(value); });
180}
181
182ParticleHandle::ParticleHandle() {
183 /* Warning: the order of particle property setters matters! Some properties
184 * override each other, e.g. quat/director/dip or dip/dipm.
185 * This is revelant during checkpointing: all particle properties are set at
186 * once, in the order specified in the following call to `add_parameters()`.
187 */
188 add_parameters({
189 {"id", AutoParameter::read_only, [this]() { return m_pid; }},
190 {"type",
191 [this](Variant const &value) {
192 auto const old_type = get_particle_property(&Particle::type);
193 auto const new_type = get_value<int>(value);
194 if (new_type < 0) {
195 throw std::domain_error(
196 error_msg("type", "must be an integer >= 0"));
197 }
198 get_system()->nonbonded_ias->make_particle_type_exist(new_type);
199 on_particle_type_change(m_pid, old_type, new_type);
200 set_particle_property(&Particle::type, value);
201 },
202 [this]() { return get_particle_data(m_pid).type(); }},
203 {"pos",
204 [this](Variant const &value) {
205 auto const pos = get_value<Utils::Vector3d>(value);
206 particle_checks(m_pid, pos);
207 set_particle_pos(m_pid, pos);
208 },
209 [this]() {
210 auto const p = get_particle_data(m_pid);
211 auto const pos = p.pos();
212 auto const image_box = p.image_box();
213 return get_system()->box_geo->unfolded_position(pos, image_box);
214 }},
215 {"v",
216 [this](Variant const &value) {
217 set_particle_property(&Particle::v, value);
218 },
219 [this]() { return get_particle_data(m_pid).v(); }},
220 {"f",
221 [this](Variant const &value) {
222 set_particle_property(&Particle::force, value);
223 },
224 [this]() { return get_particle_data(m_pid).force(); }},
225 {"mass",
226#ifdef MASS
227 [this](Variant const &value) {
228 if (get_value<double>(value) <= 0.) {
229 throw std::domain_error(error_msg("mass", "must be a float > 0"));
230 }
231 set_particle_property(&Particle::mass, value);
232 },
233#else // MASS
234 [](Variant const &value) {
235 auto const default_mass = Particle().mass();
236 if (std::abs(get_value<double>(value) - default_mass) > 1e-10) {
237 throw std::runtime_error("Feature MASS not compiled in");
238 }
239 },
240#endif // MASS
241 [this]() { return get_particle_data(m_pid).mass(); }},
242 {"q",
243#ifdef ELECTROSTATICS
244 [this](Variant const &value) {
245 set_particle_property(&Particle::q, value);
246 },
247#else // ELECTROSTATICS
248 [](Variant const &value) {
249 if (get_value<double>(value) != 0.) {
250 throw std::runtime_error("Feature ELECTROSTATICS not compiled in");
251 }
252 },
253#endif // ELECTROSTATICS
254 [this]() { return get_particle_data(m_pid).q(); }},
255#ifdef DIPOLES
256 {"dip",
257 [this](Variant const &value) {
258 set_particle_property([&value](Particle &p) {
259 auto const dip = get_value<Utils::Vector3d>(value);
260 std::tie(p.quat(), p.dipm()) = convert_dip_to_quat(dip);
261 });
262 },
263 [this]() { return get_particle_data(m_pid).calc_dip(); }},
264 {"dipm",
265 [this](Variant const &value) {
266 set_particle_property(&Particle::dipm, value);
267 },
268 [this]() { return get_particle_data(m_pid).dipm(); }},
269#endif // DIPOLES
270#ifdef DIPOLE_FIELD_TRACKING
271 {"dip_fld",
272 [this](Variant const &value) {
273 set_particle_property(&Particle::dip_fld, value);
274 },
275 [this]() { return get_particle_data(m_pid).dip_fld(); }},
276#endif
277#ifdef ROTATION
278 {"director",
279 [this](Variant const &value) {
280 set_particle_property([&value](Particle &p) {
281 auto const director = get_value<Utils::Vector3d>(value).normalized();
283 });
284 },
285 [this]() {
286 auto const quat = get_particle_data(m_pid).quat();
288 }},
289 {"quat",
290 [this](Variant const &value) {
291 auto const quat = get_quaternion_safe("quat", value);
292 set_particle_property([&quat](Particle &p) { p.quat() = quat; });
293 },
294 [this]() { return quat2vector(get_particle_data(m_pid).quat()); }},
295 {"omega_body",
296 [this](Variant const &value) {
297 set_particle_property(&Particle::omega, value);
298 },
299 [this]() { return get_particle_data(m_pid).omega(); }},
300 {"rotation",
301 [this](Variant const &value) {
302 set_particle_property([&value](Particle &p) {
303 auto const rotation_flag =
304 Utils::Vector3i{(get_value<Utils::Vector3b>(value))};
305 p.rotation() = bitfield_from_flag(rotation_flag);
306 });
307 },
308 [this]() {
309 auto const rotation_bits = get_particle_data(m_pid).rotation();
310 return Utils::Vector3b{{::detail::get_nth_bit(rotation_bits, 0),
311 ::detail::get_nth_bit(rotation_bits, 1),
312 ::detail::get_nth_bit(rotation_bits, 2)}};
313 }},
314 {"omega_lab",
315 [this](Variant const &value) {
316 set_particle_property([&value](Particle &p) {
317 auto const omega = get_value<Utils::Vector3d>(value);
318 p.omega() = convert_vector_space_to_body(p, omega);
319 });
320 },
321 [this]() {
322 auto &p = get_particle_data(m_pid);
323 return convert_vector_body_to_space(p, p.omega());
324 }},
325 {"torque_lab",
326 [this](Variant const &value) {
327 set_particle_property([&value](Particle &p) {
328 auto const torque = get_value<Utils::Vector3d>(value);
329 p.torque() = convert_vector_space_to_body(p, torque);
330 });
331 },
332 [this]() {
333 auto &p = get_particle_data(m_pid);
334 return convert_vector_body_to_space(p, p.torque());
335 }},
336#endif // ROTATION
337#ifdef ROTATIONAL_INERTIA
338 {"rinertia",
339 [this](Variant const &value) {
340 set_particle_property(&Particle::rinertia, value);
341 },
342 [this]() { return get_particle_data(m_pid).rinertia(); }},
343#endif // ROTATIONAL_INERTIA
344#ifdef LB_ELECTROHYDRODYNAMICS
345 {"mu_E",
346 [this](Variant const &value) {
347 set_particle_property(&Particle::mu_E, value);
348 },
349 [this]() { return get_particle_data(m_pid).mu_E(); }},
350#endif // LB_ELECTROHYDRODYNAMICS
351#ifdef EXTERNAL_FORCES
352 {"fix",
353 [this](Variant const &value) {
354 set_particle_property([&value](Particle &p) {
355 auto const fix_flag =
356 Utils::Vector3i{(get_value<Utils::Vector3b>(value))};
357 p.fixed() = bitfield_from_flag(fix_flag);
358 });
359 },
360 [this]() {
361 auto const fixed = get_particle_data(m_pid).fixed();
362 return Utils::Vector3b{{::detail::get_nth_bit(fixed, 0),
363 ::detail::get_nth_bit(fixed, 1),
364 ::detail::get_nth_bit(fixed, 2)}};
365 }},
366 {"ext_force",
367 [this](Variant const &value) {
368 set_particle_property(&Particle::ext_force, value);
369 },
370 [this]() { return get_particle_data(m_pid).ext_force(); }},
371#ifdef ROTATION
372 {"ext_torque",
373 [this](Variant const &value) {
374 set_particle_property(&Particle::ext_torque, value);
375 },
376 [this]() { return get_particle_data(m_pid).ext_torque(); }},
377#endif // ROTATION
378#endif // EXTERNAL_FORCES
379#ifdef THERMOSTAT_PER_PARTICLE
380 {"gamma",
381 [this](Variant const &value) {
382 set_particle_property(&Particle::gamma,
383 Variant{get_gamma_safe(value)});
384 },
385 [this]() { return get_particle_data(m_pid).gamma(); }},
386#ifdef ROTATION
387 {"gamma_rot",
388 [this](Variant const &value) {
389 set_particle_property(&Particle::gamma_rot,
390 Variant{get_gamma_safe(value)});
391 },
392 [this]() { return get_particle_data(m_pid).gamma_rot(); }},
393#endif // ROTATION
394#endif // THERMOSTAT_PER_PARTICLE
395 {"pos_folded", AutoParameter::read_only,
396 [this]() {
397 auto const &box_geo = *get_system()->box_geo;
398 return box_geo.folded_position(get_particle_data(m_pid).pos());
399 }},
400 {"lees_edwards_offset",
401 [this](Variant const &value) {
402 set_particle_property(&Particle::lees_edwards_offset, value);
403 },
404 [this]() { return get_particle_data(m_pid).lees_edwards_offset(); }},
405 {"lees_edwards_flag", AutoParameter::read_only,
406 [this]() { return get_particle_data(m_pid).lees_edwards_flag(); }},
407 {"image_box", AutoParameter::read_only,
408 [this]() {
409 auto const &box_geo = *get_system()->box_geo;
410 auto const p = get_particle_data(m_pid);
411 return box_geo.folded_image_box(p.pos(), p.image_box());
412 }},
413 {"node", AutoParameter::read_only,
414 [this]() {
415 return (context()->is_head_node()) ? get_particle_node(m_pid) : -1;
416 }},
417 {"mol_id",
418 [this](Variant const &value) {
419 auto const mol_id = get_value<int>(value);
420 if (mol_id < 0) {
421 throw std::domain_error(
422 error_msg("mol_id", "must be an integer >= 0"));
423 }
424 set_particle_property(&Particle::mol_id, Variant{mol_id});
425 },
426 [this]() { return get_particle_data(m_pid).mol_id(); }},
427#ifdef VIRTUAL_SITES_RELATIVE
428 {"vs_quat",
429 [this](Variant const &value) {
430 auto const quat = get_quaternion_safe("vs_quat", value);
431 set_particle_property(
432 [&quat](Particle &p) { p.vs_relative().quat = quat; });
433 },
434 [this]() {
435 return quat2vector(get_particle_data(m_pid).vs_relative().quat);
436 }},
437 {"vs_relative",
438 [this](Variant const &value) {
440 try {
441 auto const array = get_value<std::vector<Variant>>(value);
442 if (array.size() != 3) {
443 throw 0;
444 }
445 vs_relative.distance = get_value<double>(array[1]);
446 vs_relative.to_particle_id = get_value<int>(array[0]);
447 vs_relative.rel_orientation =
448 get_quaternion_safe("vs_relative", array[2]);
449 } catch (...) {
450 throw std::invalid_argument(error_msg(
451 "vs_relative", "must take the form [id, distance, quaternion]"));
452 }
453 set_particle_property(
454 [&vs_relative](Particle &p) { p.vs_relative() = vs_relative; });
455 },
456 [this]() {
457 auto const vs_rel = get_particle_data(m_pid).vs_relative();
458 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
459 quat2vector(vs_rel.rel_orientation)}};
460 }},
461#endif // VIRTUAL_SITES_RELATIVE
462 {"propagation",
463 [this](Variant const &value) {
464 auto const propagation = get_value<int>(value);
465 if (!is_valid_propagation_combination(propagation)) {
466 throw std::domain_error(error_msg(
467 "propagation", "propagation combination not accepted: " +
468 propagation_bitmask_to_string(propagation)));
469 }
470 set_particle_property(&Particle::propagation, value);
471 },
472 [this]() { return get_particle_data(m_pid).propagation(); }},
473#ifdef ENGINE
474 {"swimming",
475 [this](Variant const &value) {
476 set_particle_property([&value](Particle &p) {
478 swim.swimming = true;
479 auto const dict = get_value<VariantMap>(value);
480 if (dict.count("f_swim") != 0) {
481 swim.f_swim = get_value<double>(dict.at("f_swim"));
482 }
483 if (dict.count("is_engine_force_on_fluid") != 0) {
484 auto const is_engine_force_on_fluid =
485 get_value<bool>(dict.at("is_engine_force_on_fluid"));
486 swim.is_engine_force_on_fluid = is_engine_force_on_fluid;
487 }
488 p.swimming() = swim;
489 });
490 },
491 [this]() {
492 auto const swim = get_particle_data(m_pid).swimming();
493 return VariantMap{
494 {"f_swim", swim.f_swim},
495 {"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
496 };
497 }},
498#endif // ENGINE
499 });
500}
501
502#ifdef EXCLUSIONS
503/**
504 * @brief Locally add an exclusion to a particle.
505 * @param pid1 the identity of the first exclusion partner
506 * @param pid2 the identity of the second exclusion partner
507 * @param cell_structure the cell structure
508 */
509static void local_add_exclusion(int pid1, int pid2,
510 ::CellStructure &cell_structure) {
511 if (auto p1 = cell_structure.get_local_particle(pid1)) {
512 add_exclusion(*p1, pid2);
513 }
514 if (auto p2 = cell_structure.get_local_particle(pid2)) {
515 add_exclusion(*p2, pid1);
516 }
517}
518
519/**
520 * @brief Locally remove an exclusion to a particle.
521 * @param pid1 the identity of the first exclusion partner
522 * @param pid2 the identity of the second exclusion partner
523 * @param cell_structure the cell structure
524 */
525static void local_remove_exclusion(int pid1, int pid2,
526 ::CellStructure &cell_structure) {
527 if (auto p1 = cell_structure.get_local_particle(pid1)) {
528 delete_exclusion(*p1, pid2);
529 }
530 if (auto p2 = cell_structure.get_local_particle(pid2)) {
531 delete_exclusion(*p2, pid1);
532 }
533}
534
535void ParticleHandle::particle_exclusion_sanity_checks(int pid1,
536 int pid2) const {
537 if (pid1 == pid2) {
538 throw std::runtime_error("Particles cannot exclude themselves (id " +
539 std::to_string(pid1) + ")");
540 }
541 auto cell_structure_si = get_cell_structure();
542 auto &cell_structure = cell_structure_si->get_cell_structure();
543 std::ignore = get_real_particle(context()->get_comm(), pid1, cell_structure);
544 std::ignore = get_real_particle(context()->get_comm(), pid2, cell_structure);
545}
546#endif // EXCLUSIONS
547
548Variant ParticleHandle::do_call_method(std::string const &name,
549 VariantMap const &params) {
550 if (name == "set_param_parallel") {
551 auto const param_name = get_value<std::string>(params, "name");
552 if (params.count("value") == 0) {
553 throw Exception("Parameter '" + param_name + "' is missing.");
554 }
555 auto const &value = params.at("value");
556 context()->parallel_try_catch(
557 [&]() { do_set_parameter(param_name, value); });
558 return {};
559 }
560 if (name == "get_bond_by_id") {
561 if (not context()->is_head_node()) {
562 return {};
563 }
564 return get_bonded_ias()->call_method("get_bond", params);
565 }
566 if (name == "get_bonds_view") {
567 if (not context()->is_head_node()) {
568 return {};
569 }
570 auto const bond_list = get_particle_data(m_pid).bonds();
571 std::vector<std::vector<Variant>> bonds_flat;
572 for (auto const &&bond_view : bond_list) {
573 std::vector<Variant> bond_flat;
574 bond_flat.emplace_back(bond_view.bond_id());
575 for (auto const pid : bond_view.partner_ids()) {
576 bond_flat.emplace_back(pid);
577 }
578 bonds_flat.emplace_back(std::move(bond_flat));
579 }
580 return make_vector_of_variants(bonds_flat);
581 }
582 if (name == "add_bond") {
583 set_particle_property([&params](Particle &p) {
584 auto const bond_id = get_value<int>(params, "bond_id");
585 auto const part_id = get_value<std::vector<int>>(params, "part_id");
586 auto const bond_view =
587 BondView(bond_id, {part_id.data(), part_id.size()});
588 p.bonds().insert(bond_view);
589 });
590 } else if (name == "del_bond") {
591 set_particle_property([&params](Particle &p) {
592 auto const bond_id = get_value<int>(params, "bond_id");
593 auto const part_id = get_value<std::vector<int>>(params, "part_id");
594 auto const bond_view =
595 BondView(bond_id, {part_id.data(), part_id.size()});
596 auto &bond_list = p.bonds();
597 auto it = std::find(bond_list.begin(), bond_list.end(), bond_view);
598 if (it != bond_list.end()) {
599 bond_list.erase(it);
600 }
601 });
602 } else if (name == "delete_all_bonds") {
603 set_particle_property([&](Particle &p) { p.bonds().clear(); });
604 } else if (name == "is_valid_bond_id") {
605 auto const bond_id = get_value<int>(params, "bond_id");
606 return get_system()->bonded_ias->get_zero_based_type(bond_id) != 0;
607 }
608 if (name == "remove_particle") {
609 context()->parallel_try_catch([&]() {
610 auto cell_structure_si = get_cell_structure();
611 auto &cell_structure = cell_structure_si->get_cell_structure();
612 std::ignore =
613 get_real_particle(context()->get_comm(), m_pid, cell_structure);
614 remove_particle(m_pid);
615 });
616 } else if (name == "is_virtual") {
617 if (not context()->is_head_node()) {
618 return {};
619 }
620 return get_particle_data(m_pid).is_virtual();
621#ifdef VIRTUAL_SITES_RELATIVE
622 } else if (name == "vs_auto_relate_to") {
623 if (not context()->is_head_node()) {
624 return {};
625 }
626 auto const other_pid = get_value<int>(params, "pid");
627 auto const override_cutoff_check =
628 get_value<bool>(params, "override_cutoff_check");
629 if (m_pid == other_pid) {
630 throw std::invalid_argument("A virtual site cannot relate to itself");
631 }
632 if (other_pid < 0) {
633 throw std::domain_error("Invalid particle id: " +
634 std::to_string(other_pid));
635 }
636 auto const system = get_system();
637 /* note: this code can be rewritten as parallel code, but only with a call
638 * to `cells_update_ghosts(DATA_PART_POSITION | DATA_PART_PROPERTIES)`, as
639 * there is no guarantee the virtual site has visibility of the relative
640 * particle through the ghost layer during particle creation. However,
641 * ghost updates can scramble the particle ordering in the local cells,
642 * which is an issue for checkpointing: the H5MD writer will use the
643 * scrambled ordering before writing to a checkpoint file and the
644 * non-scrambled ordering after reloading from a checkpoint file.
645 */
646 auto const &p_current = get_particle_data(m_pid);
647 auto const &p_relate_to = get_particle_data(other_pid);
648 auto const [quat, dist] = calculate_vs_relate_to_params(
649 p_current, p_relate_to, *system->box_geo, system->get_min_global_cut(),
650 override_cutoff_check);
651 set_parameter("vs_relative", Variant{std::vector<Variant>{
652 {other_pid, dist, quat2vector(quat)}}});
653 set_parameter("propagation",
656#endif // VIRTUAL_SITES_RELATIVE
657#ifdef EXCLUSIONS
658 } else if (name == "has_exclusion") {
659 auto const other_pid = get_value<int>(params, "pid");
660 auto cell_structure_si = get_cell_structure();
661 auto &cell_structure = cell_structure_si->get_cell_structure();
662 auto const p =
663 get_real_particle(context()->get_comm(), m_pid, cell_structure);
664 if (p != nullptr) {
665 return p->has_exclusion(other_pid);
666 }
667 }
668 if (name == "add_exclusion") {
669 auto const other_pid = get_value<int>(params, "pid");
670 auto cell_structure_si = get_cell_structure();
671 auto &cell_structure = cell_structure_si->get_cell_structure();
672 context()->parallel_try_catch(
673 [&]() { particle_exclusion_sanity_checks(m_pid, other_pid); });
674 local_add_exclusion(m_pid, other_pid, cell_structure);
675 get_system()->on_particle_change();
676 } else if (name == "del_exclusion") {
677 auto const other_pid = get_value<int>(params, "pid");
678 auto cell_structure_si = get_cell_structure();
679 auto &cell_structure = cell_structure_si->get_cell_structure();
680 context()->parallel_try_catch(
681 [&]() { particle_exclusion_sanity_checks(m_pid, other_pid); });
682 local_remove_exclusion(m_pid, other_pid, cell_structure);
683 get_system()->on_particle_change();
684 } else if (name == "set_exclusions") {
685 std::vector<int> exclusion_list;
686 try {
687 auto const pid = get_value<int>(params, "p_ids");
688 exclusion_list.push_back(pid);
689 } catch (...) {
690 exclusion_list = get_value<std::vector<int>>(params, "p_ids");
691 }
692 context()->parallel_try_catch([&]() {
693 for (auto const pid : exclusion_list) {
694 particle_exclusion_sanity_checks(m_pid, pid);
695 }
696 });
697 set_particle_property([this, &exclusion_list](Particle &p) {
698 auto cell_structure_si = get_cell_structure();
699 auto &cell_structure = cell_structure_si->get_cell_structure();
700 for (auto const pid : p.exclusions()) {
701 local_remove_exclusion(m_pid, pid, cell_structure);
702 }
703 for (auto const pid : exclusion_list) {
704 if (!p.has_exclusion(pid)) {
705 local_add_exclusion(m_pid, pid, cell_structure);
706 }
707 }
708 });
709 } else if (name == "get_exclusions") {
710 if (not context()->is_head_node()) {
711 return {};
712 }
713 auto const excl_list = get_particle_data(m_pid).exclusions();
714 return Variant{std::vector<int>{excl_list.begin(), excl_list.end()}};
715#endif // EXCLUSIONS
716#ifdef ROTATION
717 }
718 if (name == "rotate_particle") {
719 set_particle_property([&params](Particle &p) {
720 auto const axis = get_value<Utils::Vector3d>(params, "axis");
721 auto const angle = get_value<double>(params, "angle");
722 local_rotate_particle(p, axis, angle);
723 });
724 }
725 if (name == "convert_vector_body_to_space") {
726 return get_particle_property<std::vector<double>>(
727 [&params](Particle const &p) {
728 auto const vec = get_value<Utils::Vector3d>(params, "vec");
730 });
731 }
732 if (name == "convert_vector_space_to_body") {
733 return get_particle_property<std::vector<double>>(
734 [&params](Particle const &p) {
735 auto const vec = get_value<Utils::Vector3d>(params, "vec");
737 });
738#endif // ROTATION
739 }
740 return {};
741}
742
743#ifdef ROTATION
744static auto const contradicting_arguments_quat = std::vector<
745 std::array<std::string, 3>>{{
746 {{"dip", "dipm",
747 "Setting 'dip' is sufficient as it defines the scalar dipole moment."}},
748 {{"quat", "director",
749 "Setting 'quat' is sufficient as it defines the director."}},
750 {{"dip", "quat",
751 "Setting 'dip' would overwrite 'quat'. Set 'quat' and 'dipm' instead."}},
752 {{"dip", "director",
753 "Setting 'dip' would overwrite 'director'. Set 'director' and "
754 "'dipm' instead."}},
755}};
756#endif // ROTATION
757
758void ParticleHandle::do_construct(VariantMap const &params) {
759 auto const n_extra_args = params.size() - params.count("id") -
760 params.count("__cell_structure") -
761 params.count("__bonded_ias");
762 m_pid = (params.contains("id")) ? get_value<int>(params, "id")
764
765#ifndef NDEBUG
766 if (not params.contains("id")) {
767 auto head_node_reference = m_pid;
768 boost::mpi::broadcast(context()->get_comm(), head_node_reference, 0);
769 assert(m_pid == head_node_reference && "global max_seen_pid has diverged");
770 }
771#endif
772
773 if (params.contains("__cell_structure")) {
774 auto so = get_value<std::shared_ptr<CellSystem::CellSystem>>(
775 params, "__cell_structure");
776 so->configure(*this);
777 m_cell_structure = so;
778 }
779 if (params.contains("__bonded_ias")) {
780 m_bonded_ias = get_value<std::shared_ptr<Interactions::BondedInteractions>>(
781 params, "__bonded_ias");
782 }
783
784 // create a new particle if extra arguments were passed
785 if (n_extra_args == 0) {
786 return;
787 }
788
789 auto const pos = get_value<Utils::Vector3d>(params, "pos");
790 context()->parallel_try_catch([&]() {
791 particle_checks(m_pid, pos);
792 auto cell_structure_si = get_cell_structure();
793 auto &cell_structure = cell_structure_si->get_cell_structure();
794 auto ptr = cell_structure.get_local_particle(m_pid);
795 if (ptr != nullptr) {
796 throw std::invalid_argument("Particle " + std::to_string(m_pid) +
797 " already exists");
798 }
799 });
800
801#ifdef ROTATION
802 context()->parallel_try_catch([&]() {
803 // if we are not constructing a particle from a checkpoint file,
804 // check the quaternion is not accidentally set twice by the user
805 if (not params.contains("__cpt_sentinel")) {
806 auto formatter =
807 boost::format("Contradicting particle attributes: '%s' and '%s'. %s");
808 for (auto const &[prop1, prop2, reason] : contradicting_arguments_quat) {
809 if (params.contains(prop1) and params.contains(prop2)) {
810 auto const err_msg = boost::str(formatter % prop1 % prop2 % reason);
811 throw std::invalid_argument(err_msg);
812 }
813 }
814 }
815 });
816#endif // ROTATION
817
818 // create a default-constructed particle
819 make_new_particle(m_pid, pos);
820
821 try {
822 context()->parallel_try_catch([&]() {
823 /* clang-format off */
824 // set particle properties (filter out read-only and deferred properties)
825 std::set<std::string> const skip = {
826 "pos_folded", "pos", "id", "exclusions", "node", "image_box", "bonds",
827 "lees_edwards_flag", "__cpt_sentinel",
828 };
829 /* clang-format on */
830 for (auto const &name : get_parameter_insertion_order()) {
831 if (params.contains(name) and not skip.contains(name)) {
832 do_set_parameter(name, params.at(name));
833 }
834 }
835 if (not params.contains("type")) {
836 do_set_parameter("type", 0);
837 }
838 });
839 } catch (...) {
840 remove_particle(m_pid);
841 throw;
842 }
843}
844
845} // namespace Particles
846} // namespace ScriptInterface
Vector implementation and trait types for boost qvm interoperability.
Data structures for bonded interactions.
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:118
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value)
Create a vector that has all entries set to the same value.
Definition Vector.hpp:110
This file contains the defaults for ESPResSo.
void delete_exclusion(Particle &p, int p_id)
Remove exclusion from particle if possible.
void add_exclusion(Particle &p, int p_id)
Insert an exclusion if not already set.
static uint8_t bitfield_from_flag(Utils::Vector3i const &flag)
static void local_remove_exclusion(int pid1, int pid2, ::CellStructure &cell_structure)
Locally remove an exclusion to a particle.
static void particle_checks(int p_id, Utils::Vector3d const &pos)
static void local_add_exclusion(int pid1, int pid2, ::CellStructure &cell_structure)
Locally add an exclusion to a particle.
static auto get_quaternion_safe(std::string const &name, Variant const &value)
static auto const contradicting_arguments_quat
static auto get_gamma_safe(Variant const &value)
static auto quat2vector(Utils::Quaternion< double > const &q)
static auto get_real_particle(boost::mpi::communicator const &comm, int p_id, ::CellStructure &cell_structure)
static auto error_msg(std::string const &name, std::string const &reason)
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:88
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:67
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:57
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:108
Utils::Vector3d convert_vector_space_to_body(const Particle &p, const Utils::Vector3d &v)
Definition rotation.hpp:61
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:133
static SteepestDescentParameters params
Currently active steepest descent instance.
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
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:152
Struct holding all information for one particle.
Definition Particle.hpp:395
auto const & dip_fld() const
Definition Particle.hpp:498
bool has_exclusion(int pid) const
Definition Particle.hpp:576
auto const & swimming() const
Definition Particle.hpp:561
auto const & lees_edwards_offset() const
Definition Particle.hpp:446
auto const & propagation() const
Definition Particle.hpp:421
auto const & rinertia() const
Definition Particle.hpp:502
auto is_virtual() const
Definition Particle.hpp:518
auto const & mass() const
Definition Particle.hpp:452
Utils::compact_vector< int > & exclusions()
Definition Particle.hpp:574
auto const & quat() const
Definition Particle.hpp:477
auto const & rotation() const
Definition Particle.hpp:458
auto const & vs_relative() const
Definition Particle.hpp:527
auto const & q() const
Definition Particle.hpp:508
auto const & gamma() const
Definition Particle.hpp:531
auto const & gamma_rot() const
Definition Particle.hpp:534
auto const & lees_edwards_flag() const
Definition Particle.hpp:448
auto calc_dip() const
Definition Particle.hpp:495
auto const & v() const
Definition Particle.hpp:433
auto const & torque() const
Definition Particle.hpp:479
auto const & fixed() const
Definition Particle.hpp:539
auto const & ext_force() const
Definition Particle.hpp:554
auto const & omega() const
Definition Particle.hpp:481
auto const & type() const
Definition Particle.hpp:418
auto const & ext_torque() const
Definition Particle.hpp:484
auto const & bonds() const
Definition Particle.hpp:428
auto const & dipm() const
Definition Particle.hpp:493
auto const & mol_id() const
Definition Particle.hpp:416
auto const & mu_E() const
Definition Particle.hpp:514
auto const & force() const
Definition Particle.hpp:435
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.