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 <utility>
44#include <vector>
45
46namespace ScriptInterface {
47namespace Particles {
48namespace traits {
49template <typename T> struct is_vector_like : std::false_type {};
50template <typename T, typename Alloc>
51struct is_vector_like<std::vector<T, Alloc>> : std::true_type {};
52template <typename T, std::size_t N>
53struct is_vector_like<Utils::Vector<T, N>> : std::true_type {};
54} // namespace traits
55
56template <class Container>
58 std::vector<int> const &pids, std::string const &param_name,
59 Container const &values, Context *context,
60 std::shared_ptr<CellSystem::CellSystem> cell_structure,
61 std::shared_ptr<Interactions::BondedInteractions> bonded_ias) {
62
64 VariantMap const obj_params{{"id", -1},
65 {"__cell_structure", cell_structure},
66 {"__bonded_ias", bonded_ias}};
67 auto so = std::dynamic_pointer_cast<ParticleModifier>(
68 context->make_shared("Particles::ParticleModifier", obj_params));
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
103 if (!context->is_head_node()) {
104 return result;
105 }
106
107 // sort values by particle id to retain original order
108 auto const projector = [](auto const &pair) { return pair.first; };
109 std::ranges::sort(parameters, std::less<int>{}, projector);
110 assert(std::ranges::equal(pids, parameters | std::views::keys) &&
111 "Missing or duplicate particle ids");
112
113 result.reserve(pids.size());
114 for (auto const &value : parameters | std::views::values) {
115 result.emplace_back(std::move(value));
116 }
117 return result;
118}
119
120class ParticleSlice : public AutoParameters<ParticleSlice> {
121 std::vector<int> m_id_selection;
122 int m_chunk_size;
123 std::weak_ptr<CellSystem::CellSystem> m_cell_structure;
124 std::weak_ptr<Interactions::BondedInteractions> m_bonded_ias;
125 std::weak_ptr<::System::System> m_system;
126 /** @brief Data structure to store names of parameters with special setters.
127 */
128 std::set<std::string_view> const m_special_parameters{
129 "pos", "type", "bonds",
130#ifdef ESPRESSO_ELECTROSTATICS
131 "q",
132#endif // ESPRESSO_ELECTROSTATICS
133#ifdef ESPRESSO_EXCLUSIONS
134 "exclusions",
135#endif // ESPRESSO_EXCLUSIONS
136 };
137
138 auto get_cell_structure() const {
139 auto cell_structure_ptr = m_cell_structure.lock();
140 assert(cell_structure_ptr != nullptr);
141 auto &cell_structure = cell_structure_ptr->get_cell_structure();
142 return &cell_structure;
143 }
144
145 auto get_system() const {
146 auto ptr = m_system.lock();
147 assert(ptr != nullptr);
148 return ptr;
149 }
150
151public:
154 {"chunk_size", AutoParameter::read_only,
155 [this]() { return m_chunk_size; }},
156 {"id_selection", AutoParameter::read_only,
157 [this]() { return m_id_selection; }},
158 });
159 }
160
161 void do_construct(VariantMap const &params) override;
162
163 Variant do_call_method(std::string const &name,
164 VariantMap const &params) override;
165
166 void attach(std::weak_ptr<::System::System> system) {
167 assert(m_system.expired());
168 m_system = system;
169 }
170};
171
172} // namespace Particles
173} // namespace ScriptInterface
Describes a cell structure / cell system.
Particle * get_local_particle(int id)
Get a local particle by id.
Bind parameters in the script interface.
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
std::string_view name() const
void attach(std::weak_ptr<::System::System > system)
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) override
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)
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
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.
Struct holding all information for one particle.
Definition Particle.hpp:435
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84