1 /*******************************************************************/
2 /*                               XDMF                              */
3 /*                   eXtensible Data Model and Format              */
4 /*                                                                 */
5 /*  Id : Id  */
6 /*  Date : $Date$ */
7 /*  Version : $ $ */
8 /*                                                                 */
9 /*  Author:                                                        */
10 /*     Kenneth Leiter                                              */
11 /*     kenneth.leiter@arl.army.mil                                 */
12 /*     US Army Research Laboratory                                 */
13 /*     Aberdeen Proving Ground, MD                                 */
14 /*                                                                 */
15 /*     Copyright @ 2009 US Army Research Laboratory                */
16 /*     All Rights Reserved                                         */
17 /*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
18 /*                                                                 */
19 /*     This software is distributed WITHOUT ANY WARRANTY; without  */
20 /*     even the implied warranty of MERCHANTABILITY or FITNESS     */
21 /*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
22 /*     for more information.                                       */
23 /*                                                                 */
24 /*******************************************************************/
25 
26 #include "XdmfExodusReader.h"
27 #include "XdmfExodusWriter.h"
28 #include <exodusII.h>
29 
30 #include <sstream>
31 
32 /**
33  * XdmfExodusConverter is a command line utility for converting between Xdmf and ExodusII files.
34  * If given a path to an Xdmf file, the tool converts the file to Exodus and if given a path
35  * to an Exodus file, the tool converts the file to Xdmf.
36  *
37  * Usage:
38  *     XdmfExodusConverter <path-of-file-to-convert> (Optional: <path-to-output-file>)
39  *
40  */
41 int
main(int argc,char * argv[])42 main(int argc, char* argv[])
43 {
44   std::string usage = "Converts an Exodus II file to XDMF or converts an XDMF file to Exodus II: \n \n Usage: \n \n   XdmfExodusConverter <path-of-file-to-convert> (Optional: <path-to-output-file>)";
45   std::string meshName = "";
46 
47   if (argc < 2)
48   {
49     cout << usage << endl;
50     return 1;
51   }
52 
53   if (argc >= 2)
54   {
55     FILE * refFile = fopen(argv[1], "r");
56     if (refFile)
57     {
58       // Success
59       meshName = argv[1];
60       fclose(refFile);
61     }
62     else
63     {
64       cout << "Cannot open file: " << argv[1] << endl;
65       return 1;
66     }
67 
68     if (argc >= 3)
69     {
70       meshName = argv[2];
71     }
72   }
73 
74   if(meshName.find_last_of("/\\") != std::string::npos)
75   {
76     meshName = meshName.substr(meshName.find_last_of("/\\")+1, meshName.length());
77   }
78 
79   if (meshName.rfind(".") != std::string::npos)
80   {
81     meshName = meshName.substr(0, meshName.rfind("."));
82   }
83 
84   int CPU_word_size = sizeof(double);
85   int IO_word_size = 0; // Get from file
86   float version;
87   int exodusHandle = ex_open(argv[1], EX_READ, &CPU_word_size, &IO_word_size, &version);
88   if(exodusHandle < 0)
89   {
90     // Xdmf to Exodus
91     XdmfDOM dom;
92     XdmfInt32 error = dom.Parse(argv[1]);
93     if(error == XDMF_FAIL)
94     {
95       std::cout << "File does not appear to be either an ExodusII or Xdmf file" << std::endl;
96       return 1;
97     }
98     XdmfXmlNode gridElement = dom.FindElementByPath("/Xdmf/Domain/Grid");
99     if(gridElement == NULL)
100     {
101       std::cout << "Cannot parse Xdmf file!" << std::endl;
102       return 1;
103     }
104     XdmfGrid * grid = new XdmfGrid();
105     grid->SetDOM(&dom);
106     grid->SetElement(gridElement);
107     grid->Update();
108 
109     std::stringstream outputFileStream;
110     outputFileStream << meshName << ".exo";
111 
112     XdmfExodusWriter writer;
113     writer.write(outputFileStream.str().c_str(), grid);
114 
115     cout << "Wrote: " << outputFileStream.str() << endl;
116 
117     delete grid;
118   }
119   else
120   {
121     // Exodus To Xdmf
122 
123     // Initialize xdmf file
124     XdmfDOM dom;
125     XdmfRoot root;
126     XdmfDomain domain;
127 
128     root.SetDOM(&dom);
129     root.Build();
130     root.Insert(&domain);
131 
132     XdmfExodusReader reader;
133     XdmfGrid * mesh = reader.read(argv[1], &domain);
134 
135     std::stringstream outputFileStream;
136     outputFileStream << meshName << ".xmf";
137     std::string outputFile = outputFileStream.str();
138 
139     // Set heavy data set names for geometry and topology
140     mesh->SetName(meshName.c_str());
141 
142     std::stringstream heavyPointName;
143     heavyPointName << meshName << ".h5:/XYZ";
144     mesh->GetGeometry()->GetPoints()->SetHeavyDataSetName(heavyPointName.str().c_str());
145 
146     std::stringstream heavyConnName;
147     heavyConnName << meshName << ".h5:/Connections";
148     mesh->GetTopology()->GetConnectivity()->SetHeavyDataSetName(heavyConnName.str().c_str());
149 
150     // Set heavy data set names for mesh attributes and sets
151     for(int i=0; i<mesh->GetNumberOfAttributes(); i++)
152     {
153       std::stringstream heavyAttrName;
154       heavyAttrName << meshName << ".h5:/Attribute/" << mesh->GetAttribute(i)->GetAttributeCenterAsString() << "/" << mesh->GetAttribute(i)->GetName();
155       mesh->GetAttribute(i)->GetValues()->SetHeavyDataSetName(heavyAttrName.str().c_str());
156     }
157 
158     for(int i=0; i<mesh->GetNumberOfSets(); i++)
159     {
160       std::stringstream heavySetName;
161       heavySetName << meshName << ".h5:/Set/" << mesh->GetSets(i)->GetSetTypeAsString() << "/" << mesh->GetSets(i)->GetName();
162       mesh->GetSets(i)->GetIds()->SetHeavyDataSetName(heavySetName.str().c_str());
163     }
164 
165     mesh->Build();
166     dom.Write(outputFile.c_str());
167 
168     cout << "Wrote: " << outputFile << endl;
169 
170     // cleanup
171     delete mesh;
172   }
173 
174   return 0;
175 }
176