ENH: allow wordHashSet filter for IOobjectList::names

- simplifies usage.
  Support syncPar check on names() to detect inconsistencies.

- simplify readFields, ReadFields and other routines by using these
  new methods.
This commit is contained in:
Mark Olesen
2018-07-26 14:56:52 +02:00
parent a8ef5610d0
commit 02ad76df4f
51 changed files with 1434 additions and 1397 deletions

View File

@ -109,51 +109,49 @@ int main(int argc, char *argv[])
IOobjectList objects(mesh, runTime.timeName());
// Converting volScalarField
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Search list of objects for volScalarFields
IOobjectList scalarFields(objects.lookupClass("volScalarField"));
forAllIter(IOobjectList, scalarFields, iter)
// volScalarField
for
(
const word& fieldName
: objects.sortedNames(volScalarField::typeName)
)
{
// Read field
volScalarField field(*iter(), mesh);
// lookup field from dictionary and convert field
// Lookup field from dictionary and convert field
label unitNumber;
if
(
foamDataToFluentDict.readIfPresent(field.name(), unitNumber)
foamDataToFluentDict.readIfPresent(fieldName, unitNumber)
&& unitNumber > 0
)
{
Info<< " Converting field " << field.name() << endl;
// Read field
volScalarField field(*(objects[fieldName]), mesh);
Info<< " Converting field " << fieldName << nl;
writeFluentField(field, unitNumber, fluentDataFile);
}
}
// Converting volVectorField
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Search list of objects for volVectorFields
IOobjectList vectorFields(objects.lookupClass("volVectorField"));
forAllIter(IOobjectList, vectorFields, iter)
// volVectorField
for
(
const word& fieldName
: objects.sortedNames(volVectorField::typeName)
)
{
// Read field
volVectorField field(*iter(), mesh);
// lookup field from dictionary and convert field
// Lookup field from dictionary and convert field
label unitNumber;
if
(
foamDataToFluentDict.readIfPresent(field.name(), unitNumber)
foamDataToFluentDict.readIfPresent(fieldName, unitNumber)
&& unitNumber > 0
)
{
Info<< " Converting field " << field.name() << endl;
// Read field
volVectorField field(*(objects[fieldName]), mesh);
Info<< " Converting field " << fieldName << nl;
writeFluentField(field, unitNumber, fluentDataFile);
}
}

View File

