OpenLB 1.7
Loading...
Searching...
No Matches
lbSolver.hh
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
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_HH
27#define LBSOLVER_HH
28
29
30#include "lbSolver.h"
31
32
33namespace olb {
34
35template<typename T, typename PARAMETERS, typename LATTICES>
37{
38 if (this->parameters(names::Output()).verbose) {
39 clout << "Start initialization" << std::endl;
40 }
41
42 this->prepareGeometry();
43 // Removes all not needed boundary voxels outside the surface
44 //this->geometry().clean(this->parameters(names::Output()).verbose);
45 // Removes all not needed boundary voxels inside the surface
46 this->geometry().innerClean(this->parameters(names::Output()).verbose);
47 geometry().checkForErrors(this->parameters(names::Output()).verbose);
48 if (this->parameters(names::Output()).verbose) {
49 geometry().getStatistics().print();
50 }
51
52 this->_isInitialized = true;
53
54 if (this->parameters(names::Output()).verbose) {
55 clout << "Finished initialization" << std::endl;
56 }
57}
58
59template<typename T, typename PARAMETERS, typename LATTICES>
61{
62 if (! this->_isInitialized) {
63 this->initialize();
64 }
65 build();
66 computeResults();
67}
68
69template<typename T, typename PARAMETERS, typename LATTICES>
71{
72 if (this->parameters(names::Output()).printLogConverter) {
73 writeLogConverter();
74 }
75
76 _timer = std::make_unique<util::Timer<BaseType<T>>>(
77 converter().getLatticeTime(this->parameters(names::Simulation()).maxTime),
78 _sGeometry->getStatistics().getNvoxel(),
79 1); // 1 for printing a short timer summary
80
81 if constexpr (isStationary){
82 for (unsigned i = 0; i < getNumberStationaryLattices(); ++i) {
83 _convergenceCheck[i] = std::make_unique<util::ValueTracer<T>>(
84 converter().getLatticeTime(this->parameters(names::Stationarity()).physInterval[i]),
85 this->parameters(names::Stationarity()).epsilon[i]);
86 }
87 }
88
89 _itBoundaryUpdate = util::max(
90 converter().getLatticeTime(this->parameters(names::Simulation()).physBoundaryValueUpdateTime),
91 (unsigned long) {1});
92 _itCheckStability = util::max(
93 converter().getLatticeTime(this->parameters(names::Simulation()).physTimeStabilityCheck),
94 (unsigned long) {1});
95
96 renewLattices();
97}
98
99template<typename T, typename PARAMETERS, typename LATTICES>
101{
102 if (this->parameters(names::Output()).verbose) {
103 clout << "Start prepareSimulation" << std::endl;
104 }
105
106 this->_iT = 0;
107
108 build();
109
110 setInitialValues();
111
112 meta::tuple_for_each(_sLattices, [](auto& lattice){
113 lattice->initialize();
114 lattice->getStatistics().reset();
115 lattice->getStatistics().initialize();
116 });
117
118
119 if constexpr (outputVTK) {
120 if (this->parameters(names::VisualizationVTK()).output != "off") {
121 prepareVTK();
122 }
123 }
124
125 if (this->parameters(names::Output()).verbose) {
126 clout << "Finished prepareSimulation" << std::endl;
127 clout << "Start collide-and-stream loop" << std::endl;
128 }
129 _timer->start();
130}
131
132
133template<typename T, typename PARAMETERS, typename LATTICES>
135{
136 if (iT % _itBoundaryUpdate == 0) {
137 setBoundaryValues(iT);
138 }
139
140 meta::tuple_for_each(_sLattices, [](auto& lattice) {
141 // TODO: dispose also communication of fields
142 lattice->communicate();
143 });
144
145
146 if constexpr (outputVTK) {
147 if ( this->parameters(names::VisualizationVTK()).printOutput == olb::parameters::OutputPlot<T>::pO_intervals
148 && iT % converter().getLatticeTime(this->parameters(names::VisualizationVTK()).saveTime) == 0 ) {
149 writeVTK(iT);
150 }
151 }
152 if constexpr (outputImages) {
154 && iT % converter().getLatticeTime(this->parameters(names::VisualizationImages()).saveTime) == 0 ) {
155 writeImages(iT);
156 }
157 }
158 if constexpr (outputGnuplot) {
160 && iT % converter().getLatticeTime(this->parameters(names::VisualizationGnuplot()).saveTime) == 0 ) {
161 writeGnuplot(iT);
162 }
163 }
164 if ( this->parameters(names::Output()).verbose
165 && iT % converter().getLatticeTime(this->parameters(names::Output()).logT) == 0) {
166 printLog(iT);
167 }
168 computeResults(iT);
169 getResults(iT);
170
171 meta::tuple_for_each(_sLattices, [](auto& lattice){
172 lattice->collideAndStream();
173 lattice->executeCoupling();
174 });
175
176 if (this->parameters(names::Simulation()).pressureFilter) {
177 // TODO: allow choosing the lattices here
178 std::get<0>(_sLattices)->stripeOffDensityOffset(std::get<0>(_sLattices)->getStatistics().getAverageRho() - T(1));
179 }
180
181 if constexpr (isStationary){
182 unsigned counter = 0;
183 using parameters_t = typename PARAMETERS::template value<names::Stationarity>;
184 parameters_t::stat_lattices::for_each( [&](auto Id) {
185 // using Name = typename decltype(Id)::type;
186 if (this->parameters(names::Stationarity()).convergenceType[counter] == parameters_t::MaxLatticeVelocity){
187 _convergenceCheck[counter]->takeValue( this->lattice(Id).getStatistics().getMaxU());
188 }
189 else if (this->parameters(names::Stationarity()).convergenceType[counter] == parameters_t::AverageEnergy){
190 _convergenceCheck[counter]->takeValue( this->lattice(Id).getStatistics().getAverageEnergy() );
191 }
192 else if (this->parameters(names::Stationarity()).convergenceType[counter] == parameters_t::AverageRho){
193 _convergenceCheck[counter]->takeValue( this->lattice(Id).getStatistics().getAverageRho());
194 }
195 else {
196 throw std::invalid_argument("Convergence type is not supported.\n");
197 }
198 counter++;
199 });
200 }
201
202 if (iT % _itCheckStability == 0) {
203 checkStability(iT);
204 }
205}
206
207template<typename T, typename PARAMETERS, typename LATTICES>
209{
210 meta::tuple_for_each(_sLattices, [](auto& lattice) {
211 // TODO: dispose also communication of fields...
212 lattice->communicate();
213 });
214
215 if ( this->parameters(names::Output()).verbose ) {
216 printLog(this->_iT);
217 }
218 if constexpr (outputVTK) {
219 if ( this->parameters(names::VisualizationVTK()).printOutput == olb::parameters::OutputPlot<T>::pO_final) {
220 writeVTK(this->_iT);
221 }
222 }
223 if constexpr (outputImages) {
224 if ( this->parameters(names::VisualizationImages()).printOutput == olb::parameters::OutputPlot<T>::pO_final) {
225 writeImages(this->_iT);
226 }
227 }
228 if constexpr (outputGnuplot) {
229 if ( this->parameters(names::VisualizationGnuplot()).printOutput == olb::parameters::OutputPlot<T>::pO_final) {
230 writeGnuplot(this->_iT);
231 }
232 }
233 getResults(this->_iT);
234
235 _timer->stop();
236 if (this->parameters(names::Output()).verbose) {
237 clout << "Finished collide-and-stream loop" << std::endl;
238 clout << "Start postprocessing" << std::endl;
239 }
240 computeResults();
241
242 if (this->parameters(names::Output()).verbose) {
243 _timer->printSummary();
244 }
245
246 // required to finish output tasks during use of gpu
247 meta::tuple_for_each(_sLattices, [](auto& lattice) {
248 lattice->setProcessingContext(ProcessingContext::Evaluation);
249 });
250
251 if (this->parameters(names::Output()).verbose) {
252 clout << "Finished postprocessing" << std::endl;
253 }
254}
255
256template<typename T, typename PARAMETERS, typename LATTICES>
258{
259 if (iT > this->converter().getLatticeTime(this->parameters(names::Simulation()).maxTime)){
260 return true;
261 }
262
263 if constexpr (isStationary){
264 if (std::all_of(_convergenceCheck.cbegin(), _convergenceCheck.cend(), [](auto& c){
265 return c->hasConverged();
266 } )) {
267 if (this->parameters(names::Output()).verbose) {
268 clout << "Simulation converged." << std::endl;
269 }
270 return true;
271 }
272 }
273
274 return false;
275}
276
277template<typename T, typename PARAMETERS, typename LATTICES>
279{
280 bool result = true;
281 meta::tuple_for_each(_sLattices, [&](auto& lattice){
282 if (lattice->getStatistics().getMaxU() > _boundMaxU) {
283 clout << "PROBLEM uMax=" << lattice->getStatistics().getMaxU() << std::endl;
284 lattice->getStatistics().print(iT, converter().getPhysTime(iT));
285 if (_exitMaxU) {
286 exit(1);
287 }
288 result = false;
289 }
290 });
291 return result;
292}
293
294template<typename T, typename PARAMETERS, typename LATTICES>
295void LbSolver<T,PARAMETERS,LATTICES>::renewLattices()
296{
297 meta::tuple_for_each(_sLattices, [&](auto& lattice){
298 using lattice_type = typename std::remove_reference_t<decltype(lattice)>::element_type;
299 lattice = std::make_shared<lattice_type>(geometry());
300 });
301
302 prepareLattices();
303}
304
305template<typename T, typename PARAMETERS, typename LATTICES>
307{
308 using OutputParameters_t = typename PARAMETERS::template value<names::Output>;
309 OutputParameters_t::printLatticeStatistics::for_each( [&](auto type){
310 lattice(type.get()).getStatistics().print(iT, converter().getPhysTime(iT));
311 } );
312
313 _timer->print(iT, this->parameters(names::Output()).timerPrintMode);
314}
315
316template<typename T, typename PARAMETERS, typename LATTICES>
318{
319 converter().print();
320 converter().write(this->parameters(names::Output()).name);
321}
322
323template<typename T, typename PARAMETERS, typename LATTICES>
325{
326 // write the geometric information. Since this does not depend on the
327 // lattice, we may w.l.o.g. work with the first lattice
328 auto& lattice = *std::get<0>(_sLattices);
329 using DESCRIPTOR = typename LATTICES::values_t::template get<0>;
330
331
332 SuperVTMwriter<T,dim> vtmWriter(this->parameters(names::VisualizationVTK()).filename);
333
334
336 SuperLatticeGeometry<T,DESCRIPTOR> geometry(lattice, this->geometry());
337 SuperLatticeCuboid<T,DESCRIPTOR> cuboid(lattice);
338 SuperLatticeRank<T,DESCRIPTOR> rank(lattice);
339 vtmWriter.write( cuboid );
340 vtmWriter.write( geometry );
341 vtmWriter.write( rank );
342 vtmWriter.createMasterFile();
343}
344
345} // namespace olb
346
347#endif
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
void prepareSimulation() override
Set up lattice and initialize fields.
Definition lbSolver.hh:100
void timeStep(std::size_t iT) override
Collide-and-stream + additional computations.
Definition lbSolver.hh:134
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
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
void postSimulation() override
Evaluate results.
Definition lbSolver.hh:208
void initialize() override
Set up geometry.
Definition lbSolver.hh:36
void tuple_for_each(TUPLE &tuple, F &&f)
Apply F to each element of TUPLE.
Definition meta.h:369
cpu::simd::Pack< T > max(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:130
Top level namespace for all of OpenLB.
std::conditional_t< DESCRIPTOR::d==2, SuperLatticeRank2D< T, DESCRIPTOR >, SuperLatticeRank3D< T, DESCRIPTOR > > SuperLatticeRank
Definition aliases.h:138
std::conditional_t< DESCRIPTOR::d==2, SuperLatticeGeometry2D< T, DESCRIPTOR >, SuperLatticeGeometry3D< T, DESCRIPTOR > > SuperLatticeGeometry
Definition aliases.h:118
std::conditional_t< DIM==2, SuperVTMwriter2D< T, OUT_T, W >, SuperVTMwriter3D< T, OUT_T, W > > SuperVTMwriter
Definition aliases.h:108
std::conditional_t< DESCRIPTOR::d==2, SuperLatticeCuboid2D< T, DESCRIPTOR >, SuperLatticeCuboid3D< T, DESCRIPTOR > > SuperLatticeCuboid
Definition aliases.h:128