dune-grid  2.2.1
geostorage.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALUGRIDGEOMETRYSTORAGE_HH
2 #define DUNE_ALUGRIDGEOMETRYSTORAGE_HH
3 
4 // Dune includes
5 #include <dune/common/misc.hh>
8 
9 #if HAVE_ALUGRID
10 
14 
15 namespace Dune
16 {
17  template<int dim, int dimw>
18  class ALUCubeGrid;
19 
20  template<int dim, int dimw>
21  class ALUSimplexGrid;
22 
23  template<int dim, int dimw>
24  class ALUConformGrid;
25 
26  template< class GridImp, class GeometryImpl, int nChild >
27  class ALULocalGeometryStorage
28  {
29  // array with pointers to the geometries
30  Dune::array< GeometryImpl *, nChild > geoms_;
31 
32  // count local geometry creation
33  int count_;
34 
35  // type of grid impl
36  typedef typename GridImp :: ctype ctype;
37  enum{ dimension = GridImp :: dimension };
38  enum{ dimensionworld = GridImp :: dimensionworld };
39 
40  template <int dummy, int dim, int dimworld, int >
41  struct CreateGeometries;
42 
43  template <int dummy, int dimworld>
44  struct CreateGeometries<dummy, 2, dimworld, ALU2DSPACE triangle >
45  {
46  template <class Storage>
47  static void createGeometries(Storage& storage,
48  const GeometryType& type,
49  const bool nonConform )
50  {
51  if( nonConform )
52  {
53  typedef ALUGrid< 2, dimworld, simplex, nonconforming, No_Comm > Grid;
54  storage.template createGeometries< Grid > (type);
55  }
56  else
57  {
58  typedef ALUGrid< 2, dimworld, simplex, conforming, No_Comm > Grid;
59  storage.template createGeometries< Grid > (type);
60  }
61  }
62  };
63 
64  template <int dummy>
65  struct CreateGeometries<dummy, 3, 3, ALU3DSPACE tetra >
66  {
67  template <class Storage>
68  static void createGeometries(Storage& storage,
69  const GeometryType& type,
70  const bool nonConform )
71  {
72  assert ( nonConform ) ;
73  {
74  typedef ALUGrid< 3, 3, simplex, nonconforming, No_Comm > Grid;
75  storage.template createGeometries< Grid > (type);
76  }
77  /*
78  // TODO, implement this for refinement of all edges (conforming)
79  else
80  {
81  typedef ALUGrid< 3, 3, simplex, conforming, No_Comm > Grid;
82  storage.template createGeometries< Grid > (type);
83  }
84  */
85  }
86  };
87 
88  template <int dummy, int dimworld>
89  struct CreateGeometries<dummy, 2, dimworld, ALU2DSPACE quadrilateral >
90  {
91  template <class Storage>
92  static void createGeometries(Storage& storage,
93  const GeometryType& type,
94  const bool nonConform )
95  {
96  assert ( nonConform ) ;
97  {
98  typedef ALUGrid< 2, dimworld, cube, nonconforming, No_Comm > Grid;
99  storage.template createGeometries< Grid > (type);
100  }
101  }
102  };
103 
104  template <int dummy>
105  struct CreateGeometries<dummy, 3, 3, ALU3DSPACE hexa >
106  {
107  template <class Storage>
108  static void createGeometries(Storage& storage,
109  const GeometryType& type,
110  const bool nonConform )
111  {
112  assert( nonConform );
113  {
114  typedef ALUGrid< 3, 3, cube, nonconforming, No_Comm > Grid;
115  storage.template createGeometries< Grid > (type);
116  }
117  }
118  };
119 
120  public:
121  // create empty storage
122  ALULocalGeometryStorage ( const GeometryType type, const bool nonConform )
123  : count_( 0 )
124  {
125  geoms_.fill( (GeometryImpl *) 0 );
126  // the idea is to create a grid containing the reference element,
127  // refine once and the store the father - child relations
128  CreateGeometries<0, dimension, dimensionworld, GridImp :: elementType >
129  ::createGeometries(*this, type, nonConform);
130  }
131 
132  // desctructor deleteing geometries
133  ~ALULocalGeometryStorage ()
134  {
135  for(size_t i=0; i<geoms_.size(); ++i)
136  if(geoms_[i]) delete geoms_[i];
137  }
138 
139  // check if geometry has been created
140  bool geomCreated(int child) const { return geoms_[child] != 0; }
141 
142  // return reference to local geometry
143  const GeometryImpl & operator [] (int child) const
144  {
145  assert( geomCreated(child) );
146  return *(geoms_[child]);
147  }
148 
149  template < class Grid >
150  void createGeometries(const GeometryType& type)
151  {
152  // create factory without verbosity
153  GridFactory< Grid > factory( false );
154 
155  const Dune::GenericReferenceElement< ctype, dimension > &refElem
156  = Dune::GenericReferenceElements< ctype, dimension >::general( type );
157 
158  // insert vertices
159  FieldVector<ctype, dimensionworld> pos( 0 );
160  const int vxSize = refElem.size(dimension);
161  for(int i=0; i<vxSize; ++i)
162  {
163  FieldVector<ctype, dimension> position = refElem.position(i, dimension );
164  // copy position
165  for(int d = 0; d<dimension; ++d )
166  pos[ d ] = position[ d ];
167 
168  factory.insertVertex( pos );
169  }
170 
171  std::vector< unsigned int > vertices( vxSize );
172  // create grid with reference element
173  for(size_t i=0; i<vertices.size(); ++i) vertices[ i ] = i;
174  factory.insertElement(type, vertices);
175 
176  // save original sbuf
177  std::streambuf* cerr_sbuf = std::cerr.rdbuf();
178  std::stringstream tempout;
179  // redirect 'cerr' to a 'fout' to avoid unnecessary output in constructors
180  std::cerr.rdbuf(tempout.rdbuf());
181 
182  Grid* gridPtr = factory.createGrid();
183  Grid& grid = *gridPtr;
184 
185  // restore the original stream buffer
186  std::cerr.rdbuf(cerr_sbuf);
187 
188  //std::cerr = savecerr;
189 
190  // refine once to get children
191  const int level = 1;
192  grid.globalRefine( level );
193 
194  {
195  typedef typename Grid :: template Partition< All_Partition >:: LevelGridView MacroGridView;
196  MacroGridView macroView = grid.template levelView< All_Partition > ( 0 );
197  typedef typename MacroGridView :: template Codim< 0 > :: Iterator Iterator;
198 
199  Iterator it = macroView.template begin<0> ();
200 
201  if( it == macroView.template end<0>() )
202  DUNE_THROW(InvalidStateException,"Empty Grid, should contain at least 1 element");
203 
204  typedef typename Iterator :: Entity EntityType;
205 
206  const EntityType& entity = *it;
207  const typename EntityType :: Geometry& geo = entity.geometry();
208  typedef typename EntityType :: HierarchicIterator HierarchicIteratorType;
209  const HierarchicIteratorType end = entity.hend( level );
210 
211  int childNum = 0;
212  for( HierarchicIteratorType child = entity.hbegin( level );
213  child != end; ++child, ++childNum )
214  {
215  create( geo, child->geometry(), childNum );
216  }
217  }
218 
219  // delete grid
220  delete gridPtr;
221  }
222 
223  protected:
224  // create local geometry
225  template< class Geometry >
226  void create ( const Geometry &father,
227  const Geometry &son,
228  const int child )
229  {
230  assert( !geomCreated( child ) );
231  assert( (child >= 0) && (child < nChild) );
232 
233  assert( (count_ < nChild) );
234  ++count_;
235 
236  geoms_[ child ] = new GeometryImpl();
237  geoms_[ child ]->buildGeomInFather( father, son );
238  }
239  };
240 
241 } // namespace Dune
242 
243 #endif // end HAVE_ALUGRID
244 
245 #endif // #ifndef DUNE_ALUGRIDGEOMETRYSTORAGE_HH