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

Generated on Tue May 31 14:31:40 2011 for Chaste by  doxygen 1.5.5