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

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2021 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
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>
Foam::refPtr<Foam::Function1<Type>>
Foam::Function1<Type>::New

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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