flyweight.hh

Go to the documentation of this file.
1 
5 #ifndef CONCEPTS_FLYWEIGHT
6 #define CONCEPTS_FLYWEIGHT
7 
8 #include <unordered_map>
11 
12 namespace concepts {
13 
15  public:
16  class Hash {
17  public:
18  std::size_t operator()(const ShapeFunction1DOrder& order) const {
19  return ( (order.polynomialDegree_ << 10 | order.nQuadraturePoints_) << 10) | order.quadratureType_;
20  }
21  };
22 
23  ShapeFunction1DOrder(const uint polynomialDegree,
24  const uint nQuadraturePoints,
25  const concepts::intRule quadratureType) :
26  polynomialDegree_(polynomialDegree), nQuadraturePoints_(nQuadraturePoints),
27  quadratureType_(quadratureType)
28  {}
29 
30  // bool operator<(const Order& other) const {
31  // if (static_cast<const Orders<1>&>(*this) == static_cast<const Orders<1>&>(other))
32  // return quadratureType_ < other.quadratureType_;
33  // else
34  // return static_cast<const Orders<1>&>(*this) < static_cast<const Orders<1>&>(other);
35  // }
36 
37  bool operator==(const ShapeFunction1DOrder& other) const {
38  return
42  }
43 
44  private:
48  };
49 
51  public:
52  class Hash {
53  public:
54  std::size_t operator()(const QuadratureOrder& order) const {
55  return (order.nQuadraturePoints_ << 10) | order.quadratureType_;
56  }
57  };
58 
59  QuadratureOrder(const uint nQuadraturePoints,
60  const concepts::intRule quadratureType) :
61  nQuadraturePoints_(nQuadraturePoints),
62  quadratureType_(quadratureType)
63  {}
64 
65 
66  bool operator==(const QuadratureOrder& other) const {
67  return
70  }
71 
72  private:
75  };
76 
77  template<class KeyT>
78  class Hash;
79 
80  template<>
82  {
83  typedef typename ShapeFunction1DOrder::Hash type;
84  };
85 
86  template<>
88  {
89  typedef typename QuadratureOrder::Hash type;
90  };
91 
93  template<class KeyT, class ValueT>
94  class Flyweight {
95  public:
96  Flyweight() {}
97 
98  std::shared_ptr<const ValueT> get(const KeyT& key) {
99  auto it = container_.find(key);
100  if (it != container_.end() )
101  return it->lock();
102  else
103  return std::make_shared<const ValueT>();
104  }
105 
106  void insert(const KeyT& key, std::shared_ptr<const ValueT>& value) {
107  auto it = container_.find(key);
108  if (it != container_.end() )
109  conceptsAssert(!container_[key].lock(),Assertion());
110  container_[key] = value;
111  }
112 
115 
116  protected:
117  std::unordered_map<KeyT,std::weak_ptr<const ValueT>, Hash<KeyT> > container_;
118  };
119 
121  template <class FunctionT>
122  std::shared_ptr<const FunctionT> makeShapeFunction(const concepts::QuadratureRule1d& quadratureRule,
123  const uint polynomialDegree)
124  {
125 
126  using Order = ShapeFunction1DOrder;
127  static Flyweight<Order,FunctionT> flyweight_;
128 
129  intRule ruleType;
130  if (dynamic_cast<const QuadratureRule1dGaussJacobi*>(&quadratureRule) != nullptr)
131  ruleType = intRule::GAUSS_JACOBI;
132  else if (dynamic_cast<const QuadratureRule1dGaussLobatto*>(&quadratureRule) != nullptr)
133  ruleType = intRule::GAUSS_LOBATTO;
134  else if (dynamic_cast<const QuadratureRule1dTrapeze*>(&quadratureRule) != nullptr)
135  ruleType = intRule::TRAPEZE;
136 
137  Order order(polynomialDegree,
138  quadratureRule.n(),
139  ruleType);
140 
141  if(auto function = flyweight_.lock(order))
142  return function;
143  else {
144  function = std::make_shared<FunctionT>(polynomialDegree,
145  quadratureRule);
146 
147  flyweight_.insert(order,function);
148  return function;
149  }
150  };
151 
153  template <class FunctionT>
154  std::shared_ptr<const FunctionT> makeQuadrature(const uint nQuadraturePoints,
155  const intRule quadratureType) {
156 
157  using Order = QuadratureOrder;
158  static Flyweight<Order,FunctionT> flyweight_quadrature_;
159 
160  Order order(nQuadraturePoints, quadratureType);
161 
162  if(auto quadrature = flyweight_quadrature_.lock(order))
163  return quadrature;
164  else {
165 
167  if (quadratureType == intRule::GAUSS_JACOBI)
168  {
169  quadrature = std::make_shared<QuadratureRule1dGaussJacobi>(nQuadraturePoints);
170  }
171  else if (quadratureType == intRule::GAUSS_LOBATTO)
172  {
173  quadrature = std::make_shared<QuadratureRule1dGaussLobatto>(nQuadraturePoints);
174  }
175  else if (quadratureType == intRule::TRAPEZE)
176  {
177  quadrature = std::make_shared<QuadratureRule1dTrapeze>(nQuadraturePoints);
178  }
179  else
180  {
181  throw conceptsException(concepts::MissingFeature("quadrature rule not defined"));
182  }
183 
184  flyweight_quadrature_.insert(order,quadrature);
185  return quadrature;
186  }
187  }
188 
190  template <class FunctionT>
191  std::shared_ptr<const FunctionT> makeAdaptiveQuadrature(const uint nQuadraturePoints,
192  const intRule quadratureType) {
193 
194  using Order = QuadratureOrder;
195 
196  static Flyweight<Order,FunctionT> flyweight_quadrature_;
197 
198  Order order(nQuadraturePoints, quadratureType);
199 
200  if(auto quadrature = flyweight_quadrature_.lock(order))
201  return quadrature;
202  else {
204  if (quadratureType == intRule::GAUSS_JACOBI)
205  {
206  quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::GAUSS_JACOBI> >(nQuadraturePoints);
207  }
208  else if (quadratureType == intRule::GAUSS_LOBATTO)
209  {
210  quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::GAUSS_LOBATTO> >(nQuadraturePoints);
211  }
212  else if (quadratureType == intRule::TRAPEZE)
213  {
214  quadrature = std::make_shared<AdaptiveQuadratureRule1d<intRule::TRAPEZE> >(nQuadraturePoints);
215  }
216  else
217  {
218  throw conceptsException(concepts::MissingFeature("quadrature rule not defined"));
219  }
220 
221  flyweight_quadrature_.insert(order,quadrature);
222  return quadrature;
223  }
224  }
225 
226 }
227 
228 #endif
229 
std::shared_ptr< const FunctionT > makeShapeFunction(const concepts::QuadratureRule1d &quadratureRule, const uint polynomialDegree)
factory function encapsulating the memory manager
Definition: flyweight.hh:122
std::unordered_map< KeyT, std::weak_ptr< const ValueT >, Hash< KeyT > > container_
TODO: Write extra method for the removable of entries with non-lockable weak_ptrs.
Definition: flyweight.hh:117
@ GAUSS_JACOBI
Definition: defines.hh:13
concepts::intRule quadratureType_
Definition: flyweight.hh:47
@ TRAPEZE
Definition: defines.hh:13
#define conceptsException(exc)
Prepares an exception for throwing.
Definition: exceptions.hh:344
ShapeFunction1DOrder(const uint polynomialDegree, const uint nQuadraturePoints, const concepts::intRule quadratureType)
Definition: flyweight.hh:23
void insert(const KeyT &key, std::shared_ptr< const ValueT > &value)
Definition: flyweight.hh:106
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Gauss Jacobi quadrature rule not including both endpoints.
Definition: quadRule.hh:135
Exception class for assertions.
Definition: exceptions.hh:258
std::shared_ptr< const FunctionT > makeQuadrature(const uint nQuadraturePoints, const intRule quadratureType)
factory function encapsulating the memory manager
Definition: flyweight.hh:154
std::size_t operator()(const ShapeFunction1DOrder &order) const
Definition: flyweight.hh:18
std::shared_ptr< const ValueT > get(const KeyT &key)
Definition: flyweight.hh:98
QuadratureOrder::Hash type
Definition: flyweight.hh:89
std::size_t operator()(const QuadratureOrder &order) const
Definition: flyweight.hh:54
QuadratureOrder(const uint nQuadraturePoints, const concepts::intRule quadratureType)
Definition: flyweight.hh:59
Gauss Lobatto quadrature rule including both endpoints.
Definition: quadRule.hh:94
bool operator==(const ShapeFunction1DOrder &other) const
Definition: flyweight.hh:37
intRule
Types of integration rules to choose from.
Definition: defines.hh:13
std::shared_ptr< const FunctionT > makeAdaptiveQuadrature(const uint nQuadraturePoints, const intRule quadratureType)
factory function encapsulating the memory manager
Definition: flyweight.hh:191
Exception class to express a missing feature.
Definition: exceptions.hh:206
bool operator==(const QuadratureOrder &other) const
Definition: flyweight.hh:66
flyweight memory manager
Definition: flyweight.hh:94
virtual uint n() const =0
Returns the number of points.
@ GAUSS_LOBATTO
Definition: defines.hh:13
ShapeFunction1DOrder::Hash type
Definition: flyweight.hh:83
Quadrature rule for numerical integration.
Definition: quadRule.hh:30
concepts::intRule quadratureType_
Definition: flyweight.hh:74
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich