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_))
This commit is contained in:
Mark Olesen
2023-01-19 17:48:16 +01:00
parent 70d526cd82
commit 128516b874
10 changed files with 75 additions and 69 deletions

View File

@ -198,12 +198,11 @@ void Foam::mixedFaPatchField<Type>::evaluate(const Pstream::commsTypes)
Field<Type>::operator= Field<Type>::operator=
( (
valueFraction_*refValue_ lerp
+
(1.0 - valueFraction_)*
( (
this->patchInternalField() this->patchInternalField() + refGrad_/this->patch().deltaCoeffs(),
+ refGrad_/this->patch().deltaCoeffs() refValue_,
valueFraction_
) )
); );
@ -214,11 +213,12 @@ void Foam::mixedFaPatchField<Type>::evaluate(const Pstream::commsTypes)
template<class Type> template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::mixedFaPatchField<Type>::snGrad() const Foam::tmp<Foam::Field<Type>> Foam::mixedFaPatchField<Type>::snGrad() const
{ {
return return lerp
(
refGrad_,
(refValue_ - this->patchInternalField())*this->patch().deltaCoeffs(),
valueFraction_ valueFraction_
*(refValue_ - this->patchInternalField()) );
*this->patch().deltaCoeffs()
+ (1.0 - valueFraction_)*refGrad_;
} }
@ -238,9 +238,12 @@ Foam::tmp<Foam::Field<Type>> Foam::mixedFaPatchField<Type>::valueBoundaryCoeffs
const tmp<scalarField>& const tmp<scalarField>&
) const ) const
{ {
return return lerp
valueFraction_*refValue_ (
+ (1.0 - valueFraction_)*refGrad_/this->patch().deltaCoeffs(); refGrad_/this->patch().deltaCoeffs(),
refValue_,
valueFraction_
);
} }
@ -256,9 +259,12 @@ template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>>
Foam::mixedFaPatchField<Type>::gradientBoundaryCoeffs() const Foam::mixedFaPatchField<Type>::gradientBoundaryCoeffs() const
{ {
return return lerp
valueFraction_*this->patch().deltaCoeffs()*refValue_ (
+ (1.0 - valueFraction_)*refGrad_; refGrad_,
this->patch().deltaCoeffs()*refValue_,
valueFraction_
);
} }

View File

@ -234,25 +234,14 @@ Foam::exprMixedFvPatchField<Type>::exprMixedFvPatchField
} }
else else
{ {
// Emulate mixedFvPatchField<Type>::evaluate, // Like mixedFvPatchField<Type>::evaluate()
// but avoid our own updateCoeffs // but avoid our own updateCoeffs
if (!this->updated()) if (!this->updated())
{ {
this->parent_bctype::updateCoeffs(); mixedFvPatchField<Type>::updateCoeffs();
} }
Field<Type>::operator= mixedFvPatchField<Type>::evaluate();
(
this->valueFraction()*this->refValue()
+
(1.0 - this->valueFraction())*
(
this->patchInternalField()
+ this->refGrad()/this->patch().deltaCoeffs()
)
);
fvPatchField<Type>::evaluate();
} }
} }

View File

