18#ifdef DEAL_II_WITH_PETSC
35 VectorReference::operator PetscScalar()
const
49 VecGetOwnershipRange(vector.vector, &begin, &end);
52 Vec locally_stored_elements =
nullptr;
53 ierr = VecGhostGetLocalForm(vector.vector, &locally_stored_elements);
57 ierr = VecGetSize(locally_stored_elements, &lsize);
60 const PetscScalar *ptr;
61 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
66 if (index >=
static_cast<size_type
>(begin) &&
67 index <
static_cast<size_type
>(end))
70 value = *(ptr + index - begin);
75 Assert(vector.ghost_indices.is_element(index),
77 "You are trying to access an element of a vector "
78 "that is neither a locally owned element nor a "
79 "ghost element of the vector."));
80 const size_type ghostidx =
81 vector.ghost_indices.index_within_set(index);
84 value = *(ptr + ghostidx + end - begin);
87 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
91 VecGhostRestoreLocalForm(vector.vector, &locally_stored_elements);
103 PetscErrorCode ierr = VecGetOwnershipRange(vector.vector, &begin, &end);
106 AssertThrow((index >=
static_cast<size_type
>(begin)) &&
107 (index <
static_cast<size_type
>(end)),
108 ExcAccessToNonlocalElement(index, begin, end - 1));
110 const PetscScalar *ptr;
112 ierr = VecGetArrayRead(vector.vector, &ptr);
114 value = *(ptr + index - begin);
115 ierr = VecRestoreArrayRead(vector.vector, &ptr);
134 , ghost_indices(v.ghost_indices)
152 const PetscErrorCode ierr =
153 PetscObjectReference(
reinterpret_cast<PetscObject
>(
vector));
163 const PetscErrorCode ierr = VecDestroy(&
vector);
175 PetscErrorCode ierr =
176 PetscObjectReference(
reinterpret_cast<PetscObject
>(v));
178 ierr = VecDestroy(&
vector);
188 template <
typename Iterator,
typename OutType>
189 class ConvertingIterator
194 using difference_type =
195 typename std::iterator_traits<Iterator>::difference_type;
196 using value_type = OutType;
197 using pointer = OutType *;
198 using reference = OutType &;
199 using iterator_category = std::forward_iterator_tag;
201 ConvertingIterator(
const Iterator &iterator)
208 return static_cast<OutType
>(std::real(*m_iterator));
221 ConvertingIterator old = *
this;
227 operator==(
const ConvertingIterator &other)
const
229 return this->m_iterator == other.m_iterator;
233 operator!=(
const ConvertingIterator &other)
const
235 return this->m_iterator != other.m_iterator;
255 ierr = VecGhostGetLocalForm(
vector, &ghosted_vec);
257 if (ghosted_vec && ghosted_vec !=
vector)
261 PetscInt ghost_start_index, end_index, n_elements_stored_locally;
263 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
266 ierr = VecGetOwnershipRange(
vector, &ghost_start_index, &end_index);
270 ierr = VecGetArray(tvector, &array);
278 for (PetscInt i = 0; i < end_index - ghost_start_index; i++)
280 Assert(
static_cast<PetscInt
>(
static_cast<PetscScalar
>(
281 ghost_start_index + i)) == (ghost_start_index + i),
283 array[i] = ghost_start_index + i;
286 ierr = VecRestoreArray(tvector, &array);
288 ierr = VecGhostUpdateBegin(tvector, INSERT_VALUES, SCATTER_FORWARD);
290 ierr = VecGhostUpdateEnd(tvector, INSERT_VALUES, SCATTER_FORWARD);
292 ierr = VecGhostGetLocalForm(tvector, &ghosted_vec);
294 ierr = VecGetLocalSize(ghosted_vec, &n_elements_stored_locally);
296 ierr = VecGetArrayRead(ghosted_vec, (
const PetscScalar **)&array);
308 ConvertingIterator<PetscScalar *, types::global_dof_index> begin_ghosts(
309 &array[end_index - ghost_start_index]);
310 ConvertingIterator<PetscScalar *, types::global_dof_index> end_ghosts(
311 &array[n_elements_stored_locally]);
312 if (std::is_sorted(&array[end_index - ghost_start_index],
313 &array[n_elements_stored_locally],
314 [](PetscScalar left, PetscScalar right) {
315 return static_cast<PetscInt
>(std::real(left)) <
316 static_cast<PetscInt
>(std::real(right));
323 std::vector<PetscInt> sorted_indices(begin_ghosts, end_ghosts);
324 std::sort(sorted_indices.begin(), sorted_indices.end());
326 sorted_indices.end());
330 ierr = VecRestoreArrayRead(ghosted_vec, (
const PetscScalar **)&array);
332 ierr = VecGhostRestoreLocalForm(tvector, &ghosted_vec);
334 ierr = VecDestroy(&tvector);
339 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
348 const PetscErrorCode ierr = VecDestroy(&
vector);
363 PetscErrorCode ierr = VecCopy(v,
vector);
378 PetscErrorCode ierr = VecSet(
vector, s);
384 ierr = VecGhostGetLocalForm(
vector, &ghost);
387 ierr = VecSet(ghost, s);
390 ierr = VecGhostRestoreLocalForm(
vector, &ghost);
405 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
408 return (flag == PETSC_TRUE);
419 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
422 return (flag == PETSC_FALSE);
431 const PetscErrorCode ierr = VecGetSize(
vector, &sz);
443 const PetscErrorCode ierr = VecGetLocalSize(
vector, &sz);
455 const PetscErrorCode ierr = VecGetLocalSize(
vector, &sz);
463 std::pair<VectorBase::size_type, VectorBase::size_type>
467 const PetscErrorCode ierr =
468 VecGetOwnershipRange(
static_cast<const Vec &
>(
vector), &begin, &end);
471 return std::make_pair(begin, end);
478 const std::vector<PetscScalar> &values)
480 Assert(indices.size() == values.size(),
481 ExcMessage(
"Function called with arguments of different sizes"));
489 const std::vector<PetscScalar> &values)
491 Assert(indices.size() == values.size(),
492 ExcMessage(
"Function called with arguments of different sizes"));
500 const ::Vector<PetscScalar> &values)
502 Assert(indices.size() == values.size(),
503 ExcMessage(
"Function called with arguments of different sizes"));
512 const PetscScalar *values)
533 const PetscErrorCode ierr = VecDot(vec.
vector,
vector, &result);
557# ifdef DEAL_II_WITH_MPI
561 int all_int_last_action;
563 const int ierr = MPI_Allreduce(&my_int_last_action,
564 &all_int_last_action,
573 ExcMessage(
"Error: not all processors agree on the last "
574 "VectorOperation before this compress() call."));
582 "Missing compress() or calling with wrong VectorOperation argument."));
598 PetscErrorCode ierr = VecAssemblyBegin(
vector);
600 ierr = VecAssemblyEnd(
vector);
628 const PetscErrorCode ierr = VecSum(
vector, &sum);
630 return sum /
static_cast<PetscReal
>(
size());
635 const PetscScalar *start_ptr;
636 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
639 PetscScalar mean = 0;
641 PetscScalar sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
646 const PetscScalar *ptr = start_ptr;
647 const PetscScalar *eptr = ptr + (
size() / 4) * 4;
656 while (ptr != start_ptr +
size())
659 mean = (sum0 + sum1 + sum2 + sum3) /
static_cast<PetscReal
>(
size());
664 ierr = VecRestoreArrayRead(
vector, &start_ptr);
676 const PetscErrorCode ierr = VecNorm(
vector, NORM_1, &d);
689 const PetscErrorCode ierr = VecNorm(
vector, NORM_2, &d);
702 const PetscScalar *start_ptr;
703 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
708 real_type sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
713 const PetscScalar *ptr = start_ptr;
714 const PetscScalar *eptr = ptr + (
size() / 4) * 4;
723 while (ptr != start_ptr +
size())
726 norm =
std::pow(sum0 + sum1 + sum2 + sum3, 1. / p);
731 ierr = VecRestoreArrayRead(
vector, &start_ptr);
744 const PetscErrorCode ierr = VecNorm(
vector, NORM_INFINITY, &d);
758 const PetscErrorCode ierr = VecMin(
vector, &p, &d);
771 const PetscErrorCode ierr = VecMax(
vector, &p, &d);
784 const PetscScalar *start_ptr;
785 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
788 const PetscScalar *ptr = start_ptr,
803 ierr = VecRestoreArrayRead(
vector, &start_ptr);
812 template <
typename T>
821 template <
typename T>
827 "whether it is non-negative."))
return true;
838 const PetscScalar *start_ptr;
839 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
842 const PetscScalar *ptr = start_ptr,
857 ierr = VecRestoreArrayRead(
vector, &start_ptr);
871 const PetscErrorCode ierr = VecScale(
vector, a);
885 const PetscScalar factor = 1. / a;
888 const PetscErrorCode ierr = VecScale(
vector, factor);
900 const PetscErrorCode ierr = VecAXPY(
vector, 1, v);
912 const PetscErrorCode ierr = VecAXPY(
vector, -1, v);
926 const PetscErrorCode ierr = VecShift(
vector, s);
938 const PetscErrorCode ierr = VecAXPY(
vector, a, v);
954 const PetscScalar weights[2] = {a, b};
955 Vec addends[2] = {v.
vector, w.vector};
957 const PetscErrorCode ierr = VecMAXPY(
vector, 2, weights, addends);
969 const PetscErrorCode ierr = VecAYPX(
vector, s, v);
997 const PetscErrorCode ierr = VecPointwiseMult(
vector, factors,
vector);
1011 const PetscErrorCode ierr = VecAXPBY(
vector, a, 0.0, v.
vector);
1024 PetscErrorCode ierr =
1025 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_(
comm), format);
1029 ierr = VecView(
vector, PETSC_VIEWER_STDOUT_(
comm));
1037 const unsigned int precision,
1038 const bool scientific,
1039 const bool across)
const
1045 const PetscScalar *val;
1046 PetscErrorCode ierr = VecGetArrayRead(
vector, &val);
1051 const std::ios::fmtflags old_flags = out.flags();
1052 const unsigned int old_precision = out.precision(precision);
1054 out.precision(precision);
1056 out.setf(std::ios::scientific, std::ios::floatfield);
1058 out.setf(std::ios::fixed, std::ios::floatfield);
1062 out << val[i] <<
' ';
1065 out << val[i] << std::endl;
1069 out.flags(old_flags);
1070 out.precision(old_precision);
1074 ierr = VecRestoreArrayRead(
vector, &val);
1095 VectorBase::operator
const Vec &()
const
1111 std::size_t mem =
sizeof(Vec) +
sizeof(
last_action) +
1133 const PetscScalar *values,
1134 const bool add_values)
1139 internal::VectorReference::ExcWrongMode(action,
last_action));
1147 if (n_elements != 0)
1149 const PetscInt *petsc_indices =
1150 reinterpret_cast<const PetscInt *
>(indices);
1152 const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES);
1153 const PetscErrorCode ierr =
1154 VecSetValues(
vector, n_elements, petsc_indices, values, mode);
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
size_type n_elements() const
void set_size(const size_type size)
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
real_type lp_norm(const real_type p) const
real_type l1_norm() const
VectorBase & operator+=(const VectorBase &V)
PetscScalar mean_value() const
void determine_ghost_indices()
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
std::size_t memory_consumption() const
VectorBase & operator/=(const PetscScalar factor)
bool is_non_negative() const
MPI_Comm get_mpi_communicator() const
void scale(const VectorBase &scaling_factors)
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
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
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)
VectorBase & operator=(const VectorBase &)
real_type l2_norm() const
std::enable_if_t< std::is_floating_point< T >::value &&std::is_floating_point< U >::value, typename ProductType< std::complex< T >, std::complex< U > >::type > operator*(const std::complex< T > &left, const std::complex< U > &right)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertThrowMPI(error_code)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::enable_if_t< std::is_fundamental< T >::value, std::size_t > memory_consumption(const T &t)
bool is_non_negative(const T &t)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
SynchronousIterators< Iterators > operator++(SynchronousIterators< Iterators > &a)