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 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 auto so = std::dynamic_pointer_cast<ParticleModifier>(context->make_shared(
65 "Particles::ParticleModifier", {{"id", -1},
66 {"__cell_structure", cell_structure},
67 {"__bonded_ias", bonded_ias}}));
68 for (std::size_t i = 0; i < pids.size(); ++i) {
69 so->set_pid(pids[i]);
70 so->do_set_parameter(param_name, values[i]);
71 }
72 } else {
73 throw Exception("Values must be of type vector, got " +
74 detail::demangle::simplify_symbol(&values));
75 }
76}
77
78template <typename T>
79inline auto
80get_particles_properties(std::vector<int> const &pids,
81 std::function<T(Particle const &)> const &getter,
82 Context *context,
83 CellStructure const &cell_structure) {
84
85 using value_type =
86 std::conditional_t<Variant::has_type<std::vector<T>>::value, T, Variant>;
87 std::vector<value_type> result;
88
89 auto const n_ranks = static_cast<std::size_t>(context->get_comm().size());
90 auto const size_hint = pids.size() / n_ranks;
91 std::vector<std::pair<int, T>> parameters;
92 parameters.reserve(size_hint);
93 for (auto const &pid : pids) {
94 auto const p = cell_structure.get_local_particle(pid);
95 if (p and not p->is_ghost()) {
96 parameters.emplace_back(pid, getter(*p));
97 }
98 }
99
100 // collect values from all nodes
102 if (!context->is_head_node()) {
103 return result;
104 }
105
106 // sort values by particle id to retain original order
107 auto const projector = [](auto const &pair) { return pair.first; };
108 std::ranges::sort(parameters, std::less<int>{}, projector);
109 assert(std::ranges::equal(pids, parameters | std::views::keys) &&
110 "Missing or duplicate particle ids");
111
112 result.reserve(pids.size());
113 for (auto const &value : parameters | std::views::values) {
114 result.emplace_back(std::move(value));
115 }
116 return result;
117}
118
119class ParticleSlice : public AutoParameters<ParticleSlice> {
120 std::vector<int> m_id_selection;
121 int m_chunk_size;
122 std::weak_ptr<CellSystem::CellSystem> m_cell_structure;
123 std::weak_ptr<Interactions::BondedInteractions> m_bonded_ias;
124 std::weak_ptr<::System::System> m_system;
125 /** @brief Data structure to store names of parameters with special setters.
126 */
127 std::set<std::string_view> const m_special_parameters{
128 "pos", "type", "bonds",
129#ifdef ESPRESSO_ELECTROSTATICS
130 "q",
131#endif // ESPRESSO_ELECTROSTATICS
132#ifdef ESPRESSO_EXCLUSIONS
133 "exclusions",
134#endif // ESPRESSO_EXCLUSIONS
135 };
136
137 auto get_cell_structure() const {
138 auto cell_structure_ptr = m_cell_structure.lock();
139 assert(cell_structure_ptr != nullptr);
140 auto &cell_structure = cell_structure_ptr->get_cell_structure();
141 return &cell_structure;
142 }
143
144 auto get_system() const {
145 auto ptr = m_system.lock();
146 assert(ptr != nullptr);
147 return ptr;
148 }
149
150public:
152 add_parameters({
153 {"chunk_size", AutoParameter::read_only,
154 [this]() { return m_chunk_size; }},
155 {"id_selection", AutoParameter::read_only,
156 [this]() { return m_id_selection; }},
157 });
158 }
159
160 void do_construct(VariantMap const &params) override;
161
162 Variant do_call_method(std::string const &name,
163 VariantMap const &params) override;
164
165 void attach(std::weak_ptr<::System::System> system) {
166 assert(m_system.expired());
167 m_system = system;
168 }
169};
170
171} // namespace Particles
172} // 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.
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)
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.
static auto & get_cell_structure()
Struct holding all information for one particle.
Definition Particle.hpp:435
Recursive variant implementation.
Definition Variant.hpp:84