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

View File

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

View File

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