dune-geometry 2.10
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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_GEOMETRY_REFINEMENT_HCUBE_CC
6#define DUNE_GEOMETRY_REFINEMENT_HCUBE_CC
7
41#include <cassert>
42
43#include <dune/common/fvector.hh>
44#include <dune/common/iteratorfacades.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 constexpr static int dimension = dimension_;
78 //- Know yourself
80
81 template<int codimension>
82 struct Codim;
84 typedef FieldVector<CoordType, dimension> CoordVector;
86 typedef FieldVector<int, (1<<dimension)> IndexVector;
87
88 static unsigned nVertices(unsigned nIntervals);
89 static VertexIterator vBegin(unsigned nIntervals);
90 static VertexIterator vEnd(unsigned nIntervals);
91
92 static unsigned nElements(unsigned nIntervals);
93 static ElementIterator eBegin(unsigned nIntervals);
94 static ElementIterator eEnd(unsigned nIntervals);
95 };
96
97 template<int dimension, class CoordType>
98 template<int codimension>
99 struct RefinementImp<dimension, CoordType>::Codim
100 {
101 class SubEntityIterator;
103 };
104
105 template<int dimension, class CoordType>
106 unsigned
108 nVertices(unsigned nIntervals)
109 {
110 // return (nIntervals + 1)^dim
111 return Dune::power(nIntervals+1u, static_cast<unsigned>(dimension));
112 }
113
114 template<int dimension, class CoordType>
117 vBegin(unsigned nIntervals)
118 {
119 return VertexIterator(0,nIntervals);
120 }
121
122 template<int dimension, class CoordType>
125 vEnd(unsigned nIntervals)
126 {
127 return VertexIterator(nVertices(nIntervals),nIntervals);
128 }
129
130 template<int dimension, class CoordType>
131 unsigned
133 nElements(unsigned nIntervals)
134 {
135 static_assert(dimension >= 0,
136 "Negative dimension given, what the heck is that supposed to mean?");
137 // return nIntervals^dim
138 return Dune::power(nIntervals, static_cast<unsigned>(dimension));
139 }
140
141 template<int dimension, class CoordType>
144 eBegin(unsigned nIntervals)
145 {
146 return ElementIterator(0,nIntervals);
147 }
148
149 template<int dimension, class CoordType>
152 eEnd(unsigned nIntervals)
153 {
154 return ElementIterator(nElements(nIntervals),nIntervals);
155 }
156
157 //
158 // The iterators
159 //
160
161#ifdef DOXYGEN
173 template<int dimension, class CoordType, int codimension>
175#else
176 template<int dimension, class CoordType, int codimension>
178#endif //DOXYGEN
179
180 // for vertices
181
182 template<int dimension, class CoordType>
183 class RefinementSubEntityIteratorSpecial<dimension, CoordType, dimension>
184 {
185 public:
187 typedef typename Refinement::template Codim<dimension>::SubEntityIterator Common;
188 typedef typename Refinement::CoordVector CoordVector;
189
190 CoordVector coords() const;
191
192 private:
193 const Common & asCommon() const
194 {
195 return *static_cast<const Common*>(this);
196 }
197 };
198
199 template<int dimension, class CoordType>
200 typename RefinementSubEntityIteratorSpecial<dimension, CoordType, dimension>::CoordVector
202 coords() const
203 {
204 std::array<unsigned int, dimension> v(asCommon().vertexCoord());
205 CoordVector c;
206 for (int d = 0; d < dimension; d++)
207 {
208 c[d] = v[d]*1.0 / asCommon()._nIntervals;
209 }
210 return c;
211 }
212
213 // for elements
214
215 template<int dimension, class CoordType>
216 class RefinementSubEntityIteratorSpecial<dimension, CoordType, 0>
217 {
218 public:
220 typedef typename Refinement::template Codim<0>::SubEntityIterator Common;
221 typedef typename Refinement::IndexVector IndexVector;
222 typedef typename Refinement::CoordVector CoordVector;
223
224 IndexVector vertexIndices() const;
225 CoordVector coords() const;
226
227 private:
228 const Common & asCommon() const
229 {
230 return *static_cast<const Common*>(this);
231 }
232 };
233
234 template<int dimension, class CoordType>
235 typename RefinementSubEntityIteratorSpecial<dimension, CoordType, 0>::IndexVector
237 vertexIndices() const
238 {
239 constexpr static int nIndices = 1 << dimension;
240
241 // cell index tuple
242 std::array<unsigned int, dimension> e(asCommon().cellCoord());
243
244 // vertices
245 IndexVector vec;
246 for(int i = 0; i < nIndices; ++i)
247 {
248 int base = 1;
249 std::array<unsigned int, dimension> alpha(asCommon().idx2multiidx(i));
250 for (int d = 0; d < dimension; d++) {
251 vec[i] += (alpha[d] + e[d]) * base;
252 base *= asCommon()._nIntervals+1;
253 }
254 }
255 return vec;
256 }
257
258 template<int dimension, class CoordType>
261 coords() const
262 {
263 std::array<unsigned int, dimension> v(asCommon().cellCoord());
264 CoordVector c;
265 for (int d=0; d<dimension; d++)
266 {
267 c[d] = (v[d]*1.0 + 0.5) / asCommon()._nIntervals;
268 }
269 return c;
270 }
271
272 // common
273 template<int dimension, class CoordType>
274 template<int codimension>
275 class RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator
276 : public ForwardIteratorFacade<typename RefinementImp<dimension,
277 CoordType>::template Codim<codimension>::SubEntityIterator, int>,
278 public RefinementSubEntityIteratorSpecial<dimension, CoordType, codimension>
279 {
280 public:
282 typedef typename Refinement::template Codim<codimension>::SubEntityIterator This;
283
284 SubEntityIterator(unsigned int index, unsigned int nIntervals);
285
286 bool equals(const This &other) const;
287 void increment();
288
289 int index() const;
290 Geometry geometry () const;
291 private:
292 friend class RefinementSubEntityIteratorSpecial<dimension, CoordType, codimension>;
293 unsigned int _index;
294 unsigned int _nIntervals;
295
296 std::array<unsigned int, dimension>
297 cellCoord(unsigned int idx) const
298 {
299 return idx2coord(idx, _nIntervals);
300 }
301
302 std::array<unsigned int, dimension>
303 vertexCoord(unsigned int idx) const
304 {
305 return idx2coord(idx, _nIntervals+1u);
306 }
307
308 std::array<unsigned int, dimension>
309 cellCoord() const
310 {
311 return cellCoord(_index);
312 }
313
314 std::array<unsigned int, dimension>
315 vertexCoord() const
316 {
317 return vertexCoord(_index);
318 }
319
320 std::array<unsigned int, dimension>
321 idx2coord(unsigned int idx, unsigned int w) const
322 {
323 std::array<unsigned int, dimension> c;
324 for (unsigned int d = 0; d < dimension; d++)
325 {
326 c[d] = idx%w;
327 idx = idx/w;
328 }
329 return c;
330 }
331
332 unsigned int
333 coord2idx(std::array<unsigned int, dimension> c, unsigned int w) const
334 {
335 unsigned int i = 0;
336 for (unsigned int d = dimension; d > 0; d--)
337 {
338 i *= w;
339 i += c[d-1];
340 }
341 return i;
342 }
343
344 std::array<unsigned int, dimension>
345 idx2multiidx(unsigned int idx) const
346 {
347 std::array<unsigned int, dimension> alpha;
348 for (unsigned int i = 0; i < dimension; ++i)
349 alpha[i] = (idx >> i) & 1u;
350 return alpha;
351 }
352 };
353
354#ifndef DOXYGEN
355 template<int dimension, class CoordType>
356 template<int codimension>
357 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
358 SubEntityIterator(unsigned int index, unsigned int nIntervals)
359 : _index(index), _nIntervals(nIntervals)
360 {}
361
362 template<int dimension, class CoordType>
363 template<int codimension>
364 bool
365 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
366 equals(const This &other) const
367 {
368 return ((_index == other._index) && (_nIntervals == other._nIntervals));
369 }
370
371 template<int dimension, class CoordType>
372 template<int codimension>
373 void
374 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
375 increment()
376 {
377 ++_index;
378 }
379
380 template<int dimension, class CoordType>
381 template<int codimension>
382 int
383 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
384 index() const
385 {
386 return _index;
387 }
388
389 template<int dimension, class CoordType>
390 template<int codimension>
391 typename RefinementImp<dimension, CoordType>::template Codim<codimension>::Geometry
392 RefinementImp<dimension, CoordType>::Codim<codimension>::SubEntityIterator::geometry () const
393 {
394 std::array<unsigned int,dimension> intCoords = idx2coord(_index,_nIntervals);
395
396 Dune::FieldVector<CoordType,dimension> lower;
397 Dune::FieldVector<CoordType,dimension> upper;
398
399 assert(codimension == 0 or codimension == dimension);
400
401 if constexpr (codimension == 0) {
402 for (size_t j = 0; j < dimension; j++)
403 {
404 lower[j] = double(intCoords[j]) / double(_nIntervals);
405 upper[j] = double(intCoords[j] + 1) / double(_nIntervals);
406 }
407
408 return typename RefinementImp<dimension,
409 CoordType>::template Codim<codimension>::Geometry(lower,upper);
410 } else {
411 for (size_t j = 0; j < dimension; j++)
412 lower[j] = upper[j] = double(intCoords[j]) / double(_nIntervals);
413
414 return typename RefinementImp<dimension,
415 CoordType>::template Codim<codimension>::Geometry(lower,upper,std::bitset<dimension>(0));
416 }
417 }
418
419#endif // DOXYGEN
420
421 } // namespace HCube
422
423 // ///////////////////////
424 //
425 // The refinement traits
426 //
427
428#ifndef DOXYGEN
429 template<unsigned topologyId, class CoordType, unsigned coerceToId,
430 int dim>
431 struct Traits<
432 topologyId, CoordType, coerceToId, dim,
433 typename std::enable_if<
434 (dim >= 2 &&
435 (GeometryTypes::cube(dim).id() >> 1) ==
436 (topologyId >> 1) &&
437 (GeometryTypes::cube(dim).id() >> 1) ==
438 (coerceToId >> 1)
439 )>::type
440 >
441 {
442 typedef HCube::RefinementImp<dim, CoordType> Imp;
443 };
444#endif
445
446 } // namespace RefinementImp
447
448} // namespace Dune
449
450#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.
Definition affinegeometry.hh:21
A geometry implementation for axis-aligned hypercubes.
Definition axisalignedcubegeometry.hh:50
Static tag representing a codimension.
Definition dimension.hh:24
Refinement implementation for hypercubes
Definition hcube.cc:74
Codim< 0 >::SubEntityIterator ElementIterator
Definition hcube.cc:85
static ElementIterator eEnd(unsigned nIntervals)
Definition hcube.cc:152
static unsigned nVertices(unsigned nIntervals)
Definition hcube.cc:108
static VertexIterator vEnd(unsigned nIntervals)
Definition hcube.cc:125
FieldVector< int,(1<< dimension)> IndexVector
Definition hcube.cc:86
Codim< dimension >::SubEntityIterator VertexIterator
Definition hcube.cc:83
FieldVector< CoordType, dimension > CoordVector
Definition hcube.cc:84
static unsigned nElements(unsigned nIntervals)
Definition hcube.cc:133
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:79
static ElementIterator eBegin(unsigned nIntervals)
Definition hcube.cc:144
static constexpr int dimension
Know your own dimension.
Definition hcube.cc:77
static VertexIterator vBegin(unsigned nIntervals)
Definition hcube.cc:117
Dune::AxisAlignedCubeGeometry< CoordType, dimension-codimension, dimension > Geometry
Definition hcube.cc:102
SubEntityIterator base class for hypercube refinement.
Definition hcube.cc:174
Refinement::template Codim< dimension >::SubEntityIterator Common
Definition hcube.cc:187
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:219
Refinement::template Codim< 0 >::SubEntityIterator Common
Definition hcube.cc:220
SubEntityIterator(unsigned int index, unsigned int nIntervals)
RefinementImp< dimension, CoordType > Refinement
Definition hcube.cc:281
Refinement::template Codim< codimension >::SubEntityIterator This
Definition hcube.cc:282