ENH: avoid undefined method in temperatureCoupledBase

- old constructor interface allowed arbitrary strings to specify the
  method enumeration. If actually used at runtime, they could/would
  raise a FatalError (unknown enumeration).
  Define a simpler default constructor instead.
This commit is contained in:
Mark Olesen
2022-09-14 10:36:01 +02:00
parent 1695f2f5b9
commit a8057c4bc6
9 changed files with 168 additions and 152 deletions

View File

@ -108,14 +108,7 @@ filmPyrolysisRadiativeCoupledMixedFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
filmRegionName_("surfaceFilmProperties"), filmRegionName_("surfaceFilmProperties"),
pyrolysisRegionName_("pyrolysisProperties"), pyrolysisRegionName_("pyrolysisProperties"),
TnbrName_("undefined-Tnbr"), TnbrName_("undefined-Tnbr"),

View File

@ -58,14 +58,7 @@ externalWallHeatFluxTemperatureFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
mode_(fixedHeatFlux), mode_(fixedHeatFlux),
Q_(nullptr), Q_(nullptr),
q_(nullptr), q_(nullptr),

View File

@ -45,14 +45,7 @@ fixedIncidentRadiationFvPatchScalarField
) )
: :
fixedGradientFvPatchScalarField(p, iF), fixedGradientFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
qrIncident_(p.size(), Zero) qrIncident_(p.size(), Zero)
{} {}

View File

@ -132,14 +132,7 @@ humidityTemperatureCoupledMixedFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch(), temperatureCoupledBase::mtFluidThermo),
(
patch(),
"fluidThermo",
"undefined",
"undefined-K",
"undefined-alpha"
),
mode_(mtConstantMass), mode_(mtConstantMass),
pName_("p"), pName_("p"),
UName_("U"), UName_("U"),

View File

@ -41,14 +41,7 @@ lumpedMassWallTemperatureFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
Cp_(0.0), Cp_(0.0),
mass_(0.0), mass_(0.0),
curTimeIndex_(-1) curTimeIndex_(-1)

View File

@ -54,18 +54,70 @@ Foam::temperatureCoupledBase::KMethodTypeNames_
Foam::temperatureCoupledBase::temperatureCoupledBase Foam::temperatureCoupledBase::temperatureCoupledBase
( (
const fvPatch& patch, const fvPatch& patch,
const word& calculationType, const KMethodType method
const word& kappaName,
const word& alphaAniName,
const word& alphaName
) )
: :
patch_(patch), patch_(patch),
method_(KMethodTypeNames_[calculationType]), method_(method),
kappaName_(),
alphaName_(),
alphaAniName_(),
kappaFunction1_(nullptr),
alphaFunction1_(nullptr)
{
switch (method_)
{
case mtDirectionalSolidThermo:
case mtLookup:
case mtFunction:
{
FatalErrorInFunction
<< "Cannot construct kappaMethod: "
<< KMethodTypeNames_[method_] << " without a dictionary"
<< abort(FatalError);
break;
}
default:
{
break;
}
}
}
Foam::temperatureCoupledBase::temperatureCoupledBase
(
const fvPatch& patch,
const KMethodType method,
const word& kappaName,
const word& alphaName,
const word& alphaAniName
)
:
patch_(patch),
method_(method),
kappaName_(kappaName), kappaName_(kappaName),
alphaName_(alphaName),
alphaAniName_(alphaAniName), alphaAniName_(alphaAniName),
alphaName_(alphaName) kappaFunction1_(nullptr),
{} alphaFunction1_(nullptr)
{
switch (method_)
{
case mtFunction:
{
FatalErrorInFunction
<< "Cannot construct kappaMethod: "
<< KMethodTypeNames_[method_] << " without a dictionary"
<< abort(FatalError);
break;
}
default:
{
break;
}
}
}
Foam::temperatureCoupledBase::temperatureCoupledBase Foam::temperatureCoupledBase::temperatureCoupledBase
@ -77,8 +129,10 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
patch_(patch), patch_(patch),
method_(KMethodTypeNames_.get("kappaMethod", dict)), method_(KMethodTypeNames_.get("kappaMethod", dict)),
kappaName_(dict.getOrDefault<word>("kappa", word::null)), kappaName_(dict.getOrDefault<word>("kappa", word::null)),
alphaName_(dict.getOrDefault<word>("alpha", word::null)),
alphaAniName_(dict.getOrDefault<word>("alphaAni", word::null)), alphaAniName_(dict.getOrDefault<word>("alphaAni", word::null)),
alphaName_(dict.getOrDefault<word>("alpha", word::null)) kappaFunction1_(nullptr),
alphaFunction1_(nullptr)
{ {
switch (method_) switch (method_)
{ {
@ -89,7 +143,7 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Did not find entry 'alphaAni'" << "Did not find entry 'alphaAni'"
" required for 'kappaMethod' " " required for 'kappaMethod' "
<< KMethodTypeNames_[method_] << KMethodTypeNames_[method_] << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -104,8 +158,8 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
<< "Did not find entry 'kappa'" << "Did not find entry 'kappa'"
" required for 'kappaMethod' " " required for 'kappaMethod' "
<< KMethodTypeNames_[method_] << nl << KMethodTypeNames_[method_] << nl
<< " Please set 'kappa' to the name of a volScalarField" << "Please set 'kappa' to the name of"
" or volSymmTensorField" " a volScalar or volSymmTensor field" << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -116,16 +170,18 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
{ {
kappaFunction1_ = PatchFunction1<scalar>::New kappaFunction1_ = PatchFunction1<scalar>::New
( (
patch.patch(), patch_.patch(),
"kappaValue", "kappaValue",
dict dict
); );
alphaFunction1_ = PatchFunction1<scalar>::New alphaFunction1_ = PatchFunction1<scalar>::New
( (
patch.patch(), patch_.patch(),
"alphaValue", "alphaValue",
dict dict
); );
break;
} }
default: default:
@ -141,13 +197,7 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
const temperatureCoupledBase& base const temperatureCoupledBase& base
) )
: :
patch_(base.patch_), temperatureCoupledBase(base.patch_, base)
method_(base.method_),
kappaName_(base.kappaName_),
alphaAniName_(base.alphaAniName_),
alphaName_(base.alphaName_),
kappaFunction1_(base.kappaFunction1_.clone(patch_.patch())),
alphaFunction1_(base.alphaFunction1_.clone(patch_.patch()))
{} {}
@ -160,8 +210,8 @@ Foam::temperatureCoupledBase::temperatureCoupledBase
patch_(patch), patch_(patch),
method_(base.method_), method_(base.method_),
kappaName_(base.kappaName_), kappaName_(base.kappaName_),
alphaAniName_(base.alphaAniName_),
alphaName_(base.alphaName_), alphaName_(base.alphaName_),
alphaAniName_(base.alphaAniName_),
kappaFunction1_(base.kappaFunction1_.clone(patch_.patch())), kappaFunction1_(base.kappaFunction1_.clone(patch_.patch())),
alphaFunction1_(base.alphaFunction1_.clone(patch_.patch())) alphaFunction1_(base.alphaFunction1_.clone(patch_.patch()))
{} {}
@ -281,6 +331,7 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
mesh.lookupObject<solidThermo>(basicThermo::dictName); mesh.lookupObject<solidThermo>(basicThermo::dictName);
return thermo.kappa(patchi); return thermo.kappa(patchi);
break; break;
} }
@ -306,35 +357,42 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
case mtLookup: case mtLookup:
{ {
if (mesh.foundObject<volScalarField>(kappaName_))
{ {
return patch_.lookupPatchField<volScalarField, scalar> const auto* ptr =
( mesh.cfindObject<volScalarField>(kappaName_);
kappaName_
);
}
else if (mesh.foundObject<volSymmTensorField>(kappaName_))
{
const symmTensorField& KWall =
patch_.lookupPatchField<volSymmTensorField, scalar>
(
kappaName_
);
const vectorField n(patch_.nf()); if (ptr)
{
return patch_.patchField<volScalarField>(*ptr);
}
}
return n & KWall & n;
}
else
{ {
FatalErrorInFunction const auto* ptr =
<< "Did not find field " << kappaName_ mesh.cfindObject<volSymmTensorField>(kappaName_);
<< " on mesh " << mesh.name() << " patch " << patch_.name()
<< nl if (ptr)
<< " Please set 'kappa' to the name of a volScalarField" {
<< " or volSymmTensorField." const symmTensorField& wallValues =
<< exit(FatalError); patch_.patchField<volSymmTensorField>(*ptr);
const vectorField n(patch_.nf());
return n & wallValues & n;
}
} }
FatalErrorInFunction
<< "Did not find field '" << kappaName_
<< "' on mesh " << mesh.name()
<< " patch " << patch_.name() << nl
<< "Please set 'kappa' to the name of"
" a volScalar or volSymmTensor field"
", or use another method" << nl
<< " " << flatOutput(KMethodTypeNames_.sortedToc()) << nl
<< exit(FatalError);
break; break;
} }
@ -351,8 +409,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
<< "Unimplemented method " << KMethodTypeNames_[method_] << nl << "Unimplemented method " << KMethodTypeNames_[method_] << nl
<< "Please set 'kappaMethod' to one of " << "Please set 'kappaMethod' to one of "
<< flatOutput(KMethodTypeNames_.sortedToc()) << nl << flatOutput(KMethodTypeNames_.sortedToc()) << nl
<< "and 'kappa' to the name of the volScalar" << "If kappaMethod=lookup, also set 'kappa' to the name of"
<< " or volSymmTensor field (if kappaMethod=lookup)" " a volScalar or volSymmTensor field" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
@ -452,39 +510,41 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::alpha
case mtLookup: case mtLookup:
{ {
if (mesh.foundObject<volScalarField>(alphaName_))
{ {
return const auto* ptr =
patch_.lookupPatchField<volScalarField, scalar> mesh.cfindObject<volScalarField>(alphaName_);
(
alphaName_
);
}
else if (mesh.foundObject<volSymmTensorField>(alphaName_))
{
const symmTensorField& alphaWall =
patch_.lookupPatchField<volSymmTensorField, scalar>
(
alphaName_
);
const vectorField n(patch_.nf()); if (ptr)
{
return patch_.patchField<volScalarField>(*ptr);
}
}
return n & alphaWall & n;
}
else
{ {
FatalErrorInFunction const auto* ptr =
<< "Did not find field " << alphaName_ mesh.cfindObject<volSymmTensorField>(alphaName_);
<< " on mesh " << mesh.name() << " patch " << patch_.name()
<< nl if (ptr)
<< "Please set 'kappaMethod' to one of " {
<< flatOutput(KMethodTypeNames_.sortedToc()) << nl const symmTensorField& wallValues =
<< "and 'alpha' to the name of the volScalar" patch_.patchField<volSymmTensorField>(*ptr);
<< " or volSymmTensor field (if kappaMethod=lookup)"
<< exit(FatalError); const vectorField n(patch_.nf());
return n & wallValues & n;
}
} }
FatalErrorInFunction
<< "Did not find field '" << alphaName_
<< "' on mesh " << mesh.name()
<< " patch " << patch_.name() << nl
<< "Please set 'alpha' to the name of"
" a volScalar or volSymmTensor field"
", or use another method" << nl
<< " " << flatOutput(KMethodTypeNames_.sortedToc()) << nl
<< exit(FatalError);
break; break;
} }
@ -501,8 +561,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::alpha
<< "Unimplemented method " << KMethodTypeNames_[method_] << nl << "Unimplemented method " << KMethodTypeNames_[method_] << nl
<< "Please set 'kappaMethod' to one of " << "Please set 'kappaMethod' to one of "
<< flatOutput(KMethodTypeNames_.sortedToc()) << nl << flatOutput(KMethodTypeNames_.sortedToc()) << nl
<< "and 'alpha' to the name of the volScalar" << "If kappaMethod=lookup, also set 'alpha' to the name of"
<< " or volSymmTensor field (if kappaMethod=lookup)" " a volScalar or volSymmTensor field" << nl
<< exit(FatalError); << exit(FatalError);
break; break;

View File

@ -46,11 +46,11 @@ Description
\table \table
Property | Description | Required | Default Property | Description | Required | Default
kappaMethod | Thermal conductivity method | yes | kappaMethod | Thermal conductivity method | yes |
kappa | Name of thermal conductivity field | no | kappa | Name of thermal conductivity field | partly |
alpha | Name of thermal diffusivity field | no | alpha | Name of thermal diffusivity field | partly |
alphaAni | Name of non-isotropic alpha | no | alphaAni | Name of non-isotropic alpha | partly |
kappaValue | Function1 supplying kappa | no | kappaValue | Function1 supplying kappa | partly |
alphaValue | Function1 supplying alpha | no | alphaValue | Function1 supplying alpha | partly |
\endtable \endtable
Usage Usage
@ -130,12 +130,12 @@ protected:
//- Name of thermal conductivity field (if looked up from database) //- Name of thermal conductivity field (if looked up from database)
const word kappaName_; const word kappaName_;
//- Name of the non-Isotropic alpha (default: Anialpha)
const word alphaAniName_;
//- Name of thermal diffusivity //- Name of thermal diffusivity
const word alphaName_; const word alphaName_;
//- Name of the non-isotropic alpha (for directional solidThermo)
const word alphaAniName_;
//- Function1 for kappa //- Function1 for kappa
autoPtr<PatchFunction1<scalar>> kappaFunction1_; autoPtr<PatchFunction1<scalar>> kappaFunction1_;
@ -147,14 +147,22 @@ public:
// Constructors // Constructors
//- Construct from patch and K name //- Default construct from patch, using fluidThermo (default)
//- or specified method
explicit temperatureCoupledBase
(
const fvPatch& patch,
const KMethodType method = KMethodType::mtFluidThermo
);
//- Construct from patch, method type and field names
temperatureCoupledBase temperatureCoupledBase
( (
const fvPatch& patch, const fvPatch& patch,
const word& calculationMethod, const KMethodType method,
const word& kappaName, const word& kappaName,
const word& alphaAniName, const word& alphaName,
const word& alphaName const word& alphaAniName
); );
//- Construct from patch and dictionary //- Construct from patch and dictionary
@ -171,18 +179,15 @@ public:
const temperatureCoupledBase& base const temperatureCoupledBase& base
); );
//- Construct as copy //- Copy construct
temperatureCoupledBase temperatureCoupledBase(const temperatureCoupledBase& base);
(
const temperatureCoupledBase&
);
//- Destructor //- Destructor
virtual ~temperatureCoupledBase() = default; virtual ~temperatureCoupledBase() = default;
// Member functions // Member Functions
//- Method to obtain K //- Method to obtain K
word KMethod() const word KMethod() const
@ -191,13 +196,13 @@ public:
} }
//- Name of thermal conductivity field //- Name of thermal conductivity field
const word& kappaName() const const word& kappaName() const noexcept
{ {
return kappaName_; return kappaName_;
} }
//- Name of thermal diffusivity field //- Name of thermal diffusivity field
const word& alphaName() const const word& alphaName() const noexcept
{ {
return alphaName_; return alphaName_;
} }

View File

@ -49,14 +49,7 @@ turbulentTemperatureCoupledBaffleMixedFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
mappedPatchFieldBase<scalar> mappedPatchFieldBase<scalar>
( (
mappedPatchFieldBase<scalar>::mapper(p, iF), mappedPatchFieldBase<scalar>::mapper(p, iF),

View File

@ -51,14 +51,7 @@ turbulentTemperatureRadCoupledMixedFvPatchScalarField
) )
: :
mixedFvPatchScalarField(p, iF), mixedFvPatchScalarField(p, iF),
temperatureCoupledBase temperatureCoupledBase(patch()), // default method (fluidThermo)
(
patch(),
"undefined",
"undefined",
"undefined-K",
"undefined-alpha"
),
mappedPatchFieldBase<scalar> mappedPatchFieldBase<scalar>
( (
mappedPatchFieldBase<scalar>::mapper(p, iF), mappedPatchFieldBase<scalar>::mapper(p, iF),