NodesOnlyMesh.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 <map>
00030 #include "NodesOnlyMesh.hpp"
00031 
00032 template<unsigned SPACE_DIM>
00033 void NodesOnlyMesh<SPACE_DIM>::ConstructNodesWithoutMesh(const std::vector<Node<SPACE_DIM>*>& rNodes)
00034 {
00035     this->Clear();
00036 
00037     for (unsigned i=0; i<rNodes.size(); i++)
00038     {
00039         assert(!rNodes[i]->IsDeleted());
00040         c_vector<double, SPACE_DIM> location = rNodes[i]->rGetLocation();
00041 
00042         Node<SPACE_DIM>* p_node_copy = new Node<SPACE_DIM>(i, location);
00043         this->mNodes.push_back(p_node_copy);
00044 
00045         mCellRadii.push_back(1.0);
00046 
00047     }
00048 }
00049 
00050 template<unsigned SPACE_DIM>
00051 void NodesOnlyMesh<SPACE_DIM>::ConstructNodesWithoutMesh(const AbstractMesh<SPACE_DIM,SPACE_DIM>& rGeneratingMesh)
00052 {
00053     ConstructNodesWithoutMesh(rGeneratingMesh.mNodes);
00054 }
00055 
00056 template<unsigned SPACE_DIM>
00057 void NodesOnlyMesh<SPACE_DIM>::Clear()
00058 {
00059     // Call Clear() on the parent class
00060     MutableMesh<SPACE_DIM,SPACE_DIM>::Clear();
00061 
00062     // Clear the cell radii
00063     mCellRadii.clear();
00064 }
00065 
00066 template<unsigned SPACE_DIM>
00067 double NodesOnlyMesh<SPACE_DIM>::GetCellRadius(unsigned index)
00068 {
00069     assert(index < mCellRadii.size());
00070     return mCellRadii[index];
00071 }
00072 
00073 template<unsigned SPACE_DIM>
00074 void NodesOnlyMesh<SPACE_DIM>::SetCellRadius(unsigned index, double radius)
00075 {
00076     assert(index < mCellRadii.size());
00077     mCellRadii[index] = radius;
00078 }
00079 
00080 template<unsigned SPACE_DIM>
00081 void NodesOnlyMesh<SPACE_DIM>::ReMesh(NodeMap& map)
00082 {
00083     // Store the node locations
00084     std::vector<c_vector<double, SPACE_DIM> > old_node_locations;
00085     std::vector<double> old_cell_radii;
00086     bool copy_radii = !mCellRadii.empty();
00087 
00088     unsigned new_index = 0;
00089     for (unsigned i=0; i<this->GetNumAllNodes(); i++)
00090     {
00091         if (this->mNodes[i]->IsDeleted())
00092         {
00093             map.SetDeleted(i);
00094         }
00095         else
00096         {
00097             map.SetNewIndex(i, new_index);
00098             old_node_locations.push_back(this->mNodes[i]->rGetLocation());
00099             if (copy_radii)
00100             {
00101                 old_cell_radii.push_back(mCellRadii[i]);
00102             }
00103 
00104             new_index++;
00105         }
00106     }
00107     // Remove current data
00108     this->Clear();
00109 
00110     // Replace radius data, if need be
00111     mCellRadii = old_cell_radii;
00112 
00113     // Construct the nodes and boundary nodes
00114     for (unsigned node_index=0; node_index<old_node_locations.size(); node_index++)
00115     {
00116         Node<SPACE_DIM>* p_node = new Node<SPACE_DIM>(node_index, old_node_locations[node_index], false);
00117         this->mNodes.push_back(p_node);
00118 
00119         // As we're in 1D, the boundary nodes are simply at either end of the mesh
00120         if (node_index==0 || node_index==old_node_locations.size()-1)
00121         {
00122             this->mBoundaryNodes.push_back(p_node);
00123         }
00124     }
00125 
00126     // Create a map between node indices and node locations
00127     std::map<double, unsigned> location_index_map;
00128     for (unsigned i=0; i<this->mNodes.size(); i++)
00129     {
00130         location_index_map[this->mNodes[i]->rGetLocation()[0]] = this->mNodes[i]->GetIndex();
00131     }
00132 
00133     // Use this map to generate a vector of node indices that are ordered spatially
00134     std::vector<unsigned> node_indices_ordered_spatially;
00135     for (std::map<double, unsigned>::iterator iter = location_index_map.begin();
00136          iter != location_index_map.end();
00137          ++iter)
00138     {
00139         node_indices_ordered_spatially.push_back(iter->second);
00140     }
00141 }
00142 
00143 template<unsigned SPACE_DIM>
00144 unsigned NodesOnlyMesh<SPACE_DIM>::AddNode(Node<SPACE_DIM>* pNewNode)
00145 {
00146     // Call method on parent class
00147     unsigned new_node_index = MutableMesh<SPACE_DIM, SPACE_DIM>::AddNode(pNewNode);
00148 
00149     // Then update mCellRadii
00150     if (new_node_index >= mCellRadii.size())
00151     {
00152         mCellRadii.resize(new_node_index+1);
00153     }
00154     SetCellRadius(new_node_index, 1.0);
00155 
00156     return new_node_index;
00157 }
00158 
00159 template<unsigned SPACE_DIM>
00160 void NodesOnlyMesh<SPACE_DIM>::DeleteNode(unsigned index)
00161 {
00162     if (this->mNodes[index]->IsDeleted())
00163     {
00164         EXCEPTION("Trying to delete a deleted node");
00165     }
00166 
00167     this->mNodes[index]->MarkAsDeleted();
00168     this->mDeletedNodeIndices.push_back(index);
00169 
00175 }
00176 
00178 // Explicit instantiation
00180 
00181 template class NodesOnlyMesh<1>;
00182 template class NodesOnlyMesh<2>;
00183 template class NodesOnlyMesh<3>;
00184 
00185 // Serialization for Boost >= 1.36
00186 #include "SerializationExportWrapperForCpp.hpp"
00187 EXPORT_TEMPLATE_CLASS_SAME_DIMS(NodesOnlyMesh)
Generated on Thu Dec 22 13:00:04 2011 for Chaste by  doxygen 1.6.3