Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
gridconstruction.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set ts=4 sw=2 et sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_UTILITIES_GRIDCONSTRUCTION_HH
8#define DUNE_FUFEM_UTILITIES_GRIDCONSTRUCTION_HH
9
10#include <memory>
11
14#include <dune/grid/common/gridfactory.hh>
15
16#include <dune/geometry/type.hh>
17
21
22
23namespace Dune {
24
31template <class GridType, int dim>
33
34template <class GridType>
35struct GridConstruction<GridType,1> {
36
37 const static int dim=GridType::dimension;
38 using ctype = typename GridType::ctype;
40
42 auto type = config.get<std::string>("type");
43 if (type == "interval")
44 return createInterval(config);
45 else
46 DUNE_THROW(Exception,"There is no factory yet for dimension and type "+type);
47 }
48
50
51 FV lower = config.get<FV>("lowerCorner");
52 FV upper = config.get<FV>("upperCorner");
53
54 std::array<unsigned,dim> nElements = config.get<std::array<unsigned, dim> >("elements");
55 FV size;
56 for (int i=0; i<dim; i++)
57 size[i] = (upper[i]-lower[i])/((ctype) nElements[i]);
58
60
61 // insert vertices
62 for(ctype x=lower[0]; x<=upper[0]; x+=size[0])
63 factory.insertVertex(FV({x}));
64
65 using E = std::vector<unsigned>;
66
67 for (unsigned i=0;i<nElements[0]; i++) {
68 factory.insertElement(Dune::GeometryTypes::line, E({i, i+1}));
69 }
70
71 return std::unique_ptr<GridType>(factory.createGrid());
72 }
73};
74
76template <class GridType>
77struct GridConstruction<GridType,2> {
78
79 const static int dim=GridType::dimension;
80 using ctype = typename GridType::ctype;
82
83public:
84
90
91 auto type = config.get<std::string>("type");
92 if (type == "rectangle")
93 return createRectangle(config);
94 else if (type == "circle")
95 return createCircle(config);
96 else
97 DUNE_THROW(Exception,"There is no factory yet for dimension and type "+type);
98 }
99
102
103 FV lower = config.get<FV>("lowerCorner");
104 FV upper = config.get<FV>("upperCorner");
105 std::array<unsigned,dim> nElements = config.get<std::array<unsigned, dim> >("elements");
106 FV size;
107 for (int i=0; i<dim; i++)
108 size[i] = (upper[i]-lower[i])/((ctype) nElements[i]);
109
111
112 // insert vertices
113 for(unsigned i=0; i<=nElements[0]; i++)
114 for(unsigned j=0; j<=nElements[1]; j++)
115 factory.insertVertex(FV({lower[0]+i*size[0], lower[1]+j*size[1]}));
116
117 Dune::GeometryType gt = Dune::GeometryTypes::cube(dim);
118 bool tetra = config.get<bool>("tetrahedral");
119 if (tetra)
120 gt = Dune::GeometryTypes::simplex(dim);
121
122 using E = std::vector<unsigned>;
123
124 // nodes in each slice
125 unsigned nY = nElements[1]+1;
126
127 for (unsigned i=0;i<nElements[0]; i++) {
128 for (unsigned j=0;j<nElements[1]; j++) {
129
130 if (tetra) {
131 // add two triangles
132 factory.insertElement(gt,E({i*nY+j, (i+1)*nY + j, i*nY + j+1}));
133 factory.insertElement(gt,E({(i+1)*nY+j+1, i*nY + j+1, (i+1)*nY + j}));
134 } else // add hexahedra
135 factory.insertElement(gt,E({i*nY + j, (i+1)*nY + j,
136 i*nY + j+1, (i+1)*nY + j+1}));
137 }
138 }
139
140 return std::unique_ptr<GridType>(factory.createGrid());
141 }
142
145 return ::createCircle<GridType>(config.get<FV>("center"), config.get<ctype>("radius"));
146 }
147};
148
150template <class GridType>
151struct GridConstruction<GridType,3> {
152
153 const static int dim=GridType::dimension;
154 using ctype = typename GridType::ctype;
156
157public:
158
164
165 auto type = config.get<std::string>("type");
166 if (type == "tubeSegment")
167 return createTubeSegment(config);
168 else if (type == "sphere")
169 return createSphere(config);
170 else if (type == "cuboid")
171 return createCuboid(config);
172 else
173 DUNE_THROW(Exception,"There is no factory for dimension and type "+type);
174 }
175
177
178 return makeTubeSegment3D<GridType>(config.get<FV>("center"), config.get<ctype>("thickness"),
179 config.get<ctype>("length"), config.get<ctype>("innerRadius"),
180 config.get<ctype>("fromAngle"), config.get<ctype>("toAngle"),
181 config.get<unsigned>("nElementRing"), config.get<unsigned>("nElementLength"),
182 config.get<bool>("closeTube"), config.get<size_t>("axis"),
183 config.get<bool>("tetrahedra"), config.get<bool>("parameterizedBoundary"));
184 }
185
191 return makeSphereOnOctahedron<GridType>(config.get<FV>("center"),
192 config.get<ctype>("radius"));
193 }
194
200
201 FV lower = config.get<FV>("lowerCorner");
202 FV upper = config.get<FV>("upperCorner");
203 std::array<unsigned,dim> nElements = config.get<std::array<unsigned, dim> >("elements");
204 FV size;
205 for (int i=0; i<dim; i++)
206 size[i] = (upper[i]-lower[i])/((ctype) nElements[i]);
207
209
210 // insert vertices
211 for(ctype x=lower[0]; x<=upper[0]; x+=size[0])
212 for(ctype y=lower[1]; y<=upper[1]; y+=size[1])
213 for(ctype z=lower[2]; z<=upper[2]; z+=size[2])
214 factory.insertVertex(FV({x, y, z}));
215
217 bool tetra = config.get<bool>("tetrahedral");
218 if (tetra)
219 gt = Dune::GeometryTypes::tetrahedron;
220 else
221 gt = Dune::GeometryTypes::cube(dim);
222
223 using E = std::vector<unsigned>;
224
225 // nodes in each z-y slice
226 unsigned nZY = (nElements[1]+1)*(nElements[2]+1);
227
228 for (unsigned i=0;i<nElements[0]; i++) {
229 for (unsigned j=0;j<nElements[1]; j++) {
230 for (unsigned k=0;k<nElements[2]; k++) {
231
232 // collect indices of the hexahedral
233 E hexa = {i*nZY + j*(nElements[2] + 1) + k, (i+1)*nZY + j*(nElements[2]+1) + k,
234 i*nZY + (j+1)*(nElements[2]+1) + k, (i+1)*nZY + (j+1)*(nElements[2]+1) + k,
235 i*nZY + j*(nElements[2]+1) + k + 1, (i+1)*nZY + j*(nElements[2]+1) + k + 1,
236 i*nZY + (j+1)*(nElements[2]+1) + k + 1, (i+1)*nZY + (j+1)*(nElements[2]+1) + k + 1};
237
238 if (tetra) {
239 // subdivide hexahedral into tetrahedra
240 E tet(4);
241
242 tet={hexa[0],hexa[1],hexa[3],hexa[7]};
243 factory.insertElement(gt,tet);
244 tet={hexa[0],hexa[5],hexa[1],hexa[7]};
245 factory.insertElement(gt,tet);
246 tet={hexa[0],hexa[4],hexa[5],hexa[7]};
247 factory.insertElement(gt,tet);
248 tet={hexa[0],hexa[4],hexa[6],hexa[7]};
249 factory.insertElement(gt,tet);
250 tet={hexa[0],hexa[6],hexa[2],hexa[7]};
251 factory.insertElement(gt,tet);
252 tet={hexa[0],hexa[3],hexa[2],hexa[7]};
253 factory.insertElement(gt,tet);
254 } else // add hexahedra
255 factory.insertElement(gt,hexa);
256 }
257 }
258 }
259 return std::unique_ptr<GridType>(factory.createGrid());
260 }
261};
262
263} /* namespace Dune */
264
265#endif
int size() const
size_type dim() const
#define DUNE_THROW(E,...)
std::unique_ptr< GridType > createCircle(const Dune::FieldVector< double, 2 > &center, double r)
Definition makehalfcircle.hh:19
std::string get(const std::string &key, const std::string &defaultValue) const
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
virtual std::unique_ptr< GridType > createGrid()
A factory class that can create several often used grids like cuboids, spheres and tubes from a param...
Definition gridconstruction.hh:32
typename GridType::ctype ctype
Definition gridconstruction.hh:38
static std::unique_ptr< GridType > createGrid(const ParameterTree &config)
Definition gridconstruction.hh:41
static std::unique_ptr< GridType > createInterval(const ParameterTree &config)
Definition gridconstruction.hh:49
static std::unique_ptr< GridType > createGrid(const ParameterTree &config)
Create various grids.
Definition gridconstruction.hh:89
typename GridType::ctype ctype
Definition gridconstruction.hh:80
static std::unique_ptr< GridType > createCircle(const ParameterTree &config)
Create a circular grid.
Definition gridconstruction.hh:144
static std::unique_ptr< GridType > createRectangle(const ParameterTree &config)
Create a rectangular grid.
Definition gridconstruction.hh:101
static std::unique_ptr< GridType > createTubeSegment(const ParameterTree &config)
Definition gridconstruction.hh:176
static std::unique_ptr< GridType > createGrid(const ParameterTree &config)
Create various grids.
Definition gridconstruction.hh:163
typename GridType::ctype ctype
Definition gridconstruction.hh:154
static std::unique_ptr< GridType > createCuboid(const ParameterTree &config)
Create a rectangular grid.
Definition gridconstruction.hh:199
static std::unique_ptr< GridType > createSphere(const ParameterTree &config)
Create a sphere grid.
Definition gridconstruction.hh:190
T forward(T... args)