Chaste Release::3.1
CylindricalHoneycombMeshGenerator.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 
00042 CylindricalHoneycombMeshGenerator::CylindricalHoneycombMeshGenerator(unsigned numNodesAlongWidth, unsigned numNodesAlongLength, unsigned ghosts, double scaleFactor)
00043 {
00044     mpMesh = NULL;
00045     mDomainWidth = numNodesAlongWidth*scaleFactor;
00046     mNumCellWidth = numNodesAlongWidth; //*1 because cells are considered to be size one
00047     mNumCellLength = numNodesAlongLength;
00048 
00049     // The code below won't work in parallel
00050     assert(PetscTools::IsSequential());
00051 
00052     // An older version of the constructor might allow the wrong argument through to the scale factor
00053     assert(scaleFactor > 0.0);
00054 
00055     // Get a unique mesh filename
00056     std::stringstream pid;
00057     pid << getpid();
00058     mMeshFilename = "2D_temporary_honeycomb_mesh_" + pid.str();
00059 
00060     mGhostNodeIndices.empty();
00061 
00062     OutputFileHandler output_file_handler("");
00063     std::string output_dir = output_file_handler.GetOutputDirectoryFullPath();
00064 
00065     unsigned num_nodes_along_width = mNumCellWidth;
00066     unsigned num_nodes_along_length = mNumCellLength;
00067     double horizontal_spacing = mDomainWidth / (double)num_nodes_along_width;
00068     double vertical_spacing = (sqrt(3)/2)*horizontal_spacing;
00069 
00070     // This line is needed to define ghost nodes later
00071     mDomainDepth = (double)(num_nodes_along_length) * vertical_spacing;
00072 
00073     // Take account of ghost nodes
00074     num_nodes_along_length += 2*ghosts;
00075 
00076     unsigned num_nodes            = num_nodes_along_width*num_nodes_along_length;
00077     unsigned num_elem_along_width = num_nodes_along_width-1;
00078     unsigned num_elem_along_length = num_nodes_along_length-1;
00079     unsigned num_elem             = 2*num_elem_along_width*num_elem_along_length;
00080     unsigned num_edges            = 3*num_elem_along_width*num_elem_along_length + num_elem_along_width + num_elem_along_length;
00081 
00082     double x0 = 0;
00083     double y0 = -vertical_spacing*ghosts;
00084 
00085     mBottom = -vertical_spacing*ghosts;
00086     mTop = mBottom + vertical_spacing*(num_nodes_along_length-1);
00087 
00088     // Write node file
00089     out_stream p_node_file = output_file_handler.OpenOutputFile(mMeshFilename+".node");
00090     (*p_node_file) << std::scientific;
00091     //(*p_node_file) << std::setprecision(20);
00092     (*p_node_file) << num_nodes << "\t2\t0\t1" << std::endl;
00093 
00094     unsigned node = 0;
00095     for (unsigned i=0; i<num_nodes_along_length; i++)
00096     {
00097         for (unsigned j=0; j<num_nodes_along_width; j++)
00098         {
00099             if (i<ghosts || i>=(ghosts+mNumCellLength))
00100             {
00101                 mGhostNodeIndices.insert(node);
00102             }
00103             unsigned boundary = 0;
00104             if ((i==0) || (i==num_nodes_along_length-1))
00105             {
00106                 boundary = 1;
00107             }
00108 
00109             double x = x0 + horizontal_spacing*((double)j + 0.25*(1.0+ SmallPow(-1,i+1)));
00110             double y = y0 + vertical_spacing*(double)i;
00111 
00112             // Avoid floating point errors which upset OffLatticeSimulation
00113             if ( (y<0.0) && (y>-1e-12) )
00114             {
00115                 // Difficult to cover - just corrects floating point errors that have occurred from time to time!
00116                 #define COVERAGE_IGNORE
00117                 y = 0.0;
00118                 #undef COVERAGE_IGNORE
00119             }
00120 
00121             (*p_node_file) << node++ << "\t" << x << "\t" << y << "\t" << boundary << std::endl;
00122         }
00123     }
00124     p_node_file->close();
00125 
00126     // Write element file and edge file
00127     out_stream p_elem_file = output_file_handler.OpenOutputFile(mMeshFilename+".ele");
00128     (*p_elem_file) << std::scientific;
00129 
00130     out_stream p_edge_file = output_file_handler.OpenOutputFile(mMeshFilename+".edge");
00131     (*p_node_file) << std::scientific;
00132 
00133     (*p_elem_file) << num_elem << "\t3\t0" << std::endl;
00134     (*p_edge_file) << num_edges << "\t1" << std::endl;
00135 
00136     unsigned elem = 0;
00137     unsigned edge = 0;
00138     for (unsigned i=0; i<num_elem_along_length; i++)
00139     {
00140         for (unsigned j=0; j < num_elem_along_width; j++)
00141         {
00142             unsigned node0 =     i*num_nodes_along_width + j;
00143             unsigned node1 =     i*num_nodes_along_width + j+1;
00144             unsigned node2 = (i+1)*num_nodes_along_width + j;
00145 
00146             if (i%2 != 0)
00147             {
00148                 node2 = node2 + 1;
00149             }
00150 
00151             (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
00152 
00153             unsigned horizontal_edge_is_boundary_edge = 0;
00154             unsigned vertical_edge_is_boundary_edge = 0;
00155             if (i==0)
00156             {
00157                 horizontal_edge_is_boundary_edge = 1;
00158             }
00159 
00160             (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << horizontal_edge_is_boundary_edge << std::endl;
00161             (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node2 << "\t" << 0 << std::endl;
00162             (*p_edge_file) << edge++ << "\t" << node2 << "\t" << node0 << "\t" << vertical_edge_is_boundary_edge << std::endl;
00163 
00164             node0 = i*num_nodes_along_width + j + 1;
00165 
00166             if (i%2 != 0)
00167             {
00168                 node0 = node0 - 1;
00169             }
00170             node1 = (i+1)*num_nodes_along_width + j+1;
00171             node2 = (i+1)*num_nodes_along_width + j;
00172 
00173             (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
00174         }
00175     }
00176 
00177     for (unsigned i=0; i<num_elem_along_length; i++)
00178     {
00179         unsigned node0, node1;
00180 
00181         if (i%2==0)
00182         {
00183              node0 = (i+1)*num_nodes_along_width - 1;
00184              node1 = (i+2)*num_nodes_along_width - 1;
00185         }
00186         else
00187         {
00188             node0 = (i+1)*num_nodes_along_width;
00189             node1 = (i)*num_nodes_along_width;
00190         }
00191         (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << 1 << std::endl;
00192     }
00193 
00194     for (unsigned j=0; j<num_elem_along_width; j++)
00195     {
00196         unsigned node0 = num_nodes_along_width*(num_nodes_along_length-1) + j;
00197         unsigned node1 = num_nodes_along_width*(num_nodes_along_length-1) + j+1;
00198         (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node0 << "\t" << 1 << std::endl;
00199     }
00200 
00201     p_elem_file->close();
00202     p_edge_file->close();
00203 
00204 
00205     // Having written the mesh to file, now construct it using TrianglesMeshReader
00206     TrianglesMeshReader<2,2> mesh_reader(output_dir + mMeshFilename);
00207     mpMesh = new Cylindrical2dMesh(mDomainWidth);
00208     mpMesh->ConstructFromMeshReader(mesh_reader);
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     FileFinder output_dir_finder(output_dir);
00215     BOOST_FOREACH(FileFinder temp_file, output_dir_finder.FindMatches(mMeshFilename + ".*"))
00216     {
00217         temp_file.Remove(true);
00218     }
00219 
00220     // The original files have been deleted, it is better if the mesh object forgets about them
00221     mpMesh->SetMeshHasChangedSinceLoading();
00222 }
00223 
00224 MutableMesh<2,2>* CylindricalHoneycombMeshGenerator::GetMesh()
00225 {
00226     EXCEPTION("A cylindrical mesh was created but a normal mesh is being requested.");
00227     return mpMesh; // Not really
00228 }
00229 
00230 Cylindrical2dMesh* CylindricalHoneycombMeshGenerator::GetCylindricalMesh()
00231 {
00232     return (Cylindrical2dMesh*) mpMesh;
00233 }