123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- //#include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <sstream>
- #include "../MeshDS/PointDS.h"
- #include "../MeshDS/meshunstructured.h"
- #include "Mesh_CSDInp.h"
- /**
- * @brief the constructor.
- *
- * @param[in]
- * @return
- * @author shiting
- * @date 20230407
- */
- Mesh_CSDInp::Mesh_CSDInp() : Mesh()
- {
- }
- /**
- * @brief the destructor.
- *
- * @param[in]
- * @return
- * @author shiting
- * @date 20230407
- */
- Mesh_CSDInp::~Mesh_CSDInp()
- {
- }
- /**
- * @brief load inp ascii file.
- *
- * @param[in] the input inp file.
- * @return loading is successful or not.
- * @author shiting
- * @date 20230407
- */
- bool Mesh_CSDInp::Load_Ascii(const string &gridFile)
- {
- ifstream infile(gridFile);
- string line;
- while (getline(infile, line))
- {
- line.erase(0, line.find_first_not_of(' '));
- if (line.substr(0, 5) == "*NODE")
- {
- loadNodes(infile);
- break;
- }
- }
- if (!infile.good()) return false;
- zoneNumber_ = 1;
- MeshUnstructured *unMesh = new MeshUnstructured;
- unMesh->SetPoints(zonePointsVec_.at(0));
- zoneMeshVec_.push_back(unMesh);
- while (getline(infile, line))
- {
- line.erase(0, line.find_first_not_of(' '));
- if (line.substr(0, 8) == "*ELEMENT")
- {
- //cout << line << endl;
- string eleType = getElementType(line);
- //cout << eleType << endl;
- loadElements(infile, eleType);
- }
- }
- return true;
- }
- /**
- * @brief load node data from inp.
- *
- * @param[in] the input stream.
- * @return load successfully or not.
- * @author: shiting
- * @date: 20230407
- * @reviser:
- * @date:
- */
- bool Mesh_CSDInp::loadNodes(ifstream &infile)
- {
- if (infile.peek() == '*') return true;
- map<int, vector<double> > idNodeMap;
- string line;
- while (getline(infile, line))
- {
- line.erase(0, line.find_first_not_of(' '));
- string::size_type pos = line.find_first_of(',');
- if (pos != std::string::npos)
- {
- std::replace(line.begin(), line.end(), ',', ' ');
- }
- int id;
- vector<double> xyz(3);
- stringstream ss(line);
- if (ss >> id >> xyz[0] >> xyz[1] >> xyz[2])
- {
- idNodeMap[id] = xyz;
- }
- if (infile.peek() == '*')
- {
- break;
- }
- }
- int nPoint = idNodeMap.size();
- //cout << "np = " << nPoint << endl;
- PointsDS *points = new PointsDS;
- points->SetPointNumber(nPoint);
- nodeId2PointIdMap_.clear();
- int i = 0;
- for (map<int, vector<double> >::iterator it = idNodeMap.begin(); it != idNodeMap.end(); ++it)
- {
- points->SetPoint(i, it->second.at(0), it->second.at(1), it->second.at(2));
- nodeId2PointIdMap_[it->first] = i;
- ++i;
- }
- zonePointsVec_.push_back(points);
- return true;
- }
- /**
- * @brief load element data from inp.
- *
- * @param[in] the input stream.
- * @return load successfully or not.
- * @author: shiting
- * @date: 20230407
- * @reviser:
- * @date:
- */
- bool Mesh_CSDInp::loadElements(ifstream &infile, const string &eleType)
- {
- if (infile.peek() == '*') return true;
- int nn;
- int np;
- CellBase *cell = 0;
- if (eleType == "C3D8" || eleType == "F3D8" || eleType == "C3D8R" || eleType == "C3D8I")
- {
- nn = 8+1;
- np = 8;
- cell = new CellHexa;
- }
- else if (eleType == "C3D20" || eleType == "C3D20R")
- {
- nn = 20+1;
- np = 8;
- cell = new CellHexa;
- }
- else if (eleType == "C3D6" || eleType == "F3D6")
- {
- nn = 6+1;
- np = 6;
- cell = new CellPyramid;
- }
- else if (eleType == "C3D15")
- {
- nn = 15+1;
- np = 6;
- cell = new CellPyramid;
- }
- else if (eleType == "C3D4" || eleType == "F3D4")
- {
- nn = 4+1;
- np = 4;
- cell = new CellTetra;
- }
- else if (eleType == "C3D10" || eleType == "C3D10T")
- {
- nn = 10+1;
- np = 4;
- cell = new CellTetra;
- }
- else if (eleType == "S3")
- {
- nn = 3+1;
- np = 3;
- cell = new CellTriangle;
- }
- else if (eleType == "S6")
- {
- nn = 6+1;
- np = 3;
- cell = new CellTriangle;
- }
- else if (eleType == "S4" || eleType == "S4R")
- {
- nn = 4+1;
- np = 4;
- cell = new CellQuad;
- }
- else if (eleType == "S8" || eleType == "S8R")
- {
- nn = 8+1;
- np = 4;
- cell = new CellQuad;
- }
- else if (eleType == "B21" || eleType == "B31" || eleType == "B31R")
- {
- nn = 2+1;
- np = 2;
- cell = new CellLine;
- }
- else
- {
- //unsuppored type
- return false;
- }
- MeshUnstructured *mesh = dynamic_cast<MeshUnstructured*>(zoneMeshVec_.at(0));
- int curNN = 0;
- int curNP = 0;
- string line;
- while (getline(infile, line))
- {
- line.erase(0, line.find_first_not_of(' '));
- string::size_type pos = line.find_first_of(',');
- if (pos != std::string::npos)
- {
- std::replace(line.begin(), line.end(), ',', ' ');
- }
- stringstream ss(line);
- int id;
- while (curNN < nn)
- {
- if (curNN == 0)
- {
- ss >> id;
- ++curNN;
- continue;
- }
- while (ss >> id)
- {
- if (curNP < np)
- {
- cell->SetPoint(curNP, nodeId2PointIdMap_[id]);
- ++curNP;
- }
- ++curNN;
- }
- break;
- }
- if (curNN == nn)
- {
- mesh->AppendCell(cell);
- curNN = 0;
- curNP = 0;
- }
- if (infile.peek() == '*')
- {
- break;
- }
- }
- delete cell;
- //cout << "ne = " << mesh->GetCellNumber() << endl;
- return true;
- }
- /**
- * @brief get element type from element line.
- *
- * @param[in] element header line.
- * @return the element type
- * @author: shiting
- * @date: 20230410
- * @reviser:
- * @date:
- */
- string Mesh_CSDInp::getElementType(const string &eleLine)
- {
- string line(eleLine);
- line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
- string key("TYPE");
- string::iterator pos = std::search(line.begin(), line.end(), key.begin(), key.end());
- if (pos == line.end()) return "none";
- string::iterator start = pos + 5;
- int n = 0;
- for (string::iterator it = start; it != line.end(); ++it)
- {
- if (*it != ',') ++n;
- else break;
- }
- if (n == 0) return "none";
- return line.substr(start-line.begin(), n);
- }
- /**
- * @brief load inp binary file.
- *
- * @param[in] the input inp file.
- * @return loading is successful or not.
- * @author shiting
- * @date 20230407
- */
- bool Mesh_CSDInp::Load_Binary(const string &gridFile)
- {
- ifstream infile(gridFile);
- return true;
- }
- /**
- * @brief save ascii inp file.
- *
- * @param[in] the output inp file.
- * @return saving is successful or not.
- * @author shiting
- * @date 20230407
- */
- bool Mesh_CSDInp::Save_Ascii(const string &gridFile)
- {
- //predefine interface.
- ofstream outfile(gridFile);
- return true;
- }
- /**
- * @brief save binary inp file.
- *
- * @param[in] the output inp file.
- * @return saving is successful or not.
- * @author shiting
- * @date 20230407
- */
- bool Mesh_CSDInp::Save_Binary(const string &gridFile)
- {
- //predefine interface.
- ofstream outfile(gridFile);
- return true;
- }
|