Commit d6e721a9 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: simplify subsetMesh using new IOobjectList methods

parent aeae8e70
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -112,7 +112,7 @@ labelList nearestPatch(const polyMesh& mesh, const labelList& patchIDs)
{
WarningInFunction
<< "Did not visit some faces, e.g. face " << faceI
<< " at " << mesh.faceCentres()[faceI] << endl
<< " at " << mesh.faceCentres()[faceI] << nl
<< "Using patch " << patchIDs[0] << " as nearest"
<< endl;
haveWarned = true;
......@@ -129,57 +129,38 @@ labelList nearestPatch(const polyMesh& mesh, const labelList& patchIDs)
}
template<class Type>
void subsetVolFields
//
// Subset field-type, availability information cached
// in the availableFields hashtable.
//
template<class Type, template<class> class PatchField, class GeoMesh>
void subsetFields
(
const fvMeshSubset& subsetter,
const wordList& fieldNames,
PtrList<GeometricField<Type, fvPatchField, volMesh>>& subFields
HashTable<wordHashSet>& availableFields,
PtrList<GeometricField<Type, PatchField, GeoMesh>>& subFields
)
{
const fvMesh& baseMesh = subsetter.baseMesh();
forAll(fieldNames, i)
{
const word& fieldName = fieldNames[i];
Info<< "Subsetting field " << fieldName << endl;
GeometricField<Type, fvPatchField, volMesh> fld
(
IOobject
(
fieldName,
baseMesh.time().timeName(),
baseMesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
baseMesh
);
typedef GeometricField<Type, PatchField, GeoMesh> FieldType;
const word fieldType = FieldType::typeName;
subFields.set(i, subsetter.interpolate(fld));
}
}
const wordList fieldNames = availableFields(fieldType).sortedToc();
subFields.setSize(fieldNames.size());
template<class Type>
void subsetSurfaceFields
(
const fvMeshSubset& subsetter,
const wordList& fieldNames,
PtrList<GeometricField<Type, fvsPatchField, surfaceMesh>>& subFields
)
{
const fvMesh& baseMesh = subsetter.baseMesh();
if (fieldNames.empty())
{
return;
}
Info<< "Subsetting " << fieldType << " (";
forAll(fieldNames, i)
{
const word& fieldName = fieldNames[i];
if (i) Info<< ' ';
Info<< fieldName;
Info<< "Subsetting field " << fieldName << endl;
GeometricField<Type, fvsPatchField, surfaceMesh> fld
FieldType fld
(
IOobject
(
......@@ -193,7 +174,11 @@ void subsetSurfaceFields
);
subFields.set(i, subsetter.interpolate(fld));
// Subsetting adds 'subset' prefix - rename to match original.
subFields[i].rename(fieldName);
}
Info<< ")" << nl;
}
......@@ -202,19 +187,30 @@ void subsetPointFields
(
const fvMeshSubset& subsetter,
const pointMesh& pMesh,
const wordList& fieldNames,
HashTable<wordHashSet>& availableFields,
PtrList<GeometricField<Type, pointPatchField, pointMesh>>& subFields
)
{
typedef GeometricField<Type, pointPatchField, pointMesh> FieldType;
const word fieldType = FieldType::typeName;
const wordList fieldNames = availableFields(fieldType).sortedToc();
subFields.setSize(fieldNames.size());
const fvMesh& baseMesh = subsetter.baseMesh();
if (fieldNames.empty())
{
return;
}
Info<< "Subsetting " << fieldType << " (";
forAll(fieldNames, i)
{
const word& fieldName = fieldNames[i];
if (i) Info<< ' ';
Info<< fieldName;
Info<< "Subsetting field " << fieldName << endl;
GeometricField<Type, pointPatchField, pointMesh> fld
FieldType fld
(
IOobject
(
......@@ -228,7 +224,11 @@ void subsetPointFields
);
subFields.set(i, subsetter.interpolate(fld));
// Subsetting adds 'subset' prefix - rename to match original.
subFields[i].rename(fieldName);
}
Info<< ")" << nl;
}
......@@ -236,19 +236,30 @@ template<class Type>
void subsetDimensionedFields
(
const fvMeshSubset& subsetter,
const wordList& fieldNames,
HashTable<wordHashSet>& availableFields,
PtrList<DimensionedField<Type, volMesh>>& subFields
)
{
typedef DimensionedField<Type, volMesh> FieldType;
const word fieldType = FieldType::typeName;
const wordList fieldNames = availableFields(fieldType).sortedToc();
subFields.setSize(fieldNames.size());
const fvMesh& baseMesh = subsetter.baseMesh();
if (fieldNames.empty())
{
return;
}
Info<< "Subsetting " << fieldType << " (";
forAll(fieldNames, i)
{
const word& fieldName = fieldNames[i];
if (i) Info<< ' ';
Info<< fieldName;
Info<< "Subsetting field " << fieldName << endl;
DimensionedField<Type, volMesh> fld
FieldType fld
(
IOobject
(
......@@ -262,7 +273,11 @@ void subsetDimensionedFields
);
subFields.set(i, subsetter.interpolate(fld));
// Subsetting adds 'subset' prefix - rename to match original.
subFields[i].rename(fieldName);
}
Info<< ")" << nl;
}
......@@ -289,7 +304,7 @@ void subsetTopoSets
// Map the data
PackedBoolList isSet(set.maxSize(mesh));
forAllConstIter(labelHashSet, set, iter)
forAllConstIters(set, iter)
{
isSet[iter.key()] = true;
}
......@@ -359,7 +374,6 @@ int main(int argc, char *argv[])
#include "createNamedMesh.H"
const word setName = args[1];
word meshInstance = mesh.pointsInstance();
......@@ -378,7 +392,7 @@ int main(int argc, char *argv[])
}
Info<< "Reading cell set from " << setName << endl << endl;
Info<< "Reading cell set from " << setName << nl << endl;
// Create mesh subsetting engine
fvMeshSubset subsetter(mesh);
......@@ -389,11 +403,7 @@ int main(int argc, char *argv[])
{
const word patchName = args["patch"];
exposedPatchIDs = labelList
(
1,
mesh.boundaryMesh().findPatchID(patchName)
);
exposedPatchIDs = { mesh.boundaryMesh().findPatchID(patchName) };
if (exposedPatchIDs[0] == -1)
{
......@@ -402,8 +412,8 @@ int main(int argc, char *argv[])
<< exit(FatalError);
}
Info<< "Adding exposed internal faces to patch " << patchName << endl
<< endl;
Info<< "Adding exposed internal faces to patch " << patchName
<< nl << endl;
}
else if (args.optionFound("patches"))
{
......@@ -412,14 +422,14 @@ int main(int argc, char *argv[])
exposedPatchIDs = mesh.boundaryMesh().patchSet(patchNames).sortedToc();
Info<< "Adding exposed internal faces to nearest of patches "
<< patchNames << endl << endl;
<< patchNames << nl << endl;
}
else
{
Info<< "Adding exposed internal faces to a patch called"
<< " \"oldInternalFaces\" (created if necessary)" << endl
<< endl;
exposedPatchIDs = labelList(1, label(-1));
exposedPatchIDs = { -1 };
}
......@@ -431,7 +441,6 @@ int main(int argc, char *argv[])
}
else
{
// Find per face the nearest patch
labelList nearestExposedPatch(nearestPatch(mesh, exposedPatchIDs));
......@@ -454,77 +463,45 @@ int main(int argc, char *argv[])
IOobjectList objects(mesh, runTime.timeName());
HashTable<wordHashSet> availableFields = objects.classes();
// Read vol fields and subset
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
wordList scalarNames(objects.names(volScalarField::typeName));
PtrList<volScalarField> scalarFlds(scalarNames.size());
subsetVolFields(subsetter, scalarNames, scalarFlds);
PtrList<volScalarField> scalarFlds;
subsetFields(subsetter, availableFields, scalarFlds);
wordList vectorNames(objects.names(volVectorField::typeName));
PtrList<volVectorField> vectorFlds(vectorNames.size());
subsetVolFields(subsetter, vectorNames, vectorFlds);
PtrList<volVectorField> vectorFlds;
subsetFields(subsetter, availableFields, vectorFlds);
wordList sphericalTensorNames
(
objects.names(volSphericalTensorField::typeName)
);
PtrList<volSphericalTensorField> sphericalTensorFlds
(
sphericalTensorNames.size()
);
subsetVolFields(subsetter, sphericalTensorNames, sphericalTensorFlds);
PtrList<volSphericalTensorField> sphTensorFlds;
subsetFields(subsetter, availableFields, sphTensorFlds);
wordList symmTensorNames(objects.names(volSymmTensorField::typeName));
PtrList<volSymmTensorField> symmTensorFlds(symmTensorNames.size());
subsetVolFields(subsetter, symmTensorNames, symmTensorFlds);
PtrList<volSymmTensorField> symmTensorFlds;
subsetFields(subsetter, availableFields, symmTensorFlds);
wordList tensorNames(objects.names(volTensorField::typeName));
PtrList<volTensorField> tensorFlds(tensorNames.size());
subsetVolFields(subsetter, tensorNames, tensorFlds);
PtrList<volTensorField> tensorFlds;
subsetFields(subsetter, availableFields, tensorFlds);
// Read surface fields and subset
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wordList surfScalarNames(objects.names(surfaceScalarField::typeName));
PtrList<surfaceScalarField> surfScalarFlds(surfScalarNames.size());
subsetSurfaceFields(subsetter, surfScalarNames, surfScalarFlds);
PtrList<surfaceScalarField> surfScalarFlds;
subsetFields(subsetter, availableFields, surfScalarFlds);
wordList surfVectorNames(objects.names(surfaceVectorField::typeName));
PtrList<surfaceVectorField> surfVectorFlds(surfVectorNames.size());
subsetSurfaceFields(subsetter, surfVectorNames, surfVectorFlds);
PtrList<surfaceVectorField> surfVectorFlds;
subsetFields(subsetter, availableFields, surfVectorFlds);
wordList surfSphericalTensorNames
(
objects.names(surfaceSphericalTensorField::typeName)
);
PtrList<surfaceSphericalTensorField> surfSphericalTensorFlds
(
surfSphericalTensorNames.size()
);
subsetSurfaceFields
(
subsetter,
surfSphericalTensorNames,
surfSphericalTensorFlds
);
PtrList<surfaceSphericalTensorField> surfSphTensorFlds;
subsetFields(subsetter, availableFields, surfSphTensorFlds);
wordList surfSymmTensorNames
(
objects.names(surfaceSymmTensorField::typeName)
);
PtrList<surfaceSymmTensorField> surfSymmTensorFlds
(
surfSymmTensorNames.size()
);
subsetSurfaceFields(subsetter, surfSymmTensorNames, surfSymmTensorFlds);
PtrList<surfaceSymmTensorField> surfSymmTensorFlds;
subsetFields(subsetter, availableFields, surfSymmTensorFlds);
wordList surfTensorNames(objects.names(surfaceTensorField::typeName));
PtrList<surfaceTensorField> surfTensorFlds(surfTensorNames.size());
subsetSurfaceFields(subsetter, surfTensorNames, surfTensorFlds);
PtrList<surfaceTensorField> surfTensorFlds;
subsetFields(subsetter, availableFields, surfTensorFlds);
// Read point fields and subset
......@@ -532,86 +509,39 @@ int main(int argc, char *argv[])
const pointMesh& pMesh = pointMesh::New(mesh);
wordList pointScalarNames(objects.names(pointScalarField::typeName));
PtrList<pointScalarField> pointScalarFlds(pointScalarNames.size());
subsetPointFields(subsetter, pMesh, pointScalarNames, pointScalarFlds);
PtrList<pointScalarField> pointScalarFlds;
subsetPointFields(subsetter, pMesh, availableFields, pointScalarFlds);
wordList pointVectorNames(objects.names(pointVectorField::typeName));
PtrList<pointVectorField> pointVectorFlds(pointVectorNames.size());
subsetPointFields(subsetter, pMesh, pointVectorNames, pointVectorFlds);
PtrList<pointVectorField> pointVectorFlds;
subsetPointFields(subsetter, pMesh, availableFields, pointVectorFlds);
wordList pointSphericalTensorNames
(
objects.names(pointSphericalTensorField::typeName)
);
PtrList<pointSphericalTensorField> pointSphericalTensorFlds
(
pointSphericalTensorNames.size()
);
subsetPointFields
(
subsetter,
pMesh,
pointSphericalTensorNames,
pointSphericalTensorFlds
);
PtrList<pointSphericalTensorField> pointSphTensorFlds;
subsetPointFields(subsetter, pMesh, availableFields, pointSphTensorFlds);
wordList pointSymmTensorNames
(
objects.names(pointSymmTensorField::typeName)
);
PtrList<pointSymmTensorField> pointSymmTensorFlds
(
pointSymmTensorNames.size()
);
subsetPointFields
(
subsetter,
pMesh,
pointSymmTensorNames,
pointSymmTensorFlds
);
PtrList<pointSymmTensorField> pointSymmTensorFlds;
subsetPointFields(subsetter, pMesh, availableFields, pointSymmTensorFlds);
wordList pointTensorNames(objects.names(pointTensorField::typeName));
PtrList<pointTensorField> pointTensorFlds(pointTensorNames.size());
subsetPointFields(subsetter, pMesh, pointTensorNames, pointTensorFlds);
PtrList<pointTensorField> pointTensorFlds;
subsetPointFields(subsetter, pMesh, availableFields, pointTensorFlds);
// Read dimensioned fields and subset
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef volScalarField::Internal dimScalType;
wordList scalarDimNames(objects.names(dimScalType::typeName));
PtrList<dimScalType> scalarDimFlds(scalarDimNames.size());
subsetDimensionedFields(subsetter, scalarDimNames, scalarDimFlds);
PtrList<volScalarField::Internal> scalarDimFlds;
subsetDimensionedFields(subsetter, availableFields, scalarDimFlds);
typedef volVectorField::Internal dimVecType;
wordList vectorDimNames(objects.names(dimVecType::typeName));
PtrList<dimVecType> vectorDimFlds(vectorDimNames.size());
subsetDimensionedFields(subsetter, vectorDimNames, vectorDimFlds);
PtrList<volVectorField::Internal> vectorDimFlds;
subsetDimensionedFields(subsetter, availableFields, vectorDimFlds);
typedef volSphericalTensorField::Internal dimSphereType;
wordList sphericalTensorDimNames(objects.names(dimSphereType::typeName));
PtrList<dimSphereType> sphericalTensorDimFlds
(
sphericalTensorDimNames.size()
);
subsetDimensionedFields
(
subsetter,
sphericalTensorDimNames,
sphericalTensorDimFlds
);
PtrList<volSphericalTensorField::Internal> sphTensorDimFlds;
subsetDimensionedFields(subsetter, availableFields, sphTensorDimFlds);
typedef volSymmTensorField::Internal dimSymmTensorType;
wordList symmTensorDimNames(objects.names(dimSymmTensorType::typeName));
PtrList<dimSymmTensorType> symmTensorDimFlds(symmTensorDimNames.size());
subsetDimensionedFields(subsetter, symmTensorDimNames, symmTensorDimFlds);
PtrList<volSymmTensorField::Internal> symmTensorDimFlds;
subsetDimensionedFields(subsetter, availableFields, symmTensorDimFlds);
typedef volTensorField::Internal dimTensorType;
wordList tensorDimNames(objects.names(dimTensorType::typeName));
PtrList<dimTensorType> tensorDimFlds(tensorDimNames.size());
subsetDimensionedFields(subsetter, tensorDimNames, tensorDimFlds);
PtrList<volTensorField::Internal> tensorDimFlds;
subsetDimensionedFields(subsetter, availableFields, tensorDimFlds);
// topoSets and subset
......@@ -620,6 +550,7 @@ int main(int argc, char *argv[])
PtrList<cellSet> cellSets;
PtrList<faceSet> faceSets;
PtrList<pointSet> pointSets;
{
IOobjectList objects(mesh, mesh.facesInstance(), "polyMesh/sets");
objects.remove(currentSet);
......@@ -650,7 +581,6 @@ int main(int argc, char *argv[])
}
// Write mesh and fields to new time
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -668,123 +598,100 @@ int main(int argc, char *argv[])
subsetter.subMesh().setInstance(runTime.timeName());
}
Info<< "Writing subsetted mesh and fields to time " << runTime.timeName()
<< endl;
subsetter.subMesh().write();
processorMeshes::removeFiles(subsetter.subMesh());
// Subsetting adds 'subset' prefix. Rename field to be like original.
// Volume fields
forAll(scalarFlds, i)
{
scalarFlds[i].rename(scalarNames[i]);
scalarFlds[i].write();
}
forAll(vectorFlds, i)
{
vectorFlds[i].rename(vectorNames[i]);
vectorFlds[i].write();
}
forAll(sphericalTensorFlds, i)
forAll(sphTensorFlds, i)
{
sphericalTensorFlds[i].rename(sphericalTensorNames[i]);
sphericalTensorFlds[i].write();
sphTensorFlds[i].write();
}
forAll(symmTensorFlds, i)
{
symmTensorFlds[i].rename(symmTensorNames[i]);
symmTensorFlds[i].write();
}
forAll(tensorFlds, i)
{
tensorFlds[i].rename(tensorNames[i]);
tensorFlds[i].write();
}
// Surface ones.
// Surface fields.
forAll(surfScalarFlds, i)
{
surfScalarFlds[i].rename(surfScalarNames[i]);
surfScalarFlds[i].write();
}
forAll(surfVectorFlds, i)
{
surfVectorFlds[i].rename(surfVectorNames[i]);
surfVectorFlds[i].write();
}
forAll(surfSphericalTensorFlds, i)
forAll(surfSphTensorFlds, i)
{
surfSphericalTensorFlds[i].rename(surfSphericalTensorNames[i]);
surfSphericalTensorFlds[i].write();
surfSphTensorFlds[i].write();
}
forAll(surfSymmTensorFlds, i)
{
surfSymmTensorFlds[i].rename(surfSymmTensorNames[i]);
surfSymmTensorFlds[i].write();
}
forAll(surfTensorNames, i)
forAll(surfTensorFlds, i)
{
surfTensorFlds[i].rename(surfTensorNames[i]);
surfTensorFlds[i].write();
}
// Point ones
// Point fields
forAll(pointScalarFlds, i)
{
pointScalarFlds[i].rename(pointScalarNames[i]);
pointScalarFlds[i].write();
}
forAll(pointVectorFlds, i)
{
pointVectorFlds[i].rename(pointVectorNames[i]);
pointVectorFlds[i].write();
}
forAll(pointSphericalTensorFlds, i)
forAll(pointSphTensorFlds, i)
{
pointSphericalTensorFlds[i].rename(pointSphericalTensorNames[i]);
pointSphericalTensorFlds[i].write();
pointSphTensorFlds[i].write();
}
forAll(pointSymmTensorFlds, i)
{
pointSymmTensorFlds[i].rename(pointSymmTensorNames[i]);
pointSymmTensorFlds[i].write();
}
forAll(pointTensorNames, i)
forAll(pointTensorFlds, i)
{
pointTensorFlds[i].rename(pointTensorNames[i]);
pointTensorFlds[i].write();
}
// DimensionedFields
// Dimensioned fields
forAll(scalarDimFlds, i)
{
scalarDimFlds[i].rename(scalarDimNames[i]);
scalarDimFlds[i].write();