| 1 |
/* |
| 2 |
* N2EExoWriter.cpp |
| 3 |
* |
| 4 |
* Created on: Oct 13, 2020 |
| 5 |
* Author: rjmoral |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "N2EExoWriter.h" |
| 9 |
#include "vector_data.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <cstring> |
| 13 |
#include <iostream> |
| 14 |
#include <tuple> |
| 15 |
|
| 16 |
namespace ExoModules { |
| 17 |
|
| 18 |
N2EExoWriter::~N2EExoWriter() |
| 19 |
{ |
| 20 |
if (this->exoFileID > 0) { |
| 21 |
ex_close(this->exoFileID); |
| 22 |
this->exoFileID = 0; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
bool N2EExoWriter::createDB(const std::string &name) |
| 27 |
{ |
| 28 |
this->exoFileID = ex_create(name.c_str(), EX_CLOBBER, &this->CPU_ws, &this->IO_ws); |
| 29 |
return this->exoFileID > 0; |
| 30 |
} |
| 31 |
|
| 32 |
bool N2EExoWriter::setNodes(const std::vector<gridType> &gridpts) |
| 33 |
{ |
| 34 |
try { |
| 35 |
this->gridList.reserve(gridpts.capacity()); |
| 36 |
this->gridList = gridpts; |
| 37 |
} |
| 38 |
catch (...) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
bool N2EExoWriter::setElements(const std::vector<elementType> &elist) |
| 46 |
{ |
| 47 |
try { |
| 48 |
this->elementList.reserve(elist.capacity()); |
| 49 |
this->elementList = elist; |
| 50 |
} |
| 51 |
catch (...) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
bool N2EExoWriter::setSections(const std::vector<sectionType> &sList) |
| 59 |
{ |
| 60 |
try { |
| 61 |
this->sections.reserve(sList.capacity()); |
| 62 |
this->sections = sList; |
| 63 |
} |
| 64 |
catch (...) { |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
void N2EExoWriter::setModelTitle(const std::string &title) { this->modelTitle = title; } |
| 73 |
|
| 74 |
bool N2EExoWriter::writeFile() |
| 75 |
{ |
| 76 |
// This poor variable is going to be over-used just to save 1-bit |
| 77 |
// Tsk-tsk |
| 78 |
bool result = true; |
| 79 |
|
| 80 |
this->writtenBlocks = 0; |
| 81 |
this->writtenNodes = 0; |
| 82 |
this->writtenTets = 0; |
| 83 |
this->writtenHexes = 0; |
| 84 |
|
| 85 |
result &= !this->sections.empty(); |
| 86 |
result &= this->gridList.size() >= 4; // One Tet??? |
| 87 |
result &= !this->elementList.empty(); // At least one element |
| 88 |
|
| 89 |
result &= this->writeFileParams(); |
| 90 |
result &= this->writeCoords(); |
| 91 |
result &= this->writeElements(); |
| 92 |
|
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
bool N2EExoWriter::writeCoords() |
| 97 |
{ |
| 98 |
bool result{true}; |
| 99 |
|
| 100 |
// Now we write the nodes |
| 101 |
size_t num_nodes = this->gridList.size(); |
| 102 |
std::vector<double> x; |
| 103 |
std::vector<double> y; |
| 104 |
std::vector<double> z; |
| 105 |
x.reserve(num_nodes); |
| 106 |
y.reserve(num_nodes); |
| 107 |
z.reserve(num_nodes); |
| 108 |
for (size_t i = 0; i < num_nodes; i++) { |
| 109 |
|
| 110 |
N2EPoint3D crd = std::get<1>(this->gridList[i]); |
| 111 |
x.push_back(crd.x[0]); |
| 112 |
y.push_back(crd.x[1]); |
| 113 |
z.push_back(crd.x[2]); |
| 114 |
} |
| 115 |
|
| 116 |
int ret = ex_put_coord(this->exoFileID, Data(x), Data(y), Data(z)); |
| 117 |
|
| 118 |
if (ret != 0) { |
| 119 |
std::cerr << "Problem writing node coordinates in N2EExoWriter::writeFile(). punching out.\n"; |
| 120 |
result = false; |
| 121 |
} |
| 122 |
else { |
| 123 |
this->writtenNodes = num_nodes; |
| 124 |
result = true; |
| 125 |
} |
| 126 |
|
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
bool N2EExoWriter::writeFileParams() |
| 131 |
{ |
| 132 |
bool result{true}; |
| 133 |
|
| 134 |
auto tmp = this->modelTitle.substr(0, MAX_LINE_LENGTH - 1); |
| 135 |
int ret = ex_put_init(this->exoFileID, tmp.c_str(), 3 /* 3D models only*/, |
| 136 |
this->gridList.size(), this->elementList.size(), this->sections.size(), 0, |
| 137 |
0); // Make your fancy pants nodes and side sets elsewherem, laddy. |
| 138 |
|
| 139 |
if (ret != 0) { |
| 140 |
std::cerr << "Problem initializing model params in N2EExoWriter::writeFile(). punching out\n"; |
| 141 |
result = false; |
| 142 |
} |
| 143 |
else { |
| 144 |
result = true; |
| 145 |
} |
| 146 |
|
| 147 |
return result; |
| 148 |
} |
| 149 |
|
| 150 |
bool N2EExoWriter::writeElements() |
| 151 |
{ |
| 152 |
bool result{true}; |
| 153 |
|
| 154 |
for (const sectionType § : this->sections) { |
| 155 |
|
| 156 |
std::vector<elementType> thisBlock; |
| 157 |
auto block = std::get<0>(sect); |
| 158 |
|
| 159 |
int retvalue{0}; |
| 160 |
|
| 161 |
for (const elementType &elem : this->elementList) { |
| 162 |
|
| 163 |
if (std::get<1>(elem) == block) { |
| 164 |
thisBlock.emplace_back(elem); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
auto nodes_per_elem = std::get<2>(thisBlock[0]); |
| 169 |
|
| 170 |
int n = nodes_per_elem == 4 ? 0 : 1; |
| 171 |
|
| 172 |
// This easy until we support 3 types of elements |
| 173 |
supportedElements thisElType = ExoElTypes[n]; |
| 174 |
|
| 175 |
retvalue = ex_put_block(this->exoFileID, thisElType.elementType, block, thisElType.elemDesc, |
| 176 |
thisBlock.size(), thisElType.numNodesPerElem, |
| 177 |
thisElType.numEdgesPerElem, thisElType.numFacesPerElem, 1); |
| 178 |
if (retvalue != 0) { |
| 179 |
|
| 180 |
std::cerr << "WARNING:The block: " << block << "\nMay not be configured correctly.\n"; |
| 181 |
} |
| 182 |
this->writtenBlocks++; |
| 183 |
|
| 184 |
std::vector<int> elemCon(nodes_per_elem * thisBlock.size()); |
| 185 |
|
| 186 |
int64_t numNodesCopied{0}; |
| 187 |
|
| 188 |
for (const elementType &elem : thisBlock) { |
| 189 |
|
| 190 |
const N2EModules::N2EGridPtList &pts{std::get<3>(elem)}; |
| 191 |
std::copy(pts.v, pts.v + nodes_per_elem, Data(elemCon) + numNodesCopied); |
| 192 |
numNodesCopied += nodes_per_elem; |
| 193 |
} |
| 194 |
|
| 195 |
retvalue = ex_put_conn(this->exoFileID, thisElType.elementType, block, Data(elemCon), nullptr, |
| 196 |
nullptr); |
| 197 |
|
| 198 |
switch (thisElType.numNodesPerElem) { |
| 199 |
|
| 200 |
case 4: this->writtenTets += thisBlock.size(); break; |
| 201 |
|
| 202 |
case 8: this->writtenHexes += thisBlock.size(); break; |
| 203 |
} |
| 204 |
|
| 205 |
if (retvalue != 0) { |
| 206 |
result = false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return result; |
| 211 |
} |
| 212 |
|
| 213 |
} // namespace ExoModules |