ENH: vtkUnstructuredToFoam: construct&write fields. See #3195.

This commit is contained in:
mattijs 2024-07-10 15:20:54 +01:00
parent 7ec78f6d6d
commit bb8f7799d9
2 changed files with 86 additions and 2 deletions

View File

@ -1,5 +1,7 @@
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/fileFormats/lnInclude
EXE_LIBS = \
-lfiniteVolume \
-lfileFormats

View File

@ -34,23 +34,84 @@ Description
Convert legacy VTK file (ascii) containing an unstructured grid
to an OpenFOAM mesh without boundary information.
Usage
\b vtkUnstructuredToFoam \<XXX.vtk\>
Options:
- \par -no-fields
Do not attempt to recreate volFields
Note
The .vtk format does not contain any boundary information.
It is purely a description of the internal mesh.
It is purely a description of the internal mesh. This also limits the
usefulness of reconstructing the volFields.
Not extensively tested.
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "Time.H"
#include "polyMesh.H"
#include "fvMesh.H"
#include "IFstream.H"
#include "vtkUnstructuredReader.H"
#include "columnFvMesh.H"
#include "scalarIOField.H"
#include "vectorIOField.H"
#include "volFields.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
void constructVolFields(fvMesh& mesh, const vtkUnstructuredReader& reader)
{
const auto fields(reader.cellData().csorted<IOField<Type>>());
for (const auto& field : fields)
{
Info<< "Constructing volField " << field.name() << endl;
// field is
// - cell data followed by
// - boundary face data
auto tfld = GeometricField<Type, fvPatchField, volMesh>::New
(
field.name(),
mesh,
dimless
);
auto& fld = tfld.ref();
fld.instance() = mesh.time().timeName();
fld.writeOpt() = IOobject::AUTO_WRITE;
// Fill cell values
fld.internalFieldRef().field() =
UIndirectList<Type>(field, reader.cellMap());
// Fill boundary values
const auto& map = reader.faceMap();
if (map.size())
{
for (auto& pfld : fld.boundaryFieldRef())
{
const auto& pp = pfld.patch();
forAll(pfld, i)
{
const label bFacei = pp.patch().offset()+i;
pfld[i] = field[map[bFacei]];
}
}
}
regIOobject::store(std::move(tfld));
}
}
int main(int argc, char *argv[])
{
@ -61,11 +122,14 @@ int main(int argc, char *argv[])
);
argList::noParallel();
argList::addOptionCompat("no-fields", {"noFields", 2106});
argList::addArgument("vtk-file", "The input legacy ascii vtk file");
#include "setRootCase.H"
#include "createTime.H"
const bool doFields = !args.found("no-fields");
IFstream mshStream(args.get<fileName>(1));
vtkUnstructuredReader reader(runTime, mshStream);
@ -96,6 +160,24 @@ int main(int argc, char *argv[])
mesh.removeFiles();
mesh.write();
if (doFields)
{
// Re-read mesh as fvMesh so we can have fields
Info<< "Re-reading mesh ..." << endl;
#include "createMesh.H"
constructVolFields<scalar>(mesh, reader);
constructVolFields<vector>(mesh, reader);
constructVolFields<sphericalTensor>(mesh, reader);
constructVolFields<symmTensor>(mesh, reader);
constructVolFields<tensor>(mesh, reader);
// No need to write the mesh, only fields
mesh.thisDb().write();
}
Info<< "End\n" << endl;
return 0;