16 #ifndef dealii_petsc_vector_base_h 17 #define dealii_petsc_vector_base_h 20 #include <deal.II/base/config.h> 22 #ifdef DEAL_II_WITH_PETSC 24 # include <deal.II/base/subscriptor.h> 25 # include <deal.II/lac/exceptions.h> 26 # include <deal.II/lac/vector.h> 27 # include <deal.II/lac/vector_operation.h> 32 # include <petscvec.h> 33 # include <deal.II/base/index_set.h> 35 DEAL_II_NAMESPACE_OPEN
38 template <
typename number>
class Vector;
89 VectorReference (
const VectorBase &vector,
90 const size_type index);
106 const VectorReference &operator = (
const VectorReference &r)
const;
113 VectorReference &operator = (
const VectorReference &r);
118 const VectorReference &operator = (
const PetscScalar &s)
const;
123 const VectorReference &operator += (
const PetscScalar &s)
const;
128 const VectorReference &operator -= (
const PetscScalar &s)
const;
133 const VectorReference &operator *= (
const PetscScalar &s)
const;
138 const VectorReference &operator /= (
const PetscScalar &s)
const;
143 PetscReal real ()
const;
151 PetscReal imag ()
const;
157 operator PetscScalar ()
const;
163 <<
"You tried to access element " << arg1
164 <<
" of a distributed vector, but only elements " 165 << arg2 <<
" through " << arg3
166 <<
" are stored locally and can be accessed.");
172 <<
"You tried to do a " 177 <<
" operation but the vector is currently in " 182 <<
" mode. You first have to call 'compress()'.");
188 const VectorBase &vector;
199 friend class ::PETScWrappers::VectorBase;
242 typedef PetscReal real_type;
275 virtual void clear ();
321 size_type
size ()
const;
341 std::pair<size_type, size_type>
415 void set (
const std::vector<size_type> &indices,
416 const std::vector<PetscScalar> &values);
434 std::vector<PetscScalar> &values)
const;
463 template <
typename ForwardIterator,
typename OutputIterator>
465 const ForwardIterator indices_end,
466 OutputIterator values_begin)
const;
472 void add (
const std::vector<size_type> &indices,
473 const std::vector<PetscScalar> &values);
479 void add (
const std::vector<size_type> &indices,
480 const ::Vector<PetscScalar> &values);
487 void add (
const size_type n_elements,
488 const size_type *indices,
489 const PetscScalar *values);
528 real_type
lp_norm (
const real_type p)
const;
561 real_type
min ()
const;
566 real_type
max ()
const;
606 void add (
const PetscScalar s);
622 void sadd (
const PetscScalar s,
628 void sadd (
const PetscScalar s,
665 void write_ascii (
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT) ;
674 void print (std::ostream &out,
675 const unsigned int precision = 3,
676 const bool scientific =
true,
677 const bool across =
true)
const;
700 operator const Vec &()
const;
759 const size_type *indices,
760 const PetscScalar *values,
761 const bool add_values);
794 VectorReference::VectorReference (
const VectorBase &vector,
795 const size_type index)
803 const VectorReference &
804 VectorReference::operator = (
const VectorReference &r)
const 810 *
this =
static_cast<PetscScalar
> (r);
819 VectorReference::operator = (
const VectorReference &r)
825 *
this =
static_cast<PetscScalar
> (r);
833 const VectorReference &
834 VectorReference::operator = (
const PetscScalar &value)
const 840 vector.last_action));
844 const PetscInt petsc_i = index;
846 const PetscErrorCode ierr = VecSetValues (vector, 1, &petsc_i, &value,
858 const VectorReference &
859 VectorReference::operator += (
const PetscScalar &value)
const 865 vector.last_action));
878 if (value == PetscScalar())
882 const PetscInt petsc_i = index;
883 const PetscErrorCode ierr = VecSetValues (vector, 1, &petsc_i, &value,
894 const VectorReference &
895 VectorReference::operator -= (
const PetscScalar &value)
const 901 vector.last_action));
914 if (value == PetscScalar())
919 const PetscInt petsc_i = index;
920 const PetscScalar subtractand = -value;
921 const PetscErrorCode ierr = VecSetValues (vector, 1, &petsc_i, &subtractand,
931 const VectorReference &
932 VectorReference::operator *= (
const PetscScalar &value)
const 938 vector.last_action));
954 const PetscInt petsc_i = index;
955 const PetscScalar new_value
956 =
static_cast<PetscScalar
>(*this) * value;
958 const PetscErrorCode ierr = VecSetValues (vector, 1, &petsc_i, &new_value,
968 const VectorReference &
969 VectorReference::operator /= (
const PetscScalar &value)
const 975 vector.last_action));
991 const PetscInt petsc_i = index;
992 const PetscScalar new_value
993 =
static_cast<PetscScalar
>(*this) / value;
995 const PetscErrorCode ierr = VecSetValues (vector, 1, &petsc_i, &new_value,
1006 VectorReference::real ()
const 1008 #ifndef PETSC_USE_COMPLEX 1009 return static_cast<PetscScalar
>(*this);
1011 return PetscRealPart (static_cast<PetscScalar>(*
this));
1019 VectorReference::imag ()
const 1021 #ifndef PETSC_USE_COMPLEX 1022 return PetscReal (0);
1024 return PetscImaginaryPart (static_cast<PetscScalar>(*
this));
1032 VectorBase::in_local_range (
const size_type index)
const 1034 PetscInt begin, end;
1035 const PetscErrorCode ierr = VecGetOwnershipRange (static_cast<const Vec &>(vector),
1039 return ((index >= static_cast<size_type>(begin)) &&
1040 (index < static_cast<size_type>(end)));
1046 VectorBase::locally_owned_elements()
const 1051 const std::pair<size_type, size_type> x = local_range();
1052 is.add_range (x.first, x.second);
1060 VectorBase::has_ghost_elements()
const 1069 VectorBase::update_ghost_values ()
const 1075 internal::VectorReference
1076 VectorBase::operator () (
const size_type index)
1078 return internal::VectorReference (*
this, index);
1085 VectorBase::operator () (
const size_type index)
const 1087 return static_cast<PetscScalar
>(internal::VectorReference (*
this, index));
1093 internal::VectorReference
1094 VectorBase::operator [] (
const size_type index)
1096 return operator()(index);
1103 VectorBase::operator [] (
const size_type index)
const 1105 return operator()(index);
1110 VectorBase::get_mpi_communicator ()
const 1112 static MPI_Comm comm;
1113 PetscObjectGetComm((PetscObject)vector, &comm);
1118 void VectorBase::extract_subvector_to (
const std::vector<size_type> &indices,
1119 std::vector<PetscScalar> &values)
const 1121 extract_subvector_to(&(indices[0]), &(indices[0]) + indices.size(), &(values[0]));
1124 template <
typename ForwardIterator,
typename OutputIterator>
1126 void VectorBase::extract_subvector_to (
const ForwardIterator indices_begin,
1127 const ForwardIterator indices_end,
1128 OutputIterator values_begin)
const 1130 const PetscInt n_idx =
static_cast<PetscInt
>(indices_end - indices_begin);
1155 PetscInt begin, end;
1156 PetscErrorCode ierr = VecGetOwnershipRange (vector, &begin, &end);
1159 Vec locally_stored_elements =
nullptr;
1160 ierr = VecGhostGetLocalForm(vector, &locally_stored_elements);
1164 ierr = VecGetSize(locally_stored_elements, &lsize);
1168 ierr = VecGetArray(locally_stored_elements, &ptr);
1171 for (PetscInt i=0; i<n_idx; ++i)
1173 const unsigned int index = *(indices_begin+i);
1174 if ( index>=static_cast<unsigned int>(begin)
1175 && index<static_cast<unsigned int>(end) )
1178 *(values_begin+i) = *(ptr+index-begin);
1183 const unsigned int ghostidx
1184 = ghost_indices.index_within_set(index);
1187 *(values_begin+i) = *(ptr+ghostidx+end-begin);
1191 ierr = VecRestoreArray(locally_stored_elements, &ptr);
1194 ierr = VecGhostRestoreLocalForm(vector, &locally_stored_elements);
1203 PetscInt begin, end;
1204 PetscErrorCode ierr = VecGetOwnershipRange (vector, &begin, &end);
1208 ierr = VecGetArray(vector, &ptr);
1211 for (PetscInt i=0; i<n_idx; ++i)
1213 const unsigned int index = *(indices_begin+i);
1215 Assert(index>=static_cast<unsigned int>(begin)
1218 *(values_begin+i) = *(ptr+index-begin);
1221 ierr = VecRestoreArray(vector, &ptr);
1230 DEAL_II_NAMESPACE_CLOSE
1232 #endif // DEAL_II_WITH_PETSC reference operator[](const size_type index)
types::global_dof_index size_type
#define DeclException2(Exception2, type1, type2, outsequence)
VectorBase & operator-=(const VectorBase &V)
void sadd(const PetscScalar s, const VectorBase &V)
real_type l2_norm() const
void update_ghost_values() const
void ratio(const VectorBase &a, const VectorBase &b)
void swap(VectorBase &u, VectorBase &v)
bool is_non_negative() const
VectorBase & operator/=(const PetscScalar factor)
#define AssertThrow(cond, exc)
real_type norm_sqr() const
VectorBase & operator=(const PetscScalar s)
bool in_local_range(const size_type index) const
void scale(const VectorBase &scaling_factors)
real_type linfty_norm() const
std::pair< size_type, size_type > local_range() const
real_type lp_norm(const real_type p) const
size_type local_size() const
void compress(const VectorOperation::values operation)
unsigned int global_dof_index
void do_set_add_operation(const size_type n_elements, const size_type *indices, const PetscScalar *values, const bool add_values)
VectorOperation::values last_action
#define Assert(cond, exc)
PetscScalar add_and_dot(const PetscScalar a, const VectorBase &V, const VectorBase &W)
std::size_t memory_consumption() const
void equ(const PetscScalar a, const VectorBase &V)
friend class internal::VectorReference
VectorBase & operator*=(const PetscScalar factor)
real_type l1_norm() const
static ::ExceptionBase & ExcGhostsPresent()
VectorBase & operator+=(const VectorBase &V)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
PetscScalar operator*(const VectorBase &vec) const
bool has_ghost_elements() const
PetscScalar mean_value() const
reference operator()(const size_type index)
bool operator==(const VectorBase &v) const
IndexSet locally_owned_elements() const
void add(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< PetscScalar > &values) const
virtual const MPI_Comm & get_mpi_communicator() const
static ::ExceptionBase & ExcInternalError()
bool operator!=(const VectorBase &v) const