Chaste Release::3.1
TetrahedralMesh.hpp
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 #ifndef _TETRAHEDRALMESH_HPP_
00037 #define _TETRAHEDRALMESH_HPP_
00038 
00039 #include "ChasteSerialization.hpp"
00040 #include <boost/serialization/base_object.hpp>
00041 
00042 #include "UblasVectorInclude.hpp"
00043 #include "UblasMatrixInclude.hpp"
00044 
00045 #include <vector>
00046 #include <string>
00047 #include <set>
00048 
00049 #include "AbstractTetrahedralMesh.hpp"
00050 #include "AbstractMeshReader.hpp"
00051 #include "ChastePoint.hpp"
00052 
00053 
00054 class triangulateio; 
00056 
00057 //   DECLARATION
00059 
00063 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00064 class TetrahedralMesh : public AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM>
00065 {
00066     friend class TestTetrahedralMesh; // to give access to private methods (not variables)
00067     friend class TestCryptSimulation2d; // to give access to private methods (not variables)
00068 private:
00070     friend class boost::serialization::access;
00077     template<class Archive>
00078     void serialize(Archive & archive, const unsigned int version)
00079     {
00080        archive & boost::serialization::base_object<AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM> >(*this);
00081     }
00082 
00083 protected:
00084 
00090     unsigned SolveNodeMapping(unsigned index) const;
00091 
00097     unsigned SolveElementMapping(unsigned index) const;
00098 
00104     unsigned SolveBoundaryElementMapping(unsigned index) const;
00105 
00107     std::vector< c_vector<double, SPACE_DIM> > mElementWeightedDirections;
00108 
00110     std::vector< c_matrix<double, SPACE_DIM, ELEMENT_DIM> > mElementJacobians;
00111 
00113     std::vector< c_matrix<double, ELEMENT_DIM, SPACE_DIM> > mElementInverseJacobians;
00114 
00116     std::vector<double> mElementJacobianDeterminants;
00117 
00119     std::vector< c_vector<double, SPACE_DIM> > mBoundaryElementWeightedDirections;
00120 
00122     std::vector<double> mBoundaryElementJacobianDeterminants;
00123 
00140     template <class MESHER_IO>
00141     void ExportToMesher(NodeMap& map, MESHER_IO& mesherInput, int *elementList=NULL);
00142 
00159     template <class MESHER_IO>
00160     void ImportFromMesher(MESHER_IO& mesherOutput, unsigned numberOfElements, int *elementList, unsigned numberOfFaces, int *faceList, int *edgeMarkerList);
00161 
00162 
00167     void InitialiseTriangulateIo(triangulateio& mesherIo);
00168 
00173     void FreeTriangulateIo(triangulateio& mesherIo);
00174 
00175 public:
00176 
00180     TetrahedralMesh();
00181 
00187     void ConstructFromMeshReader(AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>& rMeshReader);
00188 
00194     void ReadNodesPerProcessorFile(const std::string& rNodesPerProcessorFile);
00195 
00208     bool CheckIsConforming();
00209 
00214     double GetVolume();
00215 
00219     double GetSurfaceArea();
00220 
00224     void RefreshMesh();
00225 
00230     void PermuteNodes();
00231 
00237     void PermuteNodes(const std::vector<unsigned>& perm);
00238 
00251     unsigned GetContainingElementIndex(const ChastePoint<SPACE_DIM>& rTestPoint,
00252                                        bool strict=false,
00253                                        std::set<unsigned> testElements=std::set<unsigned>(),
00254                                        bool onlyTryWithTestElements = false);
00255 
00256 
00266     unsigned GetContainingElementIndexWithInitialGuess(const ChastePoint<SPACE_DIM>& rTestPoint, unsigned startingElementGuess, bool strict=false);
00267 
00276     unsigned GetNearestElementIndex(const ChastePoint<SPACE_DIM>& rTestPoint);
00277 
00282     unsigned GetNearestElementIndexFromTestElements(const ChastePoint<SPACE_DIM>& rTestPoint,
00283                                                     std::set<unsigned> testElements);
00284 
00285 
00291     std::vector<unsigned> GetContainingElementIndices(const ChastePoint<SPACE_DIM>& rTestPoint);
00292 
00296     virtual void Clear();
00297 
00301     std::set<unsigned> CalculateBoundaryOfFlaggedRegion();
00302 
00310     double GetAngleBetweenNodes(unsigned indexA, unsigned indexB);
00311 
00315     void UnflagAllElements();
00316 
00322     void FlagElementsNotContainingNodes(const std::set<unsigned> rNodes);
00323 
00325     virtual void RefreshJacobianCachedData();
00326 
00334     virtual void GetJacobianForElement(unsigned elementIndex, c_matrix<double, SPACE_DIM, SPACE_DIM>& rJacobian, double& rJacobianDeterminant) const;
00335 
00344     virtual void GetInverseJacobianForElement(unsigned elementIndex, c_matrix<double, SPACE_DIM, ELEMENT_DIM>& rJacobian, double& rJacobianDeterminant, c_matrix<double, ELEMENT_DIM, SPACE_DIM>& rInverseJacobian) const;
00345 
00353     virtual void GetWeightedDirectionForElement(unsigned elementIndex, c_vector<double, SPACE_DIM>& rWeightedDirection, double& rJacobianDeterminant) const;
00354 
00362     virtual void GetWeightedDirectionForBoundaryElement(unsigned elementIndex, c_vector<double, SPACE_DIM>& rWeightedDirection, double& rJacobianDeterminant) const;
00363 
00370     class EdgeIterator
00371     {
00372     public:
00376         Node<SPACE_DIM>* GetNodeA();
00380         Node<SPACE_DIM>* GetNodeB();
00381 
00387         bool operator!=(const TetrahedralMesh<ELEMENT_DIM, SPACE_DIM>::EdgeIterator& rOther);
00388 
00392         EdgeIterator& operator++();
00393 
00400         EdgeIterator(TetrahedralMesh& rMesh, unsigned elemIndex);
00401 
00402 
00403 
00404 
00405     private:
00409         std::set< std::pair<unsigned, unsigned> > mEdgesVisited;
00410 
00411         TetrahedralMesh& mrMesh;   
00413         unsigned mElemIndex;       
00414         unsigned mNodeALocalIndex; 
00415         unsigned mNodeBLocalIndex; 
00417     };
00418 
00422     EdgeIterator EdgesBegin();
00423 
00428     EdgeIterator EdgesEnd();
00429 };
00430 
00431 #include "SerializationExportWrapper.hpp"
00432 EXPORT_TEMPLATE_CLASS_ALL_DIMS(TetrahedralMesh)
00433 
00434 #endif //_TETRAHEDRALMESH_HPP_