STYLE: use clamp/clamp_range instead of max(min(..., upper), lower)

This commit is contained in:
Mark Olesen
2023-02-01 18:39:19 +01:00
parent 4d45cfd5a9
commit ba6667a344
38 changed files with 89 additions and 151 deletions

View File

@ -1,14 +1,5 @@
{ {
alphav = alphav = clamp((rho - rholSat)/(rhovSat - rholSat), zero_one{});
max
(
min
(
(rho - rholSat)/(rhovSat - rholSat),
scalar(1)
),
scalar(0)
);
alphal = 1.0 - alphav; alphal = 1.0 - alphav;
Info<< "max-min alphav: " << max(alphav).value() Info<< "max-min alphav: " << max(alphav).value()

View File

@ -108,7 +108,7 @@ void setAlpha
{ {
cutCell.calcSubCell(cellI, 0.0); cutCell.calcSubCell(cellI, 0.0);
alpha1[cellI] = max(min(cutCell.VolumeOfFluid(), 1), 0); alpha1[cellI] = clamp(cutCell.VolumeOfFluid(), 0, 1);
if (writeOBJ && (mag(cutCell.faceArea()) >= 1e-14)) if (writeOBJ && (mag(cutCell.faceArea()) >= 1e-14))
{ {

View File

@ -149,7 +149,7 @@ Type Foam::uniformInterpolationTable<Type>::interpolate(scalar x) const
{ {
if (bound_) if (bound_)
{ {
x = max(min(xMax() - SMALL*dx_, x), x0_); x = clamp(x, x0_, (xMax() - SMALL*dx_));
} }
else else
{ {

View File

@ -100,7 +100,7 @@ protected:
//- that forms the basis of many more complex ramp functions //- that forms the basis of many more complex ramp functions
inline scalar linearRamp(const scalar t) const inline scalar linearRamp(const scalar t) const
{ {
return max(min((t - start_)/duration_, 1), 0); return clamp((t - start_)/duration_, 0, 1);
} }

View File

@ -310,7 +310,7 @@ tmp<volScalarField::Internal> kOmegaSSTLM<BasicTurbulenceModel>::ReThetat0
} }
lambda = sqr(thetat)/nu[celli]*dUsds[celli]; lambda = sqr(thetat)/nu[celli]*dUsds[celli];
lambda = max(min(lambda, 0.1), -0.1); lambda = clamp(lambda, -0.1, 0.1);
lambdaErr = mag(lambda - lambda0); lambdaErr = mag(lambda - lambda0);

View File

@ -109,13 +109,13 @@ void Foam::combustionModels::EDC<ReactionThermo>::correct()
const scalar nu = mu[i]/(rho[i] + SMALL); const scalar nu = mu[i]/(rho[i] + SMALL);
const scalar Da = const scalar Da =
max(min(sqrt(nu/(epsilon[i] + SMALL))/tc[i], 10), 1e-10); clamp(sqrt(nu/(epsilon[i] + SMALL))/tc[i], 1e-10, 10);
const scalar ReT = sqr(k[i])/(nu*epsilon[i] + SMALL); const scalar ReT = sqr(k[i])/(nu*epsilon[i] + SMALL);
const scalar CtauI = min(C1_/(Da*sqrt(ReT + 1)), 2.1377); const scalar CtauI = min(C1_/(Da*sqrt(ReT + 1)), 2.1377);
const scalar CgammaI = const scalar CgammaI =
max(min(C2_*sqrt(Da*(ReT + 1)), 5), 0.4082); clamp(C2_*sqrt(Da*(ReT + 1)), 0.4082, 5);
const scalar gammaL = const scalar gammaL =
CgammaI*pow025(nu*epsilon[i]/(sqr(k[i]) + SMALL)); CgammaI*pow025(nu*epsilon[i]/(sqr(k[i]) + SMALL));

View File

@ -80,7 +80,7 @@
); );
dimensionedScalar AkEst = gSum(mgb*mesh.V().field()); dimensionedScalar AkEst = gSum(mgb*mesh.V().field());
StCorr.value() = max(min((Ak/AkEst).value(), 10.0), 1.0); StCorr.value() = clamp((Ak/AkEst).value(), 1, 10);
Info<< "StCorr = " << StCorr.value() << endl; Info<< "StCorr = " << StCorr.value() << endl;
} }

View File

@ -249,22 +249,18 @@ void Foam::activeBaffleVelocityFvPatchVectorField::updateCoeffs()
} }
openFraction_ = openFraction_ =
max
( (
min openFraction_
+ min
( (
openFraction_ this->db().time().deltaTValue()/openingTime_,
+ min maxOpenFractionDelta_
( )
this->db().time().deltaTValue()/openingTime_, *(orientation_*sign(forceDiff))
maxOpenFractionDelta_
)
*(orientation_*sign(forceDiff)),
1 - 1e-6
),
1e-6
); );
openFraction_ = clamp(openFraction_, 1e-6, 1 - 1e-6);
Info<< "openFraction = " << openFraction_ << endl; Info<< "openFraction = " << openFraction_ << endl;
vectorField::subField Sfw = this->patch().patch().faceAreas(); vectorField::subField Sfw = this->patch().patch().faceAreas();

View File

@ -299,27 +299,18 @@ void Foam::activePressureForceBaffleVelocityFvPatchVectorField::updateCoeffs()
if (mag(valueDiff) > mag(minThresholdValue_) || baffleActivated_) if (mag(valueDiff) > mag(minThresholdValue_) || baffleActivated_)
{ {
openFraction_ = openFraction_ =
max (
openFraction_
+ min
( (
min this->db().time().deltaT().value()/openingTime_,
( maxOpenFractionDelta_
openFraction_ )
+ min );
(
this->db().time().deltaT().value()/openingTime_,
maxOpenFractionDelta_
),
1 - 1e-6
),
1e-6
);
baffleActivated_ = true; baffleActivated_ = true;
}
else
{
openFraction_ = max(min(1 - 1e-6, openFraction_), 1e-6);
} }
openFraction_ = clamp(openFraction_, 1e-6, 1 - 1e-6);
if (Pstream::master()) if (Pstream::master())
{ {

View File

@ -148,9 +148,9 @@ void Foam::phaseHydrostaticPressureFvPatchScalarField::updateCoeffs()
meshObjects::gravity::New(db().time()); meshObjects::gravity::New(db().time());
// scalar rhor = 1000; // scalar rhor = 1000;
// scalarField alphap1 = max(min(alphap, scalar(1)), scalar(0)); // scalarField alphap1 = clamp(alphap, zero_one{});
// valueFraction() = alphap1/(alphap1 + rhor*(1.0 - alphap1)); // valueFraction() = alphap1/(alphap1 + rhor*(1.0 - alphap1));
valueFraction() = max(min(alphap, scalar(1)), scalar(0)); valueFraction() = clamp(alphap, zero_one{});
refValue() = refValue() =
pRefValue_ pRefValue_

View File

@ -512,22 +512,11 @@ void Foam::averageNeighbourFvGeometryScheme::makeNonOrthoWeights
scalarField& faceWeights scalarField& faceWeights
) const ) const
{ {
cosAngles = cosAngles = clamp
max (
( polyMeshTools::faceOrthogonality(mesh_, faceNormals, cellCentres),
scalar(0), zero_one{}
min );
(
scalar(1),
polyMeshTools::faceOrthogonality
(
mesh_,
faceNormals,
cellCentres
)
)
);
// Make weight: 0 for ortho faces, 1 for 90degrees non-ortho // Make weight: 0 for ortho faces, 1 for 90degrees non-ortho
//const scalarField faceWeights(scalar(1)-cosAngles); //const scalarField faceWeights(scalar(1)-cosAngles);

View File

@ -192,16 +192,7 @@ void Foam::highAspectRatioFvGeometryScheme::calcAspectRatioWeights
delta = SMALL; delta = SMALL;
} }
cellWeight = cellWeight = clamp((aratio-minAspect_)/delta, zero_one{});
max
(
scalar(0),
min
(
scalar(1),
(aratio-minAspect_)/delta
)
);
faceWeight.setSize(mesh_.nFaces()); faceWeight.setSize(mesh_.nFaces());

View File

@ -81,7 +81,7 @@ public:
faceFlux, phiP, phiN, gradcP, gradcN, d faceFlux, phiP, phiN, gradcP, gradcN, d
); );
return max(min(min(2*r, 0.5*r + 0.5), 2), 0); return clamp(min(2*r, 0.5*r + 0.5), 0, 2);
} }
}; };

