ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LeesEdwards.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-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
22#include "Protocol.hpp"
23
24#include "core/BoxGeometry.hpp"
27
31
32#include <memory>
33#include <stdexcept>
34
35namespace ScriptInterface {
36namespace LeesEdwards {
37
38class LeesEdwards : public AutoParameters<LeesEdwards, System::Leaf> {
39 std::shared_ptr<::LeesEdwards::LeesEdwards> m_lees_edwards;
40 std::shared_ptr<Protocol> m_protocol;
41 std::unique_ptr<VariantMap> m_params;
42
43public:
46 {{"protocol",
47 [this](Variant const &value) {
48 if (is_none(value)) {
49 auto const &system = get_system();
50 context()->parallel_try_catch([&system]() {
51 auto constexpr invalid_dir = LeesEdwardsBC::invalid_dir;
52 system.lb.lebc_sanity_checks(invalid_dir, invalid_dir);
53 });
54 m_protocol = nullptr;
55 system.box_geo->set_lees_edwards_bc(LeesEdwardsBC{});
56 m_lees_edwards->unset_protocol();
57 return;
58 }
59 context()->parallel_try_catch([this]() {
60 auto constexpr invalid_dir = LeesEdwardsBC::invalid_dir;
61 auto const &lebc = get_lebc();
62 if (lebc.shear_direction == invalid_dir or
63 lebc.shear_plane_normal == invalid_dir) {
64 throw std::runtime_error(
65 "Parameters 'shear_plane_normal' and 'shear_direction' "
66 "must be initialized together with 'protocol' on first "
67 "activation via set_boundary_conditions()");
68 }
69 });
70 m_protocol = get_value<std::shared_ptr<Protocol>>(value);
71 m_lees_edwards->set_protocol(m_protocol->protocol());
72 },
73 [this]() {
74 if (m_protocol)
75 return make_variant(m_protocol);
76 return make_variant(none);
77 }},
78 {"shear_velocity", AutoParameter::read_only,
79 [this]() { return get_lebc().shear_velocity; }},
80 {"pos_offset", AutoParameter::read_only,
81 [this]() { return get_lebc().pos_offset; }},
82 {"shear_direction", AutoParameter::read_only,
83 [this]() { return get_shear_name(get_lebc().shear_direction); }},
84 {"shear_plane_normal", AutoParameter::read_only,
85 [this]() { return get_shear_name(get_lebc().shear_plane_normal); }}});
86 }
87
88 Variant do_call_method(std::string const &name,
89 VariantMap const &params) override {
90 if (name == "set_boundary_conditions") {
91 context()->parallel_try_catch([this, &params]() {
92 auto const protocol = params.at("protocol");
93 if (is_none(protocol)) {
94 do_set_parameter("protocol", protocol);
95 return;
96 }
97 // check input arguments
98 m_protocol = get_value<std::shared_ptr<Protocol>>(protocol);
99 auto const shear_direction = get_shear_axis(params, "shear_direction");
100 auto const shear_plane_normal =
101 get_shear_axis(params, "shear_plane_normal");
102 if (shear_plane_normal == shear_direction) {
103 throw std::invalid_argument("Parameters 'shear_direction' and "
104 "'shear_plane_normal' must differ");
105 }
106 auto const &system = get_system();
107 system.lb.lebc_sanity_checks(shear_direction, shear_plane_normal);
108 // update box geometry and cell structure
109 system.box_geo->set_lees_edwards_bc(
110 LeesEdwardsBC{0., 0., shear_direction, shear_plane_normal});
111 m_lees_edwards->set_protocol(m_protocol->protocol());
112 });
113 }
114 return {};
115 }
116
117 void do_construct(VariantMap const &params) override {
118 m_params = std::make_unique<VariantMap>(params);
119 }
120
121private:
122 unsigned int get_shear_axis(VariantMap const &params, std::string name) {
123 auto const value = get_value<std::string>(params, name);
124 if (value == "x") {
125 return 0u;
126 }
127 if (value == "y") {
128 return 1u;
129 }
130 if (value == "z") {
131 return 2u;
132 }
133 throw std::invalid_argument("Parameter '" + name + "' is invalid");
134 }
135
136 Variant get_shear_name(unsigned int axis) {
137 if (axis == 0u) {
138 return {std::string("x")};
139 }
140 if (axis == 1u) {
141 return {std::string("y")};
142 }
143 if (axis == 2u) {
144 return {std::string("z")};
145 }
146 return {none};
147 }
148
149 LeesEdwardsBC const &get_lebc() const {
150 return get_system().box_geo->lees_edwards_bc();
151 }
152
153 void on_bind_system(::System::System &system) override {
154 m_lees_edwards = system.lees_edwards;
155 auto const &params = *m_params;
156 if (not params.empty()) {
157 do_call_method("set_boundary_conditions", params);
158 }
159 m_params.reset();
160 }
161};
162
163} // namespace LeesEdwards
164} // namespace ScriptInterface
float u[3]
Bind parameters in the script interface.
void do_set_parameter(const std::string &name, const Variant &value) final
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Variant do_call_method(std::string const &name, VariantMap const &params) override
void do_construct(VariantMap const &params) override
void on_bind_system(::System::System &system) override
boost::string_ref name() const
Context * context() const
Responsible context.
Main system class.
std::shared_ptr< LeesEdwards::LeesEdwards > lees_edwards
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
Variant make_variant(const T &x)
Make a Variant from argument.
Definition Variant.hpp:90
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:80
bool is_none(Variant const &v)
Definition Variant.hpp:128
constexpr const None none
None-"literal".
Definition Variant.hpp:63
static SteepestDescentParameters params
Currently active steepest descent instance.
static auto constexpr invalid_dir
static constexpr const ReadOnly read_only