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