Mesh_CSDInp.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //#include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include "../MeshDS/PointDS.h"
  6. #include "../MeshDS/meshunstructured.h"
  7. #include "Mesh_CSDInp.h"
  8. /**
  9. * @brief the constructor.
  10. *
  11. * @param[in]
  12. * @return
  13. * @author shiting
  14. * @date 20230407
  15. */
  16. Mesh_CSDInp::Mesh_CSDInp() : Mesh()
  17. {
  18. }
  19. /**
  20. * @brief the destructor.
  21. *
  22. * @param[in]
  23. * @return
  24. * @author shiting
  25. * @date 20230407
  26. */
  27. Mesh_CSDInp::~Mesh_CSDInp()
  28. {
  29. }
  30. /**
  31. * @brief load inp ascii file.
  32. *
  33. * @param[in] the input inp file.
  34. * @return loading is successful or not.
  35. * @author shiting
  36. * @date 20230407
  37. */
  38. bool Mesh_CSDInp::Load_Ascii(const string &gridFile)
  39. {
  40. ifstream infile(gridFile);
  41. string line;
  42. while (getline(infile, line))
  43. {
  44. line.erase(0, line.find_first_not_of(' '));
  45. if (line.substr(0, 5) == "*NODE")
  46. {
  47. loadNodes(infile);
  48. break;
  49. }
  50. }
  51. if (!infile.good()) return false;
  52. zoneNumber_ = 1;
  53. MeshUnstructured *unMesh = new MeshUnstructured;
  54. unMesh->SetPoints(zonePointsVec_.at(0));
  55. zoneMeshVec_.push_back(unMesh);
  56. while (getline(infile, line))
  57. {
  58. line.erase(0, line.find_first_not_of(' '));
  59. if (line.substr(0, 8) == "*ELEMENT")
  60. {
  61. //cout << line << endl;
  62. string eleType = getElementType(line);
  63. //cout << eleType << endl;
  64. loadElements(infile, eleType);
  65. }
  66. }
  67. return true;
  68. }
  69. /**
  70. * @brief load node data from inp.
  71. *
  72. * @param[in] the input stream.
  73. * @return load successfully or not.
  74. * @author: shiting
  75. * @date: 20230407
  76. * @reviser:
  77. * @date:
  78. */
  79. bool Mesh_CSDInp::loadNodes(ifstream &infile)
  80. {
  81. if (infile.peek() == '*') return true;
  82. map<int, vector<double> > idNodeMap;
  83. string line;
  84. while (getline(infile, line))
  85. {
  86. line.erase(0, line.find_first_not_of(' '));
  87. string::size_type pos = line.find_first_of(',');
  88. if (pos != std::string::npos)
  89. {
  90. std::replace(line.begin(), line.end(), ',', ' ');
  91. }
  92. int id;
  93. vector<double> xyz(3);
  94. stringstream ss(line);
  95. if (ss >> id >> xyz[0] >> xyz[1] >> xyz[2])
  96. {
  97. idNodeMap[id] = xyz;
  98. }
  99. if (infile.peek() == '*')
  100. {
  101. break;
  102. }
  103. }
  104. int nPoint = idNodeMap.size();
  105. //cout << "np = " << nPoint << endl;
  106. PointsDS *points = new PointsDS;
  107. points->SetPointNumber(nPoint);
  108. nodeId2PointIdMap_.clear();
  109. int i = 0;
  110. for (map<int, vector<double> >::iterator it = idNodeMap.begin(); it != idNodeMap.end(); ++it)
  111. {
  112. points->SetPoint(i, it->second.at(0), it->second.at(1), it->second.at(2));
  113. nodeId2PointIdMap_[it->first] = i;
  114. ++i;
  115. }
  116. zonePointsVec_.push_back(points);
  117. return true;
  118. }
  119. /**
  120. * @brief load element data from inp.
  121. *
  122. * @param[in] the input stream.
  123. * @return load successfully or not.
  124. * @author: shiting
  125. * @date: 20230407
  126. * @reviser:
  127. * @date:
  128. */
  129. bool Mesh_CSDInp::loadElements(ifstream &infile, const string &eleType)
  130. {
  131. if (infile.peek() == '*') return true;
  132. int nn;
  133. int np;
  134. CellBase *cell = 0;
  135. if (eleType == "C3D8" || eleType == "F3D8" || eleType == "C3D8R" || eleType == "C3D8I")
  136. {
  137. nn = 8+1;
  138. np = 8;
  139. cell = new CellHexa;
  140. }
  141. else if (eleType == "C3D20" || eleType == "C3D20R")
  142. {
  143. nn = 20+1;
  144. np = 8;
  145. cell = new CellHexa;
  146. }
  147. else if (eleType == "C3D6" || eleType == "F3D6")
  148. {
  149. nn = 6+1;
  150. np = 6;
  151. cell = new CellPyramid;
  152. }
  153. else if (eleType == "C3D15")
  154. {
  155. nn = 15+1;
  156. np = 6;
  157. cell = new CellPyramid;
  158. }
  159. else if (eleType == "C3D4" || eleType == "F3D4")
  160. {
  161. nn = 4+1;
  162. np = 4;
  163. cell = new CellTetra;
  164. }
  165. else if (eleType == "C3D10" || eleType == "C3D10T")
  166. {
  167. nn = 10+1;
  168. np = 4;
  169. cell = new CellTetra;
  170. }
  171. else if (eleType == "S3")
  172. {
  173. nn = 3+1;
  174. np = 3;
  175. cell = new CellTriangle;
  176. }
  177. else if (eleType == "S6")
  178. {
  179. nn = 6+1;
  180. np = 3;
  181. cell = new CellTriangle;
  182. }
  183. else if (eleType == "S4" || eleType == "S4R")
  184. {
  185. nn = 4+1;
  186. np = 4;
  187. cell = new CellQuad;
  188. }
  189. else if (eleType == "S8" || eleType == "S8R")
  190. {
  191. nn = 8+1;
  192. np = 4;
  193. cell = new CellQuad;
  194. }
  195. else if (eleType == "B21" || eleType == "B31" || eleType == "B31R")
  196. {
  197. nn = 2+1;
  198. np = 2;
  199. cell = new CellLine;
  200. }
  201. else
  202. {
  203. //unsuppored type
  204. return false;
  205. }
  206. MeshUnstructured *mesh = dynamic_cast<MeshUnstructured*>(zoneMeshVec_.at(0));
  207. int curNN = 0;
  208. int curNP = 0;
  209. string line;
  210. while (getline(infile, line))
  211. {
  212. line.erase(0, line.find_first_not_of(' '));
  213. string::size_type pos = line.find_first_of(',');
  214. if (pos != std::string::npos)
  215. {
  216. std::replace(line.begin(), line.end(), ',', ' ');
  217. }
  218. stringstream ss(line);
  219. int id;
  220. while (curNN < nn)
  221. {
  222. if (curNN == 0)
  223. {
  224. ss >> id;
  225. ++curNN;
  226. continue;
  227. }
  228. while (ss >> id)
  229. {
  230. if (curNP < np)
  231. {
  232. cell->SetPoint(curNP, nodeId2PointIdMap_[id]);
  233. ++curNP;
  234. }
  235. ++curNN;
  236. }
  237. break;
  238. }
  239. if (curNN == nn)
  240. {
  241. mesh->AppendCell(cell);
  242. curNN = 0;
  243. curNP = 0;
  244. }
  245. if (infile.peek() == '*')
  246. {
  247. break;
  248. }
  249. }
  250. delete cell;
  251. //cout << "ne = " << mesh->GetCellNumber() << endl;
  252. return true;
  253. }
  254. /**
  255. * @brief get element type from element line.
  256. *
  257. * @param[in] element header line.
  258. * @return the element type
  259. * @author: shiting
  260. * @date: 20230410
  261. * @reviser:
  262. * @date:
  263. */
  264. string Mesh_CSDInp::getElementType(const string &eleLine)
  265. {
  266. string line(eleLine);
  267. line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
  268. string key("TYPE");
  269. string::iterator pos = std::search(line.begin(), line.end(), key.begin(), key.end());
  270. if (pos == line.end()) return "none";
  271. string::iterator start = pos + 5;
  272. int n = 0;
  273. for (string::iterator it = start; it != line.end(); ++it)
  274. {
  275. if (*it != ',') ++n;
  276. else break;
  277. }
  278. if (n == 0) return "none";
  279. return line.substr(start-line.begin(), n);
  280. }
  281. /**
  282. * @brief load inp binary file.
  283. *
  284. * @param[in] the input inp file.
  285. * @return loading is successful or not.
  286. * @author shiting
  287. * @date 20230407
  288. */
  289. bool Mesh_CSDInp::Load_Binary(const string &gridFile)
  290. {
  291. ifstream infile(gridFile);
  292. return true;
  293. }
  294. /**
  295. * @brief save ascii inp file.
  296. *
  297. * @param[in] the output inp file.
  298. * @return saving is successful or not.
  299. * @author shiting
  300. * @date 20230407
  301. */
  302. bool Mesh_CSDInp::Save_Ascii(const string &gridFile)
  303. {
  304. //predefine interface.
  305. ofstream outfile(gridFile);
  306. return true;
  307. }
  308. /**
  309. * @brief save binary inp file.
  310. *
  311. * @param[in] the output inp file.
  312. * @return saving is successful or not.
  313. * @author shiting
  314. * @date 20230407
  315. */
  316. bool Mesh_CSDInp::Save_Binary(const string &gridFile)
  317. {
  318. //predefine interface.
  319. ofstream outfile(gridFile);
  320. return true;
  321. }