deal.II version GIT relicensing-2287-g6548a49e0a 2024-12-20 18: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
25
29
30#include <boost/serialization/split_member.hpp>
31
32#include <algorithm>
33#include <initializer_list>
34#include <iosfwd>
35#include <vector>
36
38
39
40// Forward declarations
41#ifndef DOXYGEN
42# ifdef DEAL_II_WITH_PETSC
43namespace PETScWrappers
44{
45 class VectorBase;
46}
47# endif
48
49# ifdef DEAL_II_WITH_TRILINOS
50namespace TrilinosWrappers
51{
52 namespace MPI
53 {
54 class Vector;
55 }
56} // namespace TrilinosWrappers
57# endif
58
59# ifdef DEAL_II_TRILINOS_WITH_TPETRA
60namespace LinearAlgebra
61{
62 namespace TpetraWrappers
63 {
64 template <typename Number, typename MemorySpace>
65 class Vector;
66 }
67} // namespace LinearAlgebra
68# endif
69
70template <typename number>
72
73template <typename>
74class BlockVector;
75
76namespace parallel
77{
78 namespace internal
79 {
80 class TBBPartitioner;
81 }
82} // namespace parallel
83#endif
84
85
117template <typename Number>
118class Vector : public ReadVector<Number>
119{
120public:
128 static_assert(
129 std::is_arithmetic_v<typename numbers::NumberTraits<Number>::real_type>,
130 "The Vector class only supports basic numeric types. In particular, it "
131 "does not support automatically differentiated numbers.");
132
137 using value_type = Number;
139 using const_pointer = const value_type *;
141 using const_iterator = const value_type *;
145
156
165
176
181 Vector(Vector<Number> &&v) noexcept = default;
182
191 template <typename OtherNumber>
192 explicit Vector(const Vector<OtherNumber> &v);
193
209 template <typename OtherNumber>
210 explicit Vector(const std::initializer_list<OtherNumber> &v);
211
212#ifdef DEAL_II_WITH_PETSC
225#endif
226
227#ifdef DEAL_II_WITH_TRILINOS
243#endif
244
245#ifdef DEAL_II_TRILINOS_WITH_TPETRA
260 template <typename OtherNumber, typename MemorySpace>
261 explicit Vector(
263#endif
264
274 explicit Vector(const size_type n);
275
280 template <typename InputIterator>
281 Vector(const InputIterator first, const InputIterator last);
282
287 virtual ~Vector() override = default;
288
289
293 bool
294 empty() const;
295
309 void
311
327 virtual void
328 reinit(const size_type N, const bool omit_zeroing_entries = false);
329
353 void
355
363 void
364 apply_givens_rotation(const std::array<Number, 3> &csr,
365 const size_type i,
366 const size_type k);
367
375 template <typename Number2>
376 void
377 reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
378
394 virtual void
395 swap(Vector<Number> &v) noexcept;
396
408 operator=(const Number s);
409
417
424 operator=(Vector<Number> &&v) noexcept = default;
425
431 template <typename Number2>
434
440
441#ifdef DEAL_II_WITH_PETSC
455#endif
456
457
458#ifdef DEAL_II_WITH_TRILINOS
476#endif
477
478#ifdef DEAL_II_TRILINOS_WITH_TPETRA
494 template <typename OtherNumber, typename MemorySpace>
498#endif
499
505 template <typename Number2>
506 bool
508
514 template <typename Number2>
515 bool
517
539 template <typename Number2>
540 Number
541 operator*(const Vector<Number2> &V) const;
542
551 norm_sqr() const;
552
560 Number
561 mean_value() const;
562
571 l1_norm() const;
572
582 l2_norm() const;
583
593 lp_norm(const real_type p) const;
594
599 linfty_norm() const;
600
625 Number
626 add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
627
639 pointer
641
646 data() const;
647
655
660 begin() const;
661
667
673 end() const;
674
678 Number
679 operator()(const size_type i) const;
680
684 Number &
686
692 Number
693 operator[](const size_type i) const;
694
700 Number &
702
718 template <typename OtherNumber>
719 void
720 extract_subvector_to(const std::vector<size_type> &indices,
721 std::vector<OtherNumber> &values) const;
722
726 virtual void
728 const ArrayView<Number> &elements) const override;
729
757 template <typename ForwardIterator, typename OutputIterator>
758 void
759 extract_subvector_to(ForwardIterator indices_begin,
760 const ForwardIterator indices_end,
761 OutputIterator values_begin) const;
777
785
790 template <typename OtherNumber>
791 void
792 add(const std::vector<size_type> &indices,
793 const std::vector<OtherNumber> &values);
794
799 template <typename OtherNumber>
800 void
801 add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
802
808 template <typename OtherNumber>
809 void
810 add(const size_type n_elements,
811 const size_type *indices,
812 const OtherNumber *values);
813
820 void
821 add(const Number s);
822
828 void
829 add(const Number a,
830 const Vector<Number> &V,
831 const Number b,
832 const Vector<Number> &W);
833
839 void
840 add(const Number a, const Vector<Number> &V);
841
847 void
848 sadd(const Number s, const Vector<Number> &V);
849
855 void
856 sadd(const Number s, const Number a, const Vector<Number> &V);
857
864 operator*=(const Number factor);
865
872 operator/=(const Number factor);
873
881 void
882 scale(const Vector<Number> &scaling_factors);
883
889 template <typename Number2>
890 void
891 scale(const Vector<Number2> &scaling_factors);
892
898 void
899 equ(const Number a, const Vector<Number> &u);
900
904 template <typename Number2>
905 void
906 equ(const Number a, const Vector<Number2> &u);
907
912 void
927 void
928 print(std::ostream &out,
929 const unsigned int precision = 3,
930 const bool scientific = true,
931 const bool across = true) const;
932
938 void
939 block_write(std::ostream &out) const;
940
952 void
953 block_read(std::istream &in);
954
960 template <class Archive>
961 void
962 save(Archive &ar, const unsigned int version) const;
963
969 template <class Archive>
970 void
971 load(Archive &ar, const unsigned int version);
972
973#ifdef DOXYGEN
979 template <class Archive>
980 void
981 serialize(Archive &archive, const unsigned int version);
982#else
983 // This macro defines the serialize() method that is compatible with
984 // the templated save() and load() method that have been implemented.
985 BOOST_SERIALIZATION_SPLIT_MEMBER()
986#endif
987
1002 bool
1003 in_local_range(const size_type global_index) const;
1004
1020 IndexSet
1022
1026 virtual size_type
1027 size() const override;
1028
1036 size_type
1038
1044 bool
1045 all_zero() const;
1046
1056 bool
1058
1063 std::size_t
1065
1071 bool
1073
1079 void
1081
1089 MPI_Comm
1093private:
1098
1104 void
1106
1110 void
1111 do_reinit(const size_type new_size,
1112 const bool omit_zeroing_entries,
1113 const bool reset_partitioner);
1114
1119 mutable std::shared_ptr<parallel::internal::TBBPartitioner>
1121
1122 // Make all other vector types friends.
1123 template <typename Number2>
1124 friend class Vector;
1125};
1126
1128/*----------------------- Inline functions ----------------------------------*/
1129
1130
1131#ifndef DOXYGEN
1132
1133
1134//------------------------ declarations for explicit specializations
1135template <>
1137Vector<int>::lp_norm(const real_type) const;
1138
1139
1140//------------------------ inline functions
1141
1142template <typename Number>
1144{
1145 // virtual functions called in constructors and destructors never use the
1146 // override in a derived class
1147 // for clarity be explicit on which function is called
1149}
1150
1151
1152
1153template <typename Number>
1154template <typename OtherNumber>
1155Vector<Number>::Vector(const std::initializer_list<OtherNumber> &v)
1156 : Vector(v.begin(), v.end())
1157{}
1158
1159
1160
1161template <typename Number>
1162template <typename InputIterator>
1163Vector<Number>::Vector(const InputIterator first, const InputIterator last)
1164{
1165 // allocate memory. do not initialize it, as we will copy over to it in a
1166 // second
1167 reinit(std::distance(first, last), true);
1168 std::copy(first, last, begin());
1169}
1170
1171
1172
1173template <typename Number>
1174inline Vector<Number>::Vector(const size_type n)
1175{
1176 // virtual functions called in constructors and destructors never use the
1177 // override in a derived class
1178 // for clarity be explicit on which function is called
1179 Vector<Number>::reinit(n, false);
1180}
1181
1182
1183
1184template <typename Number>
1185inline typename Vector<Number>::size_type
1187{
1188 return values.size();
1189}
1190
1191
1192
1193template <typename Number>
1194inline typename Vector<Number>::size_type
1196{
1197 return values.size();
1198}
1199
1200
1201
1202template <typename Number>
1203inline bool
1205{
1206 return true;
1207}
1208
1209
1210
1211template <typename Number>
1212inline typename Vector<Number>::pointer
1214{
1215 return values.data();
1216}
1217
1218
1219
1220template <typename Number>
1221inline typename Vector<Number>::const_pointer
1223{
1224 return values.data();
1225}
1226
1227
1228
1229template <typename Number>
1230inline typename Vector<Number>::iterator
1232{
1233 return values.begin();
1234}
1235
1236
1237
1238template <typename Number>
1239inline typename Vector<Number>::const_iterator
1241{
1242 return values.begin();
1243}
1244
1245
1246
1247template <typename Number>
1248inline typename Vector<Number>::iterator
1250{
1251 return values.end();
1252}
1253
1254
1255
1256template <typename Number>
1257inline typename Vector<Number>::const_iterator
1258Vector<Number>::end() const
1259{
1260 return values.end();
1261}
1262
1263
1264
1265template <typename Number>
1266inline Number
1268{
1269 AssertIndexRange(i, size());
1270 return values[i];
1271}
1272
1273
1274
1275template <typename Number>
1276inline Number &
1278{
1279 AssertIndexRange(i, size());
1280 return values[i];
1281}
1282
1283
1284
1285template <typename Number>
1286inline Number
1288{
1289 return operator()(i);
1290}
1291
1292
1293
1294template <typename Number>
1295inline Number &
1297{
1298 return operator()(i);
1299}
1300
1301
1302
1303template <typename Number>
1304template <typename OtherNumber>
1305inline void
1306Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1307 std::vector<OtherNumber> &values) const
1308{
1309 for (size_type i = 0; i < indices.size(); ++i)
1310 values[i] = operator()(indices[i]);
1311}
1312
1313
1314
1315template <typename Number>
1316template <typename ForwardIterator, typename OutputIterator>
1317inline void
1318Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1319 const ForwardIterator indices_end,
1320 OutputIterator values_begin) const
1321{
1322 while (indices_begin != indices_end)
1323 {
1324 *values_begin = operator()(*indices_begin);
1325 ++indices_begin;
1326 ++values_begin;
1327 }
1328}
1329
1330
1331
1332template <typename Number>
1333inline Vector<Number> &
1334Vector<Number>::operator/=(const Number factor)
1335{
1336 AssertIsFinite(factor);
1337 Assert(factor != Number(0.), ExcZero());
1338
1339 this->operator*=(Number(1.) / factor);
1340 return *this;
1341}
1342
1343
1344
1345template <typename Number>
1346template <typename OtherNumber>
1347inline void
1348Vector<Number>::add(const std::vector<size_type> &indices,
1349 const std::vector<OtherNumber> &values)
1350{
1351 Assert(indices.size() == values.size(),
1352 ExcDimensionMismatch(indices.size(), values.size()));
1353 add(indices.size(), indices.data(), values.data());
1354}
1355
1356
1357
1358template <typename Number>
1359template <typename OtherNumber>
1360inline void
1361Vector<Number>::add(const std::vector<size_type> &indices,
1363{
1364 Assert(indices.size() == values.size(),
1365 ExcDimensionMismatch(indices.size(), values.size()));
1366 add(indices.size(), indices.data(), values.values.begin());
1367}
1368
1369
1370
1371template <typename Number>
1372template <typename OtherNumber>
1373inline void
1374Vector<Number>::add(const size_type n_indices,
1375 const size_type *indices,
1376 const OtherNumber *values)
1377{
1378 for (size_type i = 0; i < n_indices; ++i)
1379 {
1380 AssertIndexRange(indices[i], size());
1381 Assert(
1383 ExcMessage(
1384 "The given value is not finite but either infinite or Not A Number (NaN)"));
1385
1386 this->values[indices[i]] += values[i];
1387 }
1388}
1389
1390
1391
1392template <typename Number>
1393template <typename Number2>
1394inline bool
1396{
1397 return !(*this == v);
1398}
1399
1400
1401template <typename Number>
1402inline bool
1404{
1405 return this->size() == 0;
1406}
1407
1408template <typename Number>
1409inline void
1411{}
1412
1413
1414
1415template <typename Number>
1416inline bool
1418{
1419 return false;
1420}
1421
1422
1423
1424template <typename Number>
1425inline void
1427{}
1428
1429
1430
1431template <typename Number>
1432inline void
1434{}
1435
1436
1437
1438template <typename Number>
1439inline MPI_Comm
1441{
1442 return MPI_COMM_SELF;
1443}
1444
1445
1446template <typename Number>
1447template <typename Number2>
1448inline void
1450 const bool omit_zeroing_entries)
1451{
1452 // go to actual reinit functions in case we need to change something with
1453 // the vector, else there is nothing to be done
1454 if (!omit_zeroing_entries || size() != v.size())
1455 {
1456 do_reinit(v.size(), omit_zeroing_entries, false);
1458 }
1459}
1460
1461
1462
1463// Moved from vector.templates.h as an inline function by Luca Heltai
1464// on 2009/04/12 to prevent strange compiling errors, after making
1465// swap virtual.
1466template <typename Number>
1467inline void
1469{
1470 values.swap(v.values);
1472}
1473
1474
1475
1476template <typename Number>
1477template <class Archive>
1478inline void
1479Vector<Number>::save(Archive &ar, const unsigned int) const
1480{
1481 // forward to serialization function in the base class.
1482 ar &static_cast<const EnableObserverPointer &>(*this);
1483 ar &values;
1484}
1485
1486
1487
1488template <typename Number>
1489template <class Archive>
1490inline void
1491Vector<Number>::load(Archive &ar, const unsigned int)
1492{
1493 // the load stuff again from the archive
1494 ar &static_cast<EnableObserverPointer &>(*this);
1495 ar &values;
1497}
1498
1499#endif
1500
1501
1515template <typename Number>
1516inline void
1518{
1519 u.swap(v);
1520}
1521
1522
1530template <typename number>
1531inline std::ostream &
1532operator<<(std::ostream &out, const Vector<number> &v)
1533{
1534 Assert(v.size() != 0, ExcEmptyObject());
1535 AssertThrow(out.fail() == false, ExcIO());
1536
1537 for (typename Vector<number>::size_type i = 0; i < v.size() - 1; ++i)
1538 out << v(i) << ' ';
1539 out << v(v.size() - 1);
1540
1541 AssertThrow(out.fail() == false, ExcIO());
1542
1543 return out;
1544}
1545
1554template <typename Number>
1555struct is_serial_vector<Vector<Number>> : std::true_type
1556{};
1557
1558
1560
1561#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:155
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:139
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:141
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:143
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:1120
void block_read(std::istream &in)
types::global_dof_index size_type
Definition vector.h:144
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:1124
Vector(const LinearAlgebra::TpetraWrappers::Vector< OtherNumber, MemorySpace > &v)
real_type l2_norm() const
value_type * pointer
Definition vector.h:138
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, const ArrayView< Number > &elements) const override
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:1097
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:140
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:142
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:137
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:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
Point< 2 > first
Definition grid_out.cc:4629
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:1532
void swap(Vector< Number > &u, Vector< Number > &v) noexcept
Definition vector.h:1517
std::size_t size
Definition mpi.cc:734
bool is_finite(const double x)
Definition numbers.h:533
unsigned int global_dof_index
Definition types.h:81