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>
69 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
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");
80 auto bitfield =
static_cast<uint8_t
>(0u);
82 bitfield |=
static_cast<uint8_t
>(1u);
84 bitfield |=
static_cast<uint8_t
>(2u);
86 bitfield |=
static_cast<uint8_t
>(4u);
90static auto error_msg(std::string
const &name, std::string
const &reason) {
91 std::stringstream msg;
92 msg <<
"attribute '" << name <<
"' of 'ParticleHandle' " << reason;
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"));
108#ifdef THERMOSTAT_PER_PARTICLE
110#ifdef PARTICLE_ANISOTROPY
114 return get_value<Utils::Vector3d>(value);
117 return get_value<double>(value);
125 throw std::domain_error(
"Invalid particle id: " + std::to_string(p_id));
128 if (ptr !=
nullptr and ptr->is_ghost()) {
131 auto const n_found = boost::mpi::all_reduce(
132 comm,
static_cast<int>(ptr !=
nullptr), std::plus<>());
134 throw std::runtime_error(
"Particle with id " + std::to_string(p_id) +
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();
145 auto const ptr =
const_cast<Particle const *
>(
147 std::optional<T> ret;
148 if (ptr ==
nullptr) {
157T ParticleHandle::get_particle_property(T
const &(
Particle::*getter)()
159 return get_particle_property<T>(
160 [getter](
Particle const &p) {
return (p.*getter)(); });
164void ParticleHandle::set_particle_property(F
const &fun)
const {
166 auto &cell_structure = cell_structure_si->get_cell_structure();
167 auto const &comm = context()->get_comm();
169 if (ptr !=
nullptr) {
176void ParticleHandle::set_particle_property(T &(
Particle::*setter)(),
178 set_particle_property(
179 [&value, setter](
Particle &p) { (p.*setter)() = get_value<T>(value); });
182ParticleHandle::ParticleHandle() {
189 {
"id", AutoParameter::read_only, [
this]() {
return m_pid; }},
193 auto const new_type = get_value<int>(value);
195 throw std::domain_error(
196 error_msg(
"type",
"must be an integer >= 0"));
198 get_system()->
nonbonded_ias->make_particle_type_exist(new_type);
205 auto const pos = get_value<Utils::Vector3d>(value);
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);
228 if (get_value<double>(value) <= 0.) {
229 throw std::domain_error(
error_msg(
"mass",
"must be a float > 0"));
236 if (std::abs(get_value<double>(value) - default_mass) > 1e-10) {
237 throw std::runtime_error(
"Feature MASS not compiled in");
249 if (get_value<double>(value) != 0.) {
250 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
258 set_particle_property([&value](
Particle &p) {
259 auto const dip = get_value<Utils::Vector3d>(value);
270#ifdef DIPOLE_FIELD_TRACKING
280 set_particle_property([&value](
Particle &p) {
281 auto const director = get_value<Utils::Vector3d>(value).normalized();
292 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
302 set_particle_property([&value](
Particle &p) {
303 auto const rotation_flag =
311 ::detail::get_nth_bit(rotation_bits, 1),
312 ::detail::get_nth_bit(rotation_bits, 2)}};
316 set_particle_property([&value](
Particle &p) {
317 auto const omega = get_value<Utils::Vector3d>(value);
327 set_particle_property([&value](
Particle &p) {
328 auto const torque = get_value<Utils::Vector3d>(value);
337#ifdef ROTATIONAL_INERTIA
344#ifdef LB_ELECTROHYDRODYNAMICS
351#ifdef EXTERNAL_FORCES
354 set_particle_property([&value](
Particle &p) {
355 auto const fix_flag =
363 ::detail::get_nth_bit(fixed, 1),
364 ::detail::get_nth_bit(fixed, 2)}};
379#ifdef THERMOSTAT_PER_PARTICLE
395 {
"pos_folded", AutoParameter::read_only,
397 auto const &box_geo = *get_system()->
box_geo;
400 {
"lees_edwards_offset",
405 {
"lees_edwards_flag", AutoParameter::read_only,
407 {
"image_box", AutoParameter::read_only,
409 auto const &box_geo = *get_system()->
box_geo;
411 return box_geo.folded_image_box(p.pos(), p.image_box());
413 {
"node", AutoParameter::read_only,
419 auto const mol_id = get_value<int>(value);
421 throw std::domain_error(
422 error_msg(
"mol_id",
"must be an integer >= 0"));
427#ifdef VIRTUAL_SITES_RELATIVE
431 set_particle_property(
441 auto const array = get_value<std::vector<Variant>>(value);
442 if (array.size() != 3) {
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 =
451 "vs_relative",
"must take the form [id, distance, quaternion]"));
453 set_particle_property(
458 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
464 auto const propagation = get_value<int>(value);
467 "propagation",
"propagation combination not accepted: " +
476 set_particle_property([&value](
Particle &p) {
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"));
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;
494 {
"f_swim", swim.f_swim},
495 {
"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
535void ParticleHandle::particle_exclusion_sanity_checks(
int pid1,
538 throw std::runtime_error(
"Particles cannot exclude themselves (id " +
539 std::to_string(pid1) +
")");
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);
548Variant ParticleHandle::do_call_method(std::string
const &name,
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.");
555 auto const &value =
params.at(
"value");
556 context()->parallel_try_catch(
557 [&]() { do_set_parameter(param_name, value); });
560 if (name ==
"get_bond_by_id") {
561 if (not context()->is_head_node()) {
564 return get_bonded_ias()->call_method(
"get_bond",
params);
566 if (name ==
"get_bonds_view") {
567 if (not context()->is_head_node()) {
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);
578 bonds_flat.emplace_back(std::move(bond_flat));
582 if (name ==
"add_bond") {
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);
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.
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)
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)
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.