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;
41 :
public array<unsigned int,dim>
45 array<unsigned int,dim> limits_;
49 MultiIndex(
const array<unsigned int,dim>& limits)
52 std::fill(this->begin(), this->end(), 0);
56 MultiIndex& operator++() {
58 for (
int i=0; i<dim; i++) {
64 if ((*
this)[i]<limits_[i])
74 size_t cycle()
const {
76 for (
int i=0; i<dim; i++)
85 const FieldVector<ctype,dimworld>& lowerLeft,
86 const FieldVector<ctype,dimworld>& upperRight,
87 const array<unsigned int,dim>& vertices)
90 MultiIndex index(vertices);
93 int numVertices = index.cycle();
96 for (
int i=0; i<numVertices; i++, ++index) {
99 FieldVector<double,dimworld> pos(0);
100 for (
int j=0; j<dim; j++)
101 pos[j] = lowerLeft[j] + index[j] * (upperRight[j]-lowerLeft[j])/(vertices[j]-1);
102 for (
int j=dim; j<dimworld; j++)
103 pos[j] = lowerLeft[j];
113 static array<unsigned int, dim> computeUnitOffsets(
const array<unsigned int,dim>& vertices)
115 array<unsigned int, dim> unitOffsets;
119 for (
int i=1; i<dim; i++)
120 unitOffsets[i] = unitOffsets[i-1] * vertices[i-1];
136 static shared_ptr<GridType>
createCubeGrid(
const FieldVector<ctype,dimworld>& lowerLeft,
137 const FieldVector<ctype,dimworld>& upperRight,
138 const array<unsigned int,dim>& elements)
143 if (MPIHelper::getCollectiveCommunication().rank() == 0)
146 array<unsigned int,dim> vertices = elements;
147 for(
size_t i = 0; i < vertices.size(); ++i )
151 insertVertices(factory, lowerLeft, upperRight, vertices);
155 array<unsigned int, dim> unitOffsets =
156 computeUnitOffsets(vertices);
160 unsigned int nCorners = 1<<dim;
162 std::vector<unsigned int> cornersTemplate(nCorners,0);
164 for (
size_t i=0; i<nCorners; i++)
165 for (
int j=0; j<dim; j++)
167 cornersTemplate[i] += unitOffsets[j];
170 MultiIndex index(elements);
173 int numElements = index.cycle();
175 for (
int i=0; i<numElements; i++, ++index) {
178 unsigned int base = 0;
179 for (
int j=0; j<dim; j++)
180 base += index[j] * unitOffsets[j];
183 std::vector<unsigned int> corners = cornersTemplate;
184 for (
size_t j=0; j<corners.size(); j++)
195 return shared_ptr<GridType>(factory.
createGrid());
213 const FieldVector<ctype,dimworld>& upperRight,
214 const array<unsigned int,dim>& elements)
219 if(MPIHelper::getCollectiveCommunication().rank() == 0)
222 array<unsigned int,dim> vertices = elements;
223 for (std::size_t i=0; i<vertices.size(); i++)
226 insertVertices(factory, lowerLeft, upperRight, vertices);
230 array<unsigned int, dim> unitOffsets =
231 computeUnitOffsets(vertices);
234 std::vector<unsigned int> corners(dim+1);
238 MultiIndex elementsIndex(elements);
239 size_t cycle = elementsIndex.cycle();
241 for (
size_t i=0; i<cycle; ++elementsIndex, i++) {
244 unsigned int base = 0;
245 for (
int j=0; j<dim; j++)
246 base += elementsIndex[j] * unitOffsets[j];
249 std::vector<unsigned int> permutation(dim);
250 for (
int j=0; j<dim; j++)
256 std::vector<unsigned int> corners(dim+1);
259 for (
int j=0; j<dim; j++)
261 corners[j] + unitOffsets[permutation[j]];
267 }
while (std::next_permutation(permutation.begin(),
275 return shared_ptr<GridType>(factory.
createGrid());
293 static const int dimworld = GridType::dimensionworld;
305 static shared_ptr<GridType>
307 const FieldVector<ctype,dimworld>& upperRight,
308 const array<unsigned int,dim>& elements)
310 for(
int d = 0; d < dimworld; ++d)
312 DUNE_THROW(
GridError, className<StructuredGridFactory>()
313 <<
"::createCubeGrid(): The lower coordinates "
314 "must be at the origin for YaspGrid.");
316 Dune::array<int, dim> elements_;
317 std::copy(elements.begin(), elements.end(), elements_.begin());
319 return shared_ptr<GridType>
320 (
new GridType(upperRight, elements_,
321 std::bitset<dim>(), 0));
329 static shared_ptr<GridType>
331 const FieldVector<ctype,dimworld>& upperRight,
332 const array<unsigned int,dim>& elements)
334 DUNE_THROW(
GridError, className<StructuredGridFactory>()
335 <<
"::createSimplexGrid(): Simplices are not supported "
351 static const int dimworld = GridType::dimensionworld;
360 static shared_ptr<GridType>
362 const FieldVector<ctype,dimworld>& upperRight,
363 const array<unsigned int,dim>& elements)
365 FieldVector<int, dim> elements_;
366 std::copy(elements.begin(), elements.end(), elements_.begin());
368 return shared_ptr<GridType>
369 (
new GridType(elements_, lowerLeft, upperRight));
381 static shared_ptr<GridType>
383 const FieldVector<ctype,dimworld>& upperRight,
384 const array<unsigned int,dim>& elements)
386 DUNE_THROW(
GridError, className<StructuredGridFactory>()
387 <<
"::createSimplexGrid(): Simplices are not supported "
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:306
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:361
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:212
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
[ provides Dune::Grid ]
Definition: yaspgrid.hh:62
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: common/gridfactory.hh:290
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:263
Definition: alugrid/common/declaration.hh:18
Provide a generic factory class for unstructured grids.
[ provides Dune::Grid ]
Definition: sgrid.hh:48
virtual GridType * createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:316
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:16
Construct structured cube and simplex grids in unstructured grid managers.
Definition: structuredgridfactory.hh:30
_ctype ctype
define type used for coordinates in grid module
Definition: sgrid.hh:1284
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: common/gridfactory.hh:279
Definition: alugrid/common/declaration.hh:18
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:330
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:382
yaspgrid_ctype ctype
Type used for coordinates.
Definition: yaspgrid.hh:196
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:136
void abs(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:332