View File

@ -81,7 +81,7 @@ public:
faceFlux, phiP, phiN, gradcP, gradcN, d faceFlux, phiP, phiN, gradcP, gradcN, d
); );
return max(min(r, 1), 0); return clamp(r, 0, 1);
} }
}; };

View File

@ -107,7 +107,7 @@ public:
((faceFlux - phiU)/stabilise(phiCD - phiU, SMALL) + k_); ((faceFlux - phiU)/stabilise(phiCD - phiU, SMALL) + k_);
// Limit the limiter between upwind and central // Limit the limiter between upwind and central
return max(min(PLimiter, 1), 0); return clamp(PLimiter, 0, 1);
} }
}; };

View File

@ -100,7 +100,7 @@ public:
scalar QLimiter = (phif - phiU)/stabilise(phiCD - phiU, SMALL); scalar QLimiter = (phif - phiU)/stabilise(phiCD - phiU, SMALL);
// Limit the limiter between upwind and downwind // Limit the limiter between upwind and downwind
return max(min(QLimiter, 2), 0); return clamp(QLimiter, 0, 2);
} }
}; };

View File

@ -99,7 +99,7 @@ public:
scalar QLimiter = (phif - phiU)/stabilise(phiCD - phiU, SMALL); scalar QLimiter = (phif - phiU)/stabilise(phiCD - phiU, SMALL);
// Limit the limiter between upwind and downwind // Limit the limiter between upwind and downwind
return max(min(QLimiter, 2), 0); return clamp(QLimiter, 0, 2);
} }
}; };

