Dune Core Modules (2.8.0)

macrodata.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ALBERTA_MACRODATA_HH
4#define DUNE_ALBERTA_MACRODATA_HH
5
13
14#include <dune/grid/albertagrid/misc.hh>
15#include <dune/grid/albertagrid/algebra.hh>
16#include <dune/grid/albertagrid/albertaheader.hh>
17
18#if HAVE_ALBERTA
19
20namespace Dune
21{
22
23 namespace Alberta
24 {
25
26 template< int dim >
27 class MacroData
28 {
29 typedef MacroData< dim > This;
30
31 typedef ALBERTA MACRO_DATA Data;
32
33 static const int dimension = dim;
34 static const int numVertices = NumSubEntities< dimension, dimension >::value;
35 static const int numEdges = NumSubEntities< dimension, dimension-1 >::value;
36
37 static const int initialSize = 4096;
38
39 public:
40 template< int >
41 struct Library;
42
43 template< int > friend struct InstantiateMacroDataLibrary;
44
45 public:
46 typedef int ElementId[ numVertices ];
47
48 static const int supportPeriodicity = 1;
49
50 MacroData ()
51 : data_( NULL ),
52 vertexCount_( -1 ),
53 elementCount_( -1 )
54 {}
55
56 operator Data * () const
57 {
58 return data_;
59 }
60
61 int vertexCount () const
62 {
63 return (vertexCount_ < 0 ? data_->n_total_vertices : vertexCount_);
64 }
65
66 int elementCount () const
67 {
68 return (elementCount_ < 0 ? data_->n_macro_elements : elementCount_);
69 }
70
71 ElementId &element ( int i ) const;
72 GlobalVector &vertex ( int i ) const;
73 int &neighbor ( int element, int i ) const;
74 BoundaryId &boundaryId ( int element, int i ) const;
75
80 void create ();
81
90 void finalize ();
91
100 void markLongestEdge ();
101
110 void setOrientation ( const Real orientation );
111
122 bool checkNeighbors () const;
123
125 void release ()
126 {
127 if( data_ != NULL )
128 {
129 ALBERTA free_macro_data( data_ );
130 data_ = NULL;
131 }
132 vertexCount_ = elementCount_ = -1;
133 }
134
140 int insertElement ( const ElementId &id );
141
147 int insertVertex ( const GlobalVector &coords )
148 {
149 assert( vertexCount_ >= 0 );
150 if( vertexCount_ >= data_->n_total_vertices )
151 resizeVertices( 2*vertexCount_ );
152 copy( coords, vertex( vertexCount_ ) );
153 return vertexCount_++;
154 }
155
161 int insertVertex ( const FieldVector< Real, dimWorld > &coords )
162 {
163 assert( vertexCount_ >= 0 );
164 if( vertexCount_ >= data_->n_total_vertices )
165 resizeVertices( 2*vertexCount_ );
166 copy( coords, vertex( vertexCount_ ) );
167 return vertexCount_++;
168 }
169
170 void insertWallTrafo ( const GlobalMatrix &m, const GlobalVector &t );
171 void insertWallTrafo ( const FieldMatrix< Real, dimWorld, dimWorld > &matrix,
172 const FieldVector< Real, dimWorld > &shift );
173
174 void checkCycles ();
175
176 void read ( const std::string &filename, bool binary = false );
177
178 bool write ( const std::string &filename, bool binary = false ) const
179 {
180 if( binary )
181 return ALBERTA write_macro_data_xdr( data_, filename.c_str() );
182 else
183 return ALBERTA write_macro_data( data_, filename.c_str() );
184 }
185
186 private:
187 template< class Vector >
188 void copy ( const Vector &x, GlobalVector &y )
189 {
190 for( int i = 0; i < dimWorld; ++i )
191 y[ i ] = x[ i ];
192 }
193
194 void resizeElements ( const int newSize );
195
196 void resizeVertices ( const int newSize )
197 {
198 const int oldSize = data_->n_total_vertices;
199 data_->n_total_vertices = newSize;
200 data_->coords = memReAlloc< GlobalVector >( data_->coords, oldSize, newSize );
201 assert( (data_->coords != NULL) || (newSize == 0) );
202 }
203
204 private:
205 Data *data_;
206 int vertexCount_;
207 int elementCount_;
208 };
209
210
211
212 // MacroData::Library
213 // ------------------
214
215 template< int dim >
216 template< int >
217 struct MacroData< dim >::Library
218 {
219 typedef Alberta::MacroData< dim > MacroData;
220
221 static bool checkNeighbors ( const MacroData &macroData );
222 static void markLongestEdge ( MacroData &macroData );
223 static void setOrientation ( [[maybe_unused]] MacroData &macroData,
224 [[maybe_unused]] const Real orientation );
225
226 private:
227 static Real edgeLength ( const MacroData &macroData, const ElementId &e, int edge );
228 static int longestEdge ( const MacroData &macroData, const ElementId &e );
229
230 template< class Type >
231 static void rotate ( Type *array, int i, int shift );
232
233 static void rotate ( MacroData &macroData, int i, int shift );
234 static void swap ( MacroData &macroData, int el, int v1, int v2 );
235 };
236
237
238
239 // Implementation of MacroData
240 // ---------------------------
241
242 template< int dim >
243 inline typename MacroData< dim >::ElementId &
244 MacroData< dim >::element ( int i ) const
245 {
246 assert( (i >= 0) && (i < data_->n_macro_elements) );
247 const int offset = i * numVertices;
248 return *reinterpret_cast< ElementId * >( data_->mel_vertices + offset );
249 }
250
251
252 template< int dim >
253 inline GlobalVector &MacroData< dim >::vertex ( int i ) const
254 {
255 assert( (i >= 0) && (i < data_->n_total_vertices) );
256 return data_->coords[ i ];
257 }
258
259
260 template< int dim >
261 inline int &MacroData< dim >::neighbor ( int element, int i ) const
262 {
263 assert( (element >= 0) && (element < data_->n_macro_elements) );
264 assert( (i >= 0) && (i < numVertices) );
265 return data_->neigh[ element*numVertices + i ];
266 }
267
268
269 template< int dim >
270 inline BoundaryId &MacroData< dim >::boundaryId ( int element, int i ) const
271 {
272 assert( (element >= 0) && (element < data_->n_macro_elements) );
273 assert( (i >= 0) && (i < numVertices) );
274 return data_->boundary[ element*numVertices + i ];
275 }
276
277
278 template< int dim >
279 inline void MacroData< dim >::create ()
280 {
281 release();
282 data_ = ALBERTA alloc_macro_data( dim, initialSize, initialSize );
283 data_->boundary = memAlloc< BoundaryId >( initialSize*numVertices );
284 if( dim == 3 )
285 data_->el_type = memAlloc< ElementType >( initialSize );
286 vertexCount_ = elementCount_ = 0;
287 elementCount_ = 0;
288 }
289
290
291 template< int dim >
292 inline void MacroData< dim >::finalize ()
293 {
294 if( (vertexCount_ >= 0) && (elementCount_ >= 0) )
295 {
296 resizeVertices( vertexCount_ );
297 resizeElements( elementCount_ );
298 ALBERTA compute_neigh_fast( data_ );
299
300 // assign default boundary id (if none is assigned)
301 for( int element = 0; element < elementCount_; ++element )
302 {
303 for( int i = 0; i < numVertices; ++i )
304 {
305 BoundaryId &id = boundaryId( element, i );
306 if( neighbor( element, i ) >= 0 )
307 {
308 assert( id == InteriorBoundary );
309 id = InteriorBoundary;
310 }
311 else
312 id = (id == InteriorBoundary ? DirichletBoundary : id);
313 }
314 }
315
316 vertexCount_ = elementCount_ = -1;
317 }
318 assert( (vertexCount_ < 0) && (elementCount_ < 0) );
319 }
320
321
322 template< int dim >
323 inline void MacroData< dim >::markLongestEdge ()
324 {
325 Library< dimWorld >::markLongestEdge( *this );
326 }
327
328
329 template< int dim >
330 inline void MacroData< dim >::setOrientation ( const Real orientation )
331 {
332 Library< dimWorld >::setOrientation( *this, orientation );
333 }
334
335
336 template< int dim >
337 inline bool MacroData< dim >::checkNeighbors () const
338 {
339 return Library< dimWorld >::checkNeighbors( *this );
340 }
341
342
343 template< int dim >
344 inline int MacroData< dim >::insertElement ( const ElementId &id )
345 {
346 assert( elementCount_ >= 0 );
347 if( elementCount_ >= data_->n_macro_elements )
348 resizeElements( 2*elementCount_ );
349
350 ElementId &e = element( elementCount_ );
351 for( int i = 0; i < numVertices; ++i )
352 {
353 e[ i ] = id[ i ];
354 boundaryId( elementCount_, i ) = InteriorBoundary;
355 }
356 if( dim == 3 )
357 data_->el_type[ elementCount_ ] = 0;
358
359 return elementCount_++;
360 }
361
362
363 template< int dim >
364 inline void MacroData< dim >
365 ::insertWallTrafo ( const GlobalMatrix &matrix, const GlobalVector &shift )
366 {
367 int &count = data_->n_wall_trafos;
368 AffineTransformation *&array = data_->wall_trafos;
369
370 // resize wall trafo array
371 array = memReAlloc< AffineTransformation >( array, count, count+1 );
372 assert( data_->wall_trafos != NULL );
373
374 // copy matrix and shift
375 for( int i = 0; i < dimWorld; ++i )
376 copy( matrix[ i ], array[ count ].M[ i ] );
377 copy( shift, array[ count ].t );
378 ++count;
379 }
380
381 template< int dim >
382 inline void MacroData< dim >
383 ::insertWallTrafo ( const FieldMatrix< Real, dimWorld, dimWorld > &matrix,
384 const FieldVector< Real, dimWorld > &shift )
385 {
386 int &count = data_->n_wall_trafos;
387 AffineTransformation *&array = data_->wall_trafos;
388
389 // resize wall trafo array
390 array = memReAlloc< AffineTransformation >( array, count, count+1 );
391 assert( data_->wall_trafos != NULL );
392
393 // copy matrix and shift
394 for( int i = 0; i < dimWorld; ++i )
395 copy( matrix[ i ], array[ count ].M[ i ] );
396 copy( shift, array[ count ].t );
397 ++count;
398 }
399
400
401 template< int dim >
402 inline void MacroData< dim >::checkCycles ()
403 {
404 // ensure that the macro data has been finalized
405 finalize();
406 ALBERTA macro_test( data_, NULL );
407 }
408
409
410 template< int dim >
411 inline void MacroData< dim >::read ( const std::string &filename, bool binary )
412 {
413 release();
414 if( binary )
415 data_ = ALBERTA read_macro_xdr( filename.c_str() );
416 else
417 data_ = ALBERTA read_macro( filename.c_str() );
418 }
419
420
421 template< int dim >
422 inline void MacroData< dim >::resizeElements ( const int newSize )
423 {
424 const int oldSize = data_->n_macro_elements;
425 data_->n_macro_elements = newSize;
426 data_->mel_vertices = memReAlloc( data_->mel_vertices, oldSize*numVertices, newSize*numVertices );
427 data_->boundary = memReAlloc( data_->boundary, oldSize*numVertices, newSize*numVertices );
428 if( dim == 3 )
429 data_->el_type = memReAlloc( data_->el_type, oldSize, newSize );
430 assert( (newSize == 0) || (data_->mel_vertices != NULL) );
431 }
432
433 }
434
435}
436
437#endif // #if HAVE_ALBERTA
438
439#endif
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:504
Dune namespace.
Definition: alignedallocator.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)