Reference documentation for deal.II version Git 92ca227 2018-05-25 19:47:08 -0500
vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2018 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_vector_h
17 #define dealii_vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/index_set.h>
24 #include <deal.II/base/logstream.h>
25 #include <deal.II/base/subscriptor.h>
26 
27 #include <deal.II/lac/vector_operation.h>
28 #include <deal.II/lac/vector_type_traits.h>
29 
30 // boost::serialization::make_array used to be in array.hpp, but was
31 // moved to a different file in BOOST 1.64
32 #include <boost/version.hpp>
33 #if BOOST_VERSION >= 106400
34 # include <boost/serialization/array_wrapper.hpp>
35 #else
36 # include <boost/serialization/array.hpp>
37 #endif
38 #include <boost/serialization/split_member.hpp>
39 
40 #include <cstdio>
41 #include <cstring>
42 #include <iostream>
43 #include <vector>
44 
45 DEAL_II_NAMESPACE_OPEN
46 
47 
48 #ifdef DEAL_II_WITH_PETSC
49 namespace PETScWrappers
50 {
51  class VectorBase;
52 }
53 #endif
54 
55 #ifdef DEAL_II_WITH_TRILINOS
56 namespace TrilinosWrappers
57 {
58  namespace MPI
59  {
60  class Vector;
61  }
62 } // namespace TrilinosWrappers
63 #endif
64 
65 template <typename number>
66 class LAPACKFullMatrix;
67 
68 template <typename>
69 class BlockVector;
70 
71 template <typename>
72 class VectorView;
73 
74 namespace parallel
75 {
76  namespace internal
77  {
78  class TBBPartitioner;
79  }
80 } // namespace parallel
81 
82 
83 
107 template <typename Number>
108 class Vector : public Subscriptor
109 {
110 public:
115  typedef Number value_type;
116  typedef value_type * pointer;
117  typedef const value_type * const_pointer;
118  typedef value_type * iterator;
119  typedef const value_type * const_iterator;
120  typedef value_type & reference;
121  typedef const value_type & const_reference;
122  typedef types::global_dof_index size_type;
123 
134 
135 public:
143  Vector();
144 
152  Vector(const Vector<Number> &v);
153 
158  Vector(Vector<Number> &&v) noexcept;
159 
172  template <typename OtherNumber>
173  explicit Vector(const Vector<OtherNumber> &v);
174 
175 #ifdef DEAL_II_WITH_PETSC
176 
187  explicit Vector(const PETScWrappers::VectorBase &v);
188 #endif
189 
190 #ifdef DEAL_II_WITH_TRILINOS
191 
205  explicit Vector(const TrilinosWrappers::MPI::Vector &v);
206 #endif
207 
217  explicit Vector(const size_type n);
218 
223  template <typename InputIterator>
224  Vector(const InputIterator first, const InputIterator last);
225 
230  virtual ~Vector() override = default;
231 
245  void
246  compress(::VectorOperation::values operation =
247  ::VectorOperation::unknown) const;
248 
264  virtual void
265  reinit(const size_type N, const bool omit_zeroing_entries = false);
266 
290  void
291  grow_or_shrink(const size_type N);
292 
300  template <typename Number2>
301  void
302  reinit(const Vector<Number2> &V, const bool omit_zeroing_entries = false);
303 
319  virtual void
320  swap(Vector<Number> &v);
321 
335  Vector<Number> &
336  operator=(const Number s);
337 
343  Vector<Number> &
344  operator=(const Vector<Number> &v);
345 
351  Vector<Number> &
352  operator=(Vector<Number> &&v) noexcept;
353 
359  template <typename Number2>
360  Vector<Number> &
361  operator=(const Vector<Number2> &v);
362 
366  Vector<Number> &
367  operator=(const BlockVector<Number> &v);
368 
369 #ifdef DEAL_II_WITH_PETSC
370 
381  Vector<Number> &
383 #endif
384 
385 
386 #ifdef DEAL_II_WITH_TRILINOS
387 
402  Vector<Number> &
404 #endif
405 
411  template <typename Number2>
412  bool
413  operator==(const Vector<Number2> &v) const;
414 
420  template <typename Number2>
421  bool
422  operator!=(const Vector<Number2> &v) const;
423 
425 
426 
431 
445  template <typename Number2>
446  Number operator*(const Vector<Number2> &V) const;
447 
455  real_type
456  norm_sqr() const;
457 
465  Number
466  mean_value() const;
467 
475  real_type
476  l1_norm() const;
477 
486  real_type
487  l2_norm() const;
488 
497  real_type
498  lp_norm(const real_type p) const;
499 
503  real_type
504  linfty_norm() const;
505 
530  Number
531  add_and_dot(const Number a, const Vector<Number> &V, const Vector<Number> &W);
532 
534 
535 
540 
546  iterator
547  begin();
548 
552  const_iterator
553  begin() const;
554 
558  iterator
559  end();
560 
565  const_iterator
566  end() const;
567 
571  Number
572  operator()(const size_type i) const;
573 
577  Number &
578  operator()(const size_type i);
579 
585  Number operator[](const size_type i) const;
586 
592  Number &operator[](const size_type i);
593 
609  template <typename OtherNumber>
610  void
611  extract_subvector_to(const std::vector<size_type> &indices,
612  std::vector<OtherNumber> & values) const;
613 
641  template <typename ForwardIterator, typename OutputIterator>
642  void
643  extract_subvector_to(ForwardIterator indices_begin,
644  const ForwardIterator indices_end,
645  OutputIterator values_begin) const;
647 
648 
653 
659  Vector<Number> &
660  operator+=(const Vector<Number> &V);
661 
667  Vector<Number> &
668  operator-=(const Vector<Number> &V);
669 
674  template <typename OtherNumber>
675  void
676  add(const std::vector<size_type> & indices,
677  const std::vector<OtherNumber> &values);
678 
683  template <typename OtherNumber>
684  void
685  add(const std::vector<size_type> &indices, const Vector<OtherNumber> &values);
686 
692  template <typename OtherNumber>
693  void
694  add(const size_type n_elements,
695  const size_type * indices,
696  const OtherNumber *values);
697 
704  void
705  add(const Number s);
706 
712  void
713  add(const Number a,
714  const Vector<Number> &V,
715  const Number b,
716  const Vector<Number> &W);
717 
723  void
724  add(const Number a, const Vector<Number> &V);
725 
731  void
732  sadd(const Number s, const Vector<Number> &V);
733 
739  void
740  sadd(const Number s, const Number a, const Vector<Number> &V);
741 
747  Vector<Number> &
748  operator*=(const Number factor);
749 
755  Vector<Number> &
756  operator/=(const Number factor);
757 
765  void
766  scale(const Vector<Number> &scaling_factors);
767 
773  template <typename Number2>
774  void
775  scale(const Vector<Number2> &scaling_factors);
776 
782  void
783  equ(const Number a, const Vector<Number> &u);
784 
788  template <typename Number2>
789  void
790  equ(const Number a, const Vector<Number2> &u);
791 
804  DEAL_II_DEPRECATED
805  void
806  ratio(const Vector<Number> &a, const Vector<Number> &b);
807 
812  void
813  update_ghost_values() const;
815 
816 
827  DEAL_II_DEPRECATED
828  void
829  print(const char *format = nullptr) const;
830 
837  void
838  print(std::ostream & out,
839  const unsigned int precision = 3,
840  const bool scientific = true,
841  const bool across = true) const;
842 
851  DEAL_II_DEPRECATED
852  void
853  print(LogStream & out,
854  const unsigned int width = 6,
855  const bool across = true) const;
856 
862  void
863  block_write(std::ostream &out) const;
864 
876  void
877  block_read(std::istream &in);
878 
883  template <class Archive>
884  void
885  save(Archive &ar, const unsigned int version) const;
886 
891  template <class Archive>
892  void
893  load(Archive &ar, const unsigned int version);
894 
895  BOOST_SERIALIZATION_SPLIT_MEMBER()
896 
897 
905 
911  bool
912  in_local_range(const size_type global_index) const;
913 
929  IndexSet
930  locally_owned_elements() const;
931 
935  size_type
936  size() const;
937 
943  bool
944  all_zero() const;
945 
955  bool
956  is_non_negative() const;
957 
962  std::size_t
963  memory_consumption() const;
965 
966 protected:
971  size_type vec_size;
972 
979  size_type max_vec_size;
980 
988  std::unique_ptr<Number[], decltype(&free)> values;
989 
994  mutable std::shared_ptr<parallel::internal::TBBPartitioner>
996 
1000  template <typename Number2>
1001  friend class Vector;
1002 
1006  template <typename Number2>
1007  friend class LAPACKFullMatrix;
1008 
1012  friend class VectorView<Number>;
1013 
1014 private:
1020  void
1021  allocate(const size_type copy_n_el = 0);
1022 };
1023 
1025 /*----------------------- Inline functions ----------------------------------*/
1026 
1027 
1028 #ifndef DOXYGEN
1029 
1030 
1031 //------------------------ declarations for explicit specializations
1032 template <>
1034 Vector<int>::lp_norm(const real_type) const;
1035 
1036 
1037 //------------------------ inline functions
1038 
1039 template <typename Number>
1040 inline Vector<Number>::Vector() :
1041  vec_size(0),
1042  max_vec_size(0),
1043  values(nullptr, &free)
1044 {
1045  // virtual functions called in constructors and destructors never use the
1046  // override in a derived class
1047  // for clarity be explicit on which function is called
1049 }
1050 
1051 
1052 
1053 template <typename Number>
1054 template <typename InputIterator>
1055 Vector<Number>::Vector(const InputIterator first, const InputIterator last) :
1056  vec_size(0),
1057  max_vec_size(0),
1058  values(nullptr, &free)
1059 {
1060  // allocate memory. do not initialize it, as we will copy over to it in a
1061  // second
1062  reinit(std::distance(first, last), true);
1063  std::copy(first, last, begin());
1064 }
1065 
1066 
1067 
1068 template <typename Number>
1069 inline Vector<Number>::Vector(const size_type n) :
1070  vec_size(0),
1071  max_vec_size(0),
1072  values(nullptr, &free)
1073 {
1074  // virtual functions called in constructors and destructors never use the
1075  // override in a derived class
1076  // for clarity be explicit on which function is called
1077  Vector<Number>::reinit(n, false);
1078 }
1079 
1080 
1081 
1082 template <typename Number>
1083 inline typename Vector<Number>::size_type
1084 Vector<Number>::size() const
1085 {
1086  return vec_size;
1087 }
1088 
1089 
1090 template <typename Number>
1091 inline bool
1092 Vector<Number>::in_local_range(const size_type) const
1093 {
1094  return true;
1095 }
1096 
1097 
1098 
1099 template <typename Number>
1100 inline typename Vector<Number>::iterator
1102 {
1103  return values.get();
1104 }
1105 
1106 
1107 
1108 template <typename Number>
1109 inline typename Vector<Number>::const_iterator
1110 Vector<Number>::begin() const
1111 {
1112  return values.get();
1113 }
1114 
1115 
1116 
1117 template <typename Number>
1118 inline typename Vector<Number>::iterator
1120 {
1121  return values.get() + vec_size;
1122 }
1123 
1124 
1125 
1126 template <typename Number>
1127 inline typename Vector<Number>::const_iterator
1128 Vector<Number>::end() const
1129 {
1130  return values.get() + vec_size;
1131 }
1132 
1133 
1134 
1135 template <typename Number>
1136 inline Number
1137 Vector<Number>::operator()(const size_type i) const
1138 {
1139  Assert(i < vec_size, ExcIndexRange(i, 0, vec_size));
1140  return values[i];
1141 }
1142 
1143 
1144 
1145 template <typename Number>
1146 inline Number &
1147 Vector<Number>::operator()(const size_type i)
1148 {
1149  Assert(i < vec_size, ExcIndexRangeType<size_type>(i, 0, vec_size));
1150  return values[i];
1151 }
1152 
1153 
1154 
1155 template <typename Number>
1156 inline Number Vector<Number>::operator[](const size_type i) const
1157 {
1158  return operator()(i);
1159 }
1160 
1161 
1162 
1163 template <typename Number>
1164 inline Number &Vector<Number>::operator[](const size_type i)
1165 {
1166  return operator()(i);
1167 }
1168 
1169 
1170 
1171 template <typename Number>
1172 template <typename OtherNumber>
1173 inline void
1174 Vector<Number>::extract_subvector_to(const std::vector<size_type> &indices,
1175  std::vector<OtherNumber> & values) const
1176 {
1177  for (size_type i = 0; i < indices.size(); ++i)
1178  values[i] = operator()(indices[i]);
1179 }
1180 
1181 
1182 
1183 template <typename Number>
1184 template <typename ForwardIterator, typename OutputIterator>
1185 inline void
1186 Vector<Number>::extract_subvector_to(ForwardIterator indices_begin,
1187  const ForwardIterator indices_end,
1188  OutputIterator values_begin) const
1189 {
1190  while (indices_begin != indices_end)
1191  {
1192  *values_begin = operator()(*indices_begin);
1193  indices_begin++;
1194  values_begin++;
1195  }
1196 }
1197 
1198 
1199 
1200 template <typename Number>
1201 inline Vector<Number> &
1202 Vector<Number>::operator/=(const Number factor)
1203 {
1204  AssertIsFinite(factor);
1205  Assert(factor != Number(0.), ExcZero());
1206 
1207  this->operator*=(Number(1.) / factor);
1208  return *this;
1209 }
1210 
1211 
1212 
1213 template <typename Number>
1214 template <typename OtherNumber>
1215 inline void
1216 Vector<Number>::add(const std::vector<size_type> & indices,
1217  const std::vector<OtherNumber> &values)
1218 {
1219  Assert(indices.size() == values.size(),
1220  ExcDimensionMismatch(indices.size(), values.size()));
1221  add(indices.size(), indices.data(), values.data());
1222 }
1223 
1224 
1225 
1226 template <typename Number>
1227 template <typename OtherNumber>
1228 inline void
1229 Vector<Number>::add(const std::vector<size_type> &indices,
1230  const Vector<OtherNumber> & values)
1231 {
1232  Assert(indices.size() == values.size(),
1233  ExcDimensionMismatch(indices.size(), values.size()));
1234  add(indices.size(), indices.data(), values.values.get());
1235 }
1236 
1237 
1238 
1239 template <typename Number>
1240 template <typename OtherNumber>
1241 inline void
1242 Vector<Number>::add(const size_type n_indices,
1243  const size_type * indices,
1244  const OtherNumber *values)
1245 {
1246  for (size_type i = 0; i < n_indices; ++i)
1247  {
1248  Assert(indices[i] < vec_size, ExcIndexRange(indices[i], 0, vec_size));
1249  Assert(
1250  numbers::is_finite(values[i]),
1251  ExcMessage(
1252  "The given value is not finite but either infinite or Not A Number (NaN)"));
1253 
1254  this->values[indices[i]] += values[i];
1255  }
1256 }
1257 
1258 
1259 
1260 template <typename Number>
1261 template <typename Number2>
1262 inline bool
1264 {
1265  return !(*this == v);
1266 }
1267 
1268 
1269 
1270 template <typename Number>
1272 {}
1273 
1274 
1275 
1276 template <typename Number>
1277 inline void
1279 {}
1280 
1281 
1282 
1283 // Moved from vector.templates.h as an inline function by Luca Heltai
1284 // on 2009/04/12 to prevent strange compiling errors, after making
1285 // swap virtual.
1286 template <typename Number>
1287 inline void
1289 {
1290  std::swap(vec_size, v.vec_size);
1291  std::swap(max_vec_size, v.max_vec_size);
1292  std::swap(values, v.values);
1293 }
1294 
1295 
1296 
1297 template <typename Number>
1298 template <class Archive>
1299 inline void
1300 Vector<Number>::save(Archive &ar, const unsigned int) const
1301 {
1302  // forward to serialization function in the base class.
1303  ar &static_cast<const Subscriptor &>(*this);
1304 
1305  ar &vec_size &max_vec_size;
1306  ar & boost::serialization::make_array(values.get(), max_vec_size);
1307 }
1308 
1309 
1310 
1311 template <typename Number>
1312 template <class Archive>
1313 inline void
1314 Vector<Number>::load(Archive &ar, const unsigned int)
1315 {
1316  // get rid of previous content
1317  values.reset();
1318 
1319  // the load stuff again from the archive
1320  ar &static_cast<Subscriptor &>(*this);
1321  ar &vec_size &max_vec_size;
1322 
1323  allocate();
1324  ar &boost::serialization::make_array(values.get(), max_vec_size);
1325 }
1326 
1327 #endif
1328 
1329 
1343 template <typename Number>
1344 inline void
1346 {
1347  u.swap(v);
1348 }
1349 
1350 
1354 template <typename number>
1355 inline std::ostream &
1356 operator<<(std::ostream &os, const Vector<number> &v)
1357 {
1358  v.print(os);
1359  return os;
1360 }
1361 
1365 template <typename number>
1366 inline LogStream &
1367 operator<<(LogStream &os, const Vector<number> &v)
1368 {
1369  v.print(os);
1370  return os;
1371 }
1372 
1381 template <typename Number>
1382 struct is_serial_vector<Vector<Number>> : std::true_type
1383 {};
1384 
1385 
1386 DEAL_II_NAMESPACE_CLOSE
1387 
1388 #endif
void block_read(std::istream &in)
real_type lp_norm(const real_type p) const
real_type norm_sqr() const
IndexSet locally_owned_elements() const
Number value_type
Definition: vector.h:115
real_type l2_norm() const
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
void ratio(const Vector< Number > &a, const Vector< Number > &b)
void load(Archive &ar, const unsigned int version)
Number mean_value() const
void sadd(const Number s, const Vector< Number > &V)
bool is_non_negative() const
size_type size() const
void equ(const Number a, const Vector< Number > &u)
bool in_local_range(const size_type global_index) const
STL namespace.
void compress(::VectorOperation::values operation=::VectorOperation::unknown) const
void update_ghost_values() const
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
iterator end()
Vector< Number > & operator=(const Number s)
bool is_finite(const double x)
Definition: numbers.h:428
void allocate(const size_type copy_n_el=0)
Number operator*(const Vector< Number2 > &V) const
size_type max_vec_size
Definition: vector.h:979
bool all_zero() const
real_type linfty_norm() const
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
bool operator==(const Vector< Number2 > &v) const
static::ExceptionBase & ExcMessage(std::string arg1)
numbers::NumberTraits< Number >::real_type real_type
Definition: vector.h:133
void swap(BlockIndices &u, BlockIndices &v)
void scale(const Vector< Number > &scaling_factors)
Vector< Number > & operator+=(const Vector< Number > &V)
unsigned int global_dof_index
Definition: types.h:89
#define Assert(cond, exc)
Definition: exceptions.h:1227
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
Number operator[](const size_type i) const
void grow_or_shrink(const size_type N)
bool operator!=(const Vector< Number2 > &v) const
Vector< Number > & operator-=(const Vector< Number > &V)
void block_write(std::ostream &out) const
Vector< Number > & operator*=(const Number factor)
iterator begin()
real_type l1_norm() const
std::unique_ptr< Number[], decltype(&free)> values
Definition: vector.h:988
virtual ~Vector() override=default
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1345
virtual void swap(Vector< Number > &v)
Vector< Number > & operator/=(const Number factor)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
size_type vec_size
Definition: vector.h:971
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:995
void save(Archive &ar, const unsigned int version) const
void free(T *&pointer)
Definition: cuda.h:88
Number operator()(const size_type i) const
static::ExceptionBase & ExcZero()
void print(const char *format=nullptr) const
#define AssertIsFinite(number)
Definition: exceptions.h:1423
std::size_t memory_consumption() const