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
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -194,7 +194,6 @@ void Foam::vtkPVFoam::convertAreaFields()
// Get objects (fields) for this time - only keep selected fields
// the region name is already in the mesh db
IOobjectList objects(mesh.mesh(), dbPtr_().timeName());
objects.filterKeys(selectedFields);
if (objects.empty())

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -53,7 +53,7 @@ class fieldInterpolator
Time& runTime_;
const fvMesh& mesh_;
const IOobjectList& objects_;
const wordHashSet& selectedFields_;
const wordRes& selectedFields_;
instant ti_;
instant ti1_;
const interpolationWeights& interpolator_;
@ -67,7 +67,7 @@ public:
Time& runTime,
const fvMesh& mesh,
const IOobjectList& objects,
const wordHashSet& selectedFields,
const wordRes& selectedFields,
const instant& ti,
const instant& ti1,
const interpolationWeights& interpolator,
@ -94,95 +94,93 @@ public:
template<class GeoFieldType>
void fieldInterpolator::interpolate()
{
const word& fieldClassName = GeoFieldType::typeName;
const word& clsName = GeoFieldType::typeName;
IOobjectList fields = objects_.lookupClass(fieldClassName);
const wordList fieldNames =
(
selectedFields_.empty()
? objects_.sortedNames(clsName)
: objects_.sortedNames(clsName, selectedFields_)
);
if (fields.size())
if (fieldNames.size())
{
Info<< " " << fieldClassName << "s:";
Info<< " " << clsName << 's';
}
forAllConstIter(IOobjectList, fields, fieldIter)
for (const word& fieldName : fieldNames)
{
Info<< ' ' << fieldName << '(';
const scalar deltaT = (ti1_.value() - ti_.value())/(divisions_ + 1);
for (int j=0; j<divisions_; j++)
{
if
(
selectedFields_.empty()
|| selectedFields_.found(fieldIter()->name())
)
instant timej = instant(ti_.value() + (j + 1)*deltaT);
runTime_.setTime(instant(timej.name()), 0);
Info<< timej.name();
if (j < divisions_-1)
{
Info<< " " << fieldIter()->name() << '(';
scalar deltaT = (ti1_.value() - ti_.value())/(divisions_ + 1);
for (int j=0; j<divisions_; j++)
{
instant timej = instant(ti_.value() + (j + 1)*deltaT);
runTime_.setTime(instant(timej.name()), 0);
Info<< timej.name();
if (j < divisions_-1)
{
Info<< " ";
}
// Calculate times to read and weights
labelList indices;
scalarField weights;
interpolator_.valueWeights
(
runTime_.value(),
indices,
weights
);
const wordList selectedTimeNames
(
UIndirectList<word>(timeNames_, indices)()
);
//Info<< "For time " << runTime_.value()
// << " need times " << selectedTimeNames
// << " need weights " << weights << endl;
// Read on the objectRegistry all the required fields
ReadFields<GeoFieldType>
(
fieldIter()->name(),
mesh_,
selectedTimeNames
);
GeoFieldType fieldj
(
uniformInterpolate<GeoFieldType>
(
IOobject
(
fieldIter()->name(),
runTime_.timeName(),
fieldIter()->db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
),
fieldIter()->name(),
selectedTimeNames,
weights
)
);
fieldj.write();
}
Info<< ')';
Info<< " ";
}
// Calculate times to read and weights
labelList indices;
scalarField weights;
interpolator_.valueWeights
(
runTime_.value(),
indices,
weights
);
const wordList selectedTimeNames
(
UIndirectList<word>(timeNames_, indices)()
);
//Info<< "For time " << runTime_.value()
// << " need times " << selectedTimeNames
// << " need weights " << weights << endl;
// Read on the objectRegistry all the required fields
ReadFields<GeoFieldType>
(
fieldName,
mesh_,
selectedTimeNames
);
GeoFieldType fieldj
(
uniformInterpolate<GeoFieldType>
(
IOobject
(
fieldName,
runTime_.timeName(),
objects_[fieldName]->db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
false
),
fieldName,
selectedTimeNames,
weights
)
);
fieldj.write();
}
Info<< endl;
Info<< ')';
}
if (fieldNames.size()) Info<< endl;
}
@ -195,9 +193,9 @@ int main(int argc, char *argv[])
argList::addOption
(
"fields",
"list",
"specify a list of fields to be interpolated. Eg, '(U T p)' - "
"regular expressions not currently supported"
"wordRes",
"the fields (or field) to be interpolated."
" Eg, '(U T p \"Y.*\")' or a single field 'U'"
);
argList::addOption
(
@ -216,24 +214,22 @@ int main(int argc, char *argv[])
#include "createTime.H"
runTime.functionObjects().off();
wordHashSet selectedFields;
args.readIfPresent("fields", selectedFields);
wordRes selectedFields;
args.readListIfPresent<wordRe>("fields", selectedFields);
if (selectedFields.size())
if (selectedFields.empty())
{
Info<< "Interpolating fields " << selectedFields << nl << endl;
Info<< "Interpolating all fields" << nl << endl;
}
else
{
Info<< "Interpolating all fields" << nl << endl;
Info<< "Interpolating fields " << flatOutput(selectedFields)
<< nl << endl;
}
int divisions = 1;
if (args.found("divisions"))
{
args.lookup("divisions")() >> divisions;
}
args.readIfPresent("divisions", divisions);
Info<< "Using " << divisions << " per time interval" << nl << endl;