View File

@ -93,7 +93,7 @@ public:
/(max(mag(dcP), mag(dcN)) + SMALL); /(max(mag(dcP), mag(dcN)) + SMALL);
// Limit the limiter between linear and 20% upwind // Limit the limiter between linear and 20% upwind
return max(min(limiter, 1), 0.8); return clamp(limiter, 0.8, 1);
} }
}; };

View File

@ -141,7 +141,7 @@ public:
} }
// Limit the limiter between linear and upwind // Limit the limiter between linear and upwind
return max(min(limiter, 1), 0); return clamp(limiter, 0, 1);
} }
}; };

View File

@ -143,7 +143,7 @@ public:
} }
// Limit the limiter between linear and upwind // Limit the limiter between linear and upwind
return max(min(limiter, 1), 0); return clamp(limiter, 0, 1);
} }
}; };

View File

@ -108,7 +108,7 @@ public:
scalar limiter = 1 - k_*(dN - df)*(dP - df)/max(sqr(dN + dP), SMALL); scalar limiter = 1 - k_*(dN - df)*(dP - df)/max(sqr(dN + dP), SMALL);
// Limit the limiter between linear and upwind // Limit the limiter between linear and upwind
return max(min(limiter, 1), 0); return clamp(limiter, 0, 1);
} }
}; };

View File

@ -110,7 +110,7 @@ public:
scalar limiter = 1 - k_*(dN - df)*(dP - df)/max(sqr(dN + dP), SMALL); scalar limiter = 1 - k_*(dN - df)*(dP - df)/max(sqr(dN + dP), SMALL);
// Limit the limiter between linear and upwind // Limit the limiter between linear and upwind
return max(min(limiter, 1), 0); return clamp(limiter, 0, 1);
} }
}; };

View File

@ -124,11 +124,11 @@ public:
cubicLimiter = twor; cubicLimiter = twor;
} }
return max(min(cubicLimiter, 2), 0); return clamp(cubicLimiter, 0, 2);
*/ */
// Limit the limiter to obey the TVD constraint // Limit the limiter to obey the TVD constraint
return max(min(min(twor, cubicLimiter), 2), 0); return clamp(min(twor, cubicLimiter), 0, 2);
} }
}; };

