1#ifndef DUNE_FEM_PETSCDOFBLOCK_HH
2#define DUNE_FEM_PETSCDOFBLOCK_HH
7#include <dune/fem/storage/envelope.hh>
11#include <dune/fem/misc/petsc/petsccommon.hh>
12#include <dune/fem/misc/petsc/petscvector.hh>
14#include <dune/fem/io/streams/streams.hh>
21 template <
class PVector >
28 template<
class PVector >
32 typedef PVector PetscVectorType;
33 typedef typename PetscDofBlock< PetscVectorType >::DofProxy ThisType;
34 typedef typename PetscDofBlock< PetscVectorType >::IndexType IndexType;
36 static const unsigned int blockSize = PetscVectorType::blockSize;
39 PetscDofProxy () =
default;
42 PetscDofProxy ( PetscScalar s ) {}
45 PetscDofProxy ( PetscVectorType &petscVector, IndexType blockIndex, PetscInt indexInBlock )
46 : petscVector_( &petscVector ),
47 blockIndex_( blockIndex ),
48 indexInBlock_( indexInBlock )
52 void assign (
const ThisType &other )
54 petscVector_ = other.petscVector_;
55 blockIndex_ = other.blockIndex_;
56 indexInBlock_ = other.indexInBlock_;
59 const ThisType& operator= ( ThisType &other )
61 setValue( other.getValue() );
65 const ThisType& operator= ( PetscScalar val )
71 const ThisType& operator*= (
const ThisType& other )
73 PetscScalar value = getValue() * other.getValue();
78 const ThisType& operator*= (
const PetscScalar scalar )
80 PetscScalar value = getValue() * scalar ;
85 const ThisType& operator+= (
const ThisType &other )
87 setValue( other.getValue(), ADD_VALUES );
91 const ThisType& operator+= (
const PetscScalar scalar )
93 setValue( scalar, ADD_VALUES );
97 const ThisType& operator-= (
const ThisType &other )
99 setValue( -other.getValue(), ADD_VALUES );
103 const ThisType& operator-= (
const PetscScalar scalar )
105 setValue( -scalar, ADD_VALUES );
110 operator PetscScalar ()
const
112 return valid() ? getValue() : PetscScalar( 0 );
115 bool valid()
const {
return bool( petscVector_ ); }
118 PetscScalar getValue ()
const
121 PetscInt index = blockSize * petscVector().mappers().ghostIndex( blockIndex_ ) + indexInBlock_;
123 ::Dune::Petsc::VecGetValues( *petscVector().getGhostedVector(), 1, &index, &ret );
127 void setValue (
const PetscScalar &val, InsertMode mode = INSERT_VALUES )
129 PetscInt index = blockSize * petscVector().mappers().ghostIndex( blockIndex_ ) + indexInBlock_;
130 ::Dune::Petsc::VecSetValue( *( petscVector().getGhostedVector() ), index, val, mode );
131 petscVector().hasBeenModified();
134 PetscVectorType& petscVector ()
136 assert( petscVector_ );
137 return *petscVector_;
140 const PetscVectorType& petscVector ()
const
142 assert( petscVector_ );
143 return *petscVector_;
147 PetscVectorType *petscVector_ =
nullptr;
148 IndexType blockIndex_ = 0;
149 PetscInt indexInBlock_ = 0;
152 template<
class Scalar,
class PVector >
153 void axpy (
const Scalar &a,
const Scalar &x, PetscDofProxy< PVector > proxy )
165 template<
class PVector >
168 typedef PetscDofBlock< PVector > ThisType;
171 typedef PVector PetscVectorType;
172 typedef PetscInt IndexType;
174 static const unsigned int blockSize = PetscVectorType::blockSize;
176 typedef PetscDofProxy< PVector > DofProxy;
180 typedef std::pair< PetscVectorType&, IndexType > UnaryConstructorParamType;
183 PetscDofBlock ( PetscVectorType &petscVector, IndexType blockIndex )
184 : petscVector_( petscVector ),
185 blockIndex_( blockIndex )
189 PetscDofBlock (
const PetscDofBlock& other )
190 : petscVector_( other.petscVector_ ),
191 blockIndex_( other.blockIndex_ )
195 explicit PetscDofBlock ( UnaryConstructorParamType arg )
196 : petscVector_( arg.first ),
197 blockIndex_( arg.second )
200 const PetscDofBlock& operator*= (
const PetscScalar value )
202 for(
unsigned int i=0; i<blockSize; ++i )
203 (*
this)[ i ] *= value ;
207 PetscDofBlock () =
delete;
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 );
224 PetscVectorType &petscVector_;
225 IndexType blockIndex_;
230 template<
class Traits,
class PVector >
231 inline OutStreamInterface< Traits >&
232 operator<< ( OutStreamInterface< Traits > &out,
233 const PetscDofProxy< PVector >& value )
236 out << PetscScalar( value );
242 template<
class Traits,
class PVector >
243 inline InStreamInterface< Traits >&
245 PetscDofProxy< PVector > value )
265 template<
class PVector >
266 class PetscDofBlock< PVector >::DofIterator
268 typedef typename PetscDofBlock< PVector >::DofIterator ThisType;
269 typedef PetscDofBlock< PVector > DofBlockType;
272 typedef std::shared_ptr< DofBlockType > DofBlockSharedPointer;
275 typedef std::input_iterator_tag iterator_category;
276 typedef typename DofBlockType::DofProxy value_type;
277 typedef std::ptrdiff_t difference_type;
278 typedef PetscScalar* pointer;
279 typedef PetscScalar& reference;
281 typedef PVector PetscVectorType;
284 DofIterator ( PetscVectorType &petscVector,
unsigned int blockIndex, PetscInt indexInBlock = 0 )
285 : petscVector_( petscVector ),
286 blockIndex_( blockIndex ),
287 indexInBlock_( indexInBlock )
290 assert(
static_cast< std::size_t
>( blockIndex ) <= petscVector_.mappers().size() );
293 if(
static_cast< std::size_t
>( blockIndex ) < petscVector_.mappers().size() )
299 bool operator== (
const ThisType &other )
const
301 return (blockIndex_ == other.blockIndex_) && (indexInBlock_ == other.indexInBlock_);
306 value_type operator* () {
return block()[ indexInBlock_]; }
307 const value_type operator* ()
const {
return block()[ indexInBlock_ ]; }
310 ThisType& operator++ () { increment();
return *
this; }
313 ThisType& operator-- () { decrement();
return *
this; }
315 DofIterator () =
delete;
318 PetscVectorType& petscVector () {
return petscVector_; }
323 if(
static_cast< std::size_t
>( indexInBlock_ ) >= DofBlockType::blockSize )
327 if(
static_cast< std::size_t
>( blockIndex_ ) < petscVector().mappers().
size() )
334 assert( blockIndex_ > 0 );
335 if( indexInBlock_ == 0 )
337 indexInBlock_ = DofBlockType::blockSize - 1;
348 void resetBlockPtr () { blockPtr_.reset(
new DofBlockType( *petscVector().block( blockIndex_ ) ) ); }
350 DofBlockType& block ()
const {
return *blockPtr_.get(); }
352 PetscVectorType &petscVector_;
353 unsigned int blockIndex_;
354 PetscInt indexInBlock_;
355 DofBlockSharedPointer blockPtr_;
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:238
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:260
typename Overloads::ScalarType< std::decay_t< V > >::type Scalar
Element type of some SIMD type.
Definition: interface.hh:235
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447