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
49 void on_bind_system(::System::System &system) override {
50 static_cast<void>(system);
51 auto const &params = *m_params;
52 for (auto const &key : get_parameter_insertion_order()) {
53 if (params.count(key)) {
54 do_set_parameter(key.c_str(), params.at(key));
55 }
56 }
57 m_params.reset();
58 }
59
60public:
63 {"solver",
64 [this](Variant const &v) {
65 if (is_none(v)) {
66 reset_solver();
67 } else {
68 auto actor = get_value<ObjectRef>(v);
69 auto leaf = std::dynamic_pointer_cast<System::Leaf>(actor);
70 assert(leaf);
71 leaf->bind_system(m_system.lock());
72 actor->do_call_method("activate", {});
73 m_solver = actor;
74 }
75 },
76 [this]() { return m_solver ? Variant{m_solver} : Variant{None{}}; }},
77 });
78 }
79
80 void do_construct(VariantMap const &params) override {
81 m_params = std::make_unique<VariantMap>(params);
82 }
83
84protected:
85 Variant do_call_method(std::string const &name,
86 VariantMap const &params) override {
87 if (name == "clear") {
88 reset_solver();
89 return {};
90 }
91 return {};
92 }
93};
94
95} // namespace ScriptInterface::Dipoles
96
97#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 &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: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.