OpenLB 1.8.1
Loading...
Searching...
No Matches
unitConverter.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2017 Max Gaedtke, Albert Mink
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
28#ifndef UNITCONVERTER_H
29#define UNITCONVERTER_H
30
31
32#include "utilities/omath.h"
33#include "io/ostreamManager.h"
34#include "core/util.h"
35#include "io/xmlReader.h"
36
37#include "descriptor/fields.h"
38
39// known design issues
40// 1. How can we prevent abuse of constructur by mixing up parameters?
41// 2. physical problems may have different names for viscosity, e.g. diffusity, temperature conductivity
42// 4. Feedback about stability or comment the chosen discretization
43// 5. Explain why Desctiptor as template
44// 6. Is it worth to introduce invConversionDensity to avoid division
45
46
47namespace olb {
48
49namespace fields {
50
51namespace converter {
52
54 template <typename T, typename DESCRIPTOR,typename FIELD>
55 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
56 return value >= 0;
57 }
58};
60 template <typename T, typename DESCRIPTOR,typename FIELD>
61 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
62 return value >= 0;
63 }
64};
66 template <typename T, typename DESCRIPTOR,typename FIELD>
67 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
68 return value > 0;
69 }
70 };
72 template <typename T, typename DESCRIPTOR,typename FIELD>
73 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
74 return value > 0;
75 }
76};
78 template <typename T, typename DESCRIPTOR,typename FIELD>
79 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
80 return value > 0;
81 }
82};
84 template <typename T, typename DESCRIPTOR,typename FIELD>
85 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
86 return value >= 0;
87 }
88};
89
91 template <typename T, typename DESCRIPTOR,typename FIELD>
92 static constexpr auto isValid(FieldD<T,DESCRIPTOR,FIELD> value) {
93 return value >= 0;
94 }
95};
96
98
99}
100
101}
102
103
105 virtual ~UnitConverterBase() = default;
106
107 virtual void print() const = 0;
108 virtual void print(std::ostream& fout) const = 0;
109 virtual void write(std::string const& fileName = "unitConverter") const = 0;
110};
111
112
140template <typename T, typename DESCRIPTOR>
142public:
152 constexpr UnitConverter( T physDeltaX, T physDeltaT, T charPhysLength, T charPhysVelocity,
153 T physViscosity, T physDensity, T charPhysPressure = 0 )
154 : _conversionLength(physDeltaX),
155 _conversionTime(physDeltaT),
157 _conversionDensity(physDensity),
163 _charPhysLength(charPhysLength),
164 _charPhysVelocity(charPhysVelocity),
165 _physViscosity(physViscosity),
166 _physDensity(physDensity),
167 _charPhysPressure(charPhysPressure),
169 _latticeRelaxationTime( (_physViscosity / _conversionViscosity * descriptors::invCs2<T,DESCRIPTOR>()) + 0.5 ),
171 clout(std::cout,"UnitConverter")
172 {
173 }
174
175 virtual ~UnitConverter() = default;
176
178 constexpr int getResolution( ) const
179 {
180 return _resolution;
181 }
183 constexpr T getLatticeRelaxationTime( ) const
184 {
186 }
188 constexpr T getLatticeRelaxationFrequency( ) const
189 {
190 return 1./_latticeRelaxationTime;
191 }
193 template <typename DESCRIPTOR_>
194 constexpr T getLatticeRelaxationFrequencyFromDiffusivity( const T physDiffusivity ) const
195 {
196 return 1.0 / ( physDiffusivity / _conversionViscosity * descriptors::invCs2<T,DESCRIPTOR_>() + 0.5 );
197 }
199 constexpr T getCharPhysLength( ) const
200 {
201 return _charPhysLength;
202 }
204 constexpr T getCharPhysVelocity( ) const
205 {
206 return _charPhysVelocity;
207 }
209 constexpr T getCharLatticeVelocity( ) const
210 {
212 }
214 constexpr T getCharCFLnumber( ) const
215 {
217 }
219 constexpr T getPhysViscosity( ) const
220 {
221 return _physViscosity;
222 }
224 constexpr T getPhysDensity( ) const
225 {
226 return _physDensity;
227 }
229 constexpr T getCharPhysPressure( ) const
230 {
231 return _charPhysPressure;
232 }
234 constexpr T getReynoldsNumber( ) const
235 {
237 }
239 constexpr T getMachNumber( ) const
240 {
242 }
244 constexpr T getKnudsenNumber( ) const
245 {
246 // This calculates the lattice Knudsen number.
247 // See e.g. (7.22) in "The Lattice Boltzmann Method: Principles and Practice" [kruger2017lattice].
248 return getMachNumber() / getReynoldsNumber();
249 }
251 constexpr T getPhysLength( int latticeLength ) const
252 {
253 return _conversionLength * latticeLength;
254 }
256 constexpr int getLatticeLength( T physLength ) const
257 {
258 return int( physLength / _conversionLength + 0.5 );
259 }
261 constexpr T getConversionFactorLength() const
262 {
263 return _conversionLength;
264 }
266 constexpr T getPhysDeltaX() const
267 {
268 return _conversionLength;
269 }
270
272 constexpr T getPhysTime( size_t latticeTime ) const
273 {
274 return _conversionTime * latticeTime;
275 }
277 constexpr size_t getLatticeTime( T physTime ) const
278 {
279 return size_t(physTime / _conversionTime + 0.5);
280 }
282 constexpr T getConversionFactorTime() const
283 {
284 return _conversionTime;
285 }
287 constexpr T getPhysDeltaT() const
288 {
289 return _conversionTime;
290 }
291
293 constexpr T getPhysVelocity( T latticeVelocity ) const
294 {
295 return _conversionVelocity * latticeVelocity;
296 }
298 constexpr T getLatticeVelocity( T physVelocity ) const
299 {
300 return physVelocity / _conversionVelocity;
301 }
303 template <unsigned D>
305 {
306 return Vector<T,D>([&](std::size_t iD) -> T {
307 return this->getLatticeVelocity(physU[iD]);
308 });
309 }
311 constexpr T getConversionFactorVelocity() const
312 {
313 return _conversionVelocity;
314 }
315
317 constexpr T getPhysDensity( T latticeDensity ) const
318 {
319 return _conversionDensity * latticeDensity;
320 }
322 constexpr T getLatticeDensity( T physDensity ) const
323 {
324 return physDensity / _conversionDensity;
325 }
326 constexpr T getLatticeDensityFromPhysPressure( T physPressure ) const
327 {
329 }
331 constexpr T getConversionFactorDensity() const
332 {
333 return _conversionDensity;
334 }
335
337 constexpr T getPhysMass( T latticeMass ) const
338 {
339 return _conversionMass * latticeMass;
340 }
342 constexpr T getLatticeMass( T physMass ) const
343 {
344 return physMass / _conversionMass;
345 }
347 constexpr T getConversionFactorMass() const
348 {
349 return _conversionMass;
350 }
351
353 constexpr T getPhysViscosity( T latticeViscosity ) const
354 {
355 return _conversionViscosity * latticeViscosity;
356 }
358 constexpr T getLatticeViscosity( ) const
359 {
361 }
364 {
366 }
367
369 constexpr T getPhysForce( T latticeForce ) const
370 {
371 return _conversionForce * latticeForce;
372 }
374 template <unsigned D>
375 constexpr Vector<T,D> getPhysForce(Vector<T,D> latticeForce) const
376 {
377 return Vector<T,D>([&](std::size_t iD) -> T {
378 return this->getPhysForce(latticeForce[iD]);
379 });
380 }
382 constexpr T getLatticeForce( T physForce ) const
383 {
384 return physForce / _conversionForce;
385 }
387 constexpr T getConversionFactorForce() const
388 {
389 return _conversionForce;
390 }
391
393 constexpr T getPhysTorque ( T latticeTorque ) const
394 {
395 return _conversionTorque * latticeTorque;
396 }
398 template <unsigned D>
399 constexpr Vector<T,D> getPhysTorque(Vector<T,D> latticeTorque) const
400 {
401 return Vector<T,D>([&](std::size_t iD) -> T {
402 return this->getPhysTorque(latticeTorque[iD]);
403 });
404 }
406 constexpr T getLatticeTorque( T physTorque ) const
407 {
408 return physTorque / _conversionTorque;
409 }
411 constexpr T getConversionFactorTorque() const
412 {
413 return _conversionTorque;
414 }
415
417 constexpr T getPhysPressure( T latticePressure ) const
418 {
419 return _conversionPressure * latticePressure + _charPhysPressure;
420 }
422 constexpr T getLatticePressure( T physPressure ) const
423 {
424 return ( physPressure - _charPhysPressure ) / _conversionPressure;
425 }
427 constexpr T getConversionFactorPressure() const
428 {
429 return _conversionPressure;
430 }
432 virtual void print() const;
433 void print(std::ostream& fout) const;
434
435 void write(std::string const& fileName = "unitConverter") const;
436
437protected:
438 // conversion factors
439 const T _conversionLength; // m
440 const T _conversionTime; // s
441 const T _conversionVelocity; // m / s
442 const T _conversionDensity; // kg / m^3
443 const T _conversionMass; // kg
444 const T _conversionViscosity; // m^2 / s
445 const T _conversionForce; // kg m / s^2
446 const T _conversionTorque; // kg m^2 / s^2
447 const T _conversionPressure; // kg / m s^2
448
449 // physical units, e.g characteristic or reference values
450 const T _charPhysLength; // m
451 const T _charPhysVelocity; // m / s
452 const T _physViscosity; // m^2 / s
453 const T _physDensity; // kg / m^3
454 const T _charPhysPressure; // kg / m s^2
455
456 // lattice units, discretization parameters
457 const size_t _resolution;
460
461private:
462 mutable OstreamManager clout;
463};
464
465template <typename T, typename DESCRIPTOR>
467 const UnitConverter<T,DESCRIPTOR>& converter,
468 unsigned scale = 2)
469{
470 const T refinementFactor = T{1} / scale;
472 converter.getPhysDeltaX() * refinementFactor,
473 converter.getPhysDeltaT() * refinementFactor,
474 converter.getCharPhysLength(),
475 converter.getCharPhysVelocity(),
476 converter.getPhysViscosity(),
477 converter.getPhysDensity(),
478 converter.getCharPhysPressure()
479 );
480}
481
483template <typename T, typename DESCRIPTOR>
484UnitConverter<T, DESCRIPTOR>* createUnitConverter(XMLreader const& params);
485
486template <typename T, typename DESCRIPTOR>
488public:
490 size_t resolution,
491 T latticeRelaxationTime,
492 T charPhysLength,
493 T charPhysVelocity,
494 T physViscosity,
495 T physDensity,
496 T charPhysPressure = 0) : UnitConverter<T, DESCRIPTOR>(
497 (charPhysLength/resolution),
498 (latticeRelaxationTime - 0.5) / descriptors::invCs2<T,DESCRIPTOR>() * util::pow((charPhysLength/resolution),2) / physViscosity,
499 charPhysLength,
500 charPhysVelocity,
501 physViscosity,
502 physDensity,
503 charPhysPressure)
504 { }
505};
506
507template <typename T, typename DESCRIPTOR>
509public:
511 size_t resolution,
512 T charLatticeVelocity,
513 T charPhysLength,
514 T charPhysVelocity,
515 T physViscosity,
516 T physDensity,
517 T charPhysPressure = 0) : UnitConverter<T, DESCRIPTOR>(
518 (charPhysLength/resolution),
519 (charLatticeVelocity / charPhysVelocity * charPhysLength / resolution),
520 charPhysLength,
521 charPhysVelocity,
522 physViscosity,
523 physDensity,
524 charPhysPressure)
525 { }
526};
527
528template <typename T, typename DESCRIPTOR>
530public:
532 T latticeRelaxationTime,
533 T charLatticeVelocity,
534 T charPhysLength,
535 T charPhysVelocity,
536 T physViscosity,
537 T physDensity,
538 T charPhysPressure = 0) : UnitConverter<T, DESCRIPTOR>(
539 (physViscosity * charLatticeVelocity / charPhysVelocity * descriptors::invCs2<T,DESCRIPTOR>() / (latticeRelaxationTime - 0.5)),
540 (physViscosity * charLatticeVelocity * charLatticeVelocity / charPhysVelocity / charPhysVelocity * descriptors::invCs2<T,DESCRIPTOR>() / (latticeRelaxationTime - 0.5)),
541 charPhysLength,
542 charPhysVelocity,
543 physViscosity,
544 physDensity,
545 charPhysPressure)
546 {
547 }
548};
549
550} // namespace olb
551
552#include "thermalUnitConverter.h"
554#endif
class for marking output with some text
constexpr UnitConverterFromRelaxationTimeAndLatticeVelocity(T latticeRelaxationTime, T charLatticeVelocity, T charPhysLength, T charPhysVelocity, T physViscosity, T physDensity, T charPhysPressure=0)
constexpr UnitConverterFromResolutionAndLatticeVelocity(size_t resolution, T charLatticeVelocity, T charPhysLength, T charPhysVelocity, T physViscosity, T physDensity, T charPhysPressure=0)
constexpr UnitConverterFromResolutionAndRelaxationTime(size_t resolution, T latticeRelaxationTime, T charPhysLength, T charPhysVelocity, T physViscosity, T physDensity, T charPhysPressure=0)
Conversion between physical and lattice units, as well as discretization.
constexpr T getConversionFactorViscosity() const
access (read-only) to private member variable
void write(std::string const &fileName="unitConverter") const
constexpr T getLatticeViscosity() const
conversion from physical to lattice viscosity
constexpr T getLatticeForce(T physForce) const
conversion from physical to lattice force
constexpr T getCharPhysLength() const
return characteristic length in physical units
constexpr T getLatticeTorque(T physTorque) const
conversion from physical to lattice torque
constexpr T getMachNumber() const
return Mach number
constexpr T getPhysViscosity(T latticeViscosity) const
conversion from lattice to physical viscosity
const size_t _resolution
constexpr int getResolution() const
return resolution
constexpr T getConversionFactorDensity() const
access (read-only) to private member variable
constexpr T getPhysDensity() const
return density in physical units
constexpr T getLatticeRelaxationFrequency() const
return relaxation frequency in lattice units
constexpr T getConversionFactorLength() const
access (read-only) to private member variable
constexpr T getCharLatticeVelocity() const
return characteristic velocity in lattice units
constexpr T getLatticeDensity(T physDensity) const
conversion from physical to lattice density
constexpr size_t getLatticeTime(T physTime) const
conversion from physical to lattice time
constexpr T getLatticeDensityFromPhysPressure(T physPressure) const
constexpr T getPhysDensity(T latticeDensity) const
conversion from lattice to physical density
constexpr T getConversionFactorVelocity() const
access (read-only) to private member variable
constexpr T getPhysTorque(T latticeTorque) const
conversion from lattice to physical torque
constexpr T getPhysMass(T latticeMass) const
conversion from lattice to physical mass
constexpr T getPhysVelocity(T latticeVelocity) const
conversion from lattice to physical velocity
constexpr T getLatticePressure(T physPressure) const
conversion from physical to lattice pressure
constexpr T getConversionFactorMass() const
access (read-only) to private member variable
constexpr T getPhysViscosity() const
return viscosity in physical units
constexpr T getConversionFactorForce() const
access (read-only) to private member variable
constexpr Vector< T, D > getLatticeVelocity(Vector< T, D > physU) const
conversion from physical to lattice velocity
constexpr T getConversionFactorPressure() const
access (read-only) to private member variable
constexpr T getLatticeVelocity(T physVelocity) const
conversion from physical to lattice velocity
constexpr UnitConverter(T physDeltaX, T physDeltaT, T charPhysLength, T charPhysVelocity, T physViscosity, T physDensity, T charPhysPressure=0)
Documentation of constructor:
constexpr T getKnudsenNumber() const
return Knudsen number
constexpr T getCharPhysVelocity() const
return characteristic velocity in physical units
constexpr T getPhysDeltaT() const
returns time spacing (timestep length) in s
constexpr T getConversionFactorTime() const
access (read-only) to private member variable
constexpr T getPhysForce(T latticeForce) const
conversion from lattice to physical force
constexpr int getLatticeLength(T physLength) const
conversion from physical to lattice length, returns number of voxels for given physical length
constexpr Vector< T, D > getPhysForce(Vector< T, D > latticeForce) const
conversion from lattice to physical force vector
constexpr T getCharPhysPressure() const
return characteristic pressure in physical units
constexpr Vector< T, D > getPhysTorque(Vector< T, D > latticeTorque) const
conversion from lattice to physical force vector
virtual void print() const
nice terminal output for conversion factors, characteristical and physical data
constexpr T getPhysPressure(T latticePressure) const
conversion from lattice to physical pressure
virtual ~UnitConverter()=default
constexpr T getPhysLength(int latticeLength) const
conversion from lattice to physical length
constexpr T getCharCFLnumber() const
return characteristic CFL number
constexpr T getLatticeRelaxationFrequencyFromDiffusivity(const T physDiffusivity) const
return relaxation frequency in lattice units computed from given physical diffusivity in m^2 / s
constexpr T getPhysTime(size_t latticeTime) const
conversion from lattice to physical time
constexpr T getLatticeMass(T physMass) const
conversion from physical to lattice mass
const T _latticeRelaxationTime
constexpr T getLatticeRelaxationTime() const
return relaxation time in lattice units
constexpr T getReynoldsNumber() const
return Reynolds number
constexpr T getPhysDeltaX() const
returns grid spacing (voxel length) in m
constexpr T getConversionFactorTorque() const
access (read-only) to private member variable
Plain old scalar vector.
Unit conversion handling – header file.
constexpr T invCs2() any_platform
Definition functions.h:107
Expr sqrt(Expr x)
Definition expr.cpp:225
any_platform T densityFromPressure(T latticePressure)
compute lattice density from lattice pressure
Definition util.h:386
Top level namespace for all of OpenLB.
UnitConverter< T, DESCRIPTOR > * createUnitConverter(XMLreader const &params)
creator function with data given by a XML file
UnitConverter< T, DESCRIPTOR > convectivelyRefineUnitConverter(const UnitConverter< T, DESCRIPTOR > &converter, unsigned scale=2)
virtual void write(std::string const &fileName="unitConverter") const =0
virtual void print() const =0
virtual void print(std::ostream &fout) const =0
virtual ~UnitConverterBase()=default
Base of a field whose size is defined by [C_0,C_1,C_2]^T * [1,D,Q].
Definition fields.h:52
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
static constexpr auto isValid(FieldD< T, DESCRIPTOR, FIELD > value)
Unit conversion handling – header file.
Set of functions commonly used in LB computations – header file.
Input/Output in XML format – header file.