OpenLB 1.7
Loading...
Searching...
No Matches
lbSolver.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2012-2021 Mathias J. Krause, Benjamin Förster,
4 * Julius Jessberger, Adrian Kummerlaender
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25
26#ifndef LBSOLVER_H
27#define LBSOLVER_H
28
29#include "solverParameters.h"
30#include "utilities/timer.h"
32#include "utilities/typeMap.h"
33
34
35namespace olb {
36
39template <typename T, typename PARAMETERS>
41
42private:
43 mutable OstreamManager clout {std::cout, "BaseSolver"};
44
45public:
46 using t = T;
47 using Parameters_t = PARAMETERS;
49
50protected:
51 bool _isInitialized {false};
52 std::size_t _iT {0};
53 bool _finishedTimeLoop {false};
54
55public:
59
61 virtual void initialize() = 0;
62
64 virtual void preProcess() { }
65
66 void solve()
67 {
68 if (! _isInitialized) {
69 initialize();
70 }
71
73
74 do
75 {
77 ++_iT;
78 } while (! exitCondition(_iT));
79
80 _finishedTimeLoop = true;
82 }
83
85 virtual void postProcess() { }
86
87protected:
89 virtual void prepareSimulation() = 0;
90
92 virtual void timeStep(std::size_t iT) = 0;
93
96 virtual bool exitCondition(std::size_t iT) const = 0;
97
99 virtual void postSimulation() = 0;
100
101public:
103 template <typename KEY>
104 auto& parameters(KEY = KEY()) {
105 return *_parameters.template get<KEY>();
106 }
107 template <typename KEY>
108 auto& parameters(KEY = KEY()) const {
109 return *_parameters.template get<KEY>();
110 }
111 template <typename KEY>
113 return *_parameters.template get<KEY>();
114 }
115 template <typename KEY>
117 return *_parameters.template get<KEY>();
118 }
119};
120
121
125template<
126 typename T,
127 typename PARAMETERS,
128 typename LATTICES
129>
130class LbSolver : public BaseSolver<T,PARAMETERS> {
131
132private:
133 mutable OstreamManager clout {std::cout, "LbSolver"};
134
135protected:
136 static constexpr unsigned dim = LATTICES::values_t::template get<0>::d;
137
138 template<typename... DESCRIPTORS>
139 using SuperLattices = std::tuple<std::shared_ptr<SuperLattice<T,DESCRIPTORS>>...>;
140
141 std::shared_ptr<SuperGeometry<T,dim>> _sGeometry;
142 std::shared_ptr<CuboidGeometry<T,dim>> _cGeometry;
143 std::shared_ptr<LoadBalancer<T>> _loadBalancer;
144
145 typename LATTICES::values_t::template decompose_into<SuperLattices> _sLattices;
146
147 std::unique_ptr<util::Timer<BaseType<T>>> _timer;
148
149 static constexpr bool isStationary = PARAMETERS::keys_t::template contains<names::Stationarity>();
150 static constexpr unsigned getNumberStationaryLattices() {
151 if constexpr (isStationary) {
152 return PARAMETERS::template value<names::Stationarity>::numberOfStationaryLattices;
153 } else {
154 return 0;
155 }
156 __builtin_unreachable();
157 }
158 std::array<std::unique_ptr<util::ValueTracer<T>>,
160
161 static constexpr bool outputGnuplot = PARAMETERS::keys_t::template contains<names::VisualizationGnuplot>();
162 static constexpr bool outputImages = PARAMETERS::keys_t::template contains<names::VisualizationImages>();
163 static constexpr bool outputVTK = PARAMETERS::keys_t::template contains<names::VisualizationVTK>();
164
165 bool _exitMaxU {false};
167 std::size_t _itCheckStability {1};
168 std::size_t _itBoundaryUpdate {1};
169
170public:
173
175 // Allows to return the geometric/ lattice data without solving
176 void buildAndReturn();
177
179 void initialize() override;
180
181protected:
183 void prepareSimulation() override;
184
186 void timeStep(std::size_t iT) override;
187
189 void postSimulation() override;
190
191
193 // Inheritant has to provide this method
194 virtual void prepareGeometry() = 0;
195
197 // Inheritant has to provide this method
198 virtual void prepareLattices() = 0;
199
201 // Inheritant has to provide this method
202 virtual void setInitialValues() = 0;
203
205 // Inheritant has to provide this method
206 virtual void setBoundaryValues(std::size_t iT) = 0;
207
209 // Inheritant may provide this method
210 virtual void getResults(std::size_t iT) { };
211
213 // Inheritant may provide these method
214 // This function is called at every time step
215 virtual void computeResults(std::size_t iT) { };
216 // This function is called after the simulation
217 virtual void computeResults() { };
218
219 virtual bool exitCondition(std::size_t iT) const override;
220
223 virtual bool checkStability(std::size_t iT);
224
225private:
227 void build();
229 void renewLattices();
230
231 // ------------ Output generation -------------------------------------------
232protected:
233 virtual void printLog(std::size_t iT) const;
234
235 virtual void writeLogConverter() const;
236
242 virtual void prepareVTK() const;
243
244 // Inheritant may override this method for vtk output
245 virtual void writeVTK(std::size_t iT) const { };
246
247 // Inheritant may override this method for image output
248 virtual void writeImages(std::size_t iT) const { };
249
250 // Inheritant may override this method for gnuplot output
251 virtual void writeGnuplot(std::size_t iT) const { };
252
253 // ------------ Access to converters and lattices ---------------------------
254 template<typename... ARGS>
255 auto& converter(ARGS&&... args) {
256 using SimulationParameters_t = typename PARAMETERS::template value<names::Simulation>;
257 if constexpr(std::is_invocable_v<decltype(&SimulationParameters_t::converter),ARGS...>) {
258 return this->parameters(names::Simulation()).converter(args...);
259 }
260 else {
261 return *this->parameters(names::Simulation()).converter;
262 }
263 __builtin_unreachable();
264 }
265 template<typename... ARGS>
266 auto& converter(ARGS&&... args) const {
267 using SimulationParameters_t = typename PARAMETERS::template value<names::Simulation>;
268 if constexpr(std::is_invocable_v<decltype(&SimulationParameters_t::converter),ARGS...>) {
269 return this->parameters(names::Simulation()).converter(args...);
270 }
271 else {
272 return *this->parameters(names::Simulation()).converter;
273 }
274 __builtin_unreachable();
275 }
276 template <typename KEY>
277 auto& lattice(KEY = KEY()) {
278 return *std::get<(LATTICES::keys_t::template index<KEY>())>(_sLattices);
279 }
280 template <typename KEY>
281 auto& lattice(KEY = KEY()) const {
282 return *std::get<(LATTICES::keys_t::template index<KEY>())>(_sLattices);
283 }
284 template <typename KEY>
286 return *std::get<(LATTICES::keys_t::template index<KEY>())>(_sLattices);
287 }
288 template <typename KEY>
289 auto& lattice(meta::id<KEY>) const {
290 return *std::get<(LATTICES::keys_t::template index<KEY>())>(_sLattices);
291 }
292 auto& lattice() {
293 static_assert((LATTICES::size == 1), "Lattice name must be provided");
294 return *std::get<0>(_sLattices);
295 }
296 auto& lattice() const {
297 static_assert((LATTICES::size == 1), "Lattice name must be provided");
298 return *std::get<0>(_sLattices);
299 }
300 auto& geometry() {
301 return *_sGeometry;
302 }
303 auto& geometry() const {
304 return *_sGeometry;
305 }
306 /*auto& results() {
307 return *(this->_simulationResults);
308 }
309 auto& results() const {
310 return *(this->_simulationResults);
311 }*/
312};
313
314
316template<typename T, typename SOLVER>
317std::function<T (const std::vector<T>&, unsigned)> getCallable(std::shared_ptr<SOLVER> solver){
318 return [=](const std::vector<T>& control, unsigned optiStep) -> T {
319 solver->parameters(names::Opti()).applyControl(control);
320 solver->parameters(names::OutputOpti()).counterOptiStep = optiStep;
321 solver->solve();
322 return solver->parameters(names::Results()).objective;
323 };
324}
325
327template<typename SOLVER>
328std::function<void ()> doPostProcess(std::shared_ptr<SOLVER> solver){
329 return [=]() {
330 solver->postProcess();
331 };
332}
333
334
335// ------------------ Creator functions for XML interface ---------------------
336
337template <class SOLVER>
338std::shared_ptr<SOLVER> createLbSolver(XMLreader const& xml)
339{
340 using parameters_map = typename SOLVER::BaseSolver::Parameters_t;
341 auto paramsTuple = createParametersTuple<parameters_map>(xml);
342 return std::make_shared<SOLVER>(paramsTuple);
343}
344
345
346} // namespace olb
347
348#endif
BaseSolver implements the solving process of an instationary simulation, consisting of preSimulationT...
Definition lbSolver.h:40
PARAMETERS Parameters_t
Definition lbSolver.h:47
auto & parameters(meta::id< KEY >)
Definition lbSolver.h:112
utilities::TypeIndexedSharedPtrTuple< PARAMETERS > _parameters
Definition lbSolver.h:48
virtual void prepareSimulation()=0
Actions that shall be executed before the time-stepping.
auto & parameters(KEY=KEY())
Access to parameter structs as parameters(KEY())
Definition lbSolver.h:104
std::size_t _iT
Definition lbSolver.h:52
virtual void initialize()=0
Actions that shall be executed once after construction.
virtual void preProcess()
Configurations that take place before solving.
Definition lbSolver.h:64
virtual bool exitCondition(std::size_t iT) const =0
Condition, when to exit the time-stepping loop Returns true if the loop shall be continued.
auto & parameters(meta::id< KEY >) const
Definition lbSolver.h:116
bool _isInitialized
Definition lbSolver.h:51
BaseSolver(utilities::TypeIndexedSharedPtrTuple< PARAMETERS > params)
Definition lbSolver.h:56
virtual void postProcess()
Actions that take place after solving.
Definition lbSolver.h:85
auto & parameters(KEY=KEY()) const
Definition lbSolver.h:108
bool _finishedTimeLoop
Definition lbSolver.h:53
virtual void timeStep(std::size_t iT)=0
Defines what to do in a single time step.
virtual void postSimulation()=0
Actions that shall be executed after the time-stepping.
LbSolver is a generic solver for Lattice-Boltzmann problems.
Definition lbSolver.h:130
void buildAndReturn()
Build geometry, lattice and call computeResults.
Definition lbSolver.hh:60
virtual void printLog(std::size_t iT) const
Definition lbSolver.hh:306
auto & geometry()
Definition lbSolver.h:300
std::tuple< std::shared_ptr< SuperLattice< T, DESCRIPTORS > >... > SuperLattices
Definition lbSolver.h:139
void prepareSimulation() override
Set up lattice and initialize fields.
Definition lbSolver.hh:100
std::shared_ptr< CuboidGeometry< T, dim > > _cGeometry
Definition lbSolver.h:142
LbSolver(utilities::TypeIndexedSharedPtrTuple< PARAMETERS > params)
Definition lbSolver.h:171
BaseType< T > _boundMaxU
Definition lbSolver.h:166
auto & converter(ARGS &&... args)
Definition lbSolver.h:255
static constexpr bool isStationary
Definition lbSolver.h:149
void timeStep(std::size_t iT) override
Collide-and-stream + additional computations.
Definition lbSolver.hh:134
virtual void prepareVTK() const
Write geometric information for vtk output The default version writes geometry, cuboid,...
Definition lbSolver.hh:324
auto & lattice(KEY=KEY()) const
Definition lbSolver.h:281
static constexpr bool outputImages
Definition lbSolver.h:162
auto & lattice() const
Definition lbSolver.h:296
auto & lattice(KEY=KEY())
Definition lbSolver.h:277
virtual void writeLogConverter() const
Definition lbSolver.hh:317
virtual void prepareLattices()=0
Choose dynamics and boundary conditions.
static constexpr unsigned dim
Definition lbSolver.h:136
virtual bool checkStability(std::size_t iT)
check stability: maxU should be <= _boundMaxU for a stable simulation Returns true if this fulfilled
Definition lbSolver.hh:278
std::shared_ptr< SuperGeometry< T, dim > > _sGeometry
Definition lbSolver.h:141
virtual void getResults(std::size_t iT)
Computation of results and output with full flexibility.
Definition lbSolver.h:210
virtual void writeGnuplot(std::size_t iT) const
Definition lbSolver.h:251
auto & converter(ARGS &&... args) const
Definition lbSolver.h:266
auto & lattice()
Definition lbSolver.h:292
auto & lattice(meta::id< KEY >)
Definition lbSolver.h:285
std::shared_ptr< LoadBalancer< T > > _loadBalancer
Definition lbSolver.h:143
virtual bool exitCondition(std::size_t iT) const override
Condition, when to exit the time-stepping loop Returns true if the loop shall be continued.
Definition lbSolver.hh:257
std::array< std::unique_ptr< util::ValueTracer< T > >, getNumberStationaryLattices()> _convergenceCheck
Definition lbSolver.h:159
LATTICES::values_t::template decompose_into< SuperLattices > _sLattices
Definition lbSolver.h:145
std::size_t _itBoundaryUpdate
Definition lbSolver.h:168
virtual void prepareGeometry()=0
Define the geometry.
virtual void computeResults(std::size_t iT)
Perform further computations (compute errors etc.)
Definition lbSolver.h:215
std::unique_ptr< util::Timer< BaseType< T > > > _timer
Definition lbSolver.h:147
auto & lattice(meta::id< KEY >) const
Definition lbSolver.h:289
auto & geometry() const
Definition lbSolver.h:303
void postSimulation() override
Evaluate results.
Definition lbSolver.hh:208
virtual void setBoundaryValues(std::size_t iT)=0
Update fields and boundary values.
virtual void computeResults()
Definition lbSolver.h:217
static constexpr bool outputVTK
Definition lbSolver.h:163
virtual void writeImages(std::size_t iT) const
Definition lbSolver.h:248
void initialize() override
Set up geometry.
Definition lbSolver.hh:36
virtual void writeVTK(std::size_t iT) const
Definition lbSolver.h:245
static constexpr unsigned getNumberStationaryLattices()
Definition lbSolver.h:150
static constexpr bool outputGnuplot
Definition lbSolver.h:161
std::size_t _itCheckStability
Definition lbSolver.h:167
virtual void setInitialValues()=0
Define fields and initialize lattice populations.
class for marking output with some text
Top level namespace for all of OpenLB.
std::function< void()> doPostProcess(std::shared_ptr< SOLVER > solver)
Returns a function that encapsulates the solving process.
Definition lbSolver.h:328
typename util::BaseTypeHelper< T >::type BaseType
Definition baseType.h:59
std::shared_ptr< SOLVER > createLbSolver(XMLreader const &xml)
Definition lbSolver.h:338
std::function< T(const std::vector< T > &, unsigned) getCallable)(std::shared_ptr< SOLVER > solver)
Returns a function that encapsulates the solving process.
Definition lbSolver.h:317
Identity type to pass non-constructible types as value.
Definition meta.h:79
Mapping between KEYs and instances of type VALUEs.
This class allows calculation and display of various time data including remaining runtime data in cp...