Reference documentation for deal.II version 9.4.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 - 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
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
45namespace PETScWrappers
46{
47 class VectorBase;
48}
49# endif
50
51# ifdef DEAL_II_WITH_TRILINOS
52namespace TrilinosWrappers
53{
54 namespace MPI
55 {
56 class Vector;
57 }
58} // namespace TrilinosWrappers
59# endif
60
61template <typename number>
63
64template <typename>
65class BlockVector;
66
67namespace parallel
68{
69 namespace internal
70 {
71 class TBBPartitioner;
72 }
73} // namespace parallel
74#endif
75
76
107template <typename Number>
108class Vector : public Subscriptor
109{
110public:
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;
123 using const_pointer = const value_type *;
125 using const_iterator = 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
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
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
444
450 template <typename Number2>
451 bool
453
455
456
461
475 template <typename Number2>
476 Number
478
487 norm_sqr() const;
488
496 Number
497 mean_value() const;
498
507 l1_norm() const;
508
518 l2_norm() const;
519
529 lp_norm(const real_type p) const;
530
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
577
582 data() const;
583
591
596 begin() const;
597
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
951
956 size() const;
957
967
973 bool
974 all_zero() const;
975
985 bool
987
992 std::size_t
994
1000 bool
1003
1004private:
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
1046template <>
1048Vector<int>::lp_norm(const real_type) const;
1049
1050
1051//------------------------ inline functions
1052
1053template <typename Number>
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
1064template <typename Number>
1065template <typename OtherNumber>
1066Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1067 : Vector(v.begin(), v.end())
1068{}
1069
1070
1071
1072template <typename Number>
1073template <typename InputIterator>
1074Vector<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
1084template <typename Number>
1085inline 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
1095template <typename Number>
1096inline typename Vector<Number>::size_type
1098{
1099 return values.size();
1100}
1101
1102
1103
1104template <typename Number>
1105inline typename Vector<Number>::size_type
1107{
1108 return values.size();
1109}
1110
1111
1112
1113template <typename Number>
1114inline bool
1116{
1117 return true;
1118}
1119
1120
1121
1122template <typename Number>
1123inline typename Vector<Number>::pointer
1125{
1126 return values.data();
1127}
1128
1129
1130
1131template <typename Number>
1132inline typename Vector<Number>::const_pointer
1134{
1135 return values.data();
1136}
1137
1138
1139
1140template <typename Number>
1141inline typename Vector<Number>::iterator
1143{
1144 return values.begin();
1145}
1146
1147
1148
1149template <typename Number>
1150inline typename Vector<Number>::const_iterator
1152{
1153 return values.begin();
1154}
1155
1156
1157
1158template <typename Number>
1159inline typename Vector<Number>::iterator
1161{
1162 return values.end();
1163}
1164
1165
1166
1167template <typename Number>
1168inline typename Vector<Number>::const_iterator
1169Vector<Number>::end() const
1170{
1171 return values.end();
1172}
1173
1174
1175
1176template <typename Number>
1177inline Number
1179{
1180 AssertIndexRange(i, size());
1181 return values[i];
1182}
1183
1184
1185
1186template <typename Number>
1187inline Number &
1189{
1190 AssertIndexRange(i, size());
1191 return values[i];
1192}
1193
1194
1195
1196template <typename Number>
1197inline Number
1199{
1200 return operator()(i);
1201}
1202
1203
1204
1205template <typename Number>
1206inline Number &
1208{
1209 return operator()(i);
1210}
1211
1212
1213
1214template <typename Number>
1215template <typename OtherNumber>
1216inline void
1217Vector<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
1226template <typename Number>
1227template <typename ForwardIterator, typename OutputIterator>
1228inline void
1229Vector<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
1243template <typename Number>
1244inline Vector<Number> &
1245Vector<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
1256template <typename Number>
1257template <typename OtherNumber>
1258inline void
1259Vector<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
1269template <typename Number>
1270template <typename OtherNumber>
1271inline void
1272Vector<Number>::add(const std::vector<size_type> &indices,
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
1282template <typename Number>
1283template <typename OtherNumber>
1284inline void
1285Vector<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
1303template <typename Number>
1304template <typename Number2>
1305inline bool
1307{
1308 return !(*this == v);
1309}
1310
1311
1312
1313template <typename Number>
1315{}
1316
1317
1318template <typename Number>
1319inline bool
1321{
1322 return false;
1323}
1324
1325template <typename Number>
1326inline 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.
1335template <typename Number>
1336inline void
1338{
1339 values.swap(v.values);
1341}
1342
1343
1344
1345template <typename Number>
1346template <class Archive>
1347inline void
1348Vector<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
1357template <typename Number>
1358template <class Archive>
1359inline void
1360Vector<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
1383template <typename Number>
1384inline void
1386{
1387 u.swap(v);
1388}
1389
1390
1398template <typename number>
1399inline std::ostream &
1400operator<<(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
1422template <typename Number>
1423struct 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:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
Point< 2 > first
Definition: grid_out.cc:4603
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcEmptyObject()
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertIsFinite(number)
Definition: exceptions.h:1758
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1583
Vector< Number > & operator=(const PETScWrappers::VectorBase &v)
typename numbers::NumberTraits< Number >::real_type real_type
Definition: vector.h:139
Number & operator[](const size_type i)
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)
bool has_ghost_elements() const
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
void add(const Number a, const Vector< Number > &V, const Number b, const Vector< Number > &W)
Vector(const TrilinosWrappers::MPI::Vector &v)
Number & operator()(const size_type i)
Vector< Number > & operator=(const TrilinosWrappers::MPI::Vector &v)
bool in_local_range(const size_type global_index) const
Vector< Number > & operator=(const Number s)
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)
Vector< Number > & operator=(const BlockVector< Number > &v)
void sadd(const Number s, const Number a, const Vector< Number > &V)
Vector(const size_type n)
Vector< Number > & operator=(Vector< Number > &&v) noexcept=default
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
pointer data()
void reinit(const Vector< Number2 > &V, const bool omit_zeroing_entries=false)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
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
Number operator[](const size_type i) const
Vector< Number > & operator*=(const Number factor)
void equ(const Number a, const Vector< Number > &u)
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)
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
real_type linfty_norm() const
Vector< Number > & operator=(const Vector< Number > &v)
Vector< Number > & operator+=(const Vector< Number > &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)
Vector< Number > & operator=(const Vector< Number2 > &v)
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
Vector< Number > & operator-=(const Vector< Number > &V)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
void update_ghost_values() const
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)
Vector< Number > & operator/=(const Number factor)
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 * end(VectorType &V)
VectorType::value_type * begin(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