From c242c1762a178c5e78304d98e059a97c0d139d54 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 26 May 2016 22:59:08 +0100 Subject: [PATCH] ReadFields: Added functions to read selected fields and store in the objectRegistry --- src/OpenFOAM/fields/ReadFields/ReadFields.C | 155 ++++++++++++++++++-- src/OpenFOAM/fields/ReadFields/ReadFields.H | 47 +++++- 2 files changed, 185 insertions(+), 17 deletions(-) diff --git a/src/OpenFOAM/fields/ReadFields/ReadFields.C b/src/OpenFOAM/fields/ReadFields/ReadFields.C index a76ca79a8e..0d75d94394 100644 --- a/src/OpenFOAM/fields/ReadFields/ReadFields.C +++ b/src/OpenFOAM/fields/ReadFields/ReadFields.C @@ -24,9 +24,8 @@ License \*---------------------------------------------------------------------------*/ #include "ReadFields.H" -#include "HashSet.H" -#include "Pstream.H" #include "IOobjectList.H" +#include "objectRegistry.H" // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // @@ -141,8 +140,6 @@ void Foam::ReadFields } } - //Info<< "Unloading times " << unusedTimes << endl; - forAll(unusedTimes, i) { objectRegistry& timeCache = const_cast @@ -162,8 +159,6 @@ void Foam::ReadFields // Create if not found if (!fieldsCache.found(tm)) { - //Info<< "Creating registry for time " << tm << endl; - // Create objectRegistry if not found objectRegistry* timeCachePtr = new objectRegistry ( @@ -189,9 +184,6 @@ void Foam::ReadFields // Store field if not found if (!timeCache.found(fieldName)) { - //Info<< "Loading field " << fieldName - // << " for time " << tm << endl; - GeoField loadedFld ( IOobject @@ -247,4 +239,149 @@ void Foam::ReadFields } +template +void Foam::readFields +( + const typename GeoFieldType::Mesh& mesh, + const IOobjectList& objects, + const HashSet& selectedFields, + LIFOStack& storedObjects +) +{ + IOobjectList fields(objects.lookupClass(GeoFieldType::typeName)); + if (!fields.size()) return; + + bool firstField = true; + + forAllConstIter(IOobjectList, fields, fieldIter) + { + const IOobject& io = *fieldIter(); + const word& fieldName = io.name(); + + if (selectedFields.found(fieldName)) + { + if (firstField) + { + Info<< " " << GeoFieldType::typeName << "s:"; + firstField = false; + } + + Info<< " " << fieldName; + + GeoFieldType* fieldPtr = new GeoFieldType + ( + IOobject + ( + fieldName, + io.instance(), + io.local(), + io.db(), + IOobject::MUST_READ, + IOobject::NO_WRITE + ), + mesh + ); + fieldPtr->store(); + storedObjects.push(fieldPtr); + } + } + + if (!firstField) + { + Info<< endl; + } +} + + +template +void Foam::readUniformFields +( + const IOobjectList& objects, + const HashSet& selectedFields, + LIFOStack& storedObjects, + const bool syncPar +) +{ + // Search list of objects for wanted type + IOobjectList fields(objects.lookupClass(UniformFieldType::typeName)); + if (!fields.size()) return; + + wordList masterNames(fields.names()); + + if (syncPar && Pstream::parRun()) + { + // Check that I have the same fields as the master + const wordList localNames(masterNames); + Pstream::scatter(masterNames); + + HashSet localNamesSet(localNames); + + forAll(masterNames, i) + { + const word& masterFld = masterNames[i]; + + HashSet::iterator iter = localNamesSet.find(masterFld); + + if (iter == localNamesSet.end()) + { + FatalErrorInFunction + << "Fields not synchronised across processors." << endl + << "Master has fields " << masterNames + << " processor " << Pstream::myProcNo() + << " has fields " << localNames << exit(FatalError); + } + else + { + localNamesSet.erase(iter); + } + } + + forAllConstIter(HashSet, localNamesSet, iter) + { + FatalErrorInFunction + << "Fields not synchronised across processors." << endl + << "Master has fields " << masterNames + << " processor " << Pstream::myProcNo() + << " has fields " << localNames << exit(FatalError); + } + } + + bool firstField = true; + + forAll(masterNames, i) + { + const IOobject& io = *fields[masterNames[i]]; + const word& fieldName = io.name(); + + if (selectedFields.found(fieldName)) + { + if (firstField) + { + Info<< " " << UniformFieldType::typeName << "s:"; + firstField = false; + } + + Info<< " " << fieldName; + + UniformFieldType* fieldPtr = new UniformFieldType + ( + IOobject + ( + fieldName, + io.instance(), + io.local(), + io.db(), + IOobject::MUST_READ, + IOobject::NO_WRITE + ) + ); + fieldPtr->store(); + storedObjects.push(fieldPtr); + } + } + + Info<< endl; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/fields/ReadFields/ReadFields.H b/src/OpenFOAM/fields/ReadFields/ReadFields.H index eb246701bd..bb464c8ba5 100644 --- a/src/OpenFOAM/fields/ReadFields/ReadFields.H +++ b/src/OpenFOAM/fields/ReadFields/ReadFields.H @@ -25,7 +25,7 @@ Global Foam::ReadFields Description - Helper routine to read fields + Field reading functions for post-processing utilities SourceFiles ReadFields.C @@ -37,17 +37,20 @@ SourceFiles #include "PtrList.H" #include "wordList.H" +#include "HashSet.H" +#include "LIFOStack.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +class regIOobject; class IOobjectList; -//- Helper routine to read fields -// Reads all fields of type. Returns names of fields read. Guarantees all -// processors to read fields in same order. +//- Read all fields of the specified type. +// Returns names of fields read. +// Guarantees all processors read fields in same order. template wordList ReadFields ( @@ -57,8 +60,8 @@ wordList ReadFields const bool syncPar = true ); -//- Helper routine to read GeometricFields. The fieldsCache is per time -// an objectRegistry of all stored fields +//- Read all GeometricFields of the specified type. +// The fieldsCache is an objectRegistry of all stored fields template static void ReadFields ( @@ -68,8 +71,8 @@ static void ReadFields objectRegistry& fieldsCache ); -//- Helper routine to read GeometricFields. The fieldsCache is per time -// an objectRegistry of all stored fields +//- Read all GeometricFields of the specified type. +// The fieldsCache is an objectRegistry of all stored fields template static void ReadFields ( @@ -79,6 +82,34 @@ static void ReadFields const word& registryName = "fieldsCache" ); +//- Read the selected GeometricFields of the specified type. +// The fields are transferred to the objectRegistry and a list of them is +// returned as a stack for later clean-up +template +void readFields +( + const typename GeoFieldType::Mesh& mesh, + const IOobjectList& objects, + const HashSet& selectedFields, + LIFOStack& storedObjects +); + + +//- Read the selected UniformDimensionedFields of the specified type. +// The fields are transferred to the objectRegistry and a list of them is +// returned as a stack for later clean-up +template +void readUniformFields +( + const IOobjectList& objects, + const HashSet& selectedFields, + LIFOStack& storedObjects, + const bool syncPar = true +); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //