From bb3660b9a5b0d2a4054cd3eb27b981453911a1c0 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 25 Sep 2020 13:41:20 +0200 Subject: [PATCH] ENH: add construct ConstantField with uniform value --- .../functions/Function1/Constant/Constant.C | 9 +--- .../functions/Function1/Constant/Constant.H | 2 +- .../functions/Function1/Function1/Function1.H | 1 + .../Function1/Function1/Function1New.C | 13 +++-- .../ConstantField/ConstantField.C | 54 ++++++++++++------- .../ConstantField/ConstantField.H | 14 ++++- .../ConstantField/ConstantFieldI.H | 3 +- src/meshTools/PatchFunction1/PatchFunction1.C | 8 +-- src/meshTools/PatchFunction1/PatchFunction1.H | 31 +++++------ .../PatchFunction1/PatchFunction1New.C | 17 +++--- .../UniformValueField/UniformValueField.H | 4 +- .../UniformValueField/UniformValueFieldI.H | 36 +++++-------- 12 files changed, 96 insertions(+), 96 deletions(-) diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C index 60a6a01b4d..869dc2a1b8 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C @@ -52,7 +52,7 @@ Foam::Function1Types::Constant::Constant Function1(entryName), value_(Zero) { - Istream& is(dict.lookup(entryName)); + Istream& is = dict.lookup(entryName); word entryType(is); is >> value_; } @@ -78,13 +78,6 @@ Foam::Function1Types::Constant::Constant(const Constant& cnst) {} -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -template -Foam::Function1Types::Constant::~Constant() -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H index 49713da4a1..e2b063aeef 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H @@ -103,7 +103,7 @@ public: //- Destructor - virtual ~Constant(); + virtual ~Constant() = default; // Member Functions diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H index 46f286ec77..8ac9135b68 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H @@ -103,6 +103,7 @@ protected: //- Name of entry const word name_; + // Protected Member Functions //- No copy assignment diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C index 56dc27900d..85084f82a2 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,10 +28,11 @@ License #include "Constant.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // template -Foam::autoPtr> Foam::Function1::New +Foam::autoPtr> +Foam::Function1::New ( const word& entryName, const dictionary& dict, @@ -86,10 +87,14 @@ Foam::autoPtr> Foam::Function1::New if (!firstToken.isWord()) { + // Backwards-compatibility for reading straight fields is.putBack(firstToken); + + const Type constValue = pTraits(is); + return autoPtr> ( - new Function1Types::Constant(entryName, is) + new Function1Types::Constant(entryName, constValue) ); } diff --git a/src/meshTools/PatchFunction1/ConstantField/ConstantField.C b/src/meshTools/PatchFunction1/ConstantField/ConstantField.C index 5317ff8053..3467d1e84b 100644 --- a/src/meshTools/PatchFunction1/ConstantField/ConstantField.C +++ b/src/meshTools/PatchFunction1/ConstantField/ConstantField.C @@ -29,6 +29,23 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // +template +Foam::PatchFunction1Types::ConstantField::ConstantField +( + const polyPatch& pp, + const word& entryName, + const Type& uniformValue, + const dictionary& dict, + const bool faceValues +) +: + PatchFunction1(pp, entryName, dict, faceValues), + isUniform_(true), + uniformValue_(uniformValue), + value_((faceValues ? pp.size() : pp.nPoints()), uniformValue_) +{} + + template Foam::PatchFunction1Types::ConstantField::ConstantField ( @@ -36,7 +53,7 @@ Foam::PatchFunction1Types::ConstantField::ConstantField const word& entryName, const bool isUniform, const Type& uniformValue, - const Field& nonUniformValue, + const Field& fieldValues, const dictionary& dict, const bool faceValues ) @@ -44,21 +61,18 @@ Foam::PatchFunction1Types::ConstantField::ConstantField PatchFunction1(pp, entryName, dict, faceValues), isUniform_(isUniform), uniformValue_(uniformValue), - value_(nonUniformValue) + value_(fieldValues) { - if (faceValues && nonUniformValue.size() != pp.size()) + const label len = (faceValues ? pp.size() : pp.nPoints()); + + if (fieldValues.size() != len) { FatalIOErrorInFunction(dict) - << "Supplied field size " << nonUniformValue.size() - << " is not equal to the number of faces " << pp.size() - << " of patch " << pp.name() << exit(FatalIOError); - } - else if (!faceValues && nonUniformValue.size() != pp.nPoints()) - { - FatalIOErrorInFunction(dict) - << "Supplied field size " << nonUniformValue.size() - << " is not equal to the number of points " << pp.nPoints() - << " of patch " << pp.name() << exit(FatalIOError); + << "Supplied field size " << fieldValues.size() + << " is not equal to the number of " + << (faceValues ? "faces" : "points") << ' ' + << len << " of patch " << pp.name() << nl + << exit(FatalIOError); } } @@ -103,7 +117,7 @@ Foam::Field Foam::PatchFunction1Types::ConstantField::getValue is >> list; isUniform = false; - label currentSize = fld.size(); + const label currentSize = fld.size(); if (currentSize != len) { if @@ -120,7 +134,7 @@ Foam::Field Foam::PatchFunction1Types::ConstantField::getValue << endl; #endif - // Resize the data + // Resize (shrink) the data fld.setSize(len); } else @@ -204,13 +218,13 @@ Foam::PatchFunction1Types::ConstantField::ConstantField uniformValue_(cnst.uniformValue_), value_(cnst.value_) { - // If different sizes do what? - value_.setSize + // If sizes are different... + value_.resize ( - this->faceValues_ - ? this->patch_.size() - : this->patch_.nPoints() + (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints()), + Zero ); + if (isUniform_) { value_ = uniformValue_; diff --git a/src/meshTools/PatchFunction1/ConstantField/ConstantField.H b/src/meshTools/PatchFunction1/ConstantField/ConstantField.H index 0cdae26d18..81ce0d6b96 100644 --- a/src/meshTools/PatchFunction1/ConstantField/ConstantField.H +++ b/src/meshTools/PatchFunction1/ConstantField/ConstantField.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -96,6 +96,16 @@ public: // Constructors + //- Construct from a uniform value + ConstantField + ( + const polyPatch& pp, + const word& entryName, + const Type& uniformValue, + const dictionary& dict = dictionary::null, + const bool faceValues = true + ); + //- Construct from components ConstantField ( @@ -103,7 +113,7 @@ public: const word& entryName, const bool isUniform, const Type& uniformValue, - const Field& nonUniformValue, + const Field& fieldValues, const dictionary& dict = dictionary::null, const bool faceValues = true ); diff --git a/src/meshTools/PatchFunction1/ConstantField/ConstantFieldI.H b/src/meshTools/PatchFunction1/ConstantField/ConstantFieldI.H index 7ed9ffff02..7427db0139 100644 --- a/src/meshTools/PatchFunction1/ConstantField/ConstantFieldI.H +++ b/src/meshTools/PatchFunction1/ConstantField/ConstantFieldI.H @@ -5,8 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. diff --git a/src/meshTools/PatchFunction1/PatchFunction1.C b/src/meshTools/PatchFunction1/PatchFunction1.C index f01196a5d1..c833c3ab66 100644 --- a/src/meshTools/PatchFunction1/PatchFunction1.C +++ b/src/meshTools/PatchFunction1/PatchFunction1.C @@ -232,19 +232,19 @@ void Foam::PatchFunction1::writeData(Ostream& os) const } -// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * IOStream Operators * * * * * * * * * * * * * // template Foam::Ostream& Foam::operator<< ( Ostream& os, - const PatchFunction1& pf1 + const PatchFunction1& rhs ) { os.check(FUNCTION_NAME); - os << pf1.name_; - pf1.writeData(os); + os << rhs.name_; + rhs.writeData(os); return os; } diff --git a/src/meshTools/PatchFunction1/PatchFunction1.H b/src/meshTools/PatchFunction1/PatchFunction1.H index bad8755982..6a812af2a5 100644 --- a/src/meshTools/PatchFunction1/PatchFunction1.H +++ b/src/meshTools/PatchFunction1/PatchFunction1.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,19 +56,12 @@ SeeAlso namespace Foam { -// Forward declarations +// Forward Declarations class Time; - -// Forward declaration of friend functions and operators -template -class PatchFunction1; +template class PatchFunction1; template -Ostream& operator<< -( - Ostream&, - const PatchFunction1& -); +Ostream& operator<<(Ostream&, const PatchFunction1&); /*---------------------------------------------------------------------------*\ Class PatchFunction1 Declaration @@ -79,15 +72,9 @@ class PatchFunction1 : public refCount { - // Private Member Functions - - //- No copy assignment - void operator=(const PatchFunction1&) = delete; - - protected: - // Protected data + // Protected Data //- Name of entry const word name_; @@ -102,6 +89,12 @@ protected: coordinateScaling coordSys_; + // Protected Member Functions + + //- No copy assignment + void operator=(const PatchFunction1&) = delete; + + public: typedef Field returnType; @@ -249,7 +242,7 @@ public: friend Ostream& operator<< ( Ostream& os, - const PatchFunction1& func + const PatchFunction1& rhs ); //- Write in dictionary format diff --git a/src/meshTools/PatchFunction1/PatchFunction1New.C b/src/meshTools/PatchFunction1/PatchFunction1New.C index d2c6fe29e8..f7821bbd1d 100644 --- a/src/meshTools/PatchFunction1/PatchFunction1New.C +++ b/src/meshTools/PatchFunction1/PatchFunction1New.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,10 +27,11 @@ License #include "ConstantField.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // template -Foam::autoPtr> Foam::PatchFunction1::New +Foam::autoPtr> +Foam::PatchFunction1::New ( const polyPatch& pp, const word& entryName, @@ -47,6 +48,7 @@ Foam::autoPtr> Foam::PatchFunction1::New << entryName << nl << nl << exit(FatalIOError); + // Failed return nullptr; } else if (eptr->isDict()) @@ -81,11 +83,6 @@ Foam::autoPtr> Foam::PatchFunction1::New is.putBack(firstToken); const Type uniformValue = pTraits(is); - const Field value - ( - (faceValues ? pp.size() : pp.nPoints()), - uniformValue - ); return autoPtr> ( @@ -93,9 +90,7 @@ Foam::autoPtr> Foam::PatchFunction1::New ( pp, entryName, - true, // uniform - uniformValue, // uniform value - value, // Supply value + uniformValue, dict, faceValues ) diff --git a/src/meshTools/PatchFunction1/UniformValueField/UniformValueField.H b/src/meshTools/PatchFunction1/UniformValueField/UniformValueField.H index 7d35b43ac6..d133dca4f9 100644 --- a/src/meshTools/PatchFunction1/UniformValueField/UniformValueField.H +++ b/src/meshTools/PatchFunction1/UniformValueField/UniformValueField.H @@ -62,9 +62,9 @@ class UniformValueField : public PatchFunction1 { - // Private data + // Private Data - //- Source of uniform values (in local co-ordinate system) + //- Source of uniform values (in local coordinate system) autoPtr> uniformValuePtr_; diff --git a/src/meshTools/PatchFunction1/UniformValueField/UniformValueFieldI.H b/src/meshTools/PatchFunction1/UniformValueField/UniformValueFieldI.H index 9a1e449acf..fa7e5a871b 100644 --- a/src/meshTools/PatchFunction1/UniformValueField/UniformValueFieldI.H +++ b/src/meshTools/PatchFunction1/UniformValueField/UniformValueFieldI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,21 +46,16 @@ Foam::PatchFunction1Types::UniformValueField::value const scalar x ) const { - label sz = - ( - this->faceValues_ - ? this->patch_.size() - : this->patch_.nPoints() - ); + const label len = + (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints()); - tmp> tfld - ( + auto tfld = tmp>::New ( - sz, + len, uniformValuePtr_->value(x) - ) - ); + ); + return this->transform(tfld); } @@ -73,21 +68,16 @@ Foam::PatchFunction1Types::UniformValueField::integrate const scalar x2 ) const { - label sz = - ( - this->faceValues_ - ? this->patch_.size() - : this->patch_.nPoints() - ); + const label len = + (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints()); - tmp> tfld - ( + auto tfld = tmp>::New ( - sz, + len, uniformValuePtr_->integrate(x1, x2) - ) - ); + ); + return this->transform(tfld); }