* Added capability of writing gz compressed STL surface meshes

* Added capability of writing gz compressed OpenFOAM meshes
This commit is contained in:
Philippose Rajan 2013-05-02 17:51:04 +00:00
parent 0ca9193607
commit f9f8d5481b
4 changed files with 3140 additions and 3080 deletions

View File

@ -45,7 +45,7 @@ namespace netgen
static void WriteOpenFOAM15xBanner(ofstream & outfile)
static void WriteOpenFOAM15xBanner(ostream * outfile)
{
static char FOAMversion[4] = "1.5";
static char spaces[40];
@ -53,10 +53,10 @@ namespace netgen
memset(spaces, ' ', 40);
spaces[38 - strlen(FOAMversion)] = '\0';
outfile <<
*outfile <<
"/*--------------------------------*- C++ -*----------------------------------*\\\n";
outfile <<
*outfile <<
"| ========= | |\n"
"| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n"
"| \\\\ / O peration | Version: " << FOAMversion << spaces << "|\n"
@ -68,17 +68,17 @@ namespace netgen
static void WriteOpenFOAM15xDividerStart(ofstream & outfile)
static void WriteOpenFOAM15xDividerStart(ostream * outfile)
{
outfile <<
*outfile <<
"// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n";
}
static void WriteOpenFOAM15xDividerEnd(ofstream & outfile)
static void WriteOpenFOAM15xDividerEnd(ostream * outfile)
{
outfile <<
*outfile <<
"// ************************************************************************* //\n";
}
@ -281,11 +281,11 @@ namespace netgen
static void WriteNeighbourFile (ofstream & outfile)
static void WriteNeighbourFile (ostream * outfile)
{
// Write the OpenFOAM standard banner and dividers, etc...
WriteOpenFOAM15xBanner(outfile);
outfile << "FoamFile \n"
*outfile << "FoamFile \n"
<< "{ \n"
<< " version 2.0; \n"
<< " format ascii; \n"
@ -296,30 +296,30 @@ namespace netgen
<< "} \n";
WriteOpenFOAM15xDividerStart(outfile);
outfile << "\n\n";
*outfile << "\n\n";
int nneighbours = neighbour_celllist.Size();
outfile << nneighbours << "\n";
*outfile << nneighbours << "\n";
outfile << "(\n";
*outfile << "(\n";
// Write the neighbour cells to file
for(int i = 1; i <= neighbour_celllist.Size(); i++)
{
outfile << neighbour_celllist.Elem(i) - 1 << "\n";
*outfile << neighbour_celllist.Elem(i) - 1 << "\n";
}
outfile << ")\n\n";
*outfile << ")\n\n";
WriteOpenFOAM15xDividerEnd(outfile);
}
static void WriteOwnerFile (ofstream & outfile)
static void WriteOwnerFile (ostream * outfile)
{
// Write the OpenFOAM standard banner and dividers, etc...
WriteOpenFOAM15xBanner(outfile);
outfile << "FoamFile \n"
*outfile << "FoamFile \n"
<< "{ \n"
<< " version 2.0; \n"
<< " format ascii; \n"
@ -330,39 +330,39 @@ namespace netgen
<< "} \n";
WriteOpenFOAM15xDividerStart(outfile);
outfile << "\n\n";
*outfile << "\n\n";
int nowners = owner_celllist.Size() + surfelem_lists.Size();
outfile << nowners << "\n";
*outfile << nowners << "\n";
outfile << "(\n";
*outfile << "(\n";
// Write the owners of the internal cells to file
for(int i = 1; i <= owner_celllist.Size(); i++)
{
outfile << owner_celllist.Elem(i) - 1 << "\n";
*outfile << owner_celllist.Elem(i) - 1 << "\n";
}
// Write the owners of the boundary cells to file
// (Written in order of ascending boundary condition numbers)
for(int i = 1; i <= surfelem_lists.Size(); i++)
{
outfile << surfelem_lists.Elem(i).I2() - 1 << "\n";
*outfile << surfelem_lists.Elem(i).I2() - 1 << "\n";
}
outfile << ")\n\n";
*outfile << ")\n\n";
WriteOpenFOAM15xDividerEnd(outfile);
}
static void WriteFacesFile (ofstream & outfile, const Mesh & mesh)
static void WriteFacesFile (ostream * outfile, const Mesh & mesh)
{
const MeshTopology& meshtopo = mesh.GetTopology();
// Write the OpenFOAM standard banner and dividers, etc...
WriteOpenFOAM15xBanner(outfile);
outfile << "FoamFile \n"
*outfile << "FoamFile \n"
<< "{ \n"
<< " version 2.0; \n"
<< " format ascii; \n"
@ -373,13 +373,13 @@ namespace netgen
<< "} \n";
WriteOpenFOAM15xDividerStart(outfile);
outfile << "\n\n";
*outfile << "\n\n";
int nfaces = owner_facelist.Size() + surfelem_lists.Size();
outfile << nfaces << "\n";
*outfile << nfaces << "\n";
outfile << "(\n";
*outfile << "(\n";
// Array to hold the indices of the points of each face to
// flip if required
@ -420,14 +420,14 @@ namespace netgen
}
}
outfile << facepnts.Size();
outfile << "(";
*outfile << facepnts.Size();
*outfile << "(";
for(int j = 1; j <= facepnts.Size(); j++)
{
outfile << facepnts.Elem(j)-1;
if(j != facepnts.Size()) outfile << " ";
*outfile << facepnts.Elem(j)-1;
if(j != facepnts.Size()) *outfile << " ";
}
outfile << ")\n";
*outfile << ")\n";
}
// Now append the faces of the surface elements (written in
@ -463,29 +463,29 @@ namespace netgen
}
}
outfile << facepnts.Size();
outfile << "(";
*outfile << facepnts.Size();
*outfile << "(";
for(int j = 1; j <= facepnts.Size(); j++)
{
outfile << facepnts.Elem(j)-1;
if(j != facepnts.Size()) outfile << " ";
*outfile << facepnts.Elem(j)-1;
if(j != facepnts.Size()) *outfile << " ";
}
outfile << ")\n";
*outfile << ")\n";
}
outfile << ")\n\n";
*outfile << ")\n\n";
WriteOpenFOAM15xDividerEnd(outfile);
}
static void WritePointsFile (ofstream & outfile, const Mesh & mesh)
static void WritePointsFile (ostream * outfile, const Mesh & mesh)
{
int np = mesh.GetNP();
// Write the OpenFOAM standard banner and dividers, etc...
WriteOpenFOAM15xBanner(outfile);
outfile << "FoamFile \n"
*outfile << "FoamFile \n"
<< "{ \n"
<< " version 2.0; \n"
<< " format ascii; \n"
@ -496,40 +496,40 @@ namespace netgen
<< "} \n";
WriteOpenFOAM15xDividerStart(outfile);
outfile << "\n\n";
*outfile << "\n\n";
// Number of points in the following list
outfile << np << "\n";
*outfile << np << "\n";
outfile.precision(6);
outfile.setf (ios::fixed, ios::floatfield);
outfile.setf (ios::showpoint);
outfile->precision(6);
outfile->setf (ios::fixed, ios::floatfield);
outfile->setf (ios::showpoint);
// Coordinate list starts here
outfile << "(\n";
*outfile << "(\n";
for(int i = 1; i <= np; i++)
{
const Point3d & p = mesh.Point(i);
// Write coordinates to file
outfile << "(";
outfile << p.X() << " ";
outfile << p.Y() << " ";
outfile << p.Z();
outfile << ")\n";
*outfile << "(";
*outfile << p.X() << " ";
*outfile << p.Y() << " ";
*outfile << p.Z();
*outfile << ")\n";
}
outfile << ")\n\n";
*outfile << ")\n\n";
WriteOpenFOAM15xDividerEnd(outfile);
}
static void WriteBoundaryFile (ofstream & outfile)
static void WriteBoundaryFile (ostream * outfile)
{
// Write the OpenFOAM standard banner and dividers, etc...
WriteOpenFOAM15xBanner(outfile);
outfile << "FoamFile \n"
*outfile << "FoamFile \n"
<< "{ \n"
<< " version 2.0; \n"
<< " format ascii; \n"
@ -540,7 +540,7 @@ namespace netgen
<< "} \n";
WriteOpenFOAM15xDividerStart(outfile);
outfile << "\n";
*outfile << "\n";
Array<INDEX_3> bcarray;
@ -570,8 +570,8 @@ namespace netgen
bcarray.SetSize(ind);
outfile << bcarray.Size() << "\n";
outfile << "(\n";
*outfile << bcarray.Size() << "\n";
*outfile << "(\n";
int startface = 0;
@ -579,7 +579,7 @@ namespace netgen
{
startface = owner_celllist.Size() + bcarray.Elem(i).I3();
outfile << " patch" << bcarray.Elem(i).I1() << "\n"
*outfile << " patch" << bcarray.Elem(i).I1() << "\n"
<< " {\n"
<< " type patch;\n"
<< " physicalType patch;\n"
@ -588,13 +588,13 @@ namespace netgen
<< " }\n";
}
outfile << ")\n\n";
*outfile << ")\n\n";
WriteOpenFOAM15xDividerEnd(outfile);
}
void WriteOpenFOAM15xFormat (const Mesh & mesh, const string & casename)
void WriteOpenFOAM15xFormat (const Mesh & mesh, const string & casename, const bool compressed)
{
bool error = false;
char casefiles[256];
@ -659,16 +659,59 @@ namespace netgen
// owner
// neighbour
// boundary
ostream *outfile_pnts;
ostream *outfile_faces;
ostream *outfile_own;
ostream *outfile_nei;
ostream *outfile_bnd;
if(compressed)
{
sprintf(casefiles, "%s/constant/polyMesh/points.gz", casename.c_str());
outfile_pnts = new ogzstream(casefiles);
}
else
{
sprintf(casefiles, "%s/constant/polyMesh/points", casename.c_str());
ofstream outfile_pnts(casefiles);
outfile_pnts = new ofstream(casefiles);
}
if(compressed)
{
sprintf(casefiles, "%s/constant/polyMesh/faces.gz", casename.c_str());
outfile_faces = new ogzstream(casefiles);
}
else
{
sprintf(casefiles, "%s/constant/polyMesh/faces", casename.c_str());
ofstream outfile_faces(casefiles);
outfile_faces = new ofstream(casefiles);
}
if(compressed)
{
sprintf(casefiles, "%s/constant/polyMesh/owner.gz", casename.c_str());
outfile_own = new ogzstream(casefiles);
}
else
{
sprintf(casefiles, "%s/constant/polyMesh/owner", casename.c_str());
ofstream outfile_own(casefiles);
outfile_own = new ofstream(casefiles);
}
if(compressed)
{
sprintf(casefiles, "%s/constant/polyMesh/neighbour.gz", casename.c_str());
outfile_nei = new ogzstream(casefiles);
}
else
{
sprintf(casefiles, "%s/constant/polyMesh/neighbour", casename.c_str());
ofstream outfile_nei(casefiles);
outfile_nei = new ofstream(casefiles);
}
// Note... the boundary file is not compressed
sprintf(casefiles, "%s/constant/polyMesh/boundary", casename.c_str());
ofstream outfile_bnd(casefiles);
outfile_bnd = new ofstream(casefiles);
ResetTime();
@ -682,11 +725,11 @@ namespace netgen
// Write the "owner" file
if(outfile_own.good() && !error)
if(outfile_own->good() && !error)
{
cout << "Writing the owner file: ";
WriteOwnerFile(outfile_own);
outfile_own.close();
delete outfile_own;
cout << "Done! (Time Elapsed = " << GetTime() << " sec)\n";
}
else
@ -697,11 +740,11 @@ namespace netgen
// Write the "neighbour" file
if(outfile_nei.good() && !error)
if(outfile_nei->good() && !error)
{
cout << "Writing the neighbour file: ";
WriteNeighbourFile(outfile_nei);
outfile_nei.close();
delete outfile_nei;
cout << "Done! (Time Elapsed = " << GetTime() << " sec)\n";
}
else
@ -712,11 +755,11 @@ namespace netgen
// Write the "faces" file
if(outfile_faces.good() && !error)
if(outfile_faces->good() && !error)
{
cout << "Writing the faces file: ";
WriteFacesFile(outfile_faces, mesh);
outfile_faces.close();
delete outfile_faces;
cout << "Done! (Time Elapsed = " << GetTime() << " sec)\n";
}
else
@ -727,11 +770,11 @@ namespace netgen
// Write the "points" file
if(outfile_pnts.good() && !error)
if(outfile_pnts->good() && !error)
{
cout << "Writing the points file: ";
WritePointsFile(outfile_pnts,mesh);
outfile_pnts.close();
delete outfile_pnts;
cout << "Done! (Time Elapsed = " << GetTime() << " sec)\n";
}
else
@ -742,11 +785,11 @@ namespace netgen
// Write the "boundary" file
if(outfile_bnd.good() && !error)
if(outfile_bnd->good() && !error)
{
cout << "Writing the boundary file: ";
WriteBoundaryFile(outfile_bnd);
outfile_bnd.close();
delete outfile_bnd;
cout << "Done! (Time Elapsed = " << GetTime() << " sec)\n";
}
else

View File

@ -37,6 +37,7 @@ namespace netgen
"Gmsh Format", ".gmsh",
"Gmsh2 Format", ".gmsh2",
"OpenFOAM 1.5+ Format", "*",
"OpenFOAM 1.5+ Compressed", "*",
"JCMwave Format", ".jcm",
"TET Format", ".tet",
// { "Chemnitz Format" },
@ -126,7 +127,10 @@ bool WriteUserFormat (const string & format,
// Philippose - 25/10/2009
// Added OpenFOAM 1.5+ Mesh export capability
else if (format == "OpenFOAM 1.5+ Format")
WriteOpenFOAM15xFormat (mesh, filename);
WriteOpenFOAM15xFormat (mesh, filename, false);
else if (format == "OpenFOAM 1.5+ Compressed")
WriteOpenFOAM15xFormat (mesh, filename, true);
else if (format == "JCMwave Format")
WriteJCMFormat (mesh, geom, filename);
@ -304,17 +308,22 @@ void WriteSTLFormat (const Mesh & mesh,
{
cout << "\nWrite STL Surface Mesh" << endl;
ofstream outfile (filename.c_str());
ostream *outfile;
if(filename.substr(filename.length()-3,3) == ".gz")
outfile = new ogzstream(filename.c_str());
else
outfile = new ofstream(filename.c_str());
int i;
outfile.precision(10);
outfile->precision(10);
outfile << "solid" << endl;
*outfile << "solid" << endl;
for (i = 1; i <= mesh.GetNSE(); i++)
{
outfile << "facet normal ";
*outfile << "facet normal ";
const Point3d& p1 = mesh.Point(mesh.SurfaceElement(i).PNum(1));
const Point3d& p2 = mesh.Point(mesh.SurfaceElement(i).PNum(2));
const Point3d& p3 = mesh.Point(mesh.SurfaceElement(i).PNum(3));
@ -325,17 +334,17 @@ void WriteSTLFormat (const Mesh & mesh,
normal /= (normal.Length());
}
outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n";
outfile << "outer loop\n";
*outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n";
*outfile << "outer loop\n";
outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n";
outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n";
outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n";
*outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n";
*outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n";
*outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n";
outfile << "endloop\n";
outfile << "endfacet\n";
*outfile << "endloop\n";
*outfile << "endfacet\n";
}
outfile << "endsolid" << endl;
*outfile << "endsolid" << endl;
}
@ -356,9 +365,14 @@ void WriteSTLExtFormat (const Mesh & mesh,
{
cout << "\nWrite STL Surface Mesh (with separated boundary faces)" << endl;
ofstream outfile (filename.c_str());
ostream *outfile;
outfile.precision(10);
if(filename.substr(filename.length()-3,3) == ".gz")
outfile = new ogzstream(filename.c_str());
else
outfile = new ofstream(filename.c_str());
outfile->precision(10);
int numBCs = 0;
@ -393,7 +407,7 @@ void WriteSTLExtFormat (const Mesh & mesh,
// Now actually write the data to file
for(int bcInd = 1; bcInd <= faceBCs.Size(); bcInd++)
{
outfile << "solid Boundary_" << faceBCs.Elem(bcInd) << "\n";
*outfile << "solid Boundary_" << faceBCs.Elem(bcInd) << "\n";
for(int faceNr = 1;faceNr <= faceBCMapping.EntrySize(bcInd); faceNr++)
{
@ -402,7 +416,7 @@ void WriteSTLExtFormat (const Mesh & mesh,
for (int i = 0; i < faceSei.Size(); i++)
{
outfile << "facet normal ";
*outfile << "facet normal ";
const Point3d& p1 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(1));
const Point3d& p2 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(2));
const Point3d& p3 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(3));
@ -413,18 +427,18 @@ void WriteSTLExtFormat (const Mesh & mesh,
normal /= (normal.Length());
}
outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n";
outfile << "outer loop\n";
*outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n";
*outfile << "outer loop\n";
outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n";
outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n";
outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n";
*outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n";
*outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n";
*outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n";
outfile << "endloop\n";
outfile << "endfacet\n";
*outfile << "endloop\n";
*outfile << "endfacet\n";
}
}
outfile << "endsolid Boundary_" << faceBCs.Elem(bcInd) << "\n";
*outfile << "endsolid Boundary_" << faceBCs.Elem(bcInd) << "\n";
}
}

View File

@ -77,7 +77,8 @@ void WriteGmsh2Format (const Mesh & mesh,
// Added OpenFOAM 1.5+ Mesh Export support
extern
void WriteOpenFOAM15xFormat (const Mesh & mesh,
const string & casename);
const string & casename,
const bool compressed);
extern

View File

@ -249,6 +249,8 @@ loadmeshinifile;
set file [file nativename [tk_chooseDirectory -title "Elmer Mesh Export - Select Directory"]]
} elseif { $exportfiletype == "OpenFOAM 1.5+ Format"} {
set file [file nativename [tk_chooseDirectory -title "OpenFOAM 1.5+ Mesh Export - Select Case Directory"]]
} elseif { $exportfiletype == "OpenFOAM 1.5+ Compressed"} {
set file [file nativename [tk_chooseDirectory -title "OpenFOAM 1.5+ Mesh Export - Select Case Directory"]]
} else {
# set file [tk_getSaveFile -filetypes "{ \"$exportfiletype\" {$extension} }" ]
set file [tk_getSaveFile -filetypes "{ \"$exportfiletype\" {*}}" ]