From b71a05a72f499bad78b6dc470ae22458ef459ea2 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 17 Feb 2023 17:19:14 +0100 Subject: [PATCH] ENH: add Function1::NewIfPresent without a redirect type - simplifies handling, consistent with PatchFunction1 STYLE: use Function1 NewIfPresent instead of separate found/New --- .../functions/Function1/Function1/Function1.H | 20 ++++++-- .../Function1/Function1/Function1New.C | 16 ++++++- .../flowRateInletVelocityFvPatchVectorField.C | 27 ++++++----- ...flowRateOutletVelocityFvPatchVectorField.C | 47 ++++++++++--------- ...flowRateOutletVelocityFvPatchVectorField.H | 10 ++-- .../fixedTemperatureConstraint.C | 2 +- .../ConeNozzleInjection/ConeNozzleInjection.C | 20 ++++---- .../cyclicACMIPolyPatch/cyclicACMIPolyPatch.C | 7 +-- .../PatchFunction1/PatchFunction1.H | 6 ++- 9 files changed, 94 insertions(+), 61 deletions(-) diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H index 33b10d3976..fa64f4f0cd 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.H @@ -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> NewIfPresent + ( + const word& entryName, + const dictionary& dict, + const word& redirectType, + const objectRegistry* obrPtr = nullptr + ); + + //- An optional selector, without fallback redirection static autoPtr> NewIfPresent ( const word& entryName, const dictionary& dict, - const word& redirectType = word::null, const objectRegistry* obrPtr = nullptr ); diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1New.C index b5be2d0a1e..0edde99337 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-2021 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -225,6 +225,20 @@ Foam::Function1::NewIfPresent } +template +Foam::autoPtr> +Foam::Function1::NewIfPresent +( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr +) +{ + // mandatory = false + return Function1::New(entryName, dict, word::null, obrPtr, false); +} + + template Foam::refPtr> Foam::Function1::New diff --git a/src/finiteVolume/fields/fvPatchFields/derived/flowRateInletVelocity/flowRateInletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/flowRateInletVelocity/flowRateInletVelocityFvPatchVectorField.C index 2489daf9c4..d2b3751b96 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/flowRateInletVelocity/flowRateInletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/flowRateInletVelocity/flowRateInletVelocityFvPatchVectorField.C @@ -42,9 +42,9 @@ flowRateInletVelocityFvPatchVectorField ) : fixedValueFvPatchField(p, iF), - flowRate_(), + flowRate_(nullptr), rhoName_("rho"), - rhoInlet_(0.0), + rhoInlet_(0), volumetric_(false), extrapolateProfile_(false) {} @@ -59,6 +59,7 @@ flowRateInletVelocityFvPatchVectorField ) : fixedValueFvPatchField(p, iF, dict, false), + flowRate_(nullptr), rhoName_("rho"), rhoInlet_(dict.getOrDefault("rhoInlet", -VGREAT)), volumetric_(false), @@ -67,23 +68,25 @@ flowRateInletVelocityFvPatchVectorField dict.getOrDefault("extrapolateProfile", false) ) { - if (dict.found("volumetricFlowRate")) + flowRate_ = + Function1::NewIfPresent("volumetricFlowRate", dict, &db()); + + if (flowRate_) { volumetric_ = true; - flowRate_ = - Function1::New("volumetricFlowRate", dict, &db()); - } - else if (dict.found("massFlowRate")) - { - volumetric_ = false; - flowRate_ = Function1::New("massFlowRate", dict, &db()); - rhoName_ = dict.getOrDefault("rho", "rho"); } else + { + dict.readIfPresent("rho", rhoName_); + flowRate_ = + Function1::NewIfPresent("massFlowRate", dict, &db()); + } + + if (!flowRate_) { FatalIOErrorInFunction(dict) << "Please supply either 'volumetricFlowRate' or" - << " 'massFlowRate' and 'rho'" << nl + << " 'massFlowRate' (optional: with 'rho')" << nl << exit(FatalIOError); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.C index eeb2d087d2..a1edea1a77 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.C @@ -41,10 +41,10 @@ flowRateOutletVelocityFvPatchVectorField ) : fixedValueFvPatchField(p, iF), - flowRate_(), - volumetric_(false), + flowRate_(nullptr), rhoName_("rho"), - rhoOutlet_(0.0) + rhoOutlet_(0), + volumetric_(false) {} @@ -57,26 +57,31 @@ flowRateOutletVelocityFvPatchVectorField ) : fixedValueFvPatchField(p, iF, dict, false), - rhoOutlet_(dict.getOrDefault("rhoOutlet", -VGREAT)) + flowRate_(nullptr), + rhoName_("rho"), + rhoOutlet_(dict.getOrDefault("rhoOutlet", -VGREAT)), + volumetric_(false) { - if (dict.found("volumetricFlowRate")) + flowRate_ = + Function1::NewIfPresent("volumetricFlowRate", dict, &db()); + + if (flowRate_) { volumetric_ = true; - flowRate_ = - Function1::New("volumetricFlowRate", dict, &db()); - rhoName_ = "rho"; - } - else if (dict.found("massFlowRate")) - { - volumetric_ = false; - flowRate_ = Function1::New("massFlowRate", dict, &db()); - rhoName_ = dict.getOrDefault("rho", "rho"); } else + { + dict.readIfPresent("rho", rhoName_); + flowRate_ = + Function1::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(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(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(ptf, iF), flowRate_(ptf.flowRate_.clone()), - volumetric_(ptf.volumetric_), rhoName_(ptf.rhoName_), - rhoOutlet_(ptf.rhoOutlet_) + rhoOutlet_(ptf.rhoOutlet_), + volumetric_(ptf.volumetric_) {} diff --git a/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.H b/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.H index 43cfa9ff65..1e88968065 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.H +++ b/src/finiteVolume/fields/fvPatchFields/derived/flowRateOutletVelocity/flowRateOutletVelocityFvPatchVectorField.H @@ -111,22 +111,22 @@ class flowRateOutletVelocityFvPatchVectorField : public fixedValueFvPatchVectorField { - // Private data + // Private Data //- Outlet integral flow rate autoPtr> 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 diff --git a/src/fvOptions/constraints/derived/fixedTemperatureConstraint/fixedTemperatureConstraint.C b/src/fvOptions/constraints/derived/fixedTemperatureConstraint/fixedTemperatureConstraint.C index 93c192c1f0..08f995d3ff 100644 --- a/src/fvOptions/constraints/derived/fixedTemperatureConstraint/fixedTemperatureConstraint.C +++ b/src/fvOptions/constraints/derived/fixedTemperatureConstraint/fixedTemperatureConstraint.C @@ -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 ( diff --git a/src/lagrangian/intermediate/submodels/Kinematic/InjectionModel/ConeNozzleInjection/ConeNozzleInjection.C b/src/lagrangian/intermediate/submodels/Kinematic/InjectionModel/ConeNozzleInjection/ConeNozzleInjection.C index 951bb484ed..527b32851d 100644 --- a/src/lagrangian/intermediate/submodels/Kinematic/InjectionModel/ConeNozzleInjection/ConeNozzleInjection.C +++ b/src/lagrangian/intermediate/submodels/Kinematic/InjectionModel/ConeNozzleInjection/ConeNozzleInjection.C @@ -186,7 +186,15 @@ Foam::ConeNozzleInjection::ConeNozzleInjection tetPti_(-1), directionVsTime_(nullptr), direction_(Zero), - omegaPtr_(nullptr), + omegaPtr_ + ( + Function1::NewIfPresent + ( + "omega", + this->coeffDict(), + &owner.mesh() + ) + ), parcelsPerSecond_(this->coeffDict().getScalar("parcelsPerSecond")), flowRateProfile_ ( @@ -247,16 +255,8 @@ Foam::ConeNozzleInjection::ConeNozzleInjection thetaInner_->userTimeToTime(time); thetaOuter_->userTimeToTime(time); - if (this->coeffDict().found("omega")) + if (omegaPtr_) { - omegaPtr_ = - Function1::New - ( - "omega", - this->coeffDict(), - &owner.mesh() - ); - omegaPtr_->userTimeToTime(time); } diff --git a/src/meshTools/AMIInterpolation/patches/cyclicACMI/cyclicACMIPolyPatch/cyclicACMIPolyPatch.C b/src/meshTools/AMIInterpolation/patches/cyclicACMI/cyclicACMIPolyPatch/cyclicACMIPolyPatch.C index 520919a041..2bc115e61c 100644 --- a/src/meshTools/AMIInterpolation/patches/cyclicACMI/cyclicACMIPolyPatch/cyclicACMIPolyPatch.C +++ b/src/meshTools/AMIInterpolation/patches/cyclicACMI/cyclicACMIPolyPatch/cyclicACMIPolyPatch.C @@ -603,12 +603,7 @@ Foam::cyclicACMIPolyPatch::cyclicACMIPolyPatch nonOverlapPatchID_(-1), srcMask_(), tgtMask_(), - srcScalePtr_ - ( - dict.found("scale") - ? PatchFunction1::New(*this, "scale", dict) - : nullptr - ), + srcScalePtr_(PatchFunction1::NewIfPresent(*this, "scale", dict)), AMITime_ ( IOobject diff --git a/src/meshTools/PatchFunction1/PatchFunction1/PatchFunction1.H b/src/meshTools/PatchFunction1/PatchFunction1/PatchFunction1.H index 2e3f6a6e98..b8e075e5f5 100644 --- a/src/meshTools/PatchFunction1/PatchFunction1/PatchFunction1.H +++ b/src/meshTools/PatchFunction1/PatchFunction1/PatchFunction1.H @@ -102,7 +102,11 @@ protected: public: - typedef Field returnType; + // Data Types + + //- The return type is a field of values + typedef Field returnType; + //- Runtime type information TypeName("PatchFunction1")