Chaste  Release::3.4
VertexMesh.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #ifndef VERTEXMESH_HPP_
36 #define VERTEXMESH_HPP_
37 
38 // Forward declaration prevents circular include chain
39 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
40 class VertexMeshWriter;
41 
42 #include <iostream>
43 #include <map>
44 #include <algorithm>
45 
46 #include "ChasteSerialization.hpp"
47 #include <boost/serialization/vector.hpp>
48 #include <boost/serialization/base_object.hpp>
49 #include <boost/serialization/split_member.hpp>
50 
51 #include "AbstractMesh.hpp"
52 #include "ArchiveLocationInfo.hpp"
53 #include "VertexMeshReader.hpp"
54 #include "VertexMeshWriter.hpp"
55 #include "VertexElement.hpp"
56 #include "VertexElementMap.hpp"
57 #include "TetrahedralMesh.hpp"
58 
73 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
74 class VertexMesh : public AbstractMesh<ELEMENT_DIM, SPACE_DIM>
75 {
76  friend class TestVertexMesh;
77 
78 protected:
79 
81  std::vector<VertexElement<ELEMENT_DIM, SPACE_DIM>*> mElements;
82 
84  std::vector<VertexElement<ELEMENT_DIM-1, SPACE_DIM>*> mFaces;
85 
93  std::map<unsigned, unsigned> mVoronoiElementIndexMap;
94 
103 
111  unsigned SolveNodeMapping(unsigned index) const;
112 
120  unsigned SolveElementMapping(unsigned index) const;
121 
129  unsigned SolveBoundaryElementMapping(unsigned index) const;
130 
139 
160  bool ElementIncludesPoint(const c_vector<double, SPACE_DIM>& rTestPoint, unsigned elementIndex);
161 
171  unsigned GetLocalIndexForElementEdgeClosestToPoint(const c_vector<double, SPACE_DIM>& rTestPoint, unsigned elementIndex);
172 
174  friend class boost::serialization::access;
175 
183  template<class Archive>
184  void save(Archive & archive, const unsigned int version) const
185  {
186  archive & boost::serialization::base_object<AbstractMesh<ELEMENT_DIM,SPACE_DIM> >(*this);
187 
188  // Create a mesh writer pointing to the correct file and directory
191  false);
192  mesh_writer.WriteFilesUsingMesh(*(const_cast<VertexMesh<ELEMENT_DIM, SPACE_DIM>*>(this)));
193  }
194 
201  template<class Archive>
202  void load(Archive & archive, const unsigned int version)
203  {
204  archive & boost::serialization::base_object<AbstractMesh<ELEMENT_DIM,SPACE_DIM> >(*this);
205 
207  this->ConstructFromMeshReader(mesh_reader);
208  }
209  BOOST_SERIALIZATION_SPLIT_MEMBER()
210 
211 public:
212 
214  class VertexElementIterator;
215 
221  inline VertexElementIterator GetElementIteratorBegin(bool skipDeletedElements=true);
222 
226  inline VertexElementIterator GetElementIteratorEnd();
227 
234  VertexMesh(std::vector<Node<SPACE_DIM>*> nodes,
235  std::vector<VertexElement<ELEMENT_DIM, SPACE_DIM>*> vertexElements);
236 
244  VertexMesh(std::vector<Node<SPACE_DIM>*> nodes,
245  std::vector<VertexElement<ELEMENT_DIM-1,SPACE_DIM>*> faces,
246  std::vector<VertexElement<ELEMENT_DIM,SPACE_DIM>*> vertexElements);
247 
257  VertexMesh(TetrahedralMesh<2,2>& rMesh, bool isPeriodic=false);
258 
267  VertexMesh(TetrahedralMesh<3,3>& rMesh);
268 
272  VertexMesh();
273 
277  virtual ~VertexMesh();
278 
282  virtual unsigned GetNumNodes() const;
283 
287  virtual unsigned GetNumElements() const;
288 
292  unsigned GetNumAllElements() const;
293 
297  virtual unsigned GetNumFaces() const;
298 
304  VertexElement<ELEMENT_DIM, SPACE_DIM>* GetElement(unsigned index) const;
305 
311  VertexElement<ELEMENT_DIM-1, SPACE_DIM>* GetFace(unsigned index) const;
312 
328  virtual c_vector<double, SPACE_DIM> GetCentroidOfElement(unsigned index);
329 
335  void ConstructFromMeshReader(AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>& rMeshReader);
336 
340  virtual void Clear();
341 
347  unsigned GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(unsigned elementIndex);
348 
356  unsigned GetVoronoiElementIndexCorrespondingToDelaunayNodeIndex(unsigned nodeIndex);
357 
367  unsigned GetRosetteRankOfElement(unsigned index);
368 
380  virtual c_vector<double, SPACE_DIM> GetVectorFromAtoB(const c_vector<double, SPACE_DIM>& rLocationA,
381  const c_vector<double, SPACE_DIM>& rLocationB);
382 
392  virtual double GetVolumeOfElement(unsigned index);
393 
403  virtual double GetSurfaceAreaOfElement(unsigned index);
404 
416  c_vector<double, SPACE_DIM> GetAreaGradientOfElementAtNode(VertexElement<ELEMENT_DIM,SPACE_DIM>* pElement, unsigned localIndex);
417 
429  c_vector<double, SPACE_DIM> GetPreviousEdgeGradientOfElementAtNode(VertexElement<ELEMENT_DIM,SPACE_DIM>* pElement, unsigned localIndex);
430 
442  c_vector<double, SPACE_DIM> GetNextEdgeGradientOfElementAtNode(VertexElement<ELEMENT_DIM,SPACE_DIM>* pElement, unsigned localIndex);
443 
453  c_vector<double, SPACE_DIM> GetPerimeterGradientOfElementAtNode(VertexElement<ELEMENT_DIM,SPACE_DIM>* pElement, unsigned localIndex);
454 
484  virtual c_vector<double, 3> CalculateMomentsOfElement(unsigned index);
485 
492  double GetEdgeLength(unsigned elementIndex1, unsigned elementIndex2);
493 
504  double GetElongationShapeFactorOfElement(unsigned elementIndex);
505 
517  double CalculateUnitNormalToFaceWithArea(VertexElement<ELEMENT_DIM-1, SPACE_DIM>* pFace, c_vector<double, SPACE_DIM>& rNormal);
518 
528  virtual double CalculateAreaOfFace(VertexElement<ELEMENT_DIM-1, SPACE_DIM>* pFace);
529 
555  c_vector<double, SPACE_DIM> GetShortAxisOfElement(unsigned index);
556 
563  std::set<unsigned> GetNeighbouringNodeIndices(unsigned nodeIndex);
564 
577  std::set<unsigned> GetNeighbouringNodeNotAlsoInElement(unsigned nodeIndex, unsigned elemIndex);
578 
585  std::set<unsigned> GetNeighbouringElementIndices(unsigned elementIndex);
586 
592  class VertexElementIterator
593  {
594  public:
600  inline VertexElement<ELEMENT_DIM, SPACE_DIM>& operator*();
601 
606  inline VertexElement<ELEMENT_DIM, SPACE_DIM>* operator->();
607 
613  inline bool operator!=(const typename VertexMesh<ELEMENT_DIM, SPACE_DIM>::VertexElementIterator& rOther);
614 
619  inline VertexElementIterator& operator++();
620 
631  VertexElementIterator(VertexMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
632  typename std::vector<VertexElement<ELEMENT_DIM, SPACE_DIM> *>::iterator elementIter,
633  bool skipDeletedElements=true);
634 
635  private:
637  VertexMesh& mrMesh;
638 
640  typename std::vector<VertexElement<ELEMENT_DIM, SPACE_DIM> *>::iterator mElementIter;
641 
644 
649  inline bool IsAtEnd();
650 
655  inline bool IsAllowedElement();
656  };
657 };
658 
661 
662 
663 // VertexElementIterator class implementation - most methods are inlined //
666 
667 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
668 typename VertexMesh<ELEMENT_DIM, SPACE_DIM>::VertexElementIterator VertexMesh<ELEMENT_DIM, SPACE_DIM>::GetElementIteratorBegin(
669  bool skipDeletedElements)
670 {
671  return VertexElementIterator(*this, mElements.begin(), skipDeletedElements);
672 }
673 
674 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
676 {
677  return VertexElementIterator(*this, mElements.end());
678 }
679 
680 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
682 {
683  assert(!IsAtEnd());
684  return **mElementIter;
685 }
686 
687 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
689 {
690  assert(!IsAtEnd());
691  return *mElementIter;
692 }
693 
694 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
696 {
697  return mElementIter != rOther.mElementIter;
698 }
699 
700 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
702 {
703  do
704  {
705  ++mElementIter;
706  }
707  while (!IsAtEnd() && !IsAllowedElement());
708 
709  return (*this);
710 }
711 
712 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
715  typename std::vector<VertexElement<ELEMENT_DIM, SPACE_DIM> *>::iterator elementIter,
716  bool skipDeletedElements)
717  : mrMesh(rMesh),
718  mElementIter(elementIter),
719  mSkipDeletedElements(skipDeletedElements)
720 {
721  if (mrMesh.mElements.empty())
722  {
723  // Cope with empty meshes
724  mElementIter = mrMesh.mElements.end();
725  }
726  else
727  {
728  // Make sure we start at an allowed element
729  if (mElementIter == mrMesh.mElements.begin() && !IsAllowedElement())
730  {
731  ++(*this);
732  }
733  }
734 }
735 
736 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
738 {
739  return mElementIter == mrMesh.mElements.end();
740 }
741 
742 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
744 {
745  return !(mSkipDeletedElements && (*this)->IsDeleted());
746 }
747 
748 #endif /*VERTEXMESH_HPP_*/
virtual c_vector< double, 3 > CalculateMomentsOfElement(unsigned index)
void ConstructFromMeshReader(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader)
virtual void Clear()
Definition: VertexMesh.cpp:585
VertexElement< ELEMENT_DIM-1, SPACE_DIM > * GetFace(unsigned index) const
Definition: VertexMesh.cpp:647
unsigned GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(unsigned elementIndex)
Definition: VertexMesh.cpp:506
virtual unsigned GetNumFaces() const
Definition: VertexMesh.cpp:632
VertexElement< ELEMENT_DIM, SPACE_DIM > * operator->()
Definition: VertexMesh.hpp:688
VertexElement< ELEMENT_DIM, SPACE_DIM > & operator*()
Definition: VertexMesh.hpp:681
virtual c_vector< double, SPACE_DIM > GetVectorFromAtoB(const c_vector< double, SPACE_DIM > &rLocationA, const c_vector< double, SPACE_DIM > &rLocationB)
std::set< unsigned > GetNeighbouringNodeIndices(unsigned nodeIndex)
Definition: VertexMesh.cpp:727
Definition: Node.hpp:58
virtual unsigned GetNumElements() const
Definition: VertexMesh.cpp:618
static std::string GetMeshFilename()
unsigned GetNumAllElements() const
Definition: VertexMesh.cpp:625
void WriteFilesUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
VertexElementIterator(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, typename std::vector< VertexElement< ELEMENT_DIM, SPACE_DIM > * >::iterator elementIter, bool skipDeletedElements=true)
Definition: VertexMesh.hpp:713
void save(Archive &archive, const unsigned int version) const
Definition: VertexMesh.hpp:184
virtual unsigned GetNumNodes() const
Definition: VertexMesh.cpp:611
void GenerateVerticesFromElementCircumcentres(TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
Definition: VertexMesh.cpp:386
void load(Archive &archive, const unsigned int version)
Definition: VertexMesh.hpp:202
c_vector< double, SPACE_DIM > GetNextEdgeGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
std::set< unsigned > GetNeighbouringNodeNotAlsoInElement(unsigned nodeIndex, unsigned elemIndex)
Definition: VertexMesh.cpp:758
bool operator!=(const typename VertexMesh< ELEMENT_DIM, SPACE_DIM >::VertexElementIterator &rOther)
Definition: VertexMesh.hpp:695
VertexElement< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
Definition: VertexMesh.cpp:639
VertexElementIterator & operator++()
Definition: VertexMesh.hpp:701
c_vector< double, SPACE_DIM > GetPreviousEdgeGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
VertexElementIterator GetElementIteratorBegin(bool skipDeletedElements=true)
Definition: VertexMesh.hpp:668
double CalculateUnitNormalToFaceWithArea(VertexElement< ELEMENT_DIM-1, SPACE_DIM > *pFace, c_vector< double, SPACE_DIM > &rNormal)
virtual double CalculateAreaOfFace(VertexElement< ELEMENT_DIM-1, SPACE_DIM > *pFace)
double GetElongationShapeFactorOfElement(unsigned elementIndex)
Definition: VertexMesh.cpp:447
virtual double GetVolumeOfElement(unsigned index)
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
std::vector< VertexElement< ELEMENT_DIM, SPACE_DIM > * > mElements
Definition: VertexMesh.hpp:81
unsigned SolveNodeMapping(unsigned index) const
Definition: VertexMesh.cpp:481
c_vector< double, SPACE_DIM > GetPerimeterGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
unsigned SolveBoundaryElementMapping(unsigned index) const
Definition: VertexMesh.cpp:497
virtual c_vector< double, SPACE_DIM > GetCentroidOfElement(unsigned index)
Definition: VertexMesh.cpp:655
std::set< unsigned > GetNeighbouringElementIndices(unsigned elementIndex)
Definition: VertexMesh.cpp:799
std::vector< VertexElement< ELEMENT_DIM, SPACE_DIM > * >::iterator mElementIter
Definition: VertexMesh.hpp:640
c_vector< double, SPACE_DIM > GetAreaGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
static std::string GetArchiveDirectory()
VertexElementIterator GetElementIteratorEnd()
Definition: VertexMesh.hpp:675
std::vector< VertexElement< ELEMENT_DIM-1, SPACE_DIM > * > mFaces
Definition: VertexMesh.hpp:84
double GetEdgeLength(unsigned elementIndex1, unsigned elementIndex2)
Definition: VertexMesh.cpp:412
static std::string GetArchiveRelativePath()
unsigned GetVoronoiElementIndexCorrespondingToDelaunayNodeIndex(unsigned nodeIndex)
Definition: VertexMesh.cpp:533
virtual double GetSurfaceAreaOfElement(unsigned index)
unsigned SolveElementMapping(unsigned index) const
Definition: VertexMesh.cpp:489
bool ElementIncludesPoint(const c_vector< double, SPACE_DIM > &rTestPoint, unsigned elementIndex)
unsigned GetLocalIndexForElementEdgeClosestToPoint(const c_vector< double, SPACE_DIM > &rTestPoint, unsigned elementIndex)
c_vector< double, SPACE_DIM > GetShortAxisOfElement(unsigned index)
TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpDelaunayMesh
Definition: VertexMesh.hpp:102
std::map< unsigned, unsigned > mVoronoiElementIndexMap
Definition: VertexMesh.hpp:93
unsigned GetRosetteRankOfElement(unsigned index)
Definition: VertexMesh.cpp:560