dune-geometry 2.8.0
Loading...
Searching...
No Matches
hcube.cc
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_GEOMETRY_REFINEMENT_HCUBE_CC
4#define DUNE_GEOMETRY_REFINEMENT_HCUBE_CC
5
40#include <cassert>
41
44#include <dune/common/power.hh>
45
48
49#include "base.cc" // for RefinementTraits
50
51namespace Dune
52{
53 namespace RefinementImp
54 {
61 namespace HCube
62 {
72 template<int dimension_, class CoordType>
74 {
75 public:
77 //- Know yourself
79
80 template<int codimension>
81 struct Codim;
86
87 static unsigned nVertices(unsigned nIntervals);
88 static VertexIterator vBegin(unsigned nIntervals);
89 static VertexIterator vEnd(unsigned nIntervals);
90
91 static unsigned nElements(unsigned nIntervals);
92 static ElementIterator eBegin(unsigned nIntervals);
93 static ElementIterator eEnd(unsigned nIntervals);
94 };
95
96 template<int dimension, class CoordType>
97 template<int codimension>
98 struct RefinementImp<dimension, CoordType>::Codim
99 {
100 class SubEntityIterator;
102 };
103
104 template<int dimension, class CoordType>
105 unsigned
107 nVertices(unsigned nIntervals)
108 {
109 // return (nIntervals + 1)^dim
111 }
112
113 template<int dimension, class CoordType>
120
121 template<int dimension, class CoordType>
128
129 template<int dimension, class CoordType>
130 unsigned
132 nElements(unsigned nIntervals)
133 {
134 static_assert(dimension >= 0,
135 "Negative dimension given, what the heck is that supposed to mean?");
136 // return nIntervals^dim
138 }
139
140 template<int dimension, class CoordType>
147
148 template<int dimension, class CoordType>
155
156 //
157 // The iterators
158 //
159
160#ifdef DOXYGEN
172 template<int dimension, class CoordType, int codimension>
174#else
175 template<int dimension, class CoordType, int codimension>
177#endif //DOXYGEN
178
179 // for vertices
180
181 template<int dimension, class CoordType>
182 class RefinementSubEntityIteratorSpecial<dimension, CoordType, dimension>
183 {
184 public:
186 typedef typename Refinement::template Codim<dimension>::SubEntityIterator Common;
187 typedef typename Refinement::CoordVector CoordVector;
188
189 CoordVector coords() const;
190
191 private:
192 const Common & asCommon() const
193 {
194 return *static_cast<const Common*>(this);
195 }
196 };
197
198 template<int dimension, class CoordType>
199 typename RefinementSubEntityIteratorSpecial<dimension, CoordType, dimension>::CoordVector
201 coords() const
202 {
203 std::array<unsigned int, dimension> v(asCommon().vertexCoord());
204 CoordVector c;
205 for (int d = 0; d < dimension; d++)
206 {
207 c[d] = v[d]*1.0 / asCommon()._nIntervals;
208 }
209 return c;
210 }
211
212 // for elements
213
214 template<int dimension, class CoordType>
215 class RefinementSubEntityIteratorSpecial<dimension, CoordType, 0>
216 {
217 public:
219 typedef typename Refinement::template Codim<0>::SubEntityIterator Common;
220 typedef typename Refinement::IndexVector IndexVector;
221 typedef typename Refinement::CoordVector CoordVector;
222
223 IndexVector vertexIndices() const;
224 CoordVector coords() const;
225
226 private:
227 const Common & asCommon() const
228 {
229 return *static_cast<const Common*>(this);
230 }
231 };
232
233 template<int dimension, class CoordType>
234 typename RefinementSubEntityIteratorSpecial<dimension, CoordType, 0>::IndexVector
236 vertexIndices() const
237 {
238 enum { nIndices = (1 << dimension) };
239
240 // cell index tuple
241 std::array<unsigned int, dimension> e(asCommon().cellCoord());
242
243 // vertices
245 for(int i = 0; i < nIndices; ++i)
246 {
247 int base = 1;
248 std::array<unsigned int, dimension> alpha(asCommon().idx2multiidx(i));
249 for (int d = 0; d < dimension; d++) {
250 vec[i] += (alpha[d] + e[d]) * base;
251 base *= asCommon()._nIntervals+1;
252 }
253 }
254 return vec;
255 }
256
257 template<int dimension, class CoordType>
260 coords() const
261 {
262 std::array<unsigned int, dimension> v(asCommon().cellCoord());
263 CoordVector c;
264 for (int d=0; d<dimension; d++)
265 {
266 c[d] = (v[d]*1.0 + 0.5) / asCommon()._nIntervals;
267 }
268 return c;
269 }
270
271 // common
272 template<int dimension, class CoordType>
273 template<int codimension>
274 class RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator
275 : public ForwardIteratorFacade<typename RefinementImp<dimension,
276 CoordType>::template Codim<codimension>::SubEntityIterator, int>,
277 public RefinementSubEntityIteratorSpecial<dimension, CoordType, codimension>
278 {
279 public:
282
283 SubEntityIterator(unsigned int index, unsigned int nIntervals);
284
285 bool equals(const This &other) const;
286 void increment();
287
288 int index() const;
289 Geometry geometry () const;
290 private:
292 unsigned int _index;
293 unsigned int _nIntervals;
294
296 cellCoord(unsigned int idx) const
297 {
298 return idx2coord(idx, _nIntervals);
299 }
300
302 vertexCoord(unsigned int idx) const
303 {
304 return idx2coord(idx, _nIntervals+1u);
305 }
306
308 cellCoord() const
309 {
310 return cellCoord(_index);
311 }
312
314 vertexCoord() const
315 {
316 return vertexCoord(_index);
317 }
318
320 idx2coord(unsigned int idx, unsigned int w) const
321 {
323 for (unsigned int d = 0; d < dimension; d++)
324 {
325 c[d] = idx%w;
326 idx = idx/w;
327 }
328 return c;
329 }
330
331 unsigned int
332 coord2idx(std::array<unsigned int, dimension> c, unsigned int w) const
333 {
334 unsigned int i = 0;
335 for (unsigned int d = dimension; d > 0; d--)
336 {
337 i *= w;
338 i += c[d-1];
339 }
340 return i;
341 }
342
344 idx2multiidx(unsigned int idx) const
345 {
347 for (unsigned int i = 0; i < dimension; ++i)
348 alpha[i] = (idx >> i) & 1u;
349 return alpha;
350 }
351 };
352
353#ifndef DOXYGEN
354 template<int dimension, class CoordType>
355 template<int codimension>
356 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
357 SubEntityIterator(unsigned int index, unsigned int nIntervals)
358 : _index(index), _nIntervals(nIntervals)
359 {}
360
361 template<int dimension, class CoordType>
362 template<int codimension>
363 bool
364 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
365 equals(const This &other) const
366 {
367 return ((_index == other._index) && (_nIntervals == other._nIntervals));
368 }
369
370 template<int dimension, class CoordType>
371 template<int codimension>
372 void
373 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
374 increment()
375 {
376 ++_index;
377 }
378
379 template<int dimension, class CoordType>
380 template<int codimension>
381 int
382 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
383 index() const
384 {
385 return _index;
386 }
387
388 template<int dimension, class CoordType>
389 template<int codimension>
390 typename RefinementImp<dimension, CoordType>::template Codim<codimension>::Geometry
391 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::geometry () const
392 {
393 std::array<unsigned int,dimension> intCoords = idx2coord(_index,_nIntervals);
394
397
398 assert(codimension == 0 or codimension == dimension);
399
400 if constexpr (codimension == 0) {
401 for (size_t j = 0; j < dimension; j++)
402 {
403 lower[j] = double(intCoords[j]) / double(_nIntervals);
404 upper[j] = double(intCoords[j] + 1) / double(_nIntervals);
405 }
406
407 return typename RefinementImp<dimension,
408 CoordType>::template Codim<codimension>::Geometry(lower,upper);
409 } else {
410 for (size_t j = 0; j < dimension; j++)
411 lower[j] = upper[j] = double(intCoords[j]) / double(_nIntervals);
412
413 return typename RefinementImp<dimension,
414 CoordType>::template Codim<codimension>::Geometry(lower,upper,std::bitset<dimension>(0));
415 }
416 }
417
418#endif // DOXYGEN
419
420 } // namespace HCube
421
422 // ///////////////////////
423 //
424 // The refinement traits
425 //
426
427#ifndef DOXYGEN
428 template<unsigned topologyId, class CoordType, unsigned coerceToId,
429 int dim>
430 struct Traits<
431 topologyId, CoordType, coerceToId, dim,
432 typename std::enable_if<
433 (dim >= 2 &&
434 (GeometryTypes::cube(dim).id() >> 1) ==
435 (topologyId >> 1) &&
436 (GeometryTypes::cube(dim).id() >> 1) ==
437 (coerceToId >> 1)
438 )>::type
439 >
440 {
441 typedef HCube::RefinementImp<dim, CoordType> Imp;
442 };
443#endif
444
445 } // namespace RefinementImp
446
447} // namespace Dune
448
449#endif // DUNE_GEOMETRY_REFINEMENT_HCUBE_CC
A geometry implementation for axis-aligned hypercubes.
This file contains the parts independent of a particular Refinement implementation.
STL namespace.
static constexpr auto eval(const T &a)
size_type dim() const
std::ptrdiff_t index() const
constexpr FieldMatrix()=default
A geometry implementation for axis-aligned hypercubes.
Definition axisalignedcubegeometry.hh:48
Static tag representing a codimension.
Definition dimension.hh:22
Refinement implementation for hypercubes
Definition hcube.cc:74
Codim< 0 >::SubEntityIterator ElementIterator
Definition hcube.cc:84
static ElementIterator eEnd(unsigned nIntervals)
Definition hcube.cc:151
static unsigned nVertices(unsigned nIntervals)
Definition hcube.cc:107
static VertexIterator vEnd(unsigned nIntervals)
Definition hcube.cc:124
Codim< dimension >::SubEntityIterator VertexIterator
Definition hcube.cc:82
FieldVector< CoordType, dimension > CoordVector
Definition hcube.cc:83
static unsigned nElements(unsigned nIntervals)
Definition hcube.cc:132
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:78
static ElementIterator eBegin(unsigned nIntervals)
Definition hcube.cc:143
static VertexIterator vBegin(unsigned nIntervals)
Definition hcube.cc:116
Dune::AxisAlignedCubeGeometry< CoordType, dimension-codimension, dimension > Geometry
Definition hcube.cc:101
SubEntityIterator base class for hypercube refinement.
Definition hcube.cc:173
Refinement::template Codim< dimension >::SubEntityIterator Common
Definition hcube.cc:186
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:218
Refinement::template Codim< 0 >::SubEntityIterator Common
Definition hcube.cc:219
SubEntityIterator(unsigned int index, unsigned int nIntervals)
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:280
Refinement::template Codim< codimension >::SubEntityIterator This
Definition hcube.cc:281