View File

@ -122,7 +122,7 @@ public:
(fVphif - fVphiU)/stabilise(fVphiCD - fVphiU, SMALL); (fVphif - fVphiU)/stabilise(fVphiCD - fVphiU, SMALL);
// Limit the limiter to obey the TVD constraint // Limit the limiter to obey the TVD constraint
return max(min(min(twor, cubicLimiter), 2), 0); return clamp(min(twor, cubicLimiter), 0, 2);
} }
}; };

View File

@ -97,7 +97,7 @@ public:
faceFlux, phiP, phiN, gradcP, gradcN, d faceFlux, phiP, phiN, gradcP, gradcN, d
); );
return max(min(twoByk_*r, 1), 0); return clamp(twoByk_*r, 0, 1);
} }
}; };

View File

@ -36,7 +36,7 @@ Description
face-based Courant number and the lower and upper Courant number limits face-based Courant number and the lower and upper Courant number limits
supplied: supplied:
\f[ \f[
weight = 1 - max(min((Co - Co1)/(Co2 - Co1), 1), 0) weight = 1 - clamp((Co - Co1)/(Co2 - Co1), 0, 1)
\f] \f]
where where
\vartable \vartable
@ -222,18 +222,14 @@ public:
( (
vf.name() + "BlendingFactor", vf.name() + "BlendingFactor",
scalar(1) scalar(1)
- max - clamp
( (
min
( (
( mesh.time().deltaT()*mesh.deltaCoeffs()
mesh.time().deltaT()*mesh.deltaCoeffs() *mag(tUflux)/mesh.magSf()
*mag(tUflux)/mesh.magSf() - Co1_
- Co1_ )/(Co2_ - Co1_),
)/(Co2_ - Co1_), zero_one{}
scalar(1)
),
scalar(0)
) )
) )
); );

View File

@ -253,14 +253,10 @@ public:
( (
vf.name() + "BlendingFactor", vf.name() + "BlendingFactor",
scalar(1) scalar(1)
- max - clamp
( (
min (fvc::interpolate(Co) - Co1_)/(Co2_ - Co1_),
( zero_one{}
(fvc::interpolate(Co) - Co1_)/(Co2_ - Co1_),
scalar(1)
),
scalar(0)
) )
) )
); );

View File

