3 #ifndef DUNE_STRUCTURED_GRID_FACTORY_HH
4 #define DUNE_STRUCTURED_GRID_FACTORY_HH
14 #include <dune/common/array.hh>
15 #include <dune/common/classname.hh>
16 #include <dune/common/exceptions.hh>
17 #include <dune/common/fvector.hh>
18 #include <dune/common/parallel/mpihelper.hh>
19 #include <dune/common/shared_ptr.hh>
29 template <
class Gr
idType>
32 typedef typename GridType::ctype ctype;
34 static const int dim = GridType::dimension;
36 static const int dimworld = GridType::dimensionworld;
40 const FieldVector<ctype,dimworld>& lowerLeft,
41 const FieldVector<ctype,dimworld>& upperRight,
42 const array<unsigned int,dim>& vertices)
47 int numVertices = index.
cycle();
50 for (
int i=0; i<numVertices; i++, ++index) {
53 FieldVector<double,dimworld> pos(0);
54 for (
int j=0; j<dim; j++)
55 pos[j] = lowerLeft[j] + index[j] * (upperRight[j]-lowerLeft[j])/(vertices[j]-1);
56 for (
int j=dim; j<dimworld; j++)
57 pos[j] = lowerLeft[j];
67 static array<unsigned int, dim> computeUnitOffsets(
const array<unsigned int,dim>& vertices)
69 array<unsigned int, dim> unitOffsets;
73 for (
int i=1; i<dim; i++)
74 unitOffsets[i] = unitOffsets[i-1] * vertices[i-1];
90 static shared_ptr<GridType>
createCubeGrid(
const FieldVector<ctype,dimworld>& lowerLeft,
91 const FieldVector<ctype,dimworld>& upperRight,
92 const array<unsigned int,dim>& elements)
97 if (MPIHelper::getCollectiveCommunication().rank() == 0)
100 array<unsigned int,dim> vertices = elements;
101 for(
size_t i = 0; i < vertices.size(); ++i )
105 insertVertices(factory, lowerLeft, upperRight, vertices);
109 array<unsigned int, dim> unitOffsets =
110 computeUnitOffsets(vertices);
114 unsigned int nCorners = 1<<dim;
116 std::vector<unsigned int> cornersTemplate(nCorners,0);
118 for (
size_t i=0; i<nCorners; i++)
119 for (
int j=0; j<dim; j++)
121 cornersTemplate[i] += unitOffsets[j];
127 int numElements = index.
cycle();
129 for (
int i=0; i<numElements; i++, ++index) {
132 unsigned int base = 0;
133 for (
int j=0; j<dim; j++)
134 base += index[j] * unitOffsets[j];
137 std::vector<unsigned int> corners = cornersTemplate;
138 for (
size_t j=0; j<corners.size(); j++)
149 return shared_ptr<GridType>(factory.
createGrid());
167 const FieldVector<ctype,dimworld>& upperRight,
168 const array<unsigned int,dim>& elements)
173 if(MPIHelper::getCollectiveCommunication().rank() == 0)
176 array<unsigned int,dim> vertices = elements;
177 for (std::size_t i=0; i<vertices.size(); i++)
180 insertVertices(factory, lowerLeft, upperRight, vertices);
184 array<unsigned int, dim> unitOffsets =
185 computeUnitOffsets(vertices);
188 std::vector<unsigned int> corners(dim+1);
193 size_t cycle = elementsIndex.
cycle();
195 for (
size_t i=0; i<cycle; ++elementsIndex, i++) {
198 unsigned int base = 0;
199 for (
int j=0; j<dim; j++)
200 base += elementsIndex[j] * unitOffsets[j];
203 std::vector<unsigned int> permutation(dim);
204 for (
int j=0; j<dim; j++)
210 std::vector<unsigned int> corners(dim+1);
213 for (
int j=0; j<dim; j++)
215 corners[j] + unitOffsets[permutation[j]];
221 }
while (std::next_permutation(permutation.begin(),
229 return shared_ptr<GridType>(factory.
createGrid());
243 template<
class ctype,
int dim>
246 static const int dimworld = GridType::dimensionworld;
259 static shared_ptr<GridType>
261 const FieldVector<ctype,dimworld>& upperRight,
262 const array<unsigned int,dim>& elements)
264 for(
int d = 0; d < dimworld; ++d)
266 DUNE_THROW(
GridError, className<StructuredGridFactory>()
267 <<
"::createCubeGrid(): You have to use Yaspgrid<dim"
268 ", EquidistantOffsetCoordinates<ctype,dim> > as your"
269 "grid type for non-trivial origin." );
272 array<int, dim> elem;
273 std::copy(elements.begin(), elements.end(), elem.begin());
275 return shared_ptr<GridType>
276 (
new GridType(upperRight, elem,
277 std::bitset<dim>(), 0));
285 static shared_ptr<GridType>
287 const FieldVector<ctype,dimworld>& upperRight,
288 const array<unsigned int,dim>& elements)
290 DUNE_THROW(
GridError, className<StructuredGridFactory>()
291 <<
"::createSimplexGrid(): Simplices are not supported "
306 template<
class ctype,
int dim>
309 static const int dimworld = GridType::dimensionworld;
318 static shared_ptr<GridType>
320 const FieldVector<ctype,dimworld>& upperRight,
321 const array<unsigned int,dim>& elements)
324 array<int, dim> elem;
325 std::copy(elements.begin(), elements.end(), elem.begin());
327 return shared_ptr<GridType>
328 (
new GridType(lowerLeft, upperRight, elem,
329 std::bitset<dim>(), 0));
337 static shared_ptr<GridType>
339 const FieldVector<ctype,dimworld>& upperRight,
340 const array<unsigned int,dim>& elements)
342 DUNE_THROW(
GridError, className<StructuredGridFactory>()
343 <<
"::createSimplexGrid(): Simplices are not supported "
Definition: alugrid/common/declaration.hh:18
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:263
[ provides Dune::Grid ]
Definition: yaspgrid.hh:56
virtual GridType * createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:316
void abs(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:326
static shared_ptr< GridType > createSimplexGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured simplex grid.
Definition: structuredgridfactory.hh:166
static shared_ptr< GridType > createSimplexGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured simplex grid.
Definition: structuredgridfactory.hh:286
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: common/gridfactory.hh:279
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:16
static shared_ptr< GridType > createCubeGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured cube grid.
Definition: structuredgridfactory.hh:90
static shared_ptr< GridType > createSimplexGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured simplex grid.
Definition: structuredgridfactory.hh:338
Provide a generic factory class for unstructured grids.
size_t cycle() const
Compute how many times you can call operator++ before getting to (0,...,0) again. ...
Definition: multiindex.hh:46
Definition: alugrid/common/declaration.hh:18
static shared_ptr< GridType > createCubeGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured cube grid.
Definition: structuredgridfactory.hh:319
Definition: multiindex.hh:16
Container for equidistant coordinates in a YaspGrid with non-trivial origin.
Definition: coordinates.hh:124
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: common/gridfactory.hh:290
Implements a multiindex with arbitrary dimension and fixed index ranges This is used by various facto...
Construct structured cube and simplex grids in unstructured grid managers.
Definition: structuredgridfactory.hh:30
Include standard header files.
Definition: agrid.hh:59
static shared_ptr< GridType > createCubeGrid(const FieldVector< ctype, dimworld > &lowerLeft, const FieldVector< ctype, dimworld > &upperRight, const array< unsigned int, dim > &elements)
Create a structured cube grid.
Definition: structuredgridfactory.hh:260
Container for equidistant coordinates in a YaspGrid.
Definition: coordinates.hh:26
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178