ENH: make expressions::FieldAssociation a common enum

- use FACE_DATA (was SURFACE_DATA) for similarity with polySurface

ENH: add expression value enumerations and traits

- simple enumeration of standard types (bool, label, scalar, vector)
  that can be used as a value type-code for internal bookkeeping.

GIT: relocate pTraits into general traits/ directory
This commit is contained in:
Mark Olesen 2021-11-23 11:13:42 +01:00
parent 141403ed98
commit fbd7b78999
19 changed files with 442 additions and 55 deletions

View File

@ -0,0 +1,3 @@
Test-exprTraits.C
EXE = $(FOAM_USER_APPBIN)/Test-exprTraits

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Description
Basic tests of expression traits
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
#include "ITstream.H"
#include "exprTraits.H"
#include "uLabel.H"
#include "error.H"
#include "stringList.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class T>
void printTraits()
{
const auto typeCode = exprTypeTraits<T>::value;
Info<< "type " << pTraits<T>::typeName
<< " code:" << int(typeCode)
<< " name:" << exprTypeTraits<T>::name;
if (pTraits<T>::typeName != word(exprTypeTraits<T>::name))
{
Info<< " (UNSUPPORTED)";
}
Info << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main()
{
Info<< nl << "Traits:" << nl;
printTraits<word>();
printTraits<string>();
printTraits<bool>();
printTraits<label>();
printTraits<scalar>();
printTraits<vector>();
printTraits<tensor>();
printTraits<symmTensor>();
printTraits<sphericalTensor>();
const auto getName = nameOp<expressions::valueTypeCode>();
Info<< nl;
Info<< "Name of typeCode: "
<< Foam::name(expressions::valueTypeCode::type_scalar) << nl;
Info<< "Name of typeCode: "
<< getName(expressions::valueTypeCode::type_bool) << nl;
Info<< nl << "Done" << nl;
return 0;
}
// ************************************************************************* //

View File

@ -53,15 +53,15 @@ Note
using namespace Foam;
using FieldAssociation = expressions::volumeExpr::FieldAssociation;
using FieldAssociation = expressions::FieldAssociation;
word fieldGeoType(const FieldAssociation geoType)
{
switch (geoType)
{
case FieldAssociation::VOLUME_DATA : return "cells"; break;
case FieldAssociation::SURFACE_DATA : return "faces"; break;
case FieldAssociation::POINT_DATA : return "points"; break;
case FieldAssociation::FACE_DATA : return "faces"; break;
case FieldAssociation::VOLUME_DATA : return "cells"; break;
default: break;
}
@ -356,6 +356,7 @@ void evaluate
if (ptr)
{
conditionField = ptr->internalField();
// VOLUME_DATA
break;
}
}
@ -366,7 +367,7 @@ void evaluate
if (ptr)
{
conditionField = ptr->internalField();
conditionDataType = FieldAssociation::SURFACE_DATA;
conditionDataType = FieldAssociation::FACE_DATA;
break;
}
}

View File

@ -163,6 +163,8 @@ $(expr)/exprResult/exprResultStoredStack.C
$(expr)/exprString/exprString.C
$(expr)/exprTools/exprTools.C
$(expr)/traits/exprTraits.C
$(expr)/exprDriver/exprDriver.C
$(expr)/exprDriver/exprDriverFields.C
$(expr)/exprDriver/exprDriverIO.C

View File

