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 =
max
(
min
(
(rho - rholSat)/(rhovSat - rholSat),
scalar(1)
),
scalar(0)
);
alphav = clamp((rho - rholSat)/(rhovSat - rholSat), zero_one{});
alphal = 1.0 - alphav;
Info<< "max-min alphav: " << max(alphav).value()

View File

@ -108,7 +108,7 @@ void setAlpha
{
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))
{

View File

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

View File

@ -100,7 +100,7 @@ protected:
//- that forms the basis of many more complex ramp functions
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 = max(min(lambda, 0.1), -0.1);
lambda = clamp(lambda, -0.1, 0.1);
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 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 CtauI = min(C1_/(Da*sqrt(ReT + 1)), 2.1377);
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 =
CgammaI*pow025(nu*epsilon[i]/(sqr(k[i]) + SMALL));

View File

@ -80,7 +80,7 @@
);
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;
}

View File

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

View File

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

View File

@ -148,9 +148,9 @@ void Foam::phaseHydrostaticPressureFvPatchScalarField::updateCoeffs()
meshObjects::gravity::New(db().time());
// 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() = max(min(alphap, scalar(1)), scalar(0));
valueFraction() = clamp(alphap, zero_one{});
refValue() =
pRefValue_

View File

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

View File

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

View File

@ -81,7 +81,7 @@ public:
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
);
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_);
// 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);
// 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);
// 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);
// 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
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
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);
// 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);
// 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;
}
return max(min(cubicLimiter, 2), 0);
return clamp(cubicLimiter, 0, 2);
*/
// 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);
// 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
);
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
supplied:
\f[
weight = 1 - max(min((Co - Co1)/(Co2 - Co1), 1), 0)
weight = 1 - clamp((Co - Co1)/(Co2 - Co1), 0, 1)
\f]
where
\vartable
@ -222,18 +222,14 @@ public:
(
vf.name() + "BlendingFactor",
scalar(1)
- max
- clamp
(
min
(
(
mesh.time().deltaT()*mesh.deltaCoeffs()
*mag(tUflux)/mesh.magSf()
- Co1_
)/(Co2_ - Co1_),
scalar(1)
),
scalar(0)
mesh.time().deltaT()*mesh.deltaCoeffs()
*mag(tUflux)/mesh.magSf()
- Co1_
)/(Co2_ - Co1_),
zero_one{}
)
)
);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -75,7 +75,7 @@ Foam::wallBoilingModels::TDNBModels::Shirai::TDNB
{
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
(

View File

@ -78,7 +78,11 @@ TolubinskiKostanchuk::dDeparture
const scalarField& L
) 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();
}
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();
}
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)
{
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);
weight += mag(normal);
if (cosAngle < maxDiffNormal)

View File

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