Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
33template<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
58template<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
76template<class chemistryType>
79(
80 const basicChemistryModel& basicChemistry
81)
82{
83 auto tRR = volScalarField::Internal::New
84 (
85 "RR",
86 IOobject::NO_REGISTER,
87 mesh_,
88 dimensionedScalar(dimMass/dimVolume/dimTime, Zero)
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
122template<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}
164
165
166// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
167
168template<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);
243}
244
245
246// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
247
248template<class chemistryType>
250(
251 const dictionary& dict
252)
253{
256
257 return true;
258}
259
260
261template<class chemistryType>
263execute()
264{
265 createFileNames();
266
268 lookupObject<basicChemistryModel>("chemistryProperties");
269 calculateSpeciesRR(chemistry);
270
271 return true;
272}
273
274
275template<class chemistryType>
277write()
278{
279 if (Pstream::master())
280 {
281 writeSpeciesRR();
282
283 startTime_ = endTime_;
284 }
285
286 return true;
287}
288
289
290// ************************************************************************* //
label k
void setSize(label n)
Alias for resize().
Definition List.H:536
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
Base class for chemistry models.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const Type & value() const noexcept
Return const reference to value.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
const fvMesh & mesh_
Reference to the fvMesh.
fvMeshFunctionObject(const fvMeshFunctionObject &)=delete
No copy construct.
Computes indicators for reaction rates of creation or destruction of species in each reaction.
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
reactionsSensitivityAnalysis(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
virtual bool execute()
Execute the function-object operations.
virtual bool write()
Write the function-object results.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
const Time & time() const
Return time database.
Base class for writing single files from the function objects.
Definition writeFile.H:113
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true, const string &ext=".dat")
Construct from objectRegistry, prefix, fileName.
Definition writeFile.C:200
virtual bool read(const dictionary &dict)
Read.
Definition writeFile.C:240
label nCells() const noexcept
Number of mesh cells.
A class for handling words, derived from Foam::string.
Definition word.H:66
BasicChemistryModel< psiReactionThermo > & chemistry
engineTime & runTime
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
const scalar RR
Universal gas constant: default in [J/(kmol K)].
Type & refCast(U &obj)
A dynamic_cast (for references) to Type reference.
Definition typeInfo.H:172
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299