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; }},
195 throw std::domain_error(
196 error_msg(
"type",
"must be an integer >= 0"));
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);
229 throw std::domain_error(
error_msg(
"mass",
"must be a float > 0"));
237 throw std::runtime_error(
"Feature MASS not compiled in");
243#ifdef ESPRESSO_ELECTROSTATICS
250 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
255#ifdef ESPRESSO_DIPOLES
258 set_particle_property([&value](
Particle &p) {
270#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
277#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
280 set_particle_property([&value](
Particle &p) {
282 if (
dict.contains(
"is_enabled"))
285 if (
dict.contains(
"sw_phi_0"))
288 if (
dict.contains(
"sat_mag"))
291 if (
dict.contains(
"anisotropy_field_inv"))
294 if (
dict.contains(
"anisotropy_energy"))
297 if (
dict.contains(
"sw_tau0_inv"))
300 if (
dict.contains(
"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) {
333 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
343 set_particle_property([&value](
Particle &p) {
357 set_particle_property([&value](
Particle &p) {
368 set_particle_property([&value](
Particle &p) {
378#ifdef ESPRESSO_ROTATIONAL_INERTIA
385#ifdef ESPRESSO_LB_ELECTROHYDRODYNAMICS
392#ifdef ESPRESSO_EXTERNAL_FORCES
395 set_particle_property([&value](
Particle &p) {
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,
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(
483 if (array.size() != 3) {
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,
508 "propagation",
"propagation combination not accepted: " +
514#ifdef ESPRESSO_ENGINE
517 set_particle_property([&value](
Particle &p) {
521 if (
dict.contains(
"f_swim")) {
524 if (
dict.contains(
"is_engine_force_on_fluid")) {
525 auto const 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") {
547 if (
not params.contains(
"value")) {
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(); });
576 for (std::size_t i = 0; i <
bonds_ids.size(); i += 1) {
584#ifdef ESPRESSO_EXCLUSIONS
586 if (params.contains(
"exclusions")) {
593 context()->parallel_try_catch([&]() {
597 context()->get_comm());
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()) {
629 for (
auto const pid :
bond_view.partner_ids()) {
636 if (name ==
"add_bond") {
640 std::ranges::copy(partner_ids, std::back_inserter(
particle_ids));
643 }
else if (name ==
"del_bond") {
644 set_particle_property([¶ms](
Particle &p) {
655 }
else if (name ==
"delete_all_bonds") {
656 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
657 }
else if (name ==
"is_valid_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()) {
682 throw std::invalid_argument(
"A virtual site cannot relate to itself");
685 throw std::domain_error(
"Invalid particle id: " +
688 auto const system = get_system();
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") {
714 if (
not context()->is_head_node()) {
718 throw std::domain_error(
"Invalid molecule id: " + std::to_string(
molid));
721 throw std::runtime_error(
722 "Molecule id: " + std::to_string(
molid) +
723 " is already tracked by virtual site with particle id: " +
726 set_parameter(
"mol_id", params.at(
"molid"));
731#ifdef ESPRESSO_EXCLUSIONS
732 }
else if (name ==
"has_exclusion") {
741 if (name ==
"add_exclusion") {
744 context()->parallel_try_catch([&]() {
746 context()->get_comm());
750 }
else if (name ==
"del_exclusion") {
753 context()->parallel_try_catch([&]() {
755 context()->get_comm());
759 }
else if (name ==
"set_exclusions") {
768 context()->parallel_try_catch([&]() {
771 context()->get_comm());
785 }
else if (name ==
"get_exclusions") {
786 if (
not context()->is_head_node()) {
792#ifdef ESPRESSO_ROTATION
794 if (name ==
"rotate_particle") {
795 set_particle_property([¶ms](
Particle &p) {
801 if (name ==
"convert_vector_body_to_space") {
808 if (name ==
"convert_vector_space_to_body") {
819std::size_t ParticleHandle::setup_hidden_args(
VariantMap const ¶ms) {
821 if (params.contains(
"__cell_structure")) {
823 params,
"__cell_structure");
824 so->configure(*
this);
825 m_cell_structure =
so;
828 if (params.contains(
"__bonded_ias")) {
830 params,
"__bonded_ias");
842 if (
not params.contains(
"id")) {
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)
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
@ 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.
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.