From 128516b8744b7aaf1b41d7ec1069e360632fa184 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 19 Jan 2023 17:48:16 +0100 Subject: [PATCH] ENH: use lerp for valueFraction (mixed BCs) and field relaxation Note: in some borderline cases (eg, PDRFoam) the multiplication order and rounding imposed by the lerp function may affect the results slightly. eg, (valueFraction_ * this->patch().deltaCoeffs()*refValue_) vs. (valueFraction_ * (this->patch().deltaCoeffs()*refValue_)) --- .../basic/mixed/mixedFaPatchField.C | 36 ++++++++++-------- .../fvPatchFields/exprMixedFvPatchField.C | 17 ++------- .../basic/mixed/mixedFvPatchField.C | 38 +++++++++++-------- .../derived/fixedJump/fixedJumpFvPatchField.C | 2 +- .../partialSlip/partialSlipFvPatchField.C | 18 ++++++--- ...aseHydrostaticPressureFvPatchScalarField.C | 2 +- ...tedInletOutletVelocityFvPatchVectorField.C | 5 +-- ...malInletOutletVelocityFvPatchVectorField.C | 7 ++-- ...phaInletOutletVelocityFvPatchVectorField.C | 5 +-- ...allHeatFluxTemperatureFvPatchScalarField.C | 14 +++---- 10 files changed, 75 insertions(+), 69 deletions(-) diff --git a/src/finiteArea/fields/faPatchFields/basic/mixed/mixedFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/mixed/mixedFaPatchField.C index 226a11b02d..b1216ad42f 100644 --- a/src/finiteArea/fields/faPatchFields/basic/mixed/mixedFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/mixed/mixedFaPatchField.C @@ -198,12 +198,11 @@ void Foam::mixedFaPatchField::evaluate(const Pstream::commsTypes) Field::operator= ( - valueFraction_*refValue_ - + - (1.0 - valueFraction_)* + lerp ( - this->patchInternalField() - + refGrad_/this->patch().deltaCoeffs() + this->patchInternalField() + refGrad_/this->patch().deltaCoeffs(), + refValue_, + valueFraction_ ) ); @@ -214,11 +213,12 @@ void Foam::mixedFaPatchField::evaluate(const Pstream::commsTypes) template Foam::tmp> Foam::mixedFaPatchField::snGrad() const { - return + return lerp + ( + refGrad_, + (refValue_ - this->patchInternalField())*this->patch().deltaCoeffs(), valueFraction_ - *(refValue_ - this->patchInternalField()) - *this->patch().deltaCoeffs() - + (1.0 - valueFraction_)*refGrad_; + ); } @@ -238,9 +238,12 @@ Foam::tmp> Foam::mixedFaPatchField::valueBoundaryCoeffs const tmp& ) const { - return - valueFraction_*refValue_ - + (1.0 - valueFraction_)*refGrad_/this->patch().deltaCoeffs(); + return lerp + ( + refGrad_/this->patch().deltaCoeffs(), + refValue_, + valueFraction_ + ); } @@ -256,9 +259,12 @@ template Foam::tmp> Foam::mixedFaPatchField::gradientBoundaryCoeffs() const { - return - valueFraction_*this->patch().deltaCoeffs()*refValue_ - + (1.0 - valueFraction_)*refGrad_; + return lerp + ( + refGrad_, + this->patch().deltaCoeffs()*refValue_, + valueFraction_ + ); } diff --git a/src/finiteVolume/expressions/fields/fvPatchFields/exprMixedFvPatchField.C b/src/finiteVolume/expressions/fields/fvPatchFields/exprMixedFvPatchField.C index 10394092a8..acfa22fd2c 100644 --- a/src/finiteVolume/expressions/fields/fvPatchFields/exprMixedFvPatchField.C +++ b/src/finiteVolume/expressions/fields/fvPatchFields/exprMixedFvPatchField.C @@ -234,25 +234,14 @@ Foam::exprMixedFvPatchField::exprMixedFvPatchField } else { - // Emulate mixedFvPatchField::evaluate, + // Like mixedFvPatchField::evaluate() // but avoid our own updateCoeffs if (!this->updated()) { - this->parent_bctype::updateCoeffs(); + mixedFvPatchField::updateCoeffs(); } - Field::operator= - ( - this->valueFraction()*this->refValue() - + - (1.0 - this->valueFraction())* - ( - this->patchInternalField() - + this->refGrad()/this->patch().deltaCoeffs() - ) - ); - - fvPatchField::evaluate(); + mixedFvPatchField::evaluate(); } } diff --git a/src/finiteVolume/fields/fvPatchFields/basic/mixed/mixedFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/basic/mixed/mixedFvPatchField.C index 3aa783d5b6..4be6c72ed6 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/mixed/mixedFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/mixed/mixedFvPatchField.C @@ -209,7 +209,6 @@ void Foam::mixedFvPatchField::rmap template void Foam::mixedFvPatchField::evaluate(const Pstream::commsTypes) { - if (!this->updated()) { this->updateCoeffs(); @@ -217,11 +216,11 @@ void Foam::mixedFvPatchField::evaluate(const Pstream::commsTypes) Field::operator= ( - valueFraction_*refValue_ - + (1.0 - valueFraction_) - *( - this->patchInternalField() - + refGrad_/this->patch().deltaCoeffs() + lerp + ( + this->patchInternalField() + refGrad_/this->patch().deltaCoeffs(), + refValue_, + valueFraction_ ) ); @@ -233,11 +232,12 @@ template Foam::tmp> Foam::mixedFvPatchField::snGrad() const { - return + return lerp + ( + refGrad_, + (refValue_ - this->patchInternalField())*this->patch().deltaCoeffs(), valueFraction_ - *(refValue_ - this->patchInternalField()) - *this->patch().deltaCoeffs() - + (1.0 - valueFraction_)*refGrad_; + ); } @@ -259,9 +259,12 @@ Foam::mixedFvPatchField::valueBoundaryCoeffs const tmp& ) const { - return - valueFraction_*refValue_ - + (1.0 - valueFraction_)*refGrad_/this->patch().deltaCoeffs(); + return lerp + ( + refGrad_/this->patch().deltaCoeffs(), + refValue_, + valueFraction_ + ); } @@ -277,9 +280,12 @@ template Foam::tmp> Foam::mixedFvPatchField::gradientBoundaryCoeffs() const { - return - valueFraction_*this->patch().deltaCoeffs()*refValue_ - + (1.0 - valueFraction_)*refGrad_; + return lerp + ( + refGrad_, + this->patch().deltaCoeffs()*refValue_, + valueFraction_ + ); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/fixedJump/fixedJumpFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/fixedJump/fixedJumpFvPatchField.C index 0f38fdc737..e938771640 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/fixedJump/fixedJumpFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/fixedJump/fixedJumpFvPatchField.C @@ -212,7 +212,7 @@ void Foam::fixedJumpFvPatchField::relax() return; } - jump_ = relaxFactor_*jump_ + (1 - relaxFactor_)*jump0_; + jump_ = lerp(jump0_, jump_, relaxFactor_); if (timeIndex_ != this->db().time().timeIndex()) { diff --git a/src/finiteVolume/fields/fvPatchFields/derived/partialSlip/partialSlipFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/partialSlip/partialSlipFvPatchField.C index 7f26c14fa9..0d343a904a 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/partialSlip/partialSlipFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/partialSlip/partialSlipFvPatchField.C @@ -153,8 +153,12 @@ Foam::partialSlipFvPatchField::snGrad() const return ( - valueFraction_*refValue_ - + (1.0 - valueFraction_)*transform(I - sqr(nHat), pif) - pif + lerp + ( + transform(I - sqr(nHat), pif), + refValue_, + valueFraction_ + ) - pif )*this->patch().deltaCoeffs(); } @@ -174,10 +178,12 @@ void Foam::partialSlipFvPatchField::evaluate Field::operator= ( - valueFraction_*refValue_ - + - (1.0 - valueFraction_) - *transform(I - sqr(nHat), this->patchInternalField()) + lerp + ( + transform(I - sqr(nHat), this->patchInternalField()), + refValue_, + valueFraction_ + ) ); parent_bctype::evaluate(); diff --git a/src/finiteVolume/fields/fvPatchFields/derived/phaseHydrostaticPressure/phaseHydrostaticPressureFvPatchScalarField.C b/src/finiteVolume/fields/fvPatchFields/derived/phaseHydrostaticPressure/phaseHydrostaticPressureFvPatchScalarField.C index 1b6a532d22..a234a37342 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/phaseHydrostaticPressure/phaseHydrostaticPressureFvPatchScalarField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/phaseHydrostaticPressure/phaseHydrostaticPressureFvPatchScalarField.C @@ -180,7 +180,7 @@ void Foam::phaseHydrostaticPressureFvPatchScalarField::operator= { fvPatchScalarField::operator= ( - valueFraction()*refValue() + (1 - valueFraction())*ptf + lerp(ptf, refValue(), valueFraction()) ); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/pressureDirectedInletOutletVelocity/pressureDirectedInletOutletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/pressureDirectedInletOutletVelocity/pressureDirectedInletOutletVelocityFvPatchVectorField.C index db7d962d81..b2cef93cf2 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/pressureDirectedInletOutletVelocity/pressureDirectedInletOutletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/pressureDirectedInletOutletVelocity/pressureDirectedInletOutletVelocityFvPatchVectorField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -204,8 +204,7 @@ void Foam::pressureDirectedInletOutletVelocityFvPatchVectorField::operator= { fvPatchField::operator= ( - valueFraction()*(inletDir_*(inletDir_ & pvf)) - + (1 - valueFraction())*pvf + lerp(pvf, inletDir_*(inletDir_ & pvf), valueFraction()) ); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/pressureNormalInletOutletVelocity/pressureNormalInletOutletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/pressureNormalInletOutletVelocity/pressureNormalInletOutletVelocityFvPatchVectorField.C index e6d254f67d..e0ea49b05d 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/pressureNormalInletOutletVelocity/pressureNormalInletOutletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/pressureNormalInletOutletVelocity/pressureNormalInletOutletVelocityFvPatchVectorField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -171,10 +171,11 @@ void Foam::pressureNormalInletOutletVelocityFvPatchVectorField::operator= const fvPatchField& pvf ) { + tmp n = patch().nf(); + fvPatchField::operator= ( - valueFraction()*(patch().nf()*(patch().nf() & pvf)) - + (1 - valueFraction())*pvf + lerp(pvf, n()*(n() & pvf), valueFraction()) ); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/pressurePermeableAlphaInletOutletVelocity/pressurePermeableAlphaInletOutletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/pressurePermeableAlphaInletOutletVelocity/pressurePermeableAlphaInletOutletVelocityFvPatchVectorField.C index 30fdd16204..0a23414a40 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/pressurePermeableAlphaInletOutletVelocity/pressurePermeableAlphaInletOutletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/pressurePermeableAlphaInletOutletVelocity/pressurePermeableAlphaInletOutletVelocityFvPatchVectorField.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -202,8 +202,7 @@ void Foam::pressurePermeableAlphaInletOutletVelocityFvPatchVectorField fvPatchField::operator= ( - valueFraction()*(n()*(n() & pvf)) - + (1 - valueFraction())*pvf + lerp(pvf, n()*(n() & pvf), valueFraction()) ); } diff --git a/src/thermoTools/derivedFvPatchFields/externalWallHeatFluxTemperature/externalWallHeatFluxTemperatureFvPatchScalarField.C b/src/thermoTools/derivedFvPatchFields/externalWallHeatFluxTemperature/externalWallHeatFluxTemperatureFvPatchScalarField.C index 90fc37d1e2..a409459da4 100644 --- a/src/thermoTools/derivedFvPatchFields/externalWallHeatFluxTemperature/externalWallHeatFluxTemperatureFvPatchScalarField.C +++ b/src/thermoTools/derivedFvPatchFields/externalWallHeatFluxTemperature/externalWallHeatFluxTemperatureFvPatchScalarField.C @@ -317,11 +317,12 @@ void Foam::externalWallHeatFluxTemperatureFvPatchScalarField::updateCoeffs() scalarField qr(Tp.size(), Zero); if (qrName_ != "none") { - qr = + qr = lerp + ( + qrPrevious_, + patch().lookupPatchField(qrName_), qrRelaxation_ - *patch().lookupPatchField(qrName_) - + (1 - qrRelaxation_)*qrPrevious_; - + ); qrPrevious_ = qr; } @@ -454,9 +455,8 @@ void Foam::externalWallHeatFluxTemperatureFvPatchScalarField::updateCoeffs() } } - valueFraction() = - relaxation_*valueFraction() + (1 - relaxation_)*valueFraction0; - refValue() = relaxation_*refValue() + (1 - relaxation_)*refValue0; + valueFraction() = lerp(valueFraction0, valueFraction(), relaxation_); + refValue() = lerp(refValue0, refValue(), relaxation_); mixedFvPatchScalarField::updateCoeffs();