@ -316,7 +316,7 @@ int main(int argc, char *argv[])
// and doesn't cost much) and simultaneously remove all
// "*_0" restart fields
for (auto fieldType : volFieldTypes)
for (const word& fieldType : volFieldTypes)
{
usableObjects
(
@ -360,7 +360,7 @@ int main(int argc, char *argv[])
// ~~~~~~~~~~~~~~~~~~~~~~
Info<< "Write volume field (";
for (auto fieldType : volFieldTypes)
for (const word& fieldType : volFieldTypes)
{
// For convenience, just force each field-type into existence.
// This simplifies code logic and doesn't cost much at all.

View File

@ -13,7 +13,7 @@ if (timeDirs.size())
IOobjectList objs(mesh, lastTimeName);
forAllConstIter(IOobjectList, objs, fieldIter)
forAllConstIters(objs, fieldIter)
{
const IOobject& obj = *fieldIter();
const word& fieldName = obj.name();

View File

@ -49,13 +49,13 @@ Description
int main(int argc, char *argv[])
{
const label nTypes = 4;
const word fieldTypes[] =
{
const wordList fieldTypes
({
"volScalarField",
"volVectorField",
"surfaceScalarField",
cloud::prefix
};
});
#include "setRootCase.H"

View File

@ -1,16 +1,14 @@
for (label i=0; i < nTypes; i++)
for (const word& fieldType : fieldTypes)
{
wordList fieldNames = objects.names(fieldTypes[i]);
const wordList fieldNames = objects.sortedNames(fieldType);
if (fieldTypes[i] == "volScalarField")
if (fieldType == "volScalarField")
{
gmvFile << "variable" << nl;
}
forAll(fieldNames, j)
for (const word& fieldName : fieldNames)
{
const word& fieldName = fieldNames[j];
IOobject fieldObject
(
fieldName,
@ -20,18 +18,18 @@ for (label i=0; i < nTypes; i++)
IOobject::NO_WRITE
);
if (fieldTypes[i] == "volScalarField")
if (fieldType == "volScalarField")
{
volScalarField fld(fieldObject, mesh);
gmvFile << fieldName << " 0" << nl;
for (label indx=0;indx<mesh.nCells();indx++)
for (label indx=0; indx<mesh.nCells(); ++indx)
{
gmvFile << fld[indx] << " ";
}
gmvFile << nl;
}
if (fieldTypes[i] == "volVectorField")
if (fieldType == "volVectorField")
{
if (fieldName == vComp)
{
@ -53,14 +51,14 @@ for (label i=0; i < nTypes; i++)
}
}
if (fieldTypes[i] == "surfaceScalarField")
if (fieldType == "surfaceScalarField")
{
// ...
}
}
if (fieldTypes[i] == cloud::prefix)
if (fieldType == cloud::prefix)
{
IOobject positionsHeader
(
@ -96,8 +94,8 @@ for (label i=0; i < nTypes; i++)
IOobjectList objects(mesh, runTime.timeName(), cloud::prefix);
wordList lagrangianScalarNames = objects.names("scalarField");
wordList lagrangianVectorNames = objects.names("vectorField");
wordList lagrangianScalarNames(objects.sortedNames("scalarField"));
wordList lagrangianVectorNames(objects.sortedNames("vectorField"));
if (particles.size())
{
@ -106,7 +104,7 @@ for (label i=0; i < nTypes; i++)
}
}
if (fieldTypes[i] == "volScalarField")
if (fieldType == "volScalarField")
{
gmvFile << "endvars" << nl;
}

View File

@ -17,10 +17,8 @@ forAllConstIter(Cloud<passiveParticle>, particles, iter)
}
gmvFile << nl;
forAll(lagrangianScalarNames, i)
for (const word& name : lagrangianScalarNames)
{
const word& name = lagrangianScalarNames[i];
IOField<scalar> fld
(
IOobject
@ -48,10 +46,8 @@ forAll(lagrangianScalarNames, i)
}
forAll(lagrangianVectorNames, i)
for (const word& name : lagrangianVectorNames)
{
const word& name = lagrangianVectorNames[i];
IOField<vector> fld
(
IOobject

View File

@ -62,7 +62,7 @@ void ReadAndMapFields
tetFields.setSize(fieldObjects.size());
label i = 0;
forAllConstIter(IOobjectList, fieldObjects, iter)
forAllConstIters(fieldObjects, iter)
{
Info<< "Converting " << ReadGeoField::typeName << ' ' << iter.key()
<< endl;
@ -138,8 +138,6 @@ void ReadAndMapFields
}
int main(int argc, char *argv[])
{
#include "addOverwriteOption.H"
@ -152,7 +150,6 @@ int main(int argc, char *argv[])
#include "checkTimeOptions.H"
runTime.setTime(Times[startTime], startTime);
// Read the mesh
#include "createNamedMesh.H"

View File

@ -24,7 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "readFields.H"
#include "IOobjectList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -43,37 +42,37 @@ label readFields
PtrList<const GeoField>& fields
)
{
label nFields = 0;
// Available fields of type GeomField
const wordList fieldNames = objects.sortedNames(GeoField::typeName);
fields.setSize(fieldNames.size());
// Available fields of type GeoField, sorted order
const wordList fieldNames =
(
selectedFields.empty()
? objects.sortedNames(GeoField::typeName)
: objects.sortedNames(GeoField::typeName, selectedFields)
);
// Construct the fields
fields.resize(fieldNames.size());
label nFields = 0;
for (const word& fieldName : fieldNames)
{
if (selectedFields.empty() || selectedFields.found(fieldName))
{
fields.set
fields.set
(
nFields++,
proxy.interpolate
(
nFields++,
proxy.interpolate
(
GeoField(*(objects[fieldName]), mesh)
).ptr()
);
}
GeoField(*(objects[fieldName]), mesh)
).ptr()
);
}
fields.setSize(nFields);
return nFields;
}
template<class GeoField>
void readFields
label readFields
(
const typename GeoField::Mesh& mesh,
const IOobjectList& objects,
@ -81,26 +80,29 @@ void readFields
PtrList<const GeoField>& fields
)
{
// Search list of objects for fields of type GeomField
IOobjectList fieldObjects(objects.lookupClass(GeoField::typeName));
// Available fields of type GeoField, sorted order
const wordList fieldNames =
(
selectedFields.empty()
? objects.sortedNames(GeoField::typeName)
: objects.sortedNames(GeoField::typeName, selectedFields)
);
// Construct the fields
fields.setSize(fieldObjects.size());
fields.resize(fieldNames.size());
label nFields = 0;
forAllIters(fieldObjects, iter)
for (const word& fieldName : fieldNames)
{
if (selectedFields.empty() || selectedFields.found(iter()->name()))
{
fields.set
(
nFields++,
new GeoField(*iter(), mesh)
);
}
fields.set
(
nFields++,
new GeoField(*(objects[fieldName]), mesh)
);
}
fields.setSize(nFields);
return nFields;
}

View File

@ -35,16 +35,16 @@ SourceFiles
#define readFields_H
#include "fvMeshSubsetProxy.H"
#include "HashSet.H"
#include "PtrList.H"
#include "IOobjectList.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Read the fields, optionally subset, and place on the pointer list
//- Read the fields, optionally subset, and place on the pointer list
template<class GeoField>
label readFields
(
@ -55,6 +55,18 @@ label readFields
PtrList<const GeoField>& fields
);
//- Read the fields, and place on the pointer list
template<class GeoField>
label readFields
(
const typename GeoField::Mesh& mesh,
const IOobjectList& objects,
const wordHashSet& selectedFields,
PtrList<const GeoField>& fields
);
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //