14 Vertex(
int id,
double x,
double y,
double z):
28 Edge(
int id, vector<int> vert):
38 TwoDElement(
int id,
int type, vector<int> tags, vector<int> vert, vector<int> edge):
58 vector<Vertex>& pVertexList,
59 vector<TwoDElement>& pElementList)
61 cout <<
"Reading Ensite file..." << flush;
62 TiXmlDocument vEnsite(filename);
67 if (!vEnsite.LoadFile()) {
68 cout <<
"Unable to open file " << filename << endl;
72 vElmt = vEnsite.FirstChildElement(
"DIF");
73 if (!vElmt) cout <<
"Unable to find DIF tag" << endl;
74 vElmt = vElmt->FirstChildElement(
"DIFBody");
75 if (!vElmt) cout <<
"Unable to find DIFBody tag" << endl;
76 vElmt = vElmt->FirstChildElement(
"Volumes");
77 if (!vElmt) cout <<
"Unable to find Volumes tag" << endl;
78 vElmt = vElmt->FirstChildElement(
"Volume");
79 if (!vElmt) cout <<
"Unable to find Volume tag" << endl;
81 TiXmlElement * vVertices = vElmt->FirstChildElement(
"Vertices");
82 if (!vVertices) cout <<
"Unable to find Vertices tag" << endl;
83 int vNVertices = atoi(vVertices->Attribute(
"number"));
84 TiXmlElement * vPolygons = vElmt->FirstChildElement(
"Polygons");
85 if (!vPolygons) cout <<
"Unable to find Polygons tag" << endl;
86 int vNPolygons = atoi(vPolygons->Attribute(
"number"));
89 vTmp.write(vVertices->GetText(), strlen(vVertices->GetText()));
91 for (n = 0; n < vNVertices; ++n)
94 pVertexList.push_back(
Vertex(n,x,y,z));
99 vTmp.write(vPolygons->GetText(), strlen(vPolygons->GetText()));
101 for (n = 0; n < vNPolygons; ++n)
110 cout <<
"done." << endl;
113 int GetEdge(vector<int> &vert, vector<Edge>& edges,
int elm_type)
116 int size = edges.size();
117 for(i = 0; i < size; i++)
119 if( ((vert[0] == edges[i].vert[0])&&
120 (vert[1] == edges[i].vert[1]))
122 ((vert[1] == edges[i].vert[0])&&
123 (vert[0] == edges[i].vert[1])) )
130 vector<int> edgevert;
131 edgevert.push_back(vert[0]);
132 edgevert.push_back(vert[1]);
135 edgevert.push_back(vert[2]);
137 edges.push_back(
Edge(size,edgevert) );
142 vector<Edge>& pEdgeList,
143 vector<TwoDElement>& pElementList)
147 cout <<
"Creating edge list..." << flush;
148 for (i = 0; i < pElementList.size(); ++i)
150 for (j = 0; j < 3; ++j)
153 verts.push_back(pElementList[i].verts[j%3]);
154 verts.push_back(pElementList[i].verts[(j+1)%3]);
155 edgeid =
GetEdge(verts, pEdgeList, 2);
156 pElementList[i].edges.push_back(edgeid);
159 cout <<
"done." << endl;
162 void WriteToXMLFile(
const char* outfile,
const vector<Vertex> & nodes,
const vector<Edge> & edges,
163 const vector<TwoDElement> & twoDElements)
165 cout <<
"Writing XML file..." << flush;
168 TiXmlDeclaration* decl =
new TiXmlDeclaration(
"1.0",
"utf-8",
"");
169 doc.LinkEndChild( decl );
171 TiXmlElement * root =
new TiXmlElement(
"NEKTAR" );
172 doc.LinkEndChild( root );
176 TiXmlElement * geomTag =
new TiXmlElement(
"GEOMETRY" );
177 geomTag->SetAttribute(
"DIM",
"2");
178 geomTag->SetAttribute(
"SPACE",
"3");
179 root->LinkEndChild( geomTag );
184 TiXmlElement* verTag =
new TiXmlElement(
"VERTEX" );
186 for(
int i = 0; i < nodes.size(); ++i ) {
188 s << scientific << setprecision(3) << nodes[i].x <<
" "
189 << nodes[i].y <<
" " << nodes[i].z;
190 TiXmlElement * v =
new TiXmlElement(
"V" );
191 v->SetAttribute(
"ID",nodes[i].
id);
192 v->LinkEndChild(
new TiXmlText(s.str()) );
193 verTag->LinkEndChild(v);
195 geomTag->LinkEndChild( verTag );
202 verTag =
new TiXmlElement(
"EDGE" );
204 for(
int i = 0; i < edges.size(); ++i ) {
207 s << setw(5) << edges[i].vert[0] <<
" " << edges[i].vert[1] <<
" ";
208 TiXmlElement * e =
new TiXmlElement(
"E" );
209 e->SetAttribute(
"ID",edgecnt++);
210 e->LinkEndChild(
new TiXmlText(s.str()) );
211 verTag->LinkEndChild(e);
213 geomTag->LinkEndChild( verTag );
219 verTag =
new TiXmlElement(
"ELEMENT" );
222 for(
int i=0; i<twoDElements.size(); ++i){
224 for(
int j=0; j<twoDElements[i].edges.size(); ++j){
225 st << setw(5) << twoDElements[i].edges[j] <<
" ";
228 TiXmlElement *elm_tag;
229 elm_tag =
new TiXmlElement(
"T");
230 elm_tag->SetAttribute(
"ID", elmcnt++);
231 elm_tag->LinkEndChild(
new TiXmlText(st.str()) );
232 verTag->LinkEndChild(elm_tag);
234 geomTag->LinkEndChild( verTag );
240 verTag =
new TiXmlElement(
"COMPOSITE");
246 st <<
" T[0-" << twoDElements.size()-1 <<
"]";
248 TiXmlElement *comp_tag =
new TiXmlElement(
"C");
249 comp_tag->SetAttribute(
"ID",
"0");
250 comp_tag->LinkEndChild(
new TiXmlText(st.str()) );
251 verTag->LinkEndChild(comp_tag);
252 geomTag->LinkEndChild( verTag );
255 TiXmlElement * domain =
new TiXmlElement (
"DOMAIN" );
256 domain->LinkEndChild(
new TiXmlText(
" C[0] " ));
257 geomTag->LinkEndChild( domain );
259 doc.SaveFile(outfile );
261 cout <<
"done." << endl;
264 void WriteMshFile(
const char* outfile,
const vector<Vertex> & nodes,
const vector<Edge> & edges,
265 const vector<TwoDElement> & twoDElements)
267 cout <<
"Write GMSH file..." << flush;
269 ofstream out(outfile);
271 out <<
"$MeshFormat" << endl;
272 out <<
"2.1 0 8" << endl;
273 out <<
"$EndMeshFormat" << endl;
274 out <<
"$Nodes" << endl;
275 out << nodes.size() << endl;
276 for (n = 0; n < nodes.size(); ++n)
278 out << n+1 <<
" " << nodes[n].x <<
" " << nodes[n].y <<
" "
279 << nodes[n].z << endl;
281 out <<
"$EndNodes" << endl;
282 out <<
"$Elements" << endl;
283 out << twoDElements.size() << endl;
284 for (n = 0; n < twoDElements.size(); ++n)
286 out << n+1 <<
" 2 3 1 0 0 " << twoDElements[n].verts[0] + 1 <<
" "
287 << twoDElements[n].verts[1] + 1
288 <<
" " << twoDElements[n].verts[2] + 1 << endl;
290 out <<
"$EndElements" << endl;
291 cout <<
"done." << endl;
294 int main(
int argc,
char *argv[])
298 fprintf(stderr,
"Usage: EnsiteToXml ensite.xml session.xml mesh.msh\n");
302 vector<Vertex> vVertexList;
303 vector<Edge> vEdgeList;
304 vector<TwoDElement> vElementList;
306 readEnsite(argv[1], vVertexList, vElementList);
309 WriteMshFile(argv[3], vVertexList, vEdgeList, vElementList);