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
61 void on_bind_system(::System::System &system) override {
62 static_cast<void>(system);
63 auto const &params = *m_params;
64 for (auto const &key : get_parameter_insertion_order()) {
65 if (params.count(key)) {
66 do_set_parameter(key.c_str(), params.at(key));
67 }
68 }
69 m_params.reset();
70 }
71
72public:
75 {"solver",
76 [this](Variant const &v) {
77 if (m_extension) {
78 if (context()->is_head_node()) {
79 throw std::runtime_error(
80 "Cannot change solver when an extension is active");
81 }
82 throw Exception("");
83 }
84 if (is_none(v)) {
85 reset_solver();
86 } else {
87 auto actor = get_value<ObjectRef>(v);
88 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
89 assert(leaf);
90 leaf->bind_system(m_system.lock());
91 actor->do_call_method("activate", {});
92 m_solver = actor;
93 }
94 },
95 [this]() { return m_solver ? Variant{m_solver} : Variant{None{}}; }},
96 {"extension",
97 [this](Variant const &v) {
98 if (is_none(v)) {
99 reset_extension();
100 } else {
101 auto actor = get_value<ObjectRef>(v);
102 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
103 assert(leaf);
104 leaf->bind_system(m_system.lock());
105 actor->do_call_method("activate", {});
106 m_extension = actor;
107 }
108 },
109 [this]() {
110 return m_extension ? Variant{m_extension} : Variant{None{}};
111 }},
112 });
113 }
114
115 void do_construct(VariantMap const &params) override {
116 m_params = std::make_unique<VariantMap>(params);
117 }
118
119protected:
120 Variant do_call_method(std::string const &name,
121 VariantMap const &params) override {
122 if (name == "clear") {
123 reset_solver();
124 return {};
125 }
126 return {};
127 }
128};
129
130} // namespace ScriptInterface::Coulomb
131
132#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)
void on_bind_system(::System::System &system) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) 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:59
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
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:80
bool is_none(Variant const &v)
Definition Variant.hpp:128
static SteepestDescentParameters params
Currently active steepest descent instance.