ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKSpeciesSlice.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-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 "EKSpecies.hpp"
27
28#include "LatticeSlice.hpp"
29
32
36
37#include <utils/Vector.hpp>
39
40#include <cassert>
41#include <memory>
42#include <optional>
43#include <string>
44#include <type_traits>
45#include <unordered_map>
46#include <vector>
47
49
50using DensityBoundaryType = std::optional<double>;
51using FluxBoundaryType = std::optional<Utils::Vector3d>;
52
54
55 template <typename T> static Variant serialize(std::vector<T> const &values) {
56 if constexpr (std::is_same_v<T, FluxBoundaryType> or
57 std::is_same_v<T, DensityBoundaryType>) {
58 std::vector<Variant> vec;
59 vec.reserve(values.size());
60 for (auto const &opt : values) {
61 if (opt) {
62 vec.emplace_back(*opt);
63 } else {
64 vec.emplace_back(None{});
65 }
66 }
67 return {vec};
68 } else if constexpr (std::is_same_v<T, int> or std::is_same_v<T, double>) {
69 return {values};
70 } else {
72 }
73 }
74
75 template <typename T>
76 static std::vector<T> deserialize(Variant const &variant) {
77 std::vector<T> values;
78 if constexpr (std::is_same_v<T, FluxBoundaryType>) {
80 for (auto const &value : vector_variants) {
81 if (is_none(value)) {
82 values.emplace_back(std::nullopt);
83 } else {
84 values.emplace_back(get_value<Utils::Vector3d>(value));
85 }
86 }
87 } else if constexpr (std::is_same_v<T, DensityBoundaryType>) {
89 for (auto const &value : vector_variants) {
90 if (is_none(value)) {
91 values.emplace_back(std::nullopt);
92 } else {
93 values.emplace_back(get_value<double>(value));
94 }
95 }
96 } else if constexpr (std::is_same_v<T, double>) {
97 if (is_type<std::vector<int>>(variant)) {
99 values.reserve(values_int.size());
100 for (auto const val : values_int) {
101 values.emplace_back(static_cast<double>(val));
102 }
103 } else {
105 }
106 } else {
108 }
109 return values;
110 }
111};
112
113class EKSpeciesSlice : public LatticeSlice<EKFieldSerializer> {
115 std::shared_ptr<LatticeModel> m_ek_species;
116 std::shared_ptr<EKSpecies> m_ek_sip;
117 std::optional<ResourceObserver> m_mpi_cart_comm_observer;
118 double m_conv_dens;
119 double m_conv_flux;
120 std::unordered_map<std::string, std::vector<int>> m_shape_val;
121
122public:
123 void do_construct(VariantMap const &params) override {
124 m_ek_sip = get_value<std::shared_ptr<EKSpecies>>(params, "parent_sip");
125 m_ek_species = m_ek_sip->get_ekinstance();
126 m_mpi_cart_comm_observer = m_ek_sip->get_mpi_cart_comm_observer();
127 assert(m_ek_species);
128 m_conv_dens = m_ek_sip->get_conversion_factor_density();
129 m_conv_flux = m_ek_sip->get_conversion_factor_flux();
130 m_shape = get_value<std::vector<int>>(params, "shape");
132 get_value<Utils::Vector3i>(params, "slice_lower_corner");
134 get_value<Utils::Vector3i>(params, "slice_upper_corner");
135 m_shape_val["density"] = {1};
136 m_shape_val["flux"] = {3};
137 m_shape_val["flux_at_boundary"] = {1};
138 m_shape_val["density_at_boundary"] = {1};
139 m_shape_val["is_boundary"] = {1};
140 }
141
142 Variant do_call_method(std::string const &name,
143 VariantMap const &params) override;
144
145 ::LatticeWalberla const &get_lattice() const override {
146 return m_ek_species->get_lattice();
147 }
148};
149
150} // namespace ScriptInterface::walberla
151
152#endif // ESPRESSO_WALBERLA
Vector implementation and trait types for boost qvm interoperability.
Interface of a lattice-based electrokinetic model.
Class that runs and controls the BlockForest in waLBerla.
Type to indicate no value in Variant.
Definition None.hpp:32
std::string_view name() const
Variant do_call_method(std::string const &name, VariantMap const &params) override
::LatticeWalberla const & get_lattice() const override
void do_construct(VariantMap const &params) override
std::optional< Utils::Vector3d > FluxBoundaryType
std::optional< double > DensityBoundaryType
constexpr bool is_none(Variant const &v)
Definition Variant.hpp:163
constexpr bool is_type(Variant const &v)
Check is a Variant holds a specific type.
Definition Variant.hpp:159
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
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:148
Recursive variant implementation.
Definition Variant.hpp:84
static Variant serialize(std::vector< T > const &values)
static std::vector< T > deserialize(Variant const &variant)