ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
electrostatics/Container.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 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 "config/config.hpp"
23
24#ifdef ELECTROSTATICS
25
27
31
32#include <memory>
33#include <optional>
34#include <string>
35
37
38class Container : public AutoParameters<Container, System::Leaf> {
39 ObjectRef m_solver;
40 ObjectRef m_extension;
41 std::unique_ptr<VariantMap> m_params;
42
43 void reset_solver() {
44 auto &system = get_system();
45 auto &coulomb = system.coulomb;
46 m_solver.reset();
47 m_extension.reset();
48 coulomb.impl->extension = std::nullopt;
49 coulomb.impl->solver = std::nullopt;
50 system.on_coulomb_change();
51 }
52
53 void reset_extension() {
54 auto &system = get_system();
55 auto &coulomb = system.coulomb;
56 m_extension.reset();
57 coulomb.impl->extension = std::nullopt;
58 system.on_coulomb_change();
59 }
60
62 auto const &params = *m_params;
63 for (auto const &key : get_parameter_insertion_order()) {
64 if (params.count(key)) {
65 do_set_parameter(key.c_str(), params.at(key));
66 }
67 }
68 m_params.reset();
69 }
70
71public:
74 {"solver",
75 [this](Variant const &v) {
76 if (m_extension) {
77 if (context()->is_head_node()) {
78 throw std::runtime_error(
79 "Cannot change solver when an extension is active");
80 }
81 throw Exception("");
82 }
83 if (is_none(v)) {
84 reset_solver();
85 } else {
86 auto actor = get_value<ObjectRef>(v);
87 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
88 assert(leaf);
89 leaf->bind_system(m_system.lock());
90 actor->do_call_method("activate", {});
91 m_solver = actor;
92 }
93 },
94 [this]() { return m_solver ? Variant{m_solver} : Variant{None{}}; }},
95 {"extension",
96 [this](Variant const &v) {
97 if (is_none(v)) {
98 reset_extension();
99 } else {
100 auto actor = get_value<ObjectRef>(v);
101 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
102 assert(leaf);
103 leaf->bind_system(m_system.lock());
104 actor->do_call_method("activate", {});
105 m_extension = actor;
106 }
107 },
108 [this]() {
109 return m_extension ? Variant{m_extension} : Variant{None{}};
110 }},
111 });
112 }
113
114 void do_construct(VariantMap const &params) override {
115 m_params = std::make_unique<VariantMap>(params);
116 }
117
118protected:
119 Variant do_call_method(std::string const &name,
120 VariantMap const &params) override {
121 if (name == "clear") {
122 reset_solver();
123 return {};
124 }
125 return {};
126 }
127};
128
129} // namespace ScriptInterface::Coulomb
130
131#endif // ELECTROSTATICS
Bind parameters in the script interface.
void do_set_parameter(const std::string &name, const Variant &value) final
void add_parameters(std::vector< AutoParameter > &&params)
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) override
void on_bind_system(::System::System &) override
Type to indicate no value in Variant.
boost::string_ref name() const
Context * context() const
Responsible context.
std::weak_ptr<::System::System > m_system
Main system class.
This file contains the defaults for ESPResSo.
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:46
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:67
bool is_none(Variant const &v)
Definition Variant.hpp:115
static SteepestDescentParameters params
Currently active steepest descent instance.