1#ifndef DUNE_ALUGRID_FROMTOGRIDFACTORY_HH
2#define DUNE_ALUGRID_FROMTOGRIDFACTORY_HH
10#include <dune/grid/common/exceptions.hh>
12#include <dune/alugrid/common/alugrid_assert.hh>
13#include <dune/alugrid/common/declaration.hh>
21 template<
class ToGr
id >
22 class FromToGridFactory;
28 template<
int dim,
int dimworld, ALUGr
idElementType eltype, ALUGr
idRefinementType refineType,
class Comm >
29 class FromToGridFactory< ALUGrid< dim, dimworld, eltype, refineType, Comm > >
33 typedef ALUGrid< dim, dimworld, eltype, refineType, Comm > Grid;
35 typedef FromToGridFactory< Grid > This;
38 typedef decltype(std::declval< Dune::GridFactoryInterface< Grid >* >()->createGrid()) GridPtrType;
40 std::vector< unsigned int > ordering_ ;
43 template <
class FromGr
id,
class Vector>
44 GridPtrType convert(
const FromGrid& grid, Vector& cellData, std::vector<unsigned int>& ordering )
48 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
57 typedef typename FromGrid :: LeafGridView GridView ;
60 typedef typename GridView :: template
Codim< 0 > :: Iterator ElementIterator ;
61 typedef typename ElementIterator::Entity Entity ;
65 GridView gridView = grid.leafGridView();
66 const IndexSet &indexSet = gridView.indexSet();
69 std::map< IndexType, unsigned int > vtxMap;
71 const int numVertices = (1 << dim);
72 std::vector< unsigned int > vertices( numVertices );
73 typedef std::pair< Dune::GeometryType, std::vector< unsigned int > > ElementPair;
74 std::vector< ElementPair > elements;
75 if( ! ordering.empty() )
76 elements.resize( ordering.size() );
78 int nextElementIndex = 0;
79 const ElementIterator end = gridView.template end< 0 >();
80 for( ElementIterator it = gridView.template begin< 0 >(); it != end; ++it )
82 const Entity &entity = *it;
86 alugrid_assert( numVertices == geo.corners() );
87 for(
int i = 0; i < numVertices; ++i )
89 const IndexType vtxId = indexSet.subIndex( entity, i, dim );
90 std::pair< typename std::map< IndexType, unsigned int >::iterator,
bool > result
91 = vtxMap.insert( std::make_pair( vtxId, vtxMap.size() ) );
94 vertices[ i ] = result.first->second;
96 if( ordering.empty() )
103 elements[ ordering[ nextElementIndex++ ] ] = ElementPair( entity.type(), vertices ) ;
107 if( ! ordering.empty() )
110 for(
auto it = elements.begin(), end = elements.end(); it != end; ++it )
116 nextElementIndex = 0;
117 for( ElementIterator it = gridView.template begin< 0 >(); it != end; ++it )
119 const Entity &entity = *it;
121 const int elementIndex = ordering.empty() ? nextElementIndex++ : ordering[ nextElementIndex++ ];
122 const IntersectionIterator iend = gridView.iend( entity );
123 for( IntersectionIterator iit = gridView.ibegin( entity ); iit != iend; ++iit )
125 const Intersection &intersection = *iit;
126 const int faceNumber = intersection.indexInInside();
128 if( intersection.boundary() )
129 factory.insertBoundary( elementIndex, faceNumber );
132 if( intersection.neighbor() &&
136 factory.insertProcessBorder( elementIndex, faceNumber );
143 GridPtrType newgrid = factory.
createGrid(
true,
true, std::string(
"FromToGrid") );
145 if( ! cellData.empty() )
147 Vector oldCellData( cellData );
148 auto macroView = newgrid->levelGridView( 0 );
150 for(
auto it = macroView.template begin<0>(), end = macroView.template end<0>();
151 it != end; ++it, ++idx )
153 const int insertionIndex = ordering.empty() ?
155 cellData[ idx ] = oldCellData[ insertionIndex ] ;
160 if( ordering.empty() )
161 ordering = factory.ordering();
164 MPI_Barrier( MPI_COMM_WORLD );
170 template <
class FromGr
id,
class Vector>
171 GridPtrType convert(
const FromGrid& fromGrid, Vector& cellData )
173 return convert( fromGrid, cellData, ordering_ );
176 template <
class FromGr
id>
177 GridPtrType convert(
const FromGrid& fromGrid )
179 std::vector<int> dummy(0);
180 return convert( fromGrid, dummy, ordering_ );
183 template <
int codim,
class Entity>
184 int subEntities (
const Entity& entity )
const
186 return entity.subEntities( codim );
GridImp::template Codim< cd >::Geometry Geometry
The corresponding geometry type.
Definition: entity.hh:100
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: gridfactory.hh:220
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: gridfactory.hh:346
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: gridfactory.hh:335
virtual std::unique_ptr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition: gridfactory.hh:372
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:92
Dune::Intersection< GridImp, IntersectionImp > Intersection
Type of Intersection this IntersectionIterator points to.
Definition: intersectioniterator.hh:111
Provide a generic factory class for unstructured grids.
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:92
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:86
Dune namespace.
Definition: alignedallocator.hh:13
Various macros to work with Dune module version numbers.