Chaste  Release::3.4
PottsMesh.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 
36 #ifndef POTTSMESH_HPP_
37 #define POTTSMESH_HPP_
38 
39 // Forward declaration prevents circular include chain
40 template<unsigned DIM>
42 
43 #include <iostream>
44 #include <map>
45 #include <algorithm>
46 
47 #include "ChasteSerialization.hpp"
48 #include <boost/serialization/vector.hpp>
49 #include <boost/serialization/set.hpp>
50 #include <boost/serialization/base_object.hpp>
51 #include <boost/serialization/split_member.hpp>
52 
53 #include "AbstractMesh.hpp"
54 #include "ArchiveLocationInfo.hpp"
55 #include "PottsMeshReader.hpp"
56 #include "PottsMeshWriter.hpp"
57 #include "PottsElement.hpp"
58 
62 template<unsigned DIM>
63 class PottsMesh : public AbstractMesh<DIM, DIM>
64 {
65  friend class TestPottsMesh;
66 
67 protected:
69  std::vector<PottsElement<DIM>*> mElements;
70 
75  std::vector<unsigned> mDeletedElementIndices;
76 
78  std::vector< std::set<unsigned> > mVonNeumannNeighbouringNodeIndices;
79 
81  std::vector< std::set<unsigned> > mMooreNeighbouringNodeIndices;
82 
90  unsigned SolveNodeMapping(unsigned index) const;
91 
99  unsigned SolveElementMapping(unsigned index) const;
100 
108  unsigned SolveBoundaryElementMapping(unsigned index) const;
109 
111  friend class boost::serialization::access;
112 
120  template<class Archive>
121  void save(Archive & archive, const unsigned int version) const
122  {
123  // NOTE - Subclasses must archive their member variables BEFORE calling this method.
124  archive & mDeletedElementIndices;
127  archive & boost::serialization::base_object<AbstractMesh<DIM, DIM> >(*this);
128 
129  // Create a mesh writer pointing to the correct file and directory
132  false);
133  mesh_writer.WriteFilesUsingMesh(*(const_cast<PottsMesh<DIM>*>(this)));
134  }
135 
142  template<class Archive>
143  void load(Archive & archive, const unsigned int version)
144  {
145  // NOTE - Subclasses must archive their member variables BEFORE calling this method.
146  archive & mDeletedElementIndices;
149  archive & boost::serialization::base_object<AbstractMesh<DIM, DIM> >(*this);
150 
152  this->ConstructFromMeshReader(mesh_reader);
153  }
154  BOOST_SERIALIZATION_SPLIT_MEMBER()
155 
156 public:
157 
159  // Iterators //
161 
163  class PottsElementIterator;
164 
170  inline PottsElementIterator GetElementIteratorBegin(bool skipDeletedElements=true);
171 
175  inline PottsElementIterator GetElementIteratorEnd();
176 
178  // Methods //
180 
189  PottsMesh(std::vector<Node<DIM>*> nodes,
190  std::vector<PottsElement<DIM>*> pottsElements,
191  std::vector< std::set<unsigned> > vonNeumannNeighbouringNodeIndices,
192  std::vector< std::set<unsigned> > mooreNeighbouringNodeIndices);
193 
197  PottsMesh();
198 
202  virtual ~PottsMesh();
203 
207  virtual unsigned GetNumNodes() const;
208 
212  virtual unsigned GetNumElements() const;
213 
217  unsigned GetNumAllElements() const;
218 
224  PottsElement<DIM>* GetElement(unsigned index) const;
225 
235  virtual c_vector<double, DIM> GetCentroidOfElement(unsigned index);
236 
242  void ConstructFromMeshReader(AbstractMeshReader<DIM, DIM>& rMeshReader);
243 
247  virtual void Clear();
248 
258  virtual double GetVolumeOfElement(unsigned index);
259 
269  virtual double GetSurfaceAreaOfElement(unsigned index);
270 
277  std::set<unsigned> GetMooreNeighbouringNodeIndices(unsigned nodeIndex);
278 
285  std::set<unsigned> GetVonNeumannNeighbouringNodeIndices(unsigned nodeIndex);
286 
292  void DeleteNode(unsigned index);
293 
300  void DeleteElement(unsigned index);
301 
306  void RemoveDeletedElements();
307 
318  unsigned DivideElement(PottsElement<DIM>* pElement,
319  bool placeOriginalElementBelow=false);
320 
328  unsigned AddElement(PottsElement<DIM>* pNewElement);
329 
336  std::set<unsigned> GetNeighbouringElementIndices(unsigned elementIndex);
337 
338 
339 
340 
342  // Nested classes //
344 
350  class PottsElementIterator
351  {
352  public:
359  inline PottsElement<DIM>& operator*();
360 
365  inline PottsElement<DIM>* operator->();
366 
373  inline bool operator!=(const typename PottsMesh<DIM>::PottsElementIterator& rOther);
374 
379  inline PottsElementIterator& operator++();
380 
391  PottsElementIterator(PottsMesh<DIM>& rMesh,
392  typename std::vector<PottsElement<DIM> *>::iterator elementIter,
393  bool skipDeletedElements=true);
394 
395  private:
398 
400  typename std::vector<PottsElement<DIM> *>::iterator mElementIter;
401 
404 
408  inline bool IsAtEnd();
409 
413  inline bool IsAllowedElement();
414  };
415 };
416 
419 
420 // PottsElementIterator class implementation - most methods are inlined //
423 
424 template<unsigned DIM>
425 typename PottsMesh<DIM>::PottsElementIterator PottsMesh<DIM>::GetElementIteratorBegin(
426  bool skipDeletedElements)
427 {
428  return PottsElementIterator(*this, mElements.begin(), skipDeletedElements);
429 }
430 
431 template<unsigned DIM>
433 {
434  return PottsElementIterator(*this, mElements.end());
435 }
436 template<unsigned DIM>
438 {
439  assert(!IsAtEnd());
440  return **mElementIter;
441 }
442 
443 template<unsigned DIM>
445 {
446  assert(!IsAtEnd());
447  return *mElementIter;
448 }
449 
450 template<unsigned DIM>
452 {
453  return mElementIter != rOther.mElementIter;
454 }
455 
456 template<unsigned DIM>
458 {
459  do
460  {
461  ++mElementIter;
462  }
463  while (!IsAtEnd() && !IsAllowedElement());
464 
465  return (*this);
466 }
467 
468 template<unsigned DIM>
470  PottsMesh<DIM>& rMesh,
471  typename std::vector<PottsElement<DIM>*>::iterator elementIter,
472  bool skipDeletedElements)
473  : mrMesh(rMesh),
474  mElementIter(elementIter),
475  mSkipDeletedElements(skipDeletedElements)
476 {
477  if (mrMesh.mElements.empty())
478  {
479  // Cope with empty meshes
480  mElementIter = mrMesh.mElements.end();
481  }
482  else
483  {
484  // Make sure we start at an allowed element
485  if (mElementIter == mrMesh.mElements.begin() && !IsAllowedElement())
486  {
487  ++(*this);
488  }
489  }
490 }
491 
492 template<unsigned DIM>
494 {
495  return mElementIter == mrMesh.mElements.end();
496 }
497 
498 template<unsigned DIM>
500 {
501  return !(mSkipDeletedElements && (*this)->IsDeleted());
502 }
503 
504 #endif /*POTTSMESH_HPP_*/
unsigned AddElement(PottsElement< DIM > *pNewElement)
Definition: PottsMesh.cpp:515
unsigned SolveBoundaryElementMapping(unsigned index) const
Definition: PottsMesh.cpp:116
Definition: Node.hpp:58
void DeleteNode(unsigned index)
Definition: PottsMesh.cpp:274
void DeleteElement(unsigned index)
Definition: PottsMesh.cpp:246
virtual double GetVolumeOfElement(unsigned index)
Definition: PottsMesh.cpp:191
static std::string GetMeshFilename()
void load(Archive &archive, const unsigned int version)
Definition: PottsMesh.hpp:143
void ConstructFromMeshReader(AbstractMeshReader< DIM, DIM > &rMeshReader)
Definition: PottsMesh.cpp:575
std::vector< std::set< unsigned > > mMooreNeighbouringNodeIndices
Definition: PottsMesh.hpp:81
void RemoveDeletedElements()
Definition: PottsMesh.cpp:254
std::set< unsigned > GetVonNeumannNeighbouringNodeIndices(unsigned nodeIndex)
Definition: PottsMesh.cpp:240
unsigned GetNumAllElements() const
Definition: PottsMesh.cpp:158
void WriteFilesUsingMesh(PottsMesh< SPACE_DIM > &rMesh)
std::vector< PottsElement< DIM > * > mElements
Definition: PottsMesh.hpp:69
void save(Archive &archive, const unsigned int version) const
Definition: PottsMesh.hpp:121
std::set< unsigned > GetMooreNeighbouringNodeIndices(unsigned nodeIndex)
Definition: PottsMesh.cpp:234
unsigned SolveElementMapping(unsigned index) const
Definition: PottsMesh.cpp:109
unsigned DivideElement(PottsElement< DIM > *pElement, bool placeOriginalElementBelow=false)
Definition: PottsMesh.cpp:388
PottsElement< DIM > * operator->()
Definition: PottsMesh.hpp:444
virtual c_vector< double, DIM > GetCentroidOfElement(unsigned index)
Definition: PottsMesh.cpp:171
virtual void Clear()
Definition: PottsMesh.cpp:122
PottsElementIterator(PottsMesh< DIM > &rMesh, typename std::vector< PottsElement< DIM > * >::iterator elementIter, bool skipDeletedElements=true)
Definition: PottsMesh.hpp:469
PottsElementIterator GetElementIteratorBegin(bool skipDeletedElements=true)
Definition: PottsMesh.hpp:425
virtual unsigned GetNumNodes() const
Definition: PottsMesh.cpp:146
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
bool operator!=(const typename PottsMesh< DIM >::PottsElementIterator &rOther)
Definition: PottsMesh.hpp:451
virtual double GetSurfaceAreaOfElement(unsigned index)
Definition: PottsMesh.cpp:200
PottsElementIterator GetElementIteratorEnd()
Definition: PottsMesh.hpp:432
std::set< unsigned > GetNeighbouringElementIndices(unsigned elementIndex)
Definition: PottsMesh.cpp:532
std::vector< unsigned > mDeletedElementIndices
Definition: PottsMesh.hpp:75
PottsElement< DIM > * GetElement(unsigned index) const
Definition: PottsMesh.cpp:164
PottsElement< DIM > & operator*()
Definition: PottsMesh.hpp:437
std::vector< PottsElement< DIM > * >::iterator mElementIter
Definition: PottsMesh.hpp:400
virtual unsigned GetNumElements() const
Definition: PottsMesh.cpp:152
unsigned SolveNodeMapping(unsigned index) const
Definition: PottsMesh.cpp:102
static std::string GetArchiveDirectory()
static std::string GetArchiveRelativePath()
std::vector< std::set< unsigned > > mVonNeumannNeighbouringNodeIndices
Definition: PottsMesh.hpp:78
PottsElementIterator & operator++()
Definition: PottsMesh.hpp:457