ENH: add construct ConstantField with uniform value

This commit is contained in:
Mark Olesen 2020-09-25 13:41:20 +02:00
parent 97be8fc767
commit bb3660b9a5
12 changed files with 96 additions and 96 deletions

View File

@ -52,7 +52,7 @@ Foam::Function1Types::Constant<Type>::Constant
Function1<Type>(entryName), Function1<Type>(entryName),
value_(Zero) value_(Zero)
{ {
Istream& is(dict.lookup(entryName)); Istream& is = dict.lookup(entryName);
word entryType(is); word entryType(is);
is >> value_; is >> value_;
} }
@ -78,13 +78,6 @@ Foam::Function1Types::Constant<Type>::Constant(const Constant<Type>& cnst)
{} {}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Constant<Type>::~Constant()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type> template<class Type>

View File

@ -103,7 +103,7 @@ public:
//- Destructor //- Destructor
virtual ~Constant(); virtual ~Constant() = default;
// Member Functions // Member Functions

View File

@ -103,6 +103,7 @@ protected:
//- Name of entry //- Name of entry
const word name_; const word name_;
// Protected Member Functions // Protected Member Functions
//- No copy assignment //- No copy assignment

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,10 +28,11 @@ License
#include "Constant.H" #include "Constant.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::autoPtr<Foam::Function1<Type>> Foam::Function1<Type>::New Foam::autoPtr<Foam::Function1<Type>>
Foam::Function1<Type>::New
( (
const word& entryName, const word& entryName,
const dictionary& dict, const dictionary& dict,
@ -86,10 +87,14 @@ Foam::autoPtr<Foam::Function1<Type>> Foam::Function1<Type>::New
if (!firstToken.isWord()) if (!firstToken.isWord())
{ {
// Backwards-compatibility for reading straight fields
is.putBack(firstToken); is.putBack(firstToken);
const Type constValue = pTraits<Type>(is);
return autoPtr<Function1<Type>> return autoPtr<Function1<Type>>
( (
new Function1Types::Constant<Type>(entryName, is) new Function1Types::Constant<Type>(entryName, constValue)
); );
} }

View File

@ -29,6 +29,23 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::PatchFunction1Types::ConstantField<Type>::ConstantField
(
const polyPatch& pp,
const word& entryName,
const Type& uniformValue,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1<Type>(pp, entryName, dict, faceValues),
isUniform_(true),
uniformValue_(uniformValue),
value_((faceValues ? pp.size() : pp.nPoints()), uniformValue_)
{}
template<class Type> template<class Type>
Foam::PatchFunction1Types::ConstantField<Type>::ConstantField Foam::PatchFunction1Types::ConstantField<Type>::ConstantField
( (
@ -36,7 +53,7 @@ Foam::PatchFunction1Types::ConstantField<Type>::ConstantField
const word& entryName, const word& entryName,
const bool isUniform, const bool isUniform,
const Type& uniformValue, const Type& uniformValue,
const Field<Type>& nonUniformValue, const Field<Type>& fieldValues,
const dictionary& dict, const dictionary& dict,
const bool faceValues const bool faceValues
) )
@ -44,21 +61,18 @@ Foam::PatchFunction1Types::ConstantField<Type>::ConstantField
PatchFunction1<Type>(pp, entryName, dict, faceValues), PatchFunction1<Type>(pp, entryName, dict, faceValues),
isUniform_(isUniform), isUniform_(isUniform),
uniformValue_(uniformValue), 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) FatalIOErrorInFunction(dict)
<< "Supplied field size " << nonUniformValue.size() << "Supplied field size " << fieldValues.size()
<< " is not equal to the number of faces " << pp.size() << " is not equal to the number of "
<< " of patch " << pp.name() << exit(FatalIOError); << (faceValues ? "faces" : "points") << ' '
} << len << " of patch " << pp.name() << nl
else if (!faceValues && nonUniformValue.size() != pp.nPoints()) << exit(FatalIOError);
{
FatalIOErrorInFunction(dict)
<< "Supplied field size " << nonUniformValue.size()
<< " is not equal to the number of points " << pp.nPoints()
<< " of patch " << pp.name() << exit(FatalIOError);
} }
} }
@ -103,7 +117,7 @@ Foam::Field<Type> Foam::PatchFunction1Types::ConstantField<Type>::getValue
is >> list; is >> list;
isUniform = false; isUniform = false;
label currentSize = fld.size(); const label currentSize = fld.size();
if (currentSize != len) if (currentSize != len)
{ {
if if
@ -120,7 +134,7 @@ Foam::Field<Type> Foam::PatchFunction1Types::ConstantField<Type>::getValue
<< endl; << endl;
#endif #endif
// Resize the data // Resize (shrink) the data
fld.setSize(len); fld.setSize(len);
} }
else else
@ -204,13 +218,13 @@ Foam::PatchFunction1Types::ConstantField<Type>::ConstantField
uniformValue_(cnst.uniformValue_), uniformValue_(cnst.uniformValue_),
value_(cnst.value_) value_(cnst.value_)
{ {
// If different sizes do what? // If sizes are different...
value_.setSize value_.resize
( (
this->faceValues_ (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints()),
? this->patch_.size() Zero
: this->patch_.nPoints()
); );
if (isUniform_) if (isUniform_)
{ {
value_ = uniformValue_; value_ = uniformValue_;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -96,6 +96,16 @@ public:
// Constructors // 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 //- Construct from components
ConstantField ConstantField
( (
@ -103,7 +113,7 @@ public:
const word& entryName, const word& entryName,
const bool isUniform, const bool isUniform,
const Type& uniformValue, const Type& uniformValue,
const Field<Type>& nonUniformValue, const Field<Type>& fieldValues,
const dictionary& dict = dictionary::null, const dictionary& dict = dictionary::null,
const bool faceValues = true const bool faceValues = true
); );

View File

@ -5,8 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.

View File

@ -232,19 +232,19 @@ void Foam::PatchFunction1<Type>::writeData(Ostream& os) const
} }
// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * IOStream Operators * * * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::Ostream& Foam::operator<< Foam::Ostream& Foam::operator<<
( (
Ostream& os, Ostream& os,
const PatchFunction1<Type>& pf1 const PatchFunction1<Type>& rhs
) )
{ {
os.check(FUNCTION_NAME); os.check(FUNCTION_NAME);
os << pf1.name_; os << rhs.name_;
pf1.writeData(os); rhs.writeData(os);
return os; return os;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -56,19 +56,12 @@ SeeAlso
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class Time; class Time;
template<class Type> class PatchFunction1;
// Forward declaration of friend functions and operators
template<class Type>
class PatchFunction1;
template<class Type> template<class Type>
Ostream& operator<< Ostream& operator<<(Ostream&, const PatchFunction1<Type>&);
(
Ostream&,
const PatchFunction1<Type>&
);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class PatchFunction1 Declaration Class PatchFunction1 Declaration
@ -79,15 +72,9 @@ class PatchFunction1
: :
public refCount public refCount
{ {
// Private Member Functions
//- No copy assignment
void operator=(const PatchFunction1<Type>&) = delete;
protected: protected:
// Protected data // Protected Data
//- Name of entry //- Name of entry
const word name_; const word name_;
@ -102,6 +89,12 @@ protected:
coordinateScaling<Type> coordSys_; coordinateScaling<Type> coordSys_;
// Protected Member Functions
//- No copy assignment
void operator=(const PatchFunction1<Type>&) = delete;
public: public:
typedef Field<Type> returnType; typedef Field<Type> returnType;
@ -249,7 +242,7 @@ public:
friend Ostream& operator<< <Type> friend Ostream& operator<< <Type>
( (
Ostream& os, Ostream& os,
const PatchFunction1<Type>& func const PatchFunction1<Type>& rhs
); );
//- Write in dictionary format //- Write in dictionary format

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,10 +27,11 @@ License
#include "ConstantField.H" #include "ConstantField.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::autoPtr<Foam::PatchFunction1<Type>> Foam::PatchFunction1<Type>::New Foam::autoPtr<Foam::PatchFunction1<Type>>
Foam::PatchFunction1<Type>::New
( (
const polyPatch& pp, const polyPatch& pp,
const word& entryName, const word& entryName,
@ -47,6 +48,7 @@ Foam::autoPtr<Foam::PatchFunction1<Type>> Foam::PatchFunction1<Type>::New
<< entryName << nl << nl << entryName << nl << nl
<< exit(FatalIOError); << exit(FatalIOError);
// Failed
return nullptr; return nullptr;
} }
else if (eptr->isDict()) else if (eptr->isDict())
@ -81,11 +83,6 @@ Foam::autoPtr<Foam::PatchFunction1<Type>> Foam::PatchFunction1<Type>::New
is.putBack(firstToken); is.putBack(firstToken);
const Type uniformValue = pTraits<Type>(is); const Type uniformValue = pTraits<Type>(is);
const Field<Type> value
(
(faceValues ? pp.size() : pp.nPoints()),
uniformValue
);
return autoPtr<PatchFunction1<Type>> return autoPtr<PatchFunction1<Type>>
( (
@ -93,9 +90,7 @@ Foam::autoPtr<Foam::PatchFunction1<Type>> Foam::PatchFunction1<Type>::New
( (
pp, pp,
entryName, entryName,
true, // uniform uniformValue,
uniformValue, // uniform value
value, // Supply value
dict, dict,
faceValues faceValues
) )

View File

@ -62,9 +62,9 @@ class UniformValueField
: :
public PatchFunction1<Type> public PatchFunction1<Type>
{ {
// Private data // Private Data
//- Source of uniform values (in local co-ordinate system) //- Source of uniform values (in local coordinate system)
autoPtr<Foam::Function1<Type>> uniformValuePtr_; autoPtr<Foam::Function1<Type>> uniformValuePtr_;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -46,21 +46,16 @@ Foam::PatchFunction1Types::UniformValueField<Type>::value
const scalar x const scalar x
) const ) const
{ {
label sz = const label len =
( (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints());
this->faceValues_
? this->patch_.size()
: this->patch_.nPoints()
);
tmp<Field<Type>> tfld auto tfld =
(
tmp<Field<Type>>::New tmp<Field<Type>>::New
( (
sz, len,
uniformValuePtr_->value(x) uniformValuePtr_->value(x)
) );
);
return this->transform(tfld); return this->transform(tfld);
} }
@ -73,21 +68,16 @@ Foam::PatchFunction1Types::UniformValueField<Type>::integrate
const scalar x2 const scalar x2
) const ) const
{ {
label sz = const label len =
( (this->faceValues_ ? this->patch_.size() : this->patch_.nPoints());
this->faceValues_
? this->patch_.size()
: this->patch_.nPoints()
);
tmp<Field<Type>> tfld auto tfld =
(
tmp<Field<Type>>::New tmp<Field<Type>>::New
( (
sz, len,
uniformValuePtr_->integrate(x1, x2) uniformValuePtr_->integrate(x1, x2)
) );
);
return this->transform(tfld); return this->transform(tfld);
} }