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
73 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})) {
95 throw std::invalid_argument(
err_msg);
102#if defined(ESPRESSO_ROTATION) or defined(ESPRESSO_EXTERNAL_FORCES)
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 {
145 auto &cell_structure = get_cell_structure()->get_cell_structure();
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>(
166void ParticleHandle::set_particle_property(
F const &
fun)
const {
168 auto const &comm = context()->get_comm();
170 if (ptr !=
nullptr) {
177void ParticleHandle::set_particle_property(T &(
Particle::*
setter)(),
179 set_particle_property(
183#ifdef ESPRESSO_EXCLUSIONS
184void ParticleHandle::set_exclusions(Variant
const &value) {
192 context()->parallel_try_catch([&]() {
195 context()->get_comm());
198 set_particle_property([&](
Particle const &p) {
199 for (
auto const pid : p.exclusions()) {
211ParticleHandle::ParticleHandle() {
218 {
"id", AutoParameter::read_only, [
this]() {
return m_pid; }},
224 throw std::domain_error(
225 error_msg(
"type",
"must be an integer >= 0"));
240 auto const pos = p.
pos();
242 return get_system()->
box_geo->unfolded_position(pos, image_box);
258 throw std::domain_error(
error_msg(
"mass",
"must be a float > 0"));
266 throw std::runtime_error(
"Feature MASS not compiled in");
272#ifdef ESPRESSO_ELECTROSTATICS
279 throw std::runtime_error(
"Feature ELECTROSTATICS not compiled in");
284#ifdef ESPRESSO_DIPOLES
287 set_particle_property([&value](
Particle &p) {
299#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
306#ifdef ESPRESSO_THERMAL_STONER_WOHLFARTH
309 set_particle_property([&value](
Particle &p) {
311 if (
dict.contains(
"is_enabled"))
314 if (
dict.contains(
"sw_phi_0"))
317 if (
dict.contains(
"sat_mag"))
320 if (
dict.contains(
"anisotropy_field_inv"))
323 if (
dict.contains(
"anisotropy_energy"))
326 if (
dict.contains(
"sw_tau0_inv"))
329 if (
dict.contains(
"sw_dt_incr"))
347#ifdef ESPRESSO_ROTATION
350 set_particle_property([&value](
Particle &p) {
362 set_particle_property([&quat](
Particle &p) { p.
quat() = quat; });
372 set_particle_property([&value](
Particle &p) {
386 set_particle_property([&value](
Particle &p) {
397 set_particle_property([&value](
Particle &p) {
407#ifdef ESPRESSO_ROTATIONAL_INERTIA
414#ifdef ESPRESSO_LB_ELECTROHYDRODYNAMICS
421#ifdef ESPRESSO_EXTERNAL_FORCES
424 set_particle_property([&value](
Particle &p) {
433 ::detail::get_nth_bit(fixed, 1),
434 ::detail::get_nth_bit(fixed, 2)}};
441#ifdef ESPRESSO_ROTATION
449#ifdef ESPRESSO_THERMOSTAT_PER_PARTICLE
456#ifdef ESPRESSO_ROTATION
465 {
"pos_folded", AutoParameter::read_only,
467 auto const &box_geo = *get_system()->
box_geo;
470 {
"lees_edwards_offset",
475 {
"lees_edwards_flag", AutoParameter::read_only,
477 {
"image_box", AutoParameter::read_only,
479 auto const &box_geo = *get_system()->
box_geo;
481 return box_geo.folded_image_box(p.
pos(), p.
image_box());
483 {
"node", AutoParameter::read_only,
491 throw std::domain_error(
492 error_msg(
"mol_id",
"must be an integer >= 0"));
497#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
501 set_particle_property(
512 if (array.size() != 3) {
517 vs_relative.rel_orientation =
521 "vs_relative",
"must take the form [id, distance, quaternion]"));
523 set_particle_property(
528 return std::vector<Variant>{{
vs_rel.to_particle_id,
vs_rel.distance,
537 "propagation",
"propagation combination not accepted: " +
543#ifdef ESPRESSO_ENGINE
546 set_particle_property([&value](
Particle &p) {
550 if (
dict.contains(
"f_swim")) {
553 if (
dict.contains(
"is_engine_force_on_fluid")) {
554 auto const is_engine_force_on_fluid =
556 swim.is_engine_force_on_fluid = is_engine_force_on_fluid;
564 {
"f_swim", swim.f_swim},
565 {
"is_engine_force_on_fluid", swim.is_engine_force_on_fluid},
572Variant ParticleHandle::do_call_method(std::string
const &name,
574 if (name ==
"set_param_parallel") {
576 if (
not params.contains(
"value")) {
579 auto const &value = params.at(
"value");
580 context()->parallel_try_catch(
581 [&]() { do_set_parameter(
param_name, value); });
584 if (name ==
"update_params") {
586 context()->parallel_try_catch([&]() {
587#ifdef ESPRESSO_ROTATION
590 for (
auto const &name : get_parameter_insertion_order()) {
591 if (params.contains(name)
and name !=
"bonds") {
592 do_set_parameter(name, params.at(name));
598 if (params.contains(
"bonds_ids")) {
600 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
605 for (std::size_t i = 0; i <
bonds_ids.size(); i += 1) {
613#ifdef ESPRESSO_EXCLUSIONS
615 if (params.contains(
"exclusions")) {
616 set_exclusions(params.at(
"exclusions"));
621 if (name ==
"get_bond_by_id") {
622 if (
not context()->is_head_node()) {
625 return get_bonded_ias()->call_method(
"get_bond", params);
627 if (name ==
"get_bonds_view") {
628 if (
not context()->is_head_node()) {
633 for (
auto const &&
bond_view : bond_list) {
636 for (
auto const pid :
bond_view.partner_ids()) {
643 if (name ==
"add_bond") {
647 std::ranges::copy(partner_ids, std::back_inserter(
particle_ids));
650 }
else if (name ==
"del_bond") {
651 set_particle_property([¶ms](
Particle &p) {
656 auto &bond_list = p.
bonds();
657 auto it = std::find(bond_list.begin(), bond_list.end(),
bond_view);
658 if (
it != bond_list.end()) {
662 }
else if (name ==
"delete_all_bonds") {
663 set_particle_property([&](
Particle &p) { p.
bonds().clear(); });
664 }
else if (name ==
"is_valid_bond_id") {
666 return get_system()->
bonded_ias->get_zero_based_type(bond_id) != 0;
668 if (name ==
"remove_particle") {
669 context()->parallel_try_catch([&]() {
675 }
else if (name ==
"is_virtual") {
676 if (
not context()->is_head_node()) {
680#ifdef ESPRESSO_VIRTUAL_SITES_RELATIVE
681 }
else if (name ==
"vs_auto_relate_to") {
682 if (
not context()->is_head_node()) {
689 throw std::invalid_argument(
"A virtual site cannot relate to itself");
692 throw std::domain_error(
"Invalid particle id: " +
695 auto const system = get_system();
710 set_parameter(
"vs_relative",
Variant{std::vector<Variant>{
712 set_parameter(
"propagation",
716#ifdef ESPRESSO_VIRTUAL_SITES_CENTER_OF_MASS
717 }
else if (name ==
"vs_com_relate_to") {
721 if (
not context()->is_head_node()) {
725 throw std::domain_error(
"Invalid molecule id: " + std::to_string(
molid));
728 throw std::runtime_error(
729 "Molecule id: " + std::to_string(
molid) +
730 " is already tracked by virtual site with particle id: " +
733 set_parameter(
"mol_id", params.at(
"molid"));
738#ifdef ESPRESSO_EXCLUSIONS
739 }
else if (name ==
"has_exclusion") {
748 if (name ==
"add_exclusion") {
751 context()->parallel_try_catch([&]() {
753 context()->get_comm());
757 }
else if (name ==
"del_exclusion") {
760 context()->parallel_try_catch([&]() {
762 context()->get_comm());
766#ifdef ESPRESSO_EXCLUSIONS
767 }
else if (name ==
"set_exclusions") {
768 set_exclusions(params.at(
"p_ids"));
770 }
else if (name ==
"get_exclusions") {
771 if (
not context()->is_head_node()) {
777#ifdef ESPRESSO_ROTATION
779 if (name ==
"rotate_particle") {
780 set_particle_property([¶ms](
Particle &p) {
786 if (name ==
"convert_vector_body_to_space") {
793 if (name ==
"convert_vector_space_to_body") {
804std::size_t ParticleHandle::setup_hidden_args(
VariantMap const ¶ms) {
806 if (params.contains(
"__cell_structure")) {
808 params,
"__cell_structure");
809 so->configure(*
this);
810 m_cell_structure =
so;
813 if (params.contains(
"__bonded_ias")) {
815 params,
"__bonded_ias");
827 if (
not params.contains(
"id")) {
840 context()->parallel_try_catch([&]() {
843 auto ptr = cell_structure.get_local_particle(m_pid);
844 if (ptr !=
nullptr) {
845 throw std::invalid_argument(
"Particle " + std::to_string(m_pid) +
850#ifdef ESPRESSO_ROTATION
858 context()->parallel_try_catch([&]() {
861 std::set<std::string_view>
const skip = {
862 "pos_folded",
"pos",
"id",
"node",
"image_box",
"bonds",
863#ifdef ESPRESSO_EXCLUSIONS
866 "lees_edwards_flag",
"__cpt_sentinel",
869 for (
auto const &name : get_parameter_insertion_order()) {
870 if (params.contains(name)
and not skip.contains(name)) {
871 do_set_parameter(name, params.at(name));
874 for (
auto const &name : params | std::views::keys) {
875 if (
not skip.contains(name)
and not name.starts_with(
'_')
and
876 not has_parameter(name)) {
877 auto error_msg =
"Unknown parameter '" + name +
"' for particle.";
878 std::string
hint =
"Hint: a feature is probably not compiled in.";
882 if (
not params.contains(
"type")) {
883 do_set_parameter(
"type", 0);
885#ifdef ESPRESSO_EXCLUSIONS
886 if (params.contains(
"exclusions")) {
887 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.
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 & image_box() 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.