OpenLB 1.7
Loading...
Searching...
No Matches
unitConverter.hh
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
24
25#ifndef UNITCONVERTER_HH
26#define UNITCONVERTER_HH
27
28#include <fstream>
29#include <iostream>
30#include <unistd.h>
31#include "core/singleton.h"
32#include "io/fileName.h"
33#include "unitConverter.h"
34
35// All OpenLB code is contained in this namespace.
36namespace olb {
37
38template <typename T, typename DESCRIPTOR>
39void UnitConverter<T, DESCRIPTOR>::print(std::ostream& clout) const
40{
41 clout << "----------------- UnitConverter information -----------------" << std::endl;
42 clout << "-- Parameters:" << std::endl;
43 clout << "Resolution: N= " << getResolution() << std::endl;
44 clout << "Lattice velocity: latticeU= " << getCharLatticeVelocity() << std::endl;
45 clout << "Lattice relaxation frequency: omega= " << getLatticeRelaxationFrequency( ) << std::endl;
46 clout << "Lattice relaxation time: tau= " << getLatticeRelaxationTime() << std::endl;
47 clout << "Characteristical length(m): charL= " << getCharPhysLength() << std::endl;
48 clout << "Characteristical speed(m/s): charU= " << getCharPhysVelocity() << std::endl;
49 clout << "Phys. kinematic viscosity(m^2/s): charNu= " << getPhysViscosity() << std::endl;
50 clout << "Phys. density(kg/m^d): charRho= " << getPhysDensity() << std::endl;
51 clout << "Characteristical pressure(N/m^2): charPressure= " << getCharPhysPressure() << std::endl;
52 clout << "Mach number: machNumber= " << getMachNumber() << std::endl;
53 clout << "Reynolds number: reynoldsNumber= " << getReynoldsNumber() << std::endl;
54 clout << "Knudsen number: knudsenNumber= " << getKnudsenNumber() << std::endl;
55
56 clout << std::endl;
57 clout << "-- Conversion factors:" << std::endl;
58 clout << "Voxel length(m): physDeltaX= " << getConversionFactorLength() << std::endl;
59 clout << "Time step(s): physDeltaT= " << getConversionFactorTime() << std::endl;
60 clout << "Velocity factor(m/s): physVelocity= " << getConversionFactorVelocity() << std::endl;
61 clout << "Density factor(kg/m^3): physDensity= " << getConversionFactorDensity() << std::endl;
62 clout << "Mass factor(kg): physMass= " << getConversionFactorMass() << std::endl;
63 clout << "Viscosity factor(m^2/s): physViscosity= " << getConversionFactorViscosity() << std::endl;
64 clout << "Force factor(N): physForce= " << getConversionFactorForce() << std::endl;
65 clout << "Pressure factor(N/m^2): physPressure= " << getConversionFactorPressure() << std::endl;
66
67 clout << "-------------------------------------------------------------" << std::endl;
68
69}
70
71template <typename T, class DESCRIPTOR>
73{
74 print(clout);
75}
76
77template <typename T, typename DESCRIPTOR>
78void UnitConverter<T, DESCRIPTOR>::write(std::string const& fileName) const
79{
80 std::string dataFile = singleton::directories().getLogOutDir() + fileName + ".dat";
81
82 if (singleton::mpi().isMainProcessor()) {
83 std::ofstream fout(dataFile.c_str(), std::ios::trunc);
84 if (!fout) {
85 clout << "error write() function: can not open std::ofstream" << std::endl;
86 }
87 else {
88 print( fout );
89 fout.close();
90 }
91 }
92}
93
94template<typename T, typename DESCRIPTOR>
96{
97 OstreamManager clout(std::cout,"createUnitConverter");
98 params.setWarningsOn(false);
99
100 T physDeltaX{};
101 T physDeltaT{};
102
103 T charPhysLength{};
104 T charPhysVelocity{};
105 T physViscosity{};
106 T physDensity{};
107 T charPhysPressure = 0;
108
109 int resolution{};
110 T latticeRelaxationTime{};
111 T charLatticeVelocity{};
112
113 int counter = 0; // counting the number of Discretization parameters and returning a warning if more than 2 are provided
114
115 // params[parameter].read(value) sets the value or returns false if the parameter can not be found
116 params["Application"]["PhysParameters"]["CharPhysLength"].read(charPhysLength);
117 params["Application"]["PhysParameters"]["CharPhysVelocity"].read(charPhysVelocity);
118 params["Application"]["PhysParameters"]["PhysViscosity"].read(physViscosity);
119 params["Application"]["PhysParameters"]["PhysDensity"].read(physDensity);
120 params["Application"]["PhysParameters"]["CharPhysPressure"].read(charPhysPressure);
121
122 std::vector<std::string> discretizationParam = {"PhysDeltaX", "Resolution",
123 "CharLatticeVelocity", "PhysDeltaT", "LatticeRelaxationTime"};
124
125 for(int i = 0; i<discretizationParam.size(); i++){
126 std::string test;
127 if(params["Application"]["Discretization"][discretizationParam[i]].read(test,false)){
128 counter++;
129 }
130 }
131 if(counter>2){
132 clout << "WARNING: More than 2 discretization parameters provided" << std::endl;
133 }
134
135 if (!params["Application"]["Discretization"]["PhysDeltaX"].read(physDeltaX,false)) {
136 if (!params["Application"]["Discretization"]["Resolution"].read<int>(resolution,false)) {
137 if (!params["Application"]["Discretization"]["CharLatticeVelocity"].read(charLatticeVelocity,false)) {
138 // NOT found physDeltaX, resolution or charLatticeVelocity
139 clout << "Error: Have not found PhysDeltaX, Resolution or CharLatticeVelocity in XML file."
140 << std::endl;
141 exit (1);
142 }
143 else {
144 // found charLatticeVelocity
145 if (params["Application"]["Discretization"]["PhysDeltaT"].read(physDeltaT,false)) {
146 physDeltaX = charPhysVelocity / charLatticeVelocity * physDeltaT;
147 }
148 else if (params["Application"]["Discretization"]["LatticeRelaxationTime"].read(latticeRelaxationTime,false)) {
149 physDeltaX = physViscosity * charLatticeVelocity / charPhysVelocity * descriptors::invCs2<T,DESCRIPTOR>() / (latticeRelaxationTime - 0.5);
150 }
151 else {
152 clout << "Error: Only found CharLatticeVelocity, missing PhysDeltaT or LatticeRelaxationTime"
153 << std::endl;
154 exit (1);
155 }
156 }
157 }
158 else {
159 // found resolution
160 physDeltaX = charPhysLength / resolution;
161
162 if (params["Application"]["Discretization"]["CharLatticeVelocity"].read(charLatticeVelocity,false)) {
163 latticeRelaxationTime = physViscosity * charLatticeVelocity * descriptors::invCs2<T,DESCRIPTOR>() * resolution + 0.5;
164 }
165 else {
166 if (!params["Application"]["Discretization"]["LatticeRelaxationTime"].read(latticeRelaxationTime,false)) {
167 clout << "Error: Have not found LatticeRelaxationTime and was not able to derive it using CharLatticeVelocity"
168 << std::endl;
169 exit (1);
170 }
171 }
172 }
173 }
174 // found physDeltaX
175 if (!params["Application"]["Discretization"]["PhysDeltaT"].read(physDeltaT,false)) {
176 if (!params["Application"]["Discretization"]["LatticeRelaxationTime"].read(latticeRelaxationTime,false)) {
177 if (!params["Application"]["Discretization"]["CharLatticeVelocity"].read(charLatticeVelocity,false)) {
178 // NOT found physDeltaT, latticeRelaxationTime and charLatticeVelocity
179 clout << "Error: Have not found PhysDeltaT, LatticeRelaxationTime or CharLatticeVelocity in XML file."
180 << std::endl;
181 exit (1);
182 }
183 else {
184 // found charLatticeVelocity
185 physDeltaT = charLatticeVelocity / charPhysVelocity * physDeltaX;
186 }
187 }
188 else {
189 // found latticeRelaxationTime
190 physDeltaT = (latticeRelaxationTime - 0.5) / descriptors::invCs2<T,DESCRIPTOR>() * physDeltaX * physDeltaX / physViscosity;
191 }
192 }
193
194 return new UnitConverter<T, DESCRIPTOR>(physDeltaX, physDeltaT, charPhysLength, charPhysVelocity, physViscosity, physDensity, charPhysPressure);
195}
196
197} // namespace olb
198
199#endif
class for marking output with some text
Conversion between physical and lattice units, as well as discretization.
void write(std::string const &fileName="unitConverter") const
virtual void print() const
nice terminal output for conversion factors, characteristical and physical data
void setWarningsOn(bool warnings) const
switch warnings on/off
Definition xmlReader.h:412
bool read(T &value, bool verboseOn=true, bool exitIfMissing=false) const
Prints out the XML structure read in, mostly for debugging purposes.
std::string getLogOutDir() const
Definition singleton.h:89
These functions help you to create file names.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
UnitConverter< T, DESCRIPTOR > * createUnitConverter(XMLreader const &params)
creator function with data given by a XML file
Definition of singletons: global, publicly available information.
Unit conversion handling – header file.