Reference documentation for deal.II version GIT relicensing-1295-g157fa2deae 2024-07-24 13:00: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\}}\)
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 1999 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_vector_h
16#define dealii_vector_h
17
18
19#include <deal.II/base/config.h>
20
26
30
31#include <boost/serialization/split_member.hpp>
32
33#include <algorithm>
34#include <initializer_list>
35#include <iosfwd>
36#include <vector>
37
39
40
41// Forward declarations
42#ifndef DOXYGEN
43# ifdef DEAL_II_WITH_PETSC
44namespace PETScWrappers
45{
46 class VectorBase;
47}
48# endif
49
50# ifdef DEAL_II_WITH_TRILINOS
51namespace TrilinosWrappers
52{
53 namespace MPI
54 {
55 class Vector;
56 }
57} // namespace TrilinosWrappers
58# endif
59
60# ifdef DEAL_II_TRILINOS_WITH_TPETRA
61namespace LinearAlgebra
62{
63 namespace TpetraWrappers
64 {
65 template <typename Number, typename MemorySpace>
66 class Vector;
67 }
68} // namespace LinearAlgebra
69# endif
70
71template <typename number>
73
74template <typename>
75class BlockVector;
76
77namespace parallel
78{
79 namespace internal
80 {
81 class TBBPartitioner;
82 }
83} // namespace parallel
84#endif
85
86
118template <typename Number>
119class Vector : public Subscriptor, public ReadVector<Number>
120{
121public:
129 static_assert(
130 std::is_arithmetic_v<typename numbers::NumberTraits<Number>::real_type>,
131 "The Vector class only supports basic numeric types. In particular, it "
132 "does not support automatically differentiated numbers.");
133
138 using value_type = Number;
140 using const_pointer = const value_type *;
142 using const_iterator = const value_type *;
146
157
166
177
182 Vector(Vector<Number> &&v) noexcept = default;
183
192 template <typename OtherNumber>
193 explicit Vector(const Vector<OtherNumber> &v);
194
210 template <typename OtherNumber>
211 explicit Vector(const std::initializer_list<OtherNumber> &v);
212
213#ifdef DEAL_II_WITH_PETSC
226#endif
227
228#ifdef DEAL_II_WITH_TRILINOS
244#endif
245
246#ifdef DEAL_II_TRILINOS_WITH_TPETRA
261 template <typename OtherNumber, typename MemorySpace>
262 explicit Vector(
264#endif
265
275 explicit Vector(const size_type n);
276
281 template <typename InputIterator>
282 Vector(const InputIterator first, const InputIterator last);
283
288 virtual ~Vector() override = default;
289
303 void
305
321 virtual void
322 reinit(const size_type N, const bool omit_zeroing_entries = false);
323
347 void
349
357 void
358 apply_givens_rotation(const std::array<Number, 3> &csr,
359 const size_type i,
360 const size_type k);
361
369 template <typename Number2>
370 void
371 reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
372
388 virtual void
389 swap(Vector<Number> &v) noexcept;
390
402 operator=(const Number s);
403
411
418 operator=(Vector<Number> &&v) noexcept = default;
419
425 template <typename Number2>
428
434
435#ifdef DEAL_II_WITH_PETSC
449#endif
450
451
452#ifdef DEAL_II_WITH_TRILINOS
470#endif
471
472#ifdef DEAL_II_TRILINOS_WITH_TPETRA
488 template <typename OtherNumber, typename MemorySpace>
492#endif
493
499 template <typename Number2>
500 bool
502
508 template <typename Number2>
509 bool
511
533 template <typename Number2>
534 Number
535 operator*(const Vector<Number2> &V) const;
536
545 norm_sqr() const;
546
554 Number
555 mean_value() const;
556
565 l1_norm() const;
566
576 l2_norm() const;
577
587 lp_norm(const real_type p) const;
588
593 linfty_norm() const;
594
619 Number
620 add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
621
633 pointer
635
640 data() const;
641
649
654 begin() const;
655
661
667 end() const;
668
672 Number
673 operator()(const size_type i) const;
674
678 Number &
680
686 Number
687 operator[](const size_type i) const;
688
694 Number &
696
712 template <typename OtherNumber>
713 void
714 extract_subvector_to(const std::vector<size_type> &indices,
715 std::vector<OtherNumber> &values) const;
716
720 virtual void
722 ArrayView<Number> &elements) const override;
723
751 template <typename ForwardIterator, typename OutputIterator>
752 void
753 extract_subvector_to(ForwardIterator indices_begin,
754 const ForwardIterator indices_end,
755 OutputIterator values_begin) const;
771
779
784 template <typename OtherNumber>
785 void
786 add(const std::vector<size_type> &indices,
787 const std::vector<OtherNumber> &values);
788
793 template <typename OtherNumber>
794 void
795 add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
796
802 template <typename OtherNumber>
803 void
804 add(const size_type n_elements,
805 const size_type *indices,
806 const OtherNumber *values);
807
814 void
815 add(const Number s);
816
822 void
823 add(const Number a,
824 const Vector<Number> &V,
825 const Number b,
826 const Vector<Number> &W);
827
833 void
834 add(const Number a, const Vector<Number> &V);
835
841 void
842 sadd(const Number s, const Vector<Number> &V);
843
849 void
850 sadd(const Number s, const Number a, const Vector<Number> &V);
851
858 operator*=(const Number factor);
859
866 operator/=(const Number factor);
867
875 void
876 scale(const Vector<Number> &scaling_factors);
877
883 template <typename Number2>
884 void
885 scale(const Vector<Number2> &scaling_factors);
886
892 void
893 equ(const Number a, const Vector<Number> &u);
894
898 template <typename Number2>
899 void
900 equ(const Number a, const Vector<Number2> &u);
901
906 void
921 void
922 print(std::ostream &out,
923 const unsigned int precision = 3,
924 const bool scientific = true,
925 const bool across = true) const;
926
932 void
933 block_write(std::ostream &out) const;
934
946 void
947 block_read(std::istream &in);
948
954 template <class Archive>
955 void
956 save(Archive &ar, const unsigned int version) const;
957
963 template <class Archive>
964 void
965 load(Archive &ar, const unsigned int version);
966
967#ifdef DOXYGEN
973 template <class Archive>
974 void
975 serialize(Archive &archive, const unsigned int version);
976#else
977 // This macro defines the serialize() method that is compatible with
978 // the templated save() and load() method that have been implemented.
979 BOOST_SERIALIZATION_SPLIT_MEMBER()
980#endif
981
996 bool
997 in_local_range(const size_type global_index) const;
998
1014 IndexSet
1016
1020 virtual size_type
1021 size() const override;
1022
1030 size_type
1032
1038 bool
1039 all_zero() const;
1040
1050 bool
1052
1057 std::size_t
1059
1065 bool
1067
1073 void
1077private:
1082
1088 void
1090
1094 void
1095 do_reinit(const size_type new_size,
1096 const bool omit_zeroing_entries,
1097 const bool reset_partitioner);
1098
1103 mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1105
1106 // Make all other vector types friends.
1107 template <typename Number2>
1108 friend class Vector;
1109};
1110
1112/*----------------------- Inline functions ----------------------------------*/
1113
1114
1115#ifndef DOXYGEN
1116
1117
1118//------------------------ declarations for explicit specializations
1119template <>
1121Vector<int>::lp_norm(const real_type) const;
1122
1123
1124//------------------------ inline functions
1125
1126template <typename Number>
1128{
1129 // virtual functions called in constructors and destructors never use the
1130 // override in a derived class
1131 // for clarity be explicit on which function is called
1133}
1134
1135
1136
1137template <typename Number>
1138template <typename OtherNumber>
1139Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1140 : Vector(v.begin(), v.end())
1141{}
1142
1143
1144
1145template <typename Number>
1146template <typename InputIterator>
1147Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1148{
1149 // allocate memory. do not initialize it, as we will copy over to it in a
1150 // second
1151 reinit(std::distance(first, last), true);
1152 std::copy(first, last, begin());
1153}
1154
1155
1156
1157template <typename Number>
1158inline Vector<Number>::Vector(const size_type n)
1159{
1160 // virtual functions called in constructors and destructors never use the
1161 // override in a derived class
1162 // for clarity be explicit on which function is called
1163 Vector<Number>::reinit(n, false);
1164}
1165
1166
1167
1168template <typename Number>
1169inline typename Vector<Number>::size_type
1171{
1172 return values.size();
1173}
1174
1175
1176
1177template <typename Number>
1178inline typename Vector<Number>::size_type
1180{
1181 return values.size();
1182}
1183
1184
1185
1186template <typename Number>
1187inline bool
1189{
1190 return true;
1191}
1192
1193
1194
1195template <typename Number>
1196inline typename Vector<Number>::pointer
1198{
1199 return values.data();
1200}
1201
1202
1203
1204template <typename Number>
1205inline typename Vector<Number>::const_pointer
1207{
1208 return values.data();
1209}
1210
1211
1212
1213template <typename Number>
1214inline typename Vector<Number>::iterator
1216{
1217 return values.begin();
1218}
1219
1220
1221
1222template <typename Number>
1223inline typename Vector<Number>::const_iterator
1225{
1226 return values.begin();
1227}
1228
1229
1230
1231template <typename Number>
1232inline typename Vector<Number>::iterator
1234{
1235 return values.end();
1236}
1237
1238
1239
1240template <typename Number>
1241inline typename Vector<Number>::const_iterator
1242Vector<Number>::end() const
1243{
1244 return values.end();
1245}
1246
1247
1248
1249template <typename Number>
1250inline Number
1252{
1253 AssertIndexRange(i, size());
1254 return values[i];
1255}
1256
1257
1258
1259template <typename Number>
1260inline Number &
1262{
1263 AssertIndexRange(i, size());
1264 return values[i];
1265}
1266
1267
1268
1269template <typename Number>
1270inline Number
1272{
1273 return operator()(i);
1274}
1275
1276
1277
1278template <typename Number>
1279inline Number &
1281{
1282 return operator()(i);
1283}
1284
1285
1286
1287template <typename Number>
1288template <typename OtherNumber>
1289inline void
1290Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1291 std::vector<OtherNumber> &values) const
1292{
1293 for (size_type i = 0; i < indices.size(); ++i)
1294 values[i] = operator()(indices[i]);
1295}
1296
1297
1298
1299template <typename Number>
1300template <typename ForwardIterator, typename OutputIterator>
1301inline void
1302Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1303 const ForwardIterator indices_end,
1304 OutputIterator values_begin) const
1305{
1306 while (indices_begin != indices_end)
1307 {
1308 *values_begin = operator()(*indices_begin);
1309 ++indices_begin;
1310 ++values_begin;
1311 }
1312}
1313
1314
1315
1316template <typename Number>
1317inline Vector<Number> &
1318Vector<Number>::operator/=(const Number factor)
1319{
1320 AssertIsFinite(factor);
1321 Assert(factor != Number(0.), ExcZero());
1322
1323 this->operator*=(Number(1.) / factor);
1324 return *this;
1325}
1326
1327
1328
1329template <typename Number>
1330template <typename OtherNumber>
1331inline void
1332Vector<Number>::add(const std::vector<size_type> &indices,
1333 const std::vector<OtherNumber> &values)
1334{
1335 Assert(indices.size() == values.size(),
1336 ExcDimensionMismatch(indices.size(), values.size()));
1337 add(indices.size(), indices.data(), values.data());
1338}
1339
1340
1341
1342template <typename Number>
1343template <typename OtherNumber>
1344inline void
1345Vector<Number>::add(const std::vector<size_type> &indices,
1347{
1348 Assert(indices.size() == values.size(),
1349 ExcDimensionMismatch(indices.size(), values.size()));
1350 add(indices.size(), indices.data(), values.values.begin());
1351}
1352
1353
1354
1355template <typename Number>
1356template <typename OtherNumber>
1357inline void
1358Vector<Number>::add(const size_type n_indices,
1359 const size_type *indices,
1360 const OtherNumber *values)
1361{
1362 for (size_type i = 0; i < n_indices; ++i)
1363 {
1364 AssertIndexRange(indices[i], size());
1365 Assert(
1367 ExcMessage(
1368 "The given value is not finite but either infinite or Not A Number (NaN)"));
1369
1370 this->values[indices[i]] += values[i];
1371 }
1372}
1373
1374
1375
1376template <typename Number>
1377template <typename Number2>
1378inline bool
1380{
1381 return !(*this == v);
1382}
1383
1384
1385
1386template <typename Number>
1387inline void
1389{}
1390
1391
1392
1393template <typename Number>
1394inline bool
1396{
1397 return false;
1398}
1399
1400
1401
1402template <typename Number>
1403inline void
1405{}
1406
1407
1408
1409template <typename Number>
1410inline void
1412{}
1413
1414
1415
1416template <typename Number>
1417template <typename Number2>
1418inline void
1420 const bool omit_zeroing_entries)
1421{
1422 // go to actual reinit functions in case we need to change something with
1423 // the vector, else there is nothing to be done
1424 if (!omit_zeroing_entries || size() != v.size())
1425 {
1426 do_reinit(v.size(), omit_zeroing_entries, false);
1428 }
1429}
1430
1431
1432
1433// Moved from vector.templates.h as an inline function by Luca Heltai
1434// on 2009/04/12 to prevent strange compiling errors, after making
1435// swap virtual.
1436template <typename Number>
1437inline void
1439{
1440 values.swap(v.values);
1442}
1443
1444
1445
1446template <typename Number>
1447template <class Archive>
1448inline void
1449Vector<Number>::save(Archive &ar, const unsigned int) const
1450{
1451 // forward to serialization function in the base class.
1452 ar &static_cast<const Subscriptor &>(*this);
1453 ar &values;
1454}
1455
1456
1457
1458template <typename Number>
1459template <class Archive>
1460inline void
1461Vector<Number>::load(Archive &ar, const unsigned int)
1462{
1463 // the load stuff again from the archive
1464 ar &static_cast<Subscriptor &>(*this);
1465 ar &values;
1467}
1468
1469#endif
1470
1471
1485template <typename Number>
1486inline void
1488{
1489 u.swap(v);
1490}
1491
1492
1500template <typename number>
1501inline std::ostream &
1502operator<<(std::ostream &out, const Vector<number> &v)
1503{
1504 Assert(v.size() != 0, ExcEmptyObject());
1505 AssertThrow(out.fail() == false, ExcIO());
1506
1507 for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1508 out << v(i) << ' ';
1509 out << v(v.size() - 1);
1510
1511 AssertThrow(out.fail() == false, ExcIO());
1512
1513 return out;
1514}
1515
1524template <typename Number>
1525struct is_serial_vector<Vector<Number>> : std::true_type
1526{};
1527
1528
1530
1531#endif
iterator end()
iterator begin()
pointer data()
void swap(AlignedVector< T > &vec) noexcept
size_type size() const
Vector< Number > & operator=(const PETScWrappers::VectorBase &v)
typename numbers::NumberTraits< Number >::real_type real_type
Definition vector.h:156
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:140
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:142
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:144
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:1104
void block_read(std::istream &in)
types::global_dof_index size_type
Definition vector.h:145
Number operator[](const size_type i) const
Vector< Number > & operator*=(const Number factor)
virtual size_type size() const override
void equ(const Number a, const Vector< Number > &u)
void zero_out_ghost_values() const
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)
bool operator==(const Vector< Number2 > &v) const
iterator end()
friend class Vector
Definition vector.h:1108
Vector(const LinearAlgebra::TpetraWrappers::Vector< OtherNumber, MemorySpace > &v)
real_type l2_norm() const
value_type * pointer
Definition vector.h:139
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)
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, ArrayView< Number > &elements) const override
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:1081
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:141
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
virtual void swap(Vector< Number > &v) noexcept
void compress(VectorOperation::values operation=VectorOperation::unknown) const
bool is_non_negative() const
value_type & reference
Definition vector.h:143
Vector< Number > & operator-=(const Vector< Number > &V)
Vector< Number > & operator=(const LinearAlgebra::TpetraWrappers::Vector< OtherNumber, MemorySpace > &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:138
iterator begin()
Vector(const InputIterator first, const InputIterator last)
Vector(const std::initializer_list< OtherNumber > &v)
Vector< Number > & operator/=(const Number factor)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
Point< 2 > first
Definition grid_out.cc:4623
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcEmptyObject()
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::ostream & operator<<(std::ostream &out, const Vector< number > &v)
Definition vector.h:1502
void swap(Vector< Number > &u, Vector< Number > &v) noexcept
Definition vector.h:1487
bool is_finite(const double x)
Definition numbers.h:538
unsigned int global_dof_index
Definition types.h:81