COMP: rename field methods clamp() -> clamp_range()

- this is slightly longer to write (but consistent with clamp_min
  etc). The main reason is that this allows easier use of the clamp()
  free function.

STYLE: skip checks for bad/invalid clamping ranges

- ranges are either already validated before calling, the caller logic
  has already made the branching decision.
This commit is contained in:
Mark Olesen
2023-02-02 14:15:24 +01:00
parent a7d77391fa
commit 4d7180ae7c
8 changed files with 158 additions and 160 deletions

View File

@ -162,9 +162,16 @@ int main(int argc, char *argv[])
Info<< "result: " << result << nl;
someField.clamp(zero_one{});
someField.clamp_range(zero_one{});
Info<< "inplace: " << someField << nl;
scalar val(1.414);
Info<< "clamp " << val
// nope << " : " << clamp(val, zero_one{})
// nope << " : " << clamp(val, scalarMinMax(zero_one{}))
<< " : " << clamp(val, 0, 1)
<< nl;
}
Info<< nl << "\nDone\n" << endl;

View File

@ -282,39 +282,6 @@ void FieldField<Field, Type>::replace
}
template<template<class> class Field, class Type>
void FieldField<Field, Type>::clamp
(
const Type& lower,
const Type& upper
)
{
if (lower < upper)
{
for (auto& ff : *this)
{
ff.clamp(lower, upper);
}
}
}
template<template<class> class Field, class Type>
void FieldField<Field, Type>::clamp
(
const MinMax<Type>& range
)
{
if (range.min() < range.max())
{
for (auto& ff : *this)
{
ff.clamp(range.min(), range.max());
}
}
}
template<template<class> class Field, class Type>
void FieldField<Field, Type>::clamp_min
(
@ -341,6 +308,37 @@ void FieldField<Field, Type>::clamp_max
}
template<template<class> class Field, class Type>
void FieldField<Field, Type>::clamp_range
(
const Type& lower,
const Type& upper
)
{
// Note: no checks for bad/invalid clamping ranges
for (auto& ff : *this)
{
ff.clamp_range(lower, upper);
}
}
template<template<class> class Field, class Type>
void FieldField<Field, Type>::clamp_range
(
const MinMax<Type>& range
)
{
// Note: no checks for bad/invalid clamping ranges
for (auto& ff : *this)
{
ff.clamp_range(range.min(), range.max());
}
}
template<template<class> class Field, class Type>
tmp<FieldField<Field, Type>> FieldField<Field, Type>::T() const
{

View File

@ -149,20 +149,20 @@ public:
//- Replace a component field of the field
void replace(const direction, const cmptType&);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const MinMax<Type>& range);
//- Impose lower (floor) clamp on the field values (in-place)
void clamp_min(const Type& lower);
//- Impose upper (ceiling) clamp on the field values (in-place)
void clamp_max(const Type& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const MinMax<Type>& range);
//- Return the field transpose (only defined for second rank tensors)
tmp<FieldField<Field, Type>> T() const;

View File

@ -658,27 +658,6 @@ void Foam::Field<Type>::replace
}
template<class Type>
void Foam::Field<Type>::clamp(const Type& lower, const Type& upper)
{
// Use free functions min(), max() to impose component-wise clamping
if (lower < upper)
{
// std::for_each
for (auto& val : *this)
{
val = min(max(val, lower), upper);
}
}
}
template<class Type>
void Foam::Field<Type>::clamp(const MinMax<Type>& range)
{
clamp(range.min(), range.max());
}
template<class Type>
void Foam::Field<Type>::clamp_min(const Type& lower)
{
@ -702,6 +681,26 @@ void Foam::Field<Type>::clamp_max(const Type& upper)
}
template<class Type>
void Foam::Field<Type>::clamp_range(const Type& lower, const Type& upper)
{
// Note: no checks for bad/invalid clamping ranges
// Use free functions min(), max() to impose component-wise clamping
// std::for_each
for (auto& val : *this)
{
val = min(max(val, lower), upper);
}
}
template<class Type>
void Foam::Field<Type>::clamp_range(const MinMax<Type>& range)
{
Field<Type>::clamp_range(range.min(), range.max());
}
template<class Type>
template<class VSForm>
VSForm Foam::Field<Type>::block(const label start) const

View File

@ -434,20 +434,20 @@ public:
//- Replace a component field of the field
void replace(const direction, const cmptType&);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const MinMax<Type>& range);
//- Impose lower (floor) clamp on the field values (in-place)
void clamp_min(const Type& lower);
//- Impose upper (ceiling) clamp on the field values (in-place)
void clamp_max(const Type& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const MinMax<Type>& range);
template<class VSForm>
VSForm block(const label start) const;

