ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
script_interface/walberla/EKContainer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-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 WALBERLA
25
26#include "EKFFT.hpp"
27#include "EKNone.hpp"
28#include "EKReactions.hpp"
29#include "EKSpecies.hpp"
30
33
35#include "core/ek/Solver.hpp"
37
40
41#include <cassert>
42#include <memory>
43#include <optional>
44#include <string>
45#include <variant>
46
48
49class EKContainer : public ObjectList<EKSpecies> {
51 using value_type = typename Base::value_type;
52
53 std::variant<
54#ifdef WALBERLA_FFT
55 std::shared_ptr<EKFFT>,
56#endif
57 std::shared_ptr<EKNone>>
58 m_poisson_solver;
59
60 std::shared_ptr<EKReactions> m_ek_reactions;
61 std::shared_ptr<::EK::EKWalberla> m_ek_instance;
62 std::shared_ptr<::EK::EKWalberla::ek_container_type> m_ek_container;
63 bool m_is_active;
64
65 bool has_in_core(value_type const &obj_ptr) const override {
66 return m_ek_container->contains(obj_ptr->get_ekinstance());
67 }
68 void add_in_core(value_type const &obj_ptr) override {
69 context()->parallel_try_catch(
70 [this, &obj_ptr]() { m_ek_container->add(obj_ptr->get_ekinstance()); });
71 }
72 void remove_in_core(value_type const &obj_ptr) final {
73 m_ek_container->remove(obj_ptr->get_ekinstance());
74 }
75
76 struct GetPoissonSolverAsVariant {
77 template <typename T>
78 auto operator()(std::shared_ptr<T> const &solver) const {
79 return (solver) ? Variant{solver} : Variant{none};
80 }
81 };
82
83 Variant get_solver() const {
84 return std::visit(GetPoissonSolverAsVariant(), m_poisson_solver);
85 }
86
87 struct GetPoissonSolverCoreInstance {
88 template <typename T>
89 std::shared_ptr<::walberla::PoissonSolver>
90 operator()(std::shared_ptr<T> const &solver) const {
91 return solver->get_instance();
92 }
93 };
94
95 auto extract_solver(Variant const &v) {
96 std::optional<decltype(m_poisson_solver)> solver;
98 if (auto ptr = std::dynamic_pointer_cast<EKNone>(so_ptr)) {
99 solver = std::move(ptr);
100 }
101#ifdef WALBERLA_FFT
102 else if (auto ptr = std::dynamic_pointer_cast<EKFFT>(so_ptr)) {
103 solver = std::move(ptr);
104 }
105#endif
106 assert(solver.has_value());
107 return *solver;
108 }
109
110 void set_solver(Variant const &v) {
111 m_poisson_solver = extract_solver(v);
112 auto handle = std::visit(GetPoissonSolverCoreInstance{}, m_poisson_solver);
113 context()->parallel_try_catch(
114 [this, &handle]() { m_ek_container->set_poisson_solver(handle); });
115 }
116
117public:
119 add_parameters({
121 [this]() { return m_ek_container->get_tau(); }},
122 {"solver", [this](Variant const &v) { set_solver(v); },
123 [this]() { return get_solver(); }},
124 {"reactions", AutoParameter::read_only,
125 [this]() { return m_ek_reactions; }},
126 {"is_active", AutoParameter::read_only,
127 [this]() { return m_is_active; }},
128 });
129 }
130
131 ~EKContainer() override { do_destruct(); }
132
133 void do_construct(VariantMap const &params) override {
134 m_is_active = false;
135 auto const tau = get_value<double>(params, "tau");
136 context()->parallel_try_catch([tau]() {
137 if (tau <= 0.) {
138 throw std::domain_error("Parameter 'tau' must be > 0");
139 }
140 });
141 m_poisson_solver = extract_solver(
142 (params.count("solver") == 1) ? params.at("solver") : Variant{none});
143 m_ek_container = std::make_shared<::EK::EKWalberla::ek_container_type>(
144 tau, std::visit(GetPoissonSolverCoreInstance{}, m_poisson_solver));
145 m_ek_reactions = get_value<decltype(m_ek_reactions)>(params, "reactions");
146 m_ek_instance = std::make_shared<::EK::EKWalberla>(
147 m_ek_container, m_ek_reactions->get_handle());
148 // EK species must be added after tau
150 }
151
152protected:
153 Variant do_call_method(std::string const &method,
154 VariantMap const &parameters) override {
155 if (method == "activate") {
156 context()->parallel_try_catch([this]() {
158 });
159 m_is_active = true;
160 return {};
161 }
162 if (method == "deactivate") {
163 if (m_is_active) {
165 m_is_active = false;
166 }
167 return {};
168 }
169
171 }
172};
173
174} // namespace ScriptInterface::walberla
175
176#endif // WALBERLA
Owning list of object handles.
void do_construct(VariantMap const &params) override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
std::shared_ptr< EKSpecies > value_type
bool has_in_core(value_type const &obj_ptr) const override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
This file contains the defaults for ESPResSo.
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: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
constexpr const None none
None-"literal".
Definition Variant.hpp:50
System & get_system()
static SteepestDescentParameters params
Currently active steepest descent instance.
void set(Args... args)
Set the EK solver.
void reset()
Remove the EK solver.
Definition ek/Solver.cpp:60
static constexpr const ReadOnly read_only