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>
71#ifdef ESPRESSO_ROTATION
72static std::array<std::array<std::string_view, 3>,
75 "Setting 'dip' is sufficient as it defines the scalar dipole moment."}},
77 "Setting 'quat' is sufficient as it defines the director."}},
79 "Setting 'dip' would overwrite 'quat'. Set 'quat' and 'dipm' instead."}},
81 "Setting 'dip' would overwrite 'director'. Set 'director' and "
88 if (not params.contains(
"__cpt_sentinel")) {
90 boost::format(
"Contradicting particle attributes: '%s' and '%s'. %s");
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);
102#if defined(ESPRESSO_ROTATION) or defined(ESPRESSO_EXTERNAL_FORCES)
104 auto bitfield =
static_cast<uint8_t
>(0u);
106 bitfield |=
static_cast<uint8_t
>(1u);
108 bitfield |=
static_cast<uint8_t
>(2u);
110 bitfield |=
static_cast<uint8_t
>(4u);
115#ifdef ESPRESSO_ROTATION
122 if (q.norm2() == 0.) {
123 throw std::domain_error(
error_msg(name,
"must be non-zero"));
129#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
131#ifdef ESPRESSO_PARTICLE_ANISOTROPY
143template <
typename T,
class F>
144T ParticleHandle::get_particle_property(F
const &fun)
const {
147 auto const ptr =
const_cast<Particle const *
>(
149 std::optional<T> ret;
150 if (ptr ==
nullptr) {
159T ParticleHandle::get_particle_property(T
const &(Particle::*getter)()
161 return get_particle_property<T>(
162 [getter](Particle
const &p) {
return (p.*getter)(); });
166void ParticleHandle::set_particle_property(F
const &fun)
const {
170 if (ptr !=
nullptr) {
173 get_system()->on_particle_change();
177void ParticleHandle::set_particle_property(T &(Particle::*setter)(),
179 set_particle_property(
180 [&value, setter](Particle &p) { (p.*setter)() =
get_value<T>(value); });
183#ifdef ESPRESSO_EXCLUSIONS
184void ParticleHandle::set_exclusions(
Variant const &value) {
185 std::vector<int> exclusion_list;
193 for (
auto const pid : exclusion_list) {
198 set_particle_property([&](Particle
const &p) {
202 for (
auto const pid : exclusion_list) {
223 throw std::domain_error(
224 error_msg(
"type",
"must be an integer >= 0"));
226 get_system()->nonbonded_ias->make_particle_type_exist(new_type);
238 auto const pos = p.
pos();
240 return get_system()->box_geo->unfolded_position(pos, image_box);
256 throw std::domain_error(
error_msg(
"mass",
"must be a float > 0"));
264 throw std::runtime_error(
"Feature MASS not compiled in");
270#ifdef ESPRESSO_ELECTROSTATICS
277 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
282#ifdef ESPRESSO_DIPOLES
285 set_particle_property([&value](
Particle &p) {
297#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
304#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
307 set_particle_property([&value](
Particle &p) {
309 if (dict.contains(
"is_enabled"))
312 if (dict.contains(
"sw_phi_0"))
315 if (dict.contains(
"sat_mag"))
318 if (dict.contains(
"anisotropy_field_inv"))
321 if (dict.contains(
"anisotropy_energy"))
324 if (dict.contains(
"sw_tau0_inv"))
327 if (dict.contains(
"sw_dt_incr"))
345#ifdef ESPRESSO_ROTATION
348 set_particle_property([&value](
Particle &p) {
360 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
370 set_particle_property([&value](
Particle &p) {
371 auto const rotation_flag =
379 ::detail::get_nth_bit(rotation_bits, 1u),
380 ::detail::get_nth_bit(rotation_bits, 2u)}};
384 set_particle_property([&value](
Particle &p) {
399#ifdef ESPRESSO_ROTATIONAL_INERTIA
406#ifdef ESPRESSO_LB_ELECTROHYDRODYNAMICS
413#ifdef ESPRESSO_EXTERNAL_FORCES
416 set_particle_property([&value](
Particle &p) {
417 auto const fix_flag =
425 ::detail::get_nth_bit(fixed, 1u),
426 ::detail::get_nth_bit(fixed, 2u)}};
433#ifdef ESPRESSO_ROTATION
441#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
448#ifdef ESPRESSO_ROTATION
459 auto const &box_geo = *get_system()->box_geo;
462 {
"lees_edwards_offset",
471 auto const &box_geo = *get_system()->box_geo;
473 return box_geo.folded_image_box(p.
pos(), p.
image_box());
483 throw std::domain_error(
484 error_msg(
"mol_id",
"must be an integer >= 0"));
489#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
493 set_particle_property(
504 if (array.size() != 3) {
513 "vs_relative",
"must take the form [id, distance, quaternion]"));
515 set_particle_property(
520 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
529 "propagation",
"propagation combination not accepted: " +
535#ifdef ESPRESSO_ENGINE
538 set_particle_property([&value](
Particle &p) {
542 if (dict.contains(
"f_swim")) {
545 if (dict.contains(
"is_engine_force_on_fluid")) {
546 auto const is_engine_force_on_fluid =
556 {
"f_swim", swim.f_swim},
557 {
"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
566 if (
name ==
"set_param_parallel") {
568 if (not params.contains(
"value")) {
569 throw Exception(
"Parameter '" + param_name +
"' is missing.");
571 auto const &value = params.at(
"value");
576 if (
name ==
"update_params") {
579#ifdef ESPRESSO_ROTATION
583 if (params.contains(
name) and
name !=
"bonds") {
590 if (params.contains(
"bonds_ids")) {
592 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
595 auto const bonds_partner_ids =
597 for (std::size_t i = 0; i < bonds_ids.size(); i += 1) {
598 std::vector<int> particle_ids = {m_pid};
599 std::ranges::copy(bonds_partner_ids[i],
600 std::back_inserter(particle_ids));
601 ::add_bond(*get_system(), bonds_ids[i], particle_ids);
602 get_system()->on_particle_change();
605#ifdef ESPRESSO_EXCLUSIONS
607 if (params.contains(
"exclusions")) {
608 set_exclusions(params.at(
"exclusions"));
613 if (
name ==
"get_bond_by_id") {
614 if (not
context()->is_head_node()) {
617 return get_bonded_ias()->call_method(
"get_bond", params);
619 if (
name ==
"get_bonds_view") {
620 if (not
context()->is_head_node()) {
624 std::vector<std::vector<Variant>> bonds_flat;
625 for (
auto const &&bond_view : bond_list) {
626 std::vector<Variant> bond_flat;
627 bond_flat.emplace_back(bond_view.bond_id());
628 for (
auto const pid : bond_view.partner_ids()) {
629 bond_flat.emplace_back(pid);
631 bonds_flat.emplace_back(std::move(bond_flat));
635 if (
name ==
"add_bond") {
638 std::vector<int> particle_ids = {m_pid};
639 std::ranges::copy(partner_ids, std::back_inserter(particle_ids));
640 ::add_bond(*get_system(), bond_id, particle_ids);
641 get_system()->on_particle_change();
642 }
else if (
name ==
"del_bond") {
643 set_particle_property([¶ms](
Particle &p) {
646 auto const bond_view =
647 BondView(bond_id, {part_id.data(), part_id.size()});
648 auto &bond_list = p.
bonds();
649 auto it = std::find(bond_list.begin(), bond_list.end(), bond_view);
650 if (it != bond_list.end()) {
654 }
else if (
name ==
"delete_all_bonds") {
655 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
656 }
else if (
name ==
"is_valid_bond_id") {
658 return get_system()->bonded_ias->get_zero_based_type(bond_id) != 0;
660 if (
name ==
"remove_particle") {
667 }
else if (
name ==
"is_virtual") {
668 if (not
context()->is_head_node()) {
672#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
673 }
else if (
name ==
"vs_auto_relate_to") {
674 if (not
context()->is_head_node()) {
678 auto const override_cutoff_check =
680 if (m_pid == other_pid) {
681 throw std::invalid_argument(
"A virtual site cannot relate to itself");
684 throw std::domain_error(
"Invalid particle id: " +
685 std::to_string(other_pid));
687 auto const system = get_system();
700 p_current, p_relate_to, *system->box_geo, system->get_min_global_cut(),
701 override_cutoff_check);
708#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
709 }
else if (
name ==
"vs_com_relate_to") {
713 if (not
context()->is_head_node()) {
717 throw std::domain_error(
"Invalid molecule id: " + std::to_string(molid));
719 if (maybe_exists_vs) {
720 throw std::runtime_error(
721 "Molecule id: " + std::to_string(molid) +
722 " is already tracked by virtual site with particle id: " +
723 std::to_string(*maybe_exists_vs));
730#ifdef ESPRESSO_EXCLUSIONS
731 }
else if (
name ==
"has_exclusion") {
740 if (
name ==
"add_exclusion") {
748 get_system()->on_particle_change();
749 }
else if (
name ==
"del_exclusion") {
757 get_system()->on_particle_change();
758#ifdef ESPRESSO_EXCLUSIONS
759 }
else if (
name ==
"set_exclusions") {
760 set_exclusions(params.at(
"p_ids"));
762 }
else if (
name ==
"get_exclusions") {
763 if (not
context()->is_head_node()) {
767 return Variant{std::vector<int>{excl_list.begin(), excl_list.end()}};
769#ifdef ESPRESSO_ROTATION
771 if (
name ==
"rotate_particle") {
772 set_particle_property([¶ms](
Particle &p) {
778 if (
name ==
"convert_vector_body_to_space") {
779 return get_particle_property<std::vector<double>>(
785 if (
name ==
"convert_vector_space_to_body") {
786 return get_particle_property<std::vector<double>>(
796std::size_t ParticleHandle::setup_hidden_args(
VariantMap const ¶ms) {
797 auto n_extra_args = params.size() - params.count(
"id");
798 if (params.contains(
"__cell_structure")) {
800 params,
"__cell_structure");
801 so->configure(*
this);
802 m_cell_structure = so;
805 if (params.contains(
"__bonded_ias")) {
807 params,
"__bonded_ias");
814 auto const n_extra_args = setup_hidden_args(params);
819 if (not params.contains(
"id")) {
820 auto head_node_reference = m_pid;
821 boost::mpi::broadcast(
context()->get_comm(), head_node_reference, 0);
822 assert(m_pid == head_node_reference &&
"global max_seen_pid has diverged");
827 if (n_extra_args == 0) {
835 auto ptr = cell_structure.get_local_particle(m_pid);
836 if (ptr !=
nullptr) {
837 throw std::invalid_argument(
"Particle " + std::to_string(m_pid) +
842#ifdef ESPRESSO_ROTATION
853 std::set<std::string_view>
const skip = {
854 "pos_folded",
"pos",
"id",
"node",
"image_box",
"bonds",
855#ifdef ESPRESSO_EXCLUSIONS
858 "lees_edwards_flag",
"__cpt_sentinel",
862 if (params.contains(
name) and not skip.contains(
name)) {
866 for (
auto const &
name : params | std::views::keys) {
867 if (not skip.contains(
name) and not
name.starts_with(
'_') and
869 auto error_msg =
"Unknown parameter '" +
name +
"' for particle.";
870 std::string hint =
"Hint: a feature is probably not compiled in.";
871 throw std::invalid_argument(
error_msg +
" " + hint);
874 if (not params.contains(
"type")) {
877#ifdef ESPRESSO_EXCLUSIONS
878 if (params.contains(
"exclusions")) {
879 do_call_method(
"set_exclusions", {{
"p_ids", params.at(
"exclusions")}});
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.
bool has_parameter(std::string const &name) const override
void do_set_parameter(const std::string &name, const Variant &value) final
void add_parameters(std::vector< AutoParameter > &¶ms)
auto const & get_parameter_insertion_order() const
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual boost::mpi::communicator const & get_comm() const =0
Context * context() const
Responsible context.
void set_parameter(const std::string &name, const Variant &value)
Set single parameter.
std::string_view name() const
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
void do_construct(VariantMap const ¶ms) override
static DEVICE_QUALIFIER constexpr Vector< double, N > broadcast(typename Base::value_type const &value) noexcept
std::optional< int > get_pid_for_vs_com(CellStructure &cell_structure, int mol_id)
@ TRANS_VS_CENTER_OF_MASS
static uint8_t bitfield_from_flag(Utils::Vector3i const &flag)
static void sanity_checks_rotation(VariantMap const ¶ms)
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.
void particle_checks(int p_id, Utils::Vector3d const &pos)
auto error_msg(std::string const &name, std::string const &reason)
static std::array< std::array< std::string_view, 3 >, 4 > constexpr contradicting_arguments_quat
void local_add_exclusion(int pid1, int pid2, ::CellStructure &cell_structure)
Locally add an exclusion to a particle.
constexpr bool is_type(Variant const &v)
Check is a Variant holds a specific type.
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
auto make_vector_of_variants(std::vector< T > const &v)
std::unordered_map< std::string, Variant > VariantMap
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
constexpr Vector< T, 3 > convert_quaternion_to_director(Quaternion< T > const &quat)
Convert quaternion to director.
Quaternion< T > convert_director_to_quaternion(Vector< T, 3 > const &d)
Convert director to quaternion.
Utils::Vector< bool, 3 > Vector3b
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()
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.
auto convert_vector_space_to_body(Utils::Quaternion< double > const &quat, Utils::Vector3d const &vec)
std::pair< Utils::Quaternion< double >, double > convert_dip_to_quat(const Utils::Vector3d &dip)
convert a dipole moment to quaternions and dipolar strength
auto convert_vector_body_to_space(Utils::Quaternion< double > const &quat, Utils::Vector3d const &vec)
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.
Properties of a self-propelled particle.
bool swimming
Is the particle a swimmer.
bool is_engine_force_on_fluid
Whether f_swim is applied to the particle or to the fluid.
double f_swim
Imposed constant force.
The following properties define, with respect to which real particle a virtual site is placed and at ...
Utils::Quaternion< double > rel_orientation
Relative position of the virtual site.
Struct holding all information for one particle.
constexpr auto const & dip_fld() const
bool has_exclusion(int pid) const
constexpr auto const & bonds() const
constexpr auto const & magnetic_anisotropy_field_inv() const
constexpr auto const & stoner_wohlfarth_is_enabled() const
constexpr auto const & quat() const
constexpr auto calc_dip() const
constexpr auto const & pos() const
constexpr auto const & swimming() const
constexpr auto const & rinertia() const
constexpr auto const & mass() const
constexpr auto const & dipm() const
Utils::compact_vector< int > & exclusions()
constexpr auto const & type() const
constexpr auto const & omega() const
constexpr auto const & saturation_magnetization() const
constexpr auto const & stoner_wohlfarth_dt_incr() const
constexpr auto const & magnetic_anisotropy_energy() const
constexpr auto const & ext_force() const
constexpr auto const & propagation() const
constexpr auto const & ext_torque() const
constexpr auto const & rotation() const
constexpr auto const & force() const
constexpr auto is_virtual() const
constexpr auto const & vs_relative() const
constexpr auto const & fixed() const
constexpr auto const & gamma() const
constexpr auto const & gamma_rot() const
constexpr auto const & image_box() const
constexpr auto const & mu_E() const
constexpr auto const & stoner_wohlfarth_tau0_inv() const
constexpr auto const & mol_id() const
constexpr auto const & q() const
constexpr auto const & stoner_wohlfarth_phi_0() const
constexpr auto const & v() const
constexpr auto const & torque() const
constexpr auto const & lees_edwards_flag() const
constexpr auto const & lees_edwards_offset() const
static constexpr const ReadOnly read_only
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.