16#ifndef dealii_petsc_vector_base_h
17# define dealii_petsc_vector_base_h
22# ifdef DEAL_II_WITH_PETSC
40template <
typename number>
93 VectorReference(
const VectorBase &vector,
const size_type index);
99 VectorReference(
const VectorReference &vector) =
default;
112 const VectorReference &
113 operator=(
const VectorReference &r)
const;
121 operator=(
const VectorReference &r);
126 const VectorReference &
127 operator=(
const PetscScalar &s)
const;
132 const VectorReference &
133 operator+=(
const PetscScalar &s)
const;
138 const VectorReference &
139 operator-=(
const PetscScalar &s)
const;
144 const VectorReference &
145 operator*=(
const PetscScalar &s)
const;
150 const VectorReference &
151 operator/=(
const PetscScalar &s)
const;
172 operator PetscScalar()
const;
177 ExcAccessToNonlocalElement,
181 <<
"You tried to access element " << arg1
182 <<
" of a distributed vector, but only elements in range [" << arg2
183 <<
',' << arg3 <<
"] are stored locally and can be accessed."
185 <<
"A common source for this kind of problem is that you "
186 <<
"are passing a 'fully distributed' vector into a function "
187 <<
"that needs read access to vector elements that correspond "
188 <<
"to degrees of freedom on ghost cells (or at least to "
189 <<
"'locally active' degrees of freedom that are not also "
190 <<
"'locally owned'). You need to pass a vector that has these "
191 <<
"elements as ghost entries.");
198 <<
"You tried to do a "
199 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
200 <<
" operation but the vector is currently in "
201 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
202 <<
" mode. You first have to call 'compress()'.");
208 const VectorBase &vector;
217 friend class ::PETScWrappers::VectorBase;
386 std::pair<size_type, size_type>
465 set(
const std::vector<size_type> & indices,
466 const std::vector<PetscScalar> &values);
485 std::vector<PetscScalar> & values)
const;
514 template <
typename ForwardIterator,
typename OutputIterator>
517 const ForwardIterator indices_end,
518 OutputIterator values_begin)
const;
525 add(
const std::vector<size_type> & indices,
526 const std::vector<PetscScalar> &values);
533 add(
const std::vector<size_type> & indices,
534 const ::Vector<PetscScalar> &values);
544 const PetscScalar *values);
691 add(
const PetscScalar s);
703 add(
const PetscScalar a,
718 sadd(
const PetscScalar s,
const PetscScalar a,
const VectorBase &V);
742 write_ascii(
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT);
752 print(std::ostream & out,
753 const unsigned int precision = 3,
754 const bool scientific =
true,
755 const bool across =
true)
const;
779 operator const Vec &()
const;
840 const PetscScalar *values,
841 const bool add_values);
864 inline VectorReference::VectorReference(
const VectorBase &vector,
865 const size_type index)
871 inline const VectorReference &
872 VectorReference::operator=(
const VectorReference &r)
const
878 *
this =
static_cast<PetscScalar
>(r);
885 inline VectorReference &
886 VectorReference::operator=(
const VectorReference &r)
892 *
this =
static_cast<PetscScalar
>(r);
899 inline const VectorReference &
900 VectorReference::operator=(
const PetscScalar &value)
const
908 const PetscInt petsc_i =
index;
910 const PetscErrorCode ierr =
911 VecSetValues(vector, 1, &petsc_i, &value, INSERT_VALUES);
921 inline const VectorReference &
922 VectorReference::operator+=(
const PetscScalar &value)
const
939 if (value == PetscScalar())
943 const PetscInt petsc_i =
index;
944 const PetscErrorCode ierr =
945 VecSetValues(vector, 1, &petsc_i, &value, ADD_VALUES);
954 inline const VectorReference &
955 VectorReference::operator-=(
const PetscScalar &value)
const
972 if (value == PetscScalar())
977 const PetscInt petsc_i =
index;
978 const PetscScalar subtractand = -
value;
979 const PetscErrorCode ierr =
980 VecSetValues(vector, 1, &petsc_i, &subtractand, ADD_VALUES);
988 inline const VectorReference &
989 VectorReference::operator*=(
const PetscScalar &value)
const
1009 const PetscInt petsc_i =
index;
1010 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) *
value;
1012 const PetscErrorCode ierr =
1013 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1021 inline const VectorReference &
1022 VectorReference::operator/=(
const PetscScalar &value)
const
1042 const PetscInt petsc_i =
index;
1043 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) /
value;
1045 const PetscErrorCode ierr =
1046 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1055 VectorReference::real()
const
1057# ifndef PETSC_USE_COMPLEX
1058 return static_cast<PetscScalar
>(*this);
1060 return PetscRealPart(
static_cast<PetscScalar
>(*
this));
1067 VectorReference::imag()
const
1069# ifndef PETSC_USE_COMPLEX
1070 return PetscReal(0);
1072 return PetscImaginaryPart(
static_cast<PetscScalar
>(*
this));
1079 VectorBase::in_local_range(
const size_type index)
const
1082 const PetscErrorCode ierr =
1083 VecGetOwnershipRange(
static_cast<const Vec &
>(vector), &begin, &end);
1086 return ((index >=
static_cast<size_type>(begin)) &&
1092 VectorBase::locally_owned_elements()
const
1097 const std::pair<size_type, size_type> x = local_range();
1098 is.add_range(x.first, x.second);
1105 VectorBase::has_ghost_elements()
const
1113 VectorBase::update_ghost_values()
const
1118 inline internal::VectorReference
1119 VectorBase::operator()(
const size_type index)
1121 return internal::VectorReference(*
this, index);
1127 VectorBase::operator()(
const size_type index)
const
1129 return static_cast<PetscScalar
>(internal::VectorReference(*
this, index));
1134 inline internal::VectorReference
1135 VectorBase::operator[](
const size_type index)
1137 return operator()(index);
1143 VectorBase::operator[](
const size_type index)
const
1145 return operator()(index);
1149 VectorBase::get_mpi_communicator()
const
1152 PetscObjectGetComm(
reinterpret_cast<PetscObject
>(vector), &
comm);
1157 VectorBase::extract_subvector_to(
const std::vector<size_type> &indices,
1158 std::vector<PetscScalar> & values)
const
1162 extract_subvector_to(indices.begin(), indices.end(),
values.begin());
1165 template <
typename ForwardIterator,
typename OutputIterator>
1167 VectorBase::extract_subvector_to(
const ForwardIterator indices_begin,
1168 const ForwardIterator indices_end,
1169 OutputIterator values_begin)
const
1171 const PetscInt n_idx =
static_cast<PetscInt
>(indices_end - indices_begin);
1197 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1200 Vec locally_stored_elements =
nullptr;
1201 ierr = VecGhostGetLocalForm(vector, &locally_stored_elements);
1205 ierr = VecGetSize(locally_stored_elements, &lsize);
1209 ierr = VecGetArray(locally_stored_elements, &ptr);
1212 for (PetscInt i = 0; i < n_idx; ++i)
1214 const unsigned int index = *(indices_begin + i);
1215 if (index >=
static_cast<unsigned int>(begin) &&
1216 index < static_cast<unsigned int>(end))
1219 *(values_begin + i) = *(ptr + index - begin);
1224 const unsigned int ghostidx =
1225 ghost_indices.index_within_set(index);
1228 *(values_begin + i) = *(ptr + ghostidx + end - begin);
1232 ierr = VecRestoreArray(locally_stored_elements, &ptr);
1235 ierr = VecGhostRestoreLocalForm(vector, &locally_stored_elements);
1244 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1248 ierr = VecGetArray(vector, &ptr);
1251 for (PetscInt i = 0; i < n_idx; ++i)
1253 const unsigned int index = *(indices_begin + i);
1255 Assert(index >=
static_cast<unsigned int>(begin) &&
1256 index <
static_cast<unsigned int>(end),
1257 ExcMessage(
"You are accessing elements of a vector without "
1258 "ghost elements that are not actually owned by "
1259 "this vector. A typical case where this may "
1260 "happen is if you are passing a non-ghosted "
1261 "(completely distributed) vector to a function "
1262 "that expects a vector that stores ghost "
1263 "elements for all locally relevant or locally "
1264 "active vector entries."));
1266 *(values_begin + i) = *(ptr + index - begin);
1269 ierr = VecRestoreArray(vector, &ptr);
real_type lp_norm(const real_type p) const
real_type l1_norm() const
VectorBase & operator+=(const VectorBase &V)
PetscScalar mean_value() const
size_type local_size() const
VectorOperation::values last_action
PetscScalar operator*(const VectorBase &vec) const
bool operator==(const VectorBase &v) const
VectorBase & operator*=(const PetscScalar factor)
VectorBase & operator-=(const VectorBase &V)
bool operator!=(const VectorBase &v) const
IndexSet locally_owned_elements() const
bool in_local_range(const size_type index) const
std::size_t memory_consumption() const
internal::VectorReference reference
VectorBase & operator/=(const PetscScalar factor)
bool is_non_negative() const
friend class internal::VectorReference
PetscScalar operator[](const size_type index) const
virtual const MPI_Comm & get_mpi_communicator() const
void scale(const VectorBase &scaling_factors)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< PetscScalar > &values) const
void update_ghost_values() const
std::pair< size_type, size_type > local_range() const
void sadd(const PetscScalar s, const VectorBase &V)
real_type norm_sqr() const
void compress(const VectorOperation::values operation)
void set(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
PetscScalar add_and_dot(const PetscScalar a, const VectorBase &V, const VectorBase &W)
virtual ~VectorBase() override
real_type linfty_norm() const
reference operator()(const size_type index)
const internal::VectorReference const_reference
void add(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
bool has_ghost_elements() const
reference operator[](const size_type index)
size_type locally_owned_size() const
void equ(const PetscScalar a, const VectorBase &V)
void do_set_add_operation(const size_type n_elements, const size_type *indices, const PetscScalar *values, const bool add_values)
real_type l2_norm() const
VectorBase & operator=(const VectorBase &)=delete
void swap(VectorBase &u, VectorBase &v)
PetscScalar operator()(const size_type index) const
void extract_subvector_to(const ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define DeclException2(Exception2, type1, type2, outsequence)
#define AssertIndexRange(index, range)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int global_dof_index