add support for exporting mesh to user file format (at least some of the formats available internally)

This commit is contained in:
Bryn Lloyd 2019-03-01 09:43:47 +01:00
parent 63d1380bdb
commit 227d297f78
6 changed files with 220 additions and 8 deletions

View File

@ -3,7 +3,7 @@ add_library(interface ${NG_LIB_TYPE}
nginterface.cpp nginterface_v2.cpp
read_fnf_mesh.cpp readtetmesh.cpp readuser.cpp writeabaqus.cpp writediffpack.cpp
writedolfin.cpp writeelmer.cpp writefeap.cpp writefluent.cpp writegmsh.cpp writejcm.cpp
writepermas.cpp writetecplot.cpp writetet.cpp writetochnog.cpp writeuser.cpp
writepermas.cpp writetecplot.cpp writetet.cpp writetochnog.cpp writeuser.cpp writevtk.cpp
wuchemnitz.cpp writegmsh2.cpp writeOpenFOAM15x.cpp
)

View File

@ -39,8 +39,9 @@ namespace netgen
"VRML Format", ".*",
"Gmsh Format", ".gmsh",
"Gmsh2 Format", ".gmsh2",
"VTK Format", ".vtk",
"OpenFOAM 1.5+ Format", "*",
"OpenFOAM 1.5+ Compressed", "*",
"OpenFOAM 1.5+ Compressed", "*",
"JCMwave Format", ".jcm",
"TET Format", ".tet",
// { "Chemnitz Format" },
@ -128,6 +129,9 @@ bool WriteUserFormat (const string & format,
else if (format == "Gmsh2 Format")
WriteGmsh2Format (mesh, geom, filename);
else if (format == "VTK Format")
WriteVtkFormat (mesh, filename);
// Philippose - 25/10/2009
// Added OpenFOAM 1.5+ Mesh export capability
else if (format == "OpenFOAM 1.5+ Format")

View File

@ -73,6 +73,9 @@ void WriteGmsh2Format (const Mesh & mesh,
const NetgenGeometry & geom,
const string & filename);
extern
void WriteVtkFormat (const Mesh & mesh,
const string & filename);
// Philippose - 25/10/2009
// Added OpenFOAM 1.5+ Mesh Export support

View File

@ -0,0 +1,124 @@
/*************************************
* Write Gmsh file
* First issue the 04/26/2004 by Paul CARRICO (paul.carrico@free.fr)
* At the moment, the GMSH format is available for
* linear tetrahedron elements i.e. in 3D
* (based on Neutral Format)
*
* Second issue the 05/05/2004 by Paul CARRICO
* Thanks to Joachim Schoeberl for the correction of a minor bug
* the 2 initial Gmsh Format (i.e. volume format and surface format) are group together)
* in only one file
**************************************/
#include <mystdlib.h>
#include <myadt.hpp>
#include <linalg.hpp>
#include <csg.hpp>
#include <meshing.hpp>
namespace netgen
{
#include "writeuser.hpp"
extern MeshingParameters mparam;
/*
* VTK mesh format
* points, elements, surface elements
*/
void WriteVtkFormat (const Mesh & mesh,
const string & filename)
{
ofstream outfile (filename.c_str());
outfile.precision(6);
outfile.setf (ios::fixed, ios::floatfield);
outfile.setf (ios::showpoint);
int np = mesh.GetNP(); /// number of point
int ne = mesh.GetNE(); /// number of element
int nse = mesh.GetNSE(); /// number of surface element (BC)
outfile << "# vtk DataFile Version 2.0\n";
outfile << "Created with netgen\n";
outfile << "ASCII\n";
outfile << "DATASET UNSTRUCTURED_GRID\n";
outfile << "POINTS " << np << " double\n";
for (int i=0; i<np; i++)
{
auto & p = mesh.Point(i+1);
outfile << p[0] << " " << p[1] << " " << p[2] << "\n";
}
std::vector<int> types;
if (ne > 0)
{
unsigned int size = 0;
for (int i=0; i<ne; i++)
size += mesh.VolumeElement(i+1).GetNV() + 1; // only save "linear" corners
outfile << "CELLS " << ne << " " << size << "\n";
for (int i=0; i<ne; i++)
{
auto& el = mesh.VolumeElement(i+1);
switch (el.GetType())
{
case TET:
case TET10: // reorder to follow VTK convention & zero based indices
outfile << 4 << " " << el[0]-1 << " " << el[1]-1 << " " << el[3]-1 << " " << el[2]-1 << "\n";
types.push_back(10);
break;
case PRISM: // reorder to follow VTK convention & zero based indices
outfile << 6 << " "
<< el[0]-1 << " " << el[2]-1 << " " << el[1]-1 << " "
<< el[3]-1 << " " << el[5]-1 << " " << el[4]-1 << "\n";
types.push_back(13);
break;
default:
throw ngcore::Exception("Unexpected element type");
break;
}
}
}
else
{
unsigned int size = 0;
for (int i=0; i<ne; i++)
size += mesh.SurfaceElement(i+1).GetNV() + 1;
outfile << "CELLS " << nse << " " << size << "\n";
for (int i=0; i<nse; i++)
{
auto& el = mesh.SurfaceElement(i+1);
switch (el.GetType())
{
case TRIG:
case TRIG6:
outfile << el[0]-1 << " " << el[1]-1 << " " << el[2]-1 << "\n";
types.push_back(5);
break;
case QUAD:
outfile << el[0]-1 << " " << el[1]-1 << " " << el[2]-1 << " " << el[3]-1 << "\n";
types.push_back(9);
default:
throw ngcore::Exception("Unexpected element type");
break;
}
}
}
outfile << "CELL_TYPES " << types.size() << "\n";
for (auto type_id: types)
{
outfile << type_id << "\n";
}
outfile.close();
}
}

View File

@ -18,6 +18,7 @@
#include <geometry2d.hpp>
#include <meshing.hpp>
#include <../visualization/soldata.hpp>
#include <../interface/writeuser.hpp>
#ifdef OCCGEOMETRY
#include <occgeom.hpp>
@ -154,6 +155,25 @@ namespace nglib
DLL_HEADER void Ng_ExportMesh(Ng_Mesh * ng_mesh, Ng_Export_Formats format, const char* filename)
{
Mesh * mesh = (Mesh*)ng_mesh;
switch (format)
{
case NG_GMSH:
WriteUserFormat( "Gmsh Format", *mesh, filename ); break;
case NG_GMSH2:
WriteUserFormat( "Gmsh2 Format", *mesh, filename ); break;
case NG_VTK:
WriteUserFormat( "VTK Format", *mesh, filename ); break;
case NG_FLUENT:
WriteUserFormat( "Fluent Format", *mesh, filename ); break;
case NG_ABAQUS:
WriteUserFormat( "Abaqus Format", *mesh, filename ); break;
}
}
// Merge another mesh file into the currently loaded one
DLL_HEADER Ng_Result Ng_MergeMesh( Ng_Mesh* mesh, const char* filename)
@ -265,6 +285,8 @@ namespace nglib
n = 5; break;
case NG_PRISM:
n = 6; break;
case NG_HEX:
n = 8; break;
case NG_TET10:
n = 10; break;
default: break;
@ -279,8 +301,6 @@ namespace nglib
}
// Obtain the number of points in the mesh
DLL_HEADER int Ng_GetNP (Ng_Mesh * mesh)
{
@ -366,6 +386,7 @@ namespace nglib
case 4: et = NG_TET; break;
case 5: et = NG_PYRAMID; break;
case 6: et = NG_PRISM; break;
case 8: et = NG_HEX; break;
case 10: et = NG_TET10; break;
default:
et = NG_TET; break; // for the compiler
@ -1202,6 +1223,53 @@ namespace nglib
}
void Ng_SetRefinementFlag (Ng_Mesh * ng_mesh, int ei, int flag)
{
Mesh * mesh = (Mesh*) ng_mesh;
if (mesh->GetDimension() == 3)
{
mesh->VolumeElement(ei).SetRefinementFlag (flag != 0);
mesh->VolumeElement(ei).SetStrongRefinementFlag (flag >= 10);
}
else
{
mesh->SurfaceElement(ei).SetRefinementFlag (flag != 0);
mesh->SurfaceElement(ei).SetStrongRefinementFlag (flag >= 10);
}
}
void Ng_SetSurfaceRefinementFlag (Ng_Mesh * ng_mesh, int ei, int flag)
{
Mesh * mesh = (Mesh*) ng_mesh;
if (mesh->GetDimension() == 3)
{
mesh->SurfaceElement(ei).SetRefinementFlag (flag != 0);
mesh->SurfaceElement(ei).SetStrongRefinementFlag (flag >= 10);
}
}
void Ng_Refine (Ng_Mesh * ng_mesh)
{
Mesh * mesh = (Mesh*) ng_mesh;
BisectionOptions biopt;
biopt.usemarkedelements = 1;
biopt.refine_p = 0; // only h-refinement
biopt.refine_hp = 0;
if (auto geom = mesh->GetGeometry())
geom->GetRefinement().Bisect (*mesh, biopt);
else
Refinement().Bisect (*mesh, biopt);
// not sure if this is needed?
//mesh -> UpdateTopology();
//mesh -> GetCurvedElements().SetIsHighOrder (false);
}
DLL_HEADER void Ng_2D_Uniform_Refinement (Ng_Geometry_2D * geom,
@ -1212,7 +1280,6 @@ namespace nglib
DLL_HEADER void Ng_STL_Uniform_Refinement (Ng_STL_Geometry * geom,
Ng_Mesh * mesh)
{
@ -1312,7 +1379,6 @@ void Ng_InitSolutionData (Ng_SolutionData * soldata) { ; }
*/
// Force linking libinterface to libnglib
#include <../interface/writeuser.hpp>
void MyDummyToForceLinkingLibInterface(Mesh &mesh, NetgenGeometry &geom)
{
netgen::WriteUserFormat("", mesh, /* geom, */ "");

View File

@ -71,7 +71,7 @@ enum Ng_Surface_Element_Type
/// Currently implemented volume element types
enum Ng_Volume_Element_Type
{ NG_TET = 1, NG_PYRAMID = 2, NG_PRISM = 3, NG_TET10 = 4 };
{ NG_TET = 1, NG_PYRAMID = 2, NG_PRISM = 3, NG_TET10 = 4, NG_HEX = 5 };
/// Values returned by Netgen functions
enum Ng_Result
@ -258,6 +258,13 @@ DLL_HEADER void Ng_SaveMesh(Ng_Mesh * mesh, const char* filename);
DLL_HEADER Ng_Mesh * Ng_LoadMesh(const char* filename);
/*! \brief Save mesh in various external formats, e.g. fluent, gmsh, gmsh2, vtk, ...
*/
enum Ng_Export_Formats { NG_GMSH = 1, NG_GMSH2 = 2, NG_VTK = 3, NG_FLUENT = 4, NG_ABAQUS = 5 };
DLL_HEADER void Ng_ExportMesh(Ng_Mesh * mesh, Ng_Export_Formats format, const char* filename);
/*! \brief Merge a Netgen VOL Mesh from disk into an existing mesh in memory
A Netgen mesh saved in the internal VOL format can be merged
@ -362,7 +369,7 @@ DLL_HEADER void Ng_AddSurfaceElement (Ng_Mesh * mesh, Ng_Surface_Element_Type et
*/
DLL_HEADER void Ng_AddVolumeElement (Ng_Mesh * mesh, Ng_Volume_Element_Type et, int * pi, int domain=1);
// ------------------------------------------------------------------
@ -712,6 +719,14 @@ DLL_HEADER Ng_Result Ng_OCC_GetFMap(Ng_OCC_Geometry * geom,
DLL_HEADER void Ng_Uniform_Refinement (Ng_Mesh * mesh);
// non-uniform mesh refinement
DLL_HEADER void Ng_SetRefinementFlag (Ng_Mesh * ng_mesh, int ei, int flag);
DLL_HEADER void Ng_SetSurfaceRefinementFlag (Ng_Mesh * ng_mesh, int ei, int flag);
DLL_HEADER void Ng_Refine (Ng_Mesh * ng_mesh);
// uniform mesh refinement with geometry adaption:
DLL_HEADER void Ng_2D_Uniform_Refinement (Ng_Geometry_2D * geom,