ENH: upgrade writeVTK function object -> vtkWrite function object

- user-selectable format (vtk or vtu, ascii or binary)
- dictionary syntax closer to ensightWrite
- tutorial example in windAroundBuildings
This commit is contained in:
Mark Olesen
2017-06-14 01:48:32 +02:00
parent df5b009708
commit 98cc0fc004
10 changed files with 335 additions and 246 deletions

View File

@ -1,7 +1,7 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/

View File

@ -2,7 +2,7 @@ abort/abort.C
codedFunctionObject/codedFunctionObject.C
ensightWrite/ensightWrite.C
writeVTK/writeVTK.C
vtkWrite/vtkWrite.C
removeRegisteredObject/removeRegisteredObject.C

View File

@ -76,7 +76,6 @@ SourceFiles
ensightWrite.C
ensightWriteTemplates.C
\*---------------------------------------------------------------------------*/
#ifndef functionObjects_ensightWrite_H

View File

@ -0,0 +1,221 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 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/>.
\*---------------------------------------------------------------------------*/
#include "vtkWrite.H"
#include "dictionary.H"
#include "Time.H"
#include "foamVtkInternalWriter.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(vtkWrite, 0);
addToRunTimeSelectionTable(functionObject, vtkWrite, dictionary);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::vtkWrite::vtkWrite
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict),
writeOpts_(vtk::formatType::INLINE_BASE64),
selectFields_(),
dirName_("VTK")
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::vtkWrite::~vtkWrite()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::vtkWrite::read(const dictionary& dict)
{
fvMeshFunctionObject::read(dict);
//
// writer options - default is xml base64
//
writeOpts_ = vtk::formatType::INLINE_BASE64;
if (dict.lookupOrDefault<bool>("legacy", false))
{
writeOpts_.legacy(true);
}
writeOpts_.ascii
(
dict.found("format")
&& (IOstream::formatEnum(dict.lookup("format")) == IOstream::ASCII)
);
// FUTURE?
// writeOpts_.precision
// (
// dict.lookupOrDefault
// (
// "writePrecision",
// IOstream::defaultPrecision()
// )
// );
// Info<< type() << " " << name() << " output-format: "
// << writeOpts_.description() << nl;
//
// other options
//
dict.readIfPresent("directory", dirName_);
writeIds_ = dict.lookupOrDefault<bool>("writeIds", false);
//
// output fields
//
dict.lookup("fields") >> selectFields_;
wordRes::inplaceUniq(selectFields_);
return true;
}
bool Foam::functionObjects::vtkWrite::execute()
{
return true;
}
bool Foam::functionObjects::vtkWrite::write()
{
// Count number of fields to be written: only needed for legacy vtk format
label nFields = 0;
if (writeOpts_.legacy())
{
nFields =
(
(writeIds_ ? 1 : 0)
+ countFields<volScalarField>()
+ countFields<volVectorField>()
+ countFields<volSphericalTensorField>()
+ countFields<volSymmTensorField>()
+ countFields<volTensorField>()
);
}
// const word timeDesc =
// useTimeName ? time_.timeName() : Foam::name(time_.timeIndex());
const word timeDesc = time_.timeName();
fileName vtkDir = dirName_;
if (!vtkDir.isAbsolute())
{
vtkDir = time_.path()/vtkDir;
}
mkDir(vtkDir);
string vtkName = time_.caseName();
if (Pstream::parRun())
{
// Strip off leading casename, leaving just processor_DDD ending.
string::size_type i = vtkName.rfind("processor");
if (i != string::npos)
{
vtkName = vtkName.substr(i);
}
}
// Create file and write header
const fileName outputName
(
vtkDir/vtkName
+ "_"
+ timeDesc
);
Info<< name() << " output Time: " << time_.timeName() << nl
<< " Internal : " << outputName << endl;
vtk::vtuCells vtuMeshCells
(
mesh_,
writeOpts_,
true // decompose
);
// Write mesh
vtk::internalWriter writer
(
mesh_,
vtuMeshCells,
outputName,
writeOpts_
);
// CellData
{
writer.beginCellData(nFields);
// Write cellID field
if (writeIds_)
{
writer.writeCellIDs();
}
// Write volFields
writeFields<volScalarField>(writer);
writeFields<volVectorField>(writer);
writeFields<volSphericalTensorField>(writer);
writeFields<volSymmTensorField>(writer);
writeFields<volTensorField>(writer);
writer.endCellData();
}
writer.writeFooter();
return true;
}
// ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -22,7 +22,7 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::functionObjects::writeVTK
Foam::functionObjects::vtkWrite
Group
grpUtilitiesFunctionObjects
@ -36,56 +36,47 @@ Description
Example of function object specification:
\verbatim
writeVTK1
{
type writeVTK;
libs ("libutilityFunctionObjects.so");
...
objectNames (obj1 obj2);
}
vtkWrite1
{
type vtkWrite;
libs ("libutilityFunctionObjects.so");
writeControl writeTime;
writeInterval 1;
format binary;
legacy false;
...
fields (U p);
}
\endverbatim
Usage
\table
Property | Description | Required | Default value
type | Type name: writeVTK | yes |
objectNames | objects to write | yes |
type | Type name: vtkWrite | yes |
fields | Fields to output | yes |
writeControl | Output control | recommended | timeStep
directory | The output directory name | no | "VTK"
format | ASCII or binary format | no | binary
legacy | Legacy VTK output | no | false
writeIds | Write cell ids as field | no | true
\endtable
See also
Foam::functionObjects::fvMeshFunctionObject
Foam::functionObjects::timeControl
Example of function object specification to calculate Lambda2:
\verbatim
Lambda2_1
{
type Lambda2;
functionObjectLibs ("libutilityFunctionObjects.so");
...
}
\endverbatim
\heading Function object usage
\table
Property | Description | Required | Default value
type | Type name: Lambda2 | yes |
UName | Name of velocity field | no | U
resultName | Name of Lambda2 field | no | <function name>
log | Log to standard output | no | yes
\endtable
SourceFiles
writeVTK.C
IOwriteVTK.H
vtkWrite.C
vtkWriteTemplates.C
\*---------------------------------------------------------------------------*/
#ifndef functionObjects_writeVTK_H
#define functionObjects_writeVTK_H
#ifndef functionObjects_vtkWrite_H
#define functionObjects_vtkWrite_H
#include "fvMeshFunctionObject.H"
#include "wordReList.H"
#include "foamVtkInternalWriter.H"
#include "wordRes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -95,46 +86,57 @@ namespace functionObjects
{
/*---------------------------------------------------------------------------*\
Class writeVTK Declaration
Class vtkWrite Declaration
\*---------------------------------------------------------------------------*/
class writeVTK
class vtkWrite
:
public fvMeshFunctionObject
{
// Private data
//- Names of objects
wordReList objectNames_;
//- VTK output options
vtk::outputOptions writeOpts_;
//- Result name
word resultName_;
//- Name of fields to process
wordReList selectFields_;
//- Output directory name
fileName dirName_;
//- Write cell ids field
bool writeIds_;
//- Switch to send output to Info as well as to file
Switch log_;
// Private Member Functions
//- Count number of selected fields for GeoField type.
// Only needed for legacy vtk format.
template<class GeoField>
UPtrList<const GeoField> lookupFields() const;
label countFields() const;
//- Write selected fields for GeoField type.
template<class GeoField>
label writeFields(vtk::internalWriter& writer, bool verbose=true) const;
//- Disallow default bitwise copy construct
writeVTK(const writeVTK&) = delete;
vtkWrite(const vtkWrite&) = delete;
//- Disallow default bitwise assignment
void operator=(const writeVTK&) = delete;
void operator=(const vtkWrite&) = delete;
public:
//- Runtime type information
TypeName("writeVTK");
TypeName("vtkWrite");
// Constructors
//- Construct from Time and dictionary
writeVTK
vtkWrite
(
const word& name,
const Time& t,
@ -143,18 +145,18 @@ public:
//- Destructor
virtual ~writeVTK();
virtual ~vtkWrite();
// Member Functions
//- Read the writeVTK data
//- Read the vtkWrite specification
virtual bool read(const dictionary& dict);
//- Execute, currently does nothing
virtual bool execute();
//- Write the writeVTK
//- Write fields
virtual bool write();
};
@ -167,7 +169,7 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "writeVTKTemplates.C"
#include "vtkWriteTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -23,38 +23,38 @@ License
\*---------------------------------------------------------------------------*/
#include "writeVTK.H"
#include "objectRegistry.H"
#include "DynamicList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class GeoField>
Foam::UPtrList<const GeoField>
Foam::functionObjects::writeVTK::lookupFields() const
Foam::label
Foam::functionObjects::vtkWrite::countFields() const
{
DynamicList<word> allNames(obr_.toc().size());
forAll(objectNames_, i)
{
wordList names(obr_.names<GeoField>(objectNames_[i]));
return obr_.names<GeoField>(selectFields_).size();
}
if (names.size())
{
allNames.append(names);
}
template<class GeoField>
Foam::label
Foam::functionObjects::vtkWrite::writeFields
(
vtk::internalWriter& writer,
bool verbose
) const
{
const wordList names = obr_.sortedNames<GeoField>(selectFields_);
if (verbose && names.size())
{
Info<< " " << GeoField::typeName
<< " " << flatOutput(names) << endl;
}
UPtrList<const GeoField> fields(allNames.size());
forAll(allNames, i)
for (const word& fieldName : names)
{
const GeoField& field = obr_.lookupObject<GeoField>(allNames[i]);
Info<< " Writing " << GeoField::typeName
<< " field " << field.name() << endl;
fields.set(i, &field);
writer.write(obr_.lookupObject<GeoField>(fieldName));
}
return fields;
return names.size();
}

View File

@ -1,166 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
\*---------------------------------------------------------------------------*/
#include "writeVTK.H"
#include "dictionary.H"
#include "Time.H"
#include "foamVtkInternalWriter.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(writeVTK, 0);
addToRunTimeSelectionTable(functionObject, writeVTK, dictionary);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::writeVTK::writeVTK
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict),
objectNames_()
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::writeVTK::~writeVTK()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::writeVTK::read(const dictionary& dict)
{
dict.lookup("objects") >> objectNames_;
return true;
}
bool Foam::functionObjects::writeVTK::execute()
{
return true;
}
bool Foam::functionObjects::writeVTK::write()
{
Info<< type() << " " << name() << " output:" << nl;
Info<< "Time: " << time_.timeName() << endl;
word timeDesc = time_.timeName();
// VTK/ directory in the case
fileName fvPath(time_.path()/"VTK");
mkDir(fvPath);
string vtkName = time_.caseName();
if (Pstream::parRun())
{
// Strip off leading casename, leaving just processor_DDD ending.
string::size_type i = vtkName.rfind("processor");
if (i != string::npos)
{
vtkName = vtkName.substr(i);
}
}
// Create file and write header
const fileName outputName
(
fvPath/vtkName
+ "_"
+ timeDesc
);
Info<< " Internal : " << outputName << endl;
vtk::vtuCells meshCells
(
mesh_,
vtk::vtuCells::contentType::LEGACY,
true // decompose
);
// Write mesh
vtk::internalWriter writer
(
mesh_,
meshCells,
outputName,
vtk::formatType::LEGACY_ASCII
);
UPtrList<const volScalarField> vsf(lookupFields<volScalarField>());
UPtrList<const volVectorField> vvf(lookupFields<volVectorField>());
UPtrList<const volSphericalTensorField> vsptf
(
lookupFields<volSphericalTensorField>()
);
UPtrList<const volSymmTensorField> vstf(lookupFields<volSymmTensorField>());
UPtrList<const volTensorField> vtf(lookupFields<volTensorField>());
const label nVolFields =
vsf.size() + vvf.size() + vsptf.size() + vstf.size() + vtf.size();
// CellData
{
writer.beginCellData(1 + nVolFields);
// Write cellID field
writer.writeCellIDs();
// Write volFields
writer.write(vsf);
writer.write(vvf);
writer.write(vsptf);
writer.write(vstf);
writer.write(vtf);
}
return true;
}
// ************************************************************************* //

View File

@ -10,8 +10,8 @@ ensightWrite
// Fields to output (words or regex)
fields (U p "(k|epsilon|omega)");
writeControl writeTime;
writeIterval 1;
writeControl writeTime;
writeInterval 1;
}
// ************************************************************************* //

View File

@ -50,6 +50,8 @@ runTimeModifiable true;
functions
{
#include "vtkWrite"
}
// ************************************************************************* //

View File

@ -0,0 +1,31 @@
// -*- C++ -*-
// Minimal example of using the vtkWrite function object.
vtkWrite
{
type vtkWrite;
libs ("libutilityFunctionObjects.so");
log true;
// Fields to output (words or regex)
fields (U p "(k|epsilon|omega)");
//- Output format (ascii | binary) - Default=binary
// format binary;
//- Use legacy output format - Default=false
// legacy false;
//- Output directory name - Default="VTK"
// directory "VTK";
//- Write cell ids as field - Default=true
writeIds false;
writeControl writeTime;
writeInterval 1;
writeControl timeStep;
writeInterval 25;
}
// ************************************************************************* //