Commit 4423c539 authored by Praetorius, Simon's avatar Praetorius, Simon

gridfactory cleaned up

parent 670041db
......@@ -9,8 +9,11 @@
*/
#include <memory>
#include <type_traits>
#include <vector>
#include <dune/common/hybridutilities.hh>
#include <dune/common/std/type_traits.hh>
#include <dune/grid/common/gridfactory.hh>
#include "multimesh.hh"
......@@ -41,86 +44,111 @@ namespace Dune
using GlobalCoordinate = FieldVector<ctype, dimensionworld>;
using BoundarySegment = Dune::BoundarySegment<dimension, dimensionworld>;
using ElementParametrization = VirtualFunction<LocalCoordinate,GlobalCoordinate>;
using ElementParametrization = VirtualFunction<LocalCoordinate, GlobalCoordinate>;
public:
/// \brief Constructor
/**
* \param[in] n The number of grids to construct
* \param[in] args Additional parameters passed to the constructor of the hostgridfactory
* \param[in] args Additional parameters passed to the constructor of the
* hostgridfactory
**/
template <class... Args>
explicit GridFactory (std::size_t n, Args&&... args)
: gridFactories_(n, GridFactory<HostGrid>{std::forward<Args>(args)...})
{}
{
gridFactories_.reserve(n);
for (std::size_t i = 0; i < n; ++i)
gridFactories_.emplace_back(std::forward<Args>(args)...);
}
// initialize at least 1 grid
GridFactory ()
: GridFactory{1}
{}
/// \brief insert a vertex into the macro grid
/// \brief Insert a vertex into the macro grid
/**
* \param[in] pos position of the vertex (in world coordinates)
* \param[in] pos Position of the vertex (in world coordinates)
**/
virtual void insertVertex (const GlobalCoordinate& pos)
virtual void insertVertex (const GlobalCoordinate& pos) override
{
for (auto& gridFactory : gridFactories_)
gridFactory.insertVertex(pos);
}
/// \brief insert an element into the coarse grid
/// \brief Insert an element into the coarse grid
/**
* \param[in] type GeometryType of the new element
* \param[in] vertices indices of the element vertices
* \param[in] vertices Indices of the element vertices
**/
virtual void insertElement (const GeometryType& type,
const std::vector<unsigned int>& vertices)
const std::vector<unsigned int>& vertices) override
{
for (auto& gridFactory : gridFactories_)
gridFactory.insertElement(type, vertices);
}
template <class GF>
using HasInsertElement = decltype( std::declval<GF>().insertElement(
std::declval<GeometryType>(),
std::declval<std::vector<unsigned int>>(),
std::declval<std::shared_ptr<ElementParametrization>>()) );
/// \brief Insert a parametrized element into the coarse grid
/**
* \param[in] type The GeometryType of the new element
* \param[in] vertices The vertices of the new element, using the DUNE numbering
* \param[in] elementParametrization A function prescribing the shape of this element
* \param[in] type The GeometryType of the new element
* \param[in] vertices The vertices of the new element
* \param[in] param A function prescribing the shape of this element
**/
// virtual void insertElement (const GeometryType& type,
// const std::vector<unsigned int>& vertices,
// const std::shared_ptr<ElementParametrization>& elementParametrization)
// {
// for (auto& gridFactory : gridFactories_)
// gridFactory.insertElement(type, vertices, elementParametrization);
// }
/// \brief insert a boundary segment into the macro grid
virtual void insertElement (
const GeometryType& type,
const std::vector<unsigned int>& vertices,
const std::shared_ptr<ElementParametrization>& param) override
{
Hybrid::ifElse(Std::is_detected<HasInsertElement, GridFactory<HostGrid>>{},
[&](auto id) {
for (auto& gridFactory : gridFactories_)
id(gridFactory).insertElement(type, vertices, param);
});
}
/// \brief Insert a boundary segment into the macro grid
/**
* Only influences the ordering of the boundary segments
* \param[in] vertices vertex indices of boundary face
* \param[in] vertices Vertex indices of boundary face
**/
virtual void insertBoundarySegment (const std::vector<unsigned int>& vertices) override
virtual void insertBoundarySegment (
const std::vector<unsigned int>& vertices) override
{
for (auto& gridFactory : gridFactories_)
gridFactory.insertBoundarySegment(vertices);
}
/** \brief insert a shaped boundary segment into the macro grid
template <class GF>
using HasInsertBoundarySegment = decltype( std::declval<GF>().insertBoundarySegment(
std::declval<std::vector<unsigned int>>(),
std::declval<std::shared_ptr<BoundarySegment>>()) );
/** \brief Insert a shaped boundary segment into the macro grid
*
* \param[in] vertices vertex indices of boundary face
* \param[in] boundarySegment geometric realization of shaped boundary
* \param[in] vertices Vertex indices of boundary face
* \param[in] boundarySegment Geometric realization of shaped boundary
*/
virtual void insertBoundarySegment (const std::vector<unsigned int>& vertices,
const std::shared_ptr<BoundarySegment>& boundarySegment) override
virtual void insertBoundarySegment (
const std::vector<unsigned int>& vertices,
const std::shared_ptr<BoundarySegment>& boundarySegment) override
{
for (auto& gridFactory : gridFactories_)
gridFactory.insertBoundarySegment(vertices, boundarySegment);
Hybrid::ifElse(Std::is_detected<HasInsertBoundarySegment, GridFactory<HostGrid>>{},
[&](auto id) {
for (auto& gridFactory : gridFactories_)
id(gridFactory).insertBoundarySegment(vertices, boundarySegment);
});
}
/// \brief finalize grid creation and hand over the grid
/// \brief Finalize grid creation and hand over the grid
/**
* Create n copies of the grid and store it in unique_pointers
* inside the multimesh grid.
......
......@@ -40,8 +40,8 @@ namespace Dune
template <PartitionIteratorType pitype, class GridImp>
class MultiMeshIterator<0, pitype, GridImp>
: public ForwardIteratorFacade<MultiMeshIterator<0,pitype,GridImp>,
std::vector<typename GridImp::Traits::template Codim<0>::Entity>,
std::vector<typename GridImp::Traits::template Codim<0>::Entity> >
std::vector<typename GridImp::Traits::template Codim<0>::Entity>,
std::vector<typename GridImp::Traits::template Codim<0>::Entity> >
{
private:
// LevelIterator to the equivalent entity in the host grid
......@@ -53,7 +53,7 @@ namespace Dune
struct EntityStackEntry
{
template <class Entity>
explicit EntityStackEntry(Entity&& entity)
explicit EntityStackEntry (Entity&& entity)
: it(entity.hbegin(entity.level()+1))
, end(entity.hend(entity.level()+1))
{}
......@@ -68,21 +68,18 @@ namespace Dune
using Super = std::stack<EntityStackEntry, std::vector<EntityStackEntry>>;
public:
explicit EntityStack(const GridImp* multiMesh)
explicit EntityStack (const GridImp* multiMesh)
{
c.reserve(multiMesh->maxLevel());
}
// return true of all levels >= l are finished, i.e. hierarchic iterators it == end
bool finished(std::size_t l = 0) const
// return true if all levels >= l are finished, i.e. hierarchic iterators it == end
bool finished (std::size_t l = 0) const
{
bool f = true;
while (f && l < c.size()) {
auto tmp = c[l].it;
++tmp;
f = f && tmp == c[l].end;
++l;
}
for (auto tmp = c[l].it; f && l < c.size(); ++l,
tmp = c[l].it)
f = f && (++tmp) == c[l].end;
return f;
}
......@@ -91,7 +88,6 @@ namespace Dune
};
public:
/// Constructor. Stores a pointer to the grid
explicit MultiMeshIterator (const GridImp* multiMesh, int level)
: multiMesh_(multiMesh)
......@@ -124,10 +120,12 @@ namespace Dune
}
}
/// Constructor which create the leaf-iterator
MultiMeshIterator (const GridImp* multiMesh)
: MultiMeshIterator{multiMesh, -1}
{}
/// Constructor which create the end leaf-iterator
MultiMeshIterator (const GridImp* multiMesh, bool endDummy)
: MultiMeshIterator{multiMesh, -1, endDummy}
{}
......@@ -160,11 +158,8 @@ namespace Dune
return macroIterators_ == that.macroIterators_;
}
protected:
// got to next entity in grid i
// iterator always points to a leaf entity
void increment (std::size_t i)
{
auto& entityStack = entityStacks_[i];
......@@ -191,9 +186,8 @@ namespace Dune
// 3. go down in tree until leaf entity
for (auto child = dereference(i); !entityTest(child);
child = dereference(i)) {
child = dereference(i))
entityStack.emplace(child);
}
}
/// Return true, if all stacks with size > stack[i].size are finished
......@@ -219,9 +213,8 @@ namespace Dune
// 2. go down in tree until leaf entity
for (auto child = dereference(i); !entityTest(child);
child = dereference(i)) {
child = dereference(i))
entityStack.emplace(child);
}
}
HostEntity dereference (std::size_t i) const
......@@ -232,13 +225,12 @@ namespace Dune
return *entityStacks_[i].top().it;
}
bool entityTest(HostEntity const& entity)
bool entityTest (HostEntity const& entity)
{
return entity.level() == level_ || entity.isLeaf() || !entity.isRegular();
}
private:
const GridImp* multiMesh_;
std::vector<HostGridLevelIterator> macroIterators_;
......
add_executable("testiterator" testiterator.cc)
target_link_dune_default_libraries("testiterator")
add_dune_alberta_flags(GRIDDIM 3 WORLDDIM 3 "testiterator")
add_executable("uggrid" uggrid.cc)
target_link_dune_default_libraries("uggrid")
DIM: 2
DIM_OF_WORLD: 2
number of elements: 4
number of vertices: 5
element vertices:
0 1 4
1 2 4
2 3 4
3 0 4
element boundaries:
0 0 1
0 0 2
0 0 3
0 0 4
vertex coordinates:
0.0 0.0
1.0 0.0
1.0 1.0
0.0 1.0
0.5 0.5
element neighbours:
1 3 -1
2 0 -1
3 1 -1
0 2 -1
DIM: 3
DIM_OF_WORLD: 3
number of vertices: 8
number of elements: 6
vertex coordinates:
0.0 0.0 0.0
1.0 0.0 0.0
0.0 0.0 1.0
1.0 0.0 1.0
1.0 1.0 0.0
1.0 1.0 1.0
0.0 1.0 0.0
0.0 1.0 1.0
element vertices:
0 5 4 1
0 5 3 1
0 5 3 2
0 5 4 6
0 5 7 6
0 5 7 2
element boundaries:
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
1 1 0 0
element neighbours:
-1 -1 1 3
-1 -1 0 2
-1 -1 5 1
-1 -1 4 0
-1 -1 3 5
-1 -1 2 4
......@@ -9,13 +9,14 @@
#include <dune/common/exceptions.hh> // We use exceptions
#include <dune/common/timer.hh>
#include <dune/grid/albertagrid.hh>
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/uggrid.hh>
#include <dune/alugrid/grid.hh>
#include <dune/multimesh/multimesh.hh>
#include <dune/multimesh/mmgridfactory.hh>
#include <dune/multimesh/utility/structuredgridfactory.hh>
#include <dune/multimesh/utility/structuredgridbuilder.hh>
#define GRID 1
......@@ -49,33 +50,35 @@ int main(int argc, char** argv)
MPIHelper::instance(argc, argv);
#if GRID == 1
FieldVector<double,3> lower_left = {-1.5, -1.5, -1.5};
FieldVector<double,3> bbox = {1.5, 1.5, 1.5};
std::array<int,3> num_elements = {16, 16, 16};
using HostGrid = YaspGrid<3, EquidistantOffsetCoordinates<double,3>>;
MultiMesh<HostGrid> grid(2, lower_left, bbox, num_elements);
#elif GRID == 2
FieldVector<double,2> lower_left = {-1.5, -1.5};
FieldVector<double,2> bbox = {1.5, 1.5};
std::array<unsigned int,2> num_elements = {2, 2};
using HostGrid = Dune::ALUGrid<2, 2, Dune::simplex, Dune::conforming>;
using Factory = StructuredGridFactoryWrapper<MultiMesh<HostGrid> >;
using Factory = StructuredGridBuilder<MultiMesh<HostGrid> >;
GridFactory<MultiMesh<HostGrid> > gridFactory(2);
auto gridPtr = Factory::createSimplexGrid(gridFactory, lower_left, bbox, num_elements);
auto& grid = *gridPtr;
#elif GRID == 3
FieldVector<double,2> lower_left = {-1.5, -1.5};
FieldVector<double,2> bbox = {1.5, 1.5};
std::array<unsigned int,2> num_elements = {2, 2};
using HostGrid = Dune::UGGrid<2>;
using Factory = StructuredGridFactoryWrapper<MultiMesh<HostGrid> >;
GridFactory<MultiMesh<HostGrid> > gridFactory(2);
assert(argc > 1);
std::string filename = argv[1];
using HostGrid = Dune::AlbertaGrid<3,3>;
MultiMesh<HostGrid> grid(2, filename);
grid.grid(0).globalRefine(8);
grid.grid(1).globalRefine(8);
auto gridPtr = Factory::createSimplexGrid(gridFactory, lower_left, bbox, num_elements);
auto& grid = *gridPtr;
#endif
std::cout << "number of grids = " << grid.numGrids() << "\n";
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <iostream>
#include <dune/common/parallel/mpihelper.hh> // An initializer of MPI
#include <dune/common/exceptions.hh> // We use exceptions
#include <dune/common/timer.hh>
#include <dune/grid/uggrid.hh>
#include <dune/multimesh/multimesh.hh>
#include <dune/multimesh/mmgridfactory.hh>
#include <dune/multimesh/utility/structuredgridbuilder.hh>
using namespace Dune;
int main(int argc, char** argv)
{
MPIHelper::instance(argc, argv);
FieldVector<double,2> lower_left = {-1.5, -1.5};
FieldVector<double,2> bbox = {1.5, 1.5};
std::array<unsigned int,2> num_elements = {2, 2};
using HostGrid = Dune::UGGrid<2>;
using Factory = StructuredGridBuilder<MultiMesh<HostGrid> >;
GridFactory<MultiMesh<HostGrid> > factory(1);
auto gridPtr = Factory::createSimplexGrid(factory, lower_left, bbox, num_elements);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment