chemistryReductionMethod.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2021-2024 OpenFOAM Foundation
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 \*---------------------------------------------------------------------------*/
25 
27 #include "chemistryModel.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class ThermoType>
33 (
35 )
36 :
37  chemistry_(chemistry),
38  nSpecie_(chemistry.nSpecie()),
39  nActiveSpecies_(chemistry.nSpecie()),
40  reactionsDisabled_(chemistry.nReaction(), false),
41  activeSpecies_(chemistry.nSpecie(), true),
42  log_(false),
43  tolerance_(NaN),
44  sumnActiveSpecies_(0),
45  sumn_(0),
46  reduceMechCpuTime_(0)
47 {}
48 
49 
50 template<class ThermoType>
52 (
53  const Foam::dictionary& dict,
55 )
56 :
57  chemistry_(chemistry),
58  nSpecie_(chemistry.nSpecie()),
59  nActiveSpecies_(chemistry.nSpecie()),
60  reactionsDisabled_(chemistry.nReaction(), false),
61  activeSpecies_(chemistry.nSpecie(), false),
62  log_
63  (
64  coeffDict(dict).template lookupOrDefault<Switch>("log", false)
65  ),
66  tolerance_
67  (
68  coeffDict(dict).template lookupOrDefault<scalar>("tolerance", 1e-4)
69  ),
70  sumnActiveSpecies_(0),
71  sumn_(0),
72  reduceMechCpuTime_(0)
73 {
74  if (log_)
75  {
76  cpuReduceFile_ = chemistry.logFile("cpu_reduce.out");
77  nActiveSpeciesFile_ = chemistry.logFile("nActiveSpecies.out");
78  }
79 }
80 
81 
82 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
83 
84 template<class ThermoType>
86 {}
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 template<class ThermoType>
93 {
94  if (log_)
95  {
96  cpuTime_.cpuTimeIncrement();
97  }
98 }
99 
100 
101 template<class ThermoType>
103 (
104  List<label>& ctos,
105  DynamicList<label>& stoc
106 )
107 {
108  // Disable reactions containing removed species
109  forAll(chemistry_.reactions(), i)
110  {
111  const Reaction<ThermoType>& R = chemistry_.reactions()[i];
112  reactionsDisabled_[i] = false;
113 
114  forAll(R.lhs(), s)
115  {
116  label ss = R.lhs()[s].index;
117  if (!activeSpecies_[ss])
118  {
119  reactionsDisabled_[i] = true;
120  break;
121  }
122  }
123 
124  if (!reactionsDisabled_[i])
125  {
126  forAll(R.rhs(), s)
127  {
128  label ss = R.rhs()[s].index;
129  if (!activeSpecies_[ss])
130  {
131  reactionsDisabled_[i] = true;
132  break;
133  }
134  }
135  }
136  }
137 
138  // Set the total number of active species
139  nActiveSpecies_ = count(activeSpecies_, true);
140 
141  // Set the indexing arrays
142  stoc.setSize(nActiveSpecies_);
143  for (label i=0, j=0; i<nSpecie(); i++)
144  {
145  if (activeSpecies_[i])
146  {
147  stoc[j] = i;
148  ctos[i] = j++;
149  if (!chemistry_.thermo().speciesActive()[i])
150  {
151  chemistry_.thermo().setSpecieActive(i);
152  }
153  }
154  else
155  {
156  ctos[i] = -1;
157  }
158  }
159 
160  // Change the number of species in the chemistry model
161  chemistry_.setNSpecie(nActiveSpecies_);
162 
163  if (log_)
164  {
165  sumnActiveSpecies_ += nActiveSpecies_;
166  sumn_++;
167  reduceMechCpuTime_ += cpuTime_.cpuTimeIncrement();
168  }
169 }
170 
171 
172 template<class ThermoType>
174 {
175  if (log_)
176  {
177  cpuReduceFile_()
178  << chemistry_.time().userTimeValue()
179  << " " << reduceMechCpuTime_ << endl;
180 
181  if (sumn_)
182  {
183  // Write average number of species
184  nActiveSpeciesFile_()
185  << chemistry_.time().userTimeValue()
186  << " " << sumnActiveSpecies_/sumn_ << endl;
187  }
188 
189  sumnActiveSpecies_ = 0;
190  sumn_ = 0;
191  reduceMechCpuTime_ = 0;
192  }
193 }
194 
195 
196 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:175
Simple extension of ThermoType to handle reaction kinetics in addition to the equilibrium thermodynam...
Definition: Reaction.H:72
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
Extends base chemistry model by adding a thermo package, and ODE functions. Introduces chemistry equa...
virtual ~chemistryReductionMethod()
Destructor.
void initReduceMechanism()
Initialise reduction of the mechanism.
void endReduceMechanism(List< label > &ctos, DynamicList< label > &stoc)
End reduction of the mechanism.
chemistryReductionMethod(chemistryModel< ThermoType > &chemistry)
Construct from components.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const doubleScalar e
Definition: doubleScalar.H:106
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
static scalar R(const scalar a, const scalar x)
Definition: invIncGamma.C:102
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
const label nSpecie
dictionary dict
basicChemistryModel & chemistry