FreeFOAM The Cross-Platform CFD Toolkit
IFCLookUpTableGen.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  IFC (infinitely-fast chemistry) look-up table generator
26 
27 Description
28  Calculate the the infinitely-fast chemistry relationships in function of ft.
29  for a given fuel.
30 
31  The output is given in moles. i.e. dictionary:
32 
33  @verbatim
34  fileName "SpeciesTable";
35 
36 
37  fuel CH4(ANHARMONIC);
38  n 1;
39  m 4;
40 
41 
42  fields
43  (
44  {
45  name ft;
46  min 0.;
47  max 1.;
48  N 100;
49  }
50  );
51 
52  output
53  (
54  {
55  name CH4;
56  }
57  {
58  name CO2;
59  }
60  {
61  name H2O;
62  }
63  );
64  @endverbatim
65 
66 Usage
67  - IFCLookUpTableGen <controlFile> [OPTION]
68 
69  @param <controlFile>
70  Control-dictionary file for the lookup table generation
71 
72  @param -parallel \n
73  Run the case in parallel
74 
75  @param -help \n
76  Display short usage message
77 
78  @param -doc \n
79  Display Doxygen documentation page
80 
81  @param -srcDoc \n
82  Display source code
83 
84 \*---------------------------------------------------------------------------*/
85 
86 #include <OpenFOAM/argList.H>
87 
88 #include <OpenFOAM/IFstream.H>
89 #include <OpenFOAM/OFstream.H>
90 
91 #include <specie/specieThermo.H>
92 #include <specie/janafThermo.H>
93 #include <specie/perfectGas.H>
94 
95 #include <OpenFOAM/IOdictionary.H>
96 
98 
99 using namespace Foam;
100 
102 
103 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
104 
105 int main(int argc, char *argv[])
106 {
108  argList::validArgs.append("controlFile");
109  argList args(argc, argv);
110 
111  fileName controlFileName(args.additionalArgs()[0]);
112 
113  enum varinput
114  {
115  fti,
116  CH4i,
117  CO2i,
118  H2Oi
119  };
120 
121  // Construct control dictionary
122  IFstream controlFile(controlFileName);
123 
124  // Check controlFile stream is OK
125  if (!controlFile.good())
126  {
128  << "Cannot read file " << controlFileName
129  << exit(FatalError);
130  }
131 
132  dictionary control(controlFile);
133 
134  // fuel-air mix
135  fileName BurcatCpDataFileName(findEtcFile("thermoData/BurcatCpData"));
136 
137  // Construct control dictionary
138  IFstream BurcatCpDataFile(BurcatCpDataFileName);
139 
140  // Check BurcatCpData stream is OK
141  if (!BurcatCpDataFile.good())
142  {
144  << "Cannot read file " << BurcatCpDataFileName
145  << exit(FatalError);
146  }
147 
148  dictionary CpData(BurcatCpDataFile);
149 
150  word fuelName(control.lookup("fuel"));
151  scalar n(readScalar(control.lookup("n")));
152  scalar m(readScalar(control.lookup("m")));
153 
154  scalar stoicO2 = n + m/4.0;
155  scalar stoicN2 = (0.79/0.21)*(n + m/4.0);
156  scalar stoicCO2 = n;
157  scalar stoicH2O = m/2.0;
158 
159  thermo fuel
160  (
161  "fuel",
162  thermo(CpData.lookup(fuelName))
163  );
164 
165  thermo oxidant
166  (
167  "oxidant",
168  stoicO2*thermo(CpData.lookup("O2"))
169  + stoicN2*thermo(CpData.lookup("N2"))
170  );
171 
173  (
174  "stoichiometricAirFuelMassRatio",
175  dimless,
176  (oxidant.W()*oxidant.nMoles())/(fuel.W()*fuel.nMoles())
177  );
178 
179 
180  // Open File for Look Up Table
181  fileName LookUpTableFile(control.lookup("fileName"));
182 
183  OFstream controlFileOutput(LookUpTableFile);
184 
185  if (!controlFileOutput.good())
186  {
188  << "Cannot open file " << LookUpTableFile
189  << exit(FatalError);
190  }
191 
192  // Create Look Up Table
193  interpolationLookUpTable<scalar> LookUpTable(control);
194 
195  const List<label>& dim = LookUpTable.dim();
196 
197  const List<scalar>& min = LookUpTable.min();
198 
199  const List<scalar>& delta = LookUpTable.delta();
200 
201  label count = 0;
202 
203  for (label i=0; i <= dim[fti]; i++)
204  {
205  scalar ft = Foam::min(scalar(i)*delta[fti] + min[fti] + 0.001, 0.999);
206 
207  scalar equiv = Foam::pow(((1.0 / ft) - 1.0), -1.0)*stoicRatio.value();
208 
209  scalar o2 = (1.0/equiv)*stoicO2;
210  scalar n2 = (0.79/0.21)*o2;
211  scalar fres = max(1.0 - 1.0/equiv, 0.0);
212  scalar ores = max(1.0/equiv - 1.0, 0.0);
213  scalar fburnt = 1.0 - fres;
214 
215 
216  thermo fuel
217  (
218  "fuel",
219  fres*thermo(CpData.lookup(fuelName))
220  );
221 
222  thermo N2
223  (
224  "N2",
225  n2*thermo(CpData.lookup("N2"))
226  );
227 
228  thermo O2
229  (
230  "O2",
231  ores*thermo(CpData.lookup("O2"))
232  );
233 
234  thermo CO2
235  (
236  "CO2",
237  fburnt*stoicCO2*thermo(CpData.lookup("CO2"))
238  );
239 
240  thermo H2O
241  (
242  "H2O",
243  fburnt*stoicH2O*thermo(CpData.lookup("H2O"))
244  );
245 
246 
247  scalar ToTalMoles = fuel.nMoles() + CO2.nMoles() + H2O.nMoles() +
248  N2.nMoles() + O2.nMoles();
249 
250  LookUpTable[fti][count] = ft;
251  LookUpTable[CH4i][count] = fuel.nMoles()/ToTalMoles;
252  LookUpTable[CO2i][count] = CO2.nMoles()/ToTalMoles;
253  LookUpTable[H2Oi][count] = H2O.nMoles()/ToTalMoles;
254  count++;
255  }
256 
257  IOobject::writeBanner(controlFileOutput);
258  controlFileOutput << "\n" << nl;
259  controlFileOutput.writeKeyword("fields");
260  controlFileOutput << LookUpTable.entries() << token::END_STATEMENT << nl;
261 
262  controlFileOutput.writeKeyword("output");
263  controlFileOutput << LookUpTable.output() << token::END_STATEMENT << nl;
264 
265  if (LookUpTable.size() == 0)
266  {
268  (
269  "Foam::IFCLookUpTableGen"
270  ) << "table is empty" << nl
271  << exit(FatalError);
272  }
273 
274  controlFileOutput.writeKeyword("values");
275  controlFileOutput << LookUpTable << token::END_STATEMENT << nl;
276 
277  return(0);
278 }
279 
280 
281 // ************************************************************************* //