HoneycombVertexMeshGenerator.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "HoneycombVertexMeshGenerator.hpp"
00030 
00031 
00032 HoneycombVertexMeshGenerator::HoneycombVertexMeshGenerator(unsigned numElementsAcross,
00033                                                            unsigned numElementsUp)
00034 {
00035     assert(numElementsAcross > 0);
00036     assert(numElementsUp > 0);
00037     
00038     std::vector<Node<2>*> nodes;
00039     std::vector<VertexElement<2,2>*>  elements;
00040 
00041     unsigned node_index = 0;
00042     unsigned node_indices[6];
00043     unsigned element_index;
00044 
00045     // Create the nodes, row by row, from the bottom up
00046 
00047     // On the first row we have numElementsAcross nodes, all of which are boundary nodes
00048     for (unsigned i=0; i<numElementsAcross; i++)
00049     {
00050         Node<2>* p_node = new Node<2>(node_index, true, i+0.5, 0);
00051         nodes.push_back(p_node);
00052         node_index++;
00053     }
00054 
00055     /*
00056      * On each interior row we have numElementsAcross+1 nodes. On the second and penultimate
00057      * row all nodes are boundary nodes. On other rows the first and last nodes only
00058      * are boundary nodes.
00059      */
00060     for (unsigned j=1; j<2*numElementsUp+1; j++)
00061     {
00062         for (unsigned i=0; i<=numElementsAcross; i++)
00063         {
00064             double x_coord = ((j%4 == 0)||(j%4 == 3)) ? i+0.5 : i;
00065             double y_coord = (1.5*j - 0.5*(j%2))*0.5/sqrt(3);
00066             bool is_boundary_node = (j==1 || j==2*numElementsUp || i==0 || i==numElementsAcross) ? true : false;
00067 
00068             Node<2>* p_node = new Node<2>(node_index, is_boundary_node, x_coord, y_coord);
00069             nodes.push_back(p_node);
00070             node_index++;
00071         }
00072     }
00073 
00074     /*
00075      * On the last row we have numElementsAcross nodes, all of which are boundary nodes.
00076      */
00077     double y_coord = (1.5*(2*numElementsUp+1) - 0.5*((2*numElementsUp+1)%2))*0.5/sqrt(3);
00078     if (((2*numElementsUp+1)%4 == 0)||((2*numElementsUp+1)%4 == 3))
00079     {
00080         Node<2>* p_node = new Node<2>(node_index, true, 0.5, y_coord);
00081         nodes.push_back(p_node);
00082         node_index++;
00083     }
00084     for (unsigned i=1; i<numElementsAcross; i++)
00085     {
00086         double x_coord = (((2*numElementsUp+1)%4 == 0)||((2*numElementsUp+1)%4 == 3)) ? i+0.5 : i;
00087 
00088         Node<2>* p_node = new Node<2>(node_index, true, x_coord, y_coord);
00089         nodes.push_back(p_node);
00090         node_index++;
00091     }
00092     if (((2*numElementsUp+1)%4 == 1)||((2*numElementsUp+1)%4 == 2))
00093     {
00094         Node<2>* p_node = new Node<2>(node_index, true, numElementsAcross, y_coord);
00095         nodes.push_back(p_node);
00096         node_index++;
00097     }
00098 
00099     /*
00100      * Create the elements. The array node_indices contains the
00101      * global node indices from bottom, going anticlockwise.
00102      */
00103     for (unsigned j=0; j<numElementsUp; j++)
00104     {
00105         for (unsigned i=0; i<numElementsAcross; i++)
00106         {
00107             if (j==0)
00108             {
00109                 node_indices[0] = i;
00110             }
00111             else
00112             {
00113                 node_indices[0] = 2*j*(numElementsAcross+1) - 1*(j%2==0) + i; // different for even/odd rows
00114             }
00115             node_indices[1] = node_indices[0] + numElementsAcross + 1 + 1*(j%2==0 && j>0);
00116             node_indices[2] = node_indices[1] + numElementsAcross + 1;
00117             node_indices[3] = node_indices[2] + numElementsAcross + 1*(j%2==1 && j<numElementsUp-1);
00118             node_indices[4] = node_indices[2] - 1;
00119             node_indices[5] = node_indices[1] - 1;
00120 
00121             std::vector<Node<2>*> element_nodes;
00122             for (unsigned k=0; k<6; k++)
00123             {
00124                element_nodes.push_back(nodes[node_indices[k]]);
00125             }
00126 
00127             element_index = j*numElementsAcross + i;
00128             VertexElement<2,2>* p_element = new VertexElement<2,2>(element_index, element_nodes);
00129             elements.push_back(p_element);
00130         }
00131     }
00132     mpMesh = new VertexMesh<2,2>(nodes, elements);
00133 }
00134 
00135 
00136 HoneycombVertexMeshGenerator::~HoneycombVertexMeshGenerator()
00137 {
00138     delete mpMesh;
00139 }
00140 
00141 VertexMesh<2,2>* HoneycombVertexMeshGenerator::GetMesh()
00142 {
00143     return mpMesh;
00144 }
00145 
00146 
00147 

Generated by  doxygen 1.6.2