@ -209,7 +209,6 @@ void Foam::mixedFvPatchField<Type>::rmap
template<class Type> template<class Type>
void Foam::mixedFvPatchField<Type>::evaluate(const Pstream::commsTypes) void Foam::mixedFvPatchField<Type>::evaluate(const Pstream::commsTypes)
{ {
if (!this->updated()) if (!this->updated())
{ {
this->updateCoeffs(); this->updateCoeffs();
@ -217,11 +216,11 @@ void Foam::mixedFvPatchField<Type>::evaluate(const Pstream::commsTypes)
Field<Type>::operator= Field<Type>::operator=
( (
valueFraction_*refValue_ lerp
+ (1.0 - valueFraction_) (
*( this->patchInternalField() + refGrad_/this->patch().deltaCoeffs(),
this->patchInternalField() refValue_,
+ refGrad_/this->patch().deltaCoeffs() valueFraction_
) )
); );
@ -233,11 +232,12 @@ template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>>
Foam::mixedFvPatchField<Type>::snGrad() const Foam::mixedFvPatchField<Type>::snGrad() const
{ {
return return lerp
(
refGrad_,
(refValue_ - this->patchInternalField())*this->patch().deltaCoeffs(),
valueFraction_ valueFraction_
*(refValue_ - this->patchInternalField()) );
*this->patch().deltaCoeffs()
+ (1.0 - valueFraction_)*refGrad_;
} }
@ -259,9 +259,12 @@ Foam::mixedFvPatchField<Type>::valueBoundaryCoeffs
const tmp<scalarField>& const tmp<scalarField>&
) const ) const
{ {
return return lerp
valueFraction_*refValue_ (
+ (1.0 - valueFraction_)*refGrad_/this->patch().deltaCoeffs(); refGrad_/this->patch().deltaCoeffs(),
refValue_,
valueFraction_
);
} }
@ -277,9 +280,12 @@ template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>>
Foam::mixedFvPatchField<Type>::gradientBoundaryCoeffs() const Foam::mixedFvPatchField<Type>::gradientBoundaryCoeffs() const
{ {
return return lerp
valueFraction_*this->patch().deltaCoeffs()*refValue_ (
+ (1.0 - valueFraction_)*refGrad_; refGrad_,
this->patch().deltaCoeffs()*refValue_,
valueFraction_
);
} }

View File

@ -212,7 +212,7 @@ void Foam::fixedJumpFvPatchField<Type>::relax()
return; return;
} }
jump_ = relaxFactor_*jump_ + (1 - relaxFactor_)*jump0_; jump_ = lerp(jump0_, jump_, relaxFactor_);
if (timeIndex_ != this->db().time().timeIndex()) if (timeIndex_ != this->db().time().timeIndex())
{ {

View File

@ -153,8 +153,12 @@ Foam::partialSlipFvPatchField<Type>::snGrad() const
return return
( (
valueFraction_*refValue_ lerp
+ (1.0 - valueFraction_)*transform(I - sqr(nHat), pif) - pif (
transform(I - sqr(nHat), pif),
refValue_,
valueFraction_
) - pif
)*this->patch().deltaCoeffs(); )*this->patch().deltaCoeffs();
} }
@ -174,10 +178,12 @@ void Foam::partialSlipFvPatchField<Type>::evaluate
Field<Type>::operator= Field<Type>::operator=
( (
valueFraction_*refValue_ lerp
+ (
(1.0 - valueFraction_) transform(I - sqr(nHat), this->patchInternalField()),
*transform(I - sqr(nHat), this->patchInternalField()) refValue_,
valueFraction_
)
); );
parent_bctype::evaluate(); parent_bctype::evaluate();

View File

@ -180,7 +180,7 @@ void Foam::phaseHydrostaticPressureFvPatchScalarField::operator=
{ {
fvPatchScalarField::operator= fvPatchScalarField::operator=
( (
valueFraction()*refValue() + (1 - valueFraction())*ptf lerp(ptf, refValue(), valueFraction())
); );
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd. Copyright (C) 2017-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -204,8 +204,7 @@ void Foam::pressureDirectedInletOutletVelocityFvPatchVectorField::operator=
{ {
fvPatchField<vector>::operator= fvPatchField<vector>::operator=
( (
valueFraction()*(inletDir_*(inletDir_ & pvf)) lerp(pvf, inletDir_*(inletDir_ & pvf), valueFraction())
+ (1 - valueFraction())*pvf
); );
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd. Copyright (C) 2017-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -171,10 +171,11 @@ void Foam::pressureNormalInletOutletVelocityFvPatchVectorField::operator=
const fvPatchField<vector>& pvf const fvPatchField<vector>& pvf
) )
{ {
tmp<vectorField> n = patch().nf();
fvPatchField<vector>::operator= fvPatchField<vector>::operator=
( (
valueFraction()*(patch().nf()*(patch().nf() & pvf)) lerp(pvf, n()*(n() & pvf), valueFraction())
+ (1 - valueFraction())*pvf
); );
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd. Copyright (C) 2021-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -202,8 +202,7 @@ void Foam::pressurePermeableAlphaInletOutletVelocityFvPatchVectorField
fvPatchField<vector>::operator= fvPatchField<vector>::operator=
( (
valueFraction()*(n()*(n() & pvf)) lerp(pvf, n()*(n() & pvf), valueFraction())
+ (1 - valueFraction())*pvf
); );
} }

View File

@ -317,11 +317,12 @@ void Foam::externalWallHeatFluxTemperatureFvPatchScalarField::updateCoeffs()
scalarField qr(Tp.size(), Zero); scalarField qr(Tp.size(), Zero);
if (qrName_ != "none") if (qrName_ != "none")
{ {
qr = qr = lerp
(
qrPrevious_,
patch().lookupPatchField<volScalarField>(qrName_),
qrRelaxation_ qrRelaxation_
*patch().lookupPatchField<volScalarField>(qrName_) );
+ (1 - qrRelaxation_)*qrPrevious_;
qrPrevious_ = qr; qrPrevious_ = qr;
} }
@ -454,9 +455,8 @@ void Foam::externalWallHeatFluxTemperatureFvPatchScalarField::updateCoeffs()
} }
} }
valueFraction() = valueFraction() = lerp(valueFraction0, valueFraction(), relaxation_);
relaxation_*valueFraction() + (1 - relaxation_)*valueFraction0; refValue() = lerp(refValue0, refValue(), relaxation_);
refValue() = relaxation_*refValue() + (1 - relaxation_)*refValue0;
mixedFvPatchScalarField::updateCoeffs(); mixedFvPatchScalarField::updateCoeffs();