ENH: use DynamicList for handling stored objects (ReadFields)

- DynamicList can be used as a LIFO with fewer allocations than a
  linked-list would have.

- support generic name matcher for readFields()
This commit is contained in:
Mark Olesen
2023-07-28 19:37:51 +02:00
parent 2fc2d1f95f
commit 0eb4354ee0
3 changed files with 148 additions and 49 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2018-2021 OpenCFD Ltd. Copyright (C) 2018-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -49,16 +49,6 @@ using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define ReadFields(GeoFieldType) \
readFields<GeoFieldType>(mesh, objects, selectedFields, storedObjects);
#define ReadPointFields(GeoFieldType) \
readFields<GeoFieldType>(pMesh, objects, selectedFields, storedObjects);
#define ReadUniformFields(FieldType) \
readUniformFields<FieldType> \
(constantObjects, selectedFields, storedObjects);
void executeFunctionObjects void executeFunctionObjects
( (
const argList& args, const argList& args,
@ -71,13 +61,24 @@ void executeFunctionObjects
{ {
Info<< nl << "Reading fields:" << endl; Info<< nl << "Reading fields:" << endl;
// Maintain a stack of the stored objects to clear after executing // Read objects in constant directory
// the functionObjects IOobjectList constObjects(mesh, runTime.constant());
LIFOStack<regIOobject*> storedObjects;
// Read objects in time directory // Read objects in time directory
IOobjectList objects(mesh, runTime.timeName()); IOobjectList objects(mesh, runTime.timeName());
// List of stored objects to clear after executing functionObjects
DynamicList<regIOobject*> storedObjects
(
objects.size() + constObjects.size()
);
// Read GeometricFields
#undef ReadFields
#define ReadFields(FieldType) \
readFields<FieldType>(mesh, objects, selectedFields, storedObjects);
// Read volFields // Read volFields
ReadFields(volScalarField); ReadFields(volScalarField);
ReadFields(volVectorField); ReadFields(volVectorField);
@ -99,8 +100,12 @@ void executeFunctionObjects
ReadFields(surfaceSymmTensorField); ReadFields(surfaceSymmTensorField);
ReadFields(surfaceTensorField); ReadFields(surfaceTensorField);
// Read point fields. // Read point fields.
const pointMesh& pMesh = pointMesh::New(mesh); const pointMesh& pMesh = pointMesh::New(mesh);
#undef ReadPointFields
#define ReadPointFields(FieldType) \
readFields<FieldType>(pMesh, objects, selectedFields, storedObjects);
ReadPointFields(pointScalarField) ReadPointFields(pointScalarField)
ReadPointFields(pointVectorField); ReadPointFields(pointVectorField);
@ -108,8 +113,12 @@ void executeFunctionObjects
ReadPointFields(pointSymmTensorField); ReadPointFields(pointSymmTensorField);
ReadPointFields(pointTensorField); ReadPointFields(pointTensorField);
// Read uniform dimensioned fields // Read uniform dimensioned fields
IOobjectList constantObjects(mesh, runTime.constant());
#undef ReadUniformFields
#define ReadUniformFields(FieldType) \
readUniformFields<FieldType>(constObjects, selectedFields, storedObjects);
ReadUniformFields(uniformDimensionedScalarField); ReadUniformFields(uniformDimensionedScalarField);
ReadUniformFields(uniformDimensionedVectorField); ReadUniformFields(uniformDimensionedVectorField);
@ -130,7 +139,8 @@ void executeFunctionObjects
while (!storedObjects.empty()) while (!storedObjects.empty())
{ {
storedObjects.pop()->checkOut(); storedObjects.back()->checkOut();
storedObjects.pop_back();
} }
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd. Copyright (C) 2016-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,11 +36,12 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef ReadFields_H #ifndef Foam_ReadFields_H
#define ReadFields_H #define Foam_ReadFields_H
#include "PtrList.H" #include "PtrList.H"
#include "wordList.H" #include "wordList.H"
#include "DynamicList.H"
#include "GeometricField.H" #include "GeometricField.H"
#include "HashSet.H" #include "HashSet.H"
#include "LIFOStack.H" #include "LIFOStack.H"
@ -113,27 +114,63 @@ static void ReadFields
const word& registryName = "fieldsCache" const word& registryName = "fieldsCache"
); );
//- Read the selected GeometricFields of the templated type.
// The fields are transferred to the objectRegistry and a list of them is //- Read the selected GeometricFields of the templated type
// returned as a stack for later cleanup //- and store on the objectRegistry.
template<class GeoFieldType> // Returns a list of field pointers for later cleanup
template<class GeoFieldType, class NameMatchPredicate>
void readFields void readFields
( (
const typename GeoFieldType::Mesh& mesh, const typename GeoFieldType::Mesh& mesh,
const IOobjectList& objects, const IOobjectList& objects,
const wordHashSet& selectedFields, //! Restrict to fields with matching names
const NameMatchPredicate& selectedFields,
//! [out] List of field pointers for later cleanup
DynamicList<regIOobject*>& storedObjects
);
//- Read the selected UniformDimensionedFields of the templated type
//- and store on the objectRegistry.
// Returns a list of field pointers for later cleanup
template<class UniformFieldType, class NameMatchPredicate>
void readUniformFields
(
const IOobjectList& objects,
//! Restrict to fields with matching names
const NameMatchPredicate& selectedFields,
//! [out] List of field pointers for later cleanup
DynamicList<regIOobject*>& storedObjects,
const bool syncPar = true
);
// Housekeeping
//- Read the selected GeometricFields of the templated type
//- and store on the objectRegistry.
// \deprecated(2023-07) - prefer the DynamicList version
// Returns a stack of field pointers for later cleanup
template<class GeoFieldType, class NameMatchPredicate>
FOAM_DEPRECATED_FOR(2023-07, "DynamicList version")
void readFields
(
const typename GeoFieldType::Mesh& mesh,
const IOobjectList& objects,
const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects LIFOStack<regIOobject*>& storedObjects
); );
//- Read the selected UniformDimensionedFields of the templated type. //- Read the selected UniformDimensionedFields of the templated type
// The fields are transferred to the objectRegistry and a list of them is //- and store on the objectRegistry.
// returned as a stack for later cleanup // \deprecated(2023-07) - prefer the DynamicList version
template<class GeoFieldType> // Returns a stack of field pointers for later cleanup
template<class UniformFieldType, class NameMatchPredicate>
FOAM_DEPRECATED_FOR(2023-07, "DynamicList version")
void readUniformFields void readUniformFields
( (
const IOobjectList& objects, const IOobjectList& objects,
const wordHashSet& selectedFields, const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects, LIFOStack<regIOobject*>& storedObjects,
const bool syncPar = true const bool syncPar = true
); );

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -309,25 +309,25 @@ void Foam::ReadFields
} }
template<class GeoFieldType> template<class GeoFieldType, class NameMatchPredicate>
void Foam::readFields void Foam::readFields
( (
const typename GeoFieldType::Mesh& mesh, const typename GeoFieldType::Mesh& mesh,
const IOobjectList& objects, const IOobjectList& objects,
const wordHashSet& selectedFields, const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects DynamicList<regIOobject*>& storedObjects
) )
{ {
// Names of GeoField objects, sorted order. Not synchronised. // Names of GeoField objects, sorted order. Not synchronised.
const wordList fieldNames const wordList fieldNames
( (
objects.sortedNames objects.sortedNames<GeoFieldType>(selectedFields)
(
GeoFieldType::typeName,
selectedFields // Only permit these
)
); );
// pre-extend reserve
storedObjects.reserve(storedObjects.size() + fieldNames.size());
label nFields = 0; label nFields = 0;
for (const word& fieldName : fieldNames) for (const word& fieldName : fieldNames)
@ -355,7 +355,7 @@ void Foam::readFields
mesh mesh
); );
fieldPtr->store(); fieldPtr->store();
storedObjects.push(fieldPtr); storedObjects.push_back(fieldPtr);
++nFields; ++nFields;
} }
@ -364,26 +364,24 @@ void Foam::readFields
} }
template<class UniformFieldType> template<class UniformFieldType, class NameMatchPredicate>
void Foam::readUniformFields void Foam::readUniformFields
( (
const IOobjectList& objects, const IOobjectList& objects,
const wordHashSet& selectedFields, const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects, DynamicList<regIOobject*>& storedObjects,
const bool syncPar const bool syncPar
) )
{ {
// Names of UniformField objects, sorted order. // Names of UniformField objects, sorted order.
const wordList fieldNames const wordList fieldNames
( (
objects.names objects.names<UniformFieldType>(selectedFields, syncPar)
(
UniformFieldType::typeName,
selectedFields, // Only permit these
syncPar
)
); );
// pre-extend reserve
storedObjects.reserve(storedObjects.size() + fieldNames.size());
label nFields = 0; label nFields = 0;
for (const word& fieldName : fieldNames) for (const word& fieldName : fieldNames)
@ -410,7 +408,7 @@ void Foam::readUniformFields
) )
); );
fieldPtr->store(); fieldPtr->store();
storedObjects.push(fieldPtr); storedObjects.push_back(fieldPtr);
++nFields; ++nFields;
} }
@ -419,4 +417,58 @@ void Foam::readUniformFields
} }
template<class GeoFieldType, class NameMatchPredicate>
void Foam::readFields
(
const typename GeoFieldType::Mesh& mesh,
const IOobjectList& objects,
const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects
)
{
DynamicList<regIOobject*> newObjects;
readFields<GeoFieldType, NameMatchPredicate>
(
mesh,
objects,
selectedFields,
newObjects
);
// Transcribe from list to stack
for (regIOobject* fieldPtr : newObjects)
{
storedObjects.push(fieldPtr);
}
}
template<class UniformFieldType, class NameMatchPredicate>
void Foam::readUniformFields
(
const IOobjectList& objects,
const NameMatchPredicate& selectedFields,
LIFOStack<regIOobject*>& storedObjects,
const bool syncPar
)
{
DynamicList<regIOobject*> newObjects;
readUniformFields<UniformFieldType, NameMatchPredicate>
(
objects,
selectedFields,
newObjects,
syncPar
);
// Transcribe from list to stack
for (regIOobject* fieldPtr : newObjects)
{
storedObjects.push(fieldPtr);
}
}
// ************************************************************************* // // ************************************************************************* //