1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#include <cstdio>
#include <iostream>
#include <sstream>
#include <exodusII.h>
#include "XdmfDomain.hpp"
#include "XdmfError.hpp"
#include "XdmfExodusReader.hpp"
#include "XdmfExodusWriter.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfHDF5Writer.hpp"
#include "XdmfReader.hpp"
#include "XdmfWriter.hpp"
namespace {
//
// print usage
//
inline void
printUsage(const char * programName)
{
std::cerr << "usage: " << programName << " "
<< "<input file> [output file]"
<< std::endl;
//
//
//
return;
}
//
// process command line
//
void
processCommandLine(std::string & inputFileName,
std::string & outputFileName,
int ac,
char * av[])
{
int c;
bool errorFlag = false;
while( (c=getopt(ac, av, "")) != -1 )
switch(c){
case '?':
errorFlag = true;
break;
}
if (optind >= ac)
errorFlag = true;
else {
inputFileName = av[optind];
++optind;
}
if (optind < ac) {
outputFileName = av[optind];
++optind;
}
//
// check errorFlag
//
if (errorFlag == true) {
printUsage(av[0]);
std::exit(EXIT_FAILURE);
}
}
}
/**
* XdmfExodusConverter is a command line utility for converting
* between Xdmf and Exodus files. If given an Xdmf file, the tool
* converts the file to Exodus and if given a path to an Exodus file,
* the tool converts the file to Xdmf.
*
* Usage:
* XdmfExodusConverter <path-of-file-to-convert>
* (Optional: <path-to-output-file>)
*
*/
int main(int argc, char* argv[])
{
std::string inputFileName = "";
std::string outputFileName = "";
processCommandLine(inputFileName,
outputFileName,
argc,
argv);
FILE * refFile = fopen(inputFileName.c_str(), "r");
if (refFile) {
// Success
fclose(refFile);
}
else {
std::cout << "Cannot open file: " << argv[1] << std::endl;
return 1;
}
std::string meshName;
if(outputFileName.compare("") == 0) {
meshName = inputFileName;
}
else {
meshName = outputFileName;
}
if(meshName.find_last_of("/\\") != std::string::npos) {
meshName = meshName.substr(meshName.find_last_of("/\\") + 1,
meshName.length());
}
if (meshName.rfind(".") != std::string::npos) {
meshName = meshName.substr(0, meshName.rfind("."));
}
int CPU_word_size = sizeof(double);
int IO_word_size = 0; // Get from file
float version;
const int exodusHandle =
ex_open(argv[1], EX_READ, &CPU_word_size, &IO_word_size, &version);
if(exodusHandle < 0) {
// Xdmf to Exodus
shared_ptr<XdmfReader> reader = XdmfReader::New();
shared_ptr<XdmfDomain> domain =
shared_dynamic_cast<XdmfDomain>(reader->read(inputFileName));
std::stringstream exodusFileName;
exodusFileName << meshName << ".exo";
shared_ptr<XdmfExodusWriter> writer = XdmfExodusWriter::New();
if(domain->getNumberUnstructuredGrids() == 1) {
const shared_ptr<XdmfUnstructuredGrid> grid =
domain->getUnstructuredGrid(0);
writer->write(exodusFileName.str(),
grid);
std::cout << "Wrote: " << exodusFileName.str() << std::endl;
}
else if(domain->getNumberGridCollections() == 1) {
const shared_ptr<XdmfGridCollection> grid =
domain->getGridCollection(0);
writer->write(exodusFileName.str(),
grid);
std::cout << "Wrote: " << exodusFileName.str() << std::endl;
}
else {
XdmfError::message(XdmfError::FATAL,
"Cannot find grid in Xdmf file to convert to "
"exodus.");
}
}
else {
// Exodus to Xdmf
std::stringstream heavyFileName;
heavyFileName << meshName << ".h5";
shared_ptr<XdmfHDF5Writer> heavyDataWriter =
XdmfHDF5Writer::New(heavyFileName.str());
heavyDataWriter->setReleaseData(true);
shared_ptr<XdmfExodusReader> exodusReader = XdmfExodusReader::New();
shared_ptr<XdmfUnstructuredGrid> readGrid =
exodusReader->read(inputFileName,
heavyDataWriter);
shared_ptr<XdmfDomain> newDomain = XdmfDomain::New();
newDomain->insert(readGrid);
std::stringstream xmlFileName;
xmlFileName << meshName << ".xmf";
shared_ptr<XdmfWriter> writer = XdmfWriter::New(xmlFileName.str(),
heavyDataWriter);
newDomain->accept(writer);
std::cout << "Wrote: " << xmlFileName.str() << std::endl;
}
}
|