ENH: improve some efficiency in expressions

- use refPtr to simplify some logic.
- avoid copying field if an average will be used
- initialize geometric fields with a uniform value instead of Zero
- minor tweak of method names

- apply bugfix #1889 (longer description elsewhere)
This commit is contained in:
Mark Olesen
2020-10-23 16:38:00 +02:00
parent 51b2490258
commit 5579e7a62b
16 changed files with 282 additions and 286 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider Copyright (C) 2010-2018 Bernhard Gschaider
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -284,7 +284,7 @@ public:
//- Return the expression result as a tmp field //- Return the expression result as a tmp field
template<class Type> template<class Type>
tmp<Field<Type>> getResult(bool isPointVal=false); tmp<Field<Type>> getResult(bool wantPointData=false);
//- The result type as word - same as result().valueType() //- The result type as word - same as result().valueType()
virtual word getResultType() const virtual word getResultType() const
@ -388,7 +388,7 @@ public:
bool isLocalVariable bool isLocalVariable
( (
const word& name, const word& name,
bool isPointVal, bool wantPointData = false,
label expectedSize = -1 label expectedSize = -1
) const; ) const;
@ -424,7 +424,7 @@ public:
evaluate evaluate
( (
const expressions::exprString& expr, const expressions::exprString& expr,
bool isPointVal = false bool wantPointData = false
); );
//- Evaluate the expression and return a single value //- Evaluate the expression and return a single value
@ -432,7 +432,7 @@ public:
inline Type evaluateUniform inline Type evaluateUniform
( (
const expressions::exprString& expr, const expressions::exprString& expr,
bool isPointVal = false bool wantPointData = false
); );

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -75,12 +75,12 @@ template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::expressions::exprDriver::evaluate Foam::tmp<Foam::Field<Type>> Foam::expressions::exprDriver::evaluate
( (
const expressions::exprString& expr, const expressions::exprString& expr,
bool isPointVal bool wantPointData
) )
{ {
parse(expr); parse(expr);
return getResult<Type>(isPointVal); return getResult<Type>(wantPointData);
} }
@ -89,7 +89,7 @@ template<class Type>
inline Type Foam::expressions::exprDriver::evaluateUniform inline Type Foam::expressions::exprDriver::evaluateUniform
( (
const expressions::exprString& expr, const expressions::exprString& expr,
bool isPointVal bool wantPointData
) )
{ {
parse(expr); parse(expr);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -145,13 +145,13 @@ Type Foam::expressions::exprDriver::exprDriver::weightedSum
template<class Type> template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::tmp<Foam::Field<Type>>
Foam::expressions::exprDriver::getResult(bool isPointVal) Foam::expressions::exprDriver::getResult(bool wantPointData)
{ {
if (!result_.isPointValue(isPointVal)) if (!result_.isPointData(wantPointData))
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Expected a" << (isPointVal ? " point" : "") << "Expected a" << (wantPointData ? " point" : "")
<< " field, but found a" << (!isPointVal ? " point" : "") << " field, but found a" << (!wantPointData ? " point" : "")
<< " field" << nl << " field" << nl
<< exit(FatalError); << exit(FatalError);
} }
@ -166,15 +166,16 @@ template<class Type>
bool Foam::expressions::exprDriver::isLocalVariable bool Foam::expressions::exprDriver::isLocalVariable
( (
const word& name, const word& name,
bool isPointVal, bool wantPointData,
label expectedSize label expectedSize
) const ) const
{ {
DebugInfo DebugInfo
<< "Looking for local" << (isPointVal ? " point" : "") << "Looking for local" << (wantPointData ? " point" : "")
<< " field name:" << name << " type:" << " field name:" << name << " type:"
<< pTraits<Type>::typeName << " size:" << expectedSize; << pTraits<Type>::typeName << " size:" << expectedSize;
bool good = hasVariable(name); bool good = hasVariable(name);
if (good) if (good)
@ -182,10 +183,11 @@ bool Foam::expressions::exprDriver::isLocalVariable
const exprResult& var = variable(name); const exprResult& var = variable(name);
DebugInfo DebugInfo
<< " - found (" << var.valueType() << ' ' << var.isPointValue() << ')'; << " - found (" << var.valueType() << ' '
<< var.isPointData() << ')';
good = (var.isType<Type>() && var.isPointValue(isPointVal)); good = (var.isType<Type>() && var.isPointData(wantPointData));
// Do size checking if requested // Do size checking if requested
if (good && expectedSize >= 0) if (good && expectedSize >= 0)

View File

@ -166,7 +166,7 @@ bool Foam::expressions::exprResult::getUniformCheckedBool
// TODO? // TODO?
} }
result.setResult(avg, size); result.setResult<Type>(avg, size);
return true; return true;
} }
@ -207,7 +207,7 @@ Foam::expressions::exprResult::exprResult()
refCount(), refCount(),
valType_(), valType_(),
isUniform_(false), isUniform_(false),
isPointVal_(false), isPointData_(false),
noReset_(false), noReset_(false),
needsReset_(false), needsReset_(false),
size_(0), size_(0),
@ -245,7 +245,7 @@ Foam::expressions::exprResult::exprResult
refCount(), refCount(),
valType_(dict.getOrDefault<word>("valueType", "")), valType_(dict.getOrDefault<word>("valueType", "")),
isUniform_(dict.getOrDefault("isSingleValue", uniform)), isUniform_(dict.getOrDefault("isSingleValue", uniform)),
isPointVal_(dict.getOrDefault("isPointValue", false)), isPointData_(dict.getOrDefault("isPointValue", false)),
noReset_(dict.getOrDefault("noReset", false)), noReset_(dict.getOrDefault("noReset", false)),
needsReset_(false), needsReset_(false),
size_(0), size_(0),
@ -268,20 +268,21 @@ Foam::expressions::exprResult::exprResult
const bool ok = const bool ok =
( (
readChecked<bool>("value", dict, len, uniform) // Just use <scalar> for <label>?
|| readChecked<scalar>("value", dict, len, uniform) readChecked<scalar>("value", dict, len, uniform)
|| readChecked<vector>("value", dict, len, uniform) || readChecked<vector>("value", dict, len, uniform)
|| readChecked<tensor>("value", dict, len, uniform) || readChecked<tensor>("value", dict, len, uniform)
|| readChecked<symmTensor>("value", dict, len, uniform) || readChecked<symmTensor>("value", dict, len, uniform)
|| readChecked<sphericalTensor>("value", dict, len, uniform) || readChecked<sphericalTensor>("value", dict, len, uniform)
|| readChecked<bool>("value", dict, len, uniform)
); );
if (!ok) if (!ok)
{ {
if (valType_.empty()) if (valType_.empty())
{ {
// For the error message only // For error message only
valType_ = "None"; valType_ = "none";
} }
FatalErrorInFunction FatalErrorInFunction
@ -316,12 +317,13 @@ Foam::expressions::exprResult::New
if (!cstrIter.found()) if (!cstrIter.found())
{ {
FatalErrorInLookup FatalIOErrorInLookup
( (
dict,
"resultType", "resultType",
resultType, resultType,
*emptyConstructorTablePtr_ *emptyConstructorTablePtr_
) << exit(FatalError); ) << exit(FatalIOError);
} }
DebugInfo DebugInfo
@ -335,12 +337,13 @@ Foam::expressions::exprResult::New
if (!cstrIter.found()) if (!cstrIter.found())
{ {
FatalErrorInLookup FatalIOErrorInLookup
( (
dict,
"resultType", "resultType",
resultType, resultType,
*dictionaryConstructorTablePtr_ *dictionaryConstructorTablePtr_
) << exit(FatalError); ) << exit(FatalIOError);
} }
DebugInfo DebugInfo
@ -384,7 +387,7 @@ void Foam::expressions::exprResult::clear()
{ {
uglyDelete(); uglyDelete();
valType_.clear(); valType_.clear();
objectPtr_.reset(); objectPtr_.reset(nullptr);
size_ = 0; size_ = 0;
} }
@ -496,7 +499,7 @@ void Foam::expressions::exprResult::operator=(const exprResult& rhs)
valType_ = rhs.valType_; valType_ = rhs.valType_;
isUniform_ = rhs.isUniform_; isUniform_ = rhs.isUniform_;
isPointVal_ = rhs.isPointVal_; isPointData_ = rhs.isPointData_;
single_ = rhs.single_; single_ = rhs.single_;
if (rhs.fieldPtr_) if (rhs.fieldPtr_)
@ -538,7 +541,7 @@ void Foam::expressions::exprResult::operator=(exprResult&& rhs)
valType_ = rhs.valType_; valType_ = rhs.valType_;
isUniform_ = rhs.isUniform_; isUniform_ = rhs.isUniform_;
isPointVal_ = rhs.isPointVal_; isPointData_ = rhs.isPointData_;
noReset_ = rhs.noReset_; noReset_ = rhs.noReset_;
needsReset_ = rhs.needsReset_; needsReset_ = rhs.needsReset_;
size_ = rhs.size_; size_ = rhs.size_;
@ -596,7 +599,7 @@ void Foam::expressions::exprResult::writeDict
} }
os.writeEntry("resultType", valueType()); os.writeEntry("resultType", valueType());
os.writeEntryIfDifferent<Switch>("noReset_", false, noReset_); os.writeEntryIfDifferent<Switch>("noReset", false, noReset_);
if (fieldPtr_ == nullptr) if (fieldPtr_ == nullptr)
{ {
@ -606,7 +609,7 @@ void Foam::expressions::exprResult::writeDict
{ {
os.writeEntry("valueType", valType_); os.writeEntry("valueType", valType_);
os.writeEntryIfDifferent<Switch>("isPointValue", false, isPointVal_); os.writeEntryIfDifferent<Switch>("isPointValue", false, isPointData_);
os.writeEntry<Switch>("isSingleValue", isUniform_); os.writeEntry<Switch>("isSingleValue", isUniform_);
const bool ok = const bool ok =

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -89,8 +89,8 @@ class exprResult
//- Is single, uniform value (can be a non-field) //- Is single, uniform value (can be a non-field)
bool isUniform_; bool isUniform_;
//- Is a point value //- Represents point data
bool isPointVal_; bool isPointData_;
//- Whether or not the variable will be reset //- Whether or not the variable will be reset
bool noReset_; bool noReset_;
@ -98,7 +98,7 @@ class exprResult
//- Allow override of noReset_, but only accessible for subclasses //- Allow override of noReset_, but only accessible for subclasses
bool needsReset_; bool needsReset_;
//- Size of field or object //- Size (length) of field or object
label size_; label size_;
//- A %union of single values, including standard VectorSpace types //- A %union of single values, including standard VectorSpace types
@ -112,7 +112,7 @@ class exprResult
symmTensor symmTensor_; symmTensor symmTensor_;
sphericalTensor sphTensor_; sphericalTensor sphTensor_;
//- Construct null, zero-initialized //- Default construct, zero-initialized
singleValue(); singleValue();
//- Copy construct //- Copy construct
@ -126,21 +126,24 @@ class exprResult
inline const T& get() const inline const T& get() const
{ {
WarningInFunction WarningInFunction
<< "Not implemented for type " << pTraits<T>::typeName << nl; << "Not implemented for type "
<< pTraits<T>::typeName << nl;
return pTraits<T>::zero; return pTraits<T>::zero;
} }
//- Set new value for specified type. Returns updated value. //- Set new value for specified type.
// \return updated value
template<class T> template<class T>
inline const T& set(const T& val) inline const T& set(const T& val)
{ {
WarningInFunction WarningInFunction
<< "Not implemented for type " << pTraits<T>::typeName << nl; << "Not implemented for type "
<< pTraits<T>::typeName << nl;
return val; return val;
} }
}; };
//- The single value //- The single value representation
singleValue single_; singleValue single_;
//- Allocated plain field (eg, scalarField) //- Allocated plain field (eg, scalarField)
@ -182,7 +185,7 @@ class exprResult
const bool parRun const bool parRun
) const; ) const;
//- Type-checked retrieval of uniform field from current results //- Type-checked retrieval of \c bool uniform field from current result
// \return True if the type check was satisfied // \return True if the type check was satisfied
bool getUniformCheckedBool bool getUniformCheckedBool
( (
@ -233,13 +236,13 @@ class exprResult
template<class Type> template<class Type>
inline void setResultImpl(Field<Type>*, bool isPointVal=false); inline void setResultImpl(Field<Type>*, bool wantPointData=false);
template<class Type> template<class Type>
inline void setResultImpl(const Field<Type>&, bool isPointVal=false); inline void setResultImpl(const Field<Type>&, bool wantPointData=false);
template<class Type> template<class Type>
inline void setResultImpl(Field<Type>&&, bool isPointVal=false); inline void setResultImpl(Field<Type>&&, bool wantPointData=false);
template<class Type> template<class Type>
inline void setResultImpl(const Type& val, const label len); inline void setResultImpl(const Type& val, const label len);
@ -250,19 +253,13 @@ class exprResult
template<class Type> template<class Type>
inline void setObjectResultImpl(Type* ptr); inline void setObjectResultImpl(Type* ptr);
template<class Type>
inline void setObjectResultImpl(autoPtr<Type>& ap);
template<class Type>
inline void setObjectResultImpl(autoPtr<Type>&& ap);
protected: protected:
// Protected Member Functions // Protected Member Functions
//- Simulate virtual templated methods //- Simulate virtual templated methods
inline virtual exprResult& target() { return *this; } inline virtual expressions::exprResult& target() { return *this; }
//- Reset at new timestep according to the derived class type //- Reset at new timestep according to the derived class type
virtual void resetImpl(); virtual void resetImpl();
@ -309,7 +306,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
exprResult(); exprResult();
//- Copy construct //- Copy construct
@ -328,23 +325,19 @@ public:
//- Construct by copying a field //- Construct by copying a field
template<class Type> template<class Type>
exprResult(const Field<Type>& f); explicit exprResult(const Field<Type>& fld);
//- Construct by moving a field //- Construct by moving a field
template<class Type> template<class Type>
exprResult(Field<Type>&& f); explicit exprResult(Field<Type>&& fld);
//- Construct for an IOobject //- Construct for an IOobject
template<class Type> template<class Type>
exprResult(autoPtr<Type>& ap); explicit exprResult(autoPtr<Type>&& obj);
//- Construct for an IOobject
template<class Type>
exprResult(autoPtr<Type>&& ap);
//- Construct from a dimensioned value //- Construct from a dimensioned value
template<class Type> template<class Type>
exprResult(const dimensioned<Type>& f); explicit exprResult(const dimensioned<Type>& dt);
#undef exprResult_Construct #undef exprResult_Construct
#define exprResult_Construct(Type) \ #define exprResult_Construct(Type) \
@ -390,8 +383,9 @@ public:
//- Basic type for the field or single value //- Basic type for the field or single value
inline const word& valueType() const; inline const word& valueType() const;
//- True if representing point values, or test if same as isPointVal //- True if representing point data,
inline bool isPointValue(const bool isPointVal = true) const; //- or test for same value as wantPointData argument
inline bool isPointData(const bool wantPointData=true) const;
//- True if single, uniform value //- True if single, uniform value
inline bool isUniform() const; inline bool isUniform() const;
@ -400,6 +394,12 @@ public:
template<class Type> template<class Type>
inline bool isType() const; inline bool isType() const;
//- Return a single value when isUniform() is true,
//- or Zero when it is non-uniform or if the type mismatches,
//- which means that it can generally be considered as failsafe.
template<class Type>
inline Type getValue() const;
//- True if valueType is a bool //- True if valueType is a bool
inline bool isBool() const; inline bool isBool() const;
@ -436,15 +436,15 @@ public:
//- Set result field, taking ownership of the pointer //- Set result field, taking ownership of the pointer
template<class Type> template<class Type>
inline void setResult(Field<Type>*, bool isPointVal=false); inline void setResult(Field<Type>*, bool wantPointData=false);
//- Set result field, taking copy of the field contents //- Set result field, taking copy of the field contents
template<class Type> template<class Type>
inline void setResult(const Field<Type>& fld, bool isPointVal=false); inline void setResult(const Field<Type>&, bool wantPointData=false);
//- Set result field, moving field contents //- Set result field, moving field contents
template<class Type> template<class Type>
inline void setResult(Field<Type>&&, bool isPointVal=false); inline void setResult(Field<Type>&&, bool wantPointData=false);
//- Set uniform result field of given size //- Set uniform result field of given size
template<class Type> template<class Type>
@ -454,11 +454,9 @@ public:
template<class Type> template<class Type>
inline void setSingleValue(const Type& val); inline void setSingleValue(const Type& val);
//- Set result object
template<class Type> template<class Type>
inline void setObjectResult(autoPtr<Type>& o); inline void setObjectResult(autoPtr<Type>&& obj);
template<class Type>
inline void setObjectResult(autoPtr<Type>&& o);
// Access/Get results // Access/Get results

View File

@ -33,18 +33,18 @@ namespace Foam
namespace expressions namespace expressions
{ {
#undef defineExpressionMethod #undef defineExpressionMethod
#define defineExpressionMethod(Type, Var) \ #define defineExpressionMethod(Type, Member) \
template<> \ template<> \
inline const Type& exprResult::singleValue::get<Type>() const \ inline const Type& exprResult::singleValue::get<Type>() const \
{ \ { \
return Var; \ return Member; \
} \ } \
\ \
template<> \ template<> \
inline const Type& exprResult::singleValue::set(const Type& val) \ inline const Type& exprResult::singleValue::set(const Type& val) \
{ \ { \
Var = val; \ Member = val; \
return Var; \ return Member; \
} }
defineExpressionMethod(bool, bool_); defineExpressionMethod(bool, bool_);
@ -67,6 +67,7 @@ template<class Type>
inline bool Foam::expressions::exprResult::deleteChecked() inline bool Foam::expressions::exprResult::deleteChecked()
{ {
const bool ok = isType<Type>(); const bool ok = isType<Type>();
if (ok && fieldPtr_ != nullptr) if (ok && fieldPtr_ != nullptr)
{ {
delete static_cast<Field<Type>*>(fieldPtr_); delete static_cast<Field<Type>*>(fieldPtr_);
@ -88,6 +89,7 @@ inline bool Foam::expressions::exprResult::readChecked
) )
{ {
const bool ok = isType<Type>(); const bool ok = isType<Type>();
if (ok) if (ok)
{ {
uglyDelete(); uglyDelete();
@ -190,52 +192,43 @@ bool Foam::expressions::exprResult::multiplyEqChecked
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::expressions::exprResult::exprResult(const Field<Type>& f) Foam::expressions::exprResult::exprResult(const Field<Type>& fld)
: :
exprResult() exprResult()
{ {
DebugInFunction << nl; DebugInFunction << nl;
setResult(f); setResult(fld);
} }
template<class Type> template<class Type>
Foam::expressions::exprResult::exprResult(Field<Type>&& f) Foam::expressions::exprResult::exprResult(Field<Type>&& fld)
: :
exprResult() exprResult()
{ {
DebugInFunction << nl; DebugInFunction << nl;
setResult(std::move(f)); setResult(std::move(fld));
} }
template<class Type> template<class Type>
Foam::expressions::exprResult::exprResult(autoPtr<Type>& o) Foam::expressions::exprResult::exprResult(autoPtr<Type>&& obj)
: :
exprResult() exprResult()
{ {
setObjectResult(o); setObjectResult(std::move(obj));
} }
template<class Type> template<class Type>
Foam::expressions::exprResult::exprResult(autoPtr<Type>&& o) Foam::expressions::exprResult::exprResult(const dimensioned<Type>& dt)
:
exprResult()
{
setObjectResult(o);
}
template<class Type>
Foam::expressions::exprResult::exprResult(const dimensioned<Type>& f)
: :
exprResult() exprResult()
{ {
DebugInFunction << nl; DebugInFunction << nl;
setSingleValue(f.value()); setSingleValue(dt.value());
} }
@ -253,12 +246,12 @@ inline const Foam::word& Foam::expressions::exprResult::valueType() const
} }
inline bool Foam::expressions::exprResult::isPointValue inline bool Foam::expressions::exprResult::isPointData
( (
const bool isPointVal const bool wantPointData
) const ) const
{ {
return isPointVal_ == isPointVal; return isPointData_ == wantPointData;
} }
@ -275,6 +268,18 @@ inline bool Foam::expressions::exprResult::isType() const
} }
template<class Type>
inline Type Foam::expressions::exprResult::getValue() const
{
if (!isUniform_ || !isType<Type>())
{
return Zero;
}
return single_.get<Type>();
}
inline bool Foam::expressions::exprResult::isBool() const inline bool Foam::expressions::exprResult::isBool() const
{ {
return valType_ == pTraits<bool>::typeName; return valType_ == pTraits<bool>::typeName;
@ -297,12 +302,12 @@ template<class Type>
void Foam::expressions::exprResult::setResult void Foam::expressions::exprResult::setResult
( (
const Field<Type>& val, const Field<Type>& val,
bool isPointVal bool wantPointData
) )
{ {
DebugInFunction << nl; DebugInFunction << nl;
target().setResultImpl(val, isPointVal); target().setResultImpl(val, wantPointData);
} }
@ -310,12 +315,12 @@ template<class Type>
void Foam::expressions::exprResult::setResult void Foam::expressions::exprResult::setResult
( (
Field<Type>&& val, Field<Type>&& val,
bool isPointVal bool wantPointData
) )
{ {
DebugInFunction << nl; DebugInFunction << nl;
target().setResultImpl(val, isPointVal); target().setResultImpl(val, wantPointData);
} }
@ -323,7 +328,7 @@ template<class Type>
void Foam::expressions::exprResult::setResultImpl void Foam::expressions::exprResult::setResultImpl
( (
const Field<Type>& fld, const Field<Type>& fld,
bool isPointVal bool wantPointData
) )
{ {
DebugInFunction << nl; DebugInFunction << nl;
@ -331,7 +336,7 @@ void Foam::expressions::exprResult::setResultImpl
clear(); clear();
isUniform_ = false; isUniform_ = false;
isPointVal_ = isPointVal; isPointData_ = wantPointData;
size_ = fld.size(); size_ = fld.size();
valType_ = pTraits<Type>::typeName; valType_ = pTraits<Type>::typeName;
@ -345,7 +350,7 @@ template<class Type>
void Foam::expressions::exprResult::setResultImpl void Foam::expressions::exprResult::setResultImpl
( (
Field<Type>&& fld, Field<Type>&& fld,
bool isPointVal bool wantPointData
) )
{ {
DebugInFunction << nl; DebugInFunction << nl;
@ -353,7 +358,7 @@ void Foam::expressions::exprResult::setResultImpl
clear(); clear();
isUniform_ = false; isUniform_ = false;
isPointVal_ = isPointVal; isPointData_ = wantPointData;
size_ = fld.size(); size_ = fld.size();
valType_ = pTraits<Type>::typeName; valType_ = pTraits<Type>::typeName;
@ -364,44 +369,26 @@ void Foam::expressions::exprResult::setResultImpl
template<class Type> template<class Type>
void Foam::expressions::exprResult::setObjectResult(autoPtr<Type>& ap) void Foam::expressions::exprResult::setObjectResult(autoPtr<Type>&& obj)
{ {
target().setObjectResultImpl(ap.ptr()); target().setObjectResultImpl(obj.ptr()); // release()
}
template<class Type>
void Foam::expressions::exprResult::setObjectResult(autoPtr<Type>&& ap)
{
target().setObjectResultImpl(ap.ptr());
} }
template<class T> template<class T>
void Foam::expressions::exprResult::setObjectResultImpl(T* ptr) void Foam::expressions::exprResult::setObjectResultImpl(T* ptr)
{ {
DebugInFunction << nl; clear();
isUniform_ = false; isUniform_ = false;
isPointVal_ = false; isPointData_ = false;
size_ = ptr->size(); if (ptr != nullptr)
valType_ = ptr->typeName; {
objectPtr_.reset(ptr); size_ = ptr->size();
} valType_ = ptr->typeName;
objectPtr_.reset(ptr);
}
template<class T>
void Foam::expressions::exprResult::setObjectResultImpl(autoPtr<T>& o)
{
setObjectResultImpl(o.ptr());
}
template<class T>
void Foam::expressions::exprResult::setObjectResultImpl(autoPtr<T>&& o)
{
setObjectResultImpl(o.ptr());
} }
@ -409,10 +396,10 @@ template<class Type>
void Foam::expressions::exprResult::setResult void Foam::expressions::exprResult::setResult
( (
Field<Type>* fldPtr, Field<Type>* fldPtr,
bool isPointVal bool wantPointData
) )
{ {
target().setResultImpl(fldPtr, isPointVal); target().setResultImpl(fldPtr, wantPointData);
} }
@ -420,19 +407,20 @@ template<class Type>
void Foam::expressions::exprResult::setResultImpl void Foam::expressions::exprResult::setResultImpl
( (
Field<Type>* fldPtr, Field<Type>* fldPtr,
bool isPointVal bool wantPointData
) )
{ {
DebugInFunction << nl;
clear(); clear();
isPointVal_ = isPointVal;
isUniform_ = false; isUniform_ = false;
isPointData_ = wantPointData;
size_ = fldPtr->size(); if (fldPtr != nullptr)
valType_ = pTraits<Type>::typeName; {
fieldPtr_ = fldPtr; size_ = fldPtr->size();
valType_ = pTraits<Type>::typeName;
fieldPtr_ = fldPtr;
}
} }
@ -458,7 +446,7 @@ void Foam::expressions::exprResult::setResultImpl
clear(); clear();
isPointVal_ = false; isPointData_ = false;
size_ = len; size_ = len;
valType_ = pTraits<Type>::typeName; valType_ = pTraits<Type>::typeName;
@ -653,8 +641,8 @@ void Foam::expressions::exprResult::setSingleValueImpl(const Type& val)
clear(); clear();
isPointVal_ = false;
isUniform_ = true; isUniform_ = true;
isPointData_ = false;
single_.set(val); single_.set(val);
size_ = 1; size_ = 1;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -78,7 +78,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
exprResultStack(); exprResultStack();
//- Copy construct //- Copy construct

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -89,7 +89,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
exprResultStored(); exprResultStored();
//- Copy construct //- Copy construct

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -71,7 +71,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
exprResultStoredStack(); exprResultStoredStack();
//- Copy construct //- Copy construct

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -34,43 +34,42 @@ Foam::expressions::fieldExpr::parseDriver::getField
const word& name const word& name
) const ) const
{ {
bool isPointVal = false; bool hasPointData = false;
bool isUniformVal = false;
tmp<Field<Type>> tfield; refPtr<expressions::exprResult> tvar;
if (hasVariable(name) && variable(name).isType<Type>()) if (hasVariable(name) && variable(name).isType<Type>())
{ {
const expressions::exprResult& var = variable(name); tvar.cref(variable(name));
hasPointData = tvar().isPointData();
isPointVal = var.isPointValue();
isUniformVal = var.isUniform();
tfield = var.cref<Type>().clone();
} }
if (tfield.valid())
{
const label fldLen = tfield().size();
const label len = (isPointVal ? this->pointSize() : this->size());
if (returnReduce((fldLen == len), andOp<bool>())) if (tvar.valid())
{
const auto& var = tvar.cref();
const Field<Type>& vals = var.cref<Type>();
const label len = (hasPointData ? this->pointSize() : this->size());
if (returnReduce((vals.size() == len), andOp<bool>()))
{ {
return tfield; // Return a copy of the field
return tmp<Field<Type>>::New(vals);
} }
if (!isUniformVal) if (!var.isUniform())
{ {
WarningInFunction WarningInFunction
<< "Variable " << name << "Variable " << name
<< " does not fit the size and is not a uniform value." << nl << " is nonuniform and does not fit the size"
<< "Using average value" << endl; << ". Using average" << endl;
} }
return tmp<Field<Type>>::New(this->size(), gAverage(tfield)); return tmp<Field<Type>>::New(this->size(), gAverage(vals));
} }
return tfield; return nullptr;
} }

View File

@ -163,6 +163,7 @@ protected:
} }
//- Default boundary type for volume fields is zeroGradient //- Default boundary type for volume fields is zeroGradient
//- since they are essentially just internal fields.
template<class Type> template<class Type>
static inline word defaultBoundaryType static inline word defaultBoundaryType
( (
@ -211,8 +212,8 @@ protected:
bool isGlobalVariable bool isGlobalVariable
( (
const word& name, const word& name,
bool isPointVal, const bool wantPointData = false,
label expectedSize = -1 const label expectedSize = -1
) const; ) const;
//- Return the global variable if available or a null result //- Return the global variable if available or a null result
@ -226,8 +227,8 @@ protected:
bool isField bool isField
( (
const word& name, const word& name,
bool isPointVal = false, const bool wantPointData = false,
label expectSize = -1 //!< ignored const label expectSize = -1 //!< ignored
) const; ) const;
@ -236,8 +237,8 @@ protected:
inline tmp<GeomField> getOrReadField inline tmp<GeomField> getOrReadField
( (
const word& name, const word& name,
bool mandatory = true, const bool mandatory = true,
bool getOldTime = false const bool getOldTime = false
); );
//- Retrieve point field from memory or disk //- Retrieve point field from memory or disk
@ -245,8 +246,8 @@ protected:
inline tmp<GeomField> getOrReadPointField inline tmp<GeomField> getOrReadPointField
( (
const word& name, const word& name,
bool mandatory = true, const bool mandatory = true,
bool getOldTime = false const bool getOldTime = false
); );
//- Retrieve field from memory or disk (implementation) //- Retrieve field from memory or disk (implementation)
@ -255,8 +256,8 @@ protected:
( (
const word& name, const word& name,
const MeshRef& meshRef, const MeshRef& meshRef,
bool mandatory = true, const bool mandatory = true,
bool getOldTime = false const bool getOldTime = false
); );
//- Helper function for getOrReadField //- Helper function for getOrReadField
@ -461,8 +462,8 @@ public:
inline bool isVariableOrField inline bool isVariableOrField
( (
const word& name, const word& name,
bool isPointVal = false, const bool wantPointData = false,
label expectSize = -1 const label expectSize = -1
) const; ) const;
//- Retrieve local/global variable as a tmp field //- Retrieve local/global variable as a tmp field
@ -475,7 +476,7 @@ public:
tmp<Field<Type>> getVariable tmp<Field<Type>> getVariable
( (
const word& name, const word& name,
label expectSize, const label expectSize,
const bool mandatory = true const bool mandatory = true
) const; ) const;
@ -568,7 +569,7 @@ public:
inline bool isVariable inline bool isVariable
( (
const word& name, const word& name,
bool isPointVal = false, bool wantPointData = false,
label expectSize = -1 label expectSize = -1
) const; ) const;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -73,14 +73,14 @@ template<class Type>
inline bool Foam::expressions::fvExprDriver::isVariable inline bool Foam::expressions::fvExprDriver::isVariable
( (
const word& name, const word& name,
bool isPointVal, const bool wantPointData,
label expectedSize const label expectedSize
) const ) const
{ {
return return
( (
this->isLocalVariable<Type>(name, isPointVal, expectedSize) this->isLocalVariable<Type>(name, wantPointData, expectedSize)
|| this->isGlobalVariable<Type>(name, isPointVal, expectedSize) || this->isGlobalVariable<Type>(name, wantPointData, expectedSize)
); );
} }
@ -89,15 +89,15 @@ template<class Type>
inline bool Foam::expressions::fvExprDriver::isVariableOrField inline bool Foam::expressions::fvExprDriver::isVariableOrField
( (
const word& name, const word& name,
bool isPointVal, const bool wantPointData,
label expectedSize const label expectedSize
) )
const const
{ {
return return
( (
this->isVariable<Type>(name, isPointVal, expectedSize) this->isVariable<Type>(name, wantPointData, expectedSize)
|| this->isField<Type>(name, isPointVal) || this->isField<Type>(name, wantPointData)
); );
} }
@ -107,8 +107,8 @@ inline Foam::tmp<GeomField>
Foam::expressions::fvExprDriver::getOrReadField Foam::expressions::fvExprDriver::getOrReadField
( (
const word& name, const word& name,
bool mandatory, const bool mandatory,
bool getOldTime const bool getOldTime
) )
{ {
return this->getOrReadFieldImpl<GeomField> return this->getOrReadFieldImpl<GeomField>
@ -126,8 +126,8 @@ inline Foam::tmp<GeomField>
Foam::expressions::fvExprDriver::getOrReadPointField Foam::expressions::fvExprDriver::getOrReadPointField
( (
const word& name, const word& name,
bool mandatory, const bool mandatory,
bool getOldTime const bool getOldTime
) )
{ {
return this->getOrReadFieldImpl<GeomField> return this->getOrReadFieldImpl<GeomField>

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com> Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,21 +36,23 @@ template<class Type>
bool Foam::expressions::fvExprDriver::isGlobalVariable bool Foam::expressions::fvExprDriver::isGlobalVariable
( (
const word& name, const word& name,
bool isPointVal, const bool wantPointData,
label expectedSize const label expectedSize
) const ) const
{ {
DebugInfo DebugInfo
<< "Looking for global" << (isPointVal ? " point" : "") << "Looking for global" << (wantPointData ? " point" : "")
<< " field name:" << name; << " field name:" << name;
const exprResult& result = lookupGlobal(name); const expressions::exprResult& result = lookupGlobal(name);
DebugInfo DebugInfo
<< " - found (" << result.valueType() << ' ' << result.isPointValue() << ')'; << " - found (" << result.valueType() << ' '
<< result.isPointData() << ')';
bool good = (result.isType<Type>() && result.isPointValue(isPointVal)); bool good =
(result.isType<Type>() && result.isPointData(wantPointData));
// Do size checking if requested // Do size checking if requested
if (good && expectedSize >= 0) if (good && expectedSize >= 0)
@ -75,48 +77,47 @@ Foam::tmp<Foam::Field<Type>>
Foam::expressions::fvExprDriver::getVariable Foam::expressions::fvExprDriver::getVariable
( (
const word& name, const word& name,
label expectedSize, const label expectedSize,
const bool mandatory const bool mandatory
) const ) const
{ {
tmp<Field<Type>> tresult; refPtr<expressions::exprResult> tvar;
bool isSingleValue = false;
if (hasVariable(name) && variable(name).isType<Type>()) if (hasVariable(name) && variable(name).isType<Type>())
{ {
isSingleValue = variable(name).isUniform(); tvar.cref(variable(name));
tresult = variable(name).cref<Type>().clone();
} }
else if (isGlobalVariable<Type>(name, false)) else if (isGlobalVariable<Type>(name))
{ {
const exprResult& var = lookupGlobal(name); tvar.cref(lookupGlobal(name));
isSingleValue = var.isUniform();
tresult = var.cref<Type>().clone();
} }
if (tresult.valid())
if (tvar.valid())
{ {
const auto& var = tvar.cref();
const Field<Type>& vals = var.cref<Type>();
if if
( (
expectedSize < 0 expectedSize < 0
|| returnReduce((tresult->size() == expectedSize), andOp<bool>()) || returnReduce((vals.size() == expectedSize), andOp<bool>())
) )
{ {
return tresult; // Return a copy of the field
return tmp<Field<Type>>::New(vals);
} }
if (!isSingleValue) if (!var.isUniform())
{ {
WarningInFunction WarningInFunction
<< "Variable " << name << "Variable " << name
<< " is not a single value and does not fit the size " << " is nonuniform and does not fit the size "
<< expectedSize << ". Using average" << endl; << expectedSize << ". Using average" << endl;
} }
return tmp<Field<Type>>::New(expectedSize, gAverage(tresult())); return tmp<Field<Type>>::New(expectedSize, gAverage(vals));
} }
if (mandatory) if (mandatory)
@ -197,7 +198,7 @@ template<class Type>
bool Foam::expressions::fvExprDriver::isField bool Foam::expressions::fvExprDriver::isField
( (
const word& name, const word& name,
bool isPointVal, bool wantPointData,
label label
) const ) const
{ {
@ -212,7 +213,7 @@ bool Foam::expressions::fvExprDriver::isField
return return
( (
isPointVal wantPointData
? this->foundField<pfieldType>(name) ? this->foundField<pfieldType>(name)
: :
( (
@ -240,17 +241,29 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
<< "> Type: " << GeomField::typeName << endl; << "> Type: " << GeomField::typeName << endl;
} }
refPtr<expressions::exprResult> tvar;
if (hasVariable(name) && variable(name).isType<Type>())
{
tvar.cref(variable(name));
}
else if (isGlobalVariable<Type>(name))
{
tvar.cref(lookupGlobal(name));
}
tmp<GeomField> tfield; tmp<GeomField> tfield;
if if (tvar.valid())
(
(hasVariable(name) && variable(name).isType<Type>())
|| isGlobalVariable<Type>(name, false)
)
{ {
const auto& var = tvar.cref();
const Type deflt(var.getValue<Type>());
if (debug) if (debug)
{ {
Info<< "Getting " << name << " from variables" << endl; Info<< "Getting " << name << " from variables. Default: "
<< deflt << endl;
} }
if (debug) if (debug)
@ -259,12 +272,15 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
<< GeomField::typeName << nl; << GeomField::typeName << nl;
} }
tfield.reset tfield = GeomField::New
( (
GeomField::New(name, meshRef, dimensioned<Type>(Zero)) name,
meshRef,
dimensioned<Type>(deflt),
// Patch is zeroGradient (volFields) or calculated (other)
defaultBoundaryType(GeomField::null())
); );
auto& fld = tfield.ref();
GeomField& fld = tfield.ref();
if (debug) if (debug)
{ {
@ -272,16 +288,7 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
<< fld.ownedByRegistry() << endl; << fld.ownedByRegistry() << endl;
} }
Field<Type> vals; const Field<Type>& vals = var.cref<Type>();
if (hasVariable(name) && variable(name).isType<Type>())
{
vals = variable(name).cref<Type>();
}
else
{
vals = lookupGlobal(name).cref<Type>();
}
if (debug) if (debug)
{ {
@ -294,7 +301,7 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
} }
else else
{ {
Type avg = gAverage(vals); const Type avg = gAverage(vals);
bool noWarn = false; bool noWarn = false;
@ -321,21 +328,23 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
const objectRegistry& obr = meshRef.thisDb(); const objectRegistry& obr = meshRef.thisDb();
if (searchInMemory() && obr.foundObject<GeomField>(name)) const GeomField* origFldPtr;
if
(
searchInMemory()
&& (origFldPtr = obr.cfindObject<GeomField>(name)) != nullptr
)
{ {
if (debug) if (debug)
{ {
Info<< "Retrieve registered: " << name << nl; Info<< "Retrieve registered: " << name << nl;
} }
const GeomField& origFld = obr.lookupObject<GeomField>(name); const GeomField& origFld = *origFldPtr;
// Avoid shadowing the original object // Avoid shadowing the original object
tfield = GeomField::New(name + "_exprDriverCopy", origFld);
tfield.reset
(
GeomField::New(name + "_exprDriverCopy", origFld)
);
if (getOldTime) if (getOldTime)
{ {
@ -369,9 +378,10 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
// oldTime automatically read // oldTime automatically read
} }
if (debug) if (debug)
{ {
Info<< "field: valid()=" << tfield.valid() << endl; Info<< "field: valid()=" << Switch::name(tfield.valid()) << endl;
} }
if (tfield.valid()) if (tfield.valid())

View File

@ -39,7 +39,7 @@ void Foam::expressions::patchExprFieldBase::readExpressions
( (
const dictionary& dict, const dictionary& dict,
enum expectedTypes expectedType, enum expectedTypes expectedType,
bool isPointVal bool wantPointData
) )
{ {
if (debug_) if (debug_)
@ -108,7 +108,7 @@ void Foam::expressions::patchExprFieldBase::readExpressions
else if (!exprFrac.empty()) else if (!exprFrac.empty())
{ {
evalFrac = true; evalFrac = true;
if (isPointVal) if (wantPointData)
{ {
exprFrac = "toPoint(" + exprFrac + ")"; exprFrac = "toPoint(" + exprFrac + ")";
} }
@ -149,7 +149,7 @@ Foam::expressions::patchExprFieldBase::patchExprFieldBase
( (
const dictionary& dict, const dictionary& dict,
enum expectedTypes expectedType, enum expectedTypes expectedType,
bool isPointVal bool wantPointData
) )
: :
debug_(dict.getOrDefault("debug", false)), debug_(dict.getOrDefault("debug", false)),
@ -158,7 +158,7 @@ Foam::expressions::patchExprFieldBase::patchExprFieldBase
gradExpr_(), gradExpr_(),
fracExpr_() fracExpr_()
{ {
readExpressions(dict, expectedType, isPointVal); readExpressions(dict, expectedType, wantPointData);
} }

View File

@ -99,7 +99,7 @@ private:
( (
const dictionary& dict, const dictionary& dict,
enum expectedTypes expectedType, enum expectedTypes expectedType,
bool isPointVal = false bool wantPointData = false
); );
@ -118,7 +118,7 @@ public:
( (
const dictionary& dict, const dictionary& dict,
enum expectedTypes expectedType = expectedTypes::VALUE_TYPE, enum expectedTypes expectedType = expectedTypes::VALUE_TYPE,
bool isPointVal = false bool wantPointData = false
); );

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,51 +36,46 @@ Foam::expressions::patchExpr::parseDriver::getVariableIfAvailable
const word& name const word& name
) const ) const
{ {
bool isPointVal = false; bool hasPointData = false;
bool isUniformVal = false;
tmp<Field<Type>> tfield; refPtr<expressions::exprResult> tvar;
if (hasVariable(name) && variable(name).isType<Type>()) if (hasVariable(name) && variable(name).isType<Type>())
{ {
const expressions::exprResult& var = variable(name); tvar.cref(variable(name));
hasPointData = tvar().isPointData();
isPointVal = var.isPointValue();
isUniformVal = var.isUniform();
tfield = var.cref<Type>().clone();
} }
else if (isGlobalVariable<Type>(name, false)) else if (isGlobalVariable<Type>(name))
{ {
const expressions::exprResult& var = lookupGlobal(name); tvar.cref(lookupGlobal(name));
isUniformVal = var.isUniform();
tfield = var.cref<Type>().clone();
} }
if (tfield.valid())
{
const label fldLen = tfield().size();
const label len = (isPointVal ? this->pointSize() : this->size());
if (returnReduce((fldLen == len), andOp<bool>())) if (tvar.valid())
{
const auto& var = tvar.cref();
const Field<Type>& vals = var.cref<Type>();
const label len = (hasPointData ? this->pointSize() : this->size());
if (returnReduce((vals.size() == len), andOp<bool>()))
{ {
return tfield; // Return a copy of the field
return tmp<Field<Type>>::New(vals);
} }
if (!isUniformVal) if (!var.isUniform())
{ {
WarningInFunction WarningInFunction
<< "Variable " << name << "Variable " << name
<< " does not fit the size and is not a uniform value." << nl << " is nonuniform and does not fit the size"
<< "Using average value" << endl; << ". Using average" << endl;
} }
return tmp<Field<Type>>::New(this->size(), gAverage(tfield)); return tmp<Field<Type>>::New(this->size(), gAverage(vals));
} }
return tfield; return nullptr;
} }