ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ek/Solver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 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#include "ek/Implementation.hpp"
23#include "ek/Solver.hpp"
24#include "ek/utils.hpp"
25
26#include "ek/EKNone.hpp"
27#include "ek/EKWalberla.hpp"
28
29#include "system/System.hpp"
30
31#include <utils/Vector.hpp>
32
33#include <cassert>
34#include <cmath>
35#include <limits>
36#include <memory>
37#include <optional>
38#include <stdexcept>
39#include <string>
40#include <tuple>
41#include <variant>
42#include <vector>
43
44namespace EK {
45
46Solver::Solver() { impl = std::make_unique<Implementation>(); }
47
48static auto is_solver_set(std::unique_ptr<Solver::Implementation> const &ptr) {
49 return ptr != nullptr and ptr->solver.has_value();
50}
51
52static void check_solver(std::unique_ptr<Solver::Implementation> const &ptr) {
53 if (not is_solver_set(ptr)) {
54 throw NoEKActive{};
55 }
56}
57
58bool Solver::is_solver_set() const { return EK::is_solver_set(impl); }
59
61 if (impl->solver) {
62 std::visit([this](auto &ptr) { ptr->detach_system(m_system.lock()); },
63 *impl->solver);
64 impl->solver = std::nullopt;
65 }
66}
67
69 return is_solver_set() and
70 std::visit([](auto &ptr) { return ptr->is_ready_for_propagation(); },
71 *impl->solver);
72}
73
75 check_solver(impl);
76 std::visit([](auto &ptr) { ptr->propagate(); }, *impl->solver);
77}
78
80 if (impl->solver) {
81 auto const &system = get_system();
82 std::visit([&](auto &ptr) { ptr->sanity_checks(system); }, *impl->solver);
83 }
84}
85
86void Solver::veto_time_step(double time_step) const {
87 if (impl->solver) {
88 std::visit([=](auto &ptr) { ptr->veto_time_step(time_step); },
89 *impl->solver);
90 }
91}
92
93void Solver::veto_kT(double kT) const {
94 if (impl->solver) {
95 std::visit([=](auto &ptr) { ptr->veto_kT(kT); }, *impl->solver);
96 }
97}
98
100 if (impl->solver) {
101 std::visit([](auto const &ptr) { ptr->veto_boxl_change(); }, *impl->solver);
102 }
103}
104
106 if (impl->solver) {
107 auto &solver = *impl->solver;
108 std::visit([](auto &ptr) { ptr->on_cell_structure_change(); }, solver);
109 }
110}
111
113 if (impl->solver) {
114 std::visit([](auto &ptr) { ptr->on_boxl_change(); }, *impl->solver);
115 }
116}
117
119 if (impl->solver) {
120 std::visit([](auto &ptr) { ptr->on_node_grid_change(); }, *impl->solver);
121 }
122}
123
125 if (impl->solver) {
126 std::visit([](auto &ptr) { ptr->on_timestep_change(); }, *impl->solver);
127 }
128}
129
131 if (impl->solver) {
132 std::visit([](auto &ptr) { ptr->on_temperature_change(); }, *impl->solver);
133 }
134}
135
136bool Solver::is_gpu() const {
137 check_solver(impl);
138 return std::visit([](auto &ptr) { return ptr->is_gpu(); }, *impl->solver);
139}
140
141double Solver::get_tau() const {
142 check_solver(impl);
143 return std::visit([](auto &ptr) { return ptr->get_tau(); }, *impl->solver);
144}
145
146template <> void Solver::set<EKNone>(std::shared_ptr<EKNone> ek_instance) {
147 assert(impl);
148 assert(not impl->solver.has_value());
149 ek_instance->bind_system(m_system.lock());
150 impl->solver = ek_instance;
151}
152
153#ifdef ESPRESSO_WALBERLA
154template <>
155void Solver::set<EKWalberla>(std::shared_ptr<EKWalberla> ek_instance) {
156 assert(impl);
157 assert(not impl->solver.has_value());
158 auto const &system = get_system();
159 ek_instance->bind_system(m_system.lock());
160 ek_instance->sanity_checks(system);
161 impl->solver = ek_instance;
162}
163#endif // ESPRESSO_WALBERLA
164
165} // namespace EK
Vector implementation and trait types for boost qvm interoperability.
std::weak_ptr< System > m_system
static auto is_solver_set(std::unique_ptr< Solver::Implementation > const &ptr)
Definition ek/Solver.cpp:48
static void check_solver(std::unique_ptr< Solver::Implementation > const &ptr)
Definition ek/Solver.cpp:52
void veto_time_step(double time_step) const
Check if a MD time step is compatible with the EK tau.
Definition ek/Solver.cpp:86
void on_boxl_change()
void on_node_grid_change()
void propagate()
Propagate the EK species.
Definition ek/Solver.cpp:74
void sanity_checks() const
Perform EK parameter checks.
Definition ek/Solver.cpp:79
void on_cell_structure_change()
void veto_boxl_change() const
Definition ek/Solver.cpp:99
bool is_gpu() const
bool is_ready_for_propagation() const
Return true if an EK solver can be propagated.
Definition ek/Solver.cpp:68
double get_tau() const
Get the EK time step.
void on_timestep_change()
void on_temperature_change()
void veto_kT(double kT) const
Check if a thermostat is compatible with the EK temperature.
Definition ek/Solver.cpp:93
void reset()
Remove the EK solver.
Definition ek/Solver.cpp:60
bool is_solver_set() const
Return true if an EK solver is active.
Definition ek/Solver.cpp:58