@ -58,25 +58,24 @@ class clippedLinear
: :
public surfaceInterpolationScheme<Type> public surfaceInterpolationScheme<Type>
{ {
// Private data // Private Data
const scalar cellSizeRatio_;
scalar wfLimit_; scalar wfLimit_;
// Private Member Functions // Private Member Functions
void calcWfLimit() static scalar calcWfLimit(scalar cellSizeRatio)
{ {
if (cellSizeRatio_ <= 0 || cellSizeRatio_ > 1) if (cellSizeRatio <= 0 || cellSizeRatio > 1)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Given cellSizeRatio of " << cellSizeRatio_ << "Given cellSizeRatio of " << cellSizeRatio
<< " is not between 0 and 1" << " is not between 0 and 1"
<< exit(FatalError); << exit(FatalError);
} }
wfLimit_ = cellSizeRatio_/(1.0 + cellSizeRatio_); return cellSizeRatio/(1.0 + cellSizeRatio);
} }
@ -96,19 +95,15 @@ public:
clippedLinear(const fvMesh& mesh, const scalar cellSizeRatio) clippedLinear(const fvMesh& mesh, const scalar cellSizeRatio)
: :
surfaceInterpolationScheme<Type>(mesh), surfaceInterpolationScheme<Type>(mesh),
cellSizeRatio_(cellSizeRatio) wfLimit_(calcWfLimit(cellSizeRatio))
{ {}
calcWfLimit();
}
//- Construct from Istream //- Construct from Istream
clippedLinear(const fvMesh& mesh, Istream& is) clippedLinear(const fvMesh& mesh, Istream& is)
: :
surfaceInterpolationScheme<Type>(mesh), surfaceInterpolationScheme<Type>(mesh),
cellSizeRatio_(readScalar(is)) wfLimit_(calcWfLimit(readScalar(is)))
{ {}
calcWfLimit();
}
//- Construct from faceFlux and Istream //- Construct from faceFlux and Istream
clippedLinear clippedLinear
@ -119,10 +114,8 @@ public:
) )
: :
surfaceInterpolationScheme<Type>(mesh), surfaceInterpolationScheme<Type>(mesh),
cellSizeRatio_(readScalar(is)) wfLimit_(calcWfLimit(readScalar(is)))
{ {}
calcWfLimit();
}
// Member Functions // Member Functions
@ -141,6 +134,8 @@ public:
); );
const surfaceScalarField& cdWeights = tcdWeights(); const surfaceScalarField& cdWeights = tcdWeights();
const scalarMinMax bounds(wfLimit_, 1 - wfLimit_);
tmp<surfaceScalarField> tclippedLinearWeights tmp<surfaceScalarField> tclippedLinearWeights
( (
new surfaceScalarField new surfaceScalarField
@ -159,7 +154,7 @@ public:
tclippedLinearWeights.ref(); tclippedLinearWeights.ref();
clippedLinearWeights.primitiveFieldRef() = clippedLinearWeights.primitiveFieldRef() =
max(min(cdWeights.primitiveField(), 1 - wfLimit_), wfLimit_); clamp(cdWeights.primitiveField(), bounds);
surfaceScalarField::Boundary& clwbf = surfaceScalarField::Boundary& clwbf =
clippedLinearWeights.boundaryFieldRef(); clippedLinearWeights.boundaryFieldRef();
@ -169,15 +164,7 @@ public:
if (clwbf[patchi].coupled()) if (clwbf[patchi].coupled())
{ {
clwbf[patchi] = clwbf[patchi] =
max clamp(cdWeights.boundaryField()[patchi], bounds);
(
min
(
cdWeights.boundaryField()[patchi],
1 - wfLimit_
),
wfLimit_
);
} }
else else
{ {

View File

@ -131,7 +131,7 @@ bool Foam::ETAB<CloudType>::update
scalar phic = y1/a; scalar phic = y1/a;
// constrain phic within -1 to 1 // constrain phic within -1 to 1
phic = max(min(phic, 1), -1); phic = clamp(phic, -1, 1);
scalar phit = acos(phic); scalar phit = acos(phic);
scalar phi = phit; scalar phi = phit;

View File

@ -143,7 +143,7 @@ bool Foam::TAB<CloudType>::update
scalar phic = y1/a; scalar phic = y1/a;
// constrain phic within -1 to 1 // constrain phic within -1 to 1
phic = max(min(phic, 1), -1); phic = clamp(phic, -1, 1);
scalar phit = acos(phic); scalar phit = acos(phic);
scalar phi = phit; scalar phi = phit;

View File

@ -167,7 +167,7 @@ void Foam::lineSearch::reset()
// step_ = 2*(oldMeritValue_-prevMeritValue_)/directionalDeriv_; // step_ = 2*(oldMeritValue_-prevMeritValue_)/directionalDeriv_;
// Interpolate in order to get same improvement with the previous // Interpolate in order to get same improvement with the previous
// optimisation cycle // optimisation cycle
step_ = max(min(step_*prevMeritDeriv_/directionalDeriv_, 1.), minStep_); step_ = clamp(step_*prevMeritDeriv_/directionalDeriv_, minStep_, 1);
Info<< "\n------- Computing initial step-------" << endl; Info<< "\n------- Computing initial step-------" << endl;
Info<< "old dphi(0) " << prevMeritDeriv_ << endl; Info<< "old dphi(0) " << prevMeritDeriv_ << endl;
Info<< "dphi(0) " << directionalDeriv_ << endl; Info<< "dphi(0) " << directionalDeriv_ << endl;

View File

@ -617,7 +617,7 @@ void Foam::MassTransferPhaseSystem<BasePhaseSystem>::alphaTransfer
scalar dmdt21 = dmdtNet[celli]; scalar dmdt21 = dmdtNet[celli];
scalar coeffs12Cell = coeffs12[celli]; scalar coeffs12Cell = coeffs12[celli];
scalar alpha1Limited = max(min(alpha1[celli], 1.0), 0.0); scalar alpha1Limited = clamp(alpha1[celli], 0, 1);
// exp. // exp.
SuPhase1[celli] += coeffs1[celli]*dmdt21; SuPhase1[celli] += coeffs1[celli]*dmdt21;
@ -660,7 +660,7 @@ void Foam::MassTransferPhaseSystem<BasePhaseSystem>::alphaTransfer
scalar dmdt12 = -dmdtNet[celli]; scalar dmdt12 = -dmdtNet[celli];
scalar coeffs21Cell = -coeffs12[celli]; scalar coeffs21Cell = -coeffs12[celli];
scalar alpha2Limited = max(min(alpha2[celli], 1.0), 0.0); scalar alpha2Limited = clamp(alpha2[celli], 0, 1);
// exp // exp
SuPhase2[celli] += coeffs2[celli]*dmdt12; SuPhase2[celli] += coeffs2[celli]*dmdt12;

View File

@ -75,7 +75,7 @@ Foam::wallBoilingModels::TDNBModels::Shirai::TDNB
{ {
tmp<scalarField> tp = liquid.thermo().p().boundaryField()[patchi]; tmp<scalarField> tp = liquid.thermo().p().boundaryField()[patchi];
const scalarField pRatio(max(min(tp/Pc_, scalar(1)), scalar(0))); const scalarField pRatio(clamp(tp/Pc_, zero_one{}));
return return
( (

View File

@ -78,7 +78,11 @@ TolubinskiKostanchuk::dDeparture
const scalarField& L const scalarField& L
) const ) const
{ {
return max(min(dRef_*exp(-(Tsatw - Tl)/scalar(45)), dMax_), dMin_); return clamp
(
dRef_*exp(-(Tsatw - Tl)/scalar(45)),
scalarMinMax(dMin_, dMax_)
);
} }

View File

@ -148,7 +148,7 @@ tmp<scalarField> curvatureSeparation::calcCosAngle
volCosAngle.write(); volCosAngle.write();
} }
return max(min(cosAngle, scalar(1)), scalar(-1)); return clamp(cosAngle, scalarMinMax(-1, 1));
} }

View File

@ -215,7 +215,7 @@ tmp<scalarField> curvatureSeparation::calcCosAngle
volCosAngle.write(); volCosAngle.write();
} }
return max(min(cosAngle, scalar(1)), scalar(-1)); return clamp(cosAngle, scalarMinMax(-1, 1));
} }

