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
52 std::variant<
53#ifdef WALBERLA_FFT
54 std::shared_ptr<EKFFT>,
55#endif
56 std::shared_ptr<EKNone>>
57 m_poisson_solver;
58
59 std::shared_ptr<EKReactions> m_ek_reactions;
60 std::shared_ptr<::EK::EKWalberla> m_ek_instance;
61 std::shared_ptr<::EK::EKWalberla::ek_container_type> m_ek_container;
62 bool m_is_active;
63
64 bool has_in_core(std::shared_ptr<EKSpecies> const &obj_ptr) const override {
65 return m_ek_container->contains(obj_ptr->get_ekinstance());
66 }
67 void add_in_core(std::shared_ptr<EKSpecies> const &obj_ptr) override {
68 context()->parallel_try_catch(
69 [this, &obj_ptr]() { m_ek_container->add(obj_ptr->get_ekinstance()); });
70 }
71 void remove_in_core(std::shared_ptr<EKSpecies> const &obj_ptr) override {
72 m_ek_container->remove(obj_ptr->get_ekinstance());
73 }
74
75 struct GetPoissonSolverAsVariant {
76 template <typename T>
77 auto operator()(std::shared_ptr<T> const &solver) const {
78 return (solver) ? Variant{solver} : Variant{none};
79 }
80 };
81
82 Variant get_solver() const {
83 return std::visit(GetPoissonSolverAsVariant(), m_poisson_solver);
84 }
85
86 struct GetPoissonSolverCoreInstance {
87 template <typename T>
88 std::shared_ptr<::walberla::PoissonSolver>
89 operator()(std::shared_ptr<T> const &solver) const {
90 return solver->get_instance();
91 }
92 };
93
94 auto extract_solver(Variant const &v) {
95 std::optional<decltype(m_poisson_solver)> solver;
97 if (auto ptr = std::dynamic_pointer_cast<EKNone>(so_ptr)) {
98 solver = std::move(ptr);
99 }
100#ifdef WALBERLA_FFT
101 else if (auto ptr = std::dynamic_pointer_cast<EKFFT>(so_ptr)) {
102 solver = std::move(ptr);
103 }
104#endif
105 assert(solver.has_value());
106 return *solver;
107 }
108
109 void set_solver(Variant const &v) {
110 m_poisson_solver = extract_solver(v);
111 auto handle = std::visit(GetPoissonSolverCoreInstance{}, m_poisson_solver);
112 context()->parallel_try_catch(
113 [this, &handle]() { m_ek_container->set_poisson_solver(handle); });
114 }
115
116public:
118 add_parameters({
120 [this]() { return m_ek_container->get_tau(); }},
121 {"solver", [this](Variant const &v) { set_solver(v); },
122 [this]() { return get_solver(); }},
123 {"reactions", AutoParameter::read_only,
124 [this]() { return m_ek_reactions; }},
125 {"is_active", AutoParameter::read_only,
126 [this]() { return m_is_active; }},
127 });
128 }
129
130 void do_construct(VariantMap const &params) override {
131 m_is_active = false;
132 auto const tau = get_value<double>(params, "tau");
133 context()->parallel_try_catch([tau]() {
134 if (tau <= 0.) {
135 throw std::domain_error("Parameter 'tau' must be > 0");
136 }
137 });
138 m_poisson_solver = extract_solver(
139 (params.count("solver") == 1) ? params.at("solver") : Variant{none});
140 m_ek_container = std::make_shared<::EK::EKWalberla::ek_container_type>(
141 tau, std::visit(GetPoissonSolverCoreInstance{}, m_poisson_solver));
142 m_ek_reactions = get_value<decltype(m_ek_reactions)>(params, "reactions");
143 m_ek_instance = std::make_shared<::EK::EKWalberla>(
144 m_ek_container, m_ek_reactions->get_handle());
145 // EK species must be added after tau
147 }
148
149protected:
150 Variant do_call_method(std::string const &method,
151 VariantMap const &parameters) override {
152 if (method == "activate") {
153 context()->parallel_try_catch([this]() {
155 });
156 m_is_active = true;
157 return {};
158 }
159 if (method == "deactivate") {
160 if (m_is_active) {
162 m_is_active = false;
163 }
164 return {};
165 }
166
168 }
169};
170
171} // namespace ScriptInterface::walberla
172
173#endif // WALBERLA
Owning list of ObjectHandles.
void do_construct(VariantMap const &params) override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
void add_in_core(std::shared_ptr< EKSpecies > const &obj_ptr) override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
bool has_in_core(std::shared_ptr< EKSpecies > const &obj_ptr) const override
void remove_in_core(std::shared_ptr< EKSpecies > const &obj_ptr) 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