KASKADE 7 development version
Loading...
Searching...
No Matches
gridGeneration.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2013-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12/*
13 * Created on: 25.06.2013
14 * Author: bzflubko
15 */
16
17#ifndef KASKADE_GRID_GENERATION_HH_
18#define KASKADE_GRID_GENERATION_HH_
19
20#include <dune/common/fvector.hh>
21#include <dune/grid/common/gridfactory.hh>
22
23#include "fem/fixdune.hh"
26#include "utilities/timing.hh"
27
28#include <algorithm>
29#include <cmath>
30#include <memory>
31#include <numbers>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace Kaskade
37{
41 namespace GridGeneration_Detail
42 {
56 template <class Type,
57 int dim = std::is_same_v<Type,Cube<typename Type::Real>>? 3: 2>
58 std::pair<std::vector<Dune::FieldVector<typename Type::Real,dim>>,
59 std::vector<std::vector<unsigned int>>>
60 extractSimplices(std::vector<Type> const& cubes, typename Type::Real tol=1e-6)
61 {
62 using Vec = Dune::FieldVector<typename Type::Real,dim>;
63
64 // A lexical ordering on the vertices (with tolerance).
65 auto vLess = [&](Vec const& x, Vec const& y)
66 {
67 for (int i=0; i<dim; ++i)
68 if (x[i] < y[i]-tol)
69 return true;
70 else if (x[i] > y[i]+tol)
71 return false;
72
73 return false; // equal, i.e. all components within tolerance, and thus not less
74 };
75
76 // Equality of vectors (within tolerance tol)
77 auto vEq = [&](Vec const& x, Vec const& y)
78 {
79 return !(vLess(x,y) || vLess(y,x));
80 };
81
82 // First we create a list of unique vertices, eliminating any duplicates,
83 // such that we get unique and stable vertex ids.
84 std::vector<Vec> vertices;
85 for (auto const& cube: cubes)
86 {
87 auto const& localVertices = cube.getVertices();
88 vertices.insert(vertices.end(),localVertices.begin(),localVertices.end());
89 }
90 std::sort(vertices.begin(),vertices.end(),vLess);
91 vertices.erase(std::unique(vertices.begin(),vertices.end(),vEq),vertices.end());
92
93
94
95 std::vector<std::vector<unsigned int>> simplices; // TODO: use std::array here, the number of corners is fixed
96
97 for (Type const& cube: cubes)
98 {
99 auto const& localVertices = cube.getVertices();
100 auto const& localSimplices = cube.getSimplices();
101 size_t const nVertices = localVertices.size();
102
103 // maps local vertex ids to global ones (i.e. the index in 'vertices')
104 std::vector<size_t> vertexIdMap(nVertices);
105 for(size_t i=0; i<nVertices; ++i)
106 {
107 auto it = std::lower_bound(vertices.begin(),vertices.end(),localVertices[i],vLess);
108 assert(it != vertices.end());
109 vertexIdMap[i] = it - vertices.begin();
110 }
111
112 // map local connectivity onto global one (wrt. numbering in vector 'vertices')
113 std::vector<unsigned int> newSimplex;
114 for(std::vector<unsigned int> const& tet: localSimplices)
115 {
116 newSimplex.resize(tet.size());
117 for(size_t i=0; i<newSimplex.size(); ++i)
118 newSimplex[i] = vertexIdMap[tet[i]];
119 simplices.push_back(newSimplex);
120 }
121 }
122
123 return std::make_pair(vertices,simplices);
124 }
125
126
135 template <class Scalar>
136 std::vector<Square<Scalar> > fillRectangle(Dune::FieldVector<Scalar,2> const& x0,
137 Dune::FieldVector<Scalar,2> const& dx,
138 Dune::FieldVector<Scalar,2> const& dh,
139 bool symmetric=true)
140 {
141 std::vector<Square<Scalar> > squares;
142 Dune::FieldVector<Scalar,2> dh2(dh);
143 for(int i=0; i<2; ++i)
144 if(dh[i]>dx[i]) // if dh larger than sidelength, set dh = dx
145 dh2[i] = dx[i];
146
147
148 size_t xi_end = (size_t)round(fabs(dx[0]/dh2[0]));
149 size_t yi_end = (size_t)round(fabs(dx[1]/dh2[1]));
150 Dune::FieldVector<Scalar,2> dsquare(dh2), x(x0);
151
152 for(size_t xi=0; xi<xi_end; ++xi)
153 {
154 x[0] = x0[0] + xi*dh2[0];
155 for(size_t yi=0; yi<yi_end; ++yi)
156 {
157 x[1] = x0[1] + yi*dh2[1];
158 squares.push_back(Square<Scalar>(x,dsquare,symmetric));
159 }
160 }
161
162 return squares;
163 }
164
173 template <class Scalar>
174 std::vector<Square<Scalar> > fillRectangle(Dune::FieldVector<Scalar,2> const& x0,
175 Dune::FieldVector<Scalar,2> const& dx,
176 Scalar dh,
177 bool symmetric=true)
178 {
179 return fillRectangle(x0,dx,Dune::FieldVector<Scalar,2>(dh),symmetric);
180 }
181
190 template <class Scalar>
191 std::vector<Cube<Scalar>> fillCuboid(Dune::FieldVector<Scalar,3> const& x0,
192 Dune::FieldVector<Scalar,3> const& dx,
193 Dune::FieldVector<Scalar,3> const& dh,
194 bool symmetric=true)
195 {
196 std::vector<Cube<Scalar>> cubes;
197
198 Dune::FieldVector<Scalar,3> dh2(dh);
199 for(int i=0; i<3; ++i)
200 if(dh[i]>dx[i]) // if dh larger than sidelength, set dh = dx
201 dh2[i] = dx[i];
202
203 size_t xi_end = (size_t)round(fabs(dx[0]/dh2[0])),
204 yi_end = (size_t)round(fabs(dx[1]/dh2[1])),
205 zi_end = (size_t)round(fabs(dx[2]/dh2[2]));
206
207 if (xi_end==0 || yi_end==0 || zi_end==0)
208 throw GridException("empty cubes list",__FILE__,__LINE__);
209
210 Dune::FieldVector<Scalar,3> x(x0);
211
212 for(size_t xi=0; xi<xi_end; ++xi)
213 {
214 x[0] = x0[0] + xi*dh2[0];
215 for(size_t yi=0; yi<yi_end; ++yi)
216 {
217 x[1] = x0[1] + yi*dh2[1];
218 for(size_t zi=0; zi<zi_end; ++zi)
219 {
220 x[2] = x0[2] + zi*dh2[2];
221 cubes.push_back(Cube<Scalar>(x,dh2,symmetric));
222 }
223 }
224 }
225
226 return cubes;
227 }
228
229
238 template <class Scalar>
239 std::vector<Cube<Scalar>> fillCuboid(Dune::FieldVector<Scalar,3> const& x0,
240 Dune::FieldVector<Scalar,3> const& dx,
241 Scalar dh,
242 bool symmetric=true)
243 {
244 return fillCuboid(x0,dx,Dune::FieldVector<Scalar,3>(dh),symmetric);
245 }
246
247
248
249 template<class Scalar, int dim>
250 std::vector<Cube<Scalar>> fillTwoLayerCuboid(Dune::FieldVector<Scalar,dim> x0,
251 Dune::FieldVector<Scalar,dim> const& dx1,
252 Dune::FieldVector<Scalar,dim> const& dx2,
253 Dune::FieldVector<Scalar,dim> const& dh1,
254 Scalar dh2,
255 bool symmetric=true)
256 {
257 auto cubes = fillCuboid(x0,dx1,dh1,symmetric);
258
259 x0[dim-1] += dx1[dim-1];
260 auto cubes2 = fillCuboid(x0,dx2,dh2,symmetric);
261 for( Cube<Scalar> const& cube : cubes2 ) cubes.push_back(cube);
262
263 return cubes;
264 }
265
266 template<class Scalar, int dim>
267 std::vector<Cube<Scalar> > fillTwoLayerCuboid(Dune::FieldVector<Scalar,dim> x0,
268 Dune::FieldVector<Scalar,dim> const& dx1,
269 Dune::FieldVector<Scalar,dim> const& dx2,
270 Dune::FieldVector<Scalar,dim> const& dh1,
271 Dune::FieldVector<Scalar,dim> const& dh2,
272 bool symmetric=true)
273 {
274 auto cubes = fillCuboid(x0,dx1,dh1,symmetric);
275
276 x0[dim-1] += dx1[dim-1];
277 auto cubes2 = fillCuboid(x0,dx2,dh2,symmetric);
278 for( Cube<Scalar> const& cube : cubes2 ) cubes.push_back(cube);
279
280 return cubes;
281 }
282
283 template <int dim>
284 struct FillCuboid;
285
286 template <>
287 struct FillCuboid<3>
288 {
289 template <class Scalar, class EdgeLength>
290 static std::vector<Cube<Scalar> > apply(Dune::FieldVector<Scalar,3> const& x0,
291 Dune::FieldVector<Scalar,3> const& dx,
292 EdgeLength const& dh,
293 bool symmetric=true)
294 {
295 return fillCuboid(x0, dx, dh, symmetric);
296 }
297 };
298
299 template <>
300 struct FillCuboid<2>
301 {
302 template <class Scalar, class EdgeLength>
303 static std::vector<Square<Scalar> > apply(Dune::FieldVector<Scalar,2> const& x0,
304 Dune::FieldVector<Scalar,2> const& dx,
305 EdgeLength const& dh,
306 bool symmetric=true)
307 {
308 return fillRectangle(x0, dx, dh, symmetric);
309 }
310 };
311
312
323 template <class Scalar, int dim>
324 std::vector< typename std::conditional<dim==3,Cube<Scalar>,Square<Scalar> >::type >
325 fillLShape(Dune::FieldVector<Scalar,dim> x0,
326 Scalar dx, Scalar dy,
327 Dune::FieldVector<Scalar,dim> const& thickness,
328 Scalar dh, bool symmetric=true)
329 {
330 // fill horizontal line
331 Dune::FieldVector<Scalar,dim> d0(thickness); d0[0] = dx;
332 auto cubes = FillCuboid<dim>::apply(x0,d0,dh,symmetric);
333
334 // fill vertical line
335 d0 = thickness; d0[1] = dy;
336 x0[1] += thickness[1];
337 auto cubes2 = FillCuboid<dim>::apply(x0,d0,dh,symmetric);
338
339 // merge
340 for(auto const& cube : cubes2)
341 cubes.push_back(cube);
342
343 return cubes;
344 }
345
356 template <class Scalar, int dim>
357 std::vector< typename std::conditional<dim==3,Cube<Scalar>,Square<Scalar>>::type>
358 fillTShape(Dune::FieldVector<Scalar,dim> x0,
359 Scalar dx, Scalar dy,
360 Dune::FieldVector<Scalar,dim> const& thickness,
361 Scalar dh, bool symmetric=true)
362 {
363 // fill vertical line
364 x0[0] -= 0.5*thickness[0];
365 Dune::FieldVector<Scalar,dim> d0(thickness); d0[1] = dy - thickness[1];
366 auto cubes = FillCuboid<dim>::apply(x0,d0,dh,symmetric);
367
368 // fill vertical line
369 x0[0] += 0.5*thickness[0] - 0.5*dx;
370 x0[1] += d0[1];
371 d0[1] = thickness[1]; d0[0] = dx;
372 auto cubes2 = FillCuboid<dim>::apply(x0,d0,dh,symmetric);
373
374 // merge
375 for(auto const& cube : cubes2) cubes.push_back(cube);
376
377 return cubes;
378 }
379
380
381 } // end of namespace GridGeneration_Detail
385
386 // -----------------------------------------------------------------------------------------------
387 // -----------------------------------------------------------------------------------------------
388
389 auto constexpr identityDeformation = [](auto const& x) { return x; };
391
392
413 template <class Grid, class Type, class Deformation=IdentityDeformation>
414 std::unique_ptr<Grid> createGrid(std::vector<Type> const& cubes,
415 Deformation phi = Deformation())
416 {
417 Timings& timer = Timings::instance();
418 timer.start("extract simplices");
419 auto simplices = GridGeneration_Detail::extractSimplices(cubes);
420 timer.stop("extract simplices");
421
422 Dune::GridFactory<Grid> factory;
423 Dune::GeometryType gt(Dune::GeometryType::simplex,Type::dim);
424
425 timer.start("insert vertex");
426 for (Dune::FieldVector<double,Type::dim> const& vertex: simplices.first)
427 factory.insertVertex(phi(vertex));
428 timer.stop("insert vertex");
429
430 timer.start("insert element");
431 for (std::vector<unsigned int> const& elem: simplices.second)
432 factory.insertElement(gt,elem);
433 timer.stop("insert element");
434
435 return std::unique_ptr<Grid>(factory.createGrid());
436 }
437
459 template <class Grid, class Scalar, int dim, class EdgeLength>
460 std::unique_ptr<Grid> createCuboid(Dune::FieldVector<Scalar,dim> const& x0,
462 EdgeLength const& dh,
463 bool symmetric=true)
464 {
465 Timings& timer = Timings::instance();
466 timer.start("create cubes");
467 auto const& cubes = GridGeneration_Detail::FillCuboid<dim>::apply(x0,dx,dh,symmetric);
468 timer.stop("create cubes");
469 return createGrid<Grid>(cubes);
470 }
471
478 template <class Grid>
479 std::unique_ptr<Grid> createUnitSquare(double dh = 1., bool symmetric=true)
480 {
481 using Vec = Dune::FieldVector<double,2>;
482 return createCuboid<Grid>(Vec(0.), Vec(1.), dh, symmetric);
483 }
484
490 template <class Grid>
491 std::unique_ptr<Grid> createUnitCube(double dh = 1., bool symmetric=true)
492 {
493 typedef Dune::FieldVector<double,3> Vec;
494 return createCuboid<Grid>(Vec(0.), Vec(1.), dh, symmetric);
495 }
496
497
510 template <class Grid, class Scalar, class Dh, class Deformation=IdentityDeformation>
511 std::unique_ptr<Grid> createRectangle(Dune::FieldVector<Scalar,2> const& x0,
513 Dh dh,
514 bool symmetric=true,
515 Deformation phi=Deformation())
516 {
517 if constexpr (std::is_convertible_v<Dh, Scalar>)
518 return createGrid<Grid>(GridGeneration_Detail::fillRectangle(x0,dx,static_cast<Scalar>(dh),symmetric),phi);
519 else
520 return createGrid<Grid>(GridGeneration_Detail::fillRectangle(x0,dx,dh,symmetric),phi);
521 }
522
534 template <class Grid, class Scalar, int dim>
535 std::unique_ptr<Grid> createLShape(Dune::FieldVector<Scalar,dim> const& x0, Scalar dx, Scalar dy,
536 Dune::FieldVector<Scalar,dim> const& thickness, Scalar dh,
537 bool symmetric=true)
538 {
539 return createGrid<Grid>(GridGeneration_Detail::fillLShape(x0,dx,dy,thickness,dh,symmetric));
540 }
541
553 template <class Grid, class Scalar, int dim>
554 std::unique_ptr<Grid> createTShape(Dune::FieldVector<Scalar,dim> const& x0,
555 Scalar dx, Scalar dy,
556 Dune::FieldVector<Scalar,dim> const& thickness,
557 Scalar dh, bool symmetric=true)
558 {
559 return createGrid<Grid>(GridGeneration_Detail::fillTShape(x0,dx,dy,thickness,dh,symmetric));
560 }
561
565 template <class Grid, class Scalar, int dim>
568 Dune::FieldVector<Scalar,dim> const& dh1, Scalar dh2, bool symmetric=true)
569 {
570 return createGrid<Grid>(GridGeneration_Detail::fillTwoLayerCuboid(x0,dx1,dx2,dh1,dh2,symmetric));
571 }
572
576 template <class Grid, class Scalar, int dim>
579 Dune::FieldVector<Scalar,dim> const& dh1, Dune::FieldVector<Scalar,dim> const& dh2, bool symmetric=true)
580 {
581 return createGrid<Grid>(GridGeneration_Detail::fillTwoLayerCuboid(x0,dx1,dx2,dh1,dh2,symmetric));
582 }
583
584 // --------------------------------------------------------------------------------------------------------------------
585 // --------------------------------------------------------------------------------------------------------------------
586
592
601 template <class Grid>
602 std::unique_ptr<Grid> createHexahedron()
603 {
604 Dune::GridFactory<Grid> factory;
605 Dune::GeometryType gt(Dune::GeometryType::simplex,3);
606
607 for (int i=0; i<2; ++i)
608 for (int j=0; j<2; ++j)
609 for (int k=0; k<2; ++k)
610 {
612 x[0] = i;
613 x[1] = j;
614 x[2] = k;
615 factory.insertVertex(x);
616 }
617
618 factory.insertElement(gt,std::vector<unsigned int>{7,1,4,2});
619 factory.insertElement(gt,std::vector<unsigned int>{0,4,1,2});
620 factory.insertElement(gt,std::vector<unsigned int>{5,1,4,7});
621 factory.insertElement(gt,std::vector<unsigned int>{6,2,4,7});
622 factory.insertElement(gt,std::vector<unsigned int>{3,1,7,2});
623
624 return std::unique_ptr<Grid>(factory.createGrid());
625 }
626
636 template <class Grid>
637 std::unique_ptr<Grid>
639 {
640 Dune::GridFactory<Grid> factory;
641 Dune::GeometryType gt(Dune::GeometryType::simplex,3);
642
643 for (int i=0; i<3; ++i)
644 {
646 x[i] = extent[i];
647 factory.insertVertex(x);
648 factory.insertVertex(-x);
649 }
650 factory.insertVertex(Dune::FieldVector<double,3>());
651
652 factory.insertElement(gt,std::vector<unsigned int>{0,2,4,6});
653 factory.insertElement(gt,std::vector<unsigned int>{0,3,4,6});
654 factory.insertElement(gt,std::vector<unsigned int>{0,2,5,6});
655 factory.insertElement(gt,std::vector<unsigned int>{0,3,5,6});
656 factory.insertElement(gt,std::vector<unsigned int>{1,2,4,6});
657 factory.insertElement(gt,std::vector<unsigned int>{1,3,4,6});
658 factory.insertElement(gt,std::vector<unsigned int>{1,2,5,6});
659 factory.insertElement(gt,std::vector<unsigned int>{1,3,5,6});
660
661 return std::unique_ptr<Grid>(factory.createGrid());
662 }
663
672 template <class Grid>
673 std::unique_ptr<Grid> createIcosahedron()
674 {
675 Dune::GridFactory<Grid> factory;
676 Dune::GeometryType gt(Dune::GeometryType::simplex,3);
677
678 auto vector = [=](double x, double y, double z)
679 {
681 p[0] = x; p[1] = y; p[2] = z;
682 return p;
683 };
684
685 double s = (1+std::sqrt(5))/2;
686
687 factory.insertVertex(vector( 0, 1, s)); // 0
688 factory.insertVertex(vector( 0, 1,-s)); // 1
689 factory.insertVertex(vector( 0,-1, s)); // 2
690 factory.insertVertex(vector( 0,-1,-s)); // 3
691 factory.insertVertex(vector( 1, s, 0)); // 4
692 factory.insertVertex(vector( 1,-s, 0)); // 5
693 factory.insertVertex(vector(-1, s, 0)); // 6
694 factory.insertVertex(vector(-1,-s, 0)); // 7
695 factory.insertVertex(vector( s, 0, 1)); // 8
696 factory.insertVertex(vector( s, 0,-1)); // 9
697 factory.insertVertex(vector(-s, 0, 1)); // 10
698 factory.insertVertex(vector(-s, 0,-1)); // 11
699 factory.insertVertex(vector( 0, 0, 0)); // 12
700
701 factory.insertElement(gt,std::vector<unsigned int>{0,2, 8,12});
702 factory.insertElement(gt,std::vector<unsigned int>{0,2,10,12});
703 factory.insertElement(gt,std::vector<unsigned int>{1,3, 9,12});
704 factory.insertElement(gt,std::vector<unsigned int>{1,3,11,12});
705
706 factory.insertElement(gt,std::vector<unsigned int>{4,6, 0,12});
707 factory.insertElement(gt,std::vector<unsigned int>{4,6, 1,12});
708 factory.insertElement(gt,std::vector<unsigned int>{5,7, 2,12});
709 factory.insertElement(gt,std::vector<unsigned int>{5,7, 3,12});
710
711 factory.insertElement(gt,std::vector<unsigned int>{8,9, 4,12});
712 factory.insertElement(gt,std::vector<unsigned int>{8,9, 5,12});
713 factory.insertElement(gt,std::vector<unsigned int>{10,11,6,12});
714 factory.insertElement(gt,std::vector<unsigned int>{10,11,7,12});
715
716 factory.insertElement(gt,std::vector<unsigned int>{0,4, 8,12});
717 factory.insertElement(gt,std::vector<unsigned int>{0,6,10,12});
718 factory.insertElement(gt,std::vector<unsigned int>{3,7,11,12});
719 factory.insertElement(gt,std::vector<unsigned int>{3,5,9,12});
720 factory.insertElement(gt,std::vector<unsigned int>{1,4,9,12});
721 factory.insertElement(gt,std::vector<unsigned int>{1,6,11,12});
722 factory.insertElement(gt,std::vector<unsigned int>{2,7,10,12});
723 factory.insertElement(gt,std::vector<unsigned int>{2,5,8,12});
724
725 return std::unique_ptr<Grid>(factory.createGrid());
726 }
727
728 //-----------------------------------------------------------------------------------------------
729
746 template <class Grid>
747 std::unique_ptr<Grid> createCylinder(double radius, double height, int layers, int circumEdges=6)
748 {
749 return createCylinder<Grid>(radius,height,layers,circumEdges,[](auto const& x) { return x; });
750 }
751
768 template <class Grid, class Deformation>
769 std::unique_ptr<Grid> createCylinder(double radius, double height, int layers, int circumEdges,
770 Deformation const& deformation)
771 {
772 using std::numbers::pi;
773
774 Dune::GridFactory<Grid> factory;
775 Dune::GeometryType gt(Dune::GeometryType::simplex,3);
776
777 auto vector = [](double x, double y, double z)
778 {
780 p[0] = x; p[1] = y; p[2] = z;
781 return p;
782 };
783
784 double h = height/layers;
785 // Create vertices. These are circumEdges+1 on each of the layers+1 circular disks
786 for (int i=0; i<=layers; ++i)
787 {
788 double z = i*h;
789 factory.insertVertex(deformation(vector(0,0,z)));
790 for (int j=0; j<circumEdges; ++j)
791 {
792 double angle = 2*pi*j/circumEdges;
793 auto v = vector(radius*std::cos(angle),radius*std::sin(angle),z);
794 factory.insertVertex(deformation(v));
795 }
796 }
797
798 // Create the cells. Each prismatic disc consists of circumEdges prisms
799 // connecting the central axis with a surface quadrilateral. These prisms are split
800 // into three tetrahedra.
801 for (int i=0; i<layers; ++i)
802 {
803 // The offset of the vertex indices: circular disks below current anti-prismatic disk
804 // times the number of vertices in each circular disk.
805 int n = circumEdges+1;
806 int idxOffset = i*n;
807
808 unsigned int c0 = idxOffset; // lower center vertex
809 unsigned int c1 = idxOffset+n; // top center vertex
810
811 for (int j=0; j<circumEdges; ++j)
812 {
813 unsigned int a0 = c0+1+j; // bottom left corner of boundary quadrilateral
814 unsigned int b0 = c0+1+(j+1)%circumEdges; // bottom right corner
815 unsigned int a1 = c1+1+j; // top left corner
816 unsigned int b1 = c1+1+(j+1)%circumEdges; // top right corner
817 factory.insertElement(gt,std::vector<unsigned int>{a0,b0,c0,c1});
818 factory.insertElement(gt,std::vector<unsigned int>{a0,b1,a1,c1});
819 factory.insertElement(gt,std::vector<unsigned int>{a0,b0,b1,c1});
820 }
821 }
822
823 return std::unique_ptr<Grid>(factory.createGrid());
824 }
825
826 //-----------------------------------------------------------------------------------------------
827
838 template <class Grid>
839 std::unique_ptr<Grid> createPentagon(double radius=1)
840 {
841
842 Dune::GridFactory<Grid> factory;
843 Dune::GeometryType gt(Dune::GeometryType::simplex,2);
844
845 auto vector = [=](double x, double y)
846 {
848 p[0] = x; p[1] = y;
849 return p;
850 };
851
852 double c1, c2, s1, s2;
853 c1 = 0.25*(std::sqrt(5)-1);
854 c2 = 0.25*(std::sqrt(5)+1);
855 s1 = 0.25*(std::sqrt(10+2*std::sqrt(5)));
856 s2 = 0.25*(std::sqrt(10-2*std::sqrt(5)));
857
858 factory.insertVertex(radius*vector( 1., 0.)); // 0
859 factory.insertVertex(radius*vector( c1, s1)); // 1
860 factory.insertVertex(radius*vector(-1.*c2,s2)); // 2
861 factory.insertVertex(radius*vector(-1.*c2,-1.*s2)); // 3
862 factory.insertVertex(radius*vector( c1, -1.0*s1)); // 4
863 factory.insertVertex(radius*vector( 0., 0.)); // 5
864
865
866 factory.insertElement(gt,std::vector<unsigned int>{0,1,5});
867 factory.insertElement(gt,std::vector<unsigned int>{1,2,5});
868 factory.insertElement(gt,std::vector<unsigned int>{2,3,5});
869 factory.insertElement(gt,std::vector<unsigned int>{3,4,5});
870 factory.insertElement(gt,std::vector<unsigned int>{4,0,5});
871
872 return std::unique_ptr<Grid>(factory.createGrid());
873 }
874
875
891 template <class Grid>
892 std::unique_ptr<Grid> createUshape(double l, double d, double eps)
893 {
894 assert(eps > 0);
895 assert(d > 0);
896 assert(l > d);
897
898 Dune::GridFactory<Grid> factory;
899 Dune::GeometryType gt(Dune::GeometryType::simplex,2);
900
901 auto vector = [=](double x, double y)
902 {
904 p[0] = x; p[1] = y;
905 return p;
906 };
907
908 factory.insertVertex(vector( 0, 0)); // 0
909 factory.insertVertex(vector( (l+d)/2, 0)); // 1
910 factory.insertVertex(vector( l, 0)); // 2
911 factory.insertVertex(vector( l, d)); // 3
912 factory.insertVertex(vector( (l+d)/2, d)); // 4
913 factory.insertVertex(vector( d, d)); // 5
914 factory.insertVertex(vector( d, d+eps)); // 6
915 factory.insertVertex(vector( (l+d)/2, d+eps)); // 7
916 factory.insertVertex(vector( l, d+eps)); // 8
917 factory.insertVertex(vector( l, 2*d+eps)); // 9
918 factory.insertVertex(vector( (l+d)/2, 2*d+eps)); // 10
919 factory.insertVertex(vector( 0, 2*d+eps)); // 11
920 factory.insertVertex(vector( 0, d+eps/2)); // 12
921
922 factory.insertElement(gt,std::vector<unsigned int>{0,1,5});
923 factory.insertElement(gt,std::vector<unsigned int>{1,4,5});
924 factory.insertElement(gt,std::vector<unsigned int>{1,2,4});
925 factory.insertElement(gt,std::vector<unsigned int>{2,3,4});
926 factory.insertElement(gt,std::vector<unsigned int>{5,6,12});
927 factory.insertElement(gt,std::vector<unsigned int>{6,7,10});
928 factory.insertElement(gt,std::vector<unsigned int>{7,8,9});
929 factory.insertElement(gt,std::vector<unsigned int>{7,9,10});
930 factory.insertElement(gt,std::vector<unsigned int>{6,10,11});
931 factory.insertElement(gt,std::vector<unsigned int>{6,11,12});
932 factory.insertElement(gt,std::vector<unsigned int>{0,5,12});
933
934 return std::unique_ptr<Grid>(factory.createGrid());
935 }
936
937 //-----------------------------------------------------------------------------------------------
938 //-----------------------------------------------------------------------------------------------
939
940 template <int dim, class ctype, class GridView, class ...GridViews>
942 std::vector<std::array<size_t,dim+1>> cs,
943 GridView const& gv,
944 GridViews const& ...gvs)
945 {
946 static_assert(GridView::dimension==dim,"all grids need to have the same dimension");
947
948 auto const n = vs.size(); // Vertex index offset
949
950 // Extract and store connectivity
951 auto const& is = gv.indexSet(); // Add connectivity: vertex indices of the
952 for (auto c: Dune::elements(gv)) // cells. Keep in mind that we need to shift
953 { // the vertex indices by the offset given by
954 std::array<size_t,dim+1> ci; // the total number of vertices in the
955 for (int i=0; i<dim+1; ++i) // previous grids.
956 ci[i] = n + is.subIndex(c,i,dim);
957 cs.push_back(ci);
958 }
959
960 // Extract and store vertex positions
961 vs.insert(vs.end(),is.size(dim),Dune::FieldVector<ctype,dim>());
962 for (auto v: Dune::vertices(gv)) // Add the vertex positions themselves
963 vs[n+is.index(v)] = v.geometry().center(); // note that vertices(gv) order is undefined
964
965 if constexpr (sizeof...(GridViews) > 0) // There are further grids coming... recurse.
966 return unionGrid(std::move(vs),std::move(cs),gvs...);
967 else // No further grids... create final grid.
968 {
969 using Grid = typename GridView::Grid;
970 Dune::GridFactory<Grid> factory;
971 Dune::GeometryType gt(Dune::GeometryType::simplex,dim);
972
973 for (auto const& v: vs) // Enter vertices
974 factory.insertVertex(v);
975
976 std::vector<unsigned int> ci(dim+1); // Enter cells. We need to conver from array
977 for (auto const& c: cs) // to vector, as the factory expects
978 { // vectors of unsigned int.
979 std::copy(c.begin(),c.end(),ci.begin());
980 factory.insertElement(gt,ci);
981 }
982
983 // TODO: treat boundary segment indices
984
985 return std::unique_ptr<Grid>(factory.createGrid());
986 }
987 }
988
989
1011 template <class GridView, class ...GridViews>
1012 auto unionGrid(GridView const& gv, GridViews const& ...gvs)
1013 {
1014 constexpr int dim = GridView::dimension;
1016 std::vector<std::array<size_t,dim+1>>(),
1017 gv,gvs...);
1018 }
1019
1020} // end of namespace Kaskade
1021
1022#endif /* GRIDGENERATION_HH_ */
Supports gathering and reporting execution times information for nested program parts.
Definition timing.hh:64
static Timings & instance()
Returns a reference to a single default instance.
void stop(std::string const &name)
Stops the timing of given section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
This file contains various utility functions that augment the basic functionality of Dune.
std::unique_ptr< Grid > createLShape(Dune::FieldVector< Scalar, dim > const &x0, Scalar dx, Scalar dy, Dune::FieldVector< Scalar, dim > const &thickness, Scalar dh, bool symmetric=true)
Fill L-shaped domain with cubes.
std::unique_ptr< Grid > createUnitSquare(double dh=1., bool symmetric=true)
Creates a unit square, filled regularly.
std::unique_ptr< Grid > createGrid(std::vector< Type > const &cubes, Deformation phi=Deformation())
Extract simplicial grid from list of cubes.
std::unique_ptr< Grid > createPentagon(double radius=1)
Creates an regular pentagon consisting of five triangles, centered at the origin and with radius r.
std::unique_ptr< Grid > createUnitCube(double dh=1., bool symmetric=true)
Creates a unit cube.
std::unique_ptr< Grid > createCuboid(Dune::FieldVector< Scalar, dim > const &x0, Dune::FieldVector< Scalar, dim > const &dx, EdgeLength const &dh, bool symmetric=true)
Creates a uniform simplicial grid on a rectangular or cuboid domain.
std::unique_ptr< Grid > createUshape(double l, double d, double eps)
Creates a U-shaped domain in .
std::unique_ptr< Grid > createCylinder(double radius, double height, int layers, int circumEdges=6)
Creates a cylinder.
std::unique_ptr< Grid > createTwoLayerCuboid(Dune::FieldVector< Scalar, dim > const &x0, Dune::FieldVector< Scalar, dim > const &dx1, Dune::FieldVector< Scalar, dim > const &dx2, Dune::FieldVector< Scalar, dim > const &dh1, Scalar dh2, bool symmetric=true)
std::unique_ptr< Grid > createRectangle(Dune::FieldVector< Scalar, 2 > const &x0, Dune::FieldVector< Scalar, 2 > const &dx, Dh dh, bool symmetric=true, Deformation phi=Deformation())
Cartesian structured triangulation of a rectangular domain.
std::unique_ptr< Grid > createTShape(Dune::FieldVector< Scalar, dim > const &x0, Scalar dx, Scalar dy, Dune::FieldVector< Scalar, dim > const &thickness, Scalar dh, bool symmetric=true)
Fill L-shaped domain with cubes.
std::unique_ptr< Grid > createIcosahedron()
Creates an icosahedron consisting of 20 tetrahedra.
std::unique_ptr< Grid > createOctahedron(Dune::FieldVector< typename Grid::ctype, 3 > extent=Dune::FieldVector< typename Grid::ctype, 3 >(1.0))
Creates an octahedron consisting of eight tetrahedra.
std::unique_ptr< Grid > createHexahedron()
Creates a hexahedron consisting of five tetrahedra.
auto constexpr identityDeformation
auto unionGrid(std::vector< Dune::FieldVector< ctype, dim > > vs, std::vector< std::array< size_t, dim+1 > > cs, GridView const &gv, GridViews const &...gvs)
decltype(identityDeformation) IdentityDeformation