ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ParticleSlice.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "ParticleHandle.hpp"
23
30
32
34
35#include <algorithm>
36#include <cassert>
37#include <functional>
38#include <memory>
39#include <ranges>
40#include <string>
41#include <string_view>
42#include <type_traits>
43#include <unordered_map>
44#include <utility>
45#include <vector>
46
47namespace ScriptInterface {
48namespace Particles {
49namespace traits {
50template <typename T> struct is_vector_like : std::false_type {};
51template <typename T, typename Alloc>
52struct is_vector_like<std::vector<T, Alloc>> : std::true_type {};
53template <typename T, std::size_t N>
54struct is_vector_like<Utils::Vector<T, N>> : std::true_type {};
55} // namespace traits
56
57template <class Container>
59 std::vector<int> const &pids, std::string const &param_name,
60 Container const &values, Context *context,
61 std::shared_ptr<CellSystem::CellSystem> cell_structure,
62 std::shared_ptr<Interactions::BondedInteractions> bonded_ias) {
63
65 auto so = std::dynamic_pointer_cast<ParticleModifier>(context->make_shared(
66 "Particles::ParticleModifier", {{"id", -1},
67 {"__cell_structure", cell_structure},
68 {"__bonded_ias", bonded_ias}}));
69 for (std::size_t i = 0; i < pids.size(); ++i) {
70 so->set_pid(pids[i]);
71 so->do_set_parameter(param_name, values[i]);
72 }
73 } else {
74 throw Exception("Values must be of type vector, got " +
75 detail::demangle::simplify_symbol(&values));
76 }
77}
78
79template <typename T>
80inline auto
81get_particles_properties(std::vector<int> const &pids,
82 std::function<T(Particle const &)> const &getter,
83 Context *context,
84 CellStructure const &cell_structure) {
85
86 using value_type =
87 std::conditional_t<Variant::has_type<std::vector<T>>::value, T, Variant>;
88 std::vector<value_type> result;
89
90 auto const n_ranks = static_cast<std::size_t>(context->get_comm().size());
91 auto const size_hint = pids.size() / n_ranks;
92 std::vector<std::pair<int, T>> parameters;
93 parameters.reserve(size_hint);
94 for (auto const &pid : pids) {
95 auto const p = cell_structure.get_local_particle(pid);
96 if (p and not p->is_ghost()) {
97 parameters.emplace_back(pid, getter(*p));
98 }
99 }
100
101 // collect values from all nodes
102 Utils::Mpi::gather_buffer(parameters, context->get_comm(), 0);
103 if (!context->is_head_node()) {
104 return result;
105 }
106
107 // reorder gathered values to match the caller's id_selection order
108 assert(parameters.size() == pids.size() &&
109 "Missing or duplicate particle ids");
110 std::unordered_map<int, std::size_t> lookup;
111 lookup.reserve(parameters.size());
112 std::size_t p_index = 0u;
113 for (auto const pid : pids) {
114 lookup[pid] = p_index;
115 ++p_index;
116 }
117 result.resize(pids.size());
118 for (auto &[pid, val] : parameters) {
119 result[lookup[pid]] = std::move(val);
120 }
121 return result;
122}
123
124class ParticleSlice : public AutoParameters<ParticleSlice> {
125 std::vector<int> m_id_selection;
126 int m_chunk_size;
127 std::weak_ptr<CellSystem::CellSystem> m_cell_structure;
128 std::weak_ptr<Interactions::BondedInteractions> m_bonded_ias;
129 std::weak_ptr<::System::System> m_system;
130 /** @brief Data structure to store names of parameters with special setters.
131 */
132 std::set<std::string_view> const m_special_parameters{
133 "pos", "type", "bonds",
134#ifdef ESPRESSO_ELECTROSTATICS
135 "q",
136#endif // ESPRESSO_ELECTROSTATICS
137#ifdef ESPRESSO_EXCLUSIONS
138 "exclusions",
139#endif // ESPRESSO_EXCLUSIONS
140 };
141
142 auto get_cell_structure() const {
143 auto cell_structure_ptr = m_cell_structure.lock();
144 assert(cell_structure_ptr != nullptr);
145 auto &cell_structure = cell_structure_ptr->get_cell_structure();
146 return &cell_structure;
147 }
148
149 auto get_system() const {
150 auto ptr = m_system.lock();
151 assert(ptr != nullptr);
152 return ptr;
153 }
154
155public:
158 {"chunk_size", AutoParameter::read_only,
159 [this]() { return m_chunk_size; }},
160 {"id_selection", AutoParameter::read_only,
161 [this]() { return m_id_selection; }},
162 });
163 }
164
165 void do_construct(VariantMap const &params) override;
166
167 Variant do_call_method(std::string const &name,
168 VariantMap const &params) override;
169
170 void attach(std::weak_ptr<::System::System> system) {
171 assert(m_system.expired());
172 m_system = system;
173 }
174};
175
176} // namespace Particles
177} // namespace ScriptInterface
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
void add_parameters(std::vector< AutoParameter > &&params)
Context of an object handle.
Definition Context.hpp:53
virtual std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap &parameters)=0
Get a new reference counted instance of a script interface by name.
virtual bool is_head_node() const =0
virtual boost::mpi::communicator const & get_comm() const =0
void attach(std::weak_ptr<::System::System > system)
void set_from_vector_like(std::vector< int > const &pids, std::string const &param_name, Container const &values, Context *context, std::shared_ptr< CellSystem::CellSystem > cell_structure, std::shared_ptr< Interactions::BondedInteractions > bonded_ias)
auto get_particles_properties(std::vector< int > const &pids, std::function< T(Particle const &)> const &getter, Context *context, CellStructure const &cell_structure)
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
Definition Variant.hpp:131
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
STL namespace.
static auto & get_cell_structure()
Struct holding all information for one particle.
Definition Particle.hpp:436
static constexpr const ReadOnly read_only