From 6cbe89720d178361667ef4fe5a64ecf6a0ab5dc6 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Mon, 7 Jan 2019 09:20:51 +0100 Subject: [PATCH] ENH: for-range, forAllIters() ... in thermophysicalModels/ - reduced clutter when iterating over containers --- .../basic/basicThermo/basicThermoTemplates.C | 2 +- .../TDACChemistryModel/tabulation/ISAT/ISAT.C | 10 +- .../greyMeanSolidAbsorptionEmission.C | 8 +- .../greyMeanSolidAbsorptionEmission.H | 24 +--- .../chemkinReader/chemkinLexer.L | 126 +++++++----------- .../foamChemistryReader/foamChemistryReader.C | 71 ++++------ .../Reactions/ReactionList/ReactionList.C | 13 +- .../Reactions/ReactionList/ReactionList.H | 4 +- 8 files changed, 90 insertions(+), 168 deletions(-) diff --git a/src/thermophysicalModels/basic/basicThermo/basicThermoTemplates.C b/src/thermophysicalModels/basic/basicThermo/basicThermoTemplates.C index 364311183f6..19db3c6476d 100644 --- a/src/thermophysicalModels/basic/basicThermo/basicThermoTemplates.C +++ b/src/thermophysicalModels/basic/basicThermo/basicThermoTemplates.C @@ -269,7 +269,7 @@ Foam::autoPtr<Thermo> Foam::basicThermo::New ) ); - typename Thermo::fvMeshDictPhaseConstructorTable::iterator cstrIter = + auto cstrIter = lookupThermo<Thermo, typename Thermo::fvMeshDictPhaseConstructorTable> ( thermoDict, diff --git a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/ISAT.C b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/ISAT.C index afb16ad8568..01f4c87b42d 100644 --- a/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/ISAT.C +++ b/src/thermophysicalModels/chemistryModel/chemistryModel/TDACChemistryModel/tabulation/ISAT/ISAT.C @@ -142,14 +142,13 @@ void Foam::chemistryTabulationMethods::ISAT<CompType, ThermoType>::addToMRU { if (maxMRUSize_ > 0 && MRURetrieve_) { - typename SLList<chemPointISAT<CompType, ThermoType>*>::iterator iter = - MRUList_.begin(); + auto iter = MRUList_.begin(); // First search if the chemPoint is already in the list bool isInList = false; - for ( ; iter != MRUList_.end(); ++iter) + for (; iter.good(); ++iter) { - if (iter() == phi0) + if (*iter == phi0) { isInList = true; break; @@ -161,10 +160,7 @@ void Foam::chemistryTabulationMethods::ISAT<CompType, ThermoType>::addToMRU // If it is in the list, then move it to front if (iter != MRUList_.begin()) { - // iter hold the position of the element to move MRUList_.remove(iter); - - // Insert the element in front of the list MRUList_.insert(phi0); } } diff --git a/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.C b/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.C index 6e92e3f47a1..abc441d0b0e 100644 --- a/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.C +++ b/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.C @@ -134,12 +134,6 @@ greyMeanSolidAbsorptionEmission } } -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::radiation::greyMeanSolidAbsorptionEmission:: -~greyMeanSolidAbsorptionEmission() -{} - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -167,7 +161,7 @@ calc(const label propertyId) const scalarField& a = ta.ref().primitiveFieldRef(); - forAllConstIter(HashTable<label>, speciesNames_, iter) + forAllConstIters(speciesNames_, iter) { if (mixture_.contains(iter.key())) { diff --git a/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.H b/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.H index 8d4c8bc3deb..eaa71b6090d 100644 --- a/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.H +++ b/src/thermophysicalModels/radiation/submodels/absorptionEmissionModel/greyMeanSolidAbsorptionEmission/greyMeanSolidAbsorptionEmission.H @@ -2,10 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2015 OpenCFD Ltd. - \\/ M anipulation | -------------------------------------------------------------------------------- - | Copyright (C) 2011-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,7 +59,6 @@ class greyMeanSolidAbsorptionEmission : public absorptionEmissionModel { - private: // Private data @@ -115,23 +112,16 @@ public: //- Destructor - virtual ~greyMeanSolidAbsorptionEmission(); + virtual ~greyMeanSolidAbsorptionEmission() = default; // Member Functions - // Access - - // Absorption coefficient - - //- Absorption coefficient for continuous phase - tmp<volScalarField> aCont(const label bandI = 0) const; - - - // Emission coefficient + //- Absorption coefficient for continuous phase + tmp<volScalarField> aCont(const label bandI = 0) const; - //- Emission coefficient for continuous phase - tmp<volScalarField> eCont(const label bandI = 0) const; + //- Emission coefficient for continuous phase + tmp<volScalarField> eCont(const label bandI = 0) const; // Member Functions diff --git a/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L b/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L index eb0d313ebe8..867b4fe9696 100644 --- a/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L +++ b/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -425,12 +425,12 @@ bool finishReaction = false; if (newFormat_) { specieString.replaceAll(" ", "_"); - size_t strEnd = specieString.find_last_not_of('_'); + const auto strEnd = specieString.find_last_not_of('_'); currentSpecieName = specieString.substr(0, strEnd + 1); } else { - size_t spacePos = specieString.find(' '); + const auto spacePos = specieString.find(' '); if (spacePos != string::npos) { currentSpecieName = specieString.substr(0, spacePos); @@ -459,13 +459,12 @@ bool finishReaction = false; for (int i=0; i<4; i++) { word elementName = word::validate(thermoFormula.substr(5*i, 2)); - label nAtoms = atoi(thermoFormula.substr(5*i + 2, 3).c_str()); + const int nAtoms = atoi(thermoFormula.substr(5*i + 2, 3).c_str()); if (elementName.size() && nAtoms) { correctElementName(elementName); - currentSpecieComposition[nSpecieElements].name() = - elementName; + currentSpecieComposition[nSpecieElements].name() = elementName; currentSpecieComposition[nSpecieElements++].nAtoms() = nAtoms; } } @@ -503,38 +502,26 @@ bool finishReaction = false; <readThermoFormula2>{thermoFormula2} { const string thermoFormula(YYText()); word elementName = word::validate(thermoFormula.substr(0, 2)); - const label nAtoms = atoi(thermoFormula.substr(2, 3).c_str()); + const int nAtoms = atoi(thermoFormula.substr(2, 3).c_str()); if ( elementName.size() - && elementName.find('0') == string::npos + && elementName.find('0') == std::string::npos && nAtoms ) { correctElementName(elementName); - currentSpecieComposition[nSpecieElements].name() = - elementName; + currentSpecieComposition[nSpecieElements].name() = elementName; currentSpecieComposition[nSpecieElements++].nAtoms() = nAtoms; } currentSpecieComposition.setSize(nSpecieElements); - speciesCompositionTable::iterator specieCompositionIter - ( - speciesComposition_.find(currentSpecieName) - ); - - if (specieCompositionIter != speciesComposition_.end()) - { - speciesComposition_.erase(specieCompositionIter); - } - - speciesComposition_.insert - ( - currentSpecieName, - currentSpecieComposition - ); + // Add current specie composition to the hash table + // - overwrite existing + speciesComposition_.erase(currentSpecieName); + speciesComposition_.set(currentSpecieName, currentSpecieComposition); BEGIN(readThermoLineLabel1); } @@ -589,17 +576,9 @@ bool finishReaction = false; <readThermoLineLabel4>{thermoLineLabel4} { - HashPtrTable<gasHThermoPhysics>::iterator specieThermoIter - ( - speciesThermo_.find(currentSpecieName) - ); + speciesThermo_.erase(currentSpecieName); - if (specieThermoIter != speciesThermo_.end()) - { - speciesThermo_.erase(specieThermoIter); - } - - speciesThermo_.insert + speciesThermo_.set ( currentSpecieName, autoPtr<gasHThermoPhysics>::New @@ -673,14 +652,12 @@ bool finishReaction = false; const word keyword = word::validate(YYText()); - HashTable<int>::iterator reactionKeywordIter - ( - reactionKeywordTable_.find(keyword) - ); + const auto reactionKeywordIter = + reactionKeywordTable_.cfind(keyword); - if (reactionKeywordIter != reactionKeywordTable_.end()) + if (reactionKeywordIter.found()) { - switch(reactionKeywordIter()) + switch (reactionKeywordIter.val()) { case duplicateReactionType: { @@ -785,12 +762,10 @@ bool finishReaction = false; { currentSpecieName = word::validate(YYText()); - HashTable<label>::iterator specieIndexIter - ( - specieIndices_.find(currentSpecieName) - ); + const auto specieIndexIter = + specieIndices_.cfind(currentSpecieName); - if (specieIndexIter != specieIndices_.end()) + if (specieIndexIter.found()) { if (finishReaction) { @@ -815,7 +790,7 @@ bool finishReaction = false; lrhsPtr = &lhs; } - currentSpecieCoeff.index = specieIndexIter(); + currentSpecieCoeff.index = specieIndexIter.val(); lrhsPtr->append(currentSpecieCoeff); BEGIN(readReactionDelimiter); @@ -829,16 +804,14 @@ bool finishReaction = false; <readReactionKeyword>{reactionKeywordSlash} { - word keyword = word::validate(YYText()); + const word keyword = word::validate(YYText()); - HashTable<int>::iterator reactionKeywordIter - ( - reactionKeywordTable_.find(keyword) - ); + const auto reactionKeywordIter = + reactionKeywordTable_.cfind(keyword); - if (reactionKeywordIter != reactionKeywordTable_.end()) + if (reactionKeywordIter.found()) { - switch(reactionKeywordIter()) + switch (reactionKeywordIter.val()) { case unimolecularFallOffReactionType: { @@ -1144,14 +1117,12 @@ bool finishReaction = false; } else { - HashTable<label>::iterator specieIndexIter - ( - specieIndices_.find(keyword) - ); + const auto specieIndexIter = + specieIndices_.cfind(keyword); - if (specieIndexIter != specieIndices_.end()) + if (specieIndexIter.found()) { - currentThirdBodyIndex = specieIndexIter(); + currentThirdBodyIndex = specieIndexIter.val(); } else { @@ -1170,12 +1141,10 @@ bool finishReaction = false; <readSpecieNamePlus>"+" { currentSpecieName += "+"; - HashTable<label>::iterator specieIndexIter - ( - specieIndices_.find(currentSpecieName) - ); + const auto specieIndexIter = + specieIndices_.cfind(currentSpecieName); - if (specieIndexIter != specieIndices_.end()) + if (specieIndexIter.found()) { if (finishReaction) { @@ -1200,7 +1169,7 @@ bool finishReaction = false; lrhsPtr = &lhs; } - currentSpecieCoeff.index = specieIndexIter(); + currentSpecieCoeff.index = specieIndexIter.val(); lrhsPtr->append(currentSpecieCoeff); BEGIN(readReactionDelimiter); @@ -1339,15 +1308,13 @@ bool finishReaction = false; if (pDependentSpecieName != "M") { - HashTable<label>::iterator specieIndexIter - ( - specieIndices_.find(pDependentSpecieName) - ); + const auto specieIndexIter = + specieIndices_.cfind(pDependentSpecieName); - if (specieIndexIter != specieIndices_.end()) + if (specieIndexIter.found()) { thirdBodyEfficiencies = 0.0; - thirdBodyEfficiencies[specieIndexIter()] = 1.0; + thirdBodyEfficiencies[specieIndexIter.val()] = 1.0; } else { @@ -1385,14 +1352,11 @@ bool finishReaction = false; <readReactionOrderSpecie>{specieName} { currentSpecieName = word::validate(YYText()); - HashTable<label>::iterator specieIndexIter - ( - specieIndices_.find(currentSpecieName) - ); + const auto specieIndexIter = specieIndices_.cfind(currentSpecieName); - if (specieIndexIter != specieIndices_.end()) + if (specieIndexIter.found()) { - currentSpecieIndex = specieIndexIter(); + currentSpecieIndex = specieIndexIter.val(); } else { @@ -1414,11 +1378,11 @@ bool finishReaction = false; bool found = false; - forAll(lrhs, i) + for (gasHReaction::specieCoeffs& coeffs : lrhs) { - if (lrhs[i].index == currentSpecieIndex) + if (coeffs.index == currentSpecieIndex) { - lrhs[i].exponent = stringToScalar(YYText()); + coeffs.exponent = stringToScalar(YYText()); found = true; break; } diff --git a/src/thermophysicalModels/reactionThermo/chemistryReaders/foamChemistryReader/foamChemistryReader.C b/src/thermophysicalModels/reactionThermo/chemistryReaders/foamChemistryReader/foamChemistryReader.C index f77ff82320d..5fc37619158 100644 --- a/src/thermophysicalModels/reactionThermo/chemistryReaders/foamChemistryReader/foamChemistryReader.C +++ b/src/thermophysicalModels/reactionThermo/chemistryReaders/foamChemistryReader/foamChemistryReader.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -73,52 +73,39 @@ void Foam::foamChemistryReader<ThermoType>::readSpeciesComposition() } } - // Loop through all species in thermoDict to retrieve - // the species composition - forAll(speciesTable_, si) + // Loop through all species in thermoDict to retrieve species composition + for (const word& specieName : speciesTable_) { - if (thermoDict_.subDict(speciesTable_[si]).isDict("elements")) - { - dictionary currentElements - ( - thermoDict_.subDict(speciesTable_[si]).subDict("elements") - ); - - wordList currentElementsName(currentElements.toc()); - List<specieElement> currentComposition(currentElementsName.size()); - - forAll(currentElementsName, eni) - { - currentComposition[eni].name() = currentElementsName[eni]; - - currentComposition[eni].nAtoms() = - currentElements.lookupOrDefault - ( - currentElementsName[eni], - 0 - ); - } - - // Add current specie composition to the hash table - speciesCompositionTable::iterator specieCompositionIter - ( - speciesComposition_.find(speciesTable_[si]) - ); - - if (specieCompositionIter != speciesComposition_.end()) - { - speciesComposition_.erase(specieCompositionIter); - } - - speciesComposition_.insert(speciesTable_[si], currentComposition); - } - else + const dictionary* elemsDict = + thermoDict_.subDict(specieName).findDict("elements"); + + if (!elemsDict) { FatalIOErrorInFunction(thermoDict_) - << "Specie " << speciesTable_[si] - << " does not contain element description." + << "Specie " << specieName + << " does not contain \"elements\" description." << exit(FatalIOError); } + + wordList elemNames(elemsDict->toc()); + List<specieElement> currentComposition(elemNames.size()); + + forAll(elemNames, eni) + { + currentComposition[eni].name() = elemNames[eni]; + + currentComposition[eni].nAtoms() = + elemsDict->lookupOrDefault<label> + ( + elemNames[eni], + 0 + ); + } + + // Add current specie composition to the hash table + // - overwrite existing + speciesComposition_.erase(specieName); + speciesComposition_.set(specieName, currentComposition); } } diff --git a/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.C b/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.C index ba6d8d57415..d7c6e97df07 100644 --- a/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.C +++ b/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -72,13 +72,6 @@ Foam::ReactionList<ThermoType>::ReactionList(const ReactionList& reactions) {} -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -template<class ThermoType> -Foam::ReactionList<ThermoType>::~ReactionList() -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class ThermoType> @@ -106,10 +99,8 @@ void Foam::ReactionList<ThermoType>::write(Ostream& os) const { os.beginBlock("reactions"); - forAllConstIter(typename SLPtrList<Reaction<ThermoType>>, *this, iter) + for (const Reaction<ThermoType>& r : *this) { - const Reaction<ThermoType>& r = iter(); - os.beginBlock(r.name()); os.writeEntry("type", r.type()); diff --git a/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.H b/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.H index 85aa0465295..8f392f1a905 100644 --- a/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.H +++ b/src/thermophysicalModels/specie/reaction/Reactions/ReactionList/ReactionList.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -100,7 +100,7 @@ public: //- Destructor - ~ReactionList(); + ~ReactionList() = default; // Public Member Functions -- GitLab