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>
70#ifdef ESPRESSO_ROTATION
72 std::array<std::string_view, 3>>({
74 "Setting 'dip' is sufficient as it defines the scalar dipole moment."},
76 "Setting 'quat' is sufficient as it defines the director."},
78 "Setting 'dip' would overwrite 'quat'. Set 'quat' and 'dipm' instead."},
80 "Setting 'dip' would overwrite 'director'. Set 'director' and "
87 if (
not params.contains(
"__cpt_sentinel")) {
89 boost::format(
"Contradicting particle attributes: '%s' and '%s'. %s");
91 if (params.contains(std::string{prop1})
and
92 params.contains(std::string{
prop2})) {
94 throw std::invalid_argument(
err_msg);
101#if defined(ESPRESSO_ROTATION) or defined(ESPRESSO_EXTERNAL_FORCES)
114#ifdef ESPRESSO_ROTATION
121 if (q.norm2() == 0.) {
122 throw std::domain_error(
error_msg(name,
"must be non-zero"));
128#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
130#ifdef ESPRESSO_PARTICLE_ANISOTROPY
142template <
typename T,
class F>
143T ParticleHandle::get_particle_property(
F const &
fun)
const {
144 auto &cell_structure = get_cell_structure()->get_cell_structure();
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 const &comm = context()->get_comm();
169 if (ptr !=
nullptr) {
176void ParticleHandle::set_particle_property(T &(
Particle::*setter)(),
178 set_particle_property(
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");
243#ifdef ESPRESSO_ELECTROSTATICS
249 if (get_value<double>(value) != 0.) {
250 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
255#ifdef ESPRESSO_DIPOLES
258 set_particle_property([&value](
Particle &p) {
259 auto const dip = get_value<Utils::Vector3d>(value);
270#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
277#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
280 set_particle_property([&value](
Particle &p) {
281 auto const dict = get_value<VariantMap>(value);
282 if (dict.contains(
"is_enabled"))
284 get_value<bool>(dict.at(
"is_enabled"));
285 if (dict.contains(
"sw_phi_0"))
287 get_value<double>(dict.at(
"sw_phi_0"));
288 if (dict.contains(
"sat_mag"))
290 get_value<double>(dict.at(
"sat_mag"));
291 if (dict.contains(
"anisotropy_field_inv"))
293 get_value<double>(dict.at(
"anisotropy_field_inv"));
294 if (dict.contains(
"anisotropy_energy"))
296 get_value<double>(dict.at(
"anisotropy_energy"));
297 if (dict.contains(
"sw_tau0_inv"))
299 get_value<double>(dict.at(
"sw_tau0_inv"));
300 if (dict.contains(
"sw_dt_incr"))
302 get_value<double>(dict.at(
"sw_dt_incr"));
308 {
"is_enabled", p.stoner_wohlfarth_is_enabled()},
309 {
"sw_phi_0", p.stoner_wohlfarth_phi_0()},
310 {
"sat_mag", p.saturation_magnetization()},
311 {
"anisotropy_field_inv", p.magnetic_anisotropy_field_inv()},
312 {
"anisotropy_energy", p.magnetic_anisotropy_energy()},
313 {
"sw_tau0_inv", p.stoner_wohlfarth_tau0_inv()},
314 {
"sw_dt_incr", p.stoner_wohlfarth_dt_incr()},
318#ifdef ESPRESSO_ROTATION
321 set_particle_property([&value](
Particle &p) {
322 auto const director = get_value<Utils::Vector3d>(value).normalized();
333 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
343 set_particle_property([&value](
Particle &p) {
344 auto const rotation_flag =
352 ::detail::get_nth_bit(rotation_bits, 1),
353 ::detail::get_nth_bit(rotation_bits, 2)}};
357 set_particle_property([&value](
Particle &p) {
358 auto const omega = get_value<Utils::Vector3d>(value);
368 set_particle_property([&value](
Particle &p) {
369 auto const torque = get_value<Utils::Vector3d>(value);
378#ifdef ESPRESSO_ROTATIONAL_INERTIA
385#ifdef ESPRESSO_LB_ELECTROHYDRODYNAMICS
392#ifdef ESPRESSO_EXTERNAL_FORCES
395 set_particle_property([&value](
Particle &p) {
396 auto const fix_flag =
404 ::detail::get_nth_bit(fixed, 1),
405 ::detail::get_nth_bit(fixed, 2)}};
412#ifdef ESPRESSO_ROTATION
420#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
427#ifdef ESPRESSO_ROTATION
436 {
"pos_folded", AutoParameter::read_only,
438 auto const &box_geo = *get_system()->
box_geo;
441 {
"lees_edwards_offset",
446 {
"lees_edwards_flag", AutoParameter::read_only,
448 {
"image_box", AutoParameter::read_only,
450 auto const &box_geo = *get_system()->
box_geo;
452 return box_geo.folded_image_box(p.pos(), p.image_box());
454 {
"node", AutoParameter::read_only,
460 auto const mol_id = get_value<int>(value);
462 throw std::domain_error(
463 error_msg(
"mol_id",
"must be an integer >= 0"));
468#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
472 set_particle_property(
482 auto const array = get_value<std::vector<Variant>>(value);
483 if (array.size() != 3) {
486 vs_relative.
distance = get_value<double>(array[1]);
487 vs_relative.to_particle_id = get_value<int>(array[0]);
488 vs_relative.rel_orientation =
492 "vs_relative",
"must take the form [id, distance, quaternion]"));
494 set_particle_property(
499 return std::vector<Variant>{{vs_rel.to_particle_id, vs_rel.distance,
505 auto const propagation = get_value<int>(value);
508 "propagation",
"propagation combination not accepted: " +
514#ifdef ESPRESSO_ENGINE
517 set_particle_property([&value](
Particle &p) {
520 auto const dict = get_value<VariantMap>(value);
521 if (dict.contains(
"f_swim")) {
522 swim.f_swim = get_value<double>(dict.at(
"f_swim"));
524 if (dict.contains(
"is_engine_force_on_fluid")) {
525 auto const is_engine_force_on_fluid =
526 get_value<bool>(dict.at(
"is_engine_force_on_fluid"));
527 swim.is_engine_force_on_fluid = is_engine_force_on_fluid;
535 {
"f_swim", swim.f_swim},
536 {
"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
543Variant ParticleHandle::do_call_method(std::string
const &name,
545 if (name ==
"set_param_parallel") {
546 auto const param_name = get_value<std::string>(params,
"name");
547 if (not params.contains(
"value")) {
548 throw Exception(
"Parameter '" + param_name +
"' is missing.");
550 auto const &value = params.at(
"value");
551 context()->parallel_try_catch(
552 [&]() { do_set_parameter(param_name, value); });
555 if (name ==
"update_params") {
557 context()->parallel_try_catch([&]() {
558#ifdef ESPRESSO_ROTATION
561 for (
auto const &name : get_parameter_insertion_order()) {
562 if (params.contains(name) and name !=
"bonds") {
563 do_set_parameter(name, params.at(name));
569 if (params.contains(
"bonds_ids")) {
571 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
573 auto const bonds_ids = get_value<std::vector<int>>(params,
"bonds_ids");
574 auto const bonds_partner_ids =
575 get_value<std::vector<std::vector<int>>>(params,
"bonds_parts");
576 for (std::size_t i = 0; i < bonds_ids.size(); i += 1) {
577 std::vector<int> particle_ids = {m_pid};
578 std::ranges::copy(bonds_partner_ids[i],
579 std::back_inserter(particle_ids));
580 ::add_bond(*get_system(), bonds_ids[i], particle_ids);
584#ifdef ESPRESSO_EXCLUSIONS
586 if (params.contains(
"exclusions")) {
587 std::vector<int> exclusion_list;
588 if (is_type<int>(params.at(
"exclusions"))) {
589 exclusion_list.emplace_back(get_value<int>(params,
"exclusions"));
591 exclusion_list = get_value<std::vector<int>>(params,
"exclusions");
593 context()->parallel_try_catch([&]() {
595 for (
auto const pid : exclusion_list) {
597 context()->get_comm());
600 set_particle_property([
this, &exclusion_list](
Particle &p) {
605 for (
auto const pid : exclusion_list) {
614 if (name ==
"get_bond_by_id") {
615 if (not context()->is_head_node()) {
618 return get_bonded_ias()->call_method(
"get_bond", params);
620 if (name ==
"get_bonds_view") {
621 if (not context()->is_head_node()) {
625 std::vector<std::vector<Variant>> bonds_flat;
626 for (
auto const &&bond_view : bond_list) {
627 std::vector<Variant> bond_flat;
628 bond_flat.emplace_back(bond_view.bond_id());
629 for (
auto const pid : bond_view.partner_ids()) {
630 bond_flat.emplace_back(pid);
632 bonds_flat.emplace_back(std::move(bond_flat));
636 if (name ==
"add_bond") {
637 auto const bond_id = get_value<int>(params,
"bond_id");
638 auto const partner_ids = get_value<std::vector<int>>(params,
"part_id");
639 std::vector<int> particle_ids = {m_pid};
640 std::ranges::copy(partner_ids, std::back_inserter(particle_ids));
641 ::add_bond(*get_system(), bond_id, particle_ids);
643 }
else if (name ==
"del_bond") {
644 set_particle_property([¶ms](
Particle &p) {
645 auto const bond_id = get_value<int>(params,
"bond_id");
646 auto const part_id = get_value<std::vector<int>>(params,
"part_id");
647 auto const bond_view =
648 BondView(bond_id, {part_id.data(), part_id.size()});
649 auto &bond_list = p.
bonds();
650 auto it = std::find(bond_list.begin(), bond_list.end(), bond_view);
651 if (it != bond_list.end()) {
655 }
else if (name ==
"delete_all_bonds") {
656 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
657 }
else if (name ==
"is_valid_bond_id") {
658 auto const bond_id = get_value<int>(params,
"bond_id");
659 return get_system()->
bonded_ias->get_zero_based_type(bond_id) != 0;
661 if (name ==
"remove_particle") {
662 context()->parallel_try_catch([&]() {
668 }
else if (name ==
"is_virtual") {
669 if (not context()->is_head_node()) {
673#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
674 }
else if (name ==
"vs_auto_relate_to") {
675 if (not context()->is_head_node()) {
678 auto const other_pid = get_value<int>(params,
"pid");
679 auto const override_cutoff_check =
680 get_value<bool>(params,
"override_cutoff_check");
681 if (m_pid == other_pid) {
682 throw std::invalid_argument(
"A virtual site cannot relate to itself");
685 throw std::domain_error(
"Invalid particle id: " +
686 std::to_string(other_pid));
688 auto const system = get_system();
701 p_current, p_relate_to, *system->box_geo, system->get_min_global_cut(),
702 override_cutoff_check);
703 set_parameter(
"vs_relative",
Variant{std::vector<Variant>{
705 set_parameter(
"propagation",
709#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
710 }
else if (name ==
"vs_com_relate_to") {
712 auto const molid = get_value<int>(params,
"molid");
714 if (not context()->is_head_node()) {
718 throw std::domain_error(
"Invalid molecule id: " + std::to_string(molid));
720 if (maybe_exists_vs) {
721 throw std::runtime_error(
722 "Molecule id: " + std::to_string(molid) +
723 " is already tracked by virtual site with particle id: " +
724 std::to_string(*maybe_exists_vs));
726 set_parameter(
"mol_id", params.at(
"molid"));
731#ifdef ESPRESSO_EXCLUSIONS
732 }
else if (name ==
"has_exclusion") {
733 auto const other_pid = get_value<int>(params,
"pid");
738 return p->has_exclusion(other_pid);
741 if (name ==
"add_exclusion") {
742 auto const other_pid = get_value<int>(params,
"pid");
744 context()->parallel_try_catch([&]() {
746 context()->get_comm());
750 }
else if (name ==
"del_exclusion") {
751 auto const other_pid = get_value<int>(params,
"pid");
753 context()->parallel_try_catch([&]() {
755 context()->get_comm());
759 }
else if (name ==
"set_exclusions") {
761 std::vector<int> exclusion_list;
763 auto const pid = get_value<int>(params,
"p_ids");
764 exclusion_list.push_back(pid);
766 exclusion_list = get_value<std::vector<int>>(params,
"p_ids");
768 context()->parallel_try_catch([&]() {
769 for (
auto const pid : exclusion_list) {
771 context()->get_comm());
774 set_particle_property([
this, &exclusion_list](
Particle &p) {
779 for (
auto const pid : exclusion_list) {
785 }
else if (name ==
"get_exclusions") {
786 if (not context()->is_head_node()) {
790 return Variant{std::vector<int>{excl_list.begin(), excl_list.end()}};
792#ifdef ESPRESSO_ROTATION
794 if (name ==
"rotate_particle") {
795 set_particle_property([¶ms](
Particle &p) {
796 auto const axis = get_value<Utils::Vector3d>(params,
"axis");
797 auto const angle = get_value<double>(params,
"angle");
801 if (name ==
"convert_vector_body_to_space") {
802 return get_particle_property<std::vector<double>>(
804 auto const vec = get_value<Utils::Vector3d>(params,
"vec");
808 if (name ==
"convert_vector_space_to_body") {
809 return get_particle_property<std::vector<double>>(
811 auto const vec = get_value<Utils::Vector3d>(params,
"vec");
819std::size_t ParticleHandle::setup_hidden_args(
VariantMap const ¶ms) {
820 auto n_extra_args = params.size() - params.count(
"id");
821 if (params.contains(
"__cell_structure")) {
822 auto so = get_value<std::shared_ptr<CellSystem::CellSystem>>(
823 params,
"__cell_structure");
824 so->configure(*
this);
825 m_cell_structure = so;
828 if (params.contains(
"__bonded_ias")) {
829 m_bonded_ias = get_value<std::shared_ptr<Interactions::BondedInteractions>>(
830 params,
"__bonded_ias");
837 auto const n_extra_args = setup_hidden_args(params);
838 m_pid = (params.contains(
"id")) ? get_value<int>(params,
"id")
842 if (not params.contains(
"id")) {
843 auto head_node_reference = m_pid;
844 boost::mpi::broadcast(context()->get_comm(), head_node_reference, 0);
845 assert(m_pid == head_node_reference &&
"global max_seen_pid has diverged");
850 if (n_extra_args == 0) {
854 auto const pos = get_value<Utils::Vector3d>(params,
"pos");
855 context()->parallel_try_catch([&]() {
858 auto ptr = cell_structure.get_local_particle(m_pid);
859 if (ptr !=
nullptr) {
860 throw std::invalid_argument(
"Particle " + std::to_string(m_pid) +
865#ifdef ESPRESSO_ROTATION
873 context()->parallel_try_catch([&]() {
876 std::set<std::string_view>
const skip = {
877 "pos_folded",
"pos",
"id",
"exclusions",
"node",
"image_box",
"bonds",
878 "lees_edwards_flag",
"__cpt_sentinel",
881 for (
auto const &name : get_parameter_insertion_order()) {
882 if (params.contains(name) and not skip.contains(name)) {
883 do_set_parameter(name, params.at(name));
886 if (not params.contains(
"type")) {
887 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.
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.
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.
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)
make_recursive_variant< ObjectRef > 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.
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 & magnetic_anisotropy_energy() const
auto const & rinertia() const
auto const & stoner_wohlfarth_phi_0() const
auto const & mass() const
auto const & magnetic_anisotropy_field_inv() const
Utils::compact_vector< int > & exclusions()
auto const & quat() const
auto const & rotation() const
auto const & vs_relative() const
auto const & stoner_wohlfarth_tau0_inv() const
auto const & gamma() const
auto const & gamma_rot() const
auto const & saturation_magnetization() const
auto const & stoner_wohlfarth_dt_incr() 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 & stoner_wohlfarth_is_enabled() 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
Recursive variant implementation.
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.