CylindricalHoneycombMeshGenerator.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; //*1 because cells are considered to be size one
00048     mNumCellLength = numNodesAlongLength;
00049     mMeshFilename = "mesh";
00050     mGhostNodeIndices.clear();
00051     // The code below won't work in parallel
00052     assert(PetscTools::IsSequential());
00053 
00054     // An older version of the constructor might allow the wrong argument through to the scale factor
00055     assert(scaleFactor > 0.0);
00056 
00057     // Get a unique mesh filename
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     // This line is needed to define ghost nodes later
00069     mDomainDepth = (double)(num_nodes_along_length) * vertical_spacing;
00070 
00071     // Take account of ghost nodes
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     // Write node file
00087     out_stream p_node_file = output_file_handler.OpenOutputFile(mMeshFilename+".node");
00088     (*p_node_file) << std::scientific;
00089     //(*p_node_file) << std::setprecision(20);
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             // Avoid floating point errors which upset OffLatticeSimulation
00111             if ( (y<0.0) && (y>-1e-12) )
00112             {
00113                 // Difficult to cover - just corrects floating point errors that have occurred from time to time!
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     // Write element file and edge file
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     // Having written the mesh to file, now construct it using TrianglesMeshReader.
00203     // Nested scope so the reader closes files before we delete them below.
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     // Make the mesh cylindrical (we use Triangle library mode inside this ReMesh call)
00211     mpMesh->ReMesh();
00212 
00213     // Delete the temporary files
00214     output_file_handler.FindFile("").Remove();
00215 
00216     // The original files have been deleted, it is better if the mesh object forgets about them
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; // Not really
00224 }
00225 
00226 Cylindrical2dMesh* CylindricalHoneycombMeshGenerator::GetCylindricalMesh()
00227 {
00228     return (Cylindrical2dMesh*) mpMesh;
00229 }

Generated by  doxygen 1.6.2