Cylindrical2dVertexMesh.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
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 "Cylindrical2dVertexMesh.hpp"
00030 
00031 Cylindrical2dVertexMesh::Cylindrical2dVertexMesh(double width,
00032                                                  std::vector<Node<2>*> nodes,
00033                                                  std::vector<VertexElement<2, 2>*> vertexElements,
00034                                                  double cellRearrangementThreshold,
00035                                                  double t2Threshold)
00036     : MutableVertexMesh<2,2>(nodes, vertexElements, cellRearrangementThreshold, t2Threshold),
00037       mWidth(width)
00038 {
00039     // ReMesh to remove any deleted nodes and relabel
00040     ReMesh();
00041 }
00042 
00043 Cylindrical2dVertexMesh::Cylindrical2dVertexMesh()
00044 {
00045 }
00046 
00047 Cylindrical2dVertexMesh::~Cylindrical2dVertexMesh()
00048 {
00049 }
00050 
00051 c_vector<double, 2> Cylindrical2dVertexMesh::GetVectorFromAtoB(const c_vector<double, 2>& rLocation1, const c_vector<double, 2>& rLocation2)
00052 {
00053     assert(mWidth > 0.0);
00054 
00055     c_vector<double, 2> vector = rLocation2 - rLocation1;
00056     vector[0] = fmod(vector[0], mWidth);
00057 
00058     // If the points are more than halfway around the cylinder apart, measure the other way
00059     if (vector[0] > mWidth/2.0)
00060     {
00061         vector[0] -= mWidth;
00062     }
00063     else if (vector[0] < -mWidth/2.0)
00064     {
00065         vector[0] += mWidth;
00066     }
00067     return vector;
00068 }
00069 
00070 void Cylindrical2dVertexMesh::SetNode(unsigned nodeIndex, ChastePoint<2> point)
00071 {
00072     double x_coord = point.rGetLocation()[0];
00073 
00074     // Perform a periodic movement if necessary
00075     if (x_coord >= mWidth)
00076     {
00077         // Move point to the left
00078         point.SetCoordinate(0, x_coord - mWidth);
00079     }
00080     else if (x_coord < 0.0)
00081     {
00082         // Move point to the right
00083         point.SetCoordinate(0, x_coord + mWidth);
00084     }
00085 
00086     // Update the node's location
00087     MutableVertexMesh<2,2>::SetNode(nodeIndex, point);
00088 }
00089 
00090 double Cylindrical2dVertexMesh::GetWidth(const unsigned& rDimension) const
00091 {
00092     double width = 0.0;
00093     assert(rDimension==0 || rDimension==1);
00094     if (rDimension==0)
00095     {
00096         width = mWidth;
00097     }
00098     else
00099     {
00100         width = VertexMesh<2,2>::GetWidth(rDimension);
00101     }
00102     return width;
00103 }
00104 
00105 unsigned Cylindrical2dVertexMesh::AddNode(Node<2>* pNewNode)
00106 {
00107     unsigned node_index = MutableVertexMesh<2,2>::AddNode(pNewNode);
00108 
00109     // If necessary move it to be back on the cylinder
00110     ChastePoint<2> new_node_point = pNewNode->GetPoint();
00111     SetNode(node_index, new_node_point);
00112 
00113     return node_index;
00114 }
00115 
00116 double Cylindrical2dVertexMesh::GetVolumeOfElement(unsigned index)
00117 {
00118     VertexElement<2, 2>* p_element = GetElement(index);
00119 
00120     c_vector<double, 2> first_node = p_element->GetNodeLocation(0);
00121     c_vector<double, 2> current_node;
00122     c_vector<double, 2> anticlockwise_node;
00123     c_vector<double, 2> transformed_current_node;
00124     c_vector<double, 2> transformed_anticlockwise_node;
00125 
00126     unsigned num_nodes_in_element = p_element->GetNumNodes();
00127 
00128     double element_area = 0;
00129 
00130     for (unsigned local_index=0; local_index<num_nodes_in_element; local_index++)
00131     {
00132         // Find locations of current node and anticlockwise node
00133         current_node = p_element->GetNodeLocation(local_index);
00134         anticlockwise_node = p_element->GetNodeLocation((local_index+1)%num_nodes_in_element);
00135 
00136         /*
00137          * In order to calculate the area we map the origin to (x[0],y[0])
00138          * then use GetVectorFromAtoB() to get node cooordiantes
00139          */
00140 
00141         transformed_current_node = GetVectorFromAtoB(first_node, current_node);
00142         transformed_anticlockwise_node = GetVectorFromAtoB(first_node, anticlockwise_node);
00143 
00144         element_area += 0.5*(transformed_current_node[0]*transformed_anticlockwise_node[1]
00145                            - transformed_anticlockwise_node[0]*transformed_current_node[1]);
00146     }
00147 
00148     return element_area;
00149 }
00150 
00151 c_vector<double, 2> Cylindrical2dVertexMesh::GetCentroidOfElement(unsigned index)
00152 {
00153     VertexElement<2, 2>* p_element = GetElement(index);
00154 
00155     c_vector<double, 2> centroid;
00156     c_vector<double, 2> transformed_centroid = zero_vector<double>(2);
00157     c_vector<double, 2> first_node = p_element->GetNodeLocation(0);
00158     c_vector<double, 2> current_node_location;
00159     c_vector<double, 2> next_node_location;
00160     c_vector<double, 2> transformed_current_node;
00161     c_vector<double, 2> transformed_anticlockwise_node;
00162 
00163     double temp_centroid_x = 0;
00164     double temp_centroid_y = 0;
00165 
00166     unsigned num_nodes_in_element = p_element->GetNumNodes();
00167 
00168     for (unsigned local_index=0; local_index<num_nodes_in_element; local_index++)
00169     {
00170         // Find locations of current node and anticlockwise node
00171         current_node_location = p_element->GetNodeLocation(local_index);
00172         next_node_location = p_element->GetNodeLocation((local_index+1)%num_nodes_in_element);
00173 
00174         /*
00175          * In order to calculate the centroid we map the origin to (x[0],y[0])
00176          * then use  GetVectorFromAtoB() to get node cooordiantes
00177          */
00178 
00179         transformed_current_node = GetVectorFromAtoB(first_node, current_node_location);
00180         transformed_anticlockwise_node = GetVectorFromAtoB(first_node, next_node_location);
00181 
00182         temp_centroid_x += (transformed_current_node[0]+transformed_anticlockwise_node[0])*(transformed_current_node[0]*transformed_anticlockwise_node[1]-transformed_current_node[1]*transformed_anticlockwise_node[0]);
00183         temp_centroid_y += (transformed_current_node[1]+transformed_anticlockwise_node[1])*(transformed_current_node[0]*transformed_anticlockwise_node[1]-transformed_current_node[1]*transformed_anticlockwise_node[0]);
00184     }
00185 
00186     double vertex_area = GetVolumeOfElement(index);
00187     double centroid_coefficient = 1.0/(6.0*vertex_area);
00188 
00189     transformed_centroid(0) = centroid_coefficient*temp_centroid_x;
00190     transformed_centroid(1) = centroid_coefficient*temp_centroid_y;
00191 
00192     centroid = transformed_centroid + first_node;
00193 
00194     return centroid;
00195 }
00196 
00197 // Serialization for Boost >= 1.36
00198 #include "SerializationExportWrapperForCpp.hpp"
00199 CHASTE_CLASS_EXPORT(Cylindrical2dVertexMesh)
Generated on Thu Dec 22 13:00:04 2011 for Chaste by  doxygen 1.6.3