ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ReactionAlgorithm.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-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#include "ReactionAlgorithm.hpp"
21
26
30
31#include <boost/mpi.hpp>
32#include <boost/mpi/collectives.hpp>
33#include <boost/serialization/serialization.hpp>
34
35#include <map>
36#include <memory>
37#include <stdexcept>
38#include <string>
39#include <vector>
40
41namespace ScriptInterface {
42namespace ReactionMethods {
43
45 VariantMap const &params) {
46 if (name == "count_number_of_particles_per_type") {
47 auto const &cs = m_cell_system->get_cell_structure();
48 auto const types = get_value<std::vector<int>>(params, "types");
49 std::vector<int> local_numbers;
50 std::vector<int> global_numbers(types.size());
52 for (auto const &type : types) {
53 if (type < 0) {
54 throw std::runtime_error("Types may not be negative");
55 }
56 int counter = 0;
57 for (auto const &p : cs.local_particles()) {
58 if (p.type() == type) {
59 counter++;
60 }
61 }
62 local_numbers.emplace_back(counter);
63 }
64 });
65 boost::mpi::reduce(context()->get_comm(), local_numbers, global_numbers,
66 std::plus<>(), 0);
67 return global_numbers;
68 }
69 if (name == "single_update") {
70 auto const pid = get_value<int>(params, "pid");
71 auto const properties = get_value<VariantMap>(params, "properties");
72 m_particle_modifier->set_pid(pid);
73 auto const &cs = m_cell_system->get_cell_structure();
74 auto const *p = cs.get_local_particle(pid);
75 if (p != nullptr and p->is_ghost()) {
76 p = nullptr;
77 }
78 int old_type = -1;
79 if (context()->is_head_node()) {
80 if (p) {
81 old_type = p->type();
82 } else {
83 context()->get_comm().recv(boost::mpi::any_source, 42, old_type);
84 }
85 } else if (p) {
86 context()->get_comm().send(0, 42, p->type());
87 }
88 for (auto const &[param_name, value] :
89 std::map<std::string, Variant>(properties.begin(), properties.end())) {
90 m_particle_modifier->do_set_parameter(param_name, value);
91 }
92 return old_type;
93 }
94 if (name == "batch_update") {
95 auto const pids = get_value<std::vector<int>>(params, "pids");
96 auto const properties = get_value<VariantMap>(params, "properties");
97 for (int pid : pids) {
98 m_particle_modifier->set_pid(pid);
99 for (auto const &[param_name, value] : std::map<std::string, Variant>(
100 properties.begin(), properties.end())) {
101 m_particle_modifier->do_set_parameter(param_name, value);
102 }
103 }
104 return {};
105 }
106 if (name == "delete_particle") {
107 m_particle_modifier->set_pid(get_value<int>(params, "pid"));
108 m_particle_modifier->ParticleHandle::do_call_method("remove_particle", {});
109 return {};
110 }
111 if (name == "delete_particles") {
112 auto const pids = get_value<std::vector<int>>(params, "pids");
113 for (int pid : pids) {
114 m_particle_modifier->set_pid(pid);
115 m_particle_modifier->ParticleHandle::do_call_method("remove_particle",
116 {});
117 }
118 return {};
119 }
120 if (context()->is_head_node()) {
121 throw std::runtime_error("unknown method '" + name + "'");
122 }
123 return {};
124}
125
127 m_system = get_value<std::shared_ptr<System::System>>(params, "system");
129 m_system->get_parameter("cell_system"));
131 params, "particle_modifier");
132}
133
134} /* namespace ReactionMethods */
135} /* namespace ScriptInterface */
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual bool is_head_node() const =0
virtual boost::mpi::communicator const & get_comm() const =0
Context * context() const
Responsible context.
std::string_view name() const
void do_construct(VariantMap const &params) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
std::shared_ptr< Particles::ParticleModifier > m_particle_modifier
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
Recursive variant implementation.
Definition Variant.hpp:84