| 1 |
#include "N2EDataTypes.h" |
| 2 |
#include "N2EExoWriter.h" |
| 3 |
#include "N2ENasReader.h" |
| 4 |
#include <chrono> |
| 5 |
#include <cstdlib> |
| 6 |
#include <fstream> |
| 7 |
#include <iostream> |
| 8 |
#include <memory> |
| 9 |
#include <string> |
| 10 |
|
| 11 |
using namespace N2EModules; |
| 12 |
|
| 13 |
namespace { |
| 14 |
void usageMessage(); |
| 15 |
void transferFailureMsg(const std::string &componentName); |
| 16 |
|
| 17 |
bool inputFileExists(const std::string &infile); |
| 18 |
bool outputFileNotExists(const std::string &outFile); |
| 19 |
} // namespace |
| 20 |
|
| 21 |
int main(int argc, char *argv[]) |
| 22 |
{ |
| 23 |
|
| 24 |
auto startTime = std::chrono::steady_clock::now(); |
| 25 |
|
| 26 |
std::string inFile; |
| 27 |
std::string outFile; |
| 28 |
|
| 29 |
if (argc == 2 || argc == 3) { |
| 30 |
|
| 31 |
inFile = std::string(argv[1]); |
| 32 |
|
| 33 |
if (argc == 3) { |
| 34 |
outFile = std::string(argv[2]); |
| 35 |
} |
| 36 |
else { |
| 37 |
outFile = std::string("a.exo"); |
| 38 |
} |
| 39 |
} |
| 40 |
else { |
| 41 |
|
| 42 |
usageMessage(); |
| 43 |
} |
| 44 |
|
| 45 |
if (!inputFileExists(inFile)) { |
| 46 |
|
| 47 |
std::cerr << "Input file does not exist.\n"; |
| 48 |
std::cerr << "Check paths and permissions.\n"; |
| 49 |
return EXIT_FAILURE; |
| 50 |
} |
| 51 |
|
| 52 |
if (!outputFileNotExists(outFile)) { |
| 53 |
std::cerr << "Output file already exists. This utility\ndoes not clobber existing files."; |
| 54 |
} |
| 55 |
|
| 56 |
auto readStart = std::chrono::steady_clock::now(); |
| 57 |
auto reader = std::make_unique<NasModules::N2ENasReader>(inFile); |
| 58 |
if (!reader->processFile()) { |
| 59 |
|
| 60 |
std::cerr |
| 61 |
<< "Unable to process the BDF file. Check the file and\nthe permissions. Bailing out.\n"; |
| 62 |
return EXIT_FAILURE; |
| 63 |
} |
| 64 |
auto readEnd = std::chrono::steady_clock::now(); |
| 65 |
|
| 66 |
size_t readNodes = reader->getNumberGridPts(); |
| 67 |
size_t readElements = reader->getNumberElems(); |
| 68 |
size_t readSections = reader->getNumberSects(); |
| 69 |
|
| 70 |
std::cout << "\n"; |
| 71 |
std::cout << "Entities read in:\n" |
| 72 |
<< " Number of Nodes: " << readNodes << "\n" |
| 73 |
<< " Number of Elements: " << readElements << "\n" |
| 74 |
<< " Number of Sections: " << readSections << "\n" |
| 75 |
<< " Read time: " |
| 76 |
<< std::chrono::duration_cast<std::chrono::milliseconds>(readEnd - readStart).count() |
| 77 |
<< "ms\n\n\n"; |
| 78 |
|
| 79 |
auto writeStart = std::chrono::steady_clock::now(); |
| 80 |
ExoModules::N2EExoWriter writer; |
| 81 |
|
| 82 |
if (!writer.createDB(outFile)) { |
| 83 |
|
| 84 |
std::cerr << " Unable to create output ExodisII DB. Check directory\n"; |
| 85 |
std::cerr << " permissions and try again.\n"; |
| 86 |
return EXIT_FAILURE; |
| 87 |
} |
| 88 |
|
| 89 |
if (!writer.setNodes(reader->getGridPoints())) { |
| 90 |
transferFailureMsg("grid points"); |
| 91 |
return EXIT_FAILURE; |
| 92 |
} |
| 93 |
|
| 94 |
if (!writer.setElements(reader->getElementList())) { |
| 95 |
transferFailureMsg("elements"); |
| 96 |
return EXIT_FAILURE; |
| 97 |
} |
| 98 |
|
| 99 |
if (!writer.setSections(reader->getSections())) { |
| 100 |
|
| 101 |
transferFailureMsg("blocks/sections"); |
| 102 |
return EXIT_FAILURE; |
| 103 |
} |
| 104 |
|
| 105 |
// Save some memory |
| 106 |
reader.reset(nullptr); |
| 107 |
|
| 108 |
if (!writer.writeFile()) { |
| 109 |
|
| 110 |
std::cerr << "There was problem writing out the ExodusII file. Do use it for calculations\n"; |
| 111 |
std::cerr << "Rerun utility. Do not use files from failed writes in calculations."; |
| 112 |
return EXIT_FAILURE; |
| 113 |
} |
| 114 |
auto writeEnd = std::chrono::steady_clock::now(); |
| 115 |
|
| 116 |
size_t blocksOut = writer.getBlocksOut(); |
| 117 |
size_t nodesOut = writer.getNodesOut(); |
| 118 |
size_t tetsOut = writer.getTetsOut(); |
| 119 |
size_t hexesOut = writer.getHexesOut(); |
| 120 |
|
| 121 |
std::cout << "Entities written in:\n"; |
| 122 |
std::cout << " Number of Nodes: " << nodesOut << "\n"; |
| 123 |
std::cout << " Number of TET4 Elements: " << tetsOut << "\n"; |
| 124 |
std::cout << " Number of HEX8 Elements: " << hexesOut << "\n"; |
| 125 |
std::cout << " Number of Blocks: " << blocksOut << "\n"; |
| 126 |
std::cout << " Write time: " |
| 127 |
<< std::chrono::duration_cast<std::chrono::milliseconds>(writeEnd - writeStart).count() |
| 128 |
<< "ms\n\n\n"; |
| 129 |
|
| 130 |
auto endTime = std::chrono::steady_clock::now(); |
| 131 |
|
| 132 |
std::cout << "Total Run time: " |
| 133 |
<< std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() |
| 134 |
<< "ms\n\n"; |
| 135 |
|
| 136 |
return EXIT_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
namespace { |
| 140 |
void usageMessage() |
| 141 |
{ |
| 142 |
|
| 143 |
using namespace std; |
| 144 |
|
| 145 |
std::cout << "Usage:\n"; |
| 146 |
std::cout << "nas2exo <pathtofile>/nasname.bdf <pathtoexo>/exoname.exo\n\n"; |
| 147 |
std::cout << "Notes:\n"; |
| 148 |
std::cout << " Output file designation is optional. If omitted a file\n"; |
| 149 |
std::cout << " named `a.exo` will be written in the location where this\n"; |
| 150 |
std::cout << " utility is invoked\n"; |
| 151 |
|
| 152 |
exit(EXIT_FAILURE); |
| 153 |
} |
| 154 |
|
| 155 |
void transferFailureMsg(const std::string &componentName) |
| 156 |
{ |
| 157 |
|
| 158 |
std::cerr << " Unable to set " << componentName << ". Something is really wrong\n"; |
| 159 |
std::cerr << " with the input file. Check a NASTRAN reference to insure\n"; |
| 160 |
std::cerr << " the BDF format was followed.\n"; |
| 161 |
} |
| 162 |
|
| 163 |
bool inputFileExists(const std::string &infile) |
| 164 |
{ |
| 165 |
std::ifstream inf(infile); |
| 166 |
return inf.good(); |
| 167 |
} |
| 168 |
|
| 169 |
bool outputFileNotExists(const std::string &outFile) { return !inputFileExists(outFile); } |
| 170 |
} // namespace |