ENH: handle chemical equations parsing errors (#2440)

- catch extra punctuation tokens in chemical equations
- catch unknown species

- simplify generation of reaction string (output)

ENH: allow access of solid concentrations from sub-classes (#2441)
This commit is contained in:
Bernhard F.W. Gschaider
2022-04-12 18:12:53 +02:00
committed by Mark Olesen
parent 796145ee4c
commit addfcf1bc2
5 changed files with 156 additions and 147 deletions

View File

@ -48,7 +48,7 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
// Forward Declarations
class fvMesh;
/*---------------------------------------------------------------------------*\
@ -60,21 +60,17 @@ class pyrolysisChemistryModel
:
public solidChemistryModel<CompType, SolidThermo>
{
// Private Member Functions
//- No copy assignment
void operator=(const pyrolysisChemistryModel&) = delete;
protected:
// Protected Data
//- List of gas species present in reaction system
speciesTable pyrolisisGases_;
//- Thermodynamic data of gases
PtrList<GasThermo> gasThermo_;
//- Number of gas species
//- Number of gas species
label nGases_;
//- Number of components being solved by ODE
@ -83,18 +79,21 @@ protected:
//- List of reaction rate per gas [kg/m3/s]
PtrList<volScalarField::Internal> RRg_;
//- List of accumulative solid concentrations
mutable PtrList<volScalarField> Ys0_;
// Protected Member Functions
//- Write access to source terms for gases
inline PtrList<volScalarField::Internal>& RRg();
//- No copy assignment
void operator=(const pyrolysisChemistryModel&) = delete;
private:
//- List of accumulative solid concentrations
mutable PtrList<volScalarField> Ys0_;
//- Cell counter
label cellCounter_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -69,7 +69,14 @@ Foam::solidReaction<ReactionThermo>::solidReaction
const dictionary& dict
)
:
Reaction<ReactionThermo>(species, thermoDatabase, dict, false),
Reaction<ReactionThermo>
(
species,
thermoDatabase,
dict,
false, // initReactionThermo = false
false // failUnknownSpecie = false
),
pyrolisisGases_(dict.parent().parent().lookup("gaseousSpecies")),
glhs_(),
grhs_()
@ -79,7 +86,26 @@ Foam::solidReaction<ReactionThermo>::solidReaction
IStringStream(dict.getString("reaction"))(),
pyrolisisGases_,
glhs_,
grhs_
grhs_,
false // failUnknownSpecie = false
);
speciesTable allSpecies(species);
for (const word& gasName : pyrolisisGases_)
{
allSpecies.append(gasName);
}
List<specieCoeffs> dummyLhs;
List<specieCoeffs> dummyRhs;
// Rescan (and fail) if a species is neither gas nor solid
this->setLRhs
(
IStringStream(dict.getString("reaction"))(),
allSpecies,
dummyLhs,
dummyRhs
// failUnknownSpecie = true
);
}
@ -125,20 +151,21 @@ Foam::string Foam::solidReaction<ReactionThermo>::solidReactionStr
) const
{
this->reactionStrLeft(reaction);
if (glhs().size() > 0)
if (!glhs().empty())
{
reaction << " + ";
solidReactionStrLeft(reaction);
}
reaction << " = ";
this->reactionStrRight(reaction);
if (grhs().size() > 0)
if (!grhs().empty())
{
reaction << " + ";
solidReactionStrRight(reaction);
}
return reaction.str();
}
@ -148,22 +175,7 @@ void Foam::solidReaction<ReactionThermo>::solidReactionStrLeft
OStringStream& reaction
) const
{
for (label i = 0; i < glhs().size(); ++i)
{
if (i > 0)
{
reaction << " + ";
}
if (mag(glhs()[i].stoichCoeff - 1) > SMALL)
{
reaction << glhs()[i].stoichCoeff;
}
reaction << gasSpecies()[glhs()[i].index];
if (mag(glhs()[i].exponent - glhs()[i].stoichCoeff) > SMALL)
{
reaction << "^" << glhs()[i].exponent;
}
}
Reaction<ReactionThermo>::reactionStr(reaction, gasSpecies(), glhs());
}
@ -173,23 +185,8 @@ void Foam::solidReaction<ReactionThermo>::solidReactionStrRight
OStringStream& reaction
) const
{
for (label i = 0; i < grhs().size(); ++i)
{
if (i > 0)
{
reaction << " + ";
}
if (mag(grhs()[i].stoichCoeff - 1) > SMALL)
{
reaction << grhs()[i].stoichCoeff;
}
reaction << gasSpecies()[grhs()[i].index];
if (mag(grhs()[i].exponent - grhs()[i].stoichCoeff) > SMALL)
{
reaction << "^" << grhs()[i].exponent;
}
}
Reaction<ReactionThermo>::reactionStr(reaction, gasSpecies(), grhs());
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,8 +37,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef solidReaction_H
#define solidReaction_H
#ifndef Foam_solidReaction_H
#define Foam_solidReaction_H
#include "speciesTable.H"
#include "Reaction.H"
@ -49,8 +49,7 @@ namespace Foam
{
// Forward Declarations
template<class ReactionThermo>
class solidReaction;
template<class ReactionThermo> class solidReaction;
template<class ReactionThermo>
inline Ostream& operator<<(Ostream&, const solidReaction<ReactionThermo>&);
@ -64,8 +63,6 @@ class solidReaction
:
public Reaction<ReactionThermo>
{
private:
// Private Data
typedef typename Reaction<ReactionThermo>::specieCoeffs specieCoeffs;
@ -82,15 +79,14 @@ private:
// Private Member Functions
//- Return string representation of reaction
string solidReactionStr(OStringStream&) const;
string solidReactionStr(OStringStream& reaction) const;
//- Return string representation of the left of the reaction
void solidReactionStrLeft(OStringStream&) const;
//- Add string representation of the left of the reaction
void solidReactionStrLeft(OStringStream& reaction) const;
//- Return string representation of the right of the reaction
void solidReactionStrRight(OStringStream&) const;
//- Add string representation of the right of the reaction
void solidReactionStrRight(OStringStream& reaction) const;
//- No copy assignment
void operator=(const solidReaction&) = delete;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,28 +37,42 @@ Foam::label Foam::Reaction<ReactionThermo>::nUnNamedReactions = 0;
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
template<class ReactionThermo>
void Foam::Reaction<ReactionThermo>::reactionStr
(
OStringStream& reaction,
const speciesTable& species,
const List<specieCoeffs>& reactCoeffs
)
{
for (label i = 0; i < reactCoeffs.size(); ++i)
{
const specieCoeffs& coeff = reactCoeffs[i];
if (i)
{
reaction << " + ";
}
if (mag(coeff.stoichCoeff - 1) > SMALL)
{
reaction << coeff.stoichCoeff;
}
reaction << species[coeff.index];
if (mag(coeff.exponent - coeff.stoichCoeff) > SMALL)
{
reaction << "^" << coeff.exponent;
}
}
}
template<class ReactionThermo>
void Foam::Reaction<ReactionThermo>::reactionStrLeft
(
OStringStream& reaction
) const
{
for (label i = 0; i < lhs_.size(); ++i)
{
if (i > 0)
{
reaction << " + ";
}
if (mag(lhs_[i].stoichCoeff - 1) > SMALL)
{
reaction << lhs_[i].stoichCoeff;
}
reaction << species_[lhs_[i].index];
if (mag(lhs_[i].exponent - lhs_[i].stoichCoeff) > SMALL)
{
reaction << "^" << lhs_[i].exponent;
}
}
reactionStr(reaction, species_, lhs_);
}
@ -68,22 +82,7 @@ void Foam::Reaction<ReactionThermo>::reactionStrRight
OStringStream& reaction
) const
{
for (label i = 0; i < rhs_.size(); ++i)
{
if (i > 0)
{
reaction << " + ";
}
if (mag(rhs_[i].stoichCoeff - 1) > SMALL)
{
reaction << rhs_[i].stoichCoeff;
}
reaction << species_[rhs_[i].index];
if (mag(rhs_[i].exponent - rhs_[i].stoichCoeff) > SMALL)
{
reaction << "^" << rhs_[i].exponent;
}
}
reactionStr(reaction, species_, rhs_);
}
@ -195,7 +194,8 @@ template<class ReactionThermo>
Foam::Reaction<ReactionThermo>::specieCoeffs::specieCoeffs
(
const speciesTable& species,
Istream& is
Istream& is,
bool failUnknownSpecie
)
{
token t(is);
@ -223,8 +223,16 @@ Foam::Reaction<ReactionThermo>::specieCoeffs::specieCoeffs
specieName.resize(i);
}
// -1 if not found
// Lookup specie name: -1 if not found
index = species[specieName];
if (failUnknownSpecie && index < 0)
{
FatalErrorInFunction
<< "Unknown specie " << specieName << nl
<< "Not in " << flatOutput(species) << endl
<< exit(FatalError);
}
}
else
{
@ -241,16 +249,24 @@ void Foam::Reaction<ReactionThermo>::setLRhs
Istream& is,
const speciesTable& species,
List<specieCoeffs>& lhs,
List<specieCoeffs>& rhs
List<specieCoeffs>& rhs,
bool failUnknownSpecie
)
{
DynamicList<specieCoeffs> dlrhs;
bool parsingRight = false;
while (is.good())
{
dlrhs.append(specieCoeffs(species, is));
dlrhs.append(specieCoeffs(species, is, failUnknownSpecie));
if (dlrhs.last().index != -1)
if (dlrhs.last().index < 0)
{
dlrhs.remove();
}
if (is.good())
{
token t(is);
if (t.isPunctuation())
@ -260,55 +276,39 @@ void Foam::Reaction<ReactionThermo>::setLRhs
}
else if (t == token::ASSIGN)
{
lhs = dlrhs.shrink();
if (parsingRight)
{
FatalErrorInFunction
<< "Multiple '=' in reaction equation" << endl
<< exit(FatalError);
}
lhs = dlrhs;
dlrhs.clear();
parsingRight = true;
}
else
{
rhs = dlrhs.shrink();
is.putBack(t);
return;
FatalErrorInFunction
<< "Unknown punctuation token '" << t
<< "' in reaction equation" << endl
<< exit(FatalError);
}
}
else
{
rhs = dlrhs.shrink();
rhs = dlrhs;
is.putBack(t);
return;
}
}
else
else if (parsingRight)
{
dlrhs.remove();
if (is.good())
if (!dlrhs.empty())
{
token t(is);
if (t.isPunctuation())
{
if (t == token::ADD)
{
}
else if (t == token::ASSIGN)
{
lhs = dlrhs.shrink();
dlrhs.clear();
}
else
{
rhs = dlrhs.shrink();
is.putBack(t);
return;
}
}
}
else
{
if (!dlrhs.empty())
{
rhs = dlrhs.shrink();
}
return;
rhs = dlrhs;
}
return;
}
}
@ -324,7 +324,8 @@ Foam::Reaction<ReactionThermo>::Reaction
const speciesTable& species,
const ReactionTable<ReactionThermo>& thermoDatabase,
const dictionary& dict,
bool initReactionThermo
bool initReactionThermo,
bool failUnknownSpecie
)
:
ReactionThermo::thermoType(*thermoDatabase[species[0]]),
@ -336,7 +337,8 @@ Foam::Reaction<ReactionThermo>::Reaction
IStringStream(dict.getString("reaction"))(),
species_,
lhs_,
rhs_
rhs_,
failUnknownSpecie
);
if (initReactionThermo)

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,8 +37,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef Reaction_H
#define Reaction_H
#ifndef Foam_Reaction_H
#define Foam_Reaction_H
#include "speciesTable.H"
#include "HashPtrTable.H"
@ -97,14 +97,19 @@ public:
exponent(1)
{}
specieCoeffs(const speciesTable& species, Istream& is);
specieCoeffs
(
const speciesTable& species,
Istream& is,
bool failUnknownSpecie = true
);
bool operator==(const specieCoeffs& sc) const
bool operator==(const specieCoeffs& sc) const noexcept
{
return index == sc.index;
}
bool operator!=(const specieCoeffs& sc) const
bool operator!=(const specieCoeffs& sc) const noexcept
{
return index != sc.index;
}
@ -155,10 +160,18 @@ protected:
// Protected Member Functions
//- Return string representation of the left of the reaction
//- Add string representation for given species reaction coeffs
static void reactionStr
(
OStringStream& reaction,
const speciesTable& species,
const List<specieCoeffs>& reactCoeffs
);
//- Add string representation of the left of the reaction
void reactionStrLeft(OStringStream& reaction) const;
//- Return string representation of the right of the reaction
//- Add string representation of the right of the reaction
void reactionStrRight(OStringStream& reaction) const;
@ -212,7 +225,8 @@ public:
const speciesTable& species,
const ReactionTable<ReactionThermo>& thermoDatabase,
const dictionary& dict,
bool initReactionThermo = true
bool initReactionThermo = true,
bool failUnknownSpecie = true
);
//- Construct and return a clone
@ -278,7 +292,8 @@ public:
Istream&,
const speciesTable&,
List<specieCoeffs>& lhs,
List<specieCoeffs>& rhs
List<specieCoeffs>& rhs,
bool failUnknownSpecie = true
);