ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKReaction.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 ESPRESSO_WALBERLA
25
26#include "EKReactant.hpp"
27#include "LatticeIndices.hpp"
28#include "LatticeWalberla.hpp"
29
33
36
38
39#include <algorithm>
40#include <memory>
41#include <sstream>
42#include <stdexcept>
43#include <string>
44#include <vector>
45
47
48class EKReaction : public AutoParameters<EKReaction, LatticeIndices> {
49public:
50 [[nodiscard]] std::shared_ptr<::walberla::EKReactionBase>
51 get_instance() const {
52 return m_ekreaction;
53 }
54
58
59protected:
60 auto get_agrid(VariantMap const &args) const {
61 auto lattice = get_value<std::shared_ptr<LatticeWalberla>>(args, "lattice");
62 return get_value<double>(lattice->get_parameter("agrid"));
63 }
64
65 auto get_is_gpu(VariantMap const &args) const {
66 auto const reactants = get_value<std::vector<Variant>>(args, "reactants");
67 auto const reactant =
69 return reactant->is_gpu();
70 }
71
73 auto const tau = get_value<double>(args, "tau");
74 auto const agrid = get_agrid(args);
75 auto reactant = get_value<std::vector<Variant>>(args, "reactants");
76
77 auto get_order = [](Variant const &v) {
78 return get_value<double>(
79 get_value<std::shared_ptr<EKReactant>>(v)->get_parameter("order"));
80 };
81 auto const sum_alphas =
82 std::accumulate(reactant.begin(), reactant.end(), 0.,
83 [get_order](double sum, auto &element) {
84 return sum + get_order(element);
85 });
86
87 return tau / std::pow(Utils::int_pow<3>(agrid), sum_alphas - 1.);
88 }
89
90 template <typename F>
91 auto make_instance(VariantMap const &args, F &allocator) const {
92 auto lattice = get_value<std::shared_ptr<LatticeWalberla>>(args, "lattice");
93 auto reactants = get_value<std::vector<Variant>>(args, "reactants");
95 auto get_instance = [](Variant const &v) {
97 };
98 std::ranges::transform(reactants, std::back_inserter(output), get_instance);
99
100 auto const coefficient =
102
103 return allocator(lattice->lattice(), output, coefficient);
104 }
105
106 std::shared_ptr<::walberla::EKReactionBase> m_ekreaction;
108};
109
111public:
113 add_parameters({{"coefficient",
114 [this](Variant const &v) {
115 get_instance()->set_coefficient(
117 },
118 [this]() {
119 return get_instance()->get_coefficient() /
121 }}});
122 }
123
134};
135
137public:
140 {{"coefficient",
141 [this](Variant const &v) {
142 get_instance()->set_coefficient(get_value<double>(v) *
144 },
145 [this]() {
146 return get_instance()->get_coefficient() /
148 }},
149 {"shape", AutoParameter::read_only, [this]() {
150 return get_instance()->get_lattice()->get_grid_dimensions();
151 }}});
152 }
153
154 void do_construct(VariantMap const &args) override {
155 auto const agrid = get_agrid(args);
157 if (get_is_gpu(args)) {
158#ifdef ESPRESSO_CUDA
159 m_ekreaction_impl =
161#endif
162 } else {
163 m_ekreaction_impl =
165 }
166 m_ekreaction = m_ekreaction_impl;
167 }
168
169 [[nodiscard]] Variant do_call_method(std::string const &method,
170 VariantMap const &parameters) override {
171 if (method == "set_node_is_boundary") {
172 auto const index = get_mapped_index(
174 get_instance()->get_lattice()->get_grid_dimensions());
175 m_ekreaction_impl->set_node_is_boundary(
176 index, get_value<bool>(parameters, "is_boundary"));
177 return none;
178 }
179 if (method == "get_node_is_boundary") {
180 auto const index = get_mapped_index(
182 get_instance()->get_lattice()->get_grid_dimensions());
183 auto const result = m_ekreaction_impl->get_node_is_boundary(index);
184 return Utils::Mpi::reduce_optional(context()->get_comm(), result);
185 }
186 return {};
187 }
188
189private:
190 std::shared_ptr<::walberla::EKReactionBaseIndexed> m_ekreaction_impl;
191};
192
193} // namespace ScriptInterface::walberla
194
195#endif // ESPRESSO_WALBERLA
Bind parameters in the script interface.
Variant get_parameter(const std::string &name) const final
void add_parameters(std::vector< AutoParameter > &&params)
Utils::Vector3i get_mapped_index(Utils::Vector3i const &index, Utils::Vector3i const &shape) const
Context * context() const
Responsible context.
void do_construct(VariantMap const &args) override
void do_construct(VariantMap const &args) override
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
auto get_conversion_coefficient() const noexcept
auto get_is_gpu(VariantMap const &args) const
std::shared_ptr<::walberla::EKReactionBase > m_ekreaction
auto get_agrid(VariantMap const &args) const
std::shared_ptr<::walberla::EKReactionBase > get_instance() const
auto calculate_bulk_conversion_factor(VariantMap const &args) const
auto make_instance(VariantMap const &args, F &allocator) const
std::vector< std::shared_ptr< EKReactant > > reactants_type
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:133
constexpr const None none
None-"literal".
Definition Variant.hpp:126
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
std::shared_ptr< EKReactionBaseIndexed > new_ek_reaction_indexed_gpu(std::shared_ptr< LatticeWalberla > const &lattice, typename EKReactionBase::reactants_type const &reactants, double coefficient)
std::shared_ptr< EKReactionBase > new_ek_reaction_bulk_gpu(std::shared_ptr< LatticeWalberla > const &lattice, typename EKReactionBase::reactants_type const &reactants, double coefficient)
std::shared_ptr< EKReactionBaseIndexed > new_ek_reaction_indexed_cpu(std::shared_ptr< LatticeWalberla > const &lattice, typename EKReactionBase::reactants_type const &reactants, double coefficient)
std::shared_ptr< EKReactionBase > new_ek_reaction_bulk_cpu(std::shared_ptr< LatticeWalberla > const &lattice, typename EKReactionBase::reactants_type const &reactants, double coefficient)
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84