Skip to content

Commit 2436236

Browse files
committed
add FECell mesh save function
1 parent 4da0df1 commit 2436236

5 files changed

Lines changed: 94 additions & 3 deletions

File tree

include/FECell/FECell.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ class FECell{
9090
*/
9191
inline FECellData& getCellDataRef(){return m_CellData;}
9292

93-
93+
/**
94+
* Save the FECell mesh to vtu file
95+
*/
96+
void saveFECell2VTUFile()const;
9497
/**
9598
* Print out the summary information of FECell class, i.e., mpi-based mesh distribution, elements num, nodes num, etc.
9699
*/

include/FECell/FECellData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ struct FECellData{
6767
int Ny;/**< elements num along y-axis */
6868
int Nz;/**< elements num along z-axis */
6969

70+
vector<double> NodeCoords_Global;/**< the nodal coordinates of all the mesh point, which is only stored in master rank */
71+
7072
int MaxDofsPerNode;/**< the maximum dofs number of each node */
7173
int TotalDofsNum;/**< the total dofs number of the fe cell mesh */
7274
int ActiveDofsNum;/**< the active dofs number of the fe cell mesh */

src/FECell/FECell.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//+++ Function: finite element cell management class
1313
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1414

15+
#include <fstream>
1516
#include "FECell/FECell.h"
1617

1718
FECell::FECell(){}
@@ -51,6 +52,80 @@ void FECell::setMeshInfo(const int &nx,const int &ny,const int &nz,
5152
m_CellData.Zmax=zmax;
5253
m_CellData.BulkElmtMeshType=meshtype;
5354
}
55+
//****************************************************
56+
void FECell::saveFECell2VTUFile()const{
57+
int rank;
58+
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
59+
if(rank==0){
60+
std::ofstream out;
61+
out.open("mesh.vtu",std::ios::out);
62+
if(!out.is_open()){
63+
MessagePrinter::printErrorTxt("can\'t create/open mesh.vtu, please make sure you have the write permission");
64+
MessagePrinter::exitAsFem();
65+
}
66+
out << "<?xml version=\"1.0\"?>\n";
67+
out << "<VTKFile type=\"UnstructuredGrid\" version=\"1.0\">\n";
68+
out << "<UnstructuredGrid>\n";
69+
out << "<Piece NumberOfPoints=\"" << m_CellData.NodesNum << "\" NumberOfCells=\"" << m_CellData.BulkElmtsNum << "\">\n";
70+
out << "<Points>\n";
71+
out << "<DataArray type=\"Float64\" Name=\"nodes\" NumberOfComponents=\"3\" format=\"ascii\">\n";
72+
73+
int i;
74+
//*****************************
75+
// print out node coordinates
76+
out <<std::scientific << std::setprecision(6);
77+
for (i = 1; i <= m_CellData.NodesNum; i++){
78+
out << m_CellData.NodeCoords_Global[(i-1)*3+1-1] << " ";
79+
out << m_CellData.NodeCoords_Global[(i-1)*3+2-1] << " ";
80+
out << m_CellData.NodeCoords_Global[(i-1)*3+3-1] << "\n";
81+
}
82+
out << "</DataArray>\n";
83+
out << "</Points>\n";
84+
85+
//***************************************
86+
//*** For cell information
87+
//***************************************
88+
out << "<Cells>\n";
89+
out << "<DataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">\n";
90+
for(const auto &cell:m_CellData.MeshCell_Total){
91+
for(const auto &id:cell.ElmtConn){
92+
out<<id-1<<" ";
93+
}
94+
out<<"\n";
95+
}
96+
out << "</DataArray>\n";
97+
98+
//***************************************
99+
//*** For offset
100+
//***************************************
101+
out << "<DataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">\n";
102+
int offset = 0;
103+
for(const auto &cell:m_CellData.MeshCell_Total){
104+
offset+=cell.NodesNumPerElmt;
105+
out << offset << "\n";
106+
}
107+
out << "</DataArray>\n";
108+
109+
//***************************************
110+
//*** For vtk cell type
111+
//***************************************
112+
out << "<DataArray type=\"Int32\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">\n";
113+
for(const auto &cell:m_CellData.MeshCell_Total){
114+
out<<cell.VTKCellType<<"\n";
115+
}
116+
out << "</DataArray>\n";
117+
out << "</Cells>\n";
118+
119+
//***************************************
120+
//*** End of output
121+
//***************************************
122+
out << "</Piece>\n";
123+
out << "</UnstructuredGrid>\n";
124+
out << "</VTKFile>" << endl;
125+
126+
out.close();
127+
}
128+
}
54129
void FECell::printSummaryInfo()const{
55130
MessagePrinter::printStars();
56131
MessagePrinter::printNormalTxt("Summary information of FECell system");

src/FECell/Lagrange3DMeshCellGenerator.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ bool Lagrange3DMeshCellGenerator::generateFECell(const MeshType &t_meshtype,FECe
120120
}
121121
}// end-of-node-generation
122122

123+
t_celldata.NodeCoords_Global.clear();
124+
for(const auto &it:nodecoords) t_celldata.NodeCoords_Global.push_back(it);
125+
123126
// for the connectivity information of bulk elements
124127
t_celldata.MeshCell_Total.resize(t_celldata.BulkElmtsNum);
125128
leftconn.resize(t_celldata.Ny*t_celldata.Nz);
@@ -545,6 +548,9 @@ bool Lagrange3DMeshCellGenerator::generateFECell(const MeshType &t_meshtype,FECe
545548
nodecoords[(kk-1)*3+2-1]=t_celldata.Ymin+(j-1)*2*dy;
546549
nodecoords[(kk-1)*3+3-1]=t_celldata.Zmin+(k-1)*2*dz;
547550
}
551+
t_celldata.NodeCoords_Global.clear();
552+
for(const auto &it:nodecoords) t_celldata.NodeCoords_Global.push_back(it);
553+
548554
// end-of-node-generation
549555
t_celldata.MeshCell_Total.resize(t_celldata.BulkElmtsNum);
550556
leftconn.resize(t_celldata.Ny*t_celldata.Nz);
@@ -1163,6 +1169,9 @@ bool Lagrange3DMeshCellGenerator::generateFECell(const MeshType &t_meshtype,FECe
11631169
nodecoords[(kk-1)*3+3-1]=t_celldata.Zmin+(k-1)*2*dz;
11641170
}
11651171
}
1172+
t_celldata.NodeCoords_Global.clear();
1173+
for(const auto &it:nodecoords) t_celldata.NodeCoords_Global.push_back(it);
1174+
11661175
// for the connectivity information of bulk elements
11671176
t_celldata.MeshCell_Total.resize(t_celldata.BulkElmtsNum);
11681177
leftconn.resize(t_celldata.Ny*t_celldata.Nz);

src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ int main(int args,char *argv[]){
3838

3939
FECell mycell;
4040
FECellGenerator generator;
41-
mycell.setMeshInfo(1,2,3,0.0,1.0,0.0,1.0,0.0,1.0,MeshType::HEX8);
42-
generator.createFEMeshCell(MeshType::HEX8,mycell.getCellDataRef());
41+
mycell.setMeshInfo(1,2,3,0.0,1.0,0.0,1.0,0.0,1.0,MeshType::HEX20);
42+
generator.createFEMeshCell(MeshType::HEX20,mycell.getCellDataRef());
4343
mycell.printSummaryInfo();
44+
45+
mycell.saveFECell2VTUFile();
4446

4547

4648
ierr=PetscFinalize();CHKERRQ(ierr);

0 commit comments

Comments
 (0)