dune-geometry 2.9.1
Loading...
Searching...
No Matches
virtualrefinement.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 (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5
6#ifndef DUNE_GEOMETRY_VIRTUALREFINEMENT_CC
7#define DUNE_GEOMETRY_VIRTUALREFINEMENT_CC
8
14#include <cassert>
15#include <typeinfo>
16
17#include <dune/common/exceptions.hh>
18#include <dune/common/fvector.hh>
19#include <dune/common/iteratorfacades.hh>
20
21#include "type.hh"
22#include "refinement.hh"
23
24namespace Dune
25{
26 // //////////////////////////////////////////
27 //
28 // The virtual base class and its iterators
29 //
30
31 //
32 // Refinement
33 //
34
35 template<int dimension, class CoordType>
42
43 template<int dimension, class CoordType>
50
51 template<int dimension, class CoordType>
58
59 template<int dimension, class CoordType>
66
67 //
68 // The iterators
69 //
70
71 template<int dimension, class CoordType, int codimension>
73
74 // The iterator for vertices
75 template<int dimension, class CoordType>
76 class VirtualRefinementSubEntityIteratorSpecial<dimension, CoordType, dimension>
77 {};
78
79 // The iterator for elements
80 template<int dimension, class CoordType>
81 class VirtualRefinementSubEntityIteratorSpecial<dimension, CoordType, 0>
82 {
83 public:
85 typedef typename Refinement::template Codim<0>::SubEntityIterator Common;
87
88 IndexVector vertexIndices() const;
89 };
90
91 template<int dimension, class CoordType>
94 vertexIndices() const
95 {
96 return static_cast<const Common *>(this)->backend->vertexIndices();
97 }
98
99 // The iterator common stuff
100 template<int dimension, class CoordType>
101 template<int codimension>
102 class VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator
103 : public ForwardIteratorFacade<typename VirtualRefinement<dimension, CoordType>::template Codim<codimension>::SubEntityIterator, int>,
104 public VirtualRefinementSubEntityIteratorSpecial<dimension, CoordType, codimension>
105 {
106 public:
108 typedef typename Refinement::template Codim<codimension>::SubEntityIterator This;
109 typedef typename Refinement::template SubEntityIteratorBack<codimension> IteratorBack;
111
113 SubEntityIterator(const This &other);
115
116 This &operator=(const This &other);
117
118 bool equals(const This &other) const;
119 void increment();
120
121 int index() const;
122
123 // If you simply use an unqualified CoordVector here g++-4.2 chokes
125 CoordVector coords() const;
126 private:
127 friend class VirtualRefinementSubEntityIteratorSpecial<dimension, CoordType, codimension>;
128 IteratorBack *backend;
129 };
130
131#ifndef DOXYGEN
132 template<int dimension, class CoordType>
133 template<int codimension>
135 SubEntityIterator(IteratorBack *backend_)
136 : backend(backend_)
137 {}
138
139 template<int dimension, class CoordType>
140 template<int codimension>
142 SubEntityIterator(const This &other)
143 : backend(other.backend->clone())
144 {}
145
146 template<int dimension, class CoordType>
147 template<int codimension>
148 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
149 ~SubEntityIterator()
150 {
151 delete backend;
152 }
153
154 template<int dimension, class CoordType>
155 template<int codimension>
156 typename VirtualRefinement<dimension, CoordType>::template Codim<codimension>::SubEntityIterator &
157 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
158 operator=(const This &other)
159 {
160 delete backend;
161 backend = other.backend->clone();
162 }
163
164 template<int dimension, class CoordType>
165 template<int codimension>
166 bool
167 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
168 equals(const This &other) const
169 { return *backend == *(other.backend); }
170
171 template<int dimension, class CoordType>
172 template<int codimension>
173 void
174 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
175 increment()
176 {
177 ++*backend;
178 }
179
180 template<int dimension, class CoordType>
181 template<int codimension>
182 int
183 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
184 index() const
185 { return backend->index(); }
186
187 template<int dimension, class CoordType>
188 template<int codimension>
189 typename VirtualRefinement<dimension, CoordType>::template Codim<codimension>::SubEntityIterator::CoordVector
190 VirtualRefinement<dimension, CoordType>::Codim<codimension>::SubEntityIterator::
191 coords() const
192 { return backend->coords(); }
193#endif // DOXYGEN
194
195 //
196 // The iterator backend
197 //
198
199 template<int dimension, class CoordType, int codimension>
201
202 // The iterator backend for vertices
203 template<int dimension, class CoordType>
204 class VirtualRefinementSubEntityIteratorBackSpecial<dimension, CoordType, dimension>
205 {
206 public:
207
210 };
211
212 // The iterator backend for elements
213 template<int dimension, class CoordType>
225
226 // The iterator backend common stuff
227 template<int dimension, class CoordType>
228 template<int codimension>
229 class VirtualRefinement<dimension, CoordType>::SubEntityIteratorBack
230 : public VirtualRefinementSubEntityIteratorBackSpecial<dimension, CoordType, codimension>
231 {
232 public:
234 typedef typename Refinement::template SubEntityIteratorBack<codimension> This;
236
238
239 virtual This *clone() const = 0;
240
241 virtual bool operator==(const This &other) const = 0;
242 virtual This &operator++() = 0;
243
244 virtual int index() const = 0;
245 virtual CoordVector coords() const = 0;
246 };
247
248 // /////////////////////////////////////////////////
249 //
250 // The derived classes and their iterator backends
251 //
252
253 //
254 // The refinement implementation
255 //
256
257 template<unsigned topologyId, class CoordType,
258 unsigned coerceToId, int dimension>
260 : public Dune::VirtualRefinement<dimension, CoordType>
261 {
262 public:
265
266 template<int codimension>
268
269 int nVertices(Dune::RefinementIntervals tag) const;
270
271 int nElements(Dune::RefinementIntervals tag) const;
272
274 private:
276
281 };
282
283 template<unsigned topologyId, class CoordType,
284 unsigned coerceToId, int dimension>
285 VirtualRefinementImp<topologyId, CoordType, coerceToId, dimension> &
291
292 template<unsigned topologyId, class CoordType,
293 unsigned coerceToId, int dimension>
299
300 template<unsigned topologyId, class CoordType,
301 unsigned coerceToId, int dimension>
305 {
306 return new SubEntityIteratorBack<dimension>(StaticRefinement::vBegin(tag));
307 }
308
309 template<unsigned topologyId, class CoordType,
310 unsigned coerceToId, int dimension>
312 VirtualRefinementImp<topologyId, CoordType, coerceToId, dimension>::
313 vEndBack(Dune::RefinementIntervals tag) const
314 {
315 return new SubEntityIteratorBack<dimension>(StaticRefinement::vEnd(tag));
316 }
317
318 template<unsigned topologyId, class CoordType,
319 unsigned coerceToId, int dimension>
324
325 template<unsigned topologyId, class CoordType,
326 unsigned coerceToId, int dimension>
330 {
331 return new SubEntityIteratorBack<0>(StaticRefinement::eBegin(tag));
332 }
333
334 template<unsigned topologyId, class CoordType,
335 unsigned coerceToId, int dimension>
337 VirtualRefinementImp<topologyId, CoordType, coerceToId, dimension>::
338 eEndBack(Dune::RefinementIntervals tag) const
339 {
340 return new SubEntityIteratorBack<0>(StaticRefinement::eEnd(tag));
341 }
342
343 //
344 // The iterator backend implementation
345 //
346
347 // The iterator backend implementation specialties
348 template<unsigned topologyId, class CoordType,
349 unsigned coerceToId, int dimension, int codimension>
351
352 // The iterator backend implementation specialties for vertices
353 template<unsigned topologyId, class CoordType,
354 unsigned coerceToId, int dimension>
355 class VirtualRefinementImpSubEntityIteratorBackSpecial<topologyId, CoordType, coerceToId, dimension, dimension>
356 : public VirtualRefinement<dimension, CoordType>::template SubEntityIteratorBack<dimension>
357 {};
358
359 // The iterator backend implementation specialties for elements
360
361 template<unsigned topologyId, class CoordType,
362 unsigned coerceToId, int dimension>
363 class VirtualRefinementImpSubEntityIteratorBackSpecial<topologyId, CoordType, coerceToId, dimension, 0>
364 : public VirtualRefinement<dimension, CoordType>::template SubEntityIteratorBack<0>
365 {
366 public:
368 typedef typename VirtualRefinementImp::template SubEntityIteratorBack<0> Common;
372
373 IndexVector vertexIndices() const;
374 };
375
376 template<unsigned topologyId, class CoordType,
377 unsigned coerceToId, int dimension>
380 vertexIndices() const
381 {
382 IndexVector vIndices;
383 vIndices.reserve(StaticRefinement::IndexVector::dimension);
384
385 typename StaticRefinement::IndexVector sIndices = static_cast<const Common *>(this)->backend.vertexIndices();
386 for(int i = 0; i < StaticRefinement::IndexVector::dimension; ++i)
387 vIndices.push_back(sIndices[i]);
388 return vIndices;
389 }
390
391 // The shared iterator backend implementation
392 template<unsigned topologyId, class CoordType,
393 unsigned coerceToId, int dimension>
394 template<int codimension>
395 class VirtualRefinementImp<topologyId, CoordType, coerceToId, dimension>::SubEntityIteratorBack
396 : public VirtualRefinementImpSubEntityIteratorBackSpecial<topologyId, CoordType, coerceToId, dimension, codimension>
397 {
398 public:
399 typedef typename StaticRefinement::template Codim<codimension>::SubEntityIterator BackendIterator;
401 typedef typename VirtualRefinement::template SubEntityIteratorBack<codimension> Base;
403
405 SubEntityIteratorBack(const This &other);
406
407 Base *clone() const;
408
409 bool operator==(const Base &other) const;
410 Base &operator++();
411
412 int index() const;
413 CoordVector coords() const;
414
415 private:
416 friend class VirtualRefinementImpSubEntityIteratorBackSpecial<topologyId, CoordType, coerceToId, dimension, codimension>;
417 BackendIterator backend;
418 };
419
420 template<unsigned topologyId, class CoordType,
421 unsigned coerceToId, int dimension>
422 template<int codimension>
427
428 template<unsigned topologyId, class CoordType,
429 unsigned coerceToId, int dimension>
430 template<int codimension>
432 SubEntityIteratorBack(const This &other)
433 : VirtualRefinementImpSubEntityIteratorBackSpecial<topologyId, CoordType, coerceToId, dimension, codimension>(other),
434 backend(other.backend)
435 {}
436
437 template<unsigned topologyId, class CoordType, unsigned coerceToId, int dimension>
438 template<int codimension>
443
444 template<unsigned topologyId, class CoordType, unsigned coerceToId, int dimension>
445 template<int codimension>
446 bool
448 operator==(const Base &other) const
449 {
450 try {
451 return backend == dynamic_cast<const This &>(other).backend;
452 }
453 catch(const std::bad_cast&)
454 {
455 return false;
456 }
457 }
458
459 template<unsigned topologyId, class CoordType, unsigned coerceToId, int dimension>
460 template<int codimension>
468
469 template<unsigned topologyId, class CoordType, unsigned coerceToId, int dimension>
470 template<int codimension>
471 int
475
476 template<unsigned topologyId, class CoordType, unsigned coerceToId, int dimension>
477 template<int codimension>
482
483 // ////////////////////////
484 //
485 // The refinement builder
486 //
487
488 template<int dimension, class CoordType>
489 class RefinementBuilder;
490
501 template<int dimension, class CoordType>
504 GeometryType geometryType,
506 GeometryType coerceTo)
507 {
508 // Check that the user used valid geometry types
509 assert(geometryType.dim() == dimension && coerceTo.dim() == dimension);
510 return RefinementBuilder<dimension, CoordType>::build( geometryType.id(), coerceTo.id() );
511 }
512
513 // In principle the trick with the class is no longer necessary,
514 // but I'm keeping it in here so it will be easier to specialize
515 // buildRefinement when someone implements pyramids and prisms
516 template<int dimension, class CoordType>
518 {
519 public:
520 static
522 build(unsigned topologyId, unsigned coerceToId)
523 {
524 topologyId &= ~1;
525 coerceToId &= ~1;
526
527 constexpr unsigned idSimplex = GeometryTypes::simplex(dimension).id() & ~1;
528 constexpr unsigned idCube = GeometryTypes::cube(dimension).id() & ~1;
529
530 switch( topologyId )
531 {
532 //case GeometryType::simplex:
533 case idSimplex :
534 //switch( coerceTo )
535 switch( coerceToId )
536 {
537 //case GeometryType::simplex:
538 case idSimplex :
540 default :
541 break;
542 }
543 break;
544
545 //case GeometryType::cube:
546 case idCube :
547 switch( coerceToId )
548 {
549 case idSimplex :
551 case idCube :
553 default :
554 break;
555 }
556 break;
557
558 default :
559 break;
560 }
561 DUNE_THROW( NotImplemented, "No Refinement<" << topologyId << ", CoordType, "
562 << coerceToId << " >.");
563 }
564 };
565
566 template<class CoordType>
567 class RefinementBuilder<1, CoordType>
568 {
569 static const std::size_t dimension = 1;
570 public:
571 static
573 build(unsigned topologyId, unsigned coerceToId)
574 {
575 topologyId &= ~1;
576 coerceToId &= ~1;
577
578 constexpr unsigned idSimplex = GeometryTypes::simplex(dimension).id() & ~1;
579
580 if (topologyId == 0 && coerceToId == 0)
582
583 DUNE_THROW( NotImplemented, "No Refinement<" << topologyId << ", CoordType, "
584 << coerceToId << " >.");
585 }
586 };
587
588 template<class CoordType>
589 class RefinementBuilder<3, CoordType>
590 {
591 static const std::size_t dimension = 3;
592 public:
593 static
595 build(unsigned topologyId, unsigned coerceToId)
596 {
597 topologyId &= ~1;
598 coerceToId &= ~1;
599
600 constexpr unsigned idSimplex = GeometryTypes::simplex(dimension).id() & ~1;
601 constexpr unsigned idCube = GeometryTypes::cube(dimension).id() & ~1;
602 constexpr unsigned idPrism = GeometryTypes::prism.id() & ~1;
603 constexpr unsigned idPyramid = GeometryTypes::pyramid.id() & ~1;
604
605 switch( topologyId )
606 {
607 //case GeometryType::simplex:
608 case idSimplex :
609 //switch( coerceTo )
610 switch( coerceToId )
611 {
612 //case GeometryType::simplex:
613 case idSimplex :
615 default :
616 break;
617 }
618 break;
619
620 //case GeometryType::cube:
621 case idCube :
622 switch( coerceToId )
623 {
624 case idSimplex :
626 case idCube :
628 default :
629 break;
630 }
631 break;
632
633 //case GeometryType::prism:
634 case idPrism :
635 switch( coerceToId )
636 {
637 case idSimplex :
639 default :
640 break;
641 }
642 break;
643
644 //case GeometryType::pyramid:
645 case idPyramid :
646 switch( coerceToId )
647 {
648 case idSimplex :
650 default :
651 break;
652 }
653 break;
654
655 default :
656 break;
657 }
658 DUNE_THROW( NotImplemented, "No Refinement<" << topologyId << ", CoordType, "
659 << coerceToId << " >.");
660 }
661 };
662
663} // namespace Dune
664
665#endif // DUNE_GEOMETRY_VIRTUALREFINEMENT_CC
A unique label for each type of element that can occur in a grid.
This file simply includes all Refinement implementations so you don't have to do them separately.
Definition affinegeometry.hh:21
VirtualRefinement< dimension, CoordType > & buildRefinement(GeometryType geometryType, GeometryType coerceTo)
return a reference to the VirtualRefinement according to the parameters
Definition virtualrefinement.cc:503
Static tag representing a codimension.
Definition dimension.hh:24
Holds the number of refined intervals per axis needed for virtual and static refinement.
Definition base.cc:94
Wrap each Refinement implementation to get a consistent interface.
Definition base.cc:141
static ElementIterator eEnd(Dune::RefinementIntervals tag)
Get an ElementIterator.
Definition base.cc:247
static ElementIterator eBegin(Dune::RefinementIntervals tag)
Get an ElementIterator.
Definition base.cc:237
static int nElements(Dune::RefinementIntervals tag)
Get the number of Elements.
Definition base.cc:227
static int nVertices(Dune::RefinementIntervals tag)
Get the number of Vertices.
Definition base.cc:197
static VertexIterator vBegin(Dune::RefinementIntervals tag)
Get a VertexIterator.
Definition base.cc:207
typedef IndexVector
The IndexVector of the Refinement.
Definition base.cc:177
static VertexIterator vEnd(Dune::RefinementIntervals tag)
Get a VertexIterator.
Definition base.cc:217
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:126
constexpr unsigned int dim() const
Return dimension of the type.
Definition type.hh:372
constexpr unsigned int id() const
Return the topology id of the type.
Definition type.hh:377
Definition virtualrefinement.cc:72
Refinement::template Codim< 0 >::SubEntityIterator Common
Definition virtualrefinement.cc:85
VirtualRefinement< dimension, CoordType > Refinement
Definition virtualrefinement.cc:84
Refinement::IndexVector IndexVector
Definition virtualrefinement.cc:86
VirtualRefinement< dimension, CoordType >::template Codim< codimension >::SubEntityIterator::CoordVector coords() const
Refinement::template SubEntityIteratorBack< codimension > IteratorBack
Definition virtualrefinement.cc:109
Refinement::CoordVector CoordVector
Definition virtualrefinement.cc:110
Refinement::template Codim< codimension >::SubEntityIterator This
Definition virtualrefinement.cc:108
bool equals(const This &other) const
VirtualRefinement< dimension, CoordType > Refinement
Definition virtualrefinement.cc:107
Definition virtualrefinement.cc:200
virtual ~VirtualRefinementSubEntityIteratorBackSpecial()
Definition virtualrefinement.cc:208
VirtualRefinement< dimension, CoordType > Refinement
Definition virtualrefinement.cc:217
Refinement::IndexVector IndexVector
Definition virtualrefinement.cc:218
virtual ~VirtualRefinementSubEntityIteratorBackSpecial()
Definition virtualrefinement.cc:222
Definition virtualrefinement.cc:231
Refinement::template SubEntityIteratorBack< codimension > This
Definition virtualrefinement.cc:234
Refinement::CoordVector CoordVector
Definition virtualrefinement.cc:235
VirtualRefinement< dimension, CoordType > Refinement
Definition virtualrefinement.cc:233
virtual bool operator==(const This &other) const =0
virtual CoordVector coords() const =0
virtual ~SubEntityIteratorBack()
Definition virtualrefinement.cc:237
Definition virtualrefinement.cc:261
int nVertices(Dune::RefinementIntervals tag) const
Get the number of Vertices.
Definition virtualrefinement.cc:295
Dune::VirtualRefinement< dimension, CoordType > VirtualRefinement
Definition virtualrefinement.cc:264
int nElements(Dune::RefinementIntervals tag) const
Get the number of Elements.
Definition virtualrefinement.cc:320
static VirtualRefinementImp< topologyId, CoordType, coerceToId, dimension > & instance()
Definition virtualrefinement.cc:286
Dune::StaticRefinement< topologyId, CoordType, coerceToId, dimension > StaticRefinement
Definition virtualrefinement.cc:263
Definition virtualrefinement.cc:397
VirtualRefinement::template SubEntityIteratorBack< codimension > Base
Definition virtualrefinement.cc:401
VirtualRefinementImp< topologyId, CoordType, coerceToId, dimension >::template SubEntityIteratorBack< codimension > This
Definition virtualrefinement.cc:400
StaticRefinement::template Codim< codimension >::SubEntityIterator BackendIterator
Definition virtualrefinement.cc:399
VirtualRefinement::CoordVector CoordVector
Definition virtualrefinement.cc:402
VirtualRefinementImp::template SubEntityIteratorBack< 0 > Common
Definition virtualrefinement.cc:368
VirtualRefinement< dimension, CoordType > RefinementBase
Definition virtualrefinement.cc:370
Dune::VirtualRefinementImp< topologyId, CoordType, coerceToId, dimension > VirtualRefinementImp
Definition virtualrefinement.cc:367
VirtualRefinementImp::StaticRefinement StaticRefinement
Definition virtualrefinement.cc:369
Definition virtualrefinement.cc:518
static VirtualRefinement< dimension, CoordType > & build(unsigned topologyId, unsigned coerceToId)
Definition virtualrefinement.cc:522
static VirtualRefinement< dimension, CoordType > & build(unsigned topologyId, unsigned coerceToId)
Definition virtualrefinement.cc:573
static VirtualRefinement< dimension, CoordType > & build(unsigned topologyId, unsigned coerceToId)
Definition virtualrefinement.cc:595
VirtualRefinement base class.
Definition virtualrefinement.hh:294
SubEntityIteratorBack< dimension > VertexIteratorBack
Definition virtualrefinement.hh:318
VertexIterator vBegin(Dune::RefinementIntervals tag) const
Get an VertexIterator.
Definition virtualrefinement.cc:38
ElementIterator eBegin(Dune::RefinementIntervals tag) const
Get an ElementIterator.
Definition virtualrefinement.cc:54
ElementIterator eEnd(Dune::RefinementIntervals tag) const
Get an ElementIterator.
Definition virtualrefinement.cc:62
Codim< 0 >::SubEntityIterator ElementIterator
The ElementIterator of the VirtualRefinement.
Definition virtualrefinement.hh:301
VertexIterator vEnd(Dune::RefinementIntervals tag) const
Get an VertexIterator.
Definition virtualrefinement.cc:46
FieldVector< CoordType, dimension > CoordVector
The CoordVector of the VirtualRefinement.
Definition virtualrefinement.hh:308
std::vector< int > IndexVector
The IndexVector of the VirtualRefinement.
Definition virtualrefinement.hh:314
Codim< dimension >::SubEntityIterator VertexIterator
The VertexIterator of the VirtualRefinement.
Definition virtualrefinement.hh:299
SubEntityIteratorBack< 0 > ElementIteratorBack
Definition virtualrefinement.hh:319
codim database of VirtualRefinement
Definition virtualrefinement.hh:378