Chaste Release::3.1
SemMeshGenerator.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 "SemMeshGenerator.hpp"
00037 
00038 template<unsigned DIM>
00039 SemMeshGenerator<DIM>::SemMeshGenerator(unsigned numCellsAcross,
00040                                             unsigned numCellsUp,
00041                                             unsigned numCellsDeep,
00042                                             unsigned numSubCellularElementsPerCellAcross,
00043                                             unsigned numSubCellularElementsPerCellUp,
00044                                             unsigned numSubCellularElementsPerCellDeep)
00045 {
00046     mTotalSubcellularElementsPerCell = numSubCellularElementsPerCellAcross + numSubCellularElementsPerCellUp + numSubCellularElementsPerCellDeep;
00047 
00048     // Check input is of correct type
00049     if (numCellsAcross == 0)
00050     {
00051         EXCEPTION("An SemMeshGenerator must have numCellsAcross > 0");
00052     }
00053     if (numSubCellularElementsPerCellAcross == 0)
00054     {
00055         EXCEPTION("An SemMeshGenerator must have more than 1 subcellular element per cell. Set numSubCellularElementsPerCell > 0");
00056     }
00057 
00058     // Make sure the dimensions match the number of cells up/deep
00059     if (DIM == 1)
00060     {
00061         EXCEPTION("Trying to create a 1D mesh, SemMesh only defined for 2 and 3D");
00062     }
00063     if (numCellsDeep > 1 && DIM < 3)
00064     {
00065         EXCEPTION("Trying to create a 3D SemMesh with DIM < 3");
00066     }
00067 
00068     mTotalSubcellularElementsPerCell = numSubCellularElementsPerCellAcross*+ numSubCellularElementsPerCellUp * numSubCellularElementsPerCellDeep;
00069 
00078     double packing_density_of_spheres_2d = M_PI / (2.0 * sqrt(3.0));
00079     double packing_density_of_spheres_3d = M_PI / (3.0 * sqrt(2.0));
00080 
00081 
00082     double equlibrium_distance;
00083     switch (DIM)
00084     {
00085         case 2:
00086         {
00087             equlibrium_distance = 2.0 * sqrt(packing_density_of_spheres_2d / mTotalSubcellularElementsPerCell);
00088             break;
00089         }
00090         case 3:
00091         {
00092             equlibrium_distance = 2.0 * pow((packing_density_of_spheres_3d / mTotalSubcellularElementsPerCell), 1.0/3.0);
00093             break;
00094         }
00095         default:
00096         {
00097             NEVER_REACHED;
00098         }
00099     }
00100 
00101     mNodeEquilibriumDistance = equlibrium_distance;
00102 
00103     // Create a cube of nodes for each cell.
00104     std::vector<PottsElement<DIM>* > elements;
00105     std::vector<Node<DIM>* > nodes;
00106 
00107     unsigned node_index_counter = 0;
00108     unsigned element_counter = 0;
00109     for (unsigned cells_deep = 0; cells_deep < numCellsDeep; cells_deep++)                                          // Cells in the z direction
00110     {
00111         for (unsigned cells_up = 0; cells_up < numCellsUp; cells_up++)                                                  // Cells in the y direction
00112         {
00113             for (unsigned cells_across = 0; cells_across < numCellsAcross; cells_across++)                                  // Cells in the x direction
00114             {
00115                 std::vector<Node<DIM>* > local_nodes;
00116                 for (unsigned nodes_deep = 0; nodes_deep < numSubCellularElementsPerCellDeep; nodes_deep++)                     // Nodes (subcellular elements) in the z direction
00117                 {
00118                     for (unsigned nodes_up = 0; nodes_up < numSubCellularElementsPerCellUp; nodes_up++)                             // Nodes (subcellular elements) in the y direction
00119                     {
00120                         for (unsigned nodes_across = 0; nodes_across < numSubCellularElementsPerCellAcross; nodes_across++)             // Nodes (subcellular elements) in the x direction
00121                         {
00122                             Node<DIM>* p_node = new Node<DIM>(node_index_counter++,
00123                                                                 false,
00124                                                                 ((cells_across * numSubCellularElementsPerCellAcross + nodes_across) * equlibrium_distance),
00125                                                                 ((cells_up * numSubCellularElementsPerCellUp + nodes_up) * equlibrium_distance),
00126                                                                 ((cells_deep * numSubCellularElementsPerCellDeep + nodes_deep) * equlibrium_distance));
00127                             local_nodes.push_back(p_node);
00128                             nodes.push_back(p_node);
00129                         }
00130                     }
00131                 }
00132                 elements.push_back(new PottsElement<DIM>(element_counter++, local_nodes));
00133             }
00134         }
00135     }
00136 
00137     mpMesh = new SemMesh<DIM>(nodes, elements);
00138 }
00139 
00140 template<unsigned DIM>
00141 SemMeshGenerator<DIM>::~SemMeshGenerator()
00142 {
00143     delete mpMesh;
00144 }
00145 
00146 template<unsigned DIM>
00147 double SemMeshGenerator<DIM>::GetEquilibriumDistance()
00148 {
00149     return mNodeEquilibriumDistance;
00150 }
00151 
00152 template<unsigned DIM>
00153 SemMesh<DIM>* SemMeshGenerator<DIM>::GetMesh()
00154 {
00155     return mpMesh;
00156 }
00157 
00159 // Explicit instantiation
00161 
00162 template class SemMeshGenerator<1>;
00163 template class SemMeshGenerator<2>;
00164 template class SemMeshGenerator<3>;