MixedDimensionMesh.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 "MixedDimensionMesh.hpp"
00030 #include "Exception.hpp"
00031 
00032 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00033 MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::MixedDimensionMesh(DistributedTetrahedralMeshPartitionType::type partitioningMethod)
00034     : DistributedTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>::DistributedTetrahedralMesh(partitioningMethod)
00035 {
00036 }
00037 
00038 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::~MixedDimensionMesh()
00040 {
00041     for (unsigned i=0; i<mCableElements.size(); i++)
00042     {
00043         delete mCableElements[i];
00044     }
00045 }
00046 
00047 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00048 void MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::ConstructFromMeshReader(AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>& rMeshReader)
00049 {
00050     DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ConstructFromMeshReader(rMeshReader);
00051     //Note that the above method may permute the node (for a parMETIS partition) after construction
00052     //We have to convert to the permuted form first
00053 
00054     // Add cable elements
00055     mNumCableElements = rMeshReader.GetNumCableElements();
00056     //this->mCableElements.reserve(mNumCableElements);
00057 
00058     for (unsigned element_index=0; element_index < mNumCableElements; element_index++)
00059     {
00060         ElementData element_data = rMeshReader.GetNextCableElementData();
00061         //Convert the node indices from the original to the permuted
00062         if ( this->mNodesPermutation.empty() == false)
00063         {
00064             for (unsigned j=0; j<2; j++) // cables are always 1d
00065             {
00066                 element_data.NodeIndices[j] = this->mNodesPermutation[ element_data.NodeIndices[j] ];
00067             }
00068         }
00069 
00070         //Determine if we own any nodes on this cable element
00071         bool node_owned = false;
00072         for (unsigned j=0; j<2; j++) // cables are always 1d
00073         {
00074             try
00075             {
00076                 this->SolveNodeMapping(element_data.NodeIndices[j]);
00077                 node_owned = true;
00078                 break;
00079             }
00080             catch (Exception &e)
00081             {
00082                 //We deal with non-owned nodes in the next part
00083             }
00084         }
00085 
00086         //If we don't locally own either node, then we don't construct the cable
00087         if (node_owned)
00088         {
00089             std::vector<Node<SPACE_DIM>*> nodes;
00090             nodes.reserve(2u);
00091 
00092             for (unsigned j=0; j<2; j++) // cables are always 1d
00093             {
00094                 //Note that if we own one node on a cable element then we are likely to own the other.
00095                 //If not, we are likely to have a halo.
00096                 //If not, (free-running Purkinje with monodomain mesh?), then this will terminate.
00097                 try
00098                 {
00099                     nodes.push_back(this->GetNodeOrHaloNode(element_data.NodeIndices[j]) );
00100                 }
00101                 catch (Exception& e)
00102                 {
00103                     NEVER_REACHED;
00104                 }
00105             }
00106 
00107             Element<1u, SPACE_DIM>* p_element = new Element<1u,SPACE_DIM>(element_index, nodes);
00108             RegisterCableElement(element_index);
00109             this->mCableElements.push_back(p_element);
00110 
00111             if (rMeshReader.GetNumCableElementAttributes() > 0)
00112             {
00113                 assert(rMeshReader.GetNumCableElementAttributes() == 1);
00114                 p_element->SetRegion(element_data.AttributeValue);
00115             }
00116         }
00117     }
00118 
00119     rMeshReader.Reset();
00120 }
00121 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00122 void MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::RegisterCableElement(unsigned index)
00123 {
00124     mCableElementsMapping[index] = this->mCableElements.size();
00125 }
00126 
00127 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00128 unsigned MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::GetNumCableElements() const
00129 {
00130    return mNumCableElements;
00131 }
00132 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00133 unsigned MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::GetNumLocalCableElements() const
00134 {
00135    return mCableElements.size();
00136 }
00137 
00138 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00139 Element<1u, SPACE_DIM>* MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::GetCableElement(unsigned globalElementIndex) const
00140 {
00141     std::map<unsigned, unsigned>::const_iterator element_position = mCableElementsMapping.find(globalElementIndex);
00142 
00143     if (element_position == mCableElementsMapping.end())
00144     {
00145         EXCEPTION("Requested cable element " << globalElementIndex << " does not belong to processor " << PetscTools::GetMyRank());
00146     }
00147 
00148     unsigned index = element_position->second;
00149 
00150     return mCableElements[index];
00151 }
00152 
00153 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00154 bool MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::CalculateDesignatedOwnershipOfCableElement( unsigned elementIndex )
00155 {
00156     //This should not throw in the distributed parallel case
00157     try
00158     {
00159         unsigned tie_break_index = this->GetCableElement(elementIndex)->GetNodeGlobalIndex(0);
00160 
00161         //if it is in my range
00162         if (this->GetDistributedVectorFactory()->IsGlobalIndexLocal(tie_break_index))
00163         {
00164             return true;
00165         }
00166         else
00167         {
00168             return false;
00169         }
00170     }
00171     catch (Exception &e)
00172     {
00173         //We don't own this cable element
00174         return false;
00175     }
00176 }
00177 
00178 
00179 
00180 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00181 typename MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::CableElementIterator MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::GetCableElementIteratorBegin() const
00182 {
00183     return mCableElements.begin();
00184 }
00185 
00186 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00187 typename MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::CableElementIterator MixedDimensionMesh<ELEMENT_DIM, SPACE_DIM>::GetCableElementIteratorEnd() const
00188 {
00189     return mCableElements.end();
00190 }
00191 
00192 
00193 
00195 // Explicit instantiation
00197 
00198 template class MixedDimensionMesh<1,1>;
00199 template class MixedDimensionMesh<1,2>;
00200 template class MixedDimensionMesh<1,3>;
00201 template class MixedDimensionMesh<2,2>;
00202 template class MixedDimensionMesh<2,3>;
00203 template class MixedDimensionMesh<3,3>;
00204 
00205 // Serialization for Boost >= 1.36
00206 #include "SerializationExportWrapperForCpp.hpp"
00207 EXPORT_TEMPLATE_CLASS_ALL_DIMS(MixedDimensionMesh)
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3