@ -59,10 +59,12 @@ SourceFiles
#ifndef expressions_exprDriver_H
#define expressions_exprDriver_H
#include "exprString.H"
#include "exprResult.H"
#include "exprString.H"
#include "exprTraits.H"
#include "pointField.H"
#include "primitiveFields.H"
#include "objectRegistry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -57,12 +57,8 @@ SourceFiles
#ifndef expressions_exprResult_H
#define expressions_exprResult_H
#include "vector.H"
#include "tensor.H"
#include "sphericalTensor.H"
#include "symmTensor.H"
#include "exprTraits.H"
#include "dimensionedType.H"
#include "IOField.H"
#include "runTimeSelectionTables.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,62 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2021 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::expressions::FieldAssociation
Description
The field association for mesh (patch/volume) values.
\*---------------------------------------------------------------------------*/
#ifndef expressions_FieldAssociation_H
#define expressions_FieldAssociation_H
namespace Foam
{
namespace expressions
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
enum FieldAssociation : unsigned char
{
NO_DATA = 0, //!< No data
POINT_DATA = 1, //!< Point data
FACE_DATA = 2, //!< Face data
VOLUME_DATA = 4 //!< Volume data
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expressions
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,96 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 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/>.
\*---------------------------------------------------------------------------*/
#include "exprTraits.H"
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
Foam::expressions::valueTypeCode
Foam::expressions::valueTypeCodeOf(const word& dataTypeName)
{
#undef stringToTypeCode
#define stringToTypeCode(Type) \
\
if (dataTypeName == exprTypeTraits<Type>::name) \
{ \
return expressions::valueTypeCode::type_##Type; \
}
if (!dataTypeName.empty())
{
stringToTypeCode(bool);
stringToTypeCode(label);
stringToTypeCode(scalar);
stringToTypeCode(vector);
stringToTypeCode(tensor);
stringToTypeCode(sphericalTensor);
stringToTypeCode(symmTensor);
}
#undef stringToTypeCode
return expressions::valueTypeCode::INVALID;
}
Foam::word Foam::name(const expressions::valueTypeCode typeCode)
{
#undef case_typeCodeToString
#define case_typeCodeToString(Type) \
\
case expressions::valueTypeCode::type_##Type : \
{ \
return exprTypeTraits<Type>::name; \
}
switch (typeCode)
{
case expressions::valueTypeCode::NONE :
{
return "none";
}
case expressions::valueTypeCode::INVALID :
{
// ie, ""
break;
}
case_typeCodeToString(bool);
case_typeCodeToString(label);
case_typeCodeToString(scalar);
case_typeCodeToString(vector);
case_typeCodeToString(tensor);
case_typeCodeToString(sphericalTensor);
case_typeCodeToString(symmTensor);
}
#undef case_typeCodeToString
return word();
}
// ************************************************************************* //

View File

@ -0,0 +1,160 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 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::exprTypeTraits
Description
Simple type identifiers for polymorphic expression values.
The definitions are similar to std::integral_constant in that they
provide value, value_type (and name).
\*---------------------------------------------------------------------------*/
#ifndef expressions_exprTraits_H
#define expressions_exprTraits_H
// Regular field types
#include "label.H"
#include "scalar.H"
#include "vector.H"
#include "sphericalTensor.H"
#include "symmTensor.H"
#include "tensor.H"
#include "word.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
//- An enumeration of known and expected expression value types.
// Do not rely on the enumeration values for any direct coding.
//
// \note NONE use used when initializing types, whereas INVALID is used
// for unsupported types (never as a stored type).
// This avoids false positives when testing.
//
// Except NONE and INVALID, the enumerations will mostly not be used
// directly, but through exprTypeTraits :: value
enum class valueTypeCode : unsigned char
{
NONE = 0, //!< No type, or default initialized type
INVALID, //!< Invalid/unknown/error type
// Rank 0 types
type_bool, //!< Type is 'bool'
type_label, //!< Type is 'label'
type_scalar, //!< Type is 'scalar'
// Rank 1 types
type_vector, //!< Type is 'vector'
// Rank 2 types
type_sphericalTensor, //!< Type is 'sphericalTensor'
type_symmTensor, //!< Type is 'symmTensor'
type_tensor //!< Type is 'tensor'
};
// Global Functions
//- From string to valueTypeCode (if any)
valueTypeCode valueTypeCodeOf(const word& dataTypeName);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expressions
/*---------------------------------------------------------------------------*\
Class exprTypeTraits Declaration
\*---------------------------------------------------------------------------*/
// Generic enumerated traits is INVALID (unknown)
template<class Type>
struct exprTypeTraits
{
typedef Type value_type;
static constexpr const char* const name = "";
static constexpr
::Foam::expressions::valueTypeCode value =
::Foam::expressions::valueTypeCode::INVALID;
};
#undef defineExprTypeTraits
#define defineExprTypeTraits(Type, Name) \
template<> \
struct exprTypeTraits<Type> \
{ \
typedef Type value_type; \
static constexpr const char* const name = #Name; \
static constexpr \
::Foam::expressions::valueTypeCode value = \
::Foam::expressions::valueTypeCode::type_##Name; \
};
// Define with "name" to match regular pTraits typeName
defineExprTypeTraits(bool, bool);
defineExprTypeTraits(::Foam::label, label);
defineExprTypeTraits(::Foam::scalar, scalar);
defineExprTypeTraits(::Foam::vector, vector);
defineExprTypeTraits(::Foam::tensor, tensor);
defineExprTypeTraits(::Foam::sphericalTensor, sphericalTensor);
defineExprTypeTraits(::Foam::symmTensor, symmTensor);
#undef defineExprTypeTraits
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- A word representation of a valueTypeCode. Empty for INVALID
word name(const expressions::valueTypeCode typeCode);
//- A word representation of a valueTypeCode. Empty for INVALID
template<>
struct nameOp<expressions::valueTypeCode>
{
word operator()(const expressions::valueTypeCode typeCode) const
{
return Foam::name(typeCode);
}
};
// No IOstream Operators for valueTypeCode at the moment (Nov 2021)
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -67,6 +67,7 @@ SourceFiles
#include "patchExprFwd.H"
#include "fvExprDriver.H"
#include "exprFieldAssociation.H"
#include "Enum.H"
#include "volFields.H"
#include "surfaceFields.H"

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,10 +36,10 @@ inline Foam::label Foam::expressions::patchExpr::parseDriver::size
{
case FieldAssociation::POINT_DATA :
return patch_.patch().nPoints();
break;
case FieldAssociation::SURFACE_DATA :
case FieldAssociation::FACE_DATA :
return patch_.patch().size();
break;
default:
break;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -52,16 +52,6 @@ union scanToken;
//- Static debugging option
extern int debug;
//- The field association for patch expressions (mutually exclusive)
enum FieldAssociation : unsigned char
{
NO_DATA = 0, //!< No data
POINT_DATA = 1, //!< Point data
SURFACE_DATA = 2 //!< Surface data
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace patchExpr
@ -73,6 +63,7 @@ typedef patchExpr::parseDriver patchExprDriver;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expressions
} // End namespace Foam

View File

@ -84,6 +84,7 @@ SourceFiles
#include "volumeExprFwd.H"
#include "fvExprDriver.H"
#include "exprFieldAssociation.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "pointFields.H"
@ -126,7 +127,7 @@ protected:
bool isLogical_;
//- A volume/surface/point field
enum FieldAssociation fieldGeoType_;
expressions::FieldAssociation fieldGeoType_;
//- The result dimensions
dimensionSet resultDimension_;
@ -259,37 +260,37 @@ public:
//- The result type-name.
// Normally volScalarField, surfaceVectorField etc,
// but Scalar is modified for logical as volScalarField etc
const word& resultType() const
const word& resultType() const noexcept
{
return resultType_;
}
//- The geometric field association
FieldAssociation fieldAssociation() const
FieldAssociation fieldAssociation() const noexcept
{
return fieldGeoType_;
}
//- A logical (bool-like) field. Actually stored as a scalar.
bool isLogical() const
bool isLogical() const noexcept
{
return isLogical_;
}
//- A volume field
bool isVolumeData() const
bool isVolumeData() const noexcept
{
return fieldGeoType_ == FieldAssociation::VOLUME_DATA;
}
//- A surface field
bool isSurfaceData() const
bool isFaceData() const noexcept
{
return fieldGeoType_ == FieldAssociation::SURFACE_DATA;
return fieldGeoType_ == FieldAssociation::FACE_DATA;
}
//- A point field
bool isPointData() const
bool isPointData() const noexcept
{
return fieldGeoType_ == FieldAssociation::POINT_DATA;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,13 +36,13 @@ inline Foam::label Foam::expressions::volumeExpr::parseDriver::size
{
case FieldAssociation::POINT_DATA :
return mesh_.nPoints();
break;
case FieldAssociation::SURFACE_DATA :
case FieldAssociation::FACE_DATA :
return mesh_.nInternalFaces();
break;
case FieldAssociation::VOLUME_DATA :
return mesh_.nCells();
break;
default:
break;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -102,7 +102,7 @@ void Foam::expressions::volumeExpr::parseDriver::setResult
// Characteristics
resultType_ = pTraits<fieldType>::typeName;
isLogical_ = logical;
fieldGeoType_ = SURFACE_DATA;
fieldGeoType_ = FACE_DATA;
// Always strip out dimensions?
if (!resultDimension_.dimensionless())

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,6 +40,7 @@ namespace expressions
{
namespace volumeExpr
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Forward Declarations
@ -51,16 +52,6 @@ union scanToken;
//- Static debugging option
extern int debug;
//- The field association for volume expressions (mutually exclusive)
enum FieldAssociation : unsigned char
{
NO_DATA = 0, //!< No data
POINT_DATA = 1, //!< Point data
SURFACE_DATA = 2, //!< Surface data
VOLUME_DATA = 3 //!< Volume data
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace volumeExpr

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -77,9 +77,9 @@ public:
enum FieldAssociation
{
NO_DATA = 0, //!< No associated data
FACE_DATA = 0x1, //!< Data associated with faces
POINT_DATA = 0x2, //!< Data associated with points
FACE_POINT_DATA = 0x3 //!< Data associated with faces and points
FACE_DATA = 1, //!< Data associated with faces
POINT_DATA = 2, //!< Data associated with points
FACE_OR_POINT_DATA = 3 //!< Data associated with faces or points
};