diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C index 750694667786b3dcffe95c9569809b6f0af6cd8b..1f6d3e6a6479ade8d514f4f379d0dc5b513d2911 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C @@ -31,13 +31,35 @@ License // Required by clang 5 for correct instantiation of Function1::New #include "Constant.H" +template<class Type> +void Foam::Function1<Type>::addLimitedAreaToSum +( + const scalar x1, + const scalar x2, + Type& sum +) const +{ + if (x1 < minLimit_) + { + sum += (min(minLimit_, x2) - x1)*this->value(minLimit_); + } + + if (x2 > maxLimit_) + { + sum += (x2 - max(maxLimit_, x1))*this->value(maxLimit_); + } +} + + // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * // template<class Type> Foam::Function1<Type>::Function1(const word& entryName) : refCount(), - name_(entryName) + name_(entryName), + minLimit_(-GREAT), + maxLimit_(GREAT) {} @@ -45,7 +67,9 @@ template<class Type> Foam::Function1<Type>::Function1(const word& entryName, const dictionary& dict) : refCount(), - name_(entryName) + name_(entryName), + minLimit_(dict.getOrDefault<scalar>("minLimit", -GREAT)), + maxLimit_(dict.getOrDefault<scalar>("maxLimit", GREAT)) {} @@ -53,15 +77,27 @@ template<class Type> Foam::Function1<Type>::Function1(const Function1<Type>& rhs) : refCount(), - name_(rhs.name_) + name_(rhs.name_), + minLimit_(rhs.minLimit_), + maxLimit_(rhs.maxLimit_) {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class Type> -void Foam::Function1<Type>::convertTimeBase(const Time&) -{} +void Foam::Function1<Type>::convertTimeBase(const Time& t) +{ + minLimit_ = t.userTimeToTime(minLimit_); + maxLimit_ = t.userTimeToTime(maxLimit_); +} + + +template<class Type> +Foam::scalar Foam::Function1<Type>::limitValue(const scalar x) const +{ + return min(max(minLimit_, x), maxLimit_); +} template<class Type> diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H index 7de3fd720717fe733aadb03df268e780bd635bf8..c72be287da7ec2585c84bd83ac467c6761109978 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H @@ -116,12 +116,26 @@ protected: //- Name of entry const word name_; + //- Optional minimum limit (x) + scalar minLimit_; + + //- Optional maximum limit (x) + scalar maxLimit_; + // Protected Member Functions //- No copy assignment void operator=(const Function1<Type>&) = delete; + //- Helper function for integrate() function to add limited contribution + void addLimitedAreaToSum + ( + const scalar x1, + const scalar x2, + Type& sum + ) const; + public: @@ -217,6 +231,9 @@ public: //- Convert time virtual void convertTimeBase(const Time&); + //- Apply min|max limit to the input value + virtual scalar limitValue(const scalar x) const; + // Evaluation diff --git a/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.C b/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.C index a21970355696bd5d8fe3ddadac40ceee13a4c286..b6e29177411e63bf6128fc9a6ac70149f34eaf22 100644 --- a/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.C +++ b/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.C @@ -144,13 +144,15 @@ void Foam::Function1Types::Polynomial<Type>::convertTimeBase(const Time& t) template<class Type> Type Foam::Function1Types::Polynomial<Type>::value(const scalar x) const { + const scalar xlim = this->limitValue(x); + Type y(Zero); forAll(coeffs_, i) { y += cmptMultiply ( coeffs_[i].first(), - cmptPow(pTraits<Type>::one*x, coeffs_[i].second()) + cmptPow(pTraits<Type>::one*xlim, coeffs_[i].second()) ); } @@ -167,6 +169,11 @@ Type Foam::Function1Types::Polynomial<Type>::integrate { Type intx(Zero); + this->addLimitedAreaToSum(x1, x2, intx); + + const scalar x1lim = this->limitValue(x1); + const scalar x2lim = this->limitValue(x2); + if (canIntegrate_) { forAll(coeffs_, i) @@ -180,12 +187,12 @@ Type Foam::Function1Types::Polynomial<Type>::integrate ), cmptPow ( - pTraits<Type>::one*x2, + pTraits<Type>::one*x2lim, coeffs_[i].second() + pTraits<Type>::one ) - cmptPow ( - pTraits<Type>::one*x1, + pTraits<Type>::one*x1lim, coeffs_[i].second() + pTraits<Type>::one ) ); diff --git a/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.H b/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.H index 06d778c0c6cadbeaa964399368c59cf3789f7434..5f41d7ab46fc6f5ce7e16e048763b444b886aea2 100644 --- a/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.H +++ b/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -103,6 +104,12 @@ public: //- Copy constructor explicit Polynomial(const Polynomial& poly); + //- Construct and return a clone + virtual tmp<Function1<Type>> clone() const + { + return tmp<Function1<Type>>(new Polynomial<Type>(*this)); + } + //- Destructor virtual ~Polynomial() = default; diff --git a/src/OpenFOAM/primitives/functions/Function1/Scale/ScaleI.H b/src/OpenFOAM/primitives/functions/Function1/Scale/ScaleI.H index fe201a48108357c37ed9e79a86a4c62ee26bb6be..515307be29ce1db475a88e691793b1e7feffc421 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Scale/ScaleI.H +++ b/src/OpenFOAM/primitives/functions/Function1/Scale/ScaleI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,7 +33,9 @@ License template<class Type> inline Type Foam::Function1Types::Scale<Type>::value(const scalar t) const { - return scale_->value(t)*value_->value(t); + const scalar tlim = this->limitValue(t); + + return scale_->value(tlim)*value_->value(tlim); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H b/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H index be03d62ddce81c60e3ed83ecc70392860b71fccb..722562c5c94a5ddd7f31da4010f5689f21a48c04 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/SineI.H @@ -92,9 +92,11 @@ Foam::Function1Types::Sine<Type>::squareForm template<class Type> inline Type Foam::Function1Types::Sine<Type>::cosValue(const scalar t) const { + const scalar tlim = this->limitValue(t); + return ( - cosForm(t) * scale_->value(t) + level_->value(t) + cosForm(tlim) * scale_->value(tlim) + level_->value(tlim) ); } @@ -102,9 +104,11 @@ inline Type Foam::Function1Types::Sine<Type>::cosValue(const scalar t) const template<class Type> inline Type Foam::Function1Types::Sine<Type>::sinValue(const scalar t) const { + const scalar tlim = this->limitValue(t); + return ( - sinForm(t) * scale_->value(t) + level_->value(t) + sinForm(tlim) * scale_->value(tlim) + level_->value(tlim) ); } @@ -116,9 +120,11 @@ inline Type Foam::Function1Types::Sine<Type>::squareValue const scalar posFrac ) const { + const scalar tlim = this->limitValue(t); + return ( - squareForm(t, posFrac) * scale_->value(t) + level_->value(t) + squareForm(tlim, posFrac) * scale_->value(tlim) + level_->value(tlim) ); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C index 8c1fdb25957f4dd488cefe171a883be74b4d9284..715df68417d8f4d22997d3a4fe54db52feab4faf 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C +++ b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C @@ -98,13 +98,6 @@ Foam::Function1Types::TableBase<Type>::TableBase(const TableBase<Type>& tbl) {} -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -template<class Type> -Foam::Function1Types::TableBase<Type>::~TableBase() -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class Type> @@ -270,9 +263,11 @@ void Foam::Function1Types::TableBase<Type>::convertTimeBase(const Time& t) template<class Type> Type Foam::Function1Types::TableBase<Type>::value(const scalar x) const { - scalar xDash = x; + const scalar xlim = this->limitValue(x); + + scalar xDash = xlim; - if (checkMinBounds(x, xDash)) + if (checkMinBounds(xlim, xDash)) { return table_.first().second(); } @@ -286,7 +281,7 @@ Type Foam::Function1Types::TableBase<Type>::value(const scalar x) const interpolator().valueWeights(xDash, currentIndices_, currentWeights_); Type t = currentWeights_[0]*table_[currentIndices_[0]].second(); - for (label i = 1; i < currentIndices_.size(); i++) + for (label i = 1; i < currentIndices_.size(); ++i) { t += currentWeights_[i]*table_[currentIndices_[i]].second(); } @@ -302,15 +297,31 @@ Type Foam::Function1Types::TableBase<Type>::integrate const scalar x2 ) const { - // Use interpolator - interpolator().integrationWeights(x1, x2, currentIndices_, currentWeights_); + scalar x1lim = this->limitValue(x1); + scalar x2lim = this->limitValue(x2); - Type sum = currentWeights_[0]*table_[currentIndices_[0]].second(); - for (label i = 1; i < currentIndices_.size(); i++) + Type sum = Zero; + + if (mag(x2lim - x1lim) > ROOTVSMALL) { - sum += currentWeights_[i]*table_[currentIndices_[i]].second(); + // Use interpolator + interpolator().integrationWeights + ( + this->limitValue(x1), + this->limitValue(x2), + currentIndices_, + currentWeights_ + ); + + sum += currentWeights_[0]*table_[currentIndices_[0]].second(); + for (label i = 1; i < currentIndices_.size(); ++i) + { + sum += currentWeights_[i]*table_[currentIndices_[i]].second(); + } } + this->addLimitedAreaToSum(x1, x2, sum); + return sum; } @@ -323,7 +334,7 @@ Foam::tmp<Foam::scalarField> Foam::Function1Types::TableBase<Type>::x() const forAll(table_, i) { - fld[i] = table_[i].first(); + fld[i] = this->limitValue(table_[i].first()); } return tfld; diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H index 82361382bd2f2573761814b678f88b0217154a29..704230be74f993f1837c1d120aad77ade487ecc2 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H +++ b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H @@ -108,7 +108,7 @@ public: //- Destructor - virtual ~TableBase(); + virtual ~TableBase() = default; // Member Functions diff --git a/src/OpenFOAM/primitives/functions/Function1/halfCosineRamp/halfCosineRampI.H b/src/OpenFOAM/primitives/functions/Function1/halfCosineRamp/halfCosineRampI.H index 1f50e6c86807ca17511d04d913d36ff7ba21c886..641bacb6e0228ea0308976ff0444e455af5899b4 100644 --- a/src/OpenFOAM/primitives/functions/Function1/halfCosineRamp/halfCosineRampI.H +++ b/src/OpenFOAM/primitives/functions/Function1/halfCosineRamp/halfCosineRampI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,7 +36,9 @@ inline Foam::scalar Foam::Function1Types::halfCosineRamp::value const scalar t ) const { - return 0.5*(1 - cos(constant::mathematical::pi*linearRamp(t))); + return + 0.5 + *(1 - cos(constant::mathematical::pi*linearRamp(this->limitValue(t)))); } diff --git a/src/OpenFOAM/primitives/functions/Function1/linearRamp/linearRampI.H b/src/OpenFOAM/primitives/functions/Function1/linearRamp/linearRampI.H index 318323c3427a85cf3f3ff5c3e61cd6bb8c18eb90..3670f60fe0666434c210f47c497e56d6a149e313 100644 --- a/src/OpenFOAM/primitives/functions/Function1/linearRamp/linearRampI.H +++ b/src/OpenFOAM/primitives/functions/Function1/linearRamp/linearRampI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,7 +35,7 @@ inline Foam::scalar Foam::Function1Types::linearRamp::value const scalar t ) const { - return ramp::linearRamp(t); + return ramp::linearRamp(this->limitValue(t)); } diff --git a/src/OpenFOAM/primitives/functions/Function1/quadraticRamp/quadraticRampI.H b/src/OpenFOAM/primitives/functions/Function1/quadraticRamp/quadraticRampI.H index 14a850ac65f1d5c78b164f12014c8f5e4df24184..f0b2ea02a52f835f88c36d18bb1a4dc696da7c12 100644 --- a/src/OpenFOAM/primitives/functions/Function1/quadraticRamp/quadraticRampI.H +++ b/src/OpenFOAM/primitives/functions/Function1/quadraticRamp/quadraticRampI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,7 +35,7 @@ inline Foam::scalar Foam::Function1Types::quadraticRamp::value const scalar t ) const { - return sqr(linearRamp(t)); + return sqr(linearRamp(this->limitValue(t))); } diff --git a/src/OpenFOAM/primitives/functions/Function1/quarterCosineRamp/quarterCosineRampI.H b/src/OpenFOAM/primitives/functions/Function1/quarterCosineRamp/quarterCosineRampI.H index 4b5b2fabc5beea5b42d3ae8753f90ca642b230de..582e209cda594065317ecc556325ad1919b51528 100644 --- a/src/OpenFOAM/primitives/functions/Function1/quarterCosineRamp/quarterCosineRampI.H +++ b/src/OpenFOAM/primitives/functions/Function1/quarterCosineRamp/quarterCosineRampI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,7 +36,8 @@ inline Foam::scalar Foam::Function1Types::quarterCosineRamp::value const scalar t ) const { - return 1 - cos(0.5*constant::mathematical::pi*linearRamp(t)); + return + 1 - cos(0.5*constant::mathematical::pi*linearRamp(this->limitValue(t))); } diff --git a/src/OpenFOAM/primitives/functions/Function1/quarterSineRamp/quarterSineRampI.H b/src/OpenFOAM/primitives/functions/Function1/quarterSineRamp/quarterSineRampI.H index ea7d9a9a49a2eff20388ae3272c6580166f9abab..002482fd530269d5935a5e1cc947508f120f0bdc 100644 --- a/src/OpenFOAM/primitives/functions/Function1/quarterSineRamp/quarterSineRampI.H +++ b/src/OpenFOAM/primitives/functions/Function1/quarterSineRamp/quarterSineRampI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -35,7 +36,7 @@ inline Foam::scalar Foam::Function1Types::quarterSineRamp::value const scalar t ) const { - return sin(0.5*constant::mathematical::pi*linearRamp(t)); + return sin(0.5*constant::mathematical::pi*linearRamp(this->limitValue(t))); } diff --git a/src/OpenFOAM/primitives/functions/Function1/step/stepFunctionI.H b/src/OpenFOAM/primitives/functions/Function1/step/stepFunctionI.H index c3016813892b12696cb01e724222b2c9bd6b12fe..498507792870109e5ac6ad798a1889cdb9efbf35 100644 --- a/src/OpenFOAM/primitives/functions/Function1/step/stepFunctionI.H +++ b/src/OpenFOAM/primitives/functions/Function1/step/stepFunctionI.H @@ -34,7 +34,9 @@ inline Foam::scalar Foam::Function1Types::stepFunction::value const scalar t ) const { - if (t < start_ || t > start_ + duration_) + const scalar tlim = this->limitValue(t); + + if (tlim < start_ || tlim > start_ + duration_) { return 0; }