Skip to content

Commit

Permalink
final refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
truongdangqe committed Jun 12, 2021
1 parent 9931ac0 commit 392990d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
71 changes: 41 additions & 30 deletions FvmMesh2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@

using namespace std;

void FvmMesh2D::assign_vextex() {
vector<Point> coordNodes = msh_reader.getCoordNodes();
FvmMesh2D::FvmMesh2D() {
mshReader.readMesh();
mshReader.constructIdNodes();
}

vector<Cell2D> FvmMesh2D::getCells() {
return this->cells;
}

void FvmMesh2D::assignVertex() {
vector<Point> coordNodes = mshReader.getCoordNodes();

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < mshReader.getNbElm(); i++) {

Cell2D a_cell;

a_cell.setIdent(i);

NodeIdent nodeIdent = msh_reader.getIdNodes()[i];
NodeIdent nodeIdent = mshReader.getIdNodes()[i];
unsigned idnode = nodeIdent.getIdNode()[5];

for (unsigned j = 0; j < msh_reader.getNbNode(); j++) {
for (unsigned j = 0; j < mshReader.getNbNode(); j++) {
if (idnode == coordNodes[j].getId()) {
double x = coordNodes[j].getX();
double y = coordNodes[j].getY();
Expand All @@ -33,7 +42,7 @@ void FvmMesh2D::assign_vextex() {

idnode = nodeIdent.getIdNode()[6];

for (unsigned j = 0; j < msh_reader.getNbNode(); j++) {
for (unsigned j = 0; j < mshReader.getNbNode(); j++) {
if (idnode == coordNodes[j].getId()) {
double x = coordNodes[j].getX();
double y = coordNodes[j].getY();
Expand All @@ -44,7 +53,7 @@ void FvmMesh2D::assign_vextex() {

idnode = nodeIdent.getIdNode()[7];

for (unsigned j = 0; j < msh_reader.getNbNode(); j++) {
for (unsigned j = 0; j < mshReader.getNbNode(); j++) {
if (idnode == coordNodes[j].getId()) {
double x = coordNodes[j].getX();
double y = coordNodes[j].getY();
Expand All @@ -55,7 +64,7 @@ void FvmMesh2D::assign_vextex() {

idnode = nodeIdent.getIdNode()[8];

for (unsigned j = 0; j < msh_reader.getNbNode(); j++) {
for (unsigned j = 0; j < mshReader.getNbNode(); j++) {
if (idnode == coordNodes[j].getId()) {
double x = coordNodes[j].getX();
double y = coordNodes[j].getY();
Expand All @@ -68,8 +77,8 @@ void FvmMesh2D::assign_vextex() {
}
}

void FvmMesh2D::assign_faces() {
for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
void FvmMesh2D::assignFaces() {
for (unsigned i = 0; i < mshReader.getNbElm(); i++) {
this->cells[i].getFace1().setP1(this->cells[i].getVertex1());
this->cells[i].getFace1().setP2(this->cells[i].getVertex2());

Expand All @@ -84,10 +93,10 @@ void FvmMesh2D::assign_faces() {
}
}

void FvmMesh2D::assign_boundary_condition() {
vector<NodeIdentMsh> idNodeMsh = msh_reader.getIdNodesMsh();
void FvmMesh2D::assignBoundaryCondition() {
vector<NodeIdentMsh> idNodeMsh = mshReader.getIdNodesMsh();

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < mshReader.getNbElm(); i++) {
unsigned idnode1 = this->cells[i].getVertex1().getId();
unsigned idnode2 = this->cells[i].getVertex2().getId();

Expand Down Expand Up @@ -143,7 +152,7 @@ void FvmMesh2D::assign_boundary_condition() {
}
}

void FvmMesh2D::calcul_vol_cells() {
void FvmMesh2D::calculVol() {
unsigned i = 0;

for (auto it = this->cells.begin(); it != this->cells.end(); ++it) {
Expand All @@ -152,8 +161,8 @@ void FvmMesh2D::calcul_vol_cells() {
}
}

void FvmMesh2D::detect_nearest_neighbor() {
unsigned nbelm = msh_reader.getNbElm();
void FvmMesh2D::detectNearestNeighbor() {
unsigned nbelm = mshReader.getNbElm();

for (unsigned i = 0; i < nbelm; i++) {
unsigned idnode1 = this->cells[i].getVertex1().getId();
Expand Down Expand Up @@ -282,10 +291,12 @@ void FvmMesh2D::detect_nearest_neighbor() {
}
}

void FvmMesh2D::write_vtk() {
string str = msh_reader.getFname() + ".vtk";
vector<Point> coordNodes = msh_reader.getCoordNodes();
vector<NodeIdent> idNodes = msh_reader.getIdNodes();
void FvmMesh2D::writeVtk() {
string str = mshReader.getFname() + ".vtk";
vector<Point> coordNodes = mshReader.getCoordNodes();
vector<NodeIdent> idNodes = mshReader.getIdNodes();
unsigned nbNodes = mshReader.getNbNode();
unsigned nbElm = mshReader.getNbElm();
ofstream outfile(str);

outfile.setf(ios::fixed, ios::floatfield);
Expand All @@ -294,30 +305,30 @@ void FvmMesh2D::write_vtk() {
outfile << "VTK Format for unstructured grid" << endl;
outfile << "ASCII" << endl;
outfile << "DATASET POLYDATA" << endl;
outfile << "POINTS " << msh_reader.getNbNode() << " float" << endl;
outfile << "POINTS " << nbNodes << " float" << endl;

for (unsigned i = 0; i < msh_reader.getNbNode(); i++) {
for (unsigned i = 0; i < nbNodes; i++) {
outfile << setw(15) << coordNodes[i].getX() << " " << setw(15) << coordNodes[i].getY() << " " << setw(15) << 0.0f << " " << endl;
}

outfile << "POLYGONS " << msh_reader.getNbElm() << " " << 5 * msh_reader.getNbElm() << endl;
outfile << "POLYGONS " << nbElm << " " << 5 * nbElm << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
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 << "CELL_DATA " << msh_reader.getNbElm() << endl;
outfile << "CELL_DATA " << nbElm << endl;
outfile << "SCALARS CELL_IDENT integer 1" << endl;
outfile << "LOOKUP_TABLE default " << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < nbElm; i++) {
outfile << this->cells[i].getIdent() << endl;
}

outfile << "SCALARS NEIGHBOR1 integer 1" << endl;
outfile << "LOOKUP_TABLE default " << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < nbElm; i++) {
Cell2D *curr_cell = this->cells[i].getNeighbor1();

if (curr_cell != nullptr) {
Expand All @@ -330,7 +341,7 @@ void FvmMesh2D::write_vtk() {
outfile << "SCALARS NEIGHBOR2 integer 1" << endl;
outfile << "LOOKUP_TABLE default " << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < nbElm; i++) {
Cell2D *curr_cell = this->cells[i].getNeighbor2();

if (curr_cell != nullptr) {
Expand All @@ -343,7 +354,7 @@ void FvmMesh2D::write_vtk() {
outfile << "SCALARS NEIGHBOR3 integer 1" << endl;
outfile << "LOOKUP_TABLE default " << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < nbElm; i++) {
Cell2D *curr_cell = this->cells[i].getNeighbor3();

if (curr_cell != nullptr) {
Expand All @@ -356,7 +367,7 @@ void FvmMesh2D::write_vtk() {
outfile << "SCALARS NEIGHBOR4 integer 1" << endl;
outfile << "LOOKUP_TABLE default " << endl;

for (unsigned i = 0; i < msh_reader.getNbElm(); i++) {
for (unsigned i = 0; i < nbElm; i++) {
Cell2D *curr_cell = this->cells[i].getNeighbor4();

if (curr_cell != nullptr) {
Expand Down
18 changes: 11 additions & 7 deletions FvmMesh2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ using namespace std;
class FvmMesh2D {

public:
GmshReader msh_reader;
FvmMesh2D();
vector<Cell2D> getCells();
void assignVertex();
void assignFaces();
void assignBoundaryCondition();
void detectNearestNeighbor();
void calculVol();
void writeVtk();

private:
GmshReader mshReader;
vector<Cell2D> cells;
void assign_vextex();
void assign_faces();
void assign_boundary_condition();
void detect_nearest_neighbor();
void calcul_vol_cells();
void write_vtk();
};

#endif /* FVMMESH2D_H */
Expand Down
13 changes: 5 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ int main()

FvmMesh2D finiteVolumeMesh;

finiteVolumeMesh.msh_reader.readMesh();
finiteVolumeMesh.msh_reader.constructIdNodes();

finiteVolumeMesh.assign_vextex();
finiteVolumeMesh.assign_faces();
finiteVolumeMesh.assign_boundary_condition();
finiteVolumeMesh.detect_nearest_neighbor();
finiteVolumeMesh.write_vtk();
finiteVolumeMesh.assignVertex();
finiteVolumeMesh.assignFaces();
finiteVolumeMesh.assignBoundaryCondition();
finiteVolumeMesh.detectNearestNeighbor();
finiteVolumeMesh.writeVtk();

high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<seconds>(t2 - t1).count();
Expand Down

0 comments on commit 392990d

Please sign in to comment.