reactionsSensitivityAnalysis.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
29 #include "dictionary.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class chemistryType>
36 {
37  if (writeToFile() && !prodFilePtr_)
38  {
39  prodFilePtr_ = newFileAtStartTime("production");
40  writeHeader(prodFilePtr_(), "production");
41  writeFileHeader(prodFilePtr_());
42 
43  consFilePtr_ = newFileAtStartTime("consumption");
44  writeHeader(consFilePtr_(), "consumption");
45  writeFileHeader(consFilePtr_());
46 
47  prodIntFilePtr_ = newFileAtStartTime("productionInt");
48  writeHeader(prodIntFilePtr_(), "productionInt");
49  writeFileHeader(prodIntFilePtr_());
50 
51  consIntFilePtr_ = newFileAtStartTime("consumptionInt");
52  writeHeader(consIntFilePtr_(), "consumptionInt");
53  writeFileHeader(consIntFilePtr_());
54  }
55 }
56 
57 
58 template<class chemistryType>
61 (
62  OFstream& os
63 )
64 {
65  writeCommented(os, "Reaction");
66 
67  forAll(speciesNames_, k)
68  {
69  os << tab << speciesNames_[k] << tab;
70  }
71 
72  os << nl << endl;
73 }
74 
75 
76 template<class chemistryType>
79 (
80  const basicChemistryModel& basicChemistry
81 )
82 {
84  (
85  "RR",
86  IOobject::NO_REGISTER,
87  mesh_,
89  );
90  auto& RR = tRR.ref();
91 
92  scalar dt = time_.deltaTValue();
93 
94  endTime_ += dt;
95 
96  forAll(production_, speciei)
97  {
98  forAll(production_[speciei], reactioni)
99  {
100  RR = basicChemistry.calculateRR(reactioni, speciei);
101 
102  if (RR[0] > 0.0)
103  {
104  production_[speciei][reactioni] = RR[0];
105  productionInt_[speciei][reactioni] += dt*RR[0];
106  }
107  else if (RR[0] < 0.0)
108  {
109  consumption_[speciei][reactioni] = RR[0];
110  consumptionInt_[speciei][reactioni] += dt*RR[0];
111  }
112  else
113  {
114  production_[speciei][reactioni] = 0.0;
115  consumption_[speciei][reactioni] = 0.0;
116  }
117  }
118  }
119 }
120 
121 
122 template<class chemistryType>
125 {
126 
127  consFilePtr_() << "time : " << mesh_.time().value() << tab << nl;
128  consFilePtr_() << "delta T : "<< mesh_.time().deltaTValue() << nl << nl;
129  prodFilePtr_() << "time : " << mesh_.time().value() << tab << nl;
130  prodFilePtr_() << "delta T : "<< mesh_.time().deltaTValue() << nl << nl;
131 
132  consIntFilePtr_() << "start time : " << startTime_ << tab
133  << "end time :" << endTime_ << nl;
134 
135  prodIntFilePtr_() << "start time : " << startTime_ << tab
136  << "end time :" << endTime_ << nl;
137 
138  for (label reactioni = 0; reactioni < nReactions_; ++reactioni)
139  {
140  consFilePtr_() << reactioni << tab;
141  consIntFilePtr_() << reactioni << tab;
142  prodFilePtr_() << reactioni << tab;
143  prodIntFilePtr_() << reactioni << tab;
144 
145  forAll(speciesNames_, i)
146  {
147  prodFilePtr_() << production_[i][reactioni] << tab;
148  consFilePtr_() << consumption_[i][reactioni] << tab;
149  prodIntFilePtr_() << productionInt_[i][reactioni] << tab;
150  consIntFilePtr_() << consumptionInt_[i][reactioni] << tab;
151  consumptionInt_[i][reactioni] = 0.0;
152  productionInt_[i][reactioni] = 0.0;
153  }
154  consFilePtr_() << nl;
155  consIntFilePtr_() << nl;
156  prodFilePtr_() << nl;
157  prodIntFilePtr_() << nl;
158  }
159  consFilePtr_() << nl << nl;
160  consIntFilePtr_() << nl << nl;
161  prodFilePtr_() << nl << nl;
162  prodIntFilePtr_() << nl << nl;
163 }
165 
166 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
167 
168 template<class chemistryType>
171 (
172  const word& name,
173  const Time& runTime,
174  const dictionary& dict
175 )
176 :
178  writeFile(mesh_, name),
179  nReactions_(0),
180  startTime_(0),
181  endTime_(0),
182  production_(0),
183  consumption_(0),
184  productionInt_(0),
185  consumptionInt_(0),
186  speciesNames_(),
187  prodFilePtr_(),
188  consFilePtr_(),
189  prodIntFilePtr_(),
190  consIntFilePtr_()
191 {
192  read(dict);
193 
194  if (mesh_.nCells() != 1)
195  {
197  << "Function object only applicable to single cell cases"
198  << abort(FatalError);
199  }
200 
201  if (foundObject<basicChemistryModel>("chemistryProperties"))
202  {
203  const chemistryType& chemistry = refCast<const chemistryType>
204  (
205  lookupObject<basicChemistryModel>("chemistryProperties")
206  );
207 
208  speciesNames_.setSize
209  (
210  chemistry.thermo().composition().species().size()
211  );
212 
213  forAll(speciesNames_, i)
214  {
215  speciesNames_[i] = chemistry.thermo().composition().species()[i];
216  }
217 
218  nReactions_ = chemistry.nReaction();
219 
220  if (production_.size() == 0)
221  {
222  production_.setSize(speciesNames_.size());
223  consumption_.setSize(production_.size());
224  productionInt_.setSize(production_.size());
225  consumptionInt_.setSize(production_.size());
226 
227  forAll(production_, i)
228  {
229  production_[i].setSize(nReactions_, 0.0);
230  consumption_[i].setSize(nReactions_, 0.0);
231  productionInt_[i].setSize(nReactions_, 0.0);
232  consumptionInt_[i].setSize(nReactions_, 0.0);
233  }
234  }
235  }
236  else
237  {
239  << " No chemistry model found. "
240  << " Objects available are : " << mesh_.names()
241  << exit(FatalError);
242  }
243 }
244 
245 
246 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
247 
248 template<class chemistryType>
250 (
251  const dictionary& dict
252 )
253 {
257  return true;
258 }
259 
260 
261 template<class chemistryType>
263 execute()
264 {
265  createFileNames();
266 
268  lookupObject<basicChemistryModel>("chemistryProperties");
269  calculateSpeciesRR(chemistry);
271  return true;
272 }
273 
274 
275 template<class chemistryType>
277 write()
278 {
279  if (Pstream::master())
280  {
281  writeSpeciesRR();
282 
283  startTime_ = endTime_;
284  }
285 
286  return true;
287 }
288 
289 
290 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
virtual bool write()
Calculate the reactionsSensitivityAnalysis and write.
static void writeHeader(Ostream &os, const word &fieldName)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
wordList names() const
The unsorted names of all objects.
virtual bool read(const dictionary &)
Read the reactionsSensitivityAnalysis data.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
label k
Boltzmann constant.
BasicChemistryModel< psiReactionThermo > & chemistry
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
const dimensionSet dimVolume(pow3(dimLength))
Definition: dimensionSets.H:58
void setSize(const label n)
Alias for resize()
Definition: List.H:320
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
reactionsSensitivityAnalysis(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Computes indicators for reaction rates of creation or destruction of species in each reaction...
OBJstream os(runTime.globalPath()/outputName)
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:241
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
label nCells() const noexcept
Number of mesh cells.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
const dimensionSet dimTime(0, 0, 1, 0, 0, 0, 0)
Definition: dimensionSets.H:51
virtual bool read(const dictionary &dict)
Read optional controls.
const scalar RR
Universal gas constant: default in [J/(kmol K)].
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
const Time & time() const
Return time database.
const dimensionSet dimMass(1, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:49
Base class for writing single files from the function objects.
Definition: writeFile.H:112
const fvMesh & mesh_
Reference to the fvMesh.
Base class for chemistry models.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127