CylindricalHoneycombMeshGenerator.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "CylindricalHoneycombMeshGenerator.hpp"
00037
00038 #include <boost/foreach.hpp>
00039 #include "RandomNumberGenerator.hpp"
00040 #include "MathsCustomFunctions.hpp"
00041 #include "ChasteSyscalls.hpp"
00042
00043 CylindricalHoneycombMeshGenerator::CylindricalHoneycombMeshGenerator(unsigned numNodesAlongWidth, unsigned numNodesAlongLength, unsigned ghosts, double scaleFactor)
00044 {
00045 mpMesh = NULL;
00046 mDomainWidth = numNodesAlongWidth*scaleFactor;
00047 mNumCellWidth = numNodesAlongWidth;
00048 mNumCellLength = numNodesAlongLength;
00049 mMeshFilename = "mesh";
00050 mGhostNodeIndices.clear();
00051
00052 assert(PetscTools::IsSequential());
00053
00054
00055 assert(scaleFactor > 0.0);
00056
00057
00058 std::stringstream pid;
00059 pid << getpid();
00060
00061 OutputFileHandler output_file_handler("2D_temporary_honeycomb_mesh_"+ pid.str());
00062
00063 unsigned num_nodes_along_width = mNumCellWidth;
00064 unsigned num_nodes_along_length = mNumCellLength;
00065 double horizontal_spacing = mDomainWidth / (double)num_nodes_along_width;
00066 double vertical_spacing = (sqrt(3.0)/2)*horizontal_spacing;
00067
00068
00069 mDomainDepth = (double)(num_nodes_along_length) * vertical_spacing;
00070
00071
00072 num_nodes_along_length += 2*ghosts;
00073
00074 unsigned num_nodes = num_nodes_along_width*num_nodes_along_length;
00075 unsigned num_elem_along_width = num_nodes_along_width-1;
00076 unsigned num_elem_along_length = num_nodes_along_length-1;
00077 unsigned num_elem = 2*num_elem_along_width*num_elem_along_length;
00078 unsigned num_edges = 3*num_elem_along_width*num_elem_along_length + num_elem_along_width + num_elem_along_length;
00079
00080 double x0 = 0;
00081 double y0 = -vertical_spacing*ghosts;
00082
00083 mBottom = -vertical_spacing*ghosts;
00084 mTop = mBottom + vertical_spacing*(num_nodes_along_length-1);
00085
00086
00087 out_stream p_node_file = output_file_handler.OpenOutputFile(mMeshFilename+".node");
00088 (*p_node_file) << std::scientific;
00089
00090 (*p_node_file) << num_nodes << "\t2\t0\t1" << std::endl;
00091
00092 unsigned node = 0;
00093 for (unsigned i=0; i<num_nodes_along_length; i++)
00094 {
00095 for (unsigned j=0; j<num_nodes_along_width; j++)
00096 {
00097 if (i<ghosts || i>=(ghosts+mNumCellLength))
00098 {
00099 mGhostNodeIndices.insert(node);
00100 }
00101 unsigned boundary = 0;
00102 if ((i==0) || (i==num_nodes_along_length-1))
00103 {
00104 boundary = 1;
00105 }
00106
00107 double x = x0 + horizontal_spacing*((double)j + 0.25*(1.0+ SmallPow(-1.0,i+1)));
00108 double y = y0 + vertical_spacing*(double)i;
00109
00110
00111 if ( (y<0.0) && (y>-1e-12) )
00112 {
00113
00114 #define COVERAGE_IGNORE
00115 y = 0.0;
00116 #undef COVERAGE_IGNORE
00117 }
00118
00119 (*p_node_file) << node++ << "\t" << x << "\t" << y << "\t" << boundary << std::endl;
00120 }
00121 }
00122 p_node_file->close();
00123
00124
00125 out_stream p_elem_file = output_file_handler.OpenOutputFile(mMeshFilename+".ele");
00126 (*p_elem_file) << std::scientific;
00127
00128 out_stream p_edge_file = output_file_handler.OpenOutputFile(mMeshFilename+".edge");
00129 (*p_node_file) << std::scientific;
00130
00131 (*p_elem_file) << num_elem << "\t3\t0" << std::endl;
00132 (*p_edge_file) << num_edges << "\t1" << std::endl;
00133
00134 unsigned elem = 0;
00135 unsigned edge = 0;
00136 for (unsigned i=0; i<num_elem_along_length; i++)
00137 {
00138 for (unsigned j=0; j < num_elem_along_width; j++)
00139 {
00140 unsigned node0 = i*num_nodes_along_width + j;
00141 unsigned node1 = i*num_nodes_along_width + j+1;
00142 unsigned node2 = (i+1)*num_nodes_along_width + j;
00143
00144 if (i%2 != 0)
00145 {
00146 node2 = node2 + 1;
00147 }
00148
00149 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
00150
00151 unsigned horizontal_edge_is_boundary_edge = 0;
00152 unsigned vertical_edge_is_boundary_edge = 0;
00153 if (i==0)
00154 {
00155 horizontal_edge_is_boundary_edge = 1;
00156 }
00157
00158 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << horizontal_edge_is_boundary_edge << std::endl;
00159 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node2 << "\t" << 0 << std::endl;
00160 (*p_edge_file) << edge++ << "\t" << node2 << "\t" << node0 << "\t" << vertical_edge_is_boundary_edge << std::endl;
00161
00162 node0 = i*num_nodes_along_width + j + 1;
00163
00164 if (i%2 != 0)
00165 {
00166 node0 = node0 - 1;
00167 }
00168 node1 = (i+1)*num_nodes_along_width + j+1;
00169 node2 = (i+1)*num_nodes_along_width + j;
00170
00171 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
00172 }
00173 }
00174
00175 for (unsigned i=0; i<num_elem_along_length; i++)
00176 {
00177 unsigned node0, node1;
00178
00179 if (i%2==0)
00180 {
00181 node0 = (i+1)*num_nodes_along_width - 1;
00182 node1 = (i+2)*num_nodes_along_width - 1;
00183 }
00184 else
00185 {
00186 node0 = (i+1)*num_nodes_along_width;
00187 node1 = (i)*num_nodes_along_width;
00188 }
00189 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << 1 << std::endl;
00190 }
00191
00192 for (unsigned j=0; j<num_elem_along_width; j++)
00193 {
00194 unsigned node0 = num_nodes_along_width*(num_nodes_along_length-1) + j;
00195 unsigned node1 = num_nodes_along_width*(num_nodes_along_length-1) + j+1;
00196 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node0 << "\t" << 1 << std::endl;
00197 }
00198
00199 p_elem_file->close();
00200 p_edge_file->close();
00201
00202
00203
00204 {
00205 TrianglesMeshReader<2,2> mesh_reader(output_file_handler.GetOutputDirectoryFullPath() + mMeshFilename);
00206 mpMesh = new Cylindrical2dMesh(mDomainWidth);
00207 mpMesh->ConstructFromMeshReader(mesh_reader);
00208 }
00209
00210
00211 mpMesh->ReMesh();
00212
00213
00214 output_file_handler.FindFile("").Remove();
00215
00216
00217 mpMesh->SetMeshHasChangedSinceLoading();
00218 }
00219
00220 MutableMesh<2,2>* CylindricalHoneycombMeshGenerator::GetMesh()
00221 {
00222 EXCEPTION("A cylindrical mesh was created but a normal mesh is being requested.");
00223 return mpMesh;
00224 }
00225
00226 Cylindrical2dMesh* CylindricalHoneycombMeshGenerator::GetCylindricalMesh()
00227 {
00228 return (Cylindrical2dMesh*) mpMesh;
00229 }