2 #ifndef DUNE_FEM_PETSCDOFBLOCK_HH 3 #define DUNE_FEM_PETSCDOFBLOCK_HH 22 template <
class PVector >
29 template<
class PVector >
33 typedef PVector PetscVectorType;
34 typedef typename PetscDofBlock< PetscVectorType >::DofProxy ThisType;
35 typedef typename PetscDofBlock< PetscVectorType >::IndexType IndexType;
37 static const unsigned int blockSize = PetscVectorType::blockSize;
40 PetscDofProxy ( PetscScalar s = 0 )
47 PetscDofProxy ( PetscVectorType &petscVector, IndexType blockIndex, PetscInt indexInBlock )
48 : petscVector_( &petscVector ),
49 blockIndex_( blockIndex ),
50 indexInBlock_( indexInBlock )
54 void assign (
const ThisType &other )
56 petscVector_ = other.petscVector_;
57 blockIndex_ = other.blockIndex_;
58 indexInBlock_ = other.indexInBlock_;
61 const ThisType& operator= ( ThisType &other )
63 setValue( other.getValue() );
67 const ThisType& operator= ( PetscScalar val )
73 const ThisType& operator*= (
const ThisType& other )
75 PetscScalar value = getValue() * other.getValue();
80 const ThisType& operator*= (
const PetscScalar scalar )
82 PetscScalar value = getValue() * scalar ;
87 const ThisType& operator+= (
const ThisType &other )
89 setValue( other.getValue(), ADD_VALUES );
93 const ThisType& operator+= (
const PetscScalar scalar )
95 setValue( scalar, ADD_VALUES );
99 const ThisType& operator-= (
const ThisType &other )
101 setValue( -other.getValue(), ADD_VALUES );
105 const ThisType& operator-= (
const PetscScalar scalar )
107 setValue( -scalar, ADD_VALUES );
112 operator PetscScalar ()
const 114 return valid() ? getValue() : PetscScalar( 0 );
117 bool valid()
const {
return bool( petscVector_ ); }
120 PetscScalar getValue ()
const 123 PetscInt index = blockSize*petscVector().dofMapping().localSlaveMapping( blockIndex_ ) + indexInBlock_;
125 ::Dune::Petsc::VecGetValues( *petscVector().getGhostedVector(), 1, &index, &ret );
129 void setValue (
const PetscScalar &val, InsertMode mode = INSERT_VALUES )
131 PetscInt index = blockSize*petscVector().dofMapping().localSlaveMapping( blockIndex_ ) + indexInBlock_;
132 ::Dune::Petsc::VecSetValue( *( petscVector().getGhostedVector() ), index, val, mode );
133 petscVector().hasBeenModified();
136 PetscVectorType& petscVector ()
138 assert( petscVector_ );
139 return *petscVector_;
142 const PetscVectorType& petscVector ()
const 144 assert( petscVector_ );
145 return *petscVector_;
149 PetscVectorType *petscVector_;
150 IndexType blockIndex_;
151 PetscInt indexInBlock_;
161 template<
class PVector >
164 typedef PetscDofBlock< PVector > ThisType;
167 typedef PVector PetscVectorType;
168 typedef typename PetscVectorType::PetscDofMappingType PetscDofMappingType;
169 typedef typename PetscDofMappingType::IndexType IndexType;
171 static const unsigned int blockSize = PetscVectorType::blockSize;
173 typedef PetscDofProxy< PVector > DofProxy;
177 typedef std::pair< PetscVectorType&, IndexType > UnaryConstructorParamType;
180 PetscDofBlock ( PetscVectorType &petscVector, IndexType blockIndex )
181 : petscVector_( petscVector ),
182 blockIndex_( blockIndex )
186 PetscDofBlock (
const PetscDofBlock& other )
187 : petscVector_( other.petscVector_ ),
188 blockIndex_( other.blockIndex_ )
192 explicit PetscDofBlock ( UnaryConstructorParamType arg )
193 : petscVector_( arg.first ),
194 blockIndex_( arg.second )
197 const PetscDofBlock& operator*= (
const PetscScalar value )
199 for(
unsigned int i=0; i<blockSize; ++i )
200 (*
this)[ i ] *= value ;
209 IndexType size()
const {
return blockSize; }
211 DofProxy operator [] (
unsigned int index )
213 assert( index < blockSize );
214 return DofProxy( petscVector_, blockIndex_, index );
217 const DofProxy operator [] (
unsigned int index )
const 219 assert( index < blockSize );
220 return DofProxy( petscVector_, blockIndex_, index );
227 PetscVectorType &petscVector_;
228 IndexType blockIndex_;
233 template<
class Traits,
class PVector >
234 inline OutStreamInterface< Traits >&
235 operator<< ( OutStreamInterface< Traits > &out,
236 const PetscDofProxy< PVector >& value )
239 out << PetscScalar( value );
245 template<
class Traits,
class PVector >
246 inline InStreamInterface< Traits >&
248 PetscDofProxy< PVector > value )
268 template<
class PVector >
269 class PetscDofBlock< PVector >::DofIterator
270 :
public std::iterator< std::input_iterator_tag, PetscScalar >
272 typedef typename PetscDofBlock< PVector >::DofIterator ThisType;
273 typedef PetscDofBlock< PVector > DofBlockType;
275 typedef std::iterator< std::input_iterator_tag, PetscScalar > BaseType;
278 typedef std::shared_ptr< DofBlockType > DofBlockSharedPointer;
281 typedef PVector PetscVectorType;
282 typedef typename DofBlockType::DofProxy value_type;
285 DofIterator ( PetscVectorType &petscVector,
unsigned int blockIndex, PetscInt indexInBlock = 0 )
286 : petscVector_( petscVector ),
287 blockIndex_( blockIndex ),
288 indexInBlock_( indexInBlock )
291 assert( blockIndex <= petscVector_.dofMapping().size() );
294 if( blockIndex < petscVector_.dofMapping().size() )
300 bool operator== (
const ThisType &other )
const 302 return blockIndex_ == other.blockIndex_ &&
303 indexInBlock_ == other.indexInBlock_;
308 value_type
operator* () {
return block()[ indexInBlock_]; }
309 const value_type
operator* ()
const {
return block()[ indexInBlock_ ]; }
312 ThisType& operator++ () { increment();
return *
this; }
315 ThisType& operator-- () { decrement();
return *
this; }
321 PetscVectorType& petscVector () {
return petscVector_; }
326 if( static_cast< unsigned int >( indexInBlock_ ) >= DofBlockType::blockSize )
330 if( static_cast< unsigned int >( blockIndex_ ) < petscVector().dofMapping().size() )
337 assert( blockIndex_ > 0 );
338 if( indexInBlock_ == 0 )
340 indexInBlock_ = DofBlockType::blockSize - 1;
351 void resetBlockPtr () { blockPtr_.reset(
new DofBlockType( *petscVector().block( blockIndex_ ) ) ); }
353 DofBlockType& block ()
const {
return *blockPtr_.get(); }
358 PetscVectorType &petscVector_;
359 unsigned int blockIndex_;
360 PetscInt indexInBlock_;
361 DofBlockSharedPointer blockPtr_;
368 #endif // #if HAVE_PETSC 370 #endif // DUNE_FEM_PETSCDOFBLOCK_HH
InStreamInterface< StreamTraits > & operator>>(InStreamInterface< StreamTraits > &in, DiscreteFunctionInterface< Impl > &df)
read a discrete function from an input stream
Definition: discretefunction_inline.hh:395
bool operator!=(const Double &a, const Double &b)
Definition: double.hh:629
bool operator==(const Double &a, const Double &b)
Definition: double.hh:589
Definition: coordinate.hh:4
Double operator*(const Double &a, const Double &b)
Definition: double.hh:495