dune-fem  2.4.1-rc
discretefunction_inline.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_DISCRETEFUNCTION_INLINE_HH
2 #define DUNE_FEM_DISCRETEFUNCTION_INLINE_HH
3 
4 #include <fstream>
5 
6 #include <dune/geometry/referenceelements.hh>
7 
11 
12 #include "discretefunction.hh"
13 
14 namespace Dune
15 {
16 
17  namespace Fem
18  {
19 
20  // DiscreteFunctionDefault
21  // -----------------------
22 
23  template< class Impl >
25  :: DiscreteFunctionDefault ( const std::string &name,
26  const DiscreteFunctionSpaceType &dfSpace )
27  : dfSpace_( dfSpace ),
28  ldvStack_( std::max( std::max( sizeof( DofType ), sizeof( DofType* ) ),
29  sizeof(typename LocalDofVectorType::value_type) ) // for PetscDiscreteFunction
30  * space().blockMapper().maxNumDofs() * DiscreteFunctionSpaceType::localBlockSize ),
31  ldvAllocator_( &ldvStack_ ),
32  name_( name ),
33  scalarProduct_( dfSpace )
34  {
35  }
36 
37 
38  template< class Impl >
41  {
42  dofPointerLock_.lock();
43 
44  const unsigned int size = BaseType :: size();
45  RangeFieldType *dofPointer = new RangeFieldType[ size ];
46 
47  unsigned int i = 0;
48  const ConstDofIteratorType end = BaseType :: dend();
49  for( ConstDofIteratorType it = BaseType :: dbegin(); it != end; ++it )
50  dofPointer[ i++ ] = *it;
51  assert( i == size );
52 
53  return dofPointer;
54  }
55 
56 
57  template< class Impl >
60  {
61  unsigned int i = 0;
62  const DofIteratorType end = BaseType :: dend();
63  for( DofIteratorType it = BaseType :: dbegin(); it != end; ++it )
64  *it = dofPointer[ i++ ];
65  assert( i == BaseType :: size() );
66 
67  delete[] dofPointer;
68  dofPointerLock_.unlock();
69  }
70 
71 
72  template< class Impl >
74  :: freeDofPointerNoCopy( const RangeFieldType *dofPointer ) const
75  {
76  delete[] dofPointer;
77  dofPointerLock_.unlock();
78  }
79 
80 
81  template< class Impl >
83  :: print ( std::ostream &out ) const
84  {
85  const ConstDofIteratorType end = BaseType :: dend();
86  for( ConstDofIteratorType dit = BaseType :: dbegin(); dit != end; ++dit )
87  out << (*dit) << std::endl;
88  }
89 
90 
91  template< class Impl >
93  :: dofsValid () const
94  {
95  const ConstDofIteratorType end = BaseType :: dend();
96  for( ConstDofIteratorType it = BaseType :: dbegin(); it != end; ++it )
97  {
98  if( ! std::isfinite( *it ) )
99  return false ;
100  }
101 
102  return true;
103  }
104 
105 
106  template< class Impl >
107  template< class DFType >
110  {
111  if( BaseType::size() != g.size() )
112  DUNE_THROW(InvalidStateException,"DiscreteFunctionDefault: sizes do not match in axpy");
113 
114  // apply axpy to all dofs from g
115  const DofIteratorType end = BaseType::dend();
116  typename DFType :: ConstDofIteratorType git = g.dbegin();
117  for( DofIteratorType it = BaseType::dbegin(); it != end; ++it, ++git )
118  *it += s * (*git );
119  }
120 
121 
122  template< class Impl >
123  template< class DFType >
126  {
127  if( BaseType::size() != g.size() )
128  DUNE_THROW(InvalidStateException,"DiscreteFunctionDefault: sizes do not match in assign");
129 
130  // copy all dofs from g to this
131  const DofIteratorType end = BaseType::dend();
132  typename DFType :: ConstDofIteratorType git = g.dbegin();
133  for( DofIteratorType it = BaseType::dbegin(); it != end; ++it, ++git )
134  *it = *git;
135  }
136 
137 
138  template< class Impl >
139  template< class Operation >
140  inline typename DiscreteFunctionDefault< Impl >
141  :: template CommDataHandle< Operation > :: Type
142  DiscreteFunctionDefault< Impl > :: dataHandle ( const Operation *operation )
143  {
144  return BaseType :: space().createDataHandle( asImp(), operation );
145  }
146 
147 
148  template< class Impl >
149  template< class Functor >
151  ::evaluateGlobal ( const DomainType &x, Functor functor ) const
152  {
153  typedef typename DiscreteFunctionSpaceType::GridPartType GridPartType;
154  EntitySearch< GridPartType, EntityType::codimension > entitySearch( BaseType::space().gridPart() );
155 
156  const EntityType & entity(entitySearch( x ));
157  const typename EntityType::Geometry geometry = entity.geometry();
158  functor( geometry.local( x ), BaseType::localFunction( entity ) );
159  }
160 
161 
162  template< class Impl >
163  template< class DFType >
167  {
168  if( BaseType::size() != g.size() )
169  DUNE_THROW(InvalidStateException,"DiscreteFunctionDefault: sizes do not match in operator +=");
170 
171  const DofIteratorType end = BaseType::dend();
172  typename DFType :: ConstDofIteratorType git = g.dbegin();
173  for( DofIteratorType it = BaseType::dbegin(); it != end; ++it, ++git )
174  *it += *git;
175  return asImp();
176  }
177 
178 
179  template< class Impl >
180  template< class DFType >
184  {
185  if( BaseType::size() != g.size() )
186  DUNE_THROW(InvalidStateException,"DiscreteFunctionDefault: sizes do not match in operator -=");
187 
188  const DofIteratorType end = BaseType :: dend();
189  typename DFType :: ConstDofIteratorType git = g.dbegin();
190  for( DofIteratorType it = BaseType :: dbegin(); it != end; ++it, ++git )
191  *it -= *git;
192  return asImp();
193  }
194 
195 
196  template< class Impl >
200  {
201  const DofIteratorType end = BaseType :: dend();
202  for( DofIteratorType it = BaseType :: dbegin(); it != end; ++it )
203  *it *= scalar;
204  return asImp();
205  }
206 
207 
208  template< class Impl >
209  template< class StreamTraits >
212  {
213  unsigned int versionId = in.readUnsignedInt();
214  if( versionId < DUNE_VERSION_ID(0,9,1) )
215  DUNE_THROW( IOError, "Trying to read outdated file." );
216  else if( versionId > DUNE_MODULE_VERSION_ID(DUNE_FEM) )
217  std :: cerr << "Warning: Reading discrete function from newer version: "
218  << versionId << std :: endl;
219 
220  // verify space id for files written with dune-fem version 1.5 or newer
221  if( versionId >= DUNE_VERSION_ID(1,5,0) )
222  {
223  // make sure that space of discrete function matches the space
224  // of the data that was written
225  const DFSpaceIdentifier spaceId = space().type();
226  int mySpaceIdInt;
227  in >> mySpaceIdInt;
228  const DFSpaceIdentifier mySpaceId = (DFSpaceIdentifier) mySpaceIdInt;
229 
230  if( spaceId != mySpaceId )
231  DUNE_THROW( IOError, "Trying to read discrete function from different space: DFSpace (" << spaceName( spaceId ) << ") != DataSpace (" << spaceName( mySpaceId ) << ")" );
232  }
233 
234  // read name
235  in >> name_;
236 
237  // read size as integer
238  int mysize;
239  in >> mysize ;
240 
241  // check size
242  if( mysize != BaseType :: size() &&
243  BaseType :: size() != this->space().size() ) // only read compressed vectors
244  {
245  DUNE_THROW( IOError, "Trying to read discrete function of different size." );
246  }
247 
248  // read all dofs
249  const DofIteratorType end = BaseType :: dend();
250  for( DofIteratorType it = BaseType :: dbegin(); it != end; ++it )
251  in >> *it;
252  }
253 
254 
255  template< class Impl >
256  template< class StreamTraits >
259  {
260  unsigned int versionId = DUNE_MODULE_VERSION_ID(DUNE_FEM);
261  out << versionId ;
262 
263  // write space id to for testing when function is read
264  int spaceId = space().type();
265  out << spaceId ;
266 
267  // write name
268  out << name_;
269 
270  // only allow write when vector is compressed
271  if( BaseType :: size() != this->space().size() )
272  DUNE_THROW(InvalidStateException,"Writing DiscreteFunction in uncompressed state!");
273 
274  // write size as integer
275  const int mysize = BaseType :: size();
276  out << mysize;
277 
278  // write all dofs
279  const ConstDofIteratorType end = BaseType :: dend();
280  for( ConstDofIteratorType it = BaseType :: dbegin(); it != end; ++it )
281  out << *it;
282  }
283 
284 
285  template< class Impl >
288  {
289  typedef typename DiscreteFunctionSpaceType::IndexSetType IndexSetType;
290  IndexSetType &indexSet = (IndexSetType &)space().indexSet();
292  {
293  PersistentIndexSetInterface* persistentIndexSet
295 
296  // this marks the index set in the DofManager's list of index set as persistent
297  if( persistentIndexSet )
298  persistentIndexSet->addBackupRestore();
299  }
300  }
301 
302  template< class Impl >
305  {
306  typedef typename DiscreteFunctionSpaceType::IndexSetType IndexSetType;
307  IndexSetType &indexSet = (IndexSetType &)space().indexSet();
309  {
310  PersistentIndexSetInterface* persistentIndexSet
312 
313  // this unmarks the index set in the DofManager's list of index set as persistent
314  if( persistentIndexSet )
315  persistentIndexSet->removeBackupRestore();
316  }
317  }
318 
319 
320  template< class Impl >
321  template< class DFType >
324  {
325  if( BaseType :: size() != g.size() )
326  return false;
327 
328  const ConstDofIteratorType end = BaseType :: dend();
329 
330  ConstDofIteratorType fit = BaseType :: dbegin();
331  typename DFType :: ConstDofIteratorType git = g.dbegin();
332  for( ; fit != end; ++fit, ++git )
333  {
334  if( std::abs( *fit - *git ) > 1e-15 )
335  return false;
336  }
337 
338  return true;
339  }
340 
341 
342  // Stream Operators
343  // ----------------
344 
353  template< class Impl >
354  inline std :: ostream &
355  operator<< ( std :: ostream &out,
357  {
358  df.print( out );
359  return out;
360  }
361 
362 
363 
373  template< class StreamTraits, class Impl >
375  operator<< ( OutStreamInterface< StreamTraits > &out,
377  {
378  df.write( out );
379  return out;
380  }
381 
382 
383 
393  template< class StreamTraits, class Impl >
397  {
398  df.read( in );
399  return in;
400  }
401 
402  } // end namespace Fem
403 
404 } // end namespace Dune
405 #endif // #ifndef DUNE_FEM_DISCRETEFUNCTION_INLINE_HH
Definition: discretefunction.hh:622
void assign(const DiscreteFunctionInterface< DFType > &g)
Definition: discretefunction_inline.hh:125
virtual void insertSubData()
Definition: discretefunction_inline.hh:287
virtual void removeBackupRestore()=0
please doc me
RangeFieldType * allocDofPointer() const
allocate a pointer to a consecutive array storing the DoFs
Definition: discretefunction_inline.hh:40
void write(OutStreamInterface< StreamTraits > &out) const
write the discrete function into a stream
Definition: discretefunction_inline.hh:258
DiscreteFunctionType & operator-=(const DiscreteFunctionInterface< DFType > &g)
substract all degrees of freedom from given discrete function using the dof iterators ...
Definition: discretefunction_inline.hh:183
InStreamInterface< StreamTraits > & operator>>(InStreamInterface< StreamTraits > &in, DiscreteFunctionInterface< Impl > &df)
read a discrete function from an input stream
Definition: discretefunction_inline.hh:395
DFSpaceIdentifier
enumerator for identification of spaces
Definition: discretefunctionspace.hh:88
void axpy(const RangeFieldType &s, const DiscreteFunctionInterface< DFType > &g)
axpy operation
Definition: discretefunction_inline.hh:109
DiscreteFunctionSpaceType::DomainType DomainType
type of domain, i.e. type of coordinates
Definition: discretefunction.hh:107
Traits::DofType DofType
Definition: discretefunction.hh:131
bool dofsValid() const
check for NaNs
Definition: discretefunction_inline.hh:93
static double max(const Double &v, const double p)
Definition: double.hh:387
Definition: discretefunction.hh:63
Definition: entitysearch.hh:131
void read(InStreamInterface< StreamTraits > &in)
read the discrete function from a stream
Definition: discretefunction_inline.hh:211
void evaluateGlobal(const DomainType &x, Functor functor) const
evaluate functor in global coordinate
Definition: discretefunction_inline.hh:151
Traits::DofIteratorType DofIteratorType
Type of the dof iterator used in the discrete function implementation.
Definition: discretefunction.hh:126
DiscreteFunctionSpaceType::EntityType EntityType
type of entity local functions are defined on
Definition: discretefunction.hh:152
CommDataHandle< Operation >::Type dataHandle(const Operation *operation)
return reference to data handle object
ConstDofIteratorType dbegin() const
obtain an iterator pointing to the first DoF (read-only)
Definition: discretefunction.hh:265
void write(OutStreamInterface< StreamTraits > &out) const
write the discrete function into a stream
Definition: discretefunction.hh:508
Double abs(const Double &a)
Definition: double.hh:860
std::string spaceName(const DFSpaceIdentifier id)
Definition: discretefunctionspace.hh:102
DiscreteFunctionType & operator*=(const RangeFieldType &scalar)
multiply all DoFs with a scalar factor
Definition: discretefunction_inline.hh:199
void print(std::ostream &out) const
print all DoFs to a stream (for debugging purposes)
Definition: discretefunction.hh:410
Definition: coordinate.hh:4
bool operator==(const DiscreteFunctionInterface< DFType > &g) const
Definition: discretefunction_inline.hh:323
void print(std::ostream &out) const
print all DoFs to a stream (for debugging purposes)
Definition: discretefunction_inline.hh:83
virtual void addBackupRestore()=0
please doc me
Traits::DiscreteFunctionType DiscreteFunctionType
type of the implementaton (Barton-Nackman)
Definition: discretefunction.hh:94
abstract interface for an input stream
Definition: streams.hh:177
void freeDofPointerNoCopy(const RangeFieldType *dofPointer) const
allocate a pointer to a consecutive array storing the DoFs
Definition: discretefunction_inline.hh:74
void read(InStreamInterface< StreamTraits > &in)
read the discrete function from a stream
Definition: discretefunction.hh:498
BaseType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: discretefunction.hh:571
DiscreteFunctionType & operator+=(const DiscreteFunctionInterface< DFType > &g)
add another discrete function to this one
Definition: discretefunction_inline.hh:166
int size() const
obtain total number of DoFs
Definition: discretefunction.hh:226
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: streams.hh:311
STL namespace.
DiscreteFunctionSpaceType::GridPartType GridPartType
type of the underlying grid part
Definition: discretefunction.hh:114
capability for persistent index sets
Definition: persistentindexset.hh:90
virtual void removeSubData()
Definition: discretefunction_inline.hh:304
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
type of range field, i.e. dof type
Definition: discretefunction.hh:105
Traits::LocalDofVectorType LocalDofVectorType
type of LocalDofVector
Definition: discretefunction.hh:599
OutStreamInterface< StreamTraits > & operator<<(OutStreamInterface< StreamTraits > &out, const DiscreteFunctionInterface< Impl > &df)
write a discrete function into an output stream
Definition: discretefunction_inline.hh:375
static constexpr PersistentIndexSetInterface * map(IndexSet &indexSet) noexcept
please doc me
Definition: persistentindexset.hh:101
Traits::ConstDofIteratorType ConstDofIteratorType
Type of the constantdof iterator used in the discrete function implementation.
Definition: discretefunction.hh:129
void freeDofPointer(RangeFieldType *dofPointer)
allocate a pointer to a consecutive array storing the DoFs
Definition: discretefunction_inline.hh:59
virtual base class for persistent index sets
Definition: persistentindexset.hh:34
abstract interface for an output stream
Definition: streams.hh:44
Definition: discretefunction.hh:81
DiscreteFunctionDefault(const std::string &name, const DiscreteFunctionSpaceType &dfSpace)
Constructor storing discrete function space and local function factory.
Definition: discretefunction_inline.hh:25