View File

@ -664,28 +664,22 @@ void clamp
const MinMax<Type>& range
)
{
// Note: no checks for bad/invalid clamping ranges
if (result.cdata() == f1.cdata())
{
// Apply in-place
result.clamp(range);
result.clamp_range(range);
}
else
{
if (range.good())
{
std::transform
(
f1.cbegin(),
f1.cbegin(result.size()),
result.begin(),
clampOp<Type>(range)
);
}
else
{
// No clamping
std::copy(f1.cbegin(), f1.cbegin(result.size()), result.begin());
}
std::transform
(
f1.cbegin(),
f1.cbegin(result.size()),
result.begin(),
clampOp<Type>(range)
);
}
}

View File

@ -1249,50 +1249,6 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::replace
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp
(
const Type& lower,
const Type& upper
)
{
primitiveFieldRef().clamp(lower, upper);
boundaryFieldRef().clamp(lower, upper);
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp
(
const MinMax<Type>& range
)
{
primitiveFieldRef().clamp(range.min(), range.max());
boundaryFieldRef().clamp(range.min(), range.max());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp
(
const dimensioned<Type>& lower,
const dimensioned<Type>& upper
)
{
this->clamp(lower.value(), upper.value());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp
(
const dimensioned<MinMax<Type>>& range
)
{
this->clamp(range.value());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_min
(
@ -1335,6 +1291,50 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_max
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_range
(
const Type& lower,
const Type& upper
)
{
primitiveFieldRef().clamp_range(lower, upper);
boundaryFieldRef().clamp_range(lower, upper);
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_range
(
const MinMax<Type>& range
)
{
primitiveFieldRef().clamp_range(range.min(), range.max());
boundaryFieldRef().clamp_range(range.min(), range.max());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_range
(
const dimensioned<Type>& lower,
const dimensioned<Type>& upper
)
{
this->clamp_range(lower.value(), upper.value());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clamp_range
(
const dimensioned<MinMax<Type>>& range
)
{
this->clamp_range(range.value());
}
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::negate()
{

View File

@ -629,26 +629,6 @@ public:
const dimensioned<cmptType>& ds
);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range.
void clamp(const MinMax<Type>& range);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range. No dimension checking.
void clamp
(
const dimensioned<Type>& lower,
const dimensioned<Type>& upper
);
//- Clamp field values (in-place) to the specified range.
// A no-op for an invalid range. No dimension checking.
void clamp(const dimensioned<MinMax<Type>>& range);
//- Impose lower (floor) clamp on the field values (in-place)
void clamp_min(const Type& lower);
@ -663,6 +643,26 @@ public:
// No dimension checking
void clamp_max(const dimensioned<Type>& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not. No dimension checking.
void clamp_range(const dimensioned<MinMax<Type>>& range);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const Type& lower, const Type& upper);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not.
void clamp_range(const MinMax<Type>& range);
//- Clamp field values (in-place) to the specified range.
// Does not check if range is valid or not. No dimension checking.
void clamp_range
(
const dimensioned<Type>& lower,
const dimensioned<Type>& upper
);
// Member Operators
@ -715,17 +715,17 @@ public:
// Housekeeping
//- Clamp field values (in-place) to the specified range.
// \deprecated(2023-01) prefer clamp() naming
// \deprecated(2023-01) prefer clamp_range() naming
void clip(const dimensioned<MinMax<Type>>& range)
{
this->clamp(range);
this->clamp_range(range);
}
//- Clamp field values (in-place) to the specified range.
// \deprecated(2023-01) prefer clamp() naming
// \deprecated(2023-01) prefer clamp_range() naming
void clip(const dimensioned<Type>& lo, const dimensioned<Type>& hi)
{
this->clamp(lo.value(), hi.value());
this->clamp_range(lo.value(), hi.value());
}
//- Use minimum of the field and specified value. ie, clamp_max().
@ -738,12 +738,12 @@ public:
// \deprecated(2023-01) prefer clamp_min()
void max(const dimensioned<Type>& lower) { this->clamp_min(lower); }
//- Deprecated(2019-01) identical to clamp()
// \deprecated(2019-01) identical to clamp()
FOAM_DEPRECATED_FOR(2019-01, "clamp() method")
//- Deprecated(2019-01) identical to clamp_range()
// \deprecated(2019-01) identical to clamp_range()
FOAM_DEPRECATED_FOR(2019-01, "clamp_range() method")
void maxMin(const dimensioned<Type>& lo, const dimensioned<Type>& hi)
{
return this->clamp(lo.value(), hi.value());
return this->clamp_range(lo.value(), hi.value());
}
};