deal.II version GIT relicensing-1838-g97284be5cd 2024-09-11 15:30:00+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
290
294 bool
295 empty() const;
296
310 void
312
328 virtual void
329 reinit(const size_type N, const bool omit_zeroing_entries = false);
330
354 void
356
364 void
365 apply_givens_rotation(const std::array<Number, 3> &csr,
366 const size_type i,
367 const size_type k);
368
376 template <typename Number2>
377 void
378 reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
379
395 virtual void
396 swap(Vector<Number> &v) noexcept;
397
409 operator=(const Number s);
410
418
425 operator=(Vector<Number> &&v) noexcept = default;
426
432 template <typename Number2>
435
441
442#ifdef DEAL_II_WITH_PETSC
456#endif
457
458
459#ifdef DEAL_II_WITH_TRILINOS
477#endif
478
479#ifdef DEAL_II_TRILINOS_WITH_TPETRA
495 template <typename OtherNumber, typename MemorySpace>
499#endif
500
506 template <typename Number2>
507 bool
509
515 template <typename Number2>
516 bool
518
540 template <typename Number2>
541 Number
542 operator*(const Vector<Number2> &V) const;
543
552 norm_sqr() const;
553
561 Number
562 mean_value() const;
563
572 l1_norm() const;
573
583 l2_norm() const;
584
594 lp_norm(const real_type p) const;
595
600 linfty_norm() const;
601
626 Number
627 add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
628
640 pointer
642
647 data() const;
648
656
661 begin() const;
662
668
674 end() const;
675
679 Number
680 operator()(const size_type i) const;
681
685 Number &
687
693 Number
694 operator[](const size_type i) const;
695
701 Number &
703
719 template <typename OtherNumber>
720 void
721 extract_subvector_to(const std::vector<size_type> &indices,
722 std::vector<OtherNumber> &values) const;
723
727 virtual void
729 ArrayView<Number> &elements) const override;
730
758 template <typename ForwardIterator, typename OutputIterator>
759 void
760 extract_subvector_to(ForwardIterator indices_begin,
761 const ForwardIterator indices_end,
762 OutputIterator values_begin) const;
778
786
791 template <typename OtherNumber>
792 void
793 add(const std::vector<size_type> &indices,
794 const std::vector<OtherNumber> &values);
795
800 template <typename OtherNumber>
801 void
802 add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
803
809 template <typename OtherNumber>
810 void
811 add(const size_type n_elements,
812 const size_type *indices,
813 const OtherNumber *values);
814
821 void
822 add(const Number s);
823
829 void
830 add(const Number a,
831 const Vector<Number> &V,
832 const Number b,
833 const Vector<Number> &W);
834
840 void
841 add(const Number a, const Vector<Number> &V);
842
848 void
849 sadd(const Number s, const Vector<Number> &V);
850
856 void
857 sadd(const Number s, const Number a, const Vector<Number> &V);
858
865 operator*=(const Number factor);
866
873 operator/=(const Number factor);
874
882 void
883 scale(const Vector<Number> &scaling_factors);
884
890 template <typename Number2>
891 void
892 scale(const Vector<Number2> &scaling_factors);
893
899 void
900 equ(const Number a, const Vector<Number> &u);
901
905 template <typename Number2>
906 void
907 equ(const Number a, const Vector<Number2> &u);
908
913 void
928 void
929 print(std::ostream &out,
930 const unsigned int precision = 3,
931 const bool scientific = true,
932 const bool across = true) const;
933
939 void
940 block_write(std::ostream &out) const;
941
953 void
954 block_read(std::istream &in);
955
961 template <class Archive>
962 void
963 save(Archive &ar, const unsigned int version) const;
964
970 template <class Archive>
971 void
972 load(Archive &ar, const unsigned int version);
973
974#ifdef DOXYGEN
980 template <class Archive>
981 void
982 serialize(Archive &archive, const unsigned int version);
983#else
984 // This macro defines the serialize() method that is compatible with
985 // the templated save() and load() method that have been implemented.
986 BOOST_SERIALIZATION_SPLIT_MEMBER()
987#endif
988
1003 bool
1004 in_local_range(const size_type global_index) const;
1005
1021 IndexSet
1023
1027 virtual size_type
1028 size() const override;
1029
1037 size_type
1039
1045 bool
1046 all_zero() const;
1047
1057 bool
1059
1064 std::size_t
1066
1072 bool
1074
1080 void
1082
1090 MPI_Comm
1094private:
1099
1105 void
1107
1111 void
1112 do_reinit(const size_type new_size,
1113 const bool omit_zeroing_entries,
1114 const bool reset_partitioner);
1115
1120 mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1122
1123 // Make all other vector types friends.
1124 template <typename Number2>
1125 friend class Vector;
1126};
1127
1129/*----------------------- Inline functions ----------------------------------*/
1130
1131
1132#ifndef DOXYGEN
1133
1134
1135//------------------------ declarations for explicit specializations
1136template <>
1138Vector<int>::lp_norm(const real_type) const;
1139
1140
1141//------------------------ inline functions
1142
1143template <typename Number>
1145{
1146 // virtual functions called in constructors and destructors never use the
1147 // override in a derived class
1148 // for clarity be explicit on which function is called
1150}
1151
1152
1153
1154template <typename Number>
1155template <typename OtherNumber>
1156Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1157 : Vector(v.begin(), v.end())
1158{}
1159
1160
1161
1162template <typename Number>
1163template <typename InputIterator>
1164Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1165{
1166 // allocate memory. do not initialize it, as we will copy over to it in a
1167 // second
1168 reinit(std::distance(first, last), true);
1169 std::copy(first, last, begin());
1170}
1171
1172
1173
1174template <typename Number>
1175inline Vector<Number>::Vector(const size_type n)
1176{
1177 // virtual functions called in constructors and destructors never use the
1178 // override in a derived class
1179 // for clarity be explicit on which function is called
1180 Vector<Number>::reinit(n, false);
1181}
1182
1183
1184
1185template <typename Number>
1186inline typename Vector<Number>::size_type
1188{
1189 return values.size();
1190}
1191
1192
1193
1194template <typename Number>
1195inline typename Vector<Number>::size_type
1197{
1198 return values.size();
1199}
1200
1201
1202
1203template <typename Number>
1204inline bool
1206{
1207 return true;
1208}
1209
1210
1211
1212template <typename Number>
1213inline typename Vector<Number>::pointer
1215{
1216 return values.data();
1217}
1218
1219
1220
1221template <typename Number>
1222inline typename Vector<Number>::const_pointer
1224{
1225 return values.data();
1226}
1227
1228
1229
1230template <typename Number>
1231inline typename Vector<Number>::iterator
1233{
1234 return values.begin();
1235}
1236
1237
1238
1239template <typename Number>
1240inline typename Vector<Number>::const_iterator
1242{
1243 return values.begin();
1244}
1245
1246
1247
1248template <typename Number>
1249inline typename Vector<Number>::iterator
1251{
1252 return values.end();
1253}
1254
1255
1256
1257template <typename Number>
1258inline typename Vector<Number>::const_iterator
1259Vector<Number>::end() const
1260{
1261 return values.end();
1262}
1263
1264
1265
1266template <typename Number>
1267inline Number
1269{
1270 AssertIndexRange(i, size());
1271 return values[i];
1272}
1273
1274
1275
1276template <typename Number>
1277inline Number &
1279{
1280 AssertIndexRange(i, size());
1281 return values[i];
1282}
1283
1284
1285
1286template <typename Number>
1287inline Number
1289{
1290 return operator()(i);
1291}
1292
1293
1294
1295template <typename Number>
1296inline Number &
1298{
1299 return operator()(i);
1300}
1301
1302
1303
1304template <typename Number>
1305template <typename OtherNumber>
1306inline void
1307Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1308 std::vector<OtherNumber> &values) const
1309{
1310 for (size_type i = 0; i < indices.size(); ++i)
1311 values[i] = operator()(indices[i]);
1312}
1313
1314
1315
1316template <typename Number>
1317template <typename ForwardIterator, typename OutputIterator>
1318inline void
1319Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1320 const ForwardIterator indices_end,
1321 OutputIterator values_begin) const
1322{
1323 while (indices_begin != indices_end)
1324 {
1325 *values_begin = operator()(*indices_begin);
1326 ++indices_begin;
1327 ++values_begin;
1328 }
1329}
1330
1331
1332
1333template <typename Number>
1334inline Vector<Number> &
1335Vector<Number>::operator/=(const Number factor)
1336{
1337 AssertIsFinite(factor);
1338 Assert(factor != Number(0.), ExcZero());
1339
1340 this->operator*=(Number(1.) / factor);
1341 return *this;
1342}
1343
1344
1345
1346template <typename Number>
1347template <typename OtherNumber>
1348inline void
1349Vector<Number>::add(const std::vector<size_type> &indices,
1350 const std::vector<OtherNumber> &values)
1351{
1352 Assert(indices.size() == values.size(),
1353 ExcDimensionMismatch(indices.size(), values.size()));
1354 add(indices.size(), indices.data(), values.data());
1355}
1356
1357
1358
1359template <typename Number>
1360template <typename OtherNumber>
1361inline void
1362Vector<Number>::add(const std::vector<size_type> &indices,
1364{
1365 Assert(indices.size() == values.size(),
1366 ExcDimensionMismatch(indices.size(), values.size()));
1367 add(indices.size(), indices.data(), values.values.begin());
1368}
1369
1370
1371
1372template <typename Number>
1373template <typename OtherNumber>
1374inline void
1375Vector<Number>::add(const size_type n_indices,
1376 const size_type *indices,
1377 const OtherNumber *values)
1378{
1379 for (size_type i = 0; i < n_indices; ++i)
1380 {
1381 AssertIndexRange(indices[i], size());
1382 Assert(
1384 ExcMessage(
1385 "The given value is not finite but either infinite or Not A Number (NaN)"));
1386
1387 this->values[indices[i]] += values[i];
1388 }
1389}
1390
1391
1392
1393template <typename Number>
1394template <typename Number2>
1395inline bool
1397{
1398 return !(*this == v);
1399}
1400
1401
1402template <typename Number>
1403inline bool
1405{
1406 return this->size() == 0;
1407}
1408
1409template <typename Number>
1410inline void
1412{}
1413
1414
1415
1416template <typename Number>
1417inline bool
1419{
1420 return false;
1421}
1422
1423
1424
1425template <typename Number>
1426inline void
1428{}
1429
1430
1431
1432template <typename Number>
1433inline void
1435{}
1436
1437
1438
1439template <typename Number>
1440inline MPI_Comm
1442{
1443 return MPI_COMM_SELF;
1444}
1445
1446
1447template <typename Number>
1448template <typename Number2>
1449inline void
1451 const bool omit_zeroing_entries)
1452{
1453 // go to actual reinit functions in case we need to change something with
1454 // the vector, else there is nothing to be done
1455 if (!omit_zeroing_entries || size() != v.size())
1456 {
1457 do_reinit(v.size(), omit_zeroing_entries, false);
1459 }
1460}
1461
1462
1463
1464// Moved from vector.templates.h as an inline function by Luca Heltai
1465// on 2009/04/12 to prevent strange compiling errors, after making
1466// swap virtual.
1467template <typename Number>
1468inline void
1470{
1471 values.swap(v.values);
1473}
1474
1475
1476
1477template <typename Number>
1478template <class Archive>
1479inline void
1480Vector<Number>::save(Archive &ar, const unsigned int) const
1481{
1482 // forward to serialization function in the base class.
1483 ar &static_cast<const Subscriptor &>(*this);
1484 ar &values;
1485}
1486
1487
1488
1489template <typename Number>
1490template <class Archive>
1491inline void
1492Vector<Number>::load(Archive &ar, const unsigned int)
1493{
1494 // the load stuff again from the archive
1495 ar &static_cast<Subscriptor &>(*this);
1496 ar &values;
1498}
1499
1500#endif
1501
1502
1516template <typename Number>
1517inline void
1519{
1520 u.swap(v);
1521}
1522
1523
1531template <typename number>
1532inline std::ostream &
1533operator<<(std::ostream &out, const Vector<number> &v)
1534{
1535 Assert(v.size() != 0, ExcEmptyObject());
1536 AssertThrow(out.fail() == false, ExcIO());
1537
1538 for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1539 out << v(i) << ' ';
1540 out << v(v.size() - 1);
1541
1542 AssertThrow(out.fail() == false, ExcIO());
1543
1544 return out;
1545}
1546
1555template <typename Number>
1556struct is_serial_vector<Vector<Number>> : std::true_type
1557{};
1558
1559
1561
1562#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
MPI_Comm get_mpi_communicator() const
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:1121
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)
bool empty() const
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:1125
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:1098
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:500
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:501
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:1533
void swap(Vector< Number > &u, Vector< Number > &v) noexcept
Definition vector.h:1518
bool is_finite(const double x)
Definition numbers.h:533
unsigned int global_dof_index
Definition types.h:81