ENH: support assign or construct Field from primitiveEntry

For example, instead of

   if (dict.found("value"))
   {
       fvScalarField::operator=
       (
           Field<scalar>("value", dict, p.size())
       );
   }

can use more precise specifications, and also eliminate searching
the dictionary multiple times:

   const auto* eptr = dict.findEntry("value", keyType::LITERAL);

   //or:  dict.findCompat("value", {{"oldName" ... }}, keyType::LITERAL);

   if (eptr)
   {
       fvScalarField::assign(*eptr, p.size());
   }

STYLE: combine declaration of FieldBase into Field.H
This commit is contained in:
Mark Olesen 2022-09-19 14:05:50 +02:00
parent 88061f3b28
commit 88f5be479e
10 changed files with 143 additions and 140 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2021 OpenCFD Ltd.
Copyright (C) 2015-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -176,46 +176,69 @@ Foam::Field<Type>::Field
}
template<class Type>
Foam::Field<Type>::Field(const entry& e, const label len)
{
assign(e, len);
}
template<class Type>
Foam::Field<Type>::Field
(
const word& keyword,
const dictionary& dict,
const label len
const label len,
enum keyType::option matchOpt
)
{
assign(keyword, dict, len, matchOpt);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::Field<Type>::assign(const entry& e, const label len)
{
if (len)
{
ITstream& is = dict.lookup(keyword);
ITstream& is = e.stream();
// Read first token
token firstToken(is);
if (firstToken.isWord("uniform"))
{
this->resize(len);
// Resize to expected length (or -1 : retain current length)
if (len >= 0)
{
this->resize(len);
}
operator=(pTraits<Type>(is));
}
else if (firstToken.isWord("nonuniform"))
{
is >> static_cast<List<Type>&>(*this);
const label lenRead = this->size();
if (len != lenRead)
// Check lengths
if (len >= 0 && len != lenRead)
{
if (len < lenRead && allowConstructFromLargerSize)
{
// Truncate the data
this->resize(len);
#ifdef FULLDEBUG
IOWarningInFunction(dict)
IOWarningInFunction(is)
<< "Sizes do not match. Truncating " << lenRead
<< " entries to " << len << endl;
#endif
// Truncate the data
this->resize(len);
}
else
{
FatalIOErrorInFunction(dict)
FatalIOErrorInFunction(is)
<< "size " << lenRead
<< " is not equal to the expected length " << len
<< exit(FatalIOError);
@ -224,7 +247,7 @@ Foam::Field<Type>::Field
}
else
{
FatalIOErrorInFunction(dict)
FatalIOErrorInFunction(is)
<< "Expected keyword 'uniform' or 'nonuniform', found "
<< firstToken.info() << nl
<< exit(FatalIOError);
@ -233,7 +256,21 @@ Foam::Field<Type>::Field
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::Field<Type>::assign
(
const word& keyword,
const dictionary& dict,
const label len,
enum keyType::option matchOpt
)
{
if (len)
{
assign(dict.lookupEntry(keyword, matchOpt), len);
}
}
template<class Type>
void Foam::Field<Type>::map
@ -246,7 +283,7 @@ void Foam::Field<Type>::map
if (f.size() != mapAddressing.size())
{
f.setSize(mapAddressing.size());
f.resize(mapAddressing.size());
}
if (mapF.size() > 0)
@ -288,7 +325,7 @@ void Foam::Field<Type>::map
if (f.size() != mapAddressing.size())
{
f.setSize(mapAddressing.size());
f.resize(mapAddressing.size());
}
if (mapWeights.size() != mapAddressing.size())
@ -363,7 +400,7 @@ void Foam::Field<Type>::map
// from distribution. Note: this behaviour is different compared
// to local mapper.
this->transfer(newMapF);
this->setSize(mapper.size());
this->resize(mapper.size());
}
}
else
@ -435,7 +472,7 @@ void Foam::Field<Type>::autoMap
// from distribution. Note: this behaviour is different compared
// to local mapper.
this->transfer(fCpy);
this->setSize(mapper.size());
this->resize(mapper.size());
}
}
else
@ -455,7 +492,7 @@ void Foam::Field<Type>::autoMap
}
else
{
this->setSize(mapper.size());
this->resize(mapper.size());
}
}
}

View File

