BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
concrete_id_set.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef bempp_concrete_id_set_hpp
22 #define bempp_concrete_id_set_hpp
23 
24 #include "../common/common.hpp"
25 
26 #include "id_set.hpp"
27 #include "concrete_entity.hpp"
28 
29 #include <stdexcept>
30 
31 namespace Bempp
32 {
33 
41 template<typename DuneGrid, typename DuneIdSet>
42 class ConcreteIdSet: public IdSet
43 {
44 private:
45  const DuneIdSet* m_dune_id_set;
46 
47 public:
52  explicit ConcreteIdSet(const DuneIdSet* dune_id_set) :
53  m_dune_id_set(dune_id_set) {
54  }
55 
57  const DuneIdSet& duneIdSet() const {
58  return *m_dune_id_set;
59  }
60 
61  virtual IdType entityId(const Entity<0>& e) const {
62  return entityCodimNId(e);
63  }
64  virtual IdType entityId(const Entity<1>& e) const {
65  return entityCodimNId(e);
66  }
67  virtual IdType entityId(const Entity<2>& e) const {
68  return entityCodimNId(e);
69  }
70  virtual IdType entityId(const Entity<3>& e) const {
71  return entityCodimNId(e);
72  }
73 
74  virtual IdType subEntityId(const Entity<0>& e, size_t i, int codimSub) const {
75 #ifndef NDEBUG
76  // Prevent an assert in FoamGrid from crashing the Python interpreter
77  if (codimSub > DuneGrid::dimension)
78  throw std::invalid_argument("IndexSet::subEntityIndex(): codimSub exceeds grid dimension");
79 #endif
80  typedef typename DuneGrid::template Codim<0>::Entity DuneEntity;
81  typedef ConcreteEntity<0, DuneEntity> ConcEntity;
82  const ConcEntity& ce = dynamic_cast<const ConcEntity&>(e);
83  return m_dune_id_set->subId(ce.duneEntity(), i, codimSub);
84  }
85 
86 private:
87  // Below there are essentially two overloads of
88  // template <int codim>
89  // IdType entityCodimNId(const Entity<codim>& e) const;
90  // valid for codim > DuneGrid::dimension (throws exception)
91  // and for the opposite case (does real work).
92  //
93  // This is not a very pretty code, ideas how to improve it are welcome!
94  // The problem is that we cannot allow the compiler to instantiate
95  // Dune::Entity objects with codim > DuneGrid::dimension
96  // because that throws a static assert in Dune code.
97  template <int codim>
98  typename boost::disable_if_c<codim <= DuneGrid::dimension, IdType>::type
99  entityCodimNId(const Entity<codim>& e) const {
100  throw std::logic_error("IdSet::entityId(): invalid entity codimension");
101  }
102 
103  template <int codim>
104  typename boost::enable_if_c<codim <= DuneGrid::dimension, IdType>::type
105  entityCodimNId(const Entity<codim>& e) const {
106  typedef typename DuneGrid::template Codim<codim>::Entity DuneEntity;
107  typedef ConcreteEntity<codim, DuneEntity> ConcEntity;
108  const ConcEntity& ce = dynamic_cast<const ConcEntity&>(e);
109  return m_dune_id_set->id(ce.duneEntity());
110  }
111 };
112 
113 } // namespace Bempp
114 
115 #endif // ID_SET_HPP
ConcreteIdSet(const DuneIdSet *dune_id_set)
Constructor.
Definition: concrete_id_set.hpp:52
Abstract wrapper of an entity of codimension codim.
Definition: entity.hpp:46
virtual IdType subEntityId(const Entity< 0 > &e, size_t i, int codimSub) const
Id of i&#39;th subentity of codimension codimSub of entity e of codimension 0.
Definition: concrete_id_set.hpp:74
Wrapper of a Dune entity of type DuneEntity and codimension 0.
Definition: concrete_entity_decl.hpp:117
Abstract wrapper of an id set.
Definition: id_set.hpp:37
virtual IdType entityId(const Entity< 2 > &e) const
Id of the entity e of codimension 2.
Definition: concrete_id_set.hpp:67
const DuneIdSet & duneIdSet() const
Read-only access to the underlying Dune id set.
Definition: concrete_id_set.hpp:57
size_t IdType
Id type.
Definition: id_set.hpp:48
virtual IdType entityId(const Entity< 1 > &e) const
Id of the entity e of codimension 1.
Definition: concrete_id_set.hpp:64
virtual IdType entityId(const Entity< 3 > &e) const
Id of the entity e of codimension 3.
Definition: concrete_id_set.hpp:70
Wrapper of a Dune id set of type DuneIdSet providing access to the entities of a Dune grid of type Du...
Definition: concrete_id_set.hpp:42
Abstract wrapper of an entity of codimension 0.
Definition: entity.hpp:81
virtual IdType entityId(const Entity< 0 > &e) const
Id of the entity e of codimension 0.
Definition: concrete_id_set.hpp:61