46#include <boost/format.hpp>
47#include <boost/mpi/collectives/all_reduce.hpp>
48#include <boost/mpi/collectives/broadcast.hpp>
49#include <boost/mpi/communicator.hpp>
70 throw std::domain_error(
"Invalid particle id: " + std::to_string(
p_id));
73 if (std::isnan(pos[0])
or std::isnan(pos[1])
or std::isnan(pos[2])
or
74 std::isinf(pos[0])
or std::isinf(pos[1])
or std::isinf(pos[2])) {
75 throw std::domain_error(
"Particle position must be finite");
92 std::stringstream
msg;
93 msg <<
"attribute '" << name <<
"' of 'ParticleHandle' " <<
reason;
103 if (q.norm2() == 0.) {
104 throw std::domain_error(
error_msg(name,
"must be non-zero"));
109#ifdef THERMOSTAT_PER_PARTICLE
111#ifdef PARTICLE_ANISOTROPY
126 throw std::domain_error(
"Invalid particle id: " + std::to_string(
p_id));
129 if (ptr !=
nullptr and ptr->is_ghost()) {
132 auto const n_found = boost::mpi::all_reduce(
133 comm,
static_cast<int>(ptr !=
nullptr), std::plus<>());
135 throw std::runtime_error(
"Particle with id " + std::to_string(
p_id) +
141template <
typename T,
class F>
142T ParticleHandle::get_particle_property(
F const &
fun)
const {
146 auto const ptr =
const_cast<Particle const *
>(
148 std::optional<T>
ret;
149 if (ptr ==
nullptr) {
158T ParticleHandle::get_particle_property(T
const &(
Particle::*
getter)()
160 return get_particle_property<T>(
165void ParticleHandle::set_particle_property(F
const &fun)
const {
167 auto &cell_structure = cell_structure_si->get_cell_structure();
168 auto const &comm = context()->get_comm();
170 if (ptr !=
nullptr) {
177void ParticleHandle::set_particle_property(T &(
Particle::*setter)(),
179 set_particle_property(
183ParticleHandle::ParticleHandle() {
190 {
"id", AutoParameter::read_only, [
this]() {
return m_pid; }},
194 auto const new_type = get_value<int>(value);
196 throw std::domain_error(
197 error_msg(
"type",
"must be an integer >= 0"));
199 get_system()->
nonbonded_ias->make_particle_type_exist(new_type);
206 auto const pos = get_value<Utils::Vector3d>(value);
212 auto const pos = p.pos();
213 auto const image_box = p.image_box();
214 return get_system()->
box_geo->unfolded_position(pos, image_box);
229 if (get_value<double>(value) <= 0.) {
230 throw std::domain_error(
error_msg(
"mass",
"must be a float > 0"));
237 if (std::abs(get_value<double>(value) - default_mass) > 1e-10) {
238 throw std::runtime_error(
"Feature MASS not compiled in");
250 if (get_value<double>(value) != 0.) {
251 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
259 set_particle_property([&value](
Particle &p) {
260 auto const dip = get_value<Utils::Vector3d>(value);
271#ifdef DIPOLE_FIELD_TRACKING
281 set_particle_property([&value](
Particle &p) {
282 auto const director = get_value<Utils::Vector3d>(value).normalized();
293 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
303 set_particle_property([&value](
Particle &p) {
304 auto const rotation_flag =
312 ::detail::get_nth_bit(rotation_bits, 1),
313 ::detail::get_nth_bit(rotation_bits, 2)}};
317 set_particle_property([&value](
Particle &p) {
318 auto const omega = get_value<Utils::Vector3d>(value);
328 set_particle_property([&value](
Particle &p) {
329 auto const torque = get_value<Utils::Vector3d>(value);
338#ifdef ROTATIONAL_INERTIA
345#ifdef LB_ELECTROHYDRODYNAMICS
352#ifdef EXTERNAL_FORCES
355 set_particle_property([&value](
Particle &p) {
356 auto const fix_flag =
364 ::detail::get_nth_bit(fixed, 1),
365 ::detail::get_nth_bit(fixed, 2)}};
380#ifdef THERMOSTAT_PER_PARTICLE
396 {
"pos_folded", AutoParameter::read_only,
398 auto const &box_geo = *get_system()->
box_geo;
401 {
"lees_edwards_offset",
406 {
"lees_edwards_flag", AutoParameter::read_only,
408 {
"image_box", AutoParameter::read_only,
410 auto const &box_geo = *get_system()->
box_geo;
412 return box_geo.folded_image_box(p.pos(), p.image_box());
414 {
"node", AutoParameter::read_only,
420 auto const mol_id = get_value<int>(value);
422 throw std::domain_error(
423 error_msg(
"mol_id",
"must be an integer >= 0"));
428#ifdef VIRTUAL_SITES_RELATIVE
432 set_particle_property(
442 auto const array = get_value<std::vector<Variant>>(value);
443 if (array.size() != 3) {
446 vs_relative.
distance = get_value<double>(array[1]);
447 vs_relative.to_particle_id = get_value<int>(array[0]);
448 vs_relative.rel_orientation =
452 "vs_relative",
"must take the form [id, distance, quaternion]"));
454 set_particle_property(
459 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
465 auto const propagation = get_value<int>(value);
468 "propagation",
"propagation combination not accepted: " +
477 set_particle_property([&value](
Particle &p) {
480 auto const dict = get_value<VariantMap>(value);
481 if (dict.count(
"f_swim") != 0) {
482 swim.f_swim = get_value<double>(dict.at(
"f_swim"));
484 if (dict.count(
"is_engine_force_on_fluid") != 0) {
485 auto const is_engine_force_on_fluid =
486 get_value<bool>(dict.at(
"is_engine_force_on_fluid"));
487 swim.is_engine_force_on_fluid = is_engine_force_on_fluid;
495 {
"f_swim", swim.f_swim},
496 {
"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
536void ParticleHandle::particle_exclusion_sanity_checks(
int pid1,
539 throw std::runtime_error(
"Particles cannot exclude themselves (id " +
540 std::to_string(pid1) +
")");
543 auto &cell_structure = cell_structure_si->get_cell_structure();
544 std::ignore = get_real_particle(context()->get_comm(), pid1, cell_structure);
545 std::ignore = get_real_particle(context()->get_comm(), pid2, cell_structure);
549Variant ParticleHandle::do_call_method(std::string
const &name,
551 if (name ==
"set_param_parallel") {
552 auto const param_name = get_value<std::string>(
params,
"name");
553 if (
params.count(
"value") == 0) {
554 throw Exception(
"Parameter '" + param_name +
"' is missing.");
556 auto const &value =
params.at(
"value");
557 context()->parallel_try_catch(
558 [&]() { do_set_parameter(param_name, value); });
561 if (name ==
"get_bond_by_id") {
562 if (not context()->is_head_node()) {
565 return get_bonded_ias()->call_method(
"get_bond",
params);
567 if (name ==
"get_bonds_view") {
568 if (not context()->is_head_node()) {
572 std::vector<std::vector<Variant>> bonds_flat;
573 for (
auto const &&bond_view : bond_list) {
574 std::vector<Variant> bond_flat;
575 bond_flat.emplace_back(bond_view.bond_id());
576 for (
auto const pid : bond_view.partner_ids()) {
577 bond_flat.emplace_back(pid);
579 bonds_flat.emplace_back(std::move(bond_flat));
583 if (name ==
"add_bond") {
584 auto const bond_id = get_value<int>(
params,
"bond_id");
585 auto const partner_ids = get_value<std::vector<int>>(
params,
"part_id");
586 std::vector<int> particle_ids = {m_pid};
587 std::ranges::copy(partner_ids, std::back_inserter(particle_ids));
588 ::add_bond(*get_system(), bond_id, particle_ids);
590 }
else if (name ==
"del_bond") {
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()) {
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;
608 if (name ==
"remove_particle") {
609 context()->parallel_try_catch([&]() {
611 auto &cell_structure = cell_structure_si->get_cell_structure();
616 }
else if (name ==
"is_virtual") {
617 if (not context()->is_head_node()) {
621#ifdef VIRTUAL_SITES_RELATIVE
622 }
else if (name ==
"vs_auto_relate_to") {
623 if (not context()->is_head_node()) {
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");
633 throw std::domain_error(
"Invalid particle id: " +
634 std::to_string(other_pid));
636 auto const system = get_system();
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>{
653 set_parameter(
"propagation",
658 }
else if (name ==
"has_exclusion") {
659 auto const other_pid = get_value<int>(
params,
"pid");
661 auto &cell_structure = cell_structure_si->get_cell_structure();
665 return p->has_exclusion(other_pid);
668 if (name ==
"add_exclusion") {
669 auto const other_pid = get_value<int>(
params,
"pid");
671 auto &cell_structure = cell_structure_si->get_cell_structure();
672 context()->parallel_try_catch(
673 [&]() { particle_exclusion_sanity_checks(m_pid, other_pid); });
676 }
else if (name ==
"del_exclusion") {
677 auto const other_pid = get_value<int>(
params,
"pid");
679 auto &cell_structure = cell_structure_si->get_cell_structure();
680 context()->parallel_try_catch(
681 [&]() { particle_exclusion_sanity_checks(m_pid, other_pid); });
684 }
else if (name ==
"set_exclusions") {
685 std::vector<int> exclusion_list;
687 auto const pid = get_value<int>(
params,
"p_ids");
688 exclusion_list.push_back(pid);
690 exclusion_list = get_value<std::vector<int>>(
params,
"p_ids");
692 context()->parallel_try_catch([&]() {
693 for (
auto const pid : exclusion_list) {
694 particle_exclusion_sanity_checks(m_pid, pid);
697 set_particle_property([
this, &exclusion_list](
Particle &p) {
699 auto &cell_structure = cell_structure_si->get_cell_structure();
703 for (
auto const pid : exclusion_list) {
709 }
else if (name ==
"get_exclusions") {
710 if (not context()->is_head_node()) {
714 return Variant{std::vector<int>{excl_list.begin(), excl_list.end()}};
718 if (name ==
"rotate_particle") {
720 auto const axis = get_value<Utils::Vector3d>(
params,
"axis");
721 auto const angle = get_value<double>(
params,
"angle");
725 if (name ==
"convert_vector_body_to_space") {
726 return get_particle_property<std::vector<double>>(
728 auto const vec = get_value<Utils::Vector3d>(
params,
"vec");
732 if (name ==
"convert_vector_space_to_body") {
733 return get_particle_property<std::vector<double>>(
735 auto const vec = get_value<Utils::Vector3d>(
params,
"vec");
745 std::array<std::string, 3>>{{
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."}},
751 "Setting 'dip' would overwrite 'quat'. Set 'quat' and 'dipm' instead."}},
753 "Setting 'dip' would overwrite 'director'. Set 'director' and "
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")
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");
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;
779 if (
params.contains(
"__bonded_ias")) {
780 m_bonded_ias = get_value<std::shared_ptr<Interactions::BondedInteractions>>(
785 if (n_extra_args == 0) {
789 auto const pos = get_value<Utils::Vector3d>(
params,
"pos");
790 context()->parallel_try_catch([&]() {
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) +
802 context()->parallel_try_catch([&]() {
805 if (not
params.contains(
"__cpt_sentinel")) {
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);
822 context()->parallel_try_catch([&]() {
825 std::set<std::string>
const skip = {
826 "pos_folded",
"pos",
"id",
"exclusions",
"node",
"image_box",
"bonds",
827 "lees_edwards_flag",
"__cpt_sentinel",
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));
835 if (not
params.contains(
"type")) {
836 do_set_parameter(
"type", 0);
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.
Immutable view on a bond.
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
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.
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
auto make_vector_of_variants(std::vector< T > const &v)
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.
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)
std::pair< Utils::Quaternion< double >, double > convert_dip_to_quat(const Utils::Vector3d &dip)
convert a dipole moment to quaternions and dipolar strength
Utils::Vector3d convert_vector_space_to_body(const Particle &p, const Utils::Vector3d &v)
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.
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.
bool swimming
Is the particle a swimmer.
The following properties define, with respect to which real particle a virtual site is placed and at ...
Struct holding all information for one particle.
auto const & dip_fld() const
bool has_exclusion(int pid) const
auto const & swimming() const
auto const & lees_edwards_offset() const
auto const & propagation() const
auto const & rinertia() const
auto const & mass() const
Utils::compact_vector< int > & exclusions()
auto const & quat() const
auto const & rotation() const
auto const & vs_relative() const
auto const & gamma() const
auto const & gamma_rot() const
auto const & lees_edwards_flag() const
auto const & torque() const
auto const & fixed() const
auto const & ext_force() const
auto const & omega() const
auto const & type() const
auto const & ext_torque() const
auto const & bonds() const
auto const & dipm() const
auto const & mol_id() const
auto const & mu_E() const
auto const & force() const
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.