ENH: add Function1::NewIfPresent without a redirect type

- simplifies handling, consistent with PatchFunction1

STYLE: use Function1 NewIfPresent instead of separate found/New
This commit is contained in:
Mark Olesen
2023-02-17 17:19:14 +01:00
parent aef990f8df
commit b71a05a72f
9 changed files with 94 additions and 61 deletions

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-2022 OpenCFD Ltd. Copyright (C) 2018-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -119,7 +119,11 @@ protected:
public: public:
typedef Type returnType; // Data Types
//- The return type
typedef Type returnType;
//- Runtime type information //- Runtime type information
TypeName("Function1") TypeName("Function1")
@ -196,12 +200,20 @@ public:
const bool mandatory = true const bool mandatory = true
); );
//- An optional selector //- An optional selector, with fallback redirection
static autoPtr<Function1<Type>> NewIfPresent
(
const word& entryName,
const dictionary& dict,
const word& redirectType,
const objectRegistry* obrPtr = nullptr
);
//- An optional selector, without fallback redirection
static autoPtr<Function1<Type>> NewIfPresent static autoPtr<Function1<Type>> NewIfPresent
( (
const word& entryName, const word& entryName,
const dictionary& dict, const dictionary& dict,
const word& redirectType = word::null,
const objectRegistry* obrPtr = nullptr const objectRegistry* obrPtr = nullptr
); );

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-2021 OpenCFD Ltd. Copyright (C) 2018-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -225,6 +225,20 @@ Foam::Function1<Type>::NewIfPresent
} }
template<class Type>
Foam::autoPtr<Foam::Function1<Type>>
Foam::Function1<Type>::NewIfPresent
(
const word& entryName,
const dictionary& dict,
const objectRegistry* obrPtr
)
{
// mandatory = false
return Function1<Type>::New(entryName, dict, word::null, obrPtr, false);
}
template<class Type> template<class Type>
Foam::refPtr<Foam::Function1<Type>> Foam::refPtr<Foam::Function1<Type>>
Foam::Function1<Type>::New Foam::Function1<Type>::New

View File

