ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKSpeciesNode.cpp
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#include "config/config.hpp"
21
22#ifdef WALBERLA
23
24#include "EKSpeciesNode.hpp"
25
26#include "LatticeIndices.hpp"
27
29
30#include <utils/Vector.hpp>
32
33#include <boost/mpi/collectives/all_reduce.hpp>
34#include <boost/serialization/vector.hpp>
35
36#include <cassert>
37#include <memory>
38#include <optional>
39#include <stdexcept>
40#include <string>
41
43
44static bool is_boundary_all_reduce(boost::mpi::communicator const &comm,
45 std::optional<bool> const &is_boundary) {
46 return boost::mpi::all_reduce(comm, is_boundary ? *is_boundary : false,
47 std::logical_or<>());
48}
49
50Variant EKSpeciesNode::do_call_method(std::string const &name,
51 VariantMap const &params) {
52 if (name == "override_index") {
53 // this hidden feature is used to iterate an EK slice without
54 // rebuilding a EKSpeciesNode for each node in the slice
55 auto const index = get_value<Utils::Vector3i>(params, "index");
56 if (not is_index_valid(index, m_grid_size)) {
57 return 1;
58 }
59 m_index = index;
60 return 0;
61 }
62 if (name == "set_density") {
63 auto const dens = get_value<double>(params, "value");
64 m_ek_species->set_node_density(m_index, dens * m_conv_dens);
65 m_ek_species->ghost_communication();
66 return {};
67 }
68 if (name == "get_density") {
69 auto const result = m_ek_species->get_node_density(m_index);
70 return Utils::Mpi::reduce_optional(context()->get_comm(), result) /
71 m_conv_dens;
72 }
73 if (name == "get_is_boundary") {
74 auto const result = m_ek_species->get_node_is_boundary(m_index);
75 return Utils::Mpi::reduce_optional(context()->get_comm(), result);
76 }
77 if (name == "get_node_density_at_boundary") {
78 auto const boundary_opt =
79 m_ek_species->get_node_is_density_boundary(m_index);
80 if (is_boundary_all_reduce(context()->get_comm(), boundary_opt)) {
81 auto const result = m_ek_species->get_node_density_at_boundary(m_index);
82 return Utils::Mpi::reduce_optional(context()->get_comm(), result) /
83 m_conv_dens;
84 }
85 return Variant{None{}};
86 }
87 if (name == "set_node_density_at_boundary") {
88 if (is_none(params.at("value"))) {
89 m_ek_species->remove_node_from_density_boundary(m_index);
90 } else {
91 auto const dens = get_value<double>(params, "value") * m_conv_dens;
92 m_ek_species->set_node_density_boundary(m_index, dens);
93 }
94 return {};
95 }
96 if (name == "get_node_flux_at_boundary") {
97 auto const boundary_opt = m_ek_species->get_node_is_flux_boundary(m_index);
98 if (is_boundary_all_reduce(context()->get_comm(), boundary_opt)) {
99 auto const result = m_ek_species->get_node_flux_at_boundary(m_index);
100 return Utils::Mpi::reduce_optional(context()->get_comm(), result) /
101 m_conv_flux;
102 }
103 return Variant{None{}};
104 }
105 if (name == "set_node_flux_at_boundary") {
106 if (is_none(params.at("value"))) {
107 m_ek_species->remove_node_from_flux_boundary(m_index);
108 } else {
109 auto const flux =
110 get_value<Utils::Vector3d>(params, "value") * m_conv_flux;
111 m_ek_species->set_node_flux_boundary(m_index, flux);
112 }
113 return {};
114 }
115
116 return {};
117}
118
119} // namespace ScriptInterface::walberla
120
121#endif // WALBERLA
Vector implementation and trait types for boost qvm interoperability.
bool is_index_valid(Utils::Vector3i const &index, Utils::Vector3i const &shape) const
Type to indicate no value in Variant.
boost::string_ref name() const
Context * context() const
Responsible context.
Variant do_call_method(std::string const &name, VariantMap const &params) override
This file contains the defaults for ESPResSo.
static bool is_boundary_all_reduce(boost::mpi::communicator const &comm, std::optional< bool > const &is_boundary)
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
bool is_none(Variant const &v)
Definition Variant.hpp:115
T reduce_optional(boost::mpi::communicator const &comm, std::optional< T > const &result)
Reduce an optional on the head node.
static SteepestDescentParameters params
Currently active steepest descent instance.