diff --git a/src/thermophysicalModels/solidChemistryModel/pyrolysisChemistryModel/pyrolysisChemistryModel.H b/src/thermophysicalModels/solidChemistryModel/pyrolysisChemistryModel/pyrolysisChemistryModel.H index 3eb63bf038..4e8df97bf3 100644 --- a/src/thermophysicalModels/solidChemistryModel/pyrolysisChemistryModel/pyrolysisChemistryModel.H +++ b/src/thermophysicalModels/solidChemistryModel/pyrolysisChemistryModel/pyrolysisChemistryModel.H @@ -48,7 +48,7 @@ SourceFiles namespace Foam { -// Forward declaration of classes +// Forward Declarations class fvMesh; /*---------------------------------------------------------------------------*\ @@ -60,21 +60,17 @@ class pyrolysisChemistryModel : public solidChemistryModel { - // 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_; - //- 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 RRg_; + //- List of accumulative solid concentrations + mutable PtrList Ys0_; + // Protected Member Functions //- Write access to source terms for gases inline PtrList& RRg(); + //- No copy assignment + void operator=(const pyrolysisChemistryModel&) = delete; + private: - //- List of accumulative solid concentrations - mutable PtrList Ys0_; - //- Cell counter label cellCounter_; diff --git a/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.C b/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.C index 16efbc6578..8603f8677a 100644 --- a/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.C +++ b/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.C @@ -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::solidReaction const dictionary& dict ) : - Reaction(species, thermoDatabase, dict, false), + Reaction + ( + species, + thermoDatabase, + dict, + false, // initReactionThermo = false + false // failUnknownSpecie = false + ), pyrolisisGases_(dict.parent().parent().lookup("gaseousSpecies")), glhs_(), grhs_() @@ -79,7 +86,26 @@ Foam::solidReaction::solidReaction IStringStream(dict.getString("reaction"))(), pyrolisisGases_, glhs_, - grhs_ + grhs_, + false // failUnknownSpecie = false + ); + + speciesTable allSpecies(species); + for (const word& gasName : pyrolisisGases_) + { + allSpecies.append(gasName); + } + List dummyLhs; + List 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::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::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::reactionStr(reaction, gasSpecies(), glhs()); } @@ -173,23 +185,8 @@ void Foam::solidReaction::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::reactionStr(reaction, gasSpecies(), grhs()); } + // ************************************************************************* // diff --git a/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.H b/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.H index 089b092c04..b53ec0bf0b 100644 --- a/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.H +++ b/src/thermophysicalModels/solidSpecie/reaction/Reactions/solidReaction/solidReaction.H @@ -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 solidReaction; +template class solidReaction; template inline Ostream& operator<<(Ostream&, const solidReaction&); @@ -64,8 +63,6 @@ class solidReaction : public Reaction { -private: - // Private Data typedef typename Reaction::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; diff --git a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C index a1bc3468bf..9cfc74f481 100644 --- a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C +++ b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C @@ -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::nUnNamedReactions = 0; // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // +template +void Foam::Reaction::reactionStr +( + OStringStream& reaction, + const speciesTable& species, + const List& 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 void Foam::Reaction::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::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 Foam::Reaction::specieCoeffs::specieCoeffs ( const speciesTable& species, - Istream& is + Istream& is, + bool failUnknownSpecie ) { token t(is); @@ -223,8 +223,16 @@ Foam::Reaction::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::setLRhs Istream& is, const speciesTable& species, List& lhs, - List& rhs + List& rhs, + bool failUnknownSpecie ) { DynamicList 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::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::Reaction const speciesTable& species, const ReactionTable& thermoDatabase, const dictionary& dict, - bool initReactionThermo + bool initReactionThermo, + bool failUnknownSpecie ) : ReactionThermo::thermoType(*thermoDatabase[species[0]]), @@ -336,7 +337,8 @@ Foam::Reaction::Reaction IStringStream(dict.getString("reaction"))(), species_, lhs_, - rhs_ + rhs_, + failUnknownSpecie ); if (initReactionThermo) diff --git a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.H b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.H index a40e63f00e..02d27f84ed 100644 --- a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.H +++ b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.H @@ -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& 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& 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& lhs, - List& rhs + List& rhs, + bool failUnknownSpecie = true );