@ -42,9 +42,9 @@ flowRateInletVelocityFvPatchVectorField
) )
: :
fixedValueFvPatchField<vector>(p, iF), fixedValueFvPatchField<vector>(p, iF),
flowRate_(), flowRate_(nullptr),
rhoName_("rho"), rhoName_("rho"),
rhoInlet_(0.0), rhoInlet_(0),
volumetric_(false), volumetric_(false),
extrapolateProfile_(false) extrapolateProfile_(false)
{} {}
@ -59,6 +59,7 @@ flowRateInletVelocityFvPatchVectorField
) )
: :
fixedValueFvPatchField<vector>(p, iF, dict, false), fixedValueFvPatchField<vector>(p, iF, dict, false),
flowRate_(nullptr),
rhoName_("rho"), rhoName_("rho"),
rhoInlet_(dict.getOrDefault<scalar>("rhoInlet", -VGREAT)), rhoInlet_(dict.getOrDefault<scalar>("rhoInlet", -VGREAT)),
volumetric_(false), volumetric_(false),
@ -67,23 +68,25 @@ flowRateInletVelocityFvPatchVectorField
dict.getOrDefault<Switch>("extrapolateProfile", false) dict.getOrDefault<Switch>("extrapolateProfile", false)
) )
{ {
if (dict.found("volumetricFlowRate")) flowRate_ =
Function1<scalar>::NewIfPresent("volumetricFlowRate", dict, &db());
if (flowRate_)
{ {
volumetric_ = true; volumetric_ = true;
flowRate_ =
Function1<scalar>::New("volumetricFlowRate", dict, &db());
}
else if (dict.found("massFlowRate"))
{
volumetric_ = false;
flowRate_ = Function1<scalar>::New("massFlowRate", dict, &db());
rhoName_ = dict.getOrDefault<word>("rho", "rho");
} }
else else
{
dict.readIfPresent("rho", rhoName_);
flowRate_ =
Function1<scalar>::NewIfPresent("massFlowRate", dict, &db());
}
if (!flowRate_)
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Please supply either 'volumetricFlowRate' or" << "Please supply either 'volumetricFlowRate' or"
<< " 'massFlowRate' and 'rho'" << nl << " 'massFlowRate' (optional: with 'rho')" << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }

View File

@ -41,10 +41,10 @@ flowRateOutletVelocityFvPatchVectorField
) )
: :
fixedValueFvPatchField<vector>(p, iF), fixedValueFvPatchField<vector>(p, iF),
flowRate_(), flowRate_(nullptr),
volumetric_(false),
rhoName_("rho"), rhoName_("rho"),
rhoOutlet_(0.0) rhoOutlet_(0),
volumetric_(false)
{} {}
@ -57,26 +57,31 @@ flowRateOutletVelocityFvPatchVectorField
) )
: :
fixedValueFvPatchField<vector>(p, iF, dict, false), fixedValueFvPatchField<vector>(p, iF, dict, false),
rhoOutlet_(dict.getOrDefault<scalar>("rhoOutlet", -VGREAT)) flowRate_(nullptr),
rhoName_("rho"),
rhoOutlet_(dict.getOrDefault<scalar>("rhoOutlet", -VGREAT)),
volumetric_(false)
{ {
if (dict.found("volumetricFlowRate")) flowRate_ =
Function1<scalar>::NewIfPresent("volumetricFlowRate", dict, &db());
if (flowRate_)
{ {
volumetric_ = true; volumetric_ = true;
flowRate_ =
Function1<scalar>::New("volumetricFlowRate", dict, &db());
rhoName_ = "rho";
}
else if (dict.found("massFlowRate"))
{
volumetric_ = false;
flowRate_ = Function1<scalar>::New("massFlowRate", dict, &db());
rhoName_ = dict.getOrDefault<word>("rho", "rho");
} }
else else
{
dict.readIfPresent("rho", rhoName_);
flowRate_ =
Function1<scalar>::NewIfPresent("massFlowRate", dict, &db());
}
if (!flowRate_)
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Please supply either 'volumetricFlowRate' or" << "Please supply either 'volumetricFlowRate' or"
<< " 'massFlowRate' and 'rho'" << exit(FatalIOError); << " 'massFlowRate' (optional: with 'rho')" << nl
<< exit(FatalIOError);
} }
// Value field require if mass based // Value field require if mass based
@ -105,9 +110,9 @@ flowRateOutletVelocityFvPatchVectorField
: :
fixedValueFvPatchField<vector>(ptf, p, iF, mapper), fixedValueFvPatchField<vector>(ptf, p, iF, mapper),
flowRate_(ptf.flowRate_.clone()), flowRate_(ptf.flowRate_.clone()),
volumetric_(ptf.volumetric_),
rhoName_(ptf.rhoName_), rhoName_(ptf.rhoName_),
rhoOutlet_(ptf.rhoOutlet_) rhoOutlet_(ptf.rhoOutlet_),
volumetric_(ptf.volumetric_)
{} {}
@ -119,9 +124,9 @@ flowRateOutletVelocityFvPatchVectorField
: :
fixedValueFvPatchField<vector>(ptf), fixedValueFvPatchField<vector>(ptf),
flowRate_(ptf.flowRate_.clone()), flowRate_(ptf.flowRate_.clone()),
volumetric_(ptf.volumetric_),
rhoName_(ptf.rhoName_), rhoName_(ptf.rhoName_),
rhoOutlet_(ptf.rhoOutlet_) rhoOutlet_(ptf.rhoOutlet_),
volumetric_(ptf.volumetric_)
{} {}
@ -134,9 +139,9 @@ flowRateOutletVelocityFvPatchVectorField
: :
fixedValueFvPatchField<vector>(ptf, iF), fixedValueFvPatchField<vector>(ptf, iF),
flowRate_(ptf.flowRate_.clone()), flowRate_(ptf.flowRate_.clone()),
volumetric_(ptf.volumetric_),
rhoName_(ptf.rhoName_), rhoName_(ptf.rhoName_),
rhoOutlet_(ptf.rhoOutlet_) rhoOutlet_(ptf.rhoOutlet_),
volumetric_(ptf.volumetric_)
{} {}

