ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
magnetostatics/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 DIPOLES
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 std::unique_ptr<VariantMap> m_params;
41
42 void reset_solver() {
43 auto &system = get_system();
44 m_solver.reset();
45 system.dipoles.impl->solver = std::nullopt;
46 system.on_dipoles_change();
47 }
48
50 auto const &params = *m_params;
51 for (auto const &key : get_parameter_insertion_order()) {
52 if (params.count(key)) {
53 do_set_parameter(key.c_str(), params.at(key));
54 }
55 }
56 m_params.reset();
57 }
58
59public:
62 {"solver",
63 [this](Variant const &v) {
64 if (is_none(v)) {
65 reset_solver();
66 } else {
67 auto actor = get_value<ObjectRef>(v);
68 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
69 assert(leaf);
70 leaf->bind_system(m_system.lock());
71 actor->do_call_method("activate", {});
72 m_solver = actor;
73 }
74 },
75 [this]() { return m_solver ? Variant{m_solver} : Variant{None{}}; }},
76 });
77 }
78
79 void do_construct(VariantMap const &params) override {
80 m_params = std::make_unique<VariantMap>(params);
81 }
82
83protected:
84 Variant do_call_method(std::string const &name,
85 VariantMap const &params) override {
86 if (name == "clear") {
87 reset_solver();
88 return {};
89 }
90 return {};
91 }
92};
93
94} // namespace ScriptInterface::Dipoles
95
96#endif // DIPOLES
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 &) override
void do_construct(VariantMap const &params) override
Variant do_call_method(std::string const &name, VariantMap const &params) override
Type to indicate no value in Variant.
boost::string_ref name() const
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.