openfoam/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C
Mark Olesen 89cca8578c ENH: rewrite of foamToVTK to include parallel output (#926)
- Default format is now XML binary (base64) instead of legacy format.
  The old -xml option is redundant and ignored.
  The new -legacy option can be used to force legacy output instead.

- Polyhedral decomposition is now off by default (old -poly is ignored).
  The option -poly-decomp forces decomposition of polyhedrals into
  primitive shapes.

- reduced memory footprint by reading and converting fields
  successively.

- Creation of symlinks to processor files is no longer required or
  desired. The old -noLinks option is ignored.

- Ignore -useTimeName option. Always number according to timeIndex.
2018-10-09 20:00:55 +02:00

769 lines
21 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
foamToVTK
Group
grpPostProcessingUtilities
Description
General OpenFOAM to VTK file writer.
Other bits
- Handles volFields, pointFields, surfaceScalarField, surfaceVectorField
fields.
- Mesh topo changes.
- Both ascii and binary.
- Single time step writing.
- Write subset only.
- Optional decomposition of cells.
Usage
\b foamToVTK [OPTION]
Options:
- \par -ascii
Write VTK data in ASCII format instead of binary.
- \par -legacy
Write VTK data in legacy format instead of XML format
- \par -fields \<fields\>
Convert selected fields only. For example,
\verbatim
-fields '( p T U )'
\endverbatim
The quoting is required to avoid shell expansions and to pass the
information as a single argument.
- \par -surfaceFields
Write surfaceScalarFields (e.g., phi)
- \par -cellSet \<name\>
- \par -cellZone \<name\>
Restrict conversion to either the cellSet or the cellZone.
- \par -faceSet \<name\>
- \par -pointSet \<name\>
Restrict conversion to the faceSet or pointSet.
- \par -nearCellValue
Output cell value on patches instead of patch value itself
- \par -noBoundary
Suppress output for all boundary patches
- \par -noInternal
Suppress output for internal (volume) mesh
- \par -noLagrangian
Suppress writing Lagrangian positions and fields.
- \par -noPointValues
No pointFields
- \par -noFaceZones
No faceZones
- \par -poly-decomp
Decompose polyhedral cells into tets/pyramids
- \par -allPatches
Combine all patches into a single boundary file
- \par -patch \<patchNames\>
Specify which patch or patches (name or regex) to convert.
- \par -excludePatches \<patchNames\>
Specify which patch or patches (name or regex) not to convert.
For example,
\verbatim
-excludePatches '( inlet_1 inlet_2 "proc.*")'
\endverbatim
Note
The mesh subset is handled by fvMeshSubsetProxy. Slight inconsistency in
interpolation: on the internal field it interpolates the whole volField
to the whole-mesh pointField and then selects only those values it
needs for the subMesh (using the fvMeshSubset cellMap(), pointMap()
functions). For the patches however it uses the
fvMeshSubset.interpolate function to directly interpolate the
whole-mesh values onto the subset patch.
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "pointMesh.H"
#include "emptyPolyPatch.H"
#include "volPointInterpolation.H"
#include "faceZoneMesh.H"
#include "areaFields.H"
#include "fvMeshSubsetProxy.H"
#include "faceSet.H"
#include "pointSet.H"
#include "HashOps.H"
#include "regionProperties.H"
#include "stringListOps.H"
#include "Cloud.H"
#include "readFields.H"
#include "reportFields.H"
#include "foamVtmWriter.H"
#include "foamVtkInternalWriter.H"
#include "foamVtkPatchWriter.H"
#include "foamVtkSurfaceMeshWriter.H"
#include "foamVtkLagrangianWriter.H"
#include "foamVtkSurfaceFieldWriter.H"
#include "foamVtkWriteTopoSet.H"
#include "foamVtkSeriesWriter.H"
#include "writeAreaFields.H"
#include "writeDimFields.H"
#include "writeVolFields.H"
#include "writePointFields.H"
#include "memInfo.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
labelList getSelectedPatches
(
const polyBoundaryMesh& patches,
const wordRes& whitelist,
const wordRes& blacklist
)
{
DynamicList<label> patchIDs(patches.size());
for (const polyPatch& pp : patches)
{
if (isType<emptyPolyPatch>(pp))
{
continue;
}
else if (Pstream::parRun() && isType<processorPolyPatch>(pp))
{
break; // No processor patches for parallel output
}
const word& patchName = pp.name();
bool accept = false;
if (whitelist.size())
{
const auto matched = whitelist.matched(patchName);
accept =
(
matched == wordRe::LITERAL
? true
: (matched == wordRe::REGEX && !blacklist.match(patchName))
);
}
else
{
accept = !blacklist.match(patchName);
}
if (accept)
{
patchIDs.append(pp.index());
}
}
return patchIDs.shrink();
}
//
// Process args for output options (-ascii, -legacy)
//
vtk::outputOptions getOutputOptions(const argList& args)
{
// Default is inline ASCII xml
vtk::outputOptions opts;
if (args.found("legacy"))
{
opts.legacy(true);
if (!args.found("ascii"))
{
if (sizeof(floatScalar) != 4 || sizeof(label) != 4)
{
opts.ascii(true);
WarningInFunction
<< "Using ASCII rather than legacy binary VTK format since "
<< "floatScalar and/or label are not 4 bytes in size."
<< nl << endl;
}
else
{
opts.ascii(false);
}
}
}
else
{
opts.ascii(args.found("ascii"));
}
return opts;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addNote("OpenFOAM to VTK writer");
timeSelector::addOptions();
// Infrequently needed, mark as advanced.
argList::setAdvanced("noFunctionObjects");
argList::addBoolOption
(
"ascii",
"Write in ASCII format instead of binary"
);
argList::addBoolOption
(
"legacy",
"Write legacy format instead of xml"
);
argList::addBoolOption
(
"poly-decomp",
"Decompose polyhedral cells into tets/pyramids",
true // mark as an advanced option
);
argList::ignoreOptionCompat
(
{"xml", 1806}, // xml is now default, use -legacy to toggle
false // bool option, no argument
);
argList::ignoreOptionCompat
(
{"poly", 1806}, // poly is now default, use -poly-decomp to toggle
false // bool option, no argument
);
argList::addOption
(
"cellSet",
"name",
"Convert mesh subset corresponding to specified cellSet",
true // mark as an advanced option
);
argList::addOption
(
"cellZone",
"name",
"Convert mesh subset corresponding to specified cellZone",
true // mark as an advanced option
);
argList::addOption
(
"faceSet",
"name",
"Convert specified faceSet only",
true // mark as an advanced option
);
argList::addOption
(
"pointSet",
"name",
"Convert specified pointSet only",
true // mark as an advanced option
);
argList::addOption
(
"fields",
"wordRes",
"Only convert specified fields\n"
"Eg, '(p T U \"alpha.*\")'"
);
argList::addBoolOption
(
"surfaceFields",
"Write surfaceScalarFields (eg, phi)",
true // mark as an advanced option
);
argList::addBoolOption
(
"finiteAreaFields",
"Write finite area fields",
true // mark as an advanced option
);
argList::addBoolOption
(
"nearCellValue",
"Use cell value on patches instead of patch value itself",
true // mark as an advanced option
);
argList::addBoolOption
(
"noBoundary", // no-boundary
"Suppress output for boundary patches"
);
argList::addBoolOption
(
"noInternal", // no-internal
"Suppress output for internal volume mesh"
);
argList::addBoolOption
(
"noLagrangian", // no-lagrangian
"Suppress writing lagrangian positions and fields"
);
argList::addBoolOption
(
"noPointValues", // no-point-data
"No pointFields and no interpolated PointData"
);
argList::addBoolOption
(
"allPatches", // one-boundary
"Combine all patches into a single file",
true // mark as an advanced option
);
#include "addRegionOption.H"
argList::addOption
(
"regions",
"wordRes",
"Operate on selected regions from regionProperties.\n"
"Eg, '( gas \"solid.*\" )'"
);
argList::addBoolOption
(
"allRegions",
"Operate on all regions in regionProperties"
);
argList::addOption
(
"patches",
"wordRes",
"A list of patches to include.\n"
"Eg, '( front \".*back\" )'"
);
argList::addOption
(
"excludePatches",
"wordRes",
"A list of patches to exclude\n"
"Eg, '( inlet \".*Wall\" )'"
);
argList::addBoolOption
(
"noFaceZones",
"No faceZones",
true // mark as an advanced option
);
argList::ignoreOptionCompat
(
{"noLinks", 1806}, // ignore never make any links
false // bool option, no argument
);
argList::ignoreOptionCompat
(
{"useTimeName", 1806}, // ignore - works poorly with VTM formats
false // bool option, no argument
);
argList::addBoolOption
(
"overwrite",
"Remove any existing VTK output directory"
);
argList::addOption
(
"name",
"subdir",
"Directory name for VTK output (default: 'VTK')"
);
#include "setRootCase.H"
const bool decomposePoly = args.found("poly-decomp");
const bool doBoundary = !args.found("noBoundary");
const bool doInternal = !args.found("noInternal");
const bool doLagrangian = !args.found("noLagrangian");
const bool doFiniteArea = args.found("finiteAreaFields");
const bool doSurfaceFields = args.found("surfaceFields");
const bool doFaceZones = !args.found("noFaceZones") && doInternal;
const bool oneBoundary = args.found("allPatches") && doBoundary;
const bool nearCellValue = args.found("nearCellValue") && doBoundary;
const bool allRegions = args.found("allRegions");
const vtk::outputOptions writeOpts = getOutputOptions(args);
if (nearCellValue)
{
Info<< "Using neighbouring cell value instead of patch value"
<< nl << endl;
}
const bool noPointValues = args.found("noPointValues");
if (noPointValues)
{
Info<< "Outputting cell values only."
<< " Point fields disabled by '-noPointValues' option"
<< nl;
}
wordRes includePatches, excludePatches;
if (doBoundary)
{
if (args.readListIfPresent<wordRe>("patches", includePatches))
{
Info<< "Including patches " << flatOutput(includePatches)
<< nl << endl;
}
if (args.readListIfPresent<wordRe>("excludePatches", excludePatches))
{
Info<< "Excluding patches " << flatOutput(excludePatches)
<< nl << endl;
}
}
wordRes selectedFields;
const bool useFieldFilter =
args.readListIfPresent<wordRe>("fields", selectedFields);
#include "createTime.H"
instantList timeDirs = timeSelector::select0(runTime, args);
// Information for file series
HashTable<vtk::seriesWriter, fileName> vtkSeries;
wordList regionNames;
wordRes selectRegions;
if (allRegions)
{
regionNames =
regionProperties(runTime, IOobject::READ_IF_PRESENT).names();
if (regionNames.empty())
{
Info<<"Warning: "
<< "No regionProperties - assuming default region"
<< nl << endl;
regionNames.resize(1);
regionNames.first() = fvMesh::defaultRegion;
}
else
{
Info<< "Using all regions in regionProperties" << nl
<< " "<< flatOutput(regionNames) << nl;
}
}
else if (args.readListIfPresent<wordRe>("regions", selectRegions))
{
if (selectRegions.empty())
{
regionNames.resize(1);
regionNames.first() = fvMesh::defaultRegion;
}
else if
(
selectRegions.size() == 1 && !selectRegions.first().isPattern()
)
{
regionNames.resize(1);
regionNames.first() = selectRegions.first();
}
else
{
regionNames =
regionProperties(runTime, IOobject::READ_IF_PRESENT).names();
if (regionNames.empty())
{
Info<<"Warning: "
<< "No regionProperties - assuming default region"
<< nl << endl;
regionNames.resize(1);
regionNames.first() = fvMesh::defaultRegion;
}
else
{
inplaceSubsetStrings(selectRegions, regionNames);
if (regionNames.empty())
{
Info<< "No matching regions ... stopping" << nl << endl;
return 1;
}
Info<< "Using matching regions: "
<< flatOutput(regionNames) << nl;
}
}
}
else
{
regionNames.resize(1);
regionNames.first() =
args.lookupOrDefault<word>("region", fvMesh::defaultRegion);
}
// Names for sets and zones
word cellSelectionName;
word faceSetName;
word pointSetName;
fvMeshSubsetProxy::subsetType cellSubsetType = fvMeshSubsetProxy::NONE;
string vtkName = runTime.globalCaseName();
if (regionNames.size() == 1)
{
if (args.readIfPresent("cellSet", cellSelectionName))
{
vtkName = cellSelectionName;
cellSubsetType = fvMeshSubsetProxy::SET;
Info<< "Converting cellSet " << cellSelectionName
<< " only. New outside faces as \"oldInternalFaces\"."
<< nl;
}
else if (args.readIfPresent("cellZone", cellSelectionName))
{
vtkName = cellSelectionName;
cellSubsetType = fvMeshSubsetProxy::ZONE;
Info<< "Converting cellZone " << cellSelectionName
<< " only. New outside faces as \"oldInternalFaces\"."
<< nl;
}
args.readIfPresent("faceSet", faceSetName);
args.readIfPresent("pointSet", pointSetName);
}
else
{
for
(
const word& opt
: { "cellSet", "cellZone", "faceSet", "pointSet" }
)
{
if (args.found(opt))
{
Info<< "Ignoring -" << opt << " for multi-regions" << nl;
}
}
}
cpuTime timer;
memInfo mem;
Info<< "Initial memory " << mem.update().size() << " kB" << endl;
#include "createMeshes.H"
// Directory management
// Sub-directory for output
const word vtkDirName = args.lookupOrDefault<word>("name", "VTK");
const fileName outputDir(runTime.globalPath()/vtkDirName);
if (Pstream::master())
{
for (const word& regionName : regionNames)
{
// VTK/regionName directory in the case
fileName regionDir;
if (regionName != polyMesh::defaultRegion)
{
regionDir = outputDir / regionName;
}
if (args.found("overwrite") && isDir(regionDir))
{
Info<< "Deleting old VTK files in "
<< regionDir.relative(runTime.globalPath())
<< nl << endl;
rmDir(regionDir);
}
mkDir(regionDir);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
forAll(timeDirs, timei)
{
runTime.setTime(timeDirs[timei], timei);
const word timeDesc = "_" + Foam::name(runTime.timeIndex());
const scalar timeValue = runTime.value();
Info<< "Time: " << runTime.timeName() << endl;
// Accumulate information for multi-region VTM
vtk::vtmWriter vtmMultiRegion;
// vtmMultiRegion.set(vtkDir/vtkName + timeDesc)
forAll(regionNames, regioni)
{
const word& regionName = regionNames[regioni];
fileName regionPrefix;
if (regionName != polyMesh::defaultRegion)
{
regionPrefix = regionName;
}
auto& meshProxy = meshProxies[regioni];
auto& vtuMeshCells = vtuMappings[regioni];
// polyMesh::readUpdateState meshState = mesh.readUpdate();
// Check for new polyMesh/ and update mesh, fvMeshSubset
// and cell decomposition.
polyMesh::readUpdateState meshState =
meshProxy.readUpdate();
const fvMesh& mesh = meshProxy.mesh();
if
(
meshState == polyMesh::TOPO_CHANGE
|| meshState == polyMesh::TOPO_PATCH_CHANGE
)
{
// Trigger change for vtk cells too
vtuMeshCells.clear();
}
// Write topoSets before attempting anything else
{
#include "convertTopoSet.H"
if (wroteTopoSet)
{
continue;
}
}
// Search for list of objects for this time
IOobjectList objects(meshProxy.baseMesh(), runTime.timeName());
if (useFieldFilter)
{
objects.filterObjects(selectedFields);
}
// Prune restart fields
objects.prune_0();
if (noPointValues)
{
// Prune point fields unless specifically requested
objects.filterClasses
(
[](const word& clsName)
{
return fieldTypes::point.found(clsName);
},
true // prune
);
}
// Volume, internal, point fields
#include "convertVolumeFields.H"
// Surface fields
#include "convertSurfaceFields.H"
// Finite-area mesh and fields - need not exist
#include "convertAreaFields.H"
// Write lagrangian data
#include "convertLagrangian.H"
}
// Emit multi-region vtm
if (Pstream::master() && regionNames.size() > 1)
{
fileName outputName
(
outputDir/vtkName + "-regions" + timeDesc + ".vtm"
);
vtmMultiRegion.setTime(timeValue);
vtmMultiRegion.write(outputName);
fileName seriesName(vtk::seriesWriter::base(outputName));
vtk::seriesWriter& series = vtkSeries(seriesName);
// First time?
// Load from file, verify against filesystem,
// prune time >= currentTime
if (series.empty())
{
series.load(seriesName, true, timeValue);
}
series.append(timeValue, outputName);
series.write(seriesName);
}
Info<< "Wrote in "
<< timer.cpuTimeIncrement() << " s, "
<< mem.update().size() << " kB" << endl;
}
Info<< "\nEnd: "
<< timer.elapsedCpuTime() << " s, "
<< mem.update().peak() << " kB (peak)\n" << endl;
return 0;
}
// ************************************************************************* //