From dfc8c047b1e7b7b890653cf54d62093f10f5a9f7 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 15 Jun 2022 11:11:50 +0200 Subject: [PATCH] ENH: simplify patchCellsSource name handling, enforce single field name BUG: inconsistent "U" name on re-read (buoyancyEnergy) STYLE: fix spelling inconsistencies --- ...IDControlInletVelocityFvPatchVectorField.C | 2 +- .../derived/buoyancyEnergy/buoyancyEnergy.C | 4 +- .../patchCellsSource/patchCellsSource.C | 143 ++++++++---------- .../patchCellsSource/patchCellsSource.H | 23 ++- .../enthalpySorptionFvPatchScalarField.C | 4 +- .../speciesSorptionFvPatchScalarField.C | 14 +- 6 files changed, 83 insertions(+), 107 deletions(-) diff --git a/src/finiteVolume/fields/fvPatchFields/derived/pressurePIDControlInletVelocity/pressurePIDControlInletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/pressurePIDControlInletVelocity/pressurePIDControlInletVelocityFvPatchVectorField.C index 81aae86891..698f3dfc1b 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/pressurePIDControlInletVelocity/pressurePIDControlInletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/pressurePIDControlInletVelocity/pressurePIDControlInletVelocityFvPatchVectorField.C @@ -289,7 +289,7 @@ void Foam::pressurePIDControlInletVelocityFvPatchVectorField::updateCoeffs() else { WarningInFunction - << "The pressure field name, \"pName\", is \"" << pName_ << "\", " + << "The pressure field name, 'p' is \"" << pName_ << "\", " << "but a field of that name was not found. The inlet velocity " << "will be set to an analytical value calculated from the " << "specified pressure drop. No PID control will be done and " diff --git a/src/fvOptions/sources/derived/buoyancyEnergy/buoyancyEnergy.C b/src/fvOptions/sources/derived/buoyancyEnergy/buoyancyEnergy.C index d54e8968eb..391521d56c 100644 --- a/src/fvOptions/sources/derived/buoyancyEnergy/buoyancyEnergy.C +++ b/src/fvOptions/sources/derived/buoyancyEnergy/buoyancyEnergy.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2015-2016 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -90,7 +90,7 @@ bool Foam::fv::buoyancyEnergy::read(const dictionary& dict) { if (fv::option::read(dict)) { - coeffs_.readIfPresent("UName", UName_); + coeffs_.readIfPresent("U", UName_); return true; } diff --git a/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.C b/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.C index 7d5b74f643..dbac011a51 100644 --- a/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.C +++ b/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.C @@ -26,9 +26,9 @@ License \*---------------------------------------------------------------------------*/ #include "patchCellsSource.H" +#include "boundarySourcePatch.H" #include "fvMatrices.H" #include "addToRunTimeSelectionTable.H" -#include "boundarySourcePatch.H" // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // @@ -54,46 +54,46 @@ Foam::fv::patchCellsSource::patchCellsSource : fv::option(sourceName, modelType, dict, mesh), curTimeIndex_(-1), - UName_(coeffs_.getOrDefault("U", "none")), - hName_(coeffs_.getOrDefault("he", "none")), - speciesName_(coeffs_.getOrDefault("species", "none")) + isEnergySource_(false) { - label nFields = 0; - if (UName_ != "none") - { - nFields++; - } - if (hName_ != "none") - { - nFields++; - } - if (speciesName_ != "none") - { - nFields++; - } - - if (nFields > 1) - { - FatalErrorInFunction - << "patchCellsSource : " - << " cannot be used for more than one field." - << exit(FatalError); - } - fieldNames_.resize(1); - if (speciesName_ != "none") + + label nFields = 0; + + if + ( + coeffs_.readIfPresent("U", fieldNames_[0]) + && fieldNames_[0] != "none" + ) { - fieldNames_[0] = speciesName_; + ++nFields; } - if (UName_ != "none") + if + ( + coeffs_.readIfPresent("he", fieldNames_[0]) + && fieldNames_[0] != "none" + ) { - fieldNames_[0] = UName_; + isEnergySource_ = true; + ++nFields; } - if (hName_ != "none") + if + ( + coeffs_.readIfPresent("species", fieldNames_[0]) + && fieldNames_[0] != "none" + ) { - fieldNames_[0] = hName_; + ++nFields; + } + + if (nFields != 1) + { + FatalIOErrorInFunction(coeffs_) + << "Must be specified for one field (U | he | species), but " + << nFields << " fields were specified!" << endl + << exit(FatalIOError); } fv::option::resetApplied(); @@ -109,75 +109,56 @@ void Foam::fv::patchCellsSource::addSup const label fieldi ) { + if (curTimeIndex_ == mesh_.time().timeIndex()) + { + return; + } + curTimeIndex_ = mesh_.time().timeIndex(); + if (debug) { Info<< type() << ": applying source to " << eqn.psi().name() << endl; } - if (curTimeIndex_ == mesh_.time().timeIndex()) - { - return; - } - - volScalarField* psiPtr; - - // If source applied to he, we need to loop over T for BC's - if (hName_ != "none") - { - psiPtr = mesh_.getObjectPtr("T"); - } - else - { - auto* psi = - mesh_.getObjectPtr(eqn.psi().name()); - - psiPtr = psi; - } - - const volScalarField::Boundary& psib = psiPtr->boundaryField(); - - volScalarField mDot + auto tsu = DimensionedField::New ( - IOobject - ( - "mDot", - mesh_.time().timeName(), - mesh_, - IOobject::NO_READ, - IOobject::NO_WRITE - ), + name_ + eqn.psi().name() + "Su", mesh_, dimensionedScalar(eqn.dimensions()/dimVolume, Zero) ); + // If source applied to he, we need to loop over T for BC's + const volScalarField& psi = + ( + isEnergySource_ + ? mesh_.lookupObject("T") + : mesh_.lookupObject(eqn.psi().name()) + ); + const auto& psib = psi.boundaryField(); + forAll(psib, patchi) { - if (isA(psib[patchi])) + const auto* bpatchPtr = isA(psib[patchi]); + + if (bpatchPtr) { - const boundarySourcePatch& pp = - refCast(psib[patchi]); + tmp tsbnd = bpatchPtr->patchSource(); + const auto& sbnd = tsbnd.cref(); - const labelUList& fc = mesh_.boundary()[patchi].faceCells(); - - tmp tsb = pp.patchSource(); - const scalarField& sb = tsb.cref(); - - forAll(fc, facei) - { - const label celli = fc[facei]; - mDot[celli] += sb[facei]; - } + UIndirectList + ( + tsu.ref().field(), + mesh_.boundary()[patchi].faceCells() + ) = sbnd; } } - eqn += mDot; - - curTimeIndex_ = mesh_.time().timeIndex(); if (debug) { - Info<< " Field source rate min/max : " - << gMin(mDot) << " / " << gMax(mDot) << endl; + Info<< "Field source rate min/max : " << gMinMax(tsu()) << endl; } + + eqn += tsu; } diff --git a/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.H b/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.H index 6c39b97012..965a6570a2 100644 --- a/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.H +++ b/src/fvOptions/sources/derived/patchCellsSource/patchCellsSource.H @@ -42,7 +42,7 @@ Usage // Mandatory entries type patchCellsSource; - // Optional entries + // Optional entries (one only!) U ; he ; species ; @@ -56,13 +56,13 @@ Usage \table Property | Description | Type | Reqd | Deflt type | Type name: patchCellsSource | word | yes | - - U | Name of operand velocity field | word | no | none - he | Name of operand energy field | word | no | none - species | Name of operand species field | word | no | none + U | Name of operand velocity field | word | choice | - + he | Name of operand energy field | word | choice | - + species | Name of operand species field | word | choice | - \endtable The inherited entries are elaborated in: - - \link cellSetOption.H \endlink + - \link fvOption.H \endlink SourceFiles patchCellsSource.C @@ -96,14 +96,8 @@ class patchCellsSource //- Current time index (used for updating) label curTimeIndex_; - //- Name of operand velocity field - word UName_; - - //- Name of operand energy field - word hName_; - - //- Name of operand species field - word speciesName_; + //- Special handling energy field + bool isEnergySource_; public: @@ -136,7 +130,8 @@ public: // Member Functions - //- Add explicit contribution to compressible enthalpy equation + //- Add explicit contribution to compressible + //- (momentum, enthalpy, species) equation virtual void addSup ( const volScalarField& rho, diff --git a/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/enthalpySorption/enthalpySorptionFvPatchScalarField.C b/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/enthalpySorption/enthalpySorptionFvPatchScalarField.C index 694dc96d98..da1abae0ac 100644 --- a/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/enthalpySorption/enthalpySorptionFvPatchScalarField.C +++ b/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/enthalpySorption/enthalpySorptionFvPatchScalarField.C @@ -237,14 +237,14 @@ patchSource() const const basicSpecieMixture& composition = thermo.composition(); - const label speicesId = + const label speciesId = thermo.composition().species()[speciesName_]; scalarField hsp(this->patch().size(), 0); forAll(pp, facei) { - hsp[facei] = composition.Hs(speicesId, pp[facei], Tp[facei]); + hsp[facei] = composition.Hs(speciesId, pp[facei], Tp[facei]); } dhdt += hsp*massb; diff --git a/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/speciesSorption/speciesSorptionFvPatchScalarField.C b/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/speciesSorption/speciesSorptionFvPatchScalarField.C index b64b3f2052..24eeed4b48 100644 --- a/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/speciesSorption/speciesSorptionFvPatchScalarField.C +++ b/src/thermophysicalModels/reactionThermo/derivedFvPatchFields/speciesSorption/speciesSorptionFvPatchScalarField.C @@ -58,8 +58,8 @@ Foam::speciesSorptionFvPatchScalarField::kinematicModelTypeNames Foam::tmp Foam::speciesSorptionFvPatchScalarField::calcMoleFractions() const { - auto tMole = tmp::New(patch().size(), 0); - scalarField& Mole = tMole.ref(); + auto tMole = tmp::New(patch().size(), Zero); + auto& Mole = tMole.ref(); if (db().foundObject(basicThermo::dictName)) { @@ -74,16 +74,16 @@ Foam::speciesSorptionFvPatchScalarField::calcMoleFractions() const const labelUList& faceCells = patch().faceCells(); - const label speicesId = + const label speciesId = thermo.composition().species()[this->internalField().name()]; const dimensionedScalar Wi ( dimMass/dimMoles, - thermo.composition().W(speicesId) + thermo.composition().W(speciesId) ); - const volScalarField X(W*Y[speicesId]/Wi); + const volScalarField X(W*Y[speciesId]/Wi); forAll(faceCells, i) { @@ -309,10 +309,10 @@ patchSource() const basicThermo::dictName ); - const label speicesId = + const label speciesId = thermo.composition().species()[this->internalField().name()]; - const scalar Wi(thermo.composition().W(speicesId)); + const scalar Wi(thermo.composition().W(speciesId)); const scalar t = db().time().timeOutputValue();