View File

@ -111,22 +111,22 @@ class flowRateOutletVelocityFvPatchVectorField
: :
public fixedValueFvPatchVectorField public fixedValueFvPatchVectorField
{ {
// Private data // Private Data
//- Outlet integral flow rate //- Outlet integral flow rate
autoPtr<Function1<scalar>> flowRate_; autoPtr<Function1<scalar>> flowRate_;
//- Is volumetric?
bool volumetric_;
//- Name of the density field used to normalize the mass flux //- Name of the density field used to normalize the mass flux
word rhoName_; word rhoName_;
//- Rho initialisation value (for start; if value not supplied) //- Rho initialisation value (for start; if value not supplied)
scalar rhoOutlet_; scalar rhoOutlet_;
//- Is volumetric?
bool volumetric_;
// Private member functions
// Private Member Functions
//- Update the patch values given the appropriate density type and value //- Update the patch values given the appropriate density type and value
template<class RhoType> template<class RhoType>

View File

@ -148,7 +148,7 @@ bool Foam::fv::fixedTemperatureConstraint::read(const dictionary& dict)
{ {
if (fv::cellSetOption::read(dict)) if (fv::cellSetOption::read(dict))
{ {
if (coeffs_.found(Tuniform_->name())) if (Tuniform_ && coeffs_.found(Tuniform_->name(), keyType::LITERAL))
{ {
Tuniform_.reset Tuniform_.reset
( (

View File

@ -186,7 +186,15 @@ Foam::ConeNozzleInjection<CloudType>::ConeNozzleInjection
tetPti_(-1), tetPti_(-1),
directionVsTime_(nullptr), directionVsTime_(nullptr),
direction_(Zero), direction_(Zero),
omegaPtr_(nullptr), omegaPtr_
(
Function1<scalar>::NewIfPresent
(
"omega",
this->coeffDict(),
&owner.mesh()
)
),
parcelsPerSecond_(this->coeffDict().getScalar("parcelsPerSecond")), parcelsPerSecond_(this->coeffDict().getScalar("parcelsPerSecond")),
flowRateProfile_ flowRateProfile_
( (
@ -247,16 +255,8 @@ Foam::ConeNozzleInjection<CloudType>::ConeNozzleInjection
thetaInner_->userTimeToTime(time); thetaInner_->userTimeToTime(time);
thetaOuter_->userTimeToTime(time); thetaOuter_->userTimeToTime(time);
if (this->coeffDict().found("omega")) if (omegaPtr_)
{ {
omegaPtr_ =
Function1<scalar>::New
(
"omega",
this->coeffDict(),
&owner.mesh()
);
omegaPtr_->userTimeToTime(time); omegaPtr_->userTimeToTime(time);
} }

View File

@ -603,12 +603,7 @@ Foam::cyclicACMIPolyPatch::cyclicACMIPolyPatch
nonOverlapPatchID_(-1), nonOverlapPatchID_(-1),
srcMask_(), srcMask_(),
tgtMask_(), tgtMask_(),
srcScalePtr_ srcScalePtr_(PatchFunction1<scalar>::NewIfPresent(*this, "scale", dict)),
(
dict.found("scale")
? PatchFunction1<scalar>::New(*this, "scale", dict)
: nullptr
),
AMITime_ AMITime_
( (
IOobject IOobject

View File

@ -102,7 +102,11 @@ protected:
public: public:
typedef Field<Type> returnType; // Data Types
//- The return type is a field of values
typedef Field<Type> returnType;
//- Runtime type information //- Runtime type information
TypeName("PatchFunction1") TypeName("PatchFunction1")