From 5c67812c0da460cbd299b41edfe585c5d8cf23cc Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 12:02:52 +0200 Subject: [PATCH 1/6] change logging to milliseconds --- main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index bb3bd07..6c89d55 100644 --- a/main.cpp +++ b/main.cpp @@ -19,9 +19,9 @@ int main() finiteVolumeMesh.writeVtk(); high_resolution_clock::time_point t2 = high_resolution_clock::now(); - auto duration = duration_cast(t2 - t1).count(); + auto duration = duration_cast(t2 - t1).count(); - cout << duration << " seconds." << endl; + cout << duration << " milliseconds." << endl; return 0; } From e38326af39968290dad0d842fd0d145987d3824e Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 12:03:23 +0200 Subject: [PATCH 2/6] Add CMakeLists --- CMakeLists.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..90a5afa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.5) + +project(GmshToVtkAndTecplotCpp11) + + +add_executable(gmshToX + main.cpp + + Cell2D.cpp + Face.cpp + FvmMesh2D.cpp + GmshReader.cpp + NodeIdent.cpp + NodeIdentMsh.cpp + Point.cpp + + streamIO.h + ) + + + From c94e8eb9831f417ee15cb4214a94513ce8dbb8fc Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 12:17:43 +0200 Subject: [PATCH 3/6] Avoid endl/flush --- FvmMesh2D.cpp | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/FvmMesh2D.cpp b/FvmMesh2D.cpp index fdb6dba..8a547b2 100644 --- a/FvmMesh2D.cpp +++ b/FvmMesh2D.cpp @@ -293,82 +293,85 @@ void FvmMesh2D::writeVtk() { outfile.setf(ios::fixed, ios::floatfield); outfile.precision(10); - outfile << "# vtk DataFile Version 2.0" << endl; - outfile << "VTK Format for unstructured grid" << endl; - outfile << "ASCII" << endl; - outfile << "DATASET POLYDATA" << endl; - outfile << "POINTS " << nbNodes << " float" << endl; + outfile << "# vtk DataFile Version 2.0\n"; + outfile << "VTK Format for unstructured grid\n"; + outfile << "ASCII\n"; + outfile << "DATASET POLYDATA\n"; + outfile << "POINTS " << nbNodes << " float\n"; for (unsigned i = 0; i < nbNodes; i++) { - outfile << setw(15) << coordNodes[i].getX() << " " << setw(15) << coordNodes[i].getY() << " " << setw(15) << 0.0f << " " << endl; + outfile << setw(15) << coordNodes[i].getX() << " " << setw(15) << coordNodes[i].getY() << " " << setw(15) << 0.0f << " \n"; } - outfile << "POLYGONS " << nbElm << " " << 5 * nbElm << endl; + outfile << "POLYGONS " << nbElm << " " << 5 * nbElm << '\n'; + outfile << flush; for (unsigned i = 0; i < nbElm; i++) { - outfile << 4 << " " << idNodes[i].getIdNode()[5] - 1 << " " << idNodes[i].getIdNode()[6] - 1 << " " << idNodes[i].getIdNode()[7] - 1 << " " << idNodes[i].getIdNode()[8] - 1 << endl; + outfile << 4 << " " << idNodes[i].getIdNode()[5] - 1 << " " << idNodes[i].getIdNode()[6] - 1 << " " << idNodes[i].getIdNode()[7] - 1 << " " << idNodes[i].getIdNode()[8] - 1 << "\n"; } - outfile << "CELL_DATA " << nbElm << endl; - outfile << "SCALARS CELL_IDENT integer 1" << endl; + outfile << "CELL_DATA " << nbElm << '\n'; + outfile << "SCALARS CELL_IDENT integer 1" << '\n'; outfile << "LOOKUP_TABLE default " << endl; for (unsigned i = 0; i < nbElm; i++) { - outfile << this->cells[i].getIdent() << endl; + outfile << this->cells[i].getIdent() << '\n'; } - outfile << "SCALARS NEIGHBOR1 integer 1" << endl; - outfile << "LOOKUP_TABLE default " << endl; + outfile << "SCALARS NEIGHBOR1 integer 1" << '\n'; + outfile << "LOOKUP_TABLE default " << '\n'; for (unsigned i = 0; i < nbElm; i++) { Cell2D *curr_cell = this->cells[i].getNeighbor1(); if (curr_cell != nullptr) { - outfile << curr_cell->getIdent() << endl; + outfile << curr_cell->getIdent() << '\n'; } else { - outfile << -1 << endl; + outfile << -1 << '\n'; } } - outfile << "SCALARS NEIGHBOR2 integer 1" << endl; + outfile << "SCALARS NEIGHBOR2 integer 1" << '\n'; outfile << "LOOKUP_TABLE default " << endl; for (unsigned i = 0; i < nbElm; i++) { Cell2D *curr_cell = this->cells[i].getNeighbor2(); if (curr_cell != nullptr) { - outfile << curr_cell->getIdent() << endl; + outfile << curr_cell->getIdent() << '\n'; } else { - outfile << -1 << endl; + outfile << -1 << '\n'; } } - outfile << "SCALARS NEIGHBOR3 integer 1" << endl; + outfile << "SCALARS NEIGHBOR3 integer 1" << '\n'; outfile << "LOOKUP_TABLE default " << endl; for (unsigned i = 0; i < nbElm; i++) { Cell2D *curr_cell = this->cells[i].getNeighbor3(); if (curr_cell != nullptr) { - outfile << curr_cell->getIdent() << endl; + outfile << curr_cell->getIdent() << '\n'; } else { - outfile << -1 << endl; + outfile << -1 << '\n'; } } - outfile << "SCALARS NEIGHBOR4 integer 1" << endl; + outfile << "SCALARS NEIGHBOR4 integer 1" << '\n'; outfile << "LOOKUP_TABLE default " << endl; for (unsigned i = 0; i < nbElm; i++) { Cell2D *curr_cell = this->cells[i].getNeighbor4(); if (curr_cell != nullptr) { - outfile << curr_cell->getIdent() << endl; + outfile << curr_cell->getIdent() << '\n'; } else { - outfile << -1 << endl; + outfile << -1 << '\n'; } } + outfile << flush; + outfile.close(); } From a334a28f6955e59d3b1b3fa40701c64da24adcbe Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 13:16:12 +0200 Subject: [PATCH 4/6] std algos --- Cell2D.cpp | 36 ++++++------- Cell2D.h | 20 ++++---- Face.cpp | 14 ++--- Face.h | 12 ++--- FvmMesh2D.cpp | 130 +++++++++++++++++++++++++++-------------------- GmshReader.cpp | 4 ++ NodeIdentMsh.cpp | 10 ++-- Point.cpp | 14 ++--- Point.h | 14 ++--- 9 files changed, 137 insertions(+), 117 deletions(-) diff --git a/Cell2D.cpp b/Cell2D.cpp index 8b74e9b..66178a4 100644 --- a/Cell2D.cpp +++ b/Cell2D.cpp @@ -1,40 +1,40 @@ #include "Cell2D.h" -unsigned Cell2D::getIdent() { +unsigned Cell2D::getIdent() const { return this->ident; } -Point Cell2D::getVertex1() { - return this->vertex[0]; +Point& Cell2D::getVertex1() { + return vertex[0]; } -Point Cell2D::getVertex2() { - return this->vertex[1]; +Point& Cell2D::getVertex2() { + return vertex[1]; } -Point Cell2D::getVertex3() { - return this->vertex[2]; +Point& Cell2D::getVertex3() { + return vertex[2]; } -Point Cell2D::getVertex4() { - return this->vertex[3]; +Point& Cell2D::getVertex4() { + return vertex[3]; } -Face Cell2D::getFace1() { - return this->faces[0]; +Face& Cell2D::getFace1() { + return faces[0]; } -Face Cell2D::getFace2() { - return this->faces[1]; +Face& Cell2D::getFace2() { + return faces[1]; } -Face Cell2D::getFace3() { - return this->faces[2]; +Face& Cell2D::getFace3() { + return faces[2]; } -Face Cell2D::getFace4() { - return this->faces[3]; +Face& Cell2D::getFace4() { + return faces[3]; } Cell2D* Cell2D::getNeighbor1() { @@ -53,7 +53,7 @@ Cell2D* Cell2D::getNeighbor4() { return neighbor4; } -double Cell2D::getVol() { +double Cell2D::getVol() const { double x1 = this->vertex[0].getX(); double x2 = this->vertex[1].getX(); double x3 = this->vertex[2].getX(); diff --git a/Cell2D.h b/Cell2D.h index 3b94c9c..d729277 100644 --- a/Cell2D.h +++ b/Cell2D.h @@ -11,20 +11,20 @@ class Cell2D { public: Cell2D() {}; - unsigned getIdent(); - Point getVertex1(); - Point getVertex2(); - Point getVertex3(); - Point getVertex4(); - Face getFace1(); - Face getFace2(); - Face getFace3(); - Face getFace4(); + unsigned getIdent() const; + Point& getVertex1(); + Point& getVertex2(); + Point& getVertex3(); + Point& getVertex4(); + Face& getFace1(); + Face& getFace2(); + Face& getFace3(); + Face& getFace4(); Cell2D* getNeighbor1(); Cell2D* getNeighbor2(); Cell2D* getNeighbor3(); Cell2D* getNeighbor4(); - double getVol(); + double getVol() const; void setIdent(unsigned); void setVertex(Point*); diff --git a/Face.cpp b/Face.cpp index 55195a0..9198697 100644 --- a/Face.cpp +++ b/Face.cpp @@ -5,7 +5,7 @@ void Face::setP1(Point p1) { this->p1 = p1; } -Point Face::getP1() { +Point& Face::getP1(){ return this->p1; } @@ -13,7 +13,7 @@ void Face::setP2(Point p2) { this->p2 = p2; } -Point Face::getP2() { +Point& Face::getP2(){ return this->p2; } @@ -21,7 +21,7 @@ void Face::setCentroid(Point centroid) { this->centroid = centroid; } -Point Face::getCentroid() { +Point& Face::getCentroid(){ return this->centroid; } @@ -29,7 +29,7 @@ void Face::setBcTyp(int bcTyp) { this->bcTyp = bcTyp; } -int Face::getBcTyp() { +int Face::getBcTyp() const{ return this->bcTyp; } @@ -37,7 +37,7 @@ void Face::setIdFace(unsigned idFace) { this->idFace = idFace; } -unsigned Face::getIdFace() { +unsigned Face::getIdFace() const{ return this->idFace; } @@ -45,6 +45,6 @@ void Face::setArea(double area) { this->area = area; } -double Face::getArea() { +double Face::getArea() const{ return this->area; -} \ No newline at end of file +} diff --git a/Face.h b/Face.h index 31010cb..08a3213 100644 --- a/Face.h +++ b/Face.h @@ -20,12 +20,12 @@ class Face { void setIdFace(unsigned); void setArea(double); - Point getP1(); - Point getP2(); - Point getCentroid(); - int getBcTyp(); - unsigned getIdFace(); - double getArea(); + Point& getP1(); + Point& getP2(); + Point& getCentroid(); + int getBcTyp() const; + unsigned getIdFace() const; + double getArea() const; private: Point p1, p2; diff --git a/FvmMesh2D.cpp b/FvmMesh2D.cpp index 8a547b2..8cc4246 100644 --- a/FvmMesh2D.cpp +++ b/FvmMesh2D.cpp @@ -8,6 +8,8 @@ #include "FvmMesh2D.h" #include "GmshReader.h" +#include + using namespace std; FvmMesh2D::FvmMesh2D() { @@ -22,47 +24,55 @@ vector & FvmMesh2D::getCells() { void FvmMesh2D::assignVertex() { vector coordNodes = mshReader.getCoordNodes(); + auto nodeIdents = mshReader.getIdNodes(); for (unsigned i = 0; i < mshReader.getNbElm(); i++) { Cell2D aCell; aCell.setIdent(i); - NodeIdent nodeIdent = mshReader.getIdNodes()[i]; - unsigned idnode = nodeIdent.getIdNode()[5]; + NodeIdent nodeIdent = nodeIdents[i]; + unsigned idnode = nodeIdent.getIdNode()[5]; - for (Point node : coordNodes) { - if (idnode == node.getId()) { - aCell.setVertex1(node); - break; - } + auto begin = coordNodes.begin(); + auto end = coordNodes.end(); + + auto target_node = std::find_if( + begin, end, [&](const Point& node) { + return idnode == node.getId(); + }); + if (target_node != end) { + aCell.setVertex1(*target_node); } idnode = nodeIdent.getIdNode()[6]; - for (Point node : coordNodes) { - if (idnode == node.getId()) { - aCell.setVertex2(node); - break; - } + target_node = std::find_if( + begin, end, [&](const Point& node) { + return idnode == node.getId(); + }); + if (target_node != end) { + aCell.setVertex2(*target_node); } idnode = nodeIdent.getIdNode()[7]; - for (Point node : coordNodes) { - if (idnode == node.getId()) { - aCell.setVertex3(node); - break; - } + target_node = std::find_if( + begin, end, [&](const Point& node) { + return idnode == node.getId(); + }); + if (target_node != end) { + aCell.setVertex3(*target_node); } idnode = nodeIdent.getIdNode()[8]; - for (Point node : coordNodes) { - if (idnode == node.getId()) { - aCell.setVertex4(node); - break; - } + target_node = std::find_if( + begin, end, [&](const Point& node) { + return idnode == node.getId(); + }); + if (target_node != end) { + aCell.setVertex4(*target_node); } this->cells.push_back(aCell); @@ -70,19 +80,21 @@ void FvmMesh2D::assignVertex() { } void FvmMesh2D::assignFaces() { - for (Cell2D & cell : cells) { - cell.getFace1().setP1(cell.getVertex1()); - cell.getFace1().setP2(cell.getVertex2()); - cell.getFace2().setP1(cell.getVertex2()); - cell.getFace2().setP2(cell.getVertex3()); + std::for_each(cells.begin(), cells.end(), [](Cell2D& cell) { - cell.getFace3().setP1(cell.getVertex3()); - cell.getFace3().setP2(cell.getVertex4()); - cell.getFace4().setP1(cell.getVertex4()); - cell.getFace4().setP2(cell.getVertex1()); - } + cell.getFace1().setP1(cell.getVertex1()); + cell.getFace1().setP2(cell.getVertex2()); + + cell.getFace2().setP1(cell.getVertex2()); + cell.getFace2().setP2(cell.getVertex3()); + + cell.getFace3().setP1(cell.getVertex3()); + cell.getFace3().setP2(cell.getVertex4()); + + cell.getFace4().setP1(cell.getVertex4()); + cell.getFace4().setP2(cell.getVertex1());}); } void FvmMesh2D::assignBoundaryCondition() { @@ -145,12 +157,15 @@ void FvmMesh2D::assignBoundaryCondition() { } void FvmMesh2D::calculVol() { - unsigned i = 0; - - for (auto it = this->cells.begin(); it != this->cells.end(); ++it) { - this->cells[i].setVol(this->cells[i].getVol()); - i = i + 1; - } +// unsigned i = 0; + + std::for_each(cells.begin(), cells.end(), [](Cell2D cell) { + cell.setVol(cell.getVol()); + }); +// for (auto it = this->cells.begin(); it != this->cells.end(); ++it) { +// this->cells[i].setVol(this->cells[i].getVol()); +// i = i + 1; +// } } void FvmMesh2D::detectNearestNeighbor() { @@ -388,41 +403,46 @@ void FvmMesh2D::writeTecplot() { outfile.setf(ios::fixed, ios::floatfield); outfile.precision(10); - outfile << "VARIABLES=X,Y,CELL_IDENT,NEIGHBOR1,NEIGHBOR2,NEIGHBOR3,NEIGHBOR4" << endl; - outfile << "VARIABLES=X,Y" << endl; - outfile << "ZONE T=\"UNSTRUCTURED-COUNTOUR\"" << endl; - outfile << "ZONETYPE=FEPOLYGON" << endl; - outfile << "NODES=" << nbNodes << endl; - outfile << "ELEMENTS=" << nbElm << endl; - outfile << "FACES=" << nbElm * 4 << endl; - outfile << "NumConnectedBoundaryFaces=0" << endl; - outfile << "TotalNumBoundaryConnections=0" << endl; + outfile << "VARIABLES=X,Y,CELL_IDENT,NEIGHBOR1,NEIGHBOR2,NEIGHBOR3,NEIGHBOR4" << '\n'; + outfile << "VARIABLES=X,Y" << '\n'; + outfile << "ZONE T=\"UNSTRUCTURED-COUNTOUR\"" << '\n'; + outfile << "ZONETYPE=FEPOLYGON" << '\n'; + outfile << "NODES=" << nbNodes << '\n'; + outfile << "ELEMENTS=" << nbElm << '\n'; + outfile << "FACES=" << nbElm * 4 << '\n'; + outfile << "NumConnectedBoundaryFaces=0" << '\n'; + outfile << "TotalNumBoundaryConnections=0" << '\n'; for (unsigned i = 0; i < nbNodes; i++) { - outfile << setw(15) << coordNodes[i].getX() << endl; + outfile << setw(15) << coordNodes[i].getX() << '\n'; } + outfile << flush; for (unsigned i = 0; i < nbNodes; i++) { - outfile << setw(15) << coordNodes[i].getY() << endl; + outfile << setw(15) << coordNodes[i].getY() << '\n'; } + outfile << flush; /* * Node indexes */ for (unsigned i = 0; i < nbElm; i++) { - outfile << idNodes[i].getIdNode()[5] << " " << idNodes[i].getIdNode()[6] << endl; - outfile << idNodes[i].getIdNode()[6] << " " << idNodes[i].getIdNode()[7] << endl; - outfile << idNodes[i].getIdNode()[7] << " " << idNodes[i].getIdNode()[8] << endl; - outfile << idNodes[i].getIdNode()[8] << " " << idNodes[i].getIdNode()[5] << endl; + outfile << idNodes[i].getIdNode()[5] << " " << idNodes[i].getIdNode()[6] << '\n'; + outfile << idNodes[i].getIdNode()[6] << " " << idNodes[i].getIdNode()[7] << '\n'; + outfile << idNodes[i].getIdNode()[7] << " " << idNodes[i].getIdNode()[8] << '\n'; + outfile << idNodes[i].getIdNode()[8] << " " << idNodes[i].getIdNode()[5] << '\n'; } + outfile << flush; for (unsigned i = 0; i < nbElm; i++) { - outfile << i + 1 << " " << i + 1 << " " << i + 1 << " " << i + 1 << " " << endl; + outfile << i + 1 << " " << i + 1 << " " << i + 1 << " " << i + 1 << " " << '\n'; } + outfile << flush; for (unsigned i = 0; i < nbElm; i++) { - outfile << 0 << " " << 0 << " " << 0 << " " << 0 << " " << endl; + outfile << 0 << " " << 0 << " " << 0 << " " << 0 << " " << '\n'; } + outfile << flush; outfile.close(); } diff --git a/GmshReader.cpp b/GmshReader.cpp index 5101bb8..f0a1f86 100644 --- a/GmshReader.cpp +++ b/GmshReader.cpp @@ -89,6 +89,8 @@ void GmshReader::readMesh() { line = ReadLine(stream_msh); stringstream(line) >> nbNode; + + coordNodes.reserve(nbNode); for (unsigned i = 0; i < nbNode; i++) { line = ReadLine(stream_msh); unsigned ident; @@ -104,6 +106,7 @@ void GmshReader::readMesh() { line = ReadLine(stream_msh); stringstream(line) >> nbElMsh; + idNodesMsh.reserve(nbElMsh); for (unsigned i = 0; i < nbElMsh; i++) { line = ReadLine(stream_msh); NodeIdentMsh node_msh; @@ -149,6 +152,7 @@ void GmshReader::readMesh() { } void GmshReader::constructIdNodes() { + idNodes.reserve(nbElMsh); for (unsigned i = 0; i < nbElMsh; i++) { unsigned elmTyp = idNodesMsh[i].getElemTyp(); diff --git a/NodeIdentMsh.cpp b/NodeIdentMsh.cpp index a405e68..f8abd2a 100644 --- a/NodeIdentMsh.cpp +++ b/NodeIdentMsh.cpp @@ -7,12 +7,8 @@ * and open the template in the editor. */ -NodeIdentMsh::NodeIdentMsh(unsigned ident, unsigned elemTyp, unsigned nbTags, unsigned tag1, unsigned tag2) { - this->setIdent(ident); - this->setElemTyp(elemTyp); - this->setNbTags(nbTags); - this->setTag1(tag1); - this->setTag2(tag2); +NodeIdentMsh::NodeIdentMsh(unsigned ident, unsigned elemTyp, unsigned nbTags, unsigned tag1, unsigned tag2) + : ident(ident), elemTyp(elemTyp), nbTags(nbTags), tag1(tag1), tag2(tag2){ } unsigned NodeIdentMsh::getIdent() { @@ -58,4 +54,4 @@ unsigned NodeIdentMsh::getTag2() { NodeIdentMsh* NodeIdentMsh::setTag2(unsigned tag2) { this->tag2 = tag2; return this; -} \ No newline at end of file +} diff --git a/Point.cpp b/Point.cpp index f028d31..b2aa982 100644 --- a/Point.cpp +++ b/Point.cpp @@ -12,14 +12,14 @@ Point::Point(double x, double y, double z, unsigned ident) { this -> ident = ident; } -bool Point::operator==(Point rhs) { +bool Point::operator==(Point& rhs) const{ if (abs(this->x - rhs.x) <= 1.0e-9 && abs(this->y - rhs.y) <= 1.0e-9 && abs(this->z - rhs.z) <= 1.0e-9) { return true; } return false; } -unsigned Point::getId() { +unsigned Point::getId() const{ return this->ident; } @@ -28,7 +28,7 @@ Point* Point::setId(unsigned id) { return this; } -double Point::getX() { +double Point::getX() const{ return this->x; } @@ -37,7 +37,7 @@ Point* Point::setX(double x) { return this; } -double Point::getY() { +double Point::getY() const{ return this->y; } @@ -46,7 +46,7 @@ Point* Point::setY(double y) { return this; } -double Point::getZ() { +double Point::getZ() const{ return this->z; } @@ -55,7 +55,7 @@ Point* Point::setZ(double z) { return this; } -void Point::print() +void Point::print() const { cout << this -> x << " " << this -> y << " " << this -> z << "\n" << endl; -} \ No newline at end of file +} diff --git a/Point.h b/Point.h index 159d48e..904d476 100644 --- a/Point.h +++ b/Point.h @@ -5,12 +5,12 @@ class Point { public: Point(){}; Point(double, double, double, unsigned); - bool operator==(Point); - void print(); - unsigned getId(); - double getX(); - double getY(); - double getZ(); + bool operator==(Point&) const; + void print() const; + unsigned getId() const; + double getX() const; + double getY() const; + double getZ() const; Point* setId(unsigned); Point* setX(double); Point* setY(double); @@ -23,4 +23,4 @@ class Point { unsigned ident; }; -#endif /* POINT_H */ \ No newline at end of file +#endif /* POINT_H */ From 650a0d8504806c8f6fedd04fe6c63745d7883f36 Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 13:53:10 +0200 Subject: [PATCH 5/6] use std container --- Cell2D.cpp | 44 +++++++++++++++++++++++++++----------------- Cell2D.h | 13 +++++++++---- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Cell2D.cpp b/Cell2D.cpp index 66178a4..abe2600 100644 --- a/Cell2D.cpp +++ b/Cell2D.cpp @@ -6,19 +6,19 @@ unsigned Cell2D::getIdent() const { } Point& Cell2D::getVertex1() { - return vertex[0]; + return vertices[0]; } Point& Cell2D::getVertex2() { - return vertex[1]; + return vertices[1]; } Point& Cell2D::getVertex3() { - return vertex[2]; + return vertices[2]; } Point& Cell2D::getVertex4() { - return vertex[3]; + return vertices[3]; } Face& Cell2D::getFace1() { @@ -54,15 +54,15 @@ Cell2D* Cell2D::getNeighbor4() { } double Cell2D::getVol() const { - double x1 = this->vertex[0].getX(); - double x2 = this->vertex[1].getX(); - double x3 = this->vertex[2].getX(); - double x4 = this->vertex[3].getX(); + double x1 = vertices[0].getX(); + double x2 = vertices[1].getX(); + double x3 = vertices[2].getX(); + double x4 = vertices[3].getX(); - double y1 = this->vertex[0].getY(); - double y2 = this->vertex[1].getY(); - double y3 = this->vertex[2].getY(); - double y4 = this->vertex[3].getY(); + double y1 = vertices[0].getY(); + double y2 = vertices[1].getY(); + double y3 = vertices[2].getY(); + double y4 = vertices[3].getY(); double vol = 0.5 * ((x1 - x3)*(y2 - y4) + (x4 - x2)*(y1 - y3)); return vol; @@ -73,19 +73,19 @@ void Cell2D::setIdent(unsigned ident) { } void Cell2D::setVertex1(Point p) { - this->vertex[0] = p; + vertices[0] = p; } void Cell2D::setVertex2(Point p) { - this->vertex[1] = p; + vertices[1] = p; } void Cell2D::setVertex3(Point p) { - this->vertex[2] = p; + vertices[2] = p; } void Cell2D::setVertex4(Point p) { - this->vertex[3] = p; + vertices[3] = p; } void Cell2D::setNeighbor1(Cell2D* currentCell) { @@ -105,5 +105,15 @@ void Cell2D::setNeighbor4(Cell2D* currentCell) { } void Cell2D::setVol(double vol) { - this->vol = vol; + this->vol = vol; +} + +const std::array& Cell2D::getVertices() const +{ + return vertices; +} + +const std::array& Cell2D::getFaces() const +{ + return faces; } diff --git a/Cell2D.h b/Cell2D.h index d729277..712dd17 100644 --- a/Cell2D.h +++ b/Cell2D.h @@ -7,6 +7,7 @@ #pragma once #include "Face.h" +#include class Cell2D { public: @@ -38,11 +39,15 @@ class Cell2D { void setNeighbor3(Cell2D*); void setNeighbor4(Cell2D*); void setVol(double); - -private: + + const std::array& getVertices() const; + + const std::array& getFaces() const; + + private: unsigned ident = 0; - Point vertex[4]; - Face faces[4]; + std::array vertices; + std::array faces; Cell2D *neighbor1 = nullptr; Cell2D *neighbor2 = nullptr; Cell2D *neighbor3 = nullptr; From 3702be7e46a1b53453fcc4be4d3d05b26b2d334f Mon Sep 17 00:00:00 2001 From: Sebastian Engel Date: Tue, 6 Jul 2021 13:53:22 +0200 Subject: [PATCH 6/6] use std algo --- FvmMesh2D.cpp | 193 +++++++++++++++++++++++++++----------------------- 1 file changed, 103 insertions(+), 90 deletions(-) diff --git a/FvmMesh2D.cpp b/FvmMesh2D.cpp index 8cc4246..74339dc 100644 --- a/FvmMesh2D.cpp +++ b/FvmMesh2D.cpp @@ -168,134 +168,147 @@ void FvmMesh2D::calculVol() { // } } -void FvmMesh2D::detectNearestNeighbor() { - unsigned nbelm = mshReader.getNbElm(); +void FvmMesh2D::detectNearestNeighbor() +{ +// unsigned nbelm = mshReader.getNbElm(); - for (unsigned i = 0; i < nbelm; i++) { - unsigned idnode1 = this->cells[i].getVertex1().getId(); - unsigned idnode2 = this->cells[i].getVertex2().getId(); + auto begin = cells.begin(); + auto end = cells.end(); - Cell2D *curr_cell = &this->cells[i]; - for (unsigned j = 0; j < nbelm; j++) { - Cell2D *runn_cell = &this->cells[j]; + std::for_each(begin, end, [&](Cell2D& curr_cell) { + const unsigned idnode1 = curr_cell.getVertex1().getId(); + const unsigned idnode2 = curr_cell.getVertex2().getId(); - if (curr_cell != runn_cell) { - unsigned cnt = 0; + std::for_each(begin, end, [&](Cell2D& runn_cell) { + auto vertices = runn_cell.getVertices(); + std::array ids{ + vertices[0].getId(), vertices[1].getId(), vertices[2].getId(), + vertices[3].getId()}; - if (idnode1 == runn_cell->getVertex1().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex1().getId()) cnt = cnt + 1; + if (&curr_cell != &runn_cell) { + unsigned cnt = 0; - if (idnode1 == runn_cell->getVertex2().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex2().getId()) cnt = cnt + 1; + if (idnode1 == ids[0]) cnt++; + if (idnode2 == ids[0]) cnt++; - if (idnode1 == runn_cell->getVertex3().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex3().getId()) cnt = cnt + 1; + if (idnode1 == ids[1]) cnt++; + if (idnode2 == ids[1]) cnt++; - if (idnode1 == runn_cell->getVertex4().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex4().getId()) cnt = cnt + 1; + if (idnode1 == ids[2]) cnt++; + if (idnode2 == ids[2]) cnt++; - if (cnt == 2) { - this->cells[i].setNeighbor1(runn_cell); - } - } - } - } + if (idnode1 == ids[3]) cnt++; + if (idnode2 == ids[3]) cnt++; - for (unsigned i = 0; i < nbelm; i++) { - unsigned idnode1 = this->cells[i].getVertex2().getId(); - unsigned idnode2 = this->cells[i].getVertex3().getId(); + if (cnt == 2) { + curr_cell.setNeighbor1(&runn_cell); + } - Cell2D *curr_cell = &this->cells[i]; + } + }); + }); - for (unsigned j = 0; j < nbelm; j++) { - Cell2D *runn_cell = &this->cells[j]; + std::for_each(begin, end, [&](Cell2D& curr_cell) { + const unsigned idnode1 = curr_cell.getVertex2().getId(); + const unsigned idnode2 = curr_cell.getVertex3().getId(); - if (curr_cell != runn_cell) { - unsigned cnt = 0; + std::for_each(begin, end, [&](Cell2D& runn_cell) { + auto vertices = runn_cell.getVertices(); + std::array ids{ + vertices[0].getId(), vertices[1].getId(), vertices[2].getId(), + vertices[3].getId()}; - if (idnode1 == runn_cell->getVertex1().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex1().getId()) cnt = cnt + 1; + if (&curr_cell != &runn_cell) { + unsigned cnt = 0; - if (idnode1 == runn_cell->getVertex2().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex2().getId()) cnt = cnt + 1; + if (idnode1 == ids[0]) cnt++; + if (idnode2 == ids[0]) cnt++; - if (idnode1 == runn_cell->getVertex3().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex3().getId()) cnt = cnt + 1; + if (idnode1 == ids[1]) cnt++; + if (idnode2 == ids[1]) cnt++; - if (idnode1 == runn_cell->getVertex4().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex4().getId()) cnt = cnt + 1; + if (idnode1 == ids[2]) cnt++; + if (idnode2 == ids[2]) cnt++; - if (cnt == 2) { - this->cells[i].setNeighbor2(runn_cell); - } - } - } - } + if (idnode1 == ids[3]) cnt++; + if (idnode2 == ids[3]) cnt++; - for (unsigned i = 0; i < nbelm; i++) { - unsigned idnode1 = this->cells[i].getVertex3().getId(); - unsigned idnode2 = this->cells[i].getVertex4().getId(); + if (cnt == 2) { + curr_cell.setNeighbor2(&runn_cell); + } - Cell2D *curr_cell = &this->cells[i]; + } + }); + }); - for (unsigned j = 0; j < nbelm; j++) { - Cell2D *runn_cell = &this->cells[j]; + std::for_each(begin, end, [&](Cell2D& curr_cell) { + const unsigned idnode1 = curr_cell.getVertex3().getId(); + const unsigned idnode2 = curr_cell.getVertex4().getId(); - if (curr_cell != runn_cell) { - unsigned cnt = 0; + std::for_each(begin, end, [&](Cell2D& runn_cell) { + auto vertices = runn_cell.getVertices(); + std::array ids{ + vertices[0].getId(), vertices[1].getId(), vertices[2].getId(), + vertices[3].getId()}; - if (idnode1 == runn_cell->getVertex1().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex1().getId()) cnt = cnt + 1; + if (&curr_cell != &runn_cell) { + unsigned cnt = 0; - if (idnode1 == runn_cell->getVertex2().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex2().getId()) cnt = cnt + 1; + if (idnode1 == ids[0]) cnt++; + if (idnode2 == ids[0]) cnt++; - if (idnode1 == runn_cell->getVertex3().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex3().getId()) cnt = cnt + 1; + if (idnode1 == ids[1]) cnt++; + if (idnode2 == ids[1]) cnt++; - if (idnode1 == runn_cell->getVertex4().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex4().getId()) cnt = cnt + 1; + if (idnode1 == ids[2]) cnt++; + if (idnode2 == ids[2]) cnt++; - if (cnt == 2) { - this->cells[i].setNeighbor3(runn_cell); - } - } - } + if (idnode1 == ids[3]) cnt++; + if (idnode2 == ids[3]) cnt++; - } + if (cnt == 2) { + curr_cell.setNeighbor3(&runn_cell); + } - for (unsigned i = 0; i < nbelm; i++) { - unsigned idnode1 = this->cells[i].getVertex4().getId(); - unsigned idnode2 = this->cells[i].getVertex1().getId(); + } + }); + }); - Cell2D *curr_cell = &this->cells[i]; - for (unsigned j = 0; j < nbelm; j++) { - Cell2D *runn_cell = &this->cells[j]; + std::for_each(begin, end, [&](Cell2D& curr_cell) { + const unsigned idnode1 = curr_cell.getVertex4().getId(); + const unsigned idnode2 = curr_cell.getVertex1().getId(); - if (curr_cell != runn_cell) { - unsigned cnt = 0; + std::for_each(begin, end, [&](Cell2D& runn_cell) { + auto vertices = runn_cell.getVertices(); + std::array ids{ + vertices[0].getId(), vertices[1].getId(), vertices[2].getId(), + vertices[3].getId()}; - if (idnode1 == runn_cell->getVertex1().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex1().getId()) cnt = cnt + 1; + if (&curr_cell != &runn_cell) { + unsigned cnt = 0; - if (idnode1 == runn_cell->getVertex2().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex2().getId()) cnt = cnt + 1; + if (idnode1 == ids[0]) cnt++; + if (idnode2 == ids[0]) cnt++; - if (idnode1 == runn_cell->getVertex3().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex3().getId()) cnt = cnt + 1; + if (idnode1 == ids[1]) cnt++; + if (idnode2 == ids[1]) cnt++; - if (idnode1 == runn_cell->getVertex4().getId()) cnt = cnt + 1; - if (idnode2 == runn_cell->getVertex4().getId()) cnt = cnt + 1; + if (idnode1 == ids[2]) cnt++; + if (idnode2 == ids[2]) cnt++; - if (cnt == 2) { - this->cells[i].setNeighbor4(runn_cell); - } - } - } + if (idnode1 == ids[3]) cnt++; + if (idnode2 == ids[3]) cnt++; + + if (cnt == 2) { + curr_cell.setNeighbor4(&runn_cell); + } + + } + }); + }); - } } void FvmMesh2D::writeVtk() {