View File

@ -279,7 +279,7 @@ void Foam::reconstruction::plicRDF::calcResidual
if (mag(normal) != 0 && j != 0) if (mag(normal) != 0 && j != 0)
{ {
vector n = normal/mag(normal); vector n = normal/mag(normal);
scalar cosAngle = max(min((cellNormal & n), 1), -1); scalar cosAngle = clamp((cellNormal & n), -1, 1);
avgDiffNormal += acos(cosAngle) * mag(normal); avgDiffNormal += acos(cosAngle) * mag(normal);
weight += mag(normal); weight += mag(normal);
if (cosAngle < maxDiffNormal) if (cosAngle < maxDiffNormal)

View File

@ -133,11 +133,8 @@ void Foam::alphaContactAngleTwoPhaseFvPatchScalarField::evaluate
gradient() = gradient() =
patch().deltaCoeffs() patch().deltaCoeffs()
*( *(
max(min clamp(*this + gradient()/patch().deltaCoeffs(), zero_one{})
( - *this
*this + gradient()/patch().deltaCoeffs(),
scalar(1)), scalar(0)
) - *this
); );
} }
else if (limit_ == lcZeroGradient) else if (limit_ == lcZeroGradient)
@ -149,7 +146,7 @@ void Foam::alphaContactAngleTwoPhaseFvPatchScalarField::evaluate
if (limit_ == lcAlpha) if (limit_ == lcAlpha)
{ {
scalarField::operator=(max(min(*this, scalar(1)), scalar(0))); scalarField::operator=(clamp(*this, zero_one{}));
} }
} }