Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
vector.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2020 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_vector_h
17 #define dealii_vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 
24 #include <deal.II/base/index_set.h>
26 
28 
31 
32 #include <boost/serialization/split_member.hpp>
33 
34 #include <algorithm>
35 #include <initializer_list>
36 #include <iosfwd>
37 #include <iterator>
38 #include <vector>
39 
41 
42 
43 // Forward declarations
44 #ifndef DOXYGEN
45 # ifdef DEAL_II_WITH_PETSC
46 namespace PETScWrappers
47 {
48  class VectorBase;
49 }
50 # endif
51 
52 # ifdef DEAL_II_WITH_TRILINOS
53 namespace TrilinosWrappers
54 {
55  namespace MPI
56  {
57  class Vector;
58  }
59 } // namespace TrilinosWrappers
60 # endif
61 
62 template <typename number>
63 class LAPACKFullMatrix;
64 
65 template <typename>
66 class BlockVector;
67 
68 namespace parallel
69 {
70  namespace internal
71  {
72  class TBBPartitioner;
73  }
74 } // namespace parallel
75 #endif
76 
77 
110 template <typename Number>
111 class Vector : public Subscriptor
112 {
113 public:
114  // The assertion in vector.templates.h for whether or not a number is
115  // finite is not compatible for AD number types.
116  static_assert(
118  "The Vector class does not support auto-differentiable numbers.");
119 
124  using value_type = Number;
125  using pointer = value_type *;
126  using const_pointer = const value_type *;
127  using iterator = value_type *;
128  using const_iterator = const value_type *;
130  using const_reference = const value_type &;
132 
143 
151  Vector();
152 
162  Vector(const Vector<Number> &v);
163 
168  Vector(Vector<Number> &&v) noexcept = default;
169 
178  template <typename OtherNumber>
179  explicit Vector(const Vector<OtherNumber> &v);
180 
196  template <typename OtherNumber>
197  explicit Vector(const std::initializer_list<OtherNumber> &v);
198 
199 #ifdef DEAL_II_WITH_PETSC
200 
211  explicit Vector(const PETScWrappers::VectorBase &v);
212 #endif
213 
214 #ifdef DEAL_II_WITH_TRILINOS
215 
229  explicit Vector(const TrilinosWrappers::MPI::Vector &v);
230 #endif
231 
241  explicit Vector(const size_type n);
242 
247  template <typename InputIterator>
248  Vector(const InputIterator first, const InputIterator last);
249 
254  virtual ~Vector() override = default;
255 
269  void
270  compress(::VectorOperation::values operation =
271  ::VectorOperation::unknown) const;
272 
288  virtual void
289  reinit(const size_type N, const bool omit_zeroing_entries = false);
290 
314  void
315  grow_or_shrink(const size_type N);
316 
324  void
325  apply_givens_rotation(const std::array<Number, 3> &csr,
326  const size_type i,
327  const size_type k);
328 
336  template <typename Number2>
337  void
338  reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
339 
355  virtual void
356  swap(Vector<Number> &v);
357 
368  Vector<Number> &
369  operator=(const Number s);
370 
376  Vector<Number> &
377  operator=(const Vector<Number> &v);
378 
384  Vector<Number> &
385  operator=(Vector<Number> &&v) noexcept = default;
386 
392  template <typename Number2>
393  Vector<Number> &
394  operator=(const Vector<Number2> &v);
395 
399  Vector<Number> &
400  operator=(const BlockVector<Number> &v);
401 
402 #ifdef DEAL_II_WITH_PETSC
403 
414  Vector<Number> &
415  operator=(const PETScWrappers::VectorBase &v);
416 #endif
417 
418 
419 #ifdef DEAL_II_WITH_TRILINOS
420 
435  Vector<Number> &
436  operator=(const TrilinosWrappers::MPI::Vector &v);
437 #endif
438 
444  template <typename Number2>
445  bool
446  operator==(const Vector<Number2> &v) const;
447 
453  template <typename Number2>
454  bool
455  operator!=(const Vector<Number2> &v) const;
456 
458 
459 
464 
478  template <typename Number2>
479  Number operator*(const Vector<Number2> &V) const;
480 
488  real_type
489  norm_sqr() const;
490 
498  Number
499  mean_value() const;
500 
508  real_type
509  l1_norm() const;
510 
519  real_type
520  l2_norm() const;
521 
530  real_type
531  lp_norm(const real_type p) const;
532 
536  real_type
537  linfty_norm() const;
538 
563  Number
564  add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
565 
567 
568 
573 
577  pointer
578  data();
579 
584  data() const;
585 
591  iterator
592  begin();
593 
598  begin() const;
599 
603  iterator
604  end();
605 
611  end() const;
612 
616  Number
617  operator()(const size_type i) const;
618 
622  Number &
623  operator()(const size_type i);
624 
630  Number operator[](const size_type i) const;
631 
637  Number &operator[](const size_type i);
638 
654  template <typename OtherNumber>
655  void
656  extract_subvector_to(const std::vector<size_type> &indices,
657  std::vector<OtherNumber> & values) const;
658 
686  template <typename ForwardIterator, typename OutputIterator>
687  void
688  extract_subvector_to(ForwardIterator indices_begin,
689  const ForwardIterator indices_end,
690  OutputIterator values_begin) const;
692 
693 
698 
704  Vector<Number> &
705  operator+=(const Vector<Number> &V);
706 
712  Vector<Number> &
713  operator-=(const Vector<Number> &V);
714 
719  template <typename OtherNumber>
720  void
721  add(const std::vector<size_type> & indices,
722  const std::vector<OtherNumber> &values);
723 
728  template <typename OtherNumber>
729  void
730  add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
731 
737  template <typename OtherNumber>
738  void
739  add(const size_type n_elements,
740  const size_type * indices,
741  const OtherNumber *values);
742 
749  void
750  add(const Number s);
751 
757  void
758  add(const Number a,
759  const Vector<Number> &V,
760  const Number b,
761  const Vector<Number> &W);
762 
768  void
769  add(const Number a, const Vector<Number> &V);
770 
776  void
777  sadd(const Number s, const Vector<Number> &V);
778 
784  void
785  sadd(const Number s, const Number a, const Vector<Number> &V);
786 
792  Vector<Number> &
793  operator*=(const Number factor);
794 
800  Vector<Number> &
801  operator/=(const Number factor);
802 
810  void
811  scale(const Vector<Number> &scaling_factors);
812 
818  template <typename Number2>
819  void
820  scale(const Vector<Number2> &scaling_factors);
821 
827  void
828  equ(const Number a, const Vector<Number> &u);
829 
833  template <typename Number2>
834  void
835  equ(const Number a, const Vector<Number2> &u);
836 
841  void
842  update_ghost_values() const;
844 
845 
856  void
857  print(std::ostream & out,
858  const unsigned int precision = 3,
859  const bool scientific = true,
860  const bool across = true) const;
861 
867  void
868  block_write(std::ostream &out) const;
869 
881  void
882  block_read(std::istream &in);
883 
888  template <class Archive>
889  void
890  save(Archive &ar, const unsigned int version) const;
891 
896  template <class Archive>
897  void
898  load(Archive &ar, const unsigned int version);
899 
900 #ifdef DOXYGEN
901 
905  template <class Archive>
906  void
907  serialize(Archive &archive, const unsigned int version);
908 #else
909  // This macro defines the serialize() method that is compatible with
910  // the templated save() and load() method that have been implemented.
911  BOOST_SERIALIZATION_SPLIT_MEMBER()
912 #endif
913 
922 
928  bool
929  in_local_range(const size_type global_index) const;
930 
946  IndexSet
947  locally_owned_elements() const;
948 
952  size_type
953  size() const;
954 
960  bool
961  all_zero() const;
962 
972  bool
973  is_non_negative() const;
974 
979  std::size_t
980  memory_consumption() const;
981 
987  bool
988  has_ghost_elements() const;
990 
991 private:
996 
1002  void
1003  maybe_reset_thread_partitioner();
1004 
1008  void
1009  do_reinit(const size_type new_size,
1010  const bool omit_zeroing_entries,
1011  const bool reset_partitioner);
1012 
1017  mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1019 
1020  // Make all other vector types friends.
1021  template <typename Number2>
1022  friend class Vector;
1023 };
1024 
1026 /*----------------------- Inline functions ----------------------------------*/
1027 
1028 
1029 #ifndef DOXYGEN
1030 
1031 
1032 //------------------------ declarations for explicit specializations
1033 template <>
1035 Vector<int>::lp_norm(const real_type) const;
1036 
1037 
1038 //------------------------ inline functions
1039 
1040 template <typename Number>
1041 inline Vector<Number>::Vector()
1042 {
1043  // virtual functions called in constructors and destructors never use the
1044  // override in a derived class
1045  // for clarity be explicit on which function is called
1047 }
1048 
1049 
1050 
1051 template <typename Number>
1052 template <typename OtherNumber>
1053 Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1054  : Vector(v.begin(), v.end())
1055 {}
1056 
1057 
1058 
1059 template <typename Number>
1060 template <typename InputIterator>
1061 Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1062 {
1063  // allocate memory. do not initialize it, as we will copy over to it in a
1064  // second
1065  reinit(std::distance(first, last), true);
1066  std::copy(first, last, begin());
1067 }
1068 
1069 
1070 
1071 template <typename Number>
1072 inline Vector<Number>::Vector(const size_type n)
1073 {
1074  // virtual functions called in constructors and destructors never use the
1075  // override in a derived class
1076  // for clarity be explicit on which function is called
1077  Vector<Number>::reinit(n, false);
1078 }
1079 
1080 
1081 
1082 template <typename Number>
1083 inline typename Vector<Number>::size_type
1084 Vector<Number>::size() const
1085 {
1086  return values.size();
1087 }
1088 
1089 
1090 template <typename Number>
1091 inline bool
1093 {
1094  return true;
1095 }
1096 
1097 
1098 
1099 template <typename Number>
1100 inline typename Vector<Number>::pointer
1102 {
1103  return values.data();
1104 }
1105 
1106 
1107 
1108 template <typename Number>
1109 inline typename Vector<Number>::const_pointer
1110 Vector<Number>::data() const
1111 {
1112  return values.data();
1113 }
1114 
1115 
1116 
1117 template <typename Number>
1118 inline typename Vector<Number>::iterator
1120 {
1121  return values.begin();
1122 }
1123 
1124 
1125 
1126 template <typename Number>
1127 inline typename Vector<Number>::const_iterator
1128 Vector<Number>::begin() const
1129 {
1130  return values.begin();
1131 }
1132 
1133 
1134 
1135 template <typename Number>
1136 inline typename Vector<Number>::iterator
1138 {
1139  return values.end();
1140 }
1141 
1142 
1143 
1144 template <typename Number>
1145 inline typename Vector<Number>::const_iterator
1146 Vector<Number>::end() const
1147 {
1148  return values.end();
1149 }
1150 
1151 
1152 
1153 template <typename Number>
1154 inline Number
1156 {
1157  AssertIndexRange(i, size());
1158  return values[i];
1159 }
1160 
1161 
1162 
1163 template <typename Number>
1164 inline Number &
1166 {
1167  AssertIndexRange(i, size());
1168  return values[i];
1169 }
1170 
1171 
1172 
1173 template <typename Number>
1174 inline Number Vector<Number>::operator[](const size_type i) const
1175 {
1176  return operator()(i);
1177 }
1178 
1179 
1180 
1181 template <typename Number>
1182 inline Number &Vector<Number>::operator[](const size_type i)
1183 {
1184  return operator()(i);
1185 }
1186 
1187 
1188 
1189 template <typename Number>
1190 template <typename OtherNumber>
1191 inline void
1192 Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1193  std::vector<OtherNumber> & values) const
1194 {
1195  for (size_type i = 0; i < indices.size(); ++i)
1196  values[i] = operator()(indices[i]);
1197 }
1198 
1199 
1200 
1201 template <typename Number>
1202 template <typename ForwardIterator, typename OutputIterator>
1203 inline void
1204 Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1205  const ForwardIterator indices_end,
1206  OutputIterator values_begin) const
1207 {
1208  while (indices_begin != indices_end)
1209  {
1210  *values_begin = operator()(*indices_begin);
1211  indices_begin++;
1212  values_begin++;
1213  }
1214 }
1215 
1216 
1217 
1218 template <typename Number>
1219 inline Vector<Number> &
1220 Vector<Number>::operator/=(const Number factor)
1221 {
1222  AssertIsFinite(factor);
1223  Assert(factor != Number(0.), ExcZero());
1224 
1225  this->operator*=(Number(1.) / factor);
1226  return *this;
1227 }
1228 
1229 
1230 
1231 template <typename Number>
1232 template <typename OtherNumber>
1233 inline void
1234 Vector<Number>::add(const std::vector<size_type> & indices,
1235  const std::vector<OtherNumber> &values)
1236 {
1237  Assert(indices.size() == values.size(),
1238  ExcDimensionMismatch(indices.size(), values.size()));
1239  add(indices.size(), indices.data(), values.data());
1240 }
1241 
1242 
1243 
1244 template <typename Number>
1245 template <typename OtherNumber>
1246 inline void
1247 Vector<Number>::add(const std::vector<size_type> &indices,
1248  const Vector<OtherNumber> & values)
1249 {
1250  Assert(indices.size() == values.size(),
1251  ExcDimensionMismatch(indices.size(), values.size()));
1252  add(indices.size(), indices.data(), values.values.begin());
1253 }
1254 
1255 
1256 
1257 template <typename Number>
1258 template <typename OtherNumber>
1259 inline void
1260 Vector<Number>::add(const size_type n_indices,
1261  const size_type * indices,
1262  const OtherNumber *values)
1263 {
1264  for (size_type i = 0; i < n_indices; ++i)
1265  {
1266  AssertIndexRange(indices[i], size());
1267  Assert(
1268  numbers::is_finite(values[i]),
1269  ExcMessage(
1270  "The given value is not finite but either infinite or Not A Number (NaN)"));
1271 
1272  this->values[indices[i]] += values[i];
1273  }
1274 }
1275 
1276 
1277 
1278 template <typename Number>
1279 template <typename Number2>
1280 inline bool
1281 Vector<Number>::operator!=(const Vector<Number2> &v) const
1282 {
1283  return !(*this == v);
1284 }
1285 
1286 
1287 
1288 template <typename Number>
1290 {}
1291 
1292 
1293 template <typename Number>
1294 inline bool
1296 {
1297  return false;
1298 }
1299 
1300 template <typename Number>
1301 inline void
1303 {}
1304 
1305 
1306 
1307 // Moved from vector.templates.h as an inline function by Luca Heltai
1308 // on 2009/04/12 to prevent strange compiling errors, after making
1309 // swap virtual.
1310 template <typename Number>
1311 inline void
1312 Vector<Number>::swap(Vector<Number> &v)
1313 {
1314  values.swap(v.values);
1315  std::swap(thread_loop_partitioner, v.thread_loop_partitioner);
1316 }
1317 
1318 
1319 
1320 template <typename Number>
1321 template <class Archive>
1322 inline void
1323 Vector<Number>::save(Archive &ar, const unsigned int) const
1324 {
1325  // forward to serialization function in the base class.
1326  ar &static_cast<const Subscriptor &>(*this);
1327  ar &values;
1328 }
1329 
1330 
1331 
1332 template <typename Number>
1333 template <class Archive>
1334 inline void
1335 Vector<Number>::load(Archive &ar, const unsigned int)
1336 {
1337  // the load stuff again from the archive
1338  ar &static_cast<Subscriptor &>(*this);
1339  ar &values;
1341 }
1342 
1343 #endif
1344 
1345 
1359 template <typename Number>
1360 inline void
1361 swap(Vector<Number> &u, Vector<Number> &v)
1362 {
1363  u.swap(v);
1364 }
1365 
1366 
1374 template <typename number>
1375 inline std::ostream &
1376 operator<<(std::ostream &out, const Vector<number> &v)
1377 {
1378  Assert(v.size() != 0, ExcEmptyObject());
1379  AssertThrow(out, ExcIO());
1380 
1381  for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1382  out << v(i) << ' ';
1383  out << v(v.size() - 1);
1384 
1385  AssertThrow(out, ExcIO());
1386 
1387  return out;
1388 }
1389 
1399 template <typename Number>
1400 struct is_serial_vector<Vector<Number>> : std::true_type
1401 {};
1402 
1403 
1405 
1406 #endif
void update_ghost_values() const
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
Definition: matrix_block.h:621
Number operator[](const size_type i) const
bool operator!=(const Vector< Number2 > &v) const
void load(Archive &ar, const unsigned int version)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
Number operator()(const size_type i) const
static ::ExceptionBase & ExcIO()
typename numbers::NumberTraits< typename VectorType::value_type >::real_type real_type
Definition: vector.h:142
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
std::vector< value_type > l2_norm(const typename ::Triangulation< dim, spacedim >::cell_iterator &parent, const value_type parent_value)
void scale(const double scaling_factor, Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:837
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
pointer data()
void swap(VectorBase &u, VectorBase &v)
__global__ void add_and_dot(Number *res, Number *v1, const Number *v2, const Number *v3, const Number a, const size_type N)
static const char V
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
pointer data()
iterator end()
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
bool is_finite(const double x)
Definition: numbers.h:537
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
static ::ExceptionBase & ExcMessage(std::string arg1)
bool has_ghost_elements() const
std::string compress(const std::string &input)
Definition: utilities.cc:392
typename VectorType::value_type value_type
Definition: vector.h:124
bool in_local_range(const size_type global_index) const
void maybe_reset_thread_partitioner()
#define Assert(cond, exc)
Definition: exceptions.h:1419
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
real_type lp_norm(const real_type p) const
Number linfty_norm(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2732
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
VectorType::value_type * end(VectorType &V)
Vector< Number > & operator*=(const Number factor)
iterator begin()
__global__ void equ(Number *val, const Number a, const Number *V_val, const size_type N)
__global__ void sadd(const Number s, Number *val, const Number a, const Number *V_val, const size_type N)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
Point< 2 > first
Definition: grid_out.cc:4352
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1361
unsigned int global_dof_index
Definition: types.h:76
virtual void swap(Vector< Number > &v)
Vector< Number > & operator/=(const Number factor)
void save(Archive &ar, const unsigned int version) const
iterator end()
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
VectorType::value_type * begin(VectorType &V)
iterator begin()
Number l1_norm(const Tensor< 2, dim, Number > &t)
Definition: tensor.h:2705
static const char N
static ::ExceptionBase & ExcEmptyObject()
void compress(::VectorOperation::values operation=::VectorOperation::unknown) const
size_type size() const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
AlignedVector< Number > values
Definition: vector.h:995
size_type size() const
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:1018
TrilinosWrappers::types::int_type global_index(const Epetra_BlockMap &map, const ::types::global_dof_index i)
std::enable_if< std::is_floating_point< T >::value &&std::is_floating_point< U >::value, typename ProductType< std::complex< T >, std::complex< U > >::type >::type operator*(const std::complex< T > &left, const std::complex< U > &right)
types::global_dof_index size_type
Definition: vector.h:131
static ::ExceptionBase & ExcZero()
void copy(const T *begin, const T *end, U *dest)
#define AssertIsFinite(number)
Definition: exceptions.h:1681
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)