dune-grid  2.2.1
dofvector.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALBERTA_DOFVECTOR_HH
2 #define DUNE_ALBERTA_DOFVECTOR_HH
3 
4 #include <cstdlib>
5 #include <limits>
6 
10 
11 #if HAVE_ALBERTA
12 
13 namespace Dune
14 {
15 
16  namespace Alberta
17  {
18 
19  // External Forward Declarations
20  // -----------------------------
21 
22  template< int dim >
23  class MeshPointer;
24 
25 
26 
27  // DofVectorProvider
28  // -----------------
29 
30  template< class Dof >
32 
33  template<>
34  struct DofVectorProvider< int >
35  {
36  typedef ALBERTA DOF_INT_VEC DofVector;
37 
38  static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
39  {
40  return ALBERTA get_dof_int_vec( name.c_str(), dofSpace );
41  }
42 
43  static void free ( DofVector *dofVector )
44  {
45  ALBERTA free_dof_int_vec( dofVector );
46  }
47 
48  static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
49  {
50  return ALBERTA read_dof_int_vec_xdr( filename.c_str(), mesh, dofSpace );
51  }
52 
53  static bool write ( const DofVector *dofVector, const std::string &filename )
54  {
55  int success = ALBERTA write_dof_int_vec_xdr( dofVector, filename.c_str() );
56  return (success == 0);
57  }
58  };
59 
60  template<>
61  struct DofVectorProvider< signed char >
62  {
63  typedef ALBERTA DOF_SCHAR_VEC DofVector;
64 
65  static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
66  {
67  return ALBERTA get_dof_schar_vec( name.c_str(), dofSpace );
68  }
69 
70  static void free ( DofVector *dofVector )
71  {
72  ALBERTA free_dof_schar_vec( dofVector );
73  }
74 
75  static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
76  {
77  return ALBERTA read_dof_schar_vec_xdr( filename.c_str(), mesh, dofSpace );
78  }
79 
80  static bool write ( const DofVector *dofVector, const std::string &filename )
81  {
82  int success = ALBERTA write_dof_schar_vec_xdr( dofVector, filename.c_str() );
83  return (success == 0);
84  }
85  };
86 
87  template<>
88  struct DofVectorProvider< unsigned char >
89  {
90  typedef ALBERTA DOF_UCHAR_VEC DofVector;
91 
92  static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
93  {
94  return ALBERTA get_dof_uchar_vec( name.c_str(), dofSpace );
95  }
96 
97  static void free ( DofVector *dofVector )
98  {
99  ALBERTA free_dof_uchar_vec( dofVector );
100  }
101 
102  static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
103  {
104  return ALBERTA read_dof_uchar_vec_xdr( filename.c_str(), mesh, dofSpace );
105  }
106 
107  static bool write ( const DofVector *dofVector, const std::string &filename )
108  {
109  int success = ALBERTA write_dof_uchar_vec_xdr( dofVector, filename.c_str() );
110  return (success == 0);
111  }
112  };
113 
114  template<>
116  {
117  typedef ALBERTA DOF_REAL_VEC DofVector;
118 
119  static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
120  {
121  return ALBERTA get_dof_real_vec( name.c_str(), dofSpace );
122  }
123 
124  static void free ( DofVector *dofVector )
125  {
126  ALBERTA free_dof_real_vec( dofVector );
127  }
128 
129  static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
130  {
131  return ALBERTA read_dof_real_vec_xdr( filename.c_str(), mesh, dofSpace );
132  }
133 
134  static bool write ( const DofVector *dofVector, const std::string &filename )
135  {
136  int success = ALBERTA write_dof_real_vec_xdr( dofVector, filename.c_str() );
137  return (success == 0);
138  }
139  };
140 
141  template<>
143  {
144  typedef ALBERTA DOF_REAL_D_VEC DofVector;
145 
146  static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
147  {
148  return ALBERTA get_dof_real_d_vec( name.c_str(), dofSpace );
149  }
150 
151  static void free ( DofVector *dofVector )
152  {
153  ALBERTA free_dof_real_d_vec( dofVector );
154  }
155 
156  static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
157  {
158  return ALBERTA read_dof_real_d_vec_xdr( filename.c_str(), mesh, dofSpace );
159  }
160 
161  static bool write ( const DofVector *dofVector, const std::string &filename )
162  {
163  int success = ALBERTA write_dof_real_d_vec_xdr( dofVector, filename.c_str() );
164  return (success == 0);
165  }
166  };
167 
168 
169 
170  // DofVectorPointer
171  // ----------------
172 
173  template< class Dof >
175  {
177 
179 
180  public:
182 
183  static const bool supportsAdaptationData = (DUNE_ALBERTA_VERSION >= 0x300);
184 
185  private:
186  DofVector *dofVector_;
187 
188  public:
190  : dofVector_( NULL )
191  {}
192 
193  explicit DofVectorPointer ( const DofSpace *dofSpace,
194  const std::string &name = "" )
195  : dofVector_ ( DofVectorProvider::get( dofSpace, name ) )
196  {}
197 
198  explicit DofVectorPointer ( DofVector *dofVector )
199  : dofVector_( dofVector )
200  {}
201 
202  operator bool () const
203  {
204  return (bool)dofVector_;
205  }
206 
207  operator DofVector * () const
208  {
209  return dofVector_;
210  }
211 
212  operator Dof * () const
213  {
214  Dof *ptr = NULL;
215  GET_DOF_VEC( ptr, dofVector_ );
216  return ptr;
217  }
218 
219  const DofSpace *dofSpace () const
220  {
221  return dofVector_->fe_space;
222  }
223 
224  std::string name () const
225  {
226  if( dofVector_ )
227  return dofVector_->name;
228  else
229  return std::string();
230  }
231 
232  void create ( const DofSpace *dofSpace, const std::string &name = "" )
233  {
234  release();
235  dofVector_ = DofVectorProvider::get( dofSpace, name );
236  }
237 
238  template< int dim >
239  void read ( const std::string &filename, const MeshPointer< dim > &meshPointer )
240  {
241  release();
242  dofVector_ = DofVectorProvider::read( filename, meshPointer, NULL );
243  }
244 
245  bool write ( const std::string &filename ) const
246  {
247  return DofVectorProvider::write( dofVector_, filename );
248  }
249 
250  void release ()
251  {
252  if( dofVector_ )
253  {
254  DofVectorProvider::free( dofVector_ );
255  dofVector_ = NULL;
256  }
257  }
258 
259  template< class Functor >
260  void forEach ( Functor &functor ) const
261  {
262  Dof *array = (Dof *)(*this);
263  FOR_ALL_DOFS( dofSpace()->admin, functor( array[ dof ] ) );
264  }
265 
266  void initialize ( const Dof &value )
267  {
268  Dof *array = (Dof *)(*this);
269  FOR_ALL_DOFS( dofSpace()->admin, array[ dof ] = value );
270  }
271 
272  template< class AdaptationData >
273  AdaptationData *getAdaptationData () const
274  {
275  assert( dofVector_ );
276 #if DUNE_ALBERTA_VERSION >= 0x300
277  assert( dofVector_->user_data );
278  return static_cast< AdaptationData * >( dofVector_->user_data );
279 #else
280  return 0;
281 #endif
282  }
283 
284  template< class AdaptationData >
285  void setAdaptationData ( AdaptationData *adaptationData )
286  {
287  assert( dofVector_ );
288 #if DUNE_ALBERTA_VERSION >= 0x300
289  dofVector_->user_data = adaptationData;
290 #endif // #if DUNE_ALBERTA_VERSION >= 0x300
291  }
292 
293  template< class Interpolation >
295  {
296  assert( dofVector_ );
297  dofVector_->refine_interpol = &refineInterpolate< Interpolation >;
298  }
299 
300  template< class Restriction >
302  {
303  assert( dofVector_ );
304  dofVector_->coarse_restrict = &coarsenRestrict< Restriction >;
305  }
306 
307  private:
308  template< class Interpolation >
309  static void refineInterpolate ( DofVector *dofVector, RC_LIST_EL *list, int n )
310  {
311  const This dofVectorPointer( dofVector );
312  typename Interpolation::Patch patch( list, n );
313  Interpolation::interpolateVector( dofVectorPointer, patch );
314  }
315 
316  template< class Restriction >
317  static void coarsenRestrict ( DofVector *dofVector, RC_LIST_EL *list, int n )
318  {
319  const This dofVectorPointer( dofVector );
320  typename Restriction::Patch patch( list, n );
321  Restriction::restrictVector( dofVectorPointer, patch );
322  }
323  };
324 
325 
326 
327  // Auxilliary Functions
328  // --------------------
329 
330  inline void abs ( const DofVectorPointer< int > &dofVector )
331  {
332  assert( !dofVector == false );
333  int *array = (int *)dofVector;
334  FOR_ALL_DOFS( dofVector.dofSpace()->admin,
335  array[ dof ] = std::abs( array[ dof ] ) );
336  }
337 
338 
339  inline int max ( const DofVectorPointer< int > &dofVector )
340  {
341  assert( !dofVector == false );
342  int *array = (int *)dofVector;
343  int result = std::numeric_limits< int >::min();
344  FOR_ALL_DOFS( dofVector.dofSpace()->admin,
345  result = std::max( result, array[ dof ] ) );
346  return result;
347  }
348 
349 
350  inline int min ( const DofVectorPointer< int > &dofVector )
351  {
352  assert( !dofVector == false );
353  int *array = (int *)dofVector;
354  int result = std::numeric_limits< int >::max();
355  FOR_ALL_DOFS( dofVector.dofSpace()->admin,
356  result = std::min( result, array[ dof ] ) );
357  return result;
358  }
359 
360  } // namespace Alberta
361 
362 } // namespace Dune
363 
364 #endif // #if HAVE_ALBERTA
365 
366 #endif // #ifndef DUNE_ALBERTA_DOFVECTOR_HH