Path: blob/devel/ElmerGUI/netgen/libsrc/interface/writeuser.cpp
3206 views
//1// Write user dependent output file2//34#include <mystdlib.h>56#include <myadt.hpp>7#include <linalg.hpp>8#include <csg.hpp>9#include <geometry2d.hpp>10#include <meshing.hpp>1112namespace netgen13{14#include "writeuser.hpp"151617void RegisterUserFormats (ARRAY<const char*> & names)18{19const char *types[] =20{21"Neutral Format",22"Surface Mesh Format" ,23"DIFFPACK Format",24"TecPlot Format",25"Tochnog Format",26"Abaqus Format",27"Fluent Format",28"Permas Format",29"FEAP Format",30"Elmer Format",31"STL Format",32"VRML Format",33"Gmsh Format",34"JCMwave Format",35"TET Format",36// { "Chemnitz Format" },37038};3940for (int i = 0; types[i]; i++)41names.Append (types[i]);42}43444546bool WriteUserFormat (const string & format,47const Mesh & mesh,48const CSGeometry & geom,49const string & filename)50{51PrintMessage (1, "Export mesh to file ", filename,52", format is ", format);5354if (format == "Neutral Format")55WriteNeutralFormat (mesh, geom, filename);5657else if (format == "Surface Mesh Format")58WriteSurfaceFormat (mesh, filename);5960else if (format == "DIFFPACK Format")61WriteDiffPackFormat (mesh, geom, filename);6263else if (format == "Tochnog Format")64WriteTochnogFormat (mesh, filename);6566else if (format == "TecPlot Format")67cerr << "ERROR: TecPlot format currently out of order" << endl;68// WriteTecPlotFormat (mesh, geom, filename);6970else if (format == "Abaqus Format")71WriteAbaqusFormat (mesh, filename);7273else if (format == "Fluent Format")74WriteFluentFormat (mesh, filename);7576else if (format == "Permas Format")77WritePermasFormat (mesh, filename);7879else if (format == "FEAP Format")80WriteFEAPFormat (mesh, filename);8182else if (format == "Elmer Format")83WriteElmerFormat (mesh, filename);8485else if (format == "STL Format")86WriteSTLFormat (mesh, filename);8788else if (format == "VRML Format")89WriteVRMLFormat (mesh, 1, filename);9091else if (format == "Fepp Format")92WriteFEPPFormat (mesh, geom, filename);9394else if (format == "EdgeElement Format")95WriteEdgeElementFormat (mesh, geom, filename);9697else if (format == "Chemnitz Format")98WriteUserChemnitz (mesh, filename);99100else if (format == "Gmsh Format")101WriteGmshFormat (mesh, geom, filename);102103else if (format == "JCMwave Format")104WriteJCMFormat (mesh, geom, filename);105106#ifdef OLIVER107else if (format == "TET Format")108WriteTETFormat( mesh, filename);//, "High Frequency" );109#endif110111else112{113return 1;114}115116return 0;117}118119120121122/*123* Neutral mesh format124* points, elements, surface elements125*/126127void WriteNeutralFormat (const Mesh & mesh,128const CSGeometry & geom,129const string & filename)130{131cout << "write neutral, new" << endl;132int np = mesh.GetNP();133int ne = mesh.GetNE();134int nse = mesh.GetNSE();135int nseg = mesh.GetNSeg();136int i, j;137138int inverttets = mparam.inverttets;139int invertsurf = mparam.inverttrigs;140141ofstream outfile (filename.c_str());142143outfile.precision(6);144outfile.setf (ios::fixed, ios::floatfield);145outfile.setf (ios::showpoint);146147outfile << np << "\n";148149for (i = 1; i <= np; i++)150{151const Point3d & p = mesh.Point(i);152153outfile.width(10);154outfile << p.X() << " ";155outfile.width(9);156outfile << p.Y() << " ";157if (mesh.GetDimension() == 3)158{159outfile.width(9);160outfile << p.Z();161}162outfile << "\n";163}164165if (mesh.GetDimension() == 3)166{167outfile << ne << "\n";168for (i = 1; i <= ne; i++)169{170Element el = mesh.VolumeElement(i);171if (inverttets)172el.Invert();173outfile.width(4);174outfile << el.GetIndex() << " ";175for (j = 1; j <= el.GetNP(); j++)176{177outfile << " ";178outfile.width(8);179outfile << el.PNum(j);180}181outfile << "\n";182}183}184185outfile << nse << "\n";186for (i = 1; i <= nse; i++)187{188Element2d el = mesh.SurfaceElement(i);189if (invertsurf)190el.Invert();191outfile.width(4);192outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";193for (j = 1; j <= el.GetNP(); j++)194{195outfile << " ";196outfile.width(8);197outfile << el.PNum(j);198}199outfile << "\n";200}201202203if (mesh.GetDimension() == 2)204{205outfile << nseg << "\n";206for (i = 1; i <= nseg; i++)207{208const Segment & seg = mesh.LineSegment(i);209outfile.width(4);210outfile << seg.si << " ";211212outfile << " ";213outfile.width(8);214outfile << seg.p1;215outfile << " ";216outfile.width(8);217outfile << seg.p2;218219outfile << "\n";220}221}222}223224225226227228229230231232void WriteSurfaceFormat (const Mesh & mesh,233const string & filename)234{235// surface mesh236int i, j;237238cout << "Write Surface Mesh" << endl;239240ofstream outfile (filename.c_str());241242outfile << "surfacemesh" << endl;243244outfile << mesh.GetNP() << endl;245for (i = 1; i <= mesh.GetNP(); i++)246{247for (j = 0; j < 3; j++)248{249outfile.width(10);250outfile << mesh.Point(i)(j) << " ";251}252outfile << endl;253}254outfile << mesh.GetNSE() << endl;255for (i = 1; i <= mesh.GetNSE(); i++)256{257for (j = 1; j <= 3; j++)258{259outfile.width(8);260outfile << mesh.SurfaceElement(i).PNum(j);261}262outfile << endl;263}264}265266267268269270/*271* save surface mesh as STL file272*/273274void WriteSTLFormat (const Mesh & mesh,275const string & filename)276{277cout << "\nWrite STL Surface Mesh" << endl;278279ofstream outfile (filename.c_str());280281int i;282283outfile.precision(10);284285outfile << "solid" << endl;286287for (i = 1; i <= mesh.GetNSE(); i++)288{289outfile << "facet normal ";290const Point3d& p1 = mesh.Point(mesh.SurfaceElement(i).PNum(1));291const Point3d& p2 = mesh.Point(mesh.SurfaceElement(i).PNum(2));292const Point3d& p3 = mesh.Point(mesh.SurfaceElement(i).PNum(3));293294Vec3d normal = Cross(p2-p1,p3-p1);295if (normal.Length() != 0)296{297normal /= (normal.Length());298}299300outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n";301outfile << "outer loop\n";302303outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n";304outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n";305outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n";306307outfile << "endloop\n";308outfile << "endfacet\n";309}310outfile << "endsolid" << endl;311}312313314315316317/*318*319* write surface mesh as VRML file320*321*/322323void WriteVRMLFormat (const Mesh & mesh,324bool faces,325const string & filename)326{327328if (faces)329330{331// Output in VRML, IndexedFaceSet is used332// Bartosz Sawicki <[email protected]>333334int np = mesh.GetNP();335int nse = mesh.GetNSE();336int i, j;337338ofstream outfile (filename.c_str());339340outfile.precision(6);341outfile.setf (ios::fixed, ios::floatfield);342outfile.setf (ios::showpoint);343344outfile << "#VRML V2.0 utf8 \n"345"Background {\n"346" skyColor [1 1 1]\n"347" groundColor [1 1 1]\n"348"}\n"349"Group{ children [\n"350"Shape{ \n"351"appearance Appearance { material Material { }} \n"352"geometry IndexedFaceSet { \n"353"coord Coordinate { point [ \n";354355356for (i = 1; i <= np; i++)357{358const Point3d & p = mesh.Point(i);359outfile.width(10);360outfile << p.X() << " ";361outfile << p.Y() << " ";362outfile << p.Z() << " \n";363}364365outfile << " ] } \n"366"coordIndex [ \n";367368for (i = 1; i <= nse; i++)369{370const Element2d & el = mesh.SurfaceElement(i);371372for (j = 1; j <= 3; j++)373{374outfile.width(8);375outfile << el.PNum(j)-1;376}377outfile << " -1 \n";378}379380outfile << " ] \n";381382//define number and RGB definitions of colors383outfile << "color Color { color [1 0 0, 0 1 0, 0 0 1, 1 1 0]} \n"384"colorIndex [\n";385386for (i = 1; i <= nse; i++)387{388outfile << mesh.GetFaceDescriptor(mesh.SurfaceElement(i).GetIndex ()).BCProperty();389outfile << endl;390}391392outfile << " ] \n"393"colorPerVertex FALSE \n"394"creaseAngle 0 \n"395"solid FALSE \n"396"ccw FALSE \n"397"convex TRUE \n"398"} } # end of Shape\n"399"] }\n";400401} /* end of VRMLFACES */402403404else405406{407// Output in VRML, IndexedLineSet is used408// Bartosz Sawicki <[email protected]>409410int np = mesh.GetNP();411int nse = mesh.GetNSE();412int i, j;413414ofstream outfile (filename.c_str());415416outfile.precision(6);417outfile.setf (ios::fixed, ios::floatfield);418outfile.setf (ios::showpoint);419420outfile << "#VRML V2.0 utf8 \n"421"Background {\n"422" skyColor [1 1 1]\n"423" groundColor [1 1 1]\n"424"}\n"425"Group{ children [\n"426"Shape{ \n"427"appearance Appearance { material Material { }} \n"428"geometry IndexedLineSet { \n"429"coord Coordinate { point [ \n";430431432for (i = 1; i <= np; i++)433{434const Point3d & p = mesh.Point(i);435outfile.width(10);436outfile << p.X() << " ";437outfile << p.Y() << " ";438outfile << p.Z() << " \n";439}440441outfile << " ] } \n"442"coordIndex [ \n";443444for (i = 1; i <= nse; i++)445{446const Element2d & el = mesh.SurfaceElement(i);447448for (j = 1; j <= 3; j++)449{450outfile.width(8);451outfile << el.PNum(j)-1;452}453outfile.width(8);454outfile << el.PNum(1)-1;455outfile << " -1 \n";456}457458outfile << " ] \n";459460/* Uncomment if you want color mesh461outfile << "color Color { color [1 1 1, 0 1 0, 0 0 1, 1 1 0]} \n"462"colorIndex [\n";463464for (i = 1; i <= nse; i++)465{466outfile << mesh.GetFaceDescriptor(mesh.SurfaceElement(i).GetIndex ()).BCProperty();467outfile << endl;468}469470outfile << " ] \n"471*/472outfile << "colorPerVertex FALSE \n"473"} } #end of Shape\n"474"] } \n";475476}477478}479480481482483484485/*486* FEPP .. a finite element package developed at University Linz, Austria487*/488void WriteFEPPFormat (const Mesh & mesh,489const CSGeometry & geom,490const string & filename)491{492493ofstream outfile (filename.c_str());494495if (mesh.GetDimension() == 3)496497{498499// output for FEPP500501int np = mesh.GetNP();502int ne = mesh.GetNE();503int nse = mesh.GetNSE();504int ns = mesh.GetNFD();505int i, j;506507outfile.precision(5);508outfile.setf (ios::fixed, ios::floatfield);509outfile.setf (ios::showpoint);510511outfile << "volumemesh4" << endl;512outfile << nse << endl;513for (i = 1; i <= nse; i++)514{515const Element2d & el = mesh.SurfaceElement(i);516517// int facenr = mesh.facedecoding.Get(el.GetIndex()).surfnr;518outfile.width(4);519outfile << el.GetIndex() << " ";520outfile.width(4);521// outfile << mesh.GetFaceDescriptor(el.GetIndex()).BCProperty() << " ";522outfile << mesh.GetFaceDescriptor(el.GetIndex()).BCProperty() << " ";523outfile.width(4);524outfile << el.GetNP() << " ";525for (j = 1; j <= el.GetNP(); j++)526{527outfile.width(8);528outfile << el.PNum(j);529}530outfile << "\n";531}532533534outfile << ne << "\n";535for (i = 1; i <= ne; i++)536{537const Element & el = mesh.VolumeElement(i);538outfile.width(4);539outfile << el.GetIndex() << " ";540outfile.width(4);541outfile << el.GetNP() << " ";542for (j = 1; j <= el.GetNP(); j++)543{544outfile.width(8);545outfile << el.PNum(j);546}547outfile << "\n";548}549550outfile << np << "\n";551for (i = 1; i <= np; i++)552{553const Point3d & p = mesh.Point(i);554555outfile.width(10);556outfile << p.X() << " ";557outfile.width(9);558outfile << p.Y() << " ";559outfile.width(9);560outfile << p.Z() << "\n";561}562563/*564if (typ == WRITE_FEPPML)565{566int nbn = mesh.mlbetweennodes.Size();567outfile << nbn << "\n";568for (i = 1; i <= nbn; i++)569outfile << mesh.mlbetweennodes.Get(i).I1() << " "570<< mesh.mlbetweennodes.Get(i).I2() << "\n";571572573// int ncon = mesh.connectedtonode.Size();574// outfile << ncon << "\n";575// for (i = 1; i <= ncon; i++)576// outfile << i << " " << mesh.connectedtonode.Get(i) << endl;577}578*/579580581// write CSG surfaces582if (&geom && geom.GetNSurf() >= ns)583{584outfile << ns << endl;585for (i = 1; i <= ns; i++)586geom.GetSurface(mesh.GetFaceDescriptor(i).SurfNr())->Print(outfile);587}588else589outfile << "0" << endl;590}591592593else594595{ // 2D fepp format596597;598/*599extern SplineGeometry2d * geometry2d;600if (geometry2d)601Save2DMesh (mesh, &geometry2d->GetSplines(), outfile);602else603Save2DMesh (mesh, 0, outfile);604*/605}606}607608609610611612613/*614* Edge element mesh format615* points, elements, edges616*/617618void WriteEdgeElementFormat (const Mesh & mesh,619const CSGeometry & geom,620const string & filename)621{622cout << "write edge element format" << endl;623624const MeshTopology * top = &mesh.GetTopology();625int npoints = mesh.GetNP();626int nelements = mesh.GetNE();627int nsurfelem = mesh.GetNSE();628int nedges = top->GetNEdges();629int i, j;630631int inverttets = mparam.inverttets;632int invertsurf = mparam.inverttrigs;633ARRAY<int> edges;634635ofstream outfile (filename.c_str());636637outfile.precision(6);638outfile.setf (ios::fixed, ios::floatfield);639outfile.setf (ios::showpoint);640641642// vertices with coordinates643outfile << npoints << "\n";644for (i = 1; i <= npoints; i++)645{646const Point3d & p = mesh.Point(i);647648outfile.width(10);649outfile << p.X() << " ";650outfile.width(9);651outfile << p.Y() << " ";652outfile.width(9);653outfile << p.Z() << "\n";654}655656// element - edge - list657outfile << nelements << " " << nedges << "\n";658for (i = 1; i <= nelements; i++)659{660Element el = mesh.VolumeElement(i);661if (inverttets)662el.Invert();663outfile.width(4);664outfile << el.GetIndex() << " ";665outfile.width(8);666outfile << el.GetNP();667for (j = 1; j <= el.GetNP(); j++)668{669outfile << " ";670outfile.width(8);671outfile << el.PNum(j);672}673674top->GetElementEdges(i,edges);675outfile << endl << " ";676outfile.width(8);677outfile << edges.Size();678for (j=1; j <= edges.Size(); j++)679{680outfile << " ";681outfile.width(8);682outfile << edges[j-1];683}684outfile << "\n";685686// orientation:687top->GetElementEdgeOrientations(i,edges);688outfile << " ";689for (j=1; j <= edges.Size(); j++)690{691outfile << " ";692outfile.width(8);693outfile << edges[j-1];694}695outfile << "\n";696}697698// surface element - edge - list (with boundary conditions)699outfile << nsurfelem << "\n";700for (i = 1; i <= nsurfelem; i++)701{702Element2d el = mesh.SurfaceElement(i);703if (invertsurf)704el.Invert();705outfile.width(4);706outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";707outfile.width(8);708outfile << el.GetNP();709for (j = 1; j <= el.GetNP(); j++)710{711outfile << " ";712outfile.width(8);713outfile << el.PNum(j);714}715716top->GetSurfaceElementEdges(i,edges);717outfile << endl << " ";718outfile.width(8);719outfile << edges.Size();720for (j=1; j <= edges.Size(); j++)721{722outfile << " ";723outfile.width(8);724outfile << edges[j-1];725}726outfile << "\n";727}728729730int v1, v2;731// edge - vertex - list732outfile << nedges << "\n";733for (i=1; i <= nedges; i++)734{735top->GetEdgeVertices(i,v1,v2);736outfile.width(4);737outfile << v1;738outfile << " ";739outfile.width(8);740outfile << v2 << endl;741}742}743744745746747748749750751752#ifdef OLDSTYLE_WRITE753754755void WriteFile (int typ,756const Mesh & mesh,757const CSGeometry & geom,758const char * filename,759const char * geomfile,760double h)761{762763764int inverttets = mparam.inverttets;765int invertsurf = mparam.inverttrigs;766767768769770771772773774if (typ == WRITE_EDGEELEMENT)775{776// write edge element file777// Peter Harscher, ETHZ778779cout << "Write Edge-Element Format" << endl;780781ofstream outfile (filename);782783int i, j;784int ned;785786// hash table representing edges;787INDEX_2_HASHTABLE<int> edgeht(mesh.GetNP());788789// list of edges790ARRAY<INDEX_2> edgelist;791792// edge (point) on boundary ?793BitArray bedge, bpoint(mesh.GetNP());794795static int eledges[6][2] = { { 1, 2 } , { 1, 3 } , { 1, 4 },796{ 2, 3 } , { 2, 4 } , { 3, 4 } };797798// fill hashtable (point1, point2) ----> edgenr799for (i = 1; i <= mesh.GetNE(); i++)800{801const Element & el = mesh.VolumeElement (i);802INDEX_2 edge;803for (j = 1; j <= 6; j++)804{805edge.I1() = el.PNum (eledges[j-1][0]);806edge.I2() = el.PNum (eledges[j-1][1]);807edge.Sort();808809if (!edgeht.Used (edge))810{811edgelist.Append (edge);812edgeht.Set (edge, edgelist.Size());813}814}815}816817818// set bedges, bpoints819bedge.SetSize (edgelist.Size());820bedge.Clear();821bpoint.Clear();822823for (i = 1; i <= mesh.GetNSE(); i++)824{825const Element2d & sel = mesh.SurfaceElement(i);826for (j = 1; j <= 3; j++)827{828bpoint.Set (sel.PNum(j));829830INDEX_2 edge;831edge.I1() = sel.PNum(j);832edge.I2() = sel.PNum(j%3+1);833edge.Sort();834835bedge.Set (edgeht.Get (edge));836}837}838839840841outfile << mesh.GetNE() << endl;842// write element ---> point843for (i = 1; i <= mesh.GetNE(); i++)844{845const Element & el = mesh.VolumeElement(i);846847outfile.width(8);848outfile << i;849for (j = 1; j <= 4; j++)850{851outfile.width(8);852outfile << el.PNum(j);853}854outfile << endl;855}856857// write element ---> edge858for (i = 1; i <= mesh.GetNE(); i++)859{860const Element & el = mesh.VolumeElement (i);861INDEX_2 edge;862for (j = 1; j <= 6; j++)863{864edge.I1() = el.PNum (eledges[j-1][0]);865edge.I2() = el.PNum (eledges[j-1][1]);866edge.Sort();867868outfile.width(8);869outfile << edgeht.Get (edge);870}871outfile << endl;872}873874// write points875outfile << mesh.GetNP() << endl;876outfile.precision (6);877for (i = 1; i <= mesh.GetNP(); i++)878{879const Point3d & p = mesh.Point(i);880881for (j = 1; j <= 3; j++)882{883outfile.width(8);884outfile << p.X(j);885}886outfile << " "887<< (bpoint.Test(i) ? "1" : 0) << endl;888}889890// write edges891outfile << edgelist.Size() << endl;892for (i = 1; i <= edgelist.Size(); i++)893{894outfile.width(8);895outfile << edgelist.Get(i).I1();896outfile.width(8);897outfile << edgelist.Get(i).I2();898outfile << " "899<< (bedge.Test(i) ? "1" : "0") << endl;900}901}902903904905906}907#endif908}909910911912