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