ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
fields.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 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
26
28
29#include <utils/Vector.hpp>
30
31#include <boost/multi_array.hpp>
32
33#include <algorithm>
34#include <cstddef>
35#include <stdexcept>
36#include <string>
37#include <vector>
38
39namespace ScriptInterface {
40namespace Constraints {
41
42namespace detail {
43using namespace ::FieldCoupling::Fields;
44
45/**
46 * @brief ScriptInterface implementations for the
47 * various fields provided.
48 *
49 * These are separated from the Constraints because
50 * they can be reused together with the fields themselves.
51 */
52template <typename Field> struct field_params_impl;
53
54template <typename T, std::size_t codim>
55struct field_params_impl<Constant<T, codim>> {
56 static Constant<T, codim> make(const VariantMap &params) {
57 return Constant<T, codim>{
58 get_value<typename Constant<T, codim>::value_type>(params, "value")};
59 }
60 template <typename This>
61 static std::vector<AutoParameter> params(const This &this_) {
62 return {{"value", AutoParameter::read_only,
63 [this_]() { return this_().value(); }}};
64 }
65};
66
67template <typename T, std::size_t codim>
68struct field_params_impl<AffineMap<T, codim>> {
69 using jacobian_type = typename AffineMap<T, codim>::jacobian_type;
70 using value_type = typename AffineMap<T, codim>::value_type;
71
72 static AffineMap<T, codim> make(const VariantMap &params) {
73 return AffineMap<T, codim>{
74 get_value<jacobian_type>(params, "A"),
75 get_value_or<value_type>(params, "b", value_type{})};
76 }
77
78 template <typename This>
79 static std::vector<AutoParameter> params(const This &this_) {
80 return {{"A", AutoParameter::read_only, [this_]() { return this_().A(); }},
81 {"b", AutoParameter::read_only, [this_]() { return this_().b(); }}};
82 }
83};
84
85template <typename T, std::size_t codim>
86struct field_params_impl<PlaneWave<T, codim>> {
87 using jacobian_type = typename PlaneWave<T, codim>::jacobian_type;
88 using value_type = typename PlaneWave<T, codim>::value_type;
89
90 static PlaneWave<T, codim> make(const VariantMap &params) {
91 return PlaneWave<T, codim>{get_value<value_type>(params, "amplitude"),
92 get_value<value_type>(params, "wave_vector"),
93 get_value<T>(params, "frequency"),
94 get_value_or<T>(params, "phase", 0.)};
95 }
96
97 template <typename This>
98 static std::vector<AutoParameter> params(const This &this_) {
99 return {{"amplitude", AutoParameter::read_only,
100 [this_]() { return this_().amplitude(); }},
101 {"wave_vector", AutoParameter::read_only,
102 [this_]() { return this_().k(); }},
103 {"frequency", AutoParameter::read_only,
104 [this_]() { return this_().omega(); }},
105 {"phase", AutoParameter::read_only,
106 [this_]() { return this_().phase(); }}};
107 }
108};
109
110template <typename T, std::size_t codim>
111struct field_params_impl<Interpolated<T, codim>> {
112 static Interpolated<T, codim> make(const VariantMap &params) {
113 auto const field_data =
114 get_value<std::vector<double>>(params, "_field_data");
115 auto const field_shape = get_value<Utils::Vector3i>(params, "_field_shape");
116 auto const field_codim = get_value<int>(params, "_field_codim");
117
118 if (field_codim != codim) {
119 throw std::runtime_error(
120 "Field data has the wrong dimensions, needs to be [n, m, o, " +
121 std::to_string(codim) + ']');
122 }
123
124 if (*std::min_element(field_shape.begin(), field_shape.end()) < 1) {
125 throw std::runtime_error("Field is too small, needs to be at least "
126 "one in all directions.");
127 }
128
129 auto const grid_spacing =
130 get_value<Utils::Vector3d>(params, "grid_spacing");
131 auto const origin = -0.5 * grid_spacing;
132
133 using field_data_type =
135 auto array_ref = boost::const_multi_array_ref<field_data_type, 3>(
136 reinterpret_cast<const field_data_type *>(field_data.data()),
137 field_shape);
138
139 return Interpolated<T, codim>{array_ref, grid_spacing, origin};
140 }
141
142 template <typename This>
143 static std::vector<AutoParameter> params(const This &this_) {
144 return {{"grid_spacing", AutoParameter::read_only,
145 [this_]() { return this_().grid_spacing(); }},
146 {"origin", AutoParameter::read_only,
147 [this_]() { return this_().origin(); }},
148 {"_field_shape", AutoParameter::read_only,
149 [this_]() { return this_().shape(); }},
150 {"_field_codim", AutoParameter::read_only,
151 []() { return static_cast<int>(codim); }},
152 {"_field_data", AutoParameter::read_only,
153 [this_]() { return this_().field_data_flat(); }}};
154 }
155};
156
157template <typename Field, typename T>
158static std::vector<AutoParameter> field_parameters(const T &this_) {
159 return field_params_impl<Field>::params(this_);
160}
161
162template <typename Field> Field make_field(const VariantMap &params) {
163 return field_params_impl<Field>::make(params);
164}
165} // namespace detail
166} // namespace Constraints
167} // namespace ScriptInterface
Vector implementation and trait types for boost qvm interoperability.
detail::jacobian_type< T, codim > jacobian_type
Definition AffineMap.hpp:39
typename Utils::decay_to_scalar< Utils::Vector< T, codim > >::type value_type
Definition AffineMap.hpp:38
typename Utils::decay_to_scalar< Utils::Vector< T, codim > >::type value_type
Definition PlaneWave.hpp:43
detail::jacobian_type< T, codim > jacobian_type
Definition PlaneWave.hpp:44
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only
Meta function to turns a Vector<1, T> into T.
Definition Vector.hpp:478