From fc86e524513d9f99d882ba45e83bb1281ba29d20 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 16 Jun 2023 14:25:12 +0200 Subject: [PATCH] ENH: extrapolateInternal() method for patch fields - provides a more succinct way of writing {fa,fv}PatchField::patchInternalField(*this) as well as a consistent naming that can be used for patches derived from valuePointPatchField ENH: readGradientEntry helper method for fixedGradient conditions - simplifies coding and logic. - support different read construct modes for fixedGradient --- .../tractionDisplacementFvPatchVectorField.C | 4 +- ...DisplacementCorrectionFvPatchVectorField.C | 4 +- .../fixedValuePointPatchFieldTemplate.C | 4 +- .../fixedValuePointPatchFieldTemplate.H | 4 +- .../basic/value/valuePointPatchField.C | 17 +++++ .../basic/value/valuePointPatchField.H | 3 + ...entHeatFluxTemperatureFvPatchScalarField.C | 10 +-- .../basic/calculated/calculatedFaPatchField.C | 4 +- .../basic/calculated/calculatedFaPatchField.H | 2 +- .../basic/coupled/coupledFaPatchField.C | 4 +- .../basic/coupled/coupledFaPatchField.H | 2 +- .../extrapolatedCalculatedFaPatchField.C | 6 +- .../fixedGradient/fixedGradientFaPatchField.C | 75 +++++++++++++++---- .../fixedGradient/fixedGradientFaPatchField.H | 54 +++++++++++-- .../basic/fixedValue/fixedValueFaPatchField.C | 4 +- .../basic/fixedValue/fixedValueFaPatchField.H | 2 +- .../zeroGradient/zeroGradientFaPatchField.C | 6 +- .../constraint/cyclic/cyclicFaPatchField.C | 4 +- .../constraint/cyclic/cyclicFaPatchField.H | 2 +- .../faPatchFields/faPatchField/faPatchField.C | 7 ++ .../faPatchFields/faPatchField/faPatchField.H | 3 + .../calculated/calculatedFaePatchField.C | 4 +- .../calculated/calculatedFaePatchField.H | 2 +- .../basic/coupled/coupledFaePatchField.C | 4 +- .../basic/coupled/coupledFaePatchField.H | 2 +- .../fixedValue/fixedValueFaePatchField.C | 4 +- .../fixedValue/fixedValueFaePatchField.H | 2 +- .../exprFixedValueFvPatchField.C | 2 +- .../exprFixedValueFvPatchField.H | 2 +- .../extrapolatedCalculatedFvPatchField.C | 6 +- .../fixedGradient/fixedGradientFvPatchField.C | 51 ++++++++++++- .../fixedGradient/fixedGradientFvPatchField.H | 25 +++++-- .../zeroGradient/zeroGradientFvPatchField.C | 7 +- .../fixedFluxPressureFvPatchScalarField.C | 12 +-- .../fvPatchFields/fvPatchField/fvPatchField.C | 7 ++ .../fvPatchFields/fvPatchField/fvPatchField.H | 3 + ...fixedIncidentRadiationFvPatchScalarField.C | 11 +-- .../sorptionWallFunctionFvPatchScalarField.C | 11 +-- ...haContactAngleTwoPhaseFvPatchScalarField.C | 9 +-- 39 files changed, 269 insertions(+), 116 deletions(-) diff --git a/applications/solvers/stressAnalysis/solidDisplacementFoam/tractionDisplacement/tractionDisplacementFvPatchVectorField.C b/applications/solvers/stressAnalysis/solidDisplacementFoam/tractionDisplacement/tractionDisplacementFvPatchVectorField.C index 4db517c658..234dacdfda 100644 --- a/applications/solvers/stressAnalysis/solidDisplacementFoam/tractionDisplacement/tractionDisplacementFvPatchVectorField.C +++ b/applications/solvers/stressAnalysis/solidDisplacementFoam/tractionDisplacement/tractionDisplacementFvPatchVectorField.C @@ -47,7 +47,7 @@ tractionDisplacementFvPatchVectorField traction_(p.size(), Zero), pressure_(p.size(), Zero) { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } @@ -79,7 +79,7 @@ tractionDisplacementFvPatchVectorField traction_("traction", dict, p.size()), pressure_("pressure", dict, p.size()) { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } diff --git a/applications/solvers/stressAnalysis/solidEquilibriumDisplacementFoam/tractionDisplacementCorrection/tractionDisplacementCorrectionFvPatchVectorField.C b/applications/solvers/stressAnalysis/solidEquilibriumDisplacementFoam/tractionDisplacementCorrection/tractionDisplacementCorrectionFvPatchVectorField.C index 20b3a8c8cf..040072da1b 100644 --- a/applications/solvers/stressAnalysis/solidEquilibriumDisplacementFoam/tractionDisplacementCorrection/tractionDisplacementCorrectionFvPatchVectorField.C +++ b/applications/solvers/stressAnalysis/solidEquilibriumDisplacementFoam/tractionDisplacementCorrection/tractionDisplacementCorrectionFvPatchVectorField.C @@ -47,7 +47,7 @@ tractionDisplacementCorrectionFvPatchVectorField traction_(p.size(), Zero), pressure_(p.size(), Zero) { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } @@ -79,7 +79,7 @@ tractionDisplacementCorrectionFvPatchVectorField traction_("traction", dict, p.size()), pressure_("pressure", dict, p.size()) { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } diff --git a/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.C b/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.C index b69bcdf734..39006e5318 100644 --- a/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.C +++ b/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.C @@ -125,10 +125,10 @@ ${typeName}FixedValuePointPatch${FieldType} const pointPatch& p, const DimensionedField<${TemplateType}, pointMesh>& iF, const dictionary& dict, - const bool valueRequired + IOobjectOption::readOption requireValue ) : - parent_bctype(p, iF, dict, valueRequired) + parent_bctype(p, iF, dict, requireValue) { if (${verbose:-false}) { diff --git a/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.H b/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.H index 85a9cf08fc..d46cd3e1a6 100644 --- a/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.H +++ b/etc/codeTemplates/dynamicCode/fixedValuePointPatchFieldTemplate.H @@ -89,8 +89,8 @@ public: ( const pointPatch&, const DimensionedField<${TemplateType}, pointMesh>&, - const dictionary&, - const bool valueRequired=true + const dictionary& dict, + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping a copy onto a new patch diff --git a/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.C b/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.C index a0e4ad4ee0..9718da72b3 100644 --- a/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.C +++ b/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.C @@ -62,6 +62,23 @@ bool Foam::valuePointPatchField::readValueEntry } +template +void Foam::valuePointPatchField::extrapolateInternal() +{ + const labelUList& meshPoints = pointPatchFieldBase::patch().meshPoints(); + + const Field& iF = this->primitiveField(); + Field& pfld = *this; + + pfld.resize_nocopy(meshPoints.size()); // In general this is a no-op + + forAll(meshPoints, i) + { + pfld[i] = iF[meshPoints[i]]; + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.H b/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.H index cf04967233..15c6514696 100644 --- a/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.H +++ b/src/OpenFOAM/fields/pointPatchFields/basic/value/valuePointPatchField.H @@ -74,6 +74,9 @@ protected: Field::writeEntry("value", os); } + //- Assign the patch field from the internal field + void extrapolateInternal(); + public: diff --git a/src/atmosphericModels/derivedFvPatchFields/atmTurbulentHeatFluxTemperature/atmTurbulentHeatFluxTemperatureFvPatchScalarField.C b/src/atmosphericModels/derivedFvPatchFields/atmTurbulentHeatFluxTemperature/atmTurbulentHeatFluxTemperatureFvPatchScalarField.C index d6563ca590..93561f57ec 100644 --- a/src/atmosphericModels/derivedFvPatchFields/atmTurbulentHeatFluxTemperature/atmTurbulentHeatFluxTemperatureFvPatchScalarField.C +++ b/src/atmosphericModels/derivedFvPatchFields/atmTurbulentHeatFluxTemperature/atmTurbulentHeatFluxTemperatureFvPatchScalarField.C @@ -106,15 +106,9 @@ atmTurbulentHeatFluxTemperatureFvPatchScalarField Cp0_(Function1::New("Cp0", dict, &db())), q_(PatchFunction1::New(p.patch(), "q", dict)) { - const auto* hasGrad = dict.findEntry("gradient", keyType::LITERAL); - - if (hasGrad && this->readValueEntry(dict)) + if (!this->readGradientEntry(dict) || !this->readValueEntry(dict)) { - gradient().assign(*hasGrad, p.size()); - } - else - { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } } diff --git a/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.C index 4ea2400fef..4f8eefa33c 100644 --- a/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.C @@ -48,10 +48,10 @@ Foam::calculatedFaPatchField::calculatedFaPatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : - faPatchField(p, iF, dict, valueRequired) + faPatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.H b/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.H index 4cda540f43..9f180b65f2 100644 --- a/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.H +++ b/src/finiteArea/fields/faPatchFields/basic/calculated/calculatedFaPatchField.H @@ -79,7 +79,7 @@ public: const DimensionedField&, const dictionary& dict, //! The "value" entry (default: optional) - IOobjectOption::readOption valueRequired = IOobjectOption::LAZY_READ + IOobjectOption::readOption requireValue = IOobjectOption::LAZY_READ ); //- Construct by mapping given patch field onto a new patch diff --git a/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.C index 321c607100..b0e5f2a93a 100644 --- a/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.C @@ -74,11 +74,11 @@ Foam::coupledFaPatchField::coupledFaPatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : lduInterfaceField(refCast(p, dict)), - faPatchField(p, iF, dict, valueRequired) + faPatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.H b/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.H index 57416d3c44..243016b805 100644 --- a/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.H +++ b/src/finiteArea/fields/faPatchFields/basic/coupled/coupledFaPatchField.H @@ -90,7 +90,7 @@ public: const DimensionedField&, const dictionary& dict, //! The "value" entry (default: mandatory) - IOobjectOption::readOption valueRequired = IOobjectOption::MUST_READ + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping the given coupledFaPatchField onto a new patch diff --git a/src/finiteArea/fields/faPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFaPatchField.C index d605cd73db..f390d85116 100644 --- a/src/finiteArea/fields/faPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFaPatchField.C @@ -54,8 +54,7 @@ extrapolatedCalculatedFaPatchField : calculatedFaPatchField(p, iF, dict, IOobjectOption::NO_READ) { - // Set to the internal field - faPatchField::patchInternalField(*this); + faPatchField::extrapolateInternal(); // Zero-gradient patch values } @@ -109,8 +108,7 @@ void Foam::extrapolatedCalculatedFaPatchField::evaluate this->updateCoeffs(); } - // Set to the internal field - faPatchField::patchInternalField(*this); + faPatchField::extrapolateInternal(); // Zero-gradient patch values calculatedFaPatchField::evaluate(); } diff --git a/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.C index 88c2bde870..07a045ce06 100644 --- a/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 Wikki Ltd + Copyright (C) 2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,6 +29,39 @@ License #include "fixedGradientFaPatchField.H" #include "dictionary.H" +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +template +bool Foam::fixedGradientFaPatchField::readGradientEntry +( + const dictionary& dict, + IOobjectOption::readOption readOpt +) +{ + if (!IOobjectOption::isAnyRead(readOpt)) return false; + const auto& p = faPatchFieldBase::patch(); + + + const auto* eptr = dict.findEntry("gradient", keyType::LITERAL); + + if (eptr) + { + gradient_.assign(*eptr, p.size()); + return true; + } + + if (IOobjectOption::isReadRequired(readOpt)) + { + FatalIOErrorInFunction(dict) + << "Required entry 'gradient' : missing for patch " << p.name() + << " in dictionary " << dict.relativeName() << nl + << exit(FatalIOError); + } + + return false; +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -42,6 +76,32 @@ Foam::fixedGradientFaPatchField::fixedGradientFaPatchField {} +template +Foam::fixedGradientFaPatchField::fixedGradientFaPatchField +( + const faPatch& p, + const DimensionedField& iF, + const dictionary& dict, + IOobjectOption::readOption requireGrad +) +: + faPatchField(p, iF, dict, IOobjectOption::NO_READ), + gradient_(p.size()) +{ + if (readGradientEntry(dict, requireGrad)) + { + evaluate(); + } + else + { + // Not read (eg, optional and missing): + // - treat as zero-gradient, do not evaluate + faPatchField::extrapolateInternal(); + gradient_ = Zero; + } +} + + template Foam::fixedGradientFaPatchField::fixedGradientFaPatchField ( @@ -56,21 +116,6 @@ Foam::fixedGradientFaPatchField::fixedGradientFaPatchField {} -template -Foam::fixedGradientFaPatchField::fixedGradientFaPatchField -( - const faPatch& p, - const DimensionedField& iF, - const dictionary& dict -) -: - faPatchField(p, iF, dict, IOobjectOption::NO_READ), - gradient_("gradient", dict, p.size()) -{ - evaluate(); -} - - template Foam::fixedGradientFaPatchField::fixedGradientFaPatchField ( diff --git a/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.H b/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.H index 0c38a45a41..abedc11327 100644 --- a/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.H +++ b/src/finiteArea/fields/faPatchFields/basic/fixedGradient/fixedGradientFaPatchField.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 Wikki Ltd + Copyright (C) 2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,6 +28,35 @@ Class Foam::fixedGradientFaPatchField Description + This boundary condition supplies a fixed gradient condition, such that + the patch values are calculated using: + + \f[ + x_p = x_c + \frac{\nabla(x)}{\Delta} + \f] + + where + \vartable + x_p | patch values + x_c | internal field values + \nabla(x)| gradient (user-specified) + \Delta | inverse distance from patch face centre to cell centre + \endvartable + +Usage + \table + Property | Description | Required | Default value + gradient | gradient | yes | + \endtable + + Example of the boundary condition specification: + \verbatim + + { + type fixedGradient; + gradient uniform 0; + } + \endverbatim Author Zeljko Tukovic, FMENA @@ -37,8 +67,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef fixedGradientFaPatchField_H -#define fixedGradientFaPatchField_H +#ifndef Foam_fixedGradientFaPatchField_H +#define Foam_fixedGradientFaPatchField_H #include "faPatchField.H" @@ -56,10 +86,22 @@ class fixedGradientFaPatchField : public faPatchField { - // Private data + // Private Data Field gradient_; +protected: + + // Protected Member Functions + + //- Read the "gradient" entry into corresponding member + // The reading can be optional (default), mandatory etc. + // \returns True on success + bool readGradientEntry + ( + const dictionary& dict, + IOobjectOption::readOption readOpt = IOobjectOption::LAZY_READ + ); public: @@ -76,12 +118,14 @@ public: const DimensionedField& ); - //- Construct from patch, internal field and dictionary + //- Construct from patch, internal field and dictionary. fixedGradientFaPatchField ( const faPatch&, const DimensionedField&, - const dictionary& + const dictionary& dict, + //! The "gradient" entry (default: mandatory) + IOobjectOption::readOption requireGrad = IOobjectOption::MUST_READ ); //- Construct by mapping the given fixedGradient patch field diff --git a/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.C index 718107a795..728fa8e89b 100644 --- a/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.C @@ -58,10 +58,10 @@ Foam::fixedValueFaPatchField::fixedValueFaPatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : - faPatchField(p, iF, dict, valueRequired) + faPatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.H b/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.H index f096315cee..b99f739ee9 100644 --- a/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.H +++ b/src/finiteArea/fields/faPatchFields/basic/fixedValue/fixedValueFaPatchField.H @@ -87,7 +87,7 @@ public: const DimensionedField&, const dictionary&, //! The "value" entry (default: mandatory) - IOobjectOption::readOption valueRequired = IOobjectOption::MUST_READ + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping the given fixedValue patch field diff --git a/src/finiteArea/fields/faPatchFields/basic/zeroGradient/zeroGradientFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/zeroGradient/zeroGradientFaPatchField.C index edaa2ded0a..c6e04893bc 100644 --- a/src/finiteArea/fields/faPatchFields/basic/zeroGradient/zeroGradientFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/zeroGradient/zeroGradientFaPatchField.C @@ -64,8 +64,7 @@ Foam::zeroGradientFaPatchField::zeroGradientFaPatchField : faPatchField(p, iF, dict, IOobjectOption::NO_READ) { - // Set to the internal field - faPatchField::patchInternalField(*this); + faPatchField::extrapolateInternal(); // Zero-gradient patch values } @@ -100,8 +99,7 @@ void Foam::zeroGradientFaPatchField::evaluate(const Pstream::commsTypes) this->updateCoeffs(); } - // Set to the internal field - faPatchField::patchInternalField(*this); + faPatchField::extrapolateInternal(); // Zero-gradient patch values faPatchField::evaluate(); } diff --git a/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.C b/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.C index e27ad11d7a..8aa6296aa0 100644 --- a/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.C @@ -73,7 +73,7 @@ Foam::cyclicFaPatchField::cyclicFaPatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : coupledFaPatchField(p, iF, dict, IOobjectOption::NO_READ), @@ -90,7 +90,7 @@ Foam::cyclicFaPatchField::cyclicFaPatchField << exit(FatalIOError); } - if (IOobjectOption::isReadRequired(valueRequired)) + if (IOobjectOption::isReadRequired(requireValue)) { this->evaluate(Pstream::commsTypes::blocking); } diff --git a/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.H b/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.H index b81064f795..359800d18c 100644 --- a/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.H +++ b/src/finiteArea/fields/faPatchFields/constraint/cyclic/cyclicFaPatchField.H @@ -100,7 +100,7 @@ public: const DimensionedField&, const dictionary& dict, //! Evaluate (default: mandatory) - IOobjectOption::readOption valueRequired = IOobjectOption::MUST_READ + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping given cyclicFaPatchField onto a new patch diff --git a/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.C b/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.C index 69bb63d39d..c1dfe10883 100644 --- a/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.C +++ b/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.C @@ -63,6 +63,13 @@ bool Foam::faPatchField::readValueEntry } +template +void Foam::faPatchField::extrapolateInternal() +{ + faPatchFieldBase::patch().patchInternalField(internalField_, *this); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template diff --git a/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.H b/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.H index 2398f33d9f..35a6ef29e5 100644 --- a/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.H +++ b/src/finiteArea/fields/faPatchFields/faPatchField/faPatchField.H @@ -273,6 +273,9 @@ protected: Field::writeEntry("value", os); } + //- Assign the patch field from the internal field + void extrapolateInternal(); + public: diff --git a/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.C b/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.C index ab40effddc..5fc16ad869 100644 --- a/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.C +++ b/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.C @@ -61,10 +61,10 @@ Foam::calculatedFaePatchField::calculatedFaePatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : - faePatchField(p, iF, dict, valueRequired) + faePatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.H b/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.H index f63080c194..723426ff2e 100644 --- a/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.H +++ b/src/finiteArea/fields/faePatchFields/basic/calculated/calculatedFaePatchField.H @@ -79,7 +79,7 @@ public: const DimensionedField&, const dictionary& dict, //! The "value" entry (default: mandatory) - IOobjectOption::readOption valueRequired = IOobjectOption::MUST_READ + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping given patch field onto a new patch diff --git a/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.C b/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.C index 5cd1efeef7..200e7bce10 100644 --- a/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.C +++ b/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.C @@ -71,10 +71,10 @@ Foam::coupledFaePatchField::coupledFaePatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : - faePatchField(p, iF, dict, valueRequired) + faePatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.H b/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.H index f119f21e85..507cb9ad0f 100644 --- a/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.H +++ b/src/finiteArea/fields/faePatchFields/basic/coupled/coupledFaePatchField.H @@ -88,7 +88,7 @@ public: const DimensionedField&, const dictionary& dict, //! The "value" entry (default: optional) - IOobjectOption::readOption valueRequired = IOobjectOption::LAZY_READ + IOobjectOption::readOption requireValue = IOobjectOption::LAZY_READ ); //- Construct by mapping the given coupledFaePatchField onto a new patch diff --git a/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.C b/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.C index f836aea397..6d92628dda 100644 --- a/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.C +++ b/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.C @@ -59,10 +59,10 @@ Foam::fixedValueFaePatchField::fixedValueFaePatchField const faPatch& p, const DimensionedField& iF, const dictionary& dict, - IOobjectOption::readOption valueRequired + IOobjectOption::readOption requireValue ) : - faePatchField(p, iF, dict, valueRequired) + faePatchField(p, iF, dict, requireValue) {} diff --git a/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.H b/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.H index 7fca5c8086..ae9585421c 100644 --- a/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.H +++ b/src/finiteArea/fields/faePatchFields/basic/fixedValue/fixedValueFaePatchField.H @@ -87,7 +87,7 @@ public: const DimensionedField&, const dictionary& dict, //! The "value" entry (default: mandatory) - IOobjectOption::readOption valueRequired = IOobjectOption::MUST_READ + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping the given fixedValue patch field diff --git a/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.C b/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.C index fcf136ecbd..c3cf0df50c 100644 --- a/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.C +++ b/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.C @@ -82,7 +82,7 @@ Foam::exprFixedValueFvPatchField::exprFixedValueFvPatchField const fvPatch& p, const DimensionedField& iF, const dictionary& dict, - const bool valueRequired + IOobjectOption::readOption requireValue // (ignored) ) : parent_bctype(p, iF), // bypass dictionary constructor diff --git a/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.H b/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.H index b4d375eefc..f922e6c6ac 100644 --- a/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.H +++ b/src/finiteVolume/expressions/fields/fvPatchFields/exprFixedValueFvPatchField.H @@ -110,7 +110,7 @@ public: const fvPatch&, const DimensionedField&, const dictionary& dict, - const bool valueRequired=true + IOobjectOption::readOption requireValue = IOobjectOption::MUST_READ ); //- Construct by mapping onto a new patch diff --git a/src/finiteVolume/fields/fvPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFvPatchField.C index a1442f4e1a..cad4002f40 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/extrapolatedCalculated/extrapolatedCalculatedFvPatchField.C @@ -54,8 +54,7 @@ extrapolatedCalculatedFvPatchField : calculatedFvPatchField(p, iF, dict, IOobjectOption::NO_READ) { - // Set to the internal field - fvPatchField::patchInternalField(*this); + fvPatchField::extrapolateInternal(); // Zero-gradient patch values } @@ -109,8 +108,7 @@ void Foam::extrapolatedCalculatedFvPatchField::evaluate this->updateCoeffs(); } - // Set to the internal field - fvPatchField::patchInternalField(*this); + fvPatchField::extrapolateInternal(); // Zero-gradient patch values calculatedFvPatchField::evaluate(); } diff --git a/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.C index 22af67b072..720315fd82 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,6 +29,39 @@ License #include "fixedGradientFvPatchField.H" #include "dictionary.H" +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +template +bool Foam::fixedGradientFvPatchField::readGradientEntry +( + const dictionary& dict, + IOobjectOption::readOption readOpt +) +{ + if (!IOobjectOption::isAnyRead(readOpt)) return false; + const auto& p = fvPatchFieldBase::patch(); + + + const auto* eptr = dict.findEntry("gradient", keyType::LITERAL); + + if (eptr) + { + gradient_.assign(*eptr, p.size()); + return true; + } + + if (IOobjectOption::isReadRequired(readOpt)) + { + FatalIOErrorInFunction(dict) + << "Required entry 'gradient' : missing for patch " << p.name() + << " in dictionary " << dict.relativeName() << nl + << exit(FatalIOError); + } + + return false; +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -47,13 +81,24 @@ Foam::fixedGradientFvPatchField::fixedGradientFvPatchField ( const fvPatch& p, const DimensionedField& iF, - const dictionary& dict + const dictionary& dict, + IOobjectOption::readOption requireGrad ) : fvPatchField(p, iF, dict, IOobjectOption::NO_READ), - gradient_("gradient", dict, p.size()) + gradient_(p.size()) { - evaluate(); + if (readGradientEntry(dict, requireGrad)) + { + evaluate(); + } + else + { + // Not read (eg, optional and missing): + // - treat as zero-gradient, do not evaluate + fvPatchField::extrapolateInternal(); + gradient_ = Zero; + } } diff --git a/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.H b/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.H index 9e44899544..5d10c6a2a9 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.H +++ b/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient/fixedGradientFvPatchField.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -65,8 +66,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef fixedGradientFvPatchField_H -#define fixedGradientFvPatchField_H +#ifndef Foam_fixedGradientFvPatchField_H +#define Foam_fixedGradientFvPatchField_H #include "fvPatchField.H" @@ -84,10 +85,22 @@ class fixedGradientFvPatchField : public fvPatchField { - // Private data + // Private Data Field gradient_; +protected: + + // Protected Member Functions + + //- Read the "gradient" entry into corresponding member + // The reading can be optional (default), mandatory etc. + // \returns True on success + bool readGradientEntry + ( + const dictionary& dict, + IOobjectOption::readOption readOpt = IOobjectOption::LAZY_READ + ); public: @@ -104,12 +117,14 @@ public: const DimensionedField& ); - //- Construct from patch, internal field and dictionary + //- Construct from patch, internal field and dictionary. fixedGradientFvPatchField ( const fvPatch&, const DimensionedField&, - const dictionary& + const dictionary& dict, + //! The "gradient" entry (default: mandatory) + IOobjectOption::readOption requireGrad = IOobjectOption::MUST_READ ); //- Construct by mapping the given fixedGradientFvPatchField diff --git a/src/finiteVolume/fields/fvPatchFields/basic/zeroGradient/zeroGradientFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/basic/zeroGradient/zeroGradientFvPatchField.C index fd5dfca10d..65f532974f 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/zeroGradient/zeroGradientFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/zeroGradient/zeroGradientFvPatchField.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,8 +52,7 @@ Foam::zeroGradientFvPatchField::zeroGradientFvPatchField : fvPatchField(p, iF, dict, IOobjectOption::NO_READ) { - // Set to the internal field - fvPatchField::patchInternalField(*this); + fvPatchField::extrapolateInternal(); // Zero-gradient patch values } @@ -100,8 +100,7 @@ void Foam::zeroGradientFvPatchField::evaluate(const Pstream::commsTypes) this->updateCoeffs(); } - // Set to the internal field - fvPatchField::patchInternalField(*this); + fvPatchField::extrapolateInternal(); // Zero-gradient patch values fvPatchField::evaluate(); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/fixedFluxPressure/fixedFluxPressureFvPatchScalarField.C b/src/finiteVolume/fields/fvPatchFields/derived/fixedFluxPressure/fixedFluxPressureFvPatchScalarField.C index 3187bf0a3b..86b218959d 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/fixedFluxPressure/fixedFluxPressureFvPatchScalarField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/fixedFluxPressure/fixedFluxPressureFvPatchScalarField.C @@ -57,15 +57,9 @@ Foam::fixedFluxPressureFvPatchScalarField::fixedFluxPressureFvPatchScalarField { fvPatchFieldBase::readDict(dict); - const auto* hasGrad = dict.findEntry("gradient", keyType::LITERAL); - - if (hasGrad && this->readValueEntry(dict)) + if (!this->readGradientEntry(dict) || !this->readValueEntry(dict)) { - gradient().assign(*hasGrad, p.size()); - } - else - { - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } } @@ -85,7 +79,7 @@ Foam::fixedFluxPressureFvPatchScalarField::fixedFluxPressureFvPatchScalarField patchType() = ptf.patchType(); // Map gradient. Set unmapped values and overwrite with mapped ptf - gradient() = 0.0; + gradient() = Zero; gradient().map(ptf.gradient(), mapper); // Evaluate the value field from the gradient if the internal field is valid diff --git a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.C b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.C index d0f5908b10..0590efeb01 100644 --- a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.C @@ -65,6 +65,13 @@ bool Foam::fvPatchField::readValueEntry } +template +void Foam::fvPatchField::extrapolateInternal() +{ + fvPatchFieldBase::patch().patchInternalField(internalField_, *this); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template diff --git a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H index 1382ab356c..0bacf8304d 100644 --- a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H +++ b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H @@ -302,6 +302,9 @@ protected: Field::writeEntry("value", os); } + //- Assign the patch field from the internal field + void extrapolateInternal(); + public: diff --git a/src/thermoTools/derivedFvPatchFields/fixedIncidentRadiation/fixedIncidentRadiationFvPatchScalarField.C b/src/thermoTools/derivedFvPatchFields/fixedIncidentRadiation/fixedIncidentRadiationFvPatchScalarField.C index d74e60c07b..d005c768df 100644 --- a/src/thermoTools/derivedFvPatchFields/fixedIncidentRadiation/fixedIncidentRadiationFvPatchScalarField.C +++ b/src/thermoTools/derivedFvPatchFields/fixedIncidentRadiation/fixedIncidentRadiationFvPatchScalarField.C @@ -77,16 +77,9 @@ fixedIncidentRadiationFvPatchScalarField temperatureCoupledBase(patch(), dict), qrIncident_("qrIncident", dict, p.size()) { - const auto* hasGrad = dict.findEntry("gradient", keyType::LITERAL); - - if (hasGrad && this->readValueEntry(dict)) + if (!this->readGradientEntry(dict) || !this->readValueEntry(dict)) { - gradient().assign(*hasGrad, p.size()); - } - else - { - // Still reading so cannot yet evaluate. Make up a value. - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } } diff --git a/src/thermoTools/derivedFvPatchFields/wallFunctions/sorptionWallFunction/sorptionWallFunctionFvPatchScalarField.C b/src/thermoTools/derivedFvPatchFields/wallFunctions/sorptionWallFunction/sorptionWallFunctionFvPatchScalarField.C index 3bf82af083..8e4a513a37 100644 --- a/src/thermoTools/derivedFvPatchFields/wallFunctions/sorptionWallFunction/sorptionWallFunctionFvPatchScalarField.C +++ b/src/thermoTools/derivedFvPatchFields/wallFunctions/sorptionWallFunction/sorptionWallFunctionFvPatchScalarField.C @@ -303,16 +303,9 @@ sorptionWallFunctionFvPatchScalarField::sorptionWallFunctionFvPatchScalarField << exit(FatalIOError); } - const auto* hasGrad = dict.findEntry("gradient", keyType::LITERAL); - - if (hasGrad && this->readValueEntry(dict)) + if (!this->readGradientEntry(dict) || !this->readValueEntry(dict)) { - gradient().assign(*hasGrad, p.size()); - } - else - { - // Fallback: set to zero-gradient - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } } diff --git a/src/transportModels/interfaceProperties/alphaContactAngle/alphaContactAngleTwoPhaseFvPatchScalarField.C b/src/transportModels/interfaceProperties/alphaContactAngle/alphaContactAngleTwoPhaseFvPatchScalarField.C index 216b17537c..5a13143fb0 100644 --- a/src/transportModels/interfaceProperties/alphaContactAngle/alphaContactAngleTwoPhaseFvPatchScalarField.C +++ b/src/transportModels/interfaceProperties/alphaContactAngle/alphaContactAngleTwoPhaseFvPatchScalarField.C @@ -70,18 +70,15 @@ alphaContactAngleTwoPhaseFvPatchScalarField fixedGradientFvPatchScalarField(p, iF), // Bypass dictionary constructor limit_(limitControlNames_.get("limit", dict)) { - const auto* hasGrad = dict.findEntry("gradient", keyType::LITERAL); - - if (hasGrad) + if (this->readGradientEntry(dict)) { - gradient().assign(*hasGrad, p.size()); fixedGradientFvPatchScalarField::updateCoeffs(); fixedGradientFvPatchScalarField::evaluate(); } else { // Fallback: set to zero-gradient - fvPatchField::patchInternalField(*this); + extrapolateInternal(); gradient() = Zero; } } @@ -142,7 +139,7 @@ void Foam::alphaContactAngleTwoPhaseFvPatchScalarField::evaluate } else if (limit_ == lcZeroGradient) { - gradient() = 0.0; + gradient() = Zero; } fixedGradientFvPatchScalarField::evaluate();