Reference documentation for deal.II version GIT relicensing-43-g3fc2fae7c3 2024-03-03 21: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\}}\)
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<
132 "The Vector class only supports basic numeric types. In particular, it "
133 "does not support automatically differentiated numbers.");
134
139 using value_type = Number;
141 using const_pointer = const value_type *;
143 using const_iterator = const value_type *;
147
158
167
178
183 Vector(Vector<Number> &&v) noexcept = default;
184
193 template <typename OtherNumber>
194 explicit Vector(const Vector<OtherNumber> &v);
195
211 template <typename OtherNumber>
212 explicit Vector(const std::initializer_list<OtherNumber> &v);
213
214#ifdef DEAL_II_WITH_PETSC
227#endif
228
229#ifdef DEAL_II_WITH_TRILINOS
245#endif
246
247#ifdef DEAL_II_TRILINOS_WITH_TPETRA
262 template <typename OtherNumber, typename MemorySpace>
263 explicit Vector(
265#endif
266
276 explicit Vector(const size_type n);
277
282 template <typename InputIterator>
283 Vector(const InputIterator first, const InputIterator last);
284
289 virtual ~Vector() override = default;
290
304 void
306
322 virtual void
323 reinit(const size_type N, const bool omit_zeroing_entries = false);
324
348 void
350
358 void
359 apply_givens_rotation(const std::array<Number, 3> &csr,
360 const size_type i,
361 const size_type k);
362
370 template <typename Number2>
371 void
372 reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
373
389 virtual void
391
403 operator=(const Number s);
404
412
419 operator=(Vector<Number> &&v) noexcept = default;
420
426 template <typename Number2>
429
435
436#ifdef DEAL_II_WITH_PETSC
450#endif
451
452
453#ifdef DEAL_II_WITH_TRILINOS
471#endif
472
473#ifdef DEAL_II_TRILINOS_WITH_TPETRA
489 template <typename OtherNumber, typename MemorySpace>
493#endif
494
500 template <typename Number2>
501 bool
503
509 template <typename Number2>
510 bool
512
534 template <typename Number2>
535 Number
536 operator*(const Vector<Number2> &V) const;
537
546 norm_sqr() const;
547
555 Number
556 mean_value() const;
557
566 l1_norm() const;
567
577 l2_norm() const;
578
588 lp_norm(const real_type p) const;
589
594 linfty_norm() const;
595
620 Number
621 add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
622
634 pointer
636
641 data() const;
642
650
655 begin() const;
656
662
668 end() const;
669
673 Number
674 operator()(const size_type i) const;
675
679 Number &
681
687 Number
688 operator[](const size_type i) const;
689
695 Number &
697
713 template <typename OtherNumber>
714 void
715 extract_subvector_to(const std::vector<size_type> &indices,
716 std::vector<OtherNumber> &values) const;
717
721 virtual void
723 ArrayView<Number> &elements) const override;
724
752 template <typename ForwardIterator, typename OutputIterator>
753 void
754 extract_subvector_to(ForwardIterator indices_begin,
755 const ForwardIterator indices_end,
756 OutputIterator values_begin) const;
772
780
785 template <typename OtherNumber>
786 void
787 add(const std::vector<size_type> &indices,
788 const std::vector<OtherNumber> &values);
789
794 template <typename OtherNumber>
795 void
796 add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
797
803 template <typename OtherNumber>
804 void
805 add(const size_type n_elements,
806 const size_type *indices,
807 const OtherNumber *values);
808
815 void
816 add(const Number s);
817
823 void
824 add(const Number a,
825 const Vector<Number> &V,
826 const Number b,
827 const Vector<Number> &W);
828
834 void
835 add(const Number a, const Vector<Number> &V);
836
842 void
843 sadd(const Number s, const Vector<Number> &V);
844
850 void
851 sadd(const Number s, const Number a, const Vector<Number> &V);
852
859 operator*=(const Number factor);
860
867 operator/=(const Number factor);
868
876 void
877 scale(const Vector<Number> &scaling_factors);
878
884 template <typename Number2>
885 void
886 scale(const Vector<Number2> &scaling_factors);
887
893 void
894 equ(const Number a, const Vector<Number> &u);
895
899 template <typename Number2>
900 void
901 equ(const Number a, const Vector<Number2> &u);
902
907 void
922 void
923 print(std::ostream &out,
924 const unsigned int precision = 3,
925 const bool scientific = true,
926 const bool across = true) const;
927
933 void
934 block_write(std::ostream &out) const;
935
947 void
948 block_read(std::istream &in);
949
955 template <class Archive>
956 void
957 save(Archive &ar, const unsigned int version) const;
958
964 template <class Archive>
965 void
966 load(Archive &ar, const unsigned int version);
967
968#ifdef DOXYGEN
974 template <class Archive>
975 void
976 serialize(Archive &archive, const unsigned int version);
977#else
978 // This macro defines the serialize() method that is compatible with
979 // the templated save() and load() method that have been implemented.
980 BOOST_SERIALIZATION_SPLIT_MEMBER()
981#endif
982
997 bool
998 in_local_range(const size_type global_index) const;
999
1015 IndexSet
1017
1021 virtual size_type
1022 size() const override;
1023
1031 size_type
1033
1039 bool
1040 all_zero() const;
1041
1051 bool
1053
1058 std::size_t
1060
1066 bool
1068
1074 void
1078private:
1083
1089 void
1091
1095 void
1096 do_reinit(const size_type new_size,
1097 const bool omit_zeroing_entries,
1098 const bool reset_partitioner);
1099
1104 mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1106
1107 // Make all other vector types friends.
1108 template <typename Number2>
1109 friend class Vector;
1110};
1111
1113/*----------------------- Inline functions ----------------------------------*/
1114
1115
1116#ifndef DOXYGEN
1117
1118
1119//------------------------ declarations for explicit specializations
1120template <>
1122Vector<int>::lp_norm(const real_type) const;
1123
1124
1125//------------------------ inline functions
1126
1127template <typename Number>
1129{
1130 // virtual functions called in constructors and destructors never use the
1131 // override in a derived class
1132 // for clarity be explicit on which function is called
1134}
1135
1136
1137
1138template <typename Number>
1139template <typename OtherNumber>
1140Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1141 : Vector(v.begin(), v.end())
1142{}
1143
1144
1145
1146template <typename Number>
1147template <typename InputIterator>
1148Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1149{
1150 // allocate memory. do not initialize it, as we will copy over to it in a
1151 // second
1152 reinit(std::distance(first, last), true);
1153 std::copy(first, last, begin());
1154}
1155
1156
1157
1158template <typename Number>
1159inline Vector<Number>::Vector(const size_type n)
1160{
1161 // virtual functions called in constructors and destructors never use the
1162 // override in a derived class
1163 // for clarity be explicit on which function is called
1164 Vector<Number>::reinit(n, false);
1165}
1166
1167
1168
1169template <typename Number>
1170inline typename Vector<Number>::size_type
1172{
1173 return values.size();
1174}
1175
1176
1177
1178template <typename Number>
1179inline typename Vector<Number>::size_type
1181{
1182 return values.size();
1183}
1184
1185
1186
1187template <typename Number>
1188inline bool
1190{
1191 return true;
1192}
1193
1194
1195
1196template <typename Number>
1197inline typename Vector<Number>::pointer
1199{
1200 return values.data();
1201}
1202
1203
1204
1205template <typename Number>
1206inline typename Vector<Number>::const_pointer
1208{
1209 return values.data();
1210}
1211
1212
1213
1214template <typename Number>
1215inline typename Vector<Number>::iterator
1217{
1218 return values.begin();
1219}
1220
1221
1222
1223template <typename Number>
1224inline typename Vector<Number>::const_iterator
1226{
1227 return values.begin();
1228}
1229
1230
1231
1232template <typename Number>
1233inline typename Vector<Number>::iterator
1235{
1236 return values.end();
1237}
1238
1239
1240
1241template <typename Number>
1242inline typename Vector<Number>::const_iterator
1243Vector<Number>::end() const
1244{
1245 return values.end();
1246}
1247
1248
1249
1250template <typename Number>
1251inline Number
1253{
1254 AssertIndexRange(i, size());
1255 return values[i];
1256}
1257
1258
1259
1260template <typename Number>
1261inline Number &
1263{
1264 AssertIndexRange(i, size());
1265 return values[i];
1266}
1267
1268
1269
1270template <typename Number>
1271inline Number
1273{
1274 return operator()(i);
1275}
1276
1277
1278
1279template <typename Number>
1280inline Number &
1282{
1283 return operator()(i);
1284}
1285
1286
1287
1288template <typename Number>
1289template <typename OtherNumber>
1290inline void
1291Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1292 std::vector<OtherNumber> &values) const
1293{
1294 for (size_type i = 0; i < indices.size(); ++i)
1295 values[i] = operator()(indices[i]);
1296}
1297
1298
1299
1300template <typename Number>
1301template <typename ForwardIterator, typename OutputIterator>
1302inline void
1303Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1304 const ForwardIterator indices_end,
1305 OutputIterator values_begin) const
1306{
1307 while (indices_begin != indices_end)
1308 {
1309 *values_begin = operator()(*indices_begin);
1310 ++indices_begin;
1311 ++values_begin;
1312 }
1313}
1314
1315
1316
1317template <typename Number>
1318inline Vector<Number> &
1319Vector<Number>::operator/=(const Number factor)
1320{
1321 AssertIsFinite(factor);
1322 Assert(factor != Number(0.), ExcZero());
1323
1324 this->operator*=(Number(1.) / factor);
1325 return *this;
1326}
1327
1328
1329
1330template <typename Number>
1331template <typename OtherNumber>
1332inline void
1333Vector<Number>::add(const std::vector<size_type> &indices,
1334 const std::vector<OtherNumber> &values)
1335{
1336 Assert(indices.size() == values.size(),
1337 ExcDimensionMismatch(indices.size(), values.size()));
1338 add(indices.size(), indices.data(), values.data());
1339}
1340
1341
1342
1343template <typename Number>
1344template <typename OtherNumber>
1345inline void
1346Vector<Number>::add(const std::vector<size_type> &indices,
1348{
1349 Assert(indices.size() == values.size(),
1350 ExcDimensionMismatch(indices.size(), values.size()));
1351 add(indices.size(), indices.data(), values.values.begin());
1352}
1353
1354
1355
1356template <typename Number>
1357template <typename OtherNumber>
1358inline void
1359Vector<Number>::add(const size_type n_indices,
1360 const size_type *indices,
1361 const OtherNumber *values)
1362{
1363 for (size_type i = 0; i < n_indices; ++i)
1364 {
1365 AssertIndexRange(indices[i], size());
1366 Assert(
1368 ExcMessage(
1369 "The given value is not finite but either infinite or Not A Number (NaN)"));
1370
1371 this->values[indices[i]] += values[i];
1372 }
1373}
1374
1375
1376
1377template <typename Number>
1378template <typename Number2>
1379inline bool
1381{
1382 return !(*this == v);
1383}
1384
1385
1386
1387template <typename Number>
1388inline void
1390{}
1391
1392
1393
1394template <typename Number>
1395inline bool
1397{
1398 return false;
1399}
1400
1401
1402
1403template <typename Number>
1404inline void
1406{}
1407
1408
1409
1410template <typename Number>
1411inline void
1413{}
1414
1415
1416
1417template <typename Number>
1418template <typename Number2>
1419inline void
1421 const bool omit_zeroing_entries)
1422{
1423 // go to actual reinit functions in case we need to change something with
1424 // the vector, else there is nothing to be done
1425 if (!omit_zeroing_entries || size() != v.size())
1426 {
1427 do_reinit(v.size(), omit_zeroing_entries, false);
1429 }
1430}
1431
1432
1433
1434// Moved from vector.templates.h as an inline function by Luca Heltai
1435// on 2009/04/12 to prevent strange compiling errors, after making
1436// swap virtual.
1437template <typename Number>
1438inline void
1440{
1441 values.swap(v.values);
1443}
1444
1445
1446
1447template <typename Number>
1448template <class Archive>
1449inline void
1450Vector<Number>::save(Archive &ar, const unsigned int) const
1451{
1452 // forward to serialization function in the base class.
1453 ar &static_cast<const Subscriptor &>(*this);
1454 ar &values;
1455}
1456
1457
1458
1459template <typename Number>
1460template <class Archive>
1461inline void
1462Vector<Number>::load(Archive &ar, const unsigned int)
1463{
1464 // the load stuff again from the archive
1465 ar &static_cast<Subscriptor &>(*this);
1466 ar &values;
1468}
1469
1470#endif
1471
1472
1486template <typename Number>
1487inline void
1489{
1490 u.swap(v);
1491}
1492
1493
1501template <typename number>
1502inline std::ostream &
1503operator<<(std::ostream &out, const Vector<number> &v)
1504{
1505 Assert(v.size() != 0, ExcEmptyObject());
1506 AssertThrow(out.fail() == false, ExcIO());
1507
1508 for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1509 out << v(i) << ' ';
1510 out << v(v.size() - 1);
1511
1512 AssertThrow(out.fail() == false, ExcIO());
1513
1514 return out;
1515}
1516
1525template <typename Number>
1526struct is_serial_vector<Vector<Number>> : std::true_type
1527{};
1528
1529
1531
1532#endif
iterator end()
iterator begin()
pointer data()
void swap(AlignedVector< T > &vec)
size_type size() const
Vector< Number > & operator=(const PETScWrappers::VectorBase &v)
typename numbers::NumberTraits< Number >::real_type real_type
Definition vector.h:157
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:141
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:143
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:145
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:1105
void block_read(std::istream &in)
types::global_dof_index size_type
Definition vector.h:146
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:1109
Vector(const LinearAlgebra::TpetraWrappers::Vector< OtherNumber, MemorySpace > &v)
real_type l2_norm() const
value_type * pointer
Definition vector.h:140
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)
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:1082
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:142
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
void compress(VectorOperation::values operation=VectorOperation::unknown) const
bool is_non_negative() const
value_type & reference
Definition vector.h:144
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:139
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:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
Point< 2 > first
Definition grid_out.cc:4613
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)
void swap(Vector< Number > &u, Vector< Number > &v)
Definition vector.h:1488
std::ostream & operator<<(std::ostream &out, const Vector< number > &v)
Definition vector.h:1503
bool is_finite(const double x)
Definition numbers.h:538
unsigned int global_dof_index
Definition types.h:81