Reference documentation for deal.II version 9.0.0
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 #include <deal.II/base/logstream.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/subscriptor.h>
24 #include <deal.II/base/index_set.h>
25 #include <deal.II/lac/vector_operation.h>
26 #include <deal.II/lac/vector_type_traits.h>
27 
28 // boost::serialization::make_array used to be in array.hpp, but was
29 // moved to a different file in BOOST 1.64
30 #include <boost/version.hpp>
31 #if BOOST_VERSION >= 106400
32 # include <boost/serialization/array_wrapper.hpp>
33 #else
34 # include <boost/serialization/array.hpp>
35 #endif
36 #include <boost/serialization/split_member.hpp>
37 
38 #include <cstdio>
39 #include <iostream>
40 #include <cstring>
41 #include <vector>
42 
43 DEAL_II_NAMESPACE_OPEN
44 
45 
46 #ifdef DEAL_II_WITH_PETSC
47 namespace PETScWrappers
48 {
49  class VectorBase;
50 }
51 #endif
52 
53 #ifdef DEAL_II_WITH_TRILINOS
54 namespace TrilinosWrappers
55 {
56  namespace MPI
57  {
58  class Vector;
59  }
60 }
61 #endif
62 
63 template <typename number> class LAPACKFullMatrix;
64 
65 template <typename> class BlockVector;
66 
67 template <typename> class VectorView;
68 
69 namespace parallel
70 {
71  namespace internal
72  {
73  class TBBPartitioner;
74  }
75 }
76 
77 
78 
102 template <typename Number>
103 class Vector : public Subscriptor
104 {
105 public:
110  typedef Number value_type;
111  typedef value_type *pointer;
112  typedef const value_type *const_pointer;
113  typedef value_type *iterator;
114  typedef const value_type *const_iterator;
115  typedef value_type &reference;
116  typedef const value_type &const_reference;
117  typedef types::global_dof_index size_type;
118 
129 
130 public:
131 
139  Vector ();
140 
148  Vector (const Vector<Number> &v);
149 
154  Vector (Vector<Number> &&v) noexcept;
155 
168  template <typename OtherNumber>
169  explicit
170  Vector (const Vector<OtherNumber> &v);
171 
172 #ifdef DEAL_II_WITH_PETSC
173 
184  explicit Vector (const PETScWrappers::VectorBase &v);
185 #endif
186 
187 #ifdef DEAL_II_WITH_TRILINOS
188 
202  explicit Vector (const TrilinosWrappers::MPI::Vector &v);
203 #endif
204 
214  explicit Vector (const size_type n);
215 
220  template <typename InputIterator>
221  Vector (const InputIterator first,
222  const InputIterator last);
223 
228  virtual ~Vector () = default;
229 
243  void compress (::VectorOperation::values operation
244  =::VectorOperation::unknown) const;
245 
261  virtual void reinit (const size_type N,
262  const bool omit_zeroing_entries=false);
263 
287  void grow_or_shrink (const size_type N);
288 
296  template <typename Number2>
297  void reinit (const Vector<Number2> &V,
298  const bool omit_zeroing_entries=false);
299 
315  virtual void swap (Vector<Number> &v);
316 
330  Vector<Number> &operator= (const Number s);
331 
337  Vector<Number> &operator= (const Vector<Number> &v);
338 
344  Vector<Number> &operator= (Vector<Number> &&v) noexcept;
345 
351  template <typename Number2>
352  Vector<Number> &operator= (const Vector<Number2> &v);
353 
357  Vector<Number> &operator= (const BlockVector<Number> &v);
358 
359 #ifdef DEAL_II_WITH_PETSC
360 
371  Vector<Number> &
373 #endif
374 
375 
376 #ifdef DEAL_II_WITH_TRILINOS
377 
392  Vector<Number> &
394 #endif
395 
401  template <typename Number2>
402  bool operator== (const Vector<Number2> &v) const;
403 
409  template <typename Number2>
410  bool operator != (const Vector<Number2> &v) const;
411 
413 
414 
419 
433  template <typename Number2>
434  Number operator * (const Vector<Number2> &V) const;
435 
443  real_type norm_sqr () const;
444 
452  Number mean_value () const;
453 
461  real_type l1_norm () const;
462 
471  real_type l2_norm () const;
472 
481  real_type lp_norm (const real_type p) const;
482 
486  real_type linfty_norm () const;
487 
511  Number
512  add_and_dot (const Number a,
513  const Vector<Number> &V,
514  const Vector<Number> &W);
515 
517 
518 
523 
529  iterator begin ();
530 
534  const_iterator begin () const;
535 
539  iterator end ();
540 
545  const_iterator end () const;
546 
550  Number operator() (const size_type i) const;
551 
555  Number &operator() (const size_type i);
556 
562  Number operator[] (const size_type i) const;
563 
569  Number &operator[] (const size_type i);
570 
586  template <typename OtherNumber>
587  void extract_subvector_to (const std::vector<size_type> &indices,
588  std::vector<OtherNumber> &values) const;
589 
617  template <typename ForwardIterator, typename OutputIterator>
618  void extract_subvector_to (ForwardIterator indices_begin,
619  const ForwardIterator indices_end,
620  OutputIterator values_begin) const;
622 
623 
628 
634  Vector<Number> &operator += (const Vector<Number> &V);
635 
641  Vector<Number> &operator -= (const Vector<Number> &V);
642 
647  template <typename OtherNumber>
648  void add (const std::vector<size_type> &indices,
649  const std::vector<OtherNumber> &values);
650 
655  template <typename OtherNumber>
656  void add (const std::vector<size_type> &indices,
657  const Vector<OtherNumber> &values);
658 
664  template <typename OtherNumber>
665  void add (const size_type n_elements,
666  const size_type *indices,
667  const OtherNumber *values);
668 
675  void add (const Number s);
676 
682  void add (const Number a, const Vector<Number> &V,
683  const Number b, const Vector<Number> &W);
684 
690  void add (const Number a, const Vector<Number> &V);
691 
697  void sadd (const Number s,
698  const Vector<Number> &V);
699 
705  void sadd (const Number s,
706  const Number a,
707  const Vector<Number> &V);
708 
714  Vector<Number> &operator *= (const Number factor);
715 
721  Vector<Number> &operator /= (const Number factor);
722 
730  void scale (const Vector<Number> &scaling_factors);
731 
737  template <typename Number2>
738  void scale (const Vector<Number2> &scaling_factors);
739 
745  void equ (const Number a, const Vector<Number> &u);
746 
750  template <typename Number2>
751  void equ (const Number a, const Vector<Number2> &u);
752 
765  DEAL_II_DEPRECATED
766  void ratio (const Vector<Number> &a,
767  const Vector<Number> &b);
768 
773  void update_ghost_values () const;
775 
776 
787  DEAL_II_DEPRECATED
788  void print (const char *format = nullptr) const;
789 
796  void print (std::ostream &out,
797  const unsigned int precision = 3,
798  const bool scientific = true,
799  const bool across = true) const;
800 
809  DEAL_II_DEPRECATED
810  void print (LogStream &out,
811  const unsigned int width = 6,
812  const bool across = true) const;
813 
819  void block_write (std::ostream &out) const;
820 
832  void block_read (std::istream &in);
833 
838  template <class Archive>
839  void save (Archive &ar, const unsigned int version) const;
840 
845  template <class Archive>
846  void load (Archive &ar, const unsigned int version);
847 
848  BOOST_SERIALIZATION_SPLIT_MEMBER()
849 
850 
858 
864  bool in_local_range (const size_type global_index) const;
865 
882 
886  size_type size () const;
887 
893  bool all_zero () const;
894 
904  bool is_non_negative () const;
905 
910  std::size_t memory_consumption () const;
912 
913 protected:
914 
919  size_type vec_size;
920 
927  size_type max_vec_size;
928 
936  std::unique_ptr<Number[], decltype(&free)> values;
937 
942  mutable std::shared_ptr<parallel::internal::TBBPartitioner> thread_loop_partitioner;
943 
947  template <typename Number2> friend class Vector;
948 
952  template <typename Number2> friend class LAPACKFullMatrix;
953 
957  friend class VectorView<Number>;
958 
959 private:
960 
966  void allocate(const size_type copy_n_el = 0);
967 };
968 
970 /*----------------------- Inline functions ----------------------------------*/
971 
972 
973 #ifndef DOXYGEN
974 
975 
976 //------------------------ declarations for explicit specializations
977 template <>
979 Vector<int>::lp_norm (const real_type) const;
980 
981 
982 //------------------------ inline functions
983 
984 template <typename Number>
985 inline
987  :
988  vec_size(0),
989  max_vec_size(0),
990  values(nullptr, &free)
991 {
992  // virtual functions called in constructors and destructors never use the
993  // override in a derived class
994  // for clarity be explicit on which function is called
996 }
997 
998 
999 
1000 template <typename Number>
1001 template <typename InputIterator>
1002 Vector<Number>::Vector (const InputIterator first, const InputIterator last)
1003  :
1004  vec_size (0),
1005  max_vec_size (0),
1006  values (nullptr, &free)
1007 {
1008  // allocate memory. do not initialize it, as we will copy over to it in a
1009  // second
1010  reinit (std::distance (first, last), true);
1011  std::copy (first, last, begin());
1012 }
1013 
1014 
1015 
1016 template <typename Number>
1017 inline
1018 Vector<Number>::Vector (const size_type n)
1019  :
1020  vec_size(0),
1021  max_vec_size(0),
1022  values(nullptr, &free)
1023 {
1024  // virtual functions called in constructors and destructors never use the
1025  // override in a derived class
1026  // for clarity be explicit on which function is called
1027  Vector<Number>::reinit (n, false);
1028 }
1029 
1030 
1031 
1032 template <typename Number>
1033 inline
1034 typename Vector<Number>::size_type
1035 Vector<Number>::size () const
1036 {
1037  return vec_size;
1038 }
1039 
1040 
1041 template <typename Number>
1042 inline
1044 (const size_type) const
1045 {
1046  return true;
1047 }
1048 
1049 
1050 
1051 template <typename Number>
1052 inline
1053 typename Vector<Number>::iterator
1055 {
1056  return values.get();
1057 }
1058 
1059 
1060 
1061 template <typename Number>
1062 inline
1063 typename Vector<Number>::const_iterator
1064 Vector<Number>::begin () const
1065 {
1066  return values.get();
1067 }
1068 
1069 
1070 
1071 template <typename Number>
1072 inline
1073 typename Vector<Number>::iterator
1075 {
1076  return values.get() + vec_size;
1077 }
1078 
1079 
1080 
1081 template <typename Number>
1082 inline
1083 typename Vector<Number>::const_iterator
1084 Vector<Number>::end () const
1085 {
1086  return values.get() + vec_size;
1087 }
1088 
1089 
1090 
1091 template <typename Number>
1092 inline
1093 Number Vector<Number>::operator() (const size_type i) const
1094 {
1095  Assert (i<vec_size, ExcIndexRange(i,0,vec_size));
1096  return values[i];
1097 }
1098 
1099 
1100 
1101 template <typename Number>
1102 inline
1103 Number &Vector<Number>::operator() (const size_type i)
1104 {
1105  Assert (i<vec_size, ExcIndexRangeType<size_type>(i,0,vec_size));
1106  return values[i];
1107 }
1108 
1109 
1110 
1111 template <typename Number>
1112 inline
1113 Number Vector<Number>::operator[] (const size_type i) const
1114 {
1115  return operator()(i);
1116 }
1117 
1118 
1119 
1120 template <typename Number>
1121 inline
1122 Number &Vector<Number>::operator[] (const size_type i)
1123 {
1124  return operator()(i);
1125 }
1126 
1127 
1128 
1129 template <typename Number>
1130 template <typename OtherNumber>
1131 inline
1132 void Vector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
1133  std::vector<OtherNumber> &values) const
1134 {
1135  for (size_type i = 0; i < indices.size(); ++i)
1136  values[i] = operator()(indices[i]);
1137 }
1138 
1139 
1140 
1141 template <typename Number>
1142 template <typename ForwardIterator, typename OutputIterator>
1143 inline
1144 void Vector<Number>::extract_subvector_to (ForwardIterator indices_begin,
1145  const ForwardIterator indices_end,
1146  OutputIterator values_begin) const
1147 {
1148  while (indices_begin != indices_end)
1149  {
1150  *values_begin = operator()(*indices_begin);
1151  indices_begin++;
1152  values_begin++;
1153  }
1154 }
1155 
1156 
1157 
1158 template <typename Number>
1159 inline
1161 Vector<Number>::operator /= (const Number factor)
1162 {
1163  AssertIsFinite(factor);
1164  Assert (factor != Number(0.), ExcZero() );
1165 
1166  this->operator *= (Number(1.)/factor);
1167  return *this;
1168 }
1169 
1170 
1171 
1172 template <typename Number>
1173 template <typename OtherNumber>
1174 inline
1175 void
1176 Vector<Number>::add (const std::vector<size_type> &indices,
1177  const std::vector<OtherNumber> &values)
1178 {
1179  Assert (indices.size() == values.size(),
1180  ExcDimensionMismatch(indices.size(), values.size()));
1181  add (indices.size(), indices.data(), values.data());
1182 }
1183 
1184 
1185 
1186 template <typename Number>
1187 template <typename OtherNumber>
1188 inline
1189 void
1190 Vector<Number>::add (const std::vector<size_type> &indices,
1191  const Vector<OtherNumber> &values)
1192 {
1193  Assert (indices.size() == values.size(),
1194  ExcDimensionMismatch(indices.size(), values.size()));
1195  add (indices.size(), indices.data(), values.values.get());
1196 }
1197 
1198 
1199 
1200 template <typename Number>
1201 template <typename OtherNumber>
1202 inline
1203 void
1204 Vector<Number>::add (const size_type n_indices,
1205  const size_type *indices,
1206  const OtherNumber *values)
1207 {
1208  for (size_type i=0; i<n_indices; ++i)
1209  {
1210  Assert (indices[i] < vec_size, ExcIndexRange(indices[i],0,vec_size));
1211  Assert (numbers::is_finite(values[i]),
1212  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
1213 
1214  this->values[indices[i]] += values[i];
1215  }
1216 }
1217 
1218 
1219 
1220 template <typename Number>
1221 template <typename Number2>
1222 inline
1223 bool
1225 {
1226  return ! (*this == v);
1227 }
1228 
1229 
1230 
1231 template <typename Number>
1232 inline
1233 void
1235 {}
1236 
1237 
1238 
1239 template <typename Number>
1240 inline
1241 void
1243 {}
1244 
1245 
1246 
1247 // Moved from vector.templates.h as an inline function by Luca Heltai
1248 // on 2009/04/12 to prevent strange compiling errors, after making
1249 // swap virtual.
1250 template <typename Number>
1251 inline
1252 void
1254 {
1255  std::swap (vec_size, v.vec_size);
1256  std::swap (max_vec_size, v.max_vec_size);
1257  std::swap (values, v.values);
1258 }
1259 
1260 
1261 
1262 template <typename Number>
1263 template <class Archive>
1264 inline
1265 void
1266 Vector<Number>::save (Archive &ar, const unsigned int) const
1267 {
1268  // forward to serialization function in the base class.
1269  ar &static_cast<const Subscriptor &>(*this);
1270 
1271  ar &vec_size &max_vec_size ;
1272  ar &boost::serialization::make_array(values.get(), max_vec_size);
1273 }
1274 
1275 
1276 
1277 template <typename Number>
1278 template <class Archive>
1279 inline
1280 void
1281 Vector<Number>::load (Archive &ar, const unsigned int)
1282 {
1283  // get rid of previous content
1284  values.reset();
1285 
1286  // the load stuff again from the archive
1287  ar &static_cast<Subscriptor &>(*this);
1288  ar &vec_size &max_vec_size ;
1289 
1290  allocate();
1291  ar &boost::serialization::make_array(values.get(), max_vec_size);
1292 }
1293 
1294 #endif
1295 
1296 
1310 template <typename Number>
1311 inline
1313 {
1314  u.swap (v);
1315 }
1316 
1317 
1321 template <typename number>
1322 inline
1323 std::ostream &
1324 operator << (std::ostream &os, const Vector<number> &v)
1325 {
1326  v.print(os);
1327  return os;
1328 }
1329 
1333 template <typename number>
1334 inline
1335 LogStream &
1336 operator << (LogStream &os, const Vector<number> &v)
1337 {
1338  v.print(os);
1339  return os;
1340 }
1341 
1350 template <typename Number>
1351 struct is_serial_vector< Vector<Number> > : std::true_type
1352 {
1353 };
1354 
1355 
1356 DEAL_II_NAMESPACE_CLOSE
1357 
1358 #endif
void block_read(std::istream &in)
void update_ghost_values() const
std::size_t memory_consumption() const
Number value_type
Definition: vector.h:110
real_type l1_norm() const
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
Number operator[](const size_type i) const
void ratio(const Vector< Number > &a, const Vector< Number > &b)
bool operator!=(const Vector< Number2 > &v) const
virtual ~Vector()=default
void load(Archive &ar, const unsigned int version)
void sadd(const Number s, const Vector< Number > &V)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
Number operator()(const size_type i) const
real_type linfty_norm() const
bool is_non_negative() const
void equ(const Number a, const Vector< Number > &u)
STL namespace.
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:405
void allocate(const size_type copy_n_el=0)
size_type max_vec_size
Definition: vector.h:927
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
static ::ExceptionBase & ExcMessage(std::string arg1)
numbers::NumberTraits< Number >::real_type real_type
Definition: vector.h:128
void swap(BlockIndices &u, BlockIndices &v)
bool in_local_range(const size_type global_index) const
void scale(const Vector< Number > &scaling_factors)
Vector< Number > & operator+=(const Vector< Number > &V)
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:1142
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
real_type lp_norm(const real_type p) const
void grow_or_shrink(const size_type N)
Vector< Number > & operator-=(const Vector< Number > &V)
Vector< Number > & operator*=(const Number factor)
iterator begin()
std::unique_ptr< Number[], decltype(&free)> values
Definition: vector.h:936
void print(const char *format=nullptr) const
Number operator*(const Vector< Number2 > &V) const
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1312
virtual void swap(Vector< Number > &v)
Vector< Number > & operator/=(const Number factor)
void block_write(std::ostream &out) const
IndexSet locally_owned_elements() const
void save(Archive &ar, const unsigned int version) const
bool operator==(const Vector< Number2 > &v) const
bool all_zero() const
void compress(::VectorOperation::values operation=::VectorOperation::unknown) const
size_type size() const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
real_type norm_sqr() const
size_type vec_size
Definition: vector.h:919
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:942
void free(T *&pointer)
Definition: cuda.h:84
real_type l2_norm() const
static ::ExceptionBase & ExcZero()
#define AssertIsFinite(number)
Definition: exceptions.h:1299
Number mean_value() const