17#ifdef DEAL_II_WITH_PETSC
34 VectorReference::operator PetscScalar()
const
48 VecGetOwnershipRange(vector.vector, &begin, &end);
51 Vec locally_stored_elements =
nullptr;
52 ierr = VecGhostGetLocalForm(vector.vector, &locally_stored_elements);
56 ierr = VecGetSize(locally_stored_elements, &lsize);
59 const PetscScalar *ptr;
60 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
65 if (index >=
static_cast<size_type
>(begin) &&
66 index <
static_cast<size_type
>(end))
69 value = *(ptr + index - begin);
74 Assert(vector.ghost_indices.is_element(index),
76 "You are trying to access an element of a vector "
77 "that is neither a locally owned element nor a "
78 "ghost element of the vector."));
79 const size_type ghostidx =
80 vector.ghost_indices.index_within_set(index);
83 value = *(ptr + ghostidx + end - begin);
86 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
90 VecGhostRestoreLocalForm(vector.vector, &locally_stored_elements);
102 PetscErrorCode ierr = VecGetOwnershipRange(vector.vector, &begin, &end);
105 AssertThrow((index >=
static_cast<size_type
>(begin)) &&
106 (index <
static_cast<size_type
>(end)),
107 ExcAccessToNonlocalElement(index, begin, end - 1));
109 const PetscScalar *ptr;
111 ierr = VecGetArrayRead(vector.vector, &ptr);
113 value = *(ptr + index - begin);
114 ierr = VecRestoreArrayRead(vector.vector, &ptr);
132 , ghost_indices(v.ghost_indices)
149 const PetscErrorCode ierr =
150 PetscObjectReference(
reinterpret_cast<PetscObject
>(
vector));
160 const PetscErrorCode ierr = VecDestroy(&
vector);
172 PetscErrorCode ierr =
173 PetscObjectReference(
reinterpret_cast<PetscObject
>(v));
175 ierr = VecDestroy(&
vector);
185 template <
typename Iterator,
typename OutType>
186 class ConvertingIterator
191 using difference_type =
192 typename std::iterator_traits<Iterator>::difference_type;
193 using value_type = OutType;
194 using pointer = OutType *;
195 using reference = OutType &;
196 using iterator_category = std::forward_iterator_tag;
198 ConvertingIterator(
const Iterator &iterator)
205 return static_cast<OutType
>(std::real(*m_iterator));
218 ConvertingIterator old = *
this;
224 operator==(
const ConvertingIterator &other)
const
226 return this->m_iterator == other.m_iterator;
230 operator!=(
const ConvertingIterator &other)
const
232 return this->m_iterator != other.m_iterator;
252 ierr = VecGhostGetLocalForm(
vector, &ghosted_vec);
254 if (ghosted_vec && ghosted_vec !=
vector)
258 PetscInt ghost_start_index, end_index, n_elements_stored_locally;
260 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
263 ierr = VecGetOwnershipRange(
vector, &ghost_start_index, &end_index);
265 ierr = VecDuplicate(
vector, &tvector);
267 ierr = VecGetArray(tvector, &array);
275 for (PetscInt i = 0; i < end_index - ghost_start_index; i++)
277 Assert(
static_cast<PetscInt
>(std::real(
static_cast<PetscScalar
>(
278 ghost_start_index + i))) == (ghost_start_index + i),
280 array[i] = ghost_start_index + i;
283 ierr = VecRestoreArray(tvector, &array);
285 ierr = VecGhostUpdateBegin(tvector, INSERT_VALUES, SCATTER_FORWARD);
287 ierr = VecGhostUpdateEnd(tvector, INSERT_VALUES, SCATTER_FORWARD);
289 ierr = VecGhostGetLocalForm(tvector, &ghosted_vec);
291 ierr = VecGetLocalSize(ghosted_vec, &n_elements_stored_locally);
293 ierr = VecGetArrayRead(ghosted_vec, (
const PetscScalar **)&array);
305 ConvertingIterator<PetscScalar *, types::global_dof_index> begin_ghosts(
306 &array[end_index - ghost_start_index]);
307 ConvertingIterator<PetscScalar *, types::global_dof_index> end_ghosts(
308 &array[n_elements_stored_locally]);
309 if (std::is_sorted(&array[end_index - ghost_start_index],
310 &array[n_elements_stored_locally],
311 [](PetscScalar left, PetscScalar right) {
312 return static_cast<PetscInt
>(std::real(left)) <
313 static_cast<PetscInt
>(std::real(right));
320 std::vector<PetscInt> sorted_indices(begin_ghosts, end_ghosts);
321 std::sort(sorted_indices.begin(), sorted_indices.end());
323 sorted_indices.end());
327 ierr = VecRestoreArrayRead(ghosted_vec, (
const PetscScalar **)&array);
329 ierr = VecGhostRestoreLocalForm(tvector, &ghosted_vec);
331 ierr = VecDestroy(&tvector);
336 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
345 const PetscErrorCode ierr = VecDestroy(&
vector);
360 PetscErrorCode ierr = VecCopy(v,
vector);
375 PetscErrorCode ierr = VecSet(
vector, s);
381 ierr = VecGhostGetLocalForm(
vector, &ghost);
384 ierr = VecSet(ghost, s);
387 ierr = VecGhostRestoreLocalForm(
vector, &ghost);
402 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
405 return (flag == PETSC_TRUE);
416 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
419 return (flag == PETSC_FALSE);
428 const PetscErrorCode ierr = VecGetSize(
vector, &sz);
440 const PetscErrorCode ierr = VecGetLocalSize(
vector, &sz);
448 std::pair<VectorBase::size_type, VectorBase::size_type>
452 const PetscErrorCode ierr =
453 VecGetOwnershipRange(
static_cast<const Vec &
>(
vector), &begin, &end);
456 return std::make_pair(begin, end);
463 const std::vector<PetscScalar> &values)
465 Assert(indices.size() == values.size(),
466 ExcMessage(
"Function called with arguments of different sizes"));
474 const std::vector<PetscScalar> &values)
476 Assert(indices.size() == values.size(),
477 ExcMessage(
"Function called with arguments of different sizes"));
485 const ::Vector<PetscScalar> &values)
487 Assert(indices.size() == values.size(),
488 ExcMessage(
"Function called with arguments of different sizes"));
497 const PetscScalar *values)
518 const PetscErrorCode ierr = VecDot(vec.
vector,
vector, &result);
541 ExcMessage(
"Calling compress() is only useful if a vector "
542 "has been written into, but this is a vector with ghost "
543 "elements and consequently is read-only. It does "
544 "not make sense to call compress() for such "
552 int all_int_last_action;
554 const int ierr = MPI_Allreduce(&my_int_last_action,
555 &all_int_last_action,
564 ExcMessage(
"Error: not all processors agree on the last "
565 "VectorOperation before this compress() call."));
572 "Missing compress() or calling with wrong VectorOperation argument."));
588 PetscErrorCode ierr = VecAssemblyBegin(
vector);
590 ierr = VecAssemblyEnd(
vector);
618 const PetscErrorCode ierr = VecSum(
vector, &sum);
620 return sum /
static_cast<PetscReal
>(
size());
625 const PetscScalar *start_ptr;
626 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
629 PetscScalar mean = 0;
631 PetscScalar sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
636 const PetscScalar *ptr = start_ptr;
651 static_cast<PetscReal
>(
size());
656 ierr = VecRestoreArrayRead(
vector, &start_ptr);
668 const PetscErrorCode ierr = VecNorm(
vector, NORM_1, &d);
681 const PetscErrorCode ierr = VecNorm(
vector, NORM_2, &d);
694 const PetscScalar *start_ptr;
695 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
700 real_type sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
705 const PetscScalar *ptr = start_ptr;
725 ierr = VecRestoreArrayRead(
vector, &start_ptr);
738 const PetscErrorCode ierr = VecNorm(
vector, NORM_INFINITY, &d);
751 const PetscScalar *start_ptr;
752 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
755 const PetscScalar *ptr = start_ptr,
770 ierr = VecRestoreArrayRead(
vector, &start_ptr);
779 template <
typename T>
788 template <
typename T>
794 "whether it is non-negative."));
807 const PetscErrorCode ierr = VecScale(
vector, a);
821 const PetscScalar factor = 1. / a;
824 const PetscErrorCode ierr = VecScale(
vector, factor);
836 const PetscErrorCode ierr = VecAXPY(
vector, 1, v);
848 const PetscErrorCode ierr = VecAXPY(
vector, -1, v);
862 const PetscErrorCode ierr = VecShift(
vector, s);
874 const PetscErrorCode ierr = VecAXPY(
vector, a, v);
890 const PetscScalar weights[2] = {a, b};
891 Vec addends[2] = {v.
vector, w.vector};
893 const PetscErrorCode ierr = VecMAXPY(
vector, 2, weights, addends);
905 const PetscErrorCode ierr = VecAYPX(
vector, s, v);
933 const PetscErrorCode ierr = VecPointwiseMult(
vector, factors,
vector);
947 const PetscErrorCode ierr = VecAXPBY(
vector, a, 0.0, v.
vector);
960 PetscErrorCode ierr =
961 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(
comm), format);
965 ierr = VecView(
vector, PETSC_VIEWER_STDOUT_(
comm));
967 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(
comm));
975 const unsigned int precision,
976 const bool scientific,
977 const bool across)
const
983 const PetscScalar *val;
984 PetscErrorCode ierr = VecGetArrayRead(
vector, &val);
989 const std::ios::fmtflags old_flags = out.flags();
990 const unsigned int old_precision = out.precision(precision);
992 out.precision(precision);
994 out.setf(std::ios::scientific, std::ios::floatfield);
996 out.setf(std::ios::fixed, std::ios::floatfield);
1000 out << val[i] <<
' ';
1003 out << val[i] << std::endl;
1007 out.flags(old_flags);
1008 out.precision(old_precision);
1012 ierr = VecRestoreArrayRead(
vector, &val);
1023 std::swap(this->vector, v.vector);
1024 std::swap(this->ghosted, v.ghosted);
1025 std::swap(this->last_action, v.last_action);
1028 this->ghost_indices = v.ghost_indices;
1029 v.ghost_indices = t;
1033 VectorBase::operator
const Vec &()
const
1049 std::size_t mem =
sizeof(Vec) +
sizeof(
last_action) +
1071 const PetscScalar *values,
1072 const bool add_values)
1077 internal::VectorReference::ExcWrongMode(action,
last_action));
1080 std::vector<PetscInt> petsc_indices(n_elements);
1081 for (
size_type i = 0; i < n_elements; ++i)
1083 const auto petsc_index =
static_cast<PetscInt
>(indices[i]);
1085 petsc_indices[i] = petsc_index;
1088 const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES);
1089 const PetscErrorCode ierr = VecSetValues(
1090 vector, petsc_indices.size(), petsc_indices.data(), 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()
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)
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 swap(VectorBase &v) noexcept
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
size_type size() const override
std::enable_if_t< std::is_floating_point_v< T > &&std::is_floating_point_v< U >, 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)
#define AssertIntegerConversion(index1, index2)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
bool is_non_negative(const T &t)
T sum(const T &t, const MPI_Comm mpi_communicator)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
SynchronousIterators< Iterators > operator++(SynchronousIterators< Iterators > &a)