From efabb9c9354dd44f44b950141fe38cc38bcb7458 Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Tue, 28 Feb 2023 12:32:20 +0000 Subject: [PATCH] fvModels: zeroDimensionalMassSource model This fvModel applies a mass source to the continuity equation and to all field equations, in a zero-dimensional case. Correction is made to account for the mass that exits the domain due to expansion in space, so that the model correctly applies a total mass flow rate. It is implemented as a light wrapper around the massSource model. --- src/finiteVolume/fvMesh/fvCellSet/fvCellSet.C | 16 ++- src/finiteVolume/fvMesh/fvCellSet/fvCellSet.H | 9 +- src/fvModels/Make/files | 1 + src/fvModels/derived/massSource/massSource.C | 28 ++-- src/fvModels/derived/massSource/massSource.H | 12 +- .../zeroDimensionalMassSource.C | 130 +++++++++++++++++ .../zeroDimensionalMassSource.H | 135 ++++++++++++++++++ src/meshTools/sets/polyCellSet/polyCellSet.C | 14 +- src/meshTools/sets/polyCellSet/polyCellSet.H | 9 +- 9 files changed, 320 insertions(+), 34 deletions(-) create mode 100644 src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.C create mode 100644 src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.H diff --git a/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.C b/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.C index 6bf6428079..8df0501fcd 100644 --- a/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.C +++ b/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.C @@ -67,11 +67,17 @@ void Foam::fvCellSet::writeFileHeader // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::fvCellSet::fvCellSet -( - const fvMesh& mesh, - const dictionary& dict -) +Foam::fvCellSet::fvCellSet(const fvMesh& mesh) +: + polyCellSet(mesh), + mesh_(mesh), + V_(NaN) +{ + setV(); +} + + +Foam::fvCellSet::fvCellSet(const fvMesh& mesh, const dictionary& dict) : polyCellSet(mesh, dict), mesh_(mesh), diff --git a/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.H b/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.H index 482fe18052..0647d07211 100644 --- a/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.H +++ b/src/finiteVolume/fvMesh/fvCellSet/fvCellSet.H @@ -118,12 +118,11 @@ public: // Constructors + //- Construct from mesh. Will select all. + fvCellSet(const fvMesh& mesh); + //- Construct from mesh and dictionary - fvCellSet - ( - const fvMesh& mesh, - const dictionary& dict - ); + fvCellSet(const fvMesh& mesh, const dictionary& dict); //- Destructor diff --git a/src/fvModels/Make/files b/src/fvModels/Make/files index 98e4d1a2e3..099d5c55a7 100644 --- a/src/fvModels/Make/files +++ b/src/fvModels/Make/files @@ -44,5 +44,6 @@ zeroDimensional/zeroDimensionalFvModel/zeroDimensionalFvModel.C zeroDimensional/constraintSource/constraintSource.C zeroDimensional/densityConstraintSource/densityConstraintSource.C zeroDimensional/pressureConstraintSource/pressureConstraintSource.C +zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.C LIB = $(FOAM_LIBBIN)/libfvModels diff --git a/src/fvModels/derived/massSource/massSource.C b/src/fvModels/derived/massSource/massSource.C index f5259fc808..1d00f310c9 100644 --- a/src/fvModels/derived/massSource/massSource.C +++ b/src/fvModels/derived/massSource/massSource.C @@ -40,7 +40,7 @@ namespace fv } -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // void Foam::fv::massSource::readCoeffs() { @@ -85,6 +85,12 @@ void Foam::fv::massSource::readCoeffs() } +Foam::scalar Foam::fv::massSource::massFlowRate() const +{ + return massFlowRate_->value(mesh().time().userTimeValue()); +} + + template void Foam::fv::massSource::addGeneralSupType ( @@ -92,9 +98,9 @@ void Foam::fv::massSource::addGeneralSupType const word& fieldName ) const { - const scalar t = mesh().time().userTimeValue(); - const scalar massFlowRate = massFlowRate_->value(t); - const Type value = fieldValues_[fieldName]->value(t); + const scalar massFlowRate = this->massFlowRate(); + const Type value = + fieldValues_[fieldName]->value(mesh().time().userTimeValue()); const labelUList cells = set_.cells(); @@ -127,8 +133,7 @@ void Foam::fv::massSource::addSupType if (fieldName == rhoName_) { - const scalar t = mesh().time().userTimeValue(); - const scalar massFlowRate = massFlowRate_->value(t); + const scalar massFlowRate = this->massFlowRate(); forAll(cells, i) { @@ -146,9 +151,9 @@ void Foam::fv::massSource::addSupType << endl; } - const scalar t = mesh().time().userTimeValue(); - const scalar massFlowRate = massFlowRate_->value(t); - const scalar T = fieldValues_[TName_]->value(t); + const scalar massFlowRate = this->massFlowRate(); + const scalar T = + fieldValues_[TName_]->value(mesh().time().userTimeValue()); const basicThermo& thermo = mesh().lookupObject ( @@ -204,11 +209,12 @@ Foam::fv::massSource::massSource const word& name, const word& modelType, const fvMesh& mesh, - const dictionary& dict + const dictionary& dict, + const bool all ) : fvModel(name, modelType, mesh, dict), - set_(mesh, coeffs()), + set_(all ? fvCellSet(mesh) : fvCellSet(mesh, coeffs())), phaseName_(), rhoName_(), heName_(), diff --git a/src/fvModels/derived/massSource/massSource.H b/src/fvModels/derived/massSource/massSource.H index 715389166c..d254b0dfb4 100644 --- a/src/fvModels/derived/massSource/massSource.H +++ b/src/fvModels/derived/massSource/massSource.H @@ -83,7 +83,9 @@ class massSource : public fvModel { - // Private Data +protected: + + // Protected Data //- The set of cells the fvConstraint applies to fvCellSet set_; @@ -107,11 +109,14 @@ class massSource HashPtrTable fieldValues_; - // Private Member Functions + // Protected Member Functions //- Non-virtual read void readCoeffs(); + //- Return the mass flow rate + virtual scalar massFlowRate() const; + // Sources @@ -164,7 +169,8 @@ public: const word& name, const word& modelType, const fvMesh& mesh, - const dictionary& dict + const dictionary& dict, + const bool all=false ); //- Disallow default bitwise copy construction diff --git a/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.C b/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.C new file mode 100644 index 0000000000..a22d5ea3a9 --- /dev/null +++ b/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.C @@ -0,0 +1,130 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "zeroDimensionalMassSource.H" +#include "basicThermo.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +namespace Foam +{ +namespace fv +{ + defineTypeNameAndDebug(zeroDimensionalMassSource, 0); + addToRunTimeSelectionTable(fvModel, zeroDimensionalMassSource, dictionary); +} +} + + +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +void Foam::fv::zeroDimensionalMassSource::readCoeffs() +{ + const basicThermo& thermo = + mesh().lookupObject + ( + IOobject::groupName(physicalProperties::typeName, phaseName_) + ); + + rho0Ptr_.reset + ( + new volScalarField::Internal + ( + IOobject + ( + typedName("rho0"), + mesh().time().timeName(), + mesh(), + IOobject::READ_IF_PRESENT, + IOobject::AUTO_WRITE + ), + thermo.rho()().internalField() + ) + ); +} + + +Foam::scalar Foam::fv::zeroDimensionalMassSource::massFlowRate() const +{ + const scalar t = mesh().time().userTimeValue(); + const scalar t0 = mesh().time().beginTime().value(); + + const scalar mDot = massFlowRate_->value(t); + const scalar sumMDot = massFlowRate_->integral(t0, t); + + const basicThermo& thermo = + mesh().lookupObject + ( + IOobject::groupName(physicalProperties::typeName, phaseName_) + ); + + return mDot*thermo.rho()()[0]/(rho0Ptr_()[0] + sumMDot/mesh().V()[0]); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fv::zeroDimensionalMassSource::zeroDimensionalMassSource +( + const word& name, + const word& modelType, + const fvMesh& mesh, + const dictionary& dict +) +: + massSource(name, modelType, mesh, dict, true), + rho0Ptr_() +{ + if (mesh.nGeometricD() != 0) + { + FatalIOErrorInFunction(dict) + << "Zero-dimensional fvModel applied to a " + << mesh.nGeometricD() << "-dimensional mesh" + << exit(FatalIOError); + } + + readCoeffs(); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::fv::zeroDimensionalMassSource::read(const dictionary& dict) +{ + if (fvModel::read(dict)) + { + massSource::readCoeffs(); + readCoeffs(); + return true; + } + else + { + return false; + } +} + + +// ************************************************************************* // diff --git a/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.H b/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.H new file mode 100644 index 0000000000..bba761f7e7 --- /dev/null +++ b/src/fvModels/zeroDimensional/zeroDimensionalMassSource/zeroDimensionalMassSource.H @@ -0,0 +1,135 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::fv::zeroDimensionalMassSource + +Description + This fvModel applies a mass source to the continuity equation and to all + field equations, in a zero-dimensional case. Correction is made to account + for the mass that exits the domain due to expansion in space, so that the + model correctly applies a total mass flow rate. + +Usage + Example usage: + \verbatim + zeroDimensionalMassSource + { + type zeroDimensionalMassSource; + + massFlowRate 1e-4; + + fieldValues + { + U (10 0 0); + T 300; + k 0.375; + epsilon 14.855; + } + } + \endverbatim + + Values should be provided for all solved for fields. Warnings will be + issued if values are not provided for fields for which transport equations + are solved. Warnings will also be issued if values are provided for fields + which are not solved for. + +SourceFiles + zeroDimensionalMassSource.C + +\*---------------------------------------------------------------------------*/ + +#ifndef zeroDimensionalMassSource_H +#define zeroDimensionalMassSource_H + +#include "massSource.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fv +{ + +/*---------------------------------------------------------------------------*\ + Class zeroDimensionalMassSource Declaration +\*---------------------------------------------------------------------------*/ + +class zeroDimensionalMassSource +: + public massSource +{ +protected: + + // Protected Data + + //- Cached initial density field + autoPtr rho0Ptr_; + + + // Protected Member Functions + + //- Non-virtual read + void readCoeffs(); + + //- Return the mass flow rate + virtual scalar massFlowRate() const; + + +public: + + //- Runtime type information + TypeName("zeroDimensionalMassSource"); + + + // Constructors + + //- Construct from explicit source name and mesh + zeroDimensionalMassSource + ( + const word& name, + const word& modelType, + const fvMesh& mesh, + const dictionary& dict + ); + + + // Member Functions + + // IO + + //- Read source dictionary + virtual bool read(const dictionary& dict); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fv +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/meshTools/sets/polyCellSet/polyCellSet.C b/src/meshTools/sets/polyCellSet/polyCellSet.C index aedb06a3a6..8c13444176 100644 --- a/src/meshTools/sets/polyCellSet/polyCellSet.C +++ b/src/meshTools/sets/polyCellSet/polyCellSet.C @@ -145,11 +145,15 @@ Foam::labelUList Foam::polyCellSet::identityMap(const label len) const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::polyCellSet::polyCellSet -( - const polyMesh& mesh, - const dictionary& dict -) +Foam::polyCellSet::polyCellSet(const polyMesh& mesh) +: + mesh_(mesh), + selectionType_(selectionTypes::all), + cellSetName_(word::null) +{} + + +Foam::polyCellSet::polyCellSet(const polyMesh& mesh, const dictionary& dict) : mesh_(mesh), selectionType_(selectionTypes::all), diff --git a/src/meshTools/sets/polyCellSet/polyCellSet.H b/src/meshTools/sets/polyCellSet/polyCellSet.H index abf8673e29..d0754aa0a0 100644 --- a/src/meshTools/sets/polyCellSet/polyCellSet.H +++ b/src/meshTools/sets/polyCellSet/polyCellSet.H @@ -131,12 +131,11 @@ public: // Constructors + //- Construct from mesh. Will select all. + polyCellSet(const polyMesh& mesh); + //- Construct from mesh and dictionary - polyCellSet - ( - const polyMesh& mesh, - const dictionary& dict - ); + polyCellSet(const polyMesh& mesh, const dictionary& dict); //- Destructor