Reference documentation for deal.II version GIT 90d86f7ad9 2022-05-28 14:10:02+00:00
\(\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 - 2022 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 <vector>
38 
40 
41 
42 // Forward declarations
43 #ifndef DOXYGEN
44 # ifdef DEAL_II_WITH_PETSC
45 namespace PETScWrappers
46 {
47  class VectorBase;
48 }
49 # endif
50 
51 # ifdef DEAL_II_WITH_TRILINOS
52 namespace TrilinosWrappers
53 {
54  namespace MPI
55  {
56  class Vector;
57  }
58 } // namespace TrilinosWrappers
59 # endif
60 
61 template <typename number>
62 class LAPACKFullMatrix;
63 
64 template <typename>
65 class BlockVector;
66 
67 namespace parallel
68 {
69  namespace internal
70  {
71  class TBBPartitioner;
72  }
73 } // namespace parallel
74 #endif
75 
76 
107 template <typename Number>
108 class Vector : public Subscriptor
109 {
110 public:
111  // The assertion in vector.templates.h for whether or not a number is
112  // finite is not compatible for AD number types.
113  static_assert(
115  "The Vector class does not support auto-differentiable numbers.");
116 
121  using value_type = Number;
122  using pointer = value_type *;
123  using const_pointer = const value_type *;
124  using iterator = value_type *;
125  using const_iterator = const value_type *;
127  using const_reference = const value_type &;
129 
140 
149 
160 
165  Vector(Vector<Number> &&v) noexcept = default;
166 
175  template <typename OtherNumber>
176  explicit Vector(const Vector<OtherNumber> &v);
177 
193  template <typename OtherNumber>
194  explicit Vector(const std::initializer_list<OtherNumber> &v);
195 
196 #ifdef DEAL_II_WITH_PETSC
208  explicit Vector(const PETScWrappers::VectorBase &v);
209 #endif
210 
211 #ifdef DEAL_II_WITH_TRILINOS
227 #endif
228 
238  explicit Vector(const size_type n);
239 
244  template <typename InputIterator>
245  Vector(const InputIterator first, const InputIterator last);
246 
251  virtual ~Vector() override = default;
252 
266  void
268  ::VectorOperation::unknown) const;
269 
285  virtual void
286  reinit(const size_type N, const bool omit_zeroing_entries = false);
287 
311  void
313 
321  void
322  apply_givens_rotation(const std::array<Number, 3> &csr,
323  const size_type i,
324  const size_type k);
325 
333  template <typename Number2>
334  void
335  reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
336 
352  virtual void
354 
366  operator=(const Number s);
367 
375 
382  operator=(Vector<Number> &&v) noexcept = default;
383 
389  template <typename Number2>
392 
398 
399 #ifdef DEAL_II_WITH_PETSC
413 #endif
414 
415 
416 #ifdef DEAL_II_WITH_TRILINOS
434 #endif
435 
441  template <typename Number2>
442  bool
443  operator==(const Vector<Number2> &v) const;
444 
450  template <typename Number2>
451  bool
452  operator!=(const Vector<Number2> &v) const;
453 
455 
456 
461 
475  template <typename Number2>
476  Number
477  operator*(const Vector<Number2> &V) const;
478 
486  real_type
487  norm_sqr() const;
488 
496  Number
497  mean_value() const;
498 
506  real_type
507  l1_norm() const;
508 
517  real_type
518  l2_norm() const;
519 
528  real_type
529  lp_norm(const real_type p) const;
530 
534  real_type
535  linfty_norm() const;
536 
561  Number
562  add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
563 
565 
566 
571 
575  pointer
576  data();
577 
582  data() const;
583 
589  iterator
590  begin();
591 
596  begin() const;
597 
601  iterator
602  end();
603 
609  end() const;
610 
614  Number
615  operator()(const size_type i) const;
616 
620  Number &
622 
628  Number
629  operator[](const size_type i) const;
630 
636  Number &
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 
706 
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 
793  operator*=(const Number factor);
794 
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
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 
889  template <class Archive>
890  void
891  save(Archive &ar, const unsigned int version) const;
892 
898  template <class Archive>
899  void
900  load(Archive &ar, const unsigned int version);
901 
902 #ifdef DOXYGEN
908  template <class Archive>
909  void
910  serialize(Archive &archive, const unsigned int version);
911 #else
912  // This macro defines the serialize() method that is compatible with
913  // the templated save() and load() method that have been implemented.
914  BOOST_SERIALIZATION_SPLIT_MEMBER()
915 #endif
916 
925 
931  bool
933 
949  IndexSet
951 
955  size_type
956  size() const;
957 
965  size_type
967 
973  bool
974  all_zero() const;
975 
985  bool
987 
992  std::size_t
994 
1000  bool
1003 
1004 private:
1009 
1015  void
1017 
1021  void
1022  do_reinit(const size_type new_size,
1023  const bool omit_zeroing_entries,
1024  const bool reset_partitioner);
1025 
1030  mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1032 
1033  // Make all other vector types friends.
1034  template <typename Number2>
1035  friend class Vector;
1036 };
1037 
1039 /*----------------------- Inline functions ----------------------------------*/
1040 
1041 
1042 #ifndef DOXYGEN
1043 
1044 
1045 //------------------------ declarations for explicit specializations
1046 template <>
1048 Vector<int>::lp_norm(const real_type) const;
1049 
1050 
1051 //------------------------ inline functions
1052 
1053 template <typename Number>
1054 inline Vector<Number>::Vector()
1055 {
1056  // virtual functions called in constructors and destructors never use the
1057  // override in a derived class
1058  // for clarity be explicit on which function is called
1060 }
1061 
1062 
1063 
1064 template <typename Number>
1065 template <typename OtherNumber>
1066 Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1067  : Vector(v.begin(), v.end())
1068 {}
1069 
1070 
1071 
1072 template <typename Number>
1073 template <typename InputIterator>
1074 Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1075 {
1076  // allocate memory. do not initialize it, as we will copy over to it in a
1077  // second
1078  reinit(std::distance(first, last), true);
1079  std::copy(first, last, begin());
1080 }
1081 
1082 
1083 
1084 template <typename Number>
1085 inline Vector<Number>::Vector(const size_type n)
1086 {
1087  // virtual functions called in constructors and destructors never use the
1088  // override in a derived class
1089  // for clarity be explicit on which function is called
1090  Vector<Number>::reinit(n, false);
1091 }
1092 
1093 
1094 
1095 template <typename Number>
1096 inline typename Vector<Number>::size_type
1097 Vector<Number>::size() const
1098 {
1099  return values.size();
1100 }
1101 
1102 
1103 
1104 template <typename Number>
1105 inline typename Vector<Number>::size_type
1107 {
1108  return values.size();
1109 }
1110 
1111 
1112 
1113 template <typename Number>
1114 inline bool
1116 {
1117  return true;
1118 }
1119 
1120 
1121 
1122 template <typename Number>
1123 inline typename Vector<Number>::pointer
1125 {
1126  return values.data();
1127 }
1128 
1129 
1130 
1131 template <typename Number>
1132 inline typename Vector<Number>::const_pointer
1133 Vector<Number>::data() const
1134 {
1135  return values.data();
1136 }
1137 
1138 
1139 
1140 template <typename Number>
1141 inline typename Vector<Number>::iterator
1143 {
1144  return values.begin();
1145 }
1146 
1147 
1148 
1149 template <typename Number>
1150 inline typename Vector<Number>::const_iterator
1151 Vector<Number>::begin() const
1152 {
1153  return values.begin();
1154 }
1155 
1156 
1157 
1158 template <typename Number>
1159 inline typename Vector<Number>::iterator
1161 {
1162  return values.end();
1163 }
1164 
1165 
1166 
1167 template <typename Number>
1168 inline typename Vector<Number>::const_iterator
1169 Vector<Number>::end() const
1170 {
1171  return values.end();
1172 }
1173 
1174 
1175 
1176 template <typename Number>
1177 inline Number
1179 {
1180  AssertIndexRange(i, size());
1181  return values[i];
1182 }
1183 
1184 
1185 
1186 template <typename Number>
1187 inline Number &
1189 {
1190  AssertIndexRange(i, size());
1191  return values[i];
1192 }
1193 
1194 
1195 
1196 template <typename Number>
1197 inline Number
1199 {
1200  return operator()(i);
1201 }
1202 
1203 
1204 
1205 template <typename Number>
1206 inline Number &
1208 {
1209  return operator()(i);
1210 }
1211 
1212 
1213 
1214 template <typename Number>
1215 template <typename OtherNumber>
1216 inline void
1217 Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1218  std::vector<OtherNumber> & values) const
1219 {
1220  for (size_type i = 0; i < indices.size(); ++i)
1221  values[i] = operator()(indices[i]);
1222 }
1223 
1224 
1225 
1226 template <typename Number>
1227 template <typename ForwardIterator, typename OutputIterator>
1228 inline void
1229 Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1230  const ForwardIterator indices_end,
1231  OutputIterator values_begin) const
1232 {
1233  while (indices_begin != indices_end)
1234  {
1235  *values_begin = operator()(*indices_begin);
1236  indices_begin++;
1237  values_begin++;
1238  }
1239 }
1240 
1241 
1242 
1243 template <typename Number>
1244 inline Vector<Number> &
1245 Vector<Number>::operator/=(const Number factor)
1246 {
1247  AssertIsFinite(factor);
1248  Assert(factor != Number(0.), ExcZero());
1249 
1250  this->operator*=(Number(1.) / factor);
1251  return *this;
1252 }
1253 
1254 
1255 
1256 template <typename Number>
1257 template <typename OtherNumber>
1258 inline void
1259 Vector<Number>::add(const std::vector<size_type> & indices,
1260  const std::vector<OtherNumber> &values)
1261 {
1262  Assert(indices.size() == values.size(),
1263  ExcDimensionMismatch(indices.size(), values.size()));
1264  add(indices.size(), indices.data(), values.data());
1265 }
1266 
1267 
1268 
1269 template <typename Number>
1270 template <typename OtherNumber>
1271 inline void
1272 Vector<Number>::add(const std::vector<size_type> &indices,
1273  const Vector<OtherNumber> & values)
1274 {
1275  Assert(indices.size() == values.size(),
1276  ExcDimensionMismatch(indices.size(), values.size()));
1277  add(indices.size(), indices.data(), values.values.begin());
1278 }
1279 
1280 
1281 
1282 template <typename Number>
1283 template <typename OtherNumber>
1284 inline void
1285 Vector<Number>::add(const size_type n_indices,
1286  const size_type * indices,
1287  const OtherNumber *values)
1288 {
1289  for (size_type i = 0; i < n_indices; ++i)
1290  {
1291  AssertIndexRange(indices[i], size());
1292  Assert(
1294  ExcMessage(
1295  "The given value is not finite but either infinite or Not A Number (NaN)"));
1296 
1297  this->values[indices[i]] += values[i];
1298  }
1299 }
1300 
1301 
1302 
1303 template <typename Number>
1304 template <typename Number2>
1305 inline bool
1307 {
1308  return !(*this == v);
1309 }
1310 
1311 
1312 
1313 template <typename Number>
1315 {}
1316 
1317 
1318 template <typename Number>
1319 inline bool
1321 {
1322  return false;
1323 }
1324 
1325 template <typename Number>
1326 inline void
1328 {}
1329 
1330 
1331 
1332 // Moved from vector.templates.h as an inline function by Luca Heltai
1333 // on 2009/04/12 to prevent strange compiling errors, after making
1334 // swap virtual.
1335 template <typename Number>
1336 inline void
1338 {
1339  values.swap(v.values);
1341 }
1342 
1343 
1344 
1345 template <typename Number>
1346 template <class Archive>
1347 inline void
1348 Vector<Number>::save(Archive &ar, const unsigned int) const
1349 {
1350  // forward to serialization function in the base class.
1351  ar &static_cast<const Subscriptor &>(*this);
1352  ar &values;
1353 }
1354 
1355 
1356 
1357 template <typename Number>
1358 template <class Archive>
1359 inline void
1360 Vector<Number>::load(Archive &ar, const unsigned int)
1361 {
1362  // the load stuff again from the archive
1363  ar &static_cast<Subscriptor &>(*this);
1364  ar &values;
1366 }
1367 
1368 #endif
1369 
1370 
1383 template <typename Number>
1384 inline void
1386 {
1387  u.swap(v);
1388 }
1389 
1390 
1398 template <typename number>
1399 inline std::ostream &
1400 operator<<(std::ostream &out, const Vector<number> &v)
1401 {
1402  Assert(v.size() != 0, ExcEmptyObject());
1403  AssertThrow(out.fail() == false, ExcIO());
1404 
1405  for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1406  out << v(i) << ' ';
1407  out << v(v.size() - 1);
1408 
1409  AssertThrow(out.fail() == false, ExcIO());
1410 
1411  return out;
1412 }
1413 
1422 template <typename Number>
1423 struct is_serial_vector<Vector<Number>> : std::true_type
1424 {};
1425 
1426 
1428 
1429 #endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
Definition: vector.h:109
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:433
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:434
Point< 2 > first
Definition: grid_out.cc:4603
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertIsFinite(number)
Definition: exceptions.h:1758
static ::ExceptionBase & ExcEmptyObject()
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1583
typename numbers::NumberTraits< Number >::real_type real_type
Definition: vector.h:139
void extract_subvector_to(ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
bool operator!=(const Vector< Number2 > &v) const
const value_type * const_pointer
Definition: vector.h:123
void scale(const Vector< Number2 > &scaling_factors)
Vector< Number > & operator+=(const Vector< Number > &V)
bool has_ghost_elements() const
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
Number & operator()(const size_type i)
void add(const Number a, const Vector< Number > &V, const Number b, const Vector< Number > &W)
Vector(const TrilinosWrappers::MPI::Vector &v)
bool in_local_range(const size_type global_index) const
Number operator*(const Vector< Number2 > &V) const
const_pointer data() const
void do_reinit(const size_type new_size, const bool omit_zeroing_entries, const bool reset_partitioner)
void add(const Number s)
void sadd(const Number s, const Number a, const Vector< Number > &V)
Vector(const size_type n)
const value_type * const_iterator
Definition: vector.h:125
void block_write(std::ostream &out) const
Number mean_value() const
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
void load(Archive &ar, const unsigned int version)
const value_type & const_reference
Definition: vector.h:127
Vector< Number > & operator/=(const Number factor)
pointer data()
void reinit(const Vector< Number2 > &V, const bool omit_zeroing_entries=false)
Vector< Number > & operator=(const TrilinosWrappers::MPI::Vector &v)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
Number & operator[](const size_type i)
Vector< Number > & operator*=(const Number factor)
Vector(const Vector< Number > &v)
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:1031
void block_read(std::istream &in)
types::global_dof_index size_type
Definition: vector.h:128
Vector< Number > & operator=(const BlockVector< Number > &v)
Number operator[](const size_type i) const
Vector< Number > & operator=(const Vector< Number > &v)
void equ(const Number a, const Vector< Number > &u)
Vector< Number > & operator-=(const Vector< Number > &V)
void serialize(Archive &archive, const unsigned int version)
Vector(const PETScWrappers::VectorBase &v)
real_type lp_norm(const real_type p) const
const_iterator begin() const
void sadd(const Number s, const Vector< Number > &V)
Vector< Number > & operator=(const PETScWrappers::VectorBase &v)
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1385
bool operator==(const Vector< Number2 > &v) const
iterator end()
size_type size() const
void compress(::VectorOperation::values operation=::VectorOperation::unknown) const
friend class Vector
Definition: vector.h:1035
real_type l2_norm() const
value_type * pointer
Definition: vector.h:122
virtual void swap(Vector< Number > &v)
void grow_or_shrink(const size_type N)
virtual ~Vector() override=default
Vector(const Vector< OtherNumber > &v)
void save(Archive &ar, const unsigned int version) const
Vector< Number > & operator=(const Number s)
real_type linfty_norm() const
Vector< Number > & operator=(const Vector< Number2 > &v)
void scale(const Vector< Number > &scaling_factors)
void maybe_reset_thread_partitioner()
IndexSet locally_owned_elements() const
real_type norm_sqr() const
void add(const Number a, const Vector< Number > &V)
AlignedVector< Number > values
Definition: vector.h:1008
void equ(const Number a, const Vector< Number2 > &u)
void apply_givens_rotation(const std::array< Number, 3 > &csr, const size_type i, const size_type k)
const_iterator end() const
Vector(Vector< Number > &&v) noexcept=default
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
value_type * iterator
Definition: vector.h:124
void add(const size_type n_elements, const size_type *indices, const OtherNumber *values)
size_type locally_owned_size() const
Number operator()(const size_type i) const
bool is_non_negative() const
value_type & reference
Definition: vector.h:126
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
void update_ghost_values() const
Vector< Number > & operator=(Vector< Number > &&v) noexcept=default
bool all_zero() const
std::size_t memory_consumption() const
real_type l1_norm() const
void add(const std::vector< size_type > &indices, const Vector< OtherNumber > &values)
Number value_type
Definition: vector.h:121
iterator begin()
Vector(const InputIterator first, const InputIterator last)
Vector(const std::initializer_list< OtherNumber > &v)
static const char N
static const char V
types::global_dof_index size_type
Definition: cuda_kernels.h:45
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
VectorType::value_type * begin(VectorType &V)
VectorType::value_type * end(VectorType &V)
TrilinosWrappers::types::int_type global_index(const Epetra_BlockMap &map, const ::types::global_dof_index i)
void copy(const T *begin, const T *end, U *dest)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
Definition: matrix_block.h:618
bool is_finite(const double x)
Definition: numbers.h:539
unsigned int global_dof_index
Definition: types.h:76