ENH: additional DimensionedField, GeometricField factory methods

- construct based on db and mesh information from an existing field

- check movable() instead of isTmp() when reusing fields

STYLE: isolate check for reuse GeometricField into Detail namespace
This commit is contained in:
Mark Olesen
2022-10-05 13:12:53 +02:00
parent bcd461926c
commit 0c89f38312
9 changed files with 329 additions and 113 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -65,8 +65,7 @@ int main(int argc, char *argv[])
"tensor",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
{ IOobject::READ_IF_PRESENT, IOobject::NO_REGISTER }
),
mesh,
dimensioned<tensor>(dimless, tensor(1,2,3,4,5,6,7,8,9))
@ -75,6 +74,47 @@ int main(int argc, char *argv[])
Info().beginBlock("transformed")
<< tensorfld.T() << nl;
Info().endBlock();
{
auto tfld =
DimensionedField<scalar, volMesh>::New
(
tensorfld,
"scalar",
dimensioned<scalar>(14)
);
Info().beginBlock(tfld().type())
<< tfld << nl;
Info().endBlock();
}
{
auto tfld =
volScalarField::New
(
"scalar",
tensorfld.mesh(),
dimensioned<scalar>(5)
);
Info().beginBlock(tfld().type())
<< tfld() << nl;
Info().endBlock();
// From dissimilar types
auto tfld2 =
volVectorField::New
(
tfld(),
"vector",
dimensioned<vector>(Zero)
);
Info().beginBlock(tfld2().type())
<< tfld2() << nl;
Info().endBlock();
}
}
#ifdef TEST_UINT8_FIELD

View File

@ -282,7 +282,7 @@ public:
tmp<DimensionedField<Type, GeoMesh>> clone() const;
// Static Constructors
// Factory Methods
//- Return tmp field from name, mesh, dimensions,
//- copy of internal field.
@ -334,7 +334,31 @@ public:
static tmp<DimensionedField<Type, GeoMesh>> New
(
const word& newName,
const tmp<DimensionedField<Type, GeoMesh>>&
const tmp<DimensionedField<Type, GeoMesh>>& tfld
);
//- Construct tmp field based on mesh/registry information from
//- an existing field.
// Created NO_READ, NO_WRITE, NO_REGISTER, using the instance
// from the field
template<class AnyType>
static tmp<DimensionedField<Type, GeoMesh>> New
(
const DimensionedField<AnyType, GeoMesh>& fld,
const word& name,
const dimensionSet& dims
);
//- Construct tmp field based on mesh/registry information from
//- an existing field and initialise with value.
// Created NO_READ, NO_WRITE, NO_REGISTER, using the instance
// from the field
template<class AnyType>
static tmp<DimensionedField<Type, GeoMesh>> New
(
const DimensionedField<AnyType, GeoMesh>& fld,
const word& name,
const dimensioned<Type>& dt
);

View File

@ -44,9 +44,9 @@ Foam::DimensionedField<Type, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -72,9 +72,9 @@ Foam::DimensionedField<Type, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -99,9 +99,9 @@ Foam::DimensionedField<Type, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -126,9 +126,9 @@ Foam::DimensionedField<Type, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
dt,
@ -153,13 +153,67 @@ Foam::DimensionedField<Type, GeoMesh>::New
tfld().instance(),
tfld().local(),
tfld().db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
tfld
);
}
template<class Type, class GeoMesh>
template<class AnyType>
Foam::tmp<Foam::DimensionedField<Type, GeoMesh>>
Foam::DimensionedField<Type, GeoMesh>::New
(
const DimensionedField<AnyType, GeoMesh>& fld,
const word& name,
const dimensionSet& dims
)
{
return tmp<DimensionedField<Type, GeoMesh>>::New
(
IOobject
(
name,
fld.instance(),
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dims
);
}
template<class Type, class GeoMesh>
template<class AnyType>
Foam::tmp<Foam::DimensionedField<Type, GeoMesh>>
Foam::DimensionedField<Type, GeoMesh>::New
(
const DimensionedField<AnyType, GeoMesh>& fld,
const word& name,
const dimensioned<Type>& dt
)
{
return tmp<DimensionedField<Type, GeoMesh>>::New
(
IOobject
(
name,
fld.instance(),
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dt
);
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,8 +26,8 @@ License
\*---------------------------------------------------------------------------*/
#ifndef DimensionedFieldReuseFunctions_H
#define DimensionedFieldReuseFunctions_H
#ifndef Foam_DimensionedFieldReuseFunctions_H
#define Foam_DimensionedFieldReuseFunctions_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -66,14 +66,17 @@ struct reuseTmpDimensionedField
template<class TypeR, class GeoMesh>
struct reuseTmpDimensionedField<TypeR, TypeR, GeoMesh>
{
//- Allow optional copy assignment of the initial content
//- for identical input and output types
static tmp<DimensionedField<TypeR, GeoMesh>> New
(
const tmp<DimensionedField<TypeR, GeoMesh>>& tdf1,
const word& name,
const dimensionSet& dimensions
const dimensionSet& dimensions,
const bool initCopy = false
)
{
if (tdf1.isTmp())
if (tdf1.movable())
{
auto& df1 = tdf1.constCast();
@ -84,7 +87,7 @@ struct reuseTmpDimensionedField<TypeR, TypeR, GeoMesh>
const auto& df1 = tdf1();
return tmp<DimensionedField<TypeR, GeoMesh>>::New
auto tresult = tmp<DimensionedField<TypeR, GeoMesh>>::New
(
IOobject
(
@ -95,6 +98,13 @@ struct reuseTmpDimensionedField<TypeR, TypeR, GeoMesh>
df1.mesh(),
dimensions
);
if (initCopy)
{
tresult.ref().field() = tdf1().field();
}
return tresult;
}
};
@ -105,14 +115,16 @@ tmp<DimensionedField<TypeR, GeoMesh>> New
(
const tmp<DimensionedField<TypeR, GeoMesh>>& tdf1,
const word& name,
const dimensionSet& dimensions
const dimensionSet& dimensions,
const bool initCopy = false
)
{
return reuseTmpDimensionedField<TypeR, TypeR, GeoMesh>::New
(
tdf1,
name,
dimensions
dimensions,
initCopy
);
}
@ -156,7 +168,7 @@ struct reuseTmpTmpDimensionedField<TypeR, Type1, Type12, TypeR, GeoMesh>
const dimensionSet& dimensions
)
{
if (tdf2.isTmp())
if (tdf2.movable())
{
auto& df2 = tdf2.constCast();
@ -193,7 +205,7 @@ struct reuseTmpTmpDimensionedField<TypeR, TypeR, TypeR, Type2, GeoMesh>
const dimensionSet& dimensions
)
{
if (tdf1.isTmp())
if (tdf1.movable())
{
auto& df1 = tdf1.constCast();
@ -230,7 +242,7 @@ struct reuseTmpTmpDimensionedField<TypeR, TypeR, TypeR, TypeR, GeoMesh>
const dimensionSet& dimensions
)
{
if (tdf1.isTmp())
if (tdf1.movable())
{
auto& df1 = tdf1.constCast();
@ -238,7 +250,7 @@ struct reuseTmpTmpDimensionedField<TypeR, TypeR, TypeR, TypeR, GeoMesh>
df1.dimensions().reset(dimensions);
return tdf1;
}
else if (tdf2.isTmp())
if (tdf2.movable())
{
auto& df2 = tdf2.constCast();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,8 +26,8 @@ License
\*---------------------------------------------------------------------------*/
#ifndef FieldFieldReuseFunctions_H
#define FieldFieldReuseFunctions_H
#ifndef Foam_FieldFieldReuseFunctions_H
#define Foam_FieldFieldReuseFunctions_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,7 +58,7 @@ struct reuseTmpFieldField<Field, TypeR, TypeR>
const bool initCopy = false
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
@ -117,7 +117,7 @@ struct reuseTmpTmpFieldField<Field, TypeR, Type1, Type12, TypeR>
const tmp<FieldField<Field, TypeR>>& tf2
)
{
if (tf2.isTmp())
if (tf2.movable())
{
return tf2;
}
@ -136,7 +136,7 @@ struct reuseTmpTmpFieldField<Field, TypeR, TypeR, TypeR, Type2>
const tmp<FieldField<Field, Type2>>& tf2
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
@ -155,11 +155,11 @@ struct reuseTmpTmpFieldField<Field, TypeR, TypeR, TypeR, TypeR>
const tmp<FieldField<Field, TypeR>>& tf2
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
else if (tf2.isTmp())
if (tf2.movable())
{
return tf2;
}

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.
@ -26,8 +26,8 @@ License
\*---------------------------------------------------------------------------*/
#ifndef FieldReuseFunctions_H
#define FieldReuseFunctions_H
#ifndef Foam_FieldReuseFunctions_H
#define Foam_FieldReuseFunctions_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -49,13 +49,15 @@ struct reuseTmp
template<class TypeR>
struct reuseTmp<TypeR, TypeR>
{
//- Allow optional copy assignment of the initial content
//- for identical input and output types
static tmp<Field<TypeR>> New
(
const tmp<Field<TypeR>>& tf1,
const bool initCopy = false
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
@ -106,7 +108,7 @@ struct reuseTmpTmp<TypeR, Type1, Type12, TypeR>
const tmp<Field<TypeR>>& tf2
)
{
if (tf2.isTmp())
if (tf2.movable())
{
return tf2;
}
@ -125,7 +127,7 @@ struct reuseTmpTmp<TypeR, TypeR, TypeR, Type2>
const tmp<Field<Type2>>& tf2
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
@ -144,11 +146,11 @@ struct reuseTmpTmp<TypeR, TypeR, TypeR, TypeR>
const tmp<Field<TypeR>>& tf2
)
{
if (tf1.isTmp())
if (tf1.movable())
{
return tf1;
}
else if (tf2.isTmp())
if (tf2.movable())
{
return tf2;
}

View File

@ -379,7 +379,7 @@ public:
tmp<GeometricField<Type, PatchField, GeoMesh>> clone() const;
// Static Constructors
// Factory Methods
//- Return tmp field from name, mesh, dimensions and patch type.
// The field is NO_READ, NO_WRITE, unregistered and uses the
@ -463,6 +463,32 @@ public:
const wordList& actualPatchTypes = wordList()
);
//- Construct tmp field based on mesh/registry information from
//- an existing field.
// Created NO_READ, NO_WRITE, NO_REGISTER, using the instance
// from the field
template<class AnyType>
static tmp<GeometricField<Type, PatchField, GeoMesh>> New
(
const GeometricField<AnyType, PatchField, GeoMesh>& fld,
const word& name,
const dimensionSet& dims,
const word& patchFieldType = PatchField<Type>::calculatedType()
);
//- Construct tmp field based on mesh/registry information from
//- an existing field and initialise with value.
// Created NO_READ, NO_WRITE, NO_REGISTER, using the instance
// from the field
template<class AnyType>
static tmp<GeometricField<Type, PatchField, GeoMesh>> New
(
const GeometricField<AnyType, PatchField, GeoMesh>& fld,
const word& name,
const dimensioned<Type>& dt,
const word& patchFieldType = PatchField<Type>::calculatedType()
);
//- Destructor
virtual ~GeometricField();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -45,9 +45,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -74,9 +74,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -104,9 +104,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
ds,
@ -133,9 +133,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
dt,
@ -162,9 +162,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
name,
mesh.thisDb().time().timeName(),
mesh.thisDb(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
mesh,
dt,
@ -190,9 +190,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
tgf().instance(),
tgf().local(),
tgf().db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
tgf
);
@ -217,9 +217,9 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
tgf().instance(),
tgf().local(),
tgf().db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
tgf,
patchFieldTypes,
@ -228,4 +228,62 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
}
template<class Type, template<class> class PatchField, class GeoMesh>
template<class AnyType>
Foam::tmp<Foam::GeometricField<Type, PatchField, GeoMesh>>
Foam::GeometricField<Type, PatchField, GeoMesh>::New
(
const GeometricField<AnyType, PatchField, GeoMesh>& fld,
const word& name,
const dimensionSet& dims,
const word& patchFieldType
)
{
return tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
IOobject
(
name,
fld.instance(),
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dims,
patchFieldType
);
}
template<class Type, template<class> class PatchField, class GeoMesh>
template<class AnyType>
Foam::tmp<Foam::GeometricField<Type, PatchField, GeoMesh>>
Foam::GeometricField<Type, PatchField, GeoMesh>::New
(
const GeometricField<AnyType, PatchField, GeoMesh>& fld,
const word& name,
const dimensioned<Type>& dt,
const word& patchFieldType
)
{
return tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
IOobject
(
name,
fld.instance(),
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dt,
patchFieldType
);
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,8 +26,8 @@ License
\*---------------------------------------------------------------------------*/
#ifndef GeometricFieldReuseFunctions_H
#define GeometricFieldReuseFunctions_H
#ifndef Foam_GeometricFieldReuseFunctions_H
#define Foam_GeometricFieldReuseFunctions_H
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -36,46 +36,46 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Test if a tmp GeometricField is reusable, which means that it has been
//- allocated (not a const-reference).
//
// When GeometricField debug is on, also checks that the patches have
// reusable types.
template
<
class Type,
template<class> class PatchField,
class GeoMesh
>
bool reusable(const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf)
namespace Detail
{
if (tgf.isTmp())
{
if (GeometricField<Type, PatchField, GeoMesh>::debug)
{
for (const auto& p : tgf().boundaryField())
{
if
(
!polyPatch::constraintType(p.patch().type())
&& !isA<typename PatchField<Type>::Calculated>(p)
)
{
WarningInFunction
<< "Attempt to reuse temporary with non-reusable BC "
<< p.type() << endl;
return false;
}
//- True if tmp GeometricField has a reusable pointer.
//
// With GeometricField::debug on, check if patches have reusable types
template<class Type, template<class> class PatchField, class GeoMesh>
inline bool reusable
(
const tmp<GeometricField<Type, PatchField, GeoMesh>>& tfld
)
{
const bool ok = tfld.movable();
if (ok && GeometricField<Type, PatchField, GeoMesh>::debug)
{
for (const auto& p : tfld().boundaryField())
{
if
(
!polyPatch::constraintType(p.patch().type())
&& !isA<typename PatchField<Type>::Calculated>(p)
)
{
WarningInFunction
<< "Attempt to reuse temporary with non-reusable BC "
<< p.type() << endl;
return false;
}
}
return true;
}
return false;
return ok;
}
} // End namespace Detail
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template
<
@ -113,8 +113,8 @@ struct reuseTmpGeometricField
template<class TypeR, template<class> class PatchField, class GeoMesh>
struct reuseTmpGeometricField<TypeR, TypeR, PatchField, GeoMesh>
{
//- Since the input and output types are identical, also allow
//- optional copy assignment of the initial content.
//- Allow optional copy assignment of the initial content
//- for identical input and output types
static tmp<GeometricField<TypeR, PatchField, GeoMesh>> New
(
const tmp<GeometricField<TypeR, PatchField, GeoMesh>>& tgf1,
@ -123,7 +123,7 @@ struct reuseTmpGeometricField<TypeR, TypeR, PatchField, GeoMesh>
const bool initCopy = false
)
{
if (reusable(tgf1))
if (Detail::reusable(tgf1))
{
auto& gf1 = tgf1.constCast();
@ -236,7 +236,7 @@ struct reuseTmpTmpGeometricField
const dimensionSet& dimensions
)
{
if (reusable(tgf2))
if (Detail::reusable(tgf2))
{
auto& gf2 = tgf2.constCast();
@ -282,7 +282,7 @@ struct reuseTmpTmpGeometricField
const dimensionSet& dimensions
)
{
if (reusable(tgf1))
if (Detail::reusable(tgf1))
{
auto& gf1 = tgf1.constCast();
@ -322,7 +322,7 @@ struct reuseTmpTmpGeometricField
const dimensionSet& dimensions
)
{
if (reusable(tgf1))
if (Detail::reusable(tgf1))
{
auto& gf1 = tgf1.constCast();
@ -330,7 +330,7 @@ struct reuseTmpTmpGeometricField
gf1.dimensions().reset(dimensions);
return tgf1;
}
else if (reusable(tgf2))
if (Detail::reusable(tgf2))
{
auto& gf2 = tgf2.constCast();