ENH: simplify dictionary search for value/refValue in BCs

- in expressions BCs in particular, there is various logic handling
  for if value/refValue/refGradient etc are found or not.

  Handle the lookups as findEntry and branch to use Field assign
  or other handling, depending on its existence.

STYLE: use wordList instead of wordRes for copy/filter dictionary
This commit is contained in:
Mark Olesen
2022-09-28 09:24:46 +02:00
parent ea51c2c0e4
commit 3a6e427409
21 changed files with 106 additions and 99 deletions

View File

@ -42,8 +42,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef IOstreamOption_H
#define IOstreamOption_H
#ifndef Foam_IOstreamOption_H
#define Foam_IOstreamOption_H
#include "word.H"

View File

@ -32,10 +32,11 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef dictionaryContent_H
#define dictionaryContent_H
#ifndef Foam_dictionaryContent_H
#define Foam_dictionaryContent_H
#include "dictionary.H"
#include "wordList.H"
#include "wordRes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -254,8 +254,11 @@ Foam::expressions::exprResult::exprResult
{
DebugInFunction << nl;
if (dict.found("value"))
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (hasValue)
{
const auto& valueEntry = *hasValue;
const bool uniform = isUniform_;
const label len =
@ -268,12 +271,12 @@ Foam::expressions::exprResult::exprResult
const bool ok =
(
// Just use <scalar> for <label>?
readChecked<scalar>("value", dict, len, uniform)
|| readChecked<vector>("value", dict, len, uniform)
|| readChecked<tensor>("value", dict, len, uniform)
|| readChecked<symmTensor>("value", dict, len, uniform)
|| readChecked<sphericalTensor>("value", dict, len, uniform)
|| readChecked<bool>("value", dict, len, uniform)
readChecked<scalar>(valueEntry, len, uniform)
|| readChecked<vector>(valueEntry, len, uniform)
|| readChecked<tensor>(valueEntry, len, uniform)
|| readChecked<symmTensor>(valueEntry, len, uniform)
|| readChecked<sphericalTensor>(valueEntry, len, uniform)
|| readChecked<bool>(valueEntry, len, uniform)
);
if (!ok)

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -54,8 +54,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef expressions_exprResult_H
#define expressions_exprResult_H
#ifndef Foam_expressions_exprResult_H
#define Foam_expressions_exprResult_H
#include "exprTraits.H"
#include "dimensionedType.H"
@ -156,13 +156,12 @@ class exprResult
//- Dispatch to type-checked pointer deletion
void uglyDelete();
//- Type-checked creation of field from dictionary
//- Type-checked creation of field from dictionary (primitive) entry
// \return True if the type check was satisfied
template<class Type>
inline bool readChecked
(
const word& key,
const dictionary& dict,
const entry& e,
const label len,
const bool uniform
);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -82,8 +82,7 @@ inline bool Foam::expressions::exprResult::deleteChecked()
template<class Type>
inline bool Foam::expressions::exprResult::readChecked
(
const word& key,
const dictionary& dict,
const entry& e,
const label len,
const bool uniform
)
@ -96,7 +95,7 @@ inline bool Foam::expressions::exprResult::readChecked
if (uniform)
{
const Type val(dict.get<Type>(key));
const Type val(e.get<Type>());
size_ = len;
fieldPtr_ = new Field<Type>(size_, val);
@ -106,7 +105,7 @@ inline bool Foam::expressions::exprResult::readChecked
else
{
size_ = len;
fieldPtr_ = new Field<Type>(key, dict, size_);
fieldPtr_ = new Field<Type>(e, size_);
}
isUniform_ = uniform;

View File

@ -55,11 +55,11 @@ Foam::valuePointPatchField<Type>::valuePointPatchField
pointPatchField<Type>(p, iF, dict),
Field<Type>(p.size())
{
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (eptr)
if (hasValue)
{
Field<Type>::assign(*eptr, p.size());
Field<Type>::assign(*hasValue, p.size());
}
else if (!valueRequired)
{

View File

@ -139,10 +139,10 @@ public:
inline scalar dx() const;
//- Return the log10(x) flag
inline const Switch& log10() const;
inline Switch log10() const noexcept;
//- Return the bound flag
inline const Switch& bound() const;
inline Switch bound() const noexcept;
// Edit
@ -154,10 +154,10 @@ public:
inline scalar& dx();
//- Return the log10(x) flag
inline Switch& log10();
inline Switch& log10() noexcept;
//- Return the bound flag
inline Switch& bound();
inline Switch& bound() noexcept;
// Evaluation

View File

@ -40,14 +40,14 @@ Foam::scalar Foam::uniformInterpolationTable<Type>::dx() const
template<class Type>
const Foam::Switch& Foam::uniformInterpolationTable<Type>::log10() const
Foam::Switch Foam::uniformInterpolationTable<Type>::log10() const noexcept
{
return log10_;
}
template<class Type>
const Foam::Switch& Foam::uniformInterpolationTable<Type>::bound() const
Foam::Switch Foam::uniformInterpolationTable<Type>::bound() const noexcept
{
return bound_;
}
@ -68,14 +68,14 @@ Foam::scalar& Foam::uniformInterpolationTable<Type>::dx()
template<class Type>
Foam::Switch& Foam::uniformInterpolationTable<Type>::log10()
Foam::Switch& Foam::uniformInterpolationTable<Type>::log10() noexcept
{
return log10_;
}
template<class Type>
Foam::Switch& Foam::uniformInterpolationTable<Type>::bound()
Foam::Switch& Foam::uniformInterpolationTable<Type>::bound() noexcept
{
return bound_;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -364,7 +364,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
}
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& sw)
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch sw)
{
os << sw.c_str();
return os;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -37,8 +37,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef Switch_H
#define Switch_H
#ifndef Foam_Switch_H
#define Foam_Switch_H
#include "bool.H"
#include "stdFoam.H"
@ -69,7 +69,7 @@ class Switch;
Istream& operator>>(Istream& is, Switch& sw);
//- Write Switch to stream as its text value (eg, "true", "false")
Ostream& operator<<(Ostream& is, const Switch& sw);
Ostream& operator<<(Ostream& is, const Switch sw);
/*---------------------------------------------------------------------------*\
Class Switch Declaration
@ -130,19 +130,19 @@ public:
value_(switchType::FALSE)
{}
//- Construct from enumerated value
//- Implicit construct from enumerated value
constexpr Switch(const switchType sw) noexcept
:
value_(sw)
{}
//- Construct from bool
//- Implicit construct from bool
constexpr Switch(const bool b) noexcept
:
value_(b ? switchType::TRUE : switchType::FALSE)
{}
//- Construct from int (treat integer as bool value)
//- Implicit construct from int (treat integer as bool value)
constexpr Switch(const int i) noexcept
:
value_(i ? switchType::TRUE : switchType::FALSE)

View File

@ -88,11 +88,11 @@ Foam::faPatchField<Type>::faPatchField
{
/// if (valueRequired) - not yet needed. Already a lazy evaluation
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (eptr)
if (hasValue)
{
Field<Type>::assign(*eptr, p.size());
Field<Type>::assign(*hasValue, p.size());
}
else
{

View File

@ -85,11 +85,11 @@ Foam::faePatchField<Type>::faePatchField
Field<Type>(p.size()),
internalField_(iF)
{
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (eptr)
if (hasValue)
{
Field<Type>::assign(*eptr, p.size());
Field<Type>::assign(*hasValue, p.size());
}
else
{

View File

@ -85,7 +85,7 @@ Foam::exprFixedValueFvPatchField<Type>::exprFixedValueFvPatchField
const bool valueRequired
)
:
parent_bctype(p, iF),
parent_bctype(p, iF), // bypass dictionary constructor
expressions::patchExprFieldBase
(
dict,
@ -121,12 +121,15 @@ Foam::exprFixedValueFvPatchField<Type>::exprFixedValueFvPatchField
driver_.readDict(dict_);
if (dict.found("value"))
// Similar to fvPatchField constructor, which we have bypassed
dict.readIfPresent("patchType", this->patchType(), keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (hasValue)
{
fvPatchField<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*hasValue, p.size());
}
else
{

View File

@ -87,7 +87,7 @@ Foam::exprMixedFvPatchField<Type>::exprMixedFvPatchField
const dictionary& dict
)
:
parent_bctype(p, iF),
parent_bctype(p, iF), // bypass dictionary constructor
expressions::patchExprFieldBase
(
dict,
@ -160,27 +160,32 @@ Foam::exprMixedFvPatchField<Type>::exprMixedFvPatchField
}
}
driver_.readDict(dict_);
// Similar to fvPatchField constructor, which we have bypassed
dict.readIfPresent("patchType", this->patchType(), keyType::LITERAL);
bool needsRefValue = true;
if (dict.found("refValue"))
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
const auto* hasRefValue = dict.findEntry("refValue", keyType::LITERAL);
const auto* hasRefGradient
= dict.findEntry("refGradient", keyType::LITERAL);
const auto* hasValueFraction
= dict.findEntry("valueFraction", keyType::LITERAL);
if (hasRefValue)
{
needsRefValue = false;
this->refValue() = Field<Type>("refValue", dict, p.size());
this->refValue().assign(*hasRefValue, p.size());
}
if (dict.found("value"))
if (hasValue)
{
fvPatchField<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*hasValue, p.size());
if (needsRefValue)
if (!hasRefValue)
{
// Ensure refValue has a sensible value for the "update" below
this->refValue() = static_cast<const Field<Type>&>(*this);
@ -188,7 +193,7 @@ Foam::exprMixedFvPatchField<Type>::exprMixedFvPatchField
}
else
{
if (needsRefValue)
if (!hasRefValue)
{
this->refValue() = this->patchInternalField();
}
@ -204,18 +209,18 @@ Foam::exprMixedFvPatchField<Type>::exprMixedFvPatchField
}
if (dict.found("refGradient"))
if (hasRefGradient)
{
this->refGrad() = Field<Type>("refGradient", dict, p.size());
this->refGrad().assign(*hasRefGradient, p.size());
}
else
{
this->refGrad() = Zero;
}
if (dict.found("valueFraction"))
if (hasValueFraction)
{
this->valueFraction() = Field<scalar>("valueFraction", dict, p.size());
this->valueFraction().assign(*hasValueFraction, p.size());
}
else
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -85,7 +85,7 @@ Foam::exprValuePointPatchField<Type>::exprValuePointPatchField
const dictionary& dict
)
:
parent_bctype(p, iF),
parent_bctype(p, iF), // bypass dictionary constructor
expressions::patchExprFieldBase
(
dict,
@ -123,23 +123,20 @@ Foam::exprValuePointPatchField<Type>::exprValuePointPatchField
<< exit(FatalIOError);
}
driver_.readDict(dict_);
if (dict.found("value"))
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (hasValue)
{
Field<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*hasValue, p.size());
}
else
{
WarningInFunction
<< "No value defined for "
<< this->internalField().name()
<< " on " << this->patch().name()
<< endl;
// Note: valuePointPatchField defaults to Zero
// but internalField might be better
Field<Type>::operator=(Zero);
}
if (this->evalOnConstruct_)

View File

@ -41,8 +41,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef exprValuePointPatchField_H
#define exprValuePointPatchField_H
#ifndef Foam_exprValuePointPatchField_H
#define Foam_exprValuePointPatchField_H
#include "valuePointPatchField.H"
#include "patchExprFieldBase.H"

View File

@ -104,11 +104,11 @@ Foam::fvPatchField<Type>::fvPatchField
{
if (valueRequired)
{
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (eptr)
if (hasValue)
{
Field<Type>::assign(*eptr, p.size());
Field<Type>::assign(*hasValue, p.size());
}
else
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -91,11 +91,11 @@ Foam::fvsPatchField<Type>::fvsPatchField
{
if (valueRequired)
{
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
const auto* hasValue = dict.findEntry("value", keyType::LITERAL);
if (eptr)
if (hasValue)
{
Field<Type>::assign(*eptr, p.size());
Field<Type>::assign(*hasValue, p.size());
}
else
{

View File

@ -91,8 +91,8 @@ velocityFilmShellFvPatchVectorField::velocityFilmShellFvPatchVectorField
dictionaryContent::copyDict
(
dict,
wordRes(), // allow
wordRes // deny
wordList(), // allow
wordList // deny
({
"type", // redundant
"value", "refValue", "refGradient", "valueFraction"

View File

@ -85,8 +85,8 @@ thermalShellFvPatchScalarField::thermalShellFvPatchScalarField
dictionaryContent::copyDict
(
dict,
wordRes(), // allow
wordRes // deny
wordList(), // allow
wordList // deny
({
"type", // redundant
"value"

View File

@ -87,8 +87,8 @@ vibrationShellFvPatchScalarField::vibrationShellFvPatchScalarField
dictionaryContent::copyDict
(
dict,
wordRes(), // allow
wordRes // deny
wordList(), // allow
wordList // deny
({
"type", // redundant
"value", "refValue", "refGradient", "valueFraction"