Chaste  Release::2018.1
PottsMesh.hpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 
339  // Nested classes //
341 
347  class PottsElementIterator
348  {
349  public:
356  inline PottsElement<DIM>& operator*();
357 
362  inline PottsElement<DIM>* operator->();
363 
370  inline bool operator!=(const typename PottsMesh<DIM>::PottsElementIterator& rOther);
371 
376  inline PottsElementIterator& operator++();
377 
388  PottsElementIterator(PottsMesh<DIM>& rMesh,
389  typename std::vector<PottsElement<DIM> *>::iterator elementIter,
390  bool skipDeletedElements=true);
391 
392  private:
395 
397  typename std::vector<PottsElement<DIM> *>::iterator mElementIter;
398 
401 
405  inline bool IsAtEnd();
406 
410  inline bool IsAllowedElement();
411  };
412 };
413 
416 
417 // PottsElementIterator class implementation - most methods are inlined //
420 
421 template<unsigned DIM>
422 typename PottsMesh<DIM>::PottsElementIterator PottsMesh<DIM>::GetElementIteratorBegin(
423  bool skipDeletedElements)
424 {
425  return PottsElementIterator(*this, mElements.begin(), skipDeletedElements);
426 }
427 
428 template<unsigned DIM>
430 {
431  return PottsElementIterator(*this, mElements.end());
432 }
433 
434 template<unsigned DIM>
436 {
437  assert(!IsAtEnd());
438  return **mElementIter;
439 }
440 
441 template<unsigned DIM>
443 {
444  assert(!IsAtEnd());
445  return *mElementIter;
446 }
447 
448 template<unsigned DIM>
450 {
451  return mElementIter != rOther.mElementIter;
452 }
453 
454 template<unsigned DIM>
456 {
457  do
458  {
459  ++mElementIter;
460  }
461  while (!IsAtEnd() && !IsAllowedElement());
462 
463  return (*this);
464 }
465 
466 template<unsigned DIM>
468  PottsMesh<DIM>& rMesh,
469  typename std::vector<PottsElement<DIM>*>::iterator elementIter,
470  bool skipDeletedElements)
471  : mrMesh(rMesh),
472  mElementIter(elementIter),
473  mSkipDeletedElements(skipDeletedElements)
474 {
475  if (mrMesh.mElements.empty())
476  {
477  // Cope with empty meshes
478  mElementIter = mrMesh.mElements.end();
479  }
480  else
481  {
482  // Make sure we start at an allowed element
483  if (mElementIter == mrMesh.mElements.begin() && !IsAllowedElement())
484  {
485  ++(*this);
486  }
487  }
488 }
489 
490 template<unsigned DIM>
492 {
493  return mElementIter == mrMesh.mElements.end();
494 }
495 
496 template<unsigned DIM>
498 {
499  return !(mSkipDeletedElements && (*this)->IsDeleted());
500 }
501 
502 #endif /*POTTSMESH_HPP_*/
unsigned AddElement(PottsElement< DIM > *pNewElement)
Definition: PottsMesh.cpp:513
unsigned SolveBoundaryElementMapping(unsigned index) const
Definition: PottsMesh.cpp:114
Definition: Node.hpp:58
void DeleteNode(unsigned index)
Definition: PottsMesh.cpp:272
void DeleteElement(unsigned index)
Definition: PottsMesh.cpp:245
virtual double GetVolumeOfElement(unsigned index)
Definition: PottsMesh.cpp:189
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:574
std::vector< std::set< unsigned > > mMooreNeighbouringNodeIndices
Definition: PottsMesh.hpp:81
void RemoveDeletedElements()
Definition: PottsMesh.cpp:253
std::set< unsigned > GetVonNeumannNeighbouringNodeIndices(unsigned nodeIndex)
Definition: PottsMesh.cpp:239
unsigned GetNumAllElements() const
Definition: PottsMesh.cpp:156
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:233
unsigned SolveElementMapping(unsigned index) const
Definition: PottsMesh.cpp:107
unsigned DivideElement(PottsElement< DIM > *pElement, bool placeOriginalElementBelow=false)
Definition: PottsMesh.cpp:386
PottsElement< DIM > * operator->()
Definition: PottsMesh.hpp:442
virtual c_vector< double, DIM > GetCentroidOfElement(unsigned index)
Definition: PottsMesh.cpp:169
virtual void Clear()
Definition: PottsMesh.cpp:120
PottsElementIterator(PottsMesh< DIM > &rMesh, typename std::vector< PottsElement< DIM > * >::iterator elementIter, bool skipDeletedElements=true)
Definition: PottsMesh.hpp:467
PottsElementIterator GetElementIteratorBegin(bool skipDeletedElements=true)
Definition: PottsMesh.hpp:422
virtual unsigned GetNumNodes() const
Definition: PottsMesh.cpp:144
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
bool operator!=(const typename PottsMesh< DIM >::PottsElementIterator &rOther)
Definition: PottsMesh.hpp:449
virtual double GetSurfaceAreaOfElement(unsigned index)
Definition: PottsMesh.cpp:198
PottsElementIterator GetElementIteratorEnd()
Definition: PottsMesh.hpp:429
std::set< unsigned > GetNeighbouringElementIndices(unsigned elementIndex)
Definition: PottsMesh.cpp:530
std::vector< unsigned > mDeletedElementIndices
Definition: PottsMesh.hpp:75
PottsElement< DIM > * GetElement(unsigned index) const
Definition: PottsMesh.cpp:162
PottsElement< DIM > & operator*()
Definition: PottsMesh.hpp:435
std::vector< PottsElement< DIM > * >::iterator mElementIter
Definition: PottsMesh.hpp:397
virtual unsigned GetNumElements() const
Definition: PottsMesh.cpp:150
unsigned SolveNodeMapping(unsigned index) const
Definition: PottsMesh.cpp:100
static std::string GetArchiveDirectory()
static std::string GetArchiveRelativePath()
gcov doesn't like this file...
std::vector< std::set< unsigned > > mVonNeumannNeighbouringNodeIndices
Definition: PottsMesh.hpp:78
PottsElementIterator & operator++()
Definition: PottsMesh.hpp:455