@ -37,6 +37,7 @@ SourceFiles
FieldI.H
FieldM.H
Field.C
FieldBase.C
FieldFunctions.C
FieldFunctionsM.C
@ -48,8 +49,8 @@ SourceFiles
#include "tmp.H"
#include "direction.H"
#include "labelList.H"
#include "keyType.H"
#include "scalarList.H"
#include "FieldBase.H"
#include "VectorSpace.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -60,15 +61,44 @@ namespace Foam
// Forward Declarations
class FieldMapper;
class dictionary;
class entry;
template<class Type> class Field;
template<class Type> class SubField;
template<class Type>
Ostream& operator<<(Ostream&, const Field<Type>&);
template<class Type> Ostream& operator<<(Ostream&, const Field<Type>&);
template<class Type> Ostream& operator<<(Ostream&, const tmp<Field<Type>>&);
template<class Type>
Ostream& operator<<(Ostream&, const tmp<Field<Type>>&);
/*---------------------------------------------------------------------------*\
Class FieldBase Declaration
\*---------------------------------------------------------------------------*/
//- Template invariant parts for Field and SubField
class FieldBase
:
public refCount
{
public:
// Static Data Members
//- Typename for Field
static const char* const typeName;
//- Permit read construct from a larger size.
// Mostly required for things like column mesh, for example.
static bool allowConstructFromLargerSize;
// Constructors
//- Default construct
constexpr FieldBase() noexcept
:
refCount()
{}
};
/*---------------------------------------------------------------------------*\
@ -234,8 +264,18 @@ public:
//- Construct from Istream
inline Field(Istream& is);
//- Construct from a dictionary entry
Field(const word& keyword, const dictionary& dict, const label len);
//- Construct from a dictionary (primitive) entry
Field(const entry& e, const label len);
//- Construct from lookup of a dictionary (primitive) entry.
// Uses REGEX lookup for legacy compatibility.
Field
(
const word& keyword,
const dictionary& dict,
const label len,
enum keyType::option matchOpt = keyType::REGEX
);
//- Clone
inline tmp<Field<Type>> clone() const;
@ -257,6 +297,19 @@ public:
// Member Functions
//- Assign from a dictionary (primitive) entry
void assign(const entry& e, const label len);
//- Assign from lookup of a dictionary (primitive) entry
// Uses REGEX lookup for legacy compatibility.
void assign
(
const word& keyword,
const dictionary& dict,
const label len,
enum keyType::option matchOpt = keyType::REGEX
);
//- 1 to 1 map from the given field
void map
(

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -25,9 +25,9 @@ License
\*---------------------------------------------------------------------------*/
#include "FieldBase.H"
#include "Field.H"
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::FieldBase::typeName("Field");

View File

@ -1,83 +1 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::FieldBase
Description
Template invariant parts for Field and SubField
SourceFiles
FieldBase.C
\*---------------------------------------------------------------------------*/
#ifndef FieldBase_H
#define FieldBase_H
#include "refCount.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class FieldBase Declaration
\*---------------------------------------------------------------------------*/
class FieldBase
:
public refCount
{
public:
// Static Data Members
//- Typename for Field
static const char* const typeName;
//- Permit read construct from a larger size.
// Mostly required for things like column mesh, for example.
static bool allowConstructFromLargerSize;
// Constructors
//- Default construct, refCount zero
constexpr FieldBase() noexcept
:
refCount()
{}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
#endif
// ************************************************************************* //
#warning File removed - left for old dependency check only

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -55,12 +55,11 @@ Foam::valuePointPatchField<Type>::valuePointPatchField
pointPatchField<Type>(p, iF, dict),
Field<Type>(p.size())
{
if (dict.found("value"))
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
if (eptr)
{
Field<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*eptr, p.size());
}
else if (!valueRequired)
{

View File

@ -40,8 +40,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef keyType_H
#define keyType_H
#ifndef Foam_keyType_H
#define Foam_keyType_H
#include "word.H"
#include "wordRe.H"

View File

@ -98,16 +98,15 @@ Foam::faPatchField<Type>::faPatchField
/// if (valueRequired) - not yet needed. Already a lazy evaluation
if (dict.found("value"))
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
if (eptr)
{
faPatchField<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*eptr, p.size());
}
else
{
faPatchField<Type>::operator=(pTraits<Type>::zero);
Field<Type>::operator=(Zero);
}
}

View File

@ -84,16 +84,15 @@ Foam::faePatchField<Type>::faePatchField
patch_(p),
internalField_(iF)
{
if (dict.found("value"))
const auto* eptr = dict.findEntry("value", keyType::LITERAL);
if (eptr)
{
faePatchField<Type>::operator=
(
Field<Type>("value", dict, p.size())
);
Field<Type>::assign(*eptr, p.size());
}
else
{
faePatchField<Type>::operator=(pTraits<Type>::zero);
Field<Type>::operator=(Zero);
}
}

View File

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

View File

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