proteus  1.8.1
C/C++/Fortran libraries
testMesh.cpp
Go to the documentation of this file.
1 #include "mesh.h"
2 
3 int main()
4 {
5  //todo write mesh destructor so this test doesn't leak memory
6  using namespace std;
7  int nx,ny,nz;
8  double start,stop,diff;
9  string outfilename,infilename;
10  cout<<"Testing simple mesh generation"<<endl;
11  cout<<"Enter nx,ny, and nz"<<endl;
12  cin>>nx>>ny>>nz;
13  cout<<"Print mesh? (n/filename/cout)"<<endl;
14  cin>>outfilename;
15  Mesh mesh;
16  initializeMesh(mesh);
17  start = CurrentTime();
18  if (ny == 1 && nz == 1)
19  {
20  cout<<"Generating Edge Mesh Elements"<<endl;
21  edgeMeshElements(nx,mesh);
22  cout<<"Generating Edge Mesh Nodes"<<endl;
23  regularEdgeMeshNodes(nx,1.0,mesh);
24  }
25  else if (nz == 1)
26  {
27  cout<<"Generating Triangular Mesh Elements"<<endl;
29  cout<<"Generating Triangular Mesh Nodes"<<endl;
30  regularRectangularToTriangularMeshNodes(nx,ny,1.0,1.0,mesh);
31  }
32  else
33  {
34  cout<<"Generating Tetrahedral Mesh Elements"<<endl;
36  cout<<"Generating Tetrahedral Mesh Nodes"<<endl;
37  regularHexahedralToTetrahedralMeshNodes(nx,ny,nz,1.0,1.0,1.0,mesh);
38  }
39  stop = CurrentTime();
40  cout<<"Elapsed time for mesh generation = "<<(stop-start)<<"s"<<endl;
41  cout<<"nElements_global = "<<mesh.nElements_global<<endl;
42  cout<<"nNodes_global = "<<mesh.nNodes_global<<endl;
43 
44 
45  cout<<"Writing mesh"<<endl;
46  start = CurrentTime();
47  if(outfilename != "n")
48  {
49  if(outfilename != "cout")
50  {
51  ofstream outfile(outfilename.c_str());
52  writeElements(outfile,mesh);
53  writeNodes(outfile,mesh);
54  }
55  else
56  {
57  writeElements(cout,mesh);
58  writeNodes(cout,mesh);
59  }
60  }
61  stop = CurrentTime();
62  cout<<"Elapsed time for writing mesh = "<<(stop-start)<<"s"<<endl;
63  delete [] mesh.elementNodesArray;
64 
65  cout<<"Testing readElements"<<endl;
66  cout<<"Enter mesh filename"<<endl;
67  cin>>infilename;
68  cout<<"Reading mesh from "<<infilename<<endl;
69  start = CurrentTime();
70  ifstream infile(infilename.c_str());
71  readElements(infile,mesh);
72  stop = CurrentTime();
73  cout<<"Elapsed time for reading mesh = "<<(stop-start)<<"s"<<endl;
74 
75 
76  cout<<"Testing global refinement"<<endl;
77  cout<<"Enter nLevels"<<endl;
78  int nLevels;
79  cin>>nLevels;
80  start = CurrentTime();
81  MultilevelMesh multilevelMesh;
82  if (ny == 1 && nz==1)
83  globallyRefineEdgeMesh(nLevels,mesh,multilevelMesh);
84  else if (nz == 1)
85  globallyRefineTriangularMesh(nLevels,mesh,multilevelMesh);
86  else
87  globallyRefineTetrahedralMesh(nLevels,mesh,multilevelMesh);
88  stop = CurrentTime();
89  cout<<"Elapsed time for global refinements = "<<(stop-start)<<"s"<<endl;
90  mesh = multilevelMesh.meshArray[nLevels-1];
93  Mesh edgeMesh;
94  initializeMesh(edgeMesh);
95  cout<<"nElements_global = "<<mesh.nElements_global<<endl;
96  cout<<"nNodes_global = "<<mesh.nNodes_global<<endl;
97 
98  cout<<"Constructing element boundary element arrays"<<endl;
99  if (ny == 1 && nz == 1)
100  {
102  edgeMesh = mesh;
103  }
104  else if (nz == 1)
105  {
107  //set up the edge mesh, need to make sure I've set everything
108  edgeMesh.nNodes_global = mesh.nNodes_global;
109  edgeMesh.nodeArray = mesh.nodeArray;
110  edgeMesh.nElements_global = mesh.nElementBoundaries_global;
111  edgeMesh.elementNodesArray = mesh.elementBoundaryNodesArray;
112  edgeMesh.nNodes_element = mesh.nNodes_elementBoundary;
113  }
114  else
115  {
118  //set up the face and edge meshes
119  Mesh faceMesh;
120  initializeMesh(faceMesh);
121  faceMesh.nNodes_global = mesh.nNodes_global;
122  faceMesh.nodeArray = mesh.nodeArray;
123  faceMesh.nElements_global = mesh.nElementBoundaries_global;
124  faceMesh.elementNodesArray = mesh.elementBoundaryNodesArray;
125  faceMesh.nNodes_element = mesh.nNodes_elementBoundary;
127  edgeMesh.nNodes_global = faceMesh.nNodes_global;
128  edgeMesh.nodeArray = faceMesh.nodeArray;
129  edgeMesh.nElements_global = faceMesh.nElementBoundaries_global;
130  edgeMesh.elementNodesArray = faceMesh.elementBoundaryNodesArray;
131  edgeMesh.nNodes_element = faceMesh.nNodes_elementBoundary;
132  }
133  cout<<"Print mesh? (n/filename/cout)"<<endl;
134  cin>>outfilename;
135  start = CurrentTime();
136  if(outfilename != "n")
137  {
138  if(outfilename != "cout")
139  {
140  ofstream outfile(outfilename.c_str());
141  writeElements(outfile,mesh);
142  writeNodes(outfile,mesh);
143  }
144  else
145  {
146  writeElements(cout,mesh);
147  writeNodes(cout,mesh);
148  }
149  }
150  stop = CurrentTime();
151  cout<<"Elapsed time for writing mesh = "<<(stop-start)<<"s"<<endl;
152 
153  ofstream datFile("edgeMesh.dat");
154  for(int eN=0;eN<edgeMesh.nElements_global;eN++)
155  {
156  datFile<<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+0]*3+0]
157  <<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+0]*3+1]
158  <<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+0]*3+2]<<endl
159  <<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+1]*3+0]
160  <<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+1]*3+1]
161  <<scientific<<setprecision(8)<<setw(16)<<edgeMesh.nodeArray[edgeMesh.elementNodesArray[eN*2+1]*3+2]<<endl<<endl<<endl;
162  }
163  //deleteMesh(edgeMesh); mesh.nodeArray = NULL;//the edgeMesh shares the nodes
164  for (int i=0;i<multilevelMesh.nLevels;i++)
165  deleteMesh(multilevelMesh.meshArray[i]);
166  return 0;
167 }
Mesh::elementBoundaryNodesArray
int * elementBoundaryNodesArray
Definition: mesh.h:47
MultilevelMesh
Definition: mesh.h:258
Mesh::nodeArray
double * nodeArray
Definition: mesh.h:67
constructElementBoundaryElementsArray_triangle
int constructElementBoundaryElementsArray_triangle(Mesh &mesh)
Definition: mesh.cpp:780
Mesh
Definition: mesh.h:28
Mesh::nNodes_elementBoundary
int nNodes_elementBoundary
Definition: mesh.h:33
Mesh::nElementBoundaries_global
int nElementBoundaries_global
Definition: mesh.h:35
deleteMesh
void deleteMesh(Mesh &mesh)
Definition: mesh.h:164
MultilevelMesh::meshArray
Mesh * meshArray
Definition: mesh.h:260
main
int main()
Definition: testMesh.cpp:3
regularHexahedralToTetrahedralMeshElements
int regularHexahedralToTetrahedralMeshElements(const int &nx, const int &ny, const int &nz, Mesh &mesh)
Definition: mesh.cpp:232
MultilevelMesh::nLevels
int nLevels
Definition: mesh.h:259
Mesh::nNodes_element
int nNodes_element
Definition: mesh.h:32
readElements
int readElements(std::istream &meshFile, Mesh &mesh)
Definition: mesh.cpp:4743
regularRectangularToTriangularMeshNodes
int regularRectangularToTriangularMeshNodes(const int &nx, const int &ny, const double &Lx, const double &Ly, Mesh &mesh)
Definition: mesh.cpp:166
writeNodes
int writeNodes(std::ostream &meshFile, const Mesh &mesh)
Definition: mesh.cpp:4807
globallyRefineEdgeMesh
int globallyRefineEdgeMesh(const int &nLevels, Mesh &mesh, MultilevelMesh &multilevelMesh, bool averageNewNodeFlags)
Definition: mesh.cpp:4014
edgeMeshElements
int edgeMeshElements(const int &nx, Mesh &mesh)
Definition: mesh.cpp:59
computeGeometricInfo_tetrahedron
int computeGeometricInfo_tetrahedron(Mesh &mesh)
Definition: mesh.cpp:3536
constructElementBoundaryElementsArray_tetrahedron
int constructElementBoundaryElementsArray_tetrahedron(Mesh &mesh)
Definition: mesh.cpp:1118
Mesh::elementNodesArray
int * elementNodesArray
Definition: mesh.h:42
constructElementBoundaryElementsArray_edge
int constructElementBoundaryElementsArray_edge(Mesh &mesh)
Definition: mesh.cpp:622
Mesh::nElements_global
int nElements_global
Definition: mesh.h:30
mesh.h
regularEdgeMeshNodes
int regularEdgeMeshNodes(const int &nx, const double &Lx, Mesh &mesh)
Definition: mesh.cpp:73
initializeMesh
void initializeMesh(Mesh &mesh)
Definition: mesh.h:87
Mesh::nNodes_global
int nNodes_global
Definition: mesh.h:31
regularHexahedralToTetrahedralMeshNodes
int regularHexahedralToTetrahedralMeshNodes(const int &nx, const int &ny, const int &nz, const double &Lx, const double &Ly, const double &Lz, Mesh &mesh)
Definition: mesh.cpp:457
globallyRefineTetrahedralMesh
int globallyRefineTetrahedralMesh(const int &nLevels, Mesh &mesh, MultilevelMesh &multilevelMesh, bool averageNewNodeFlags)
Definition: mesh.cpp:4253
globallyRefineTriangularMesh
int globallyRefineTriangularMesh(const int &nLevels, Mesh &mesh, MultilevelMesh &multilevelMesh, bool averageNewNodeFlags)
Definition: mesh.cpp:4109
regularRectangularToTriangularMeshElements
int regularRectangularToTriangularMeshElements(const int &nx, const int &ny, Mesh &mesh, int triangleFlag)
Definition: mesh.cpp:88
writeElements
int writeElements(std::ostream &meshFile, const Mesh &mesh)
Definition: mesh.cpp:4819