Reference documentation for deal.II version Git 2618e0f 2017-11-23 17:25:26 +0100
vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2017 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);
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 
199  explicit Vector (const TrilinosWrappers::MPI::Vector &v);
200 #endif
201 
211  explicit Vector (const size_type n);
212 
217  template <typename InputIterator>
218  Vector (const InputIterator first,
219  const InputIterator last);
220 
225  virtual ~Vector () = default;
226 
240  void compress (::VectorOperation::values operation
241  =::VectorOperation::unknown) const;
242 
258  virtual void reinit (const size_type N,
259  const bool omit_zeroing_entries=false);
260 
268  template <typename Number2>
269  void reinit (const Vector<Number2> &V,
270  const bool omit_zeroing_entries=false);
271 
287  virtual void swap (Vector<Number> &v);
288 
302  Vector<Number> &operator= (const Number s);
303 
310 
311 #ifdef DEAL_II_WITH_CXX11
312 
318 #endif
319 
325  template <typename Number2>
327 
332 
333 #ifdef DEAL_II_WITH_PETSC
334 
347 #endif
348 
349 
350 #ifdef DEAL_II_WITH_TRILINOS
351 
364 #endif
365 
371  template <typename Number2>
372  bool operator== (const Vector<Number2> &v) const;
373 
379  template <typename Number2>
380  bool operator != (const Vector<Number2> &v) const;
381 
383 
384 
389 
403  template <typename Number2>
404  Number operator * (const Vector<Number2> &V) const;
405 
413  real_type norm_sqr () const;
414 
422  Number mean_value () const;
423 
431  real_type l1_norm () const;
432 
441  real_type l2_norm () const;
442 
451  real_type lp_norm (const real_type p) const;
452 
456  real_type linfty_norm () const;
457 
478  Number add_and_dot (const Number a,
479  const Vector<Number> &V,
480  const Vector<Number> &W);
481 
483 
484 
489 
495  iterator begin ();
496 
500  const_iterator begin () const;
501 
505  iterator end ();
506 
511  const_iterator end () const;
512 
516  Number operator() (const size_type i) const;
517 
521  Number &operator() (const size_type i);
522 
528  Number operator[] (const size_type i) const;
529 
535  Number &operator[] (const size_type i);
536 
552  template <typename OtherNumber>
553  void extract_subvector_to (const std::vector<size_type> &indices,
554  std::vector<OtherNumber> &values) const;
555 
583  template <typename ForwardIterator, typename OutputIterator>
584  void extract_subvector_to (ForwardIterator indices_begin,
585  const ForwardIterator indices_end,
586  OutputIterator values_begin) const;
588 
589 
594 
601 
608 
613  template <typename OtherNumber>
614  void add (const std::vector<size_type> &indices,
615  const std::vector<OtherNumber> &values);
616 
621  template <typename OtherNumber>
622  void add (const std::vector<size_type> &indices,
623  const Vector<OtherNumber> &values);
624 
630  template <typename OtherNumber>
631  void add (const size_type n_elements,
632  const size_type *indices,
633  const OtherNumber *values);
634 
641  void add (const Number s);
642 
648  void add (const Number a, const Vector<Number> &V,
649  const Number b, const Vector<Number> &W);
650 
656  void add (const Number a, const Vector<Number> &V);
657 
663  void sadd (const Number s,
664  const Vector<Number> &V);
665 
671  void sadd (const Number s,
672  const Number a,
673  const Vector<Number> &V);
674 
680  Vector<Number> &operator *= (const Number factor);
681 
687  Vector<Number> &operator /= (const Number factor);
688 
696  void scale (const Vector<Number> &scaling_factors);
697 
703  template <typename Number2>
704  void scale (const Vector<Number2> &scaling_factors);
705 
711  void equ (const Number a, const Vector<Number> &u);
712 
716  template <typename Number2>
717  void equ (const Number a, const Vector<Number2> &u);
718 
731  DEAL_II_DEPRECATED
732  void ratio (const Vector<Number> &a,
733  const Vector<Number> &b);
734 
739  void update_ghost_values () const;
741 
742 
753  DEAL_II_DEPRECATED
754  void print (const char *format = nullptr) const;
755 
762  void print (std::ostream &out,
763  const unsigned int precision = 3,
764  const bool scientific = true,
765  const bool across = true) const;
766 
775  DEAL_II_DEPRECATED
776  void print (LogStream &out,
777  const unsigned int width = 6,
778  const bool across = true) const;
779 
785  void block_write (std::ostream &out) const;
786 
798  void block_read (std::istream &in);
799 
804  template <class Archive>
805  void save (Archive &ar, const unsigned int version) const;
806 
811  template <class Archive>
812  void load (Archive &ar, const unsigned int version);
813 
814  BOOST_SERIALIZATION_SPLIT_MEMBER()
815 
816 
824 
830  bool in_local_range (const size_type global_index) const;
831 
848 
852  std::size_t size () const;
853 
859  bool all_zero () const;
860 
870  bool is_non_negative () const;
871 
876  std::size_t memory_consumption () const;
878 
879 protected:
880 
885  size_type vec_size;
886 
893  size_type max_vec_size;
894 
902  std::unique_ptr<Number[], decltype(&free)> values;
903 
908  mutable std::shared_ptr<parallel::internal::TBBPartitioner> thread_loop_partitioner;
909 
913  template <typename Number2> friend class Vector;
914 
918  template <typename Number2> friend class LAPACKFullMatrix;
919 
923  friend class VectorView<Number>;
924 
925 private:
926 
931  void allocate();
932 };
933 
935 /*----------------------- Inline functions ----------------------------------*/
936 
937 
938 #ifndef DOXYGEN
939 
940 
941 //------------------------ declarations for explicit specializations
942 template <>
944 Vector<int>::lp_norm (const real_type) const;
945 
946 
947 //------------------------ inline functions
948 
949 template <typename Number>
950 inline
952  :
953  vec_size(0),
954  max_vec_size(0),
955  values(nullptr, &free)
956 {
957  reinit(0);
958 }
959 
960 
961 
962 template <typename Number>
963 template <typename InputIterator>
964 Vector<Number>::Vector (const InputIterator first, const InputIterator last)
965  :
966  vec_size (0),
967  max_vec_size (0),
968  values (nullptr, &free)
969 {
970  // allocate memory. do not initialize it, as we will copy over to it in a
971  // second
972  reinit (std::distance (first, last), true);
973  std::copy (first, last, begin());
974 }
975 
976 
977 
978 template <typename Number>
979 inline
980 Vector<Number>::Vector (const size_type n)
981  :
982  vec_size(0),
983  max_vec_size(0),
984  values(nullptr, &free)
985 {
986  reinit (n, false);
987 }
988 
989 
990 
991 template <typename Number>
992 inline
993 std::size_t Vector<Number>::size () const
994 {
995  return vec_size;
996 }
997 
998 
999 template <typename Number>
1000 inline
1002 (const size_type) const
1003 {
1004  return true;
1005 }
1006 
1007 
1008 
1009 template <typename Number>
1010 inline
1011 typename Vector<Number>::iterator
1013 {
1014  return values.get();
1015 }
1016 
1017 
1018 
1019 template <typename Number>
1020 inline
1021 typename Vector<Number>::const_iterator
1022 Vector<Number>::begin () const
1023 {
1024  return values.get();
1025 }
1026 
1027 
1028 
1029 template <typename Number>
1030 inline
1031 typename Vector<Number>::iterator
1033 {
1034  return values.get() + vec_size;
1035 }
1036 
1037 
1038 
1039 template <typename Number>
1040 inline
1041 typename Vector<Number>::const_iterator
1042 Vector<Number>::end () const
1043 {
1044  return values.get() + vec_size;
1045 }
1046 
1047 
1048 
1049 template <typename Number>
1050 inline
1051 Number Vector<Number>::operator() (const size_type i) const
1052 {
1053  Assert (i<vec_size, ExcIndexRange(i,0,vec_size));
1054  return values[i];
1055 }
1056 
1057 
1058 
1059 template <typename Number>
1060 inline
1061 Number &Vector<Number>::operator() (const size_type i)
1062 {
1063  Assert (i<vec_size, ExcIndexRangeType<size_type>(i,0,vec_size));
1064  return values[i];
1065 }
1066 
1067 
1068 
1069 template <typename Number>
1070 inline
1071 Number Vector<Number>::operator[] (const size_type i) const
1072 {
1073  return operator()(i);
1074 }
1075 
1076 
1077 
1078 template <typename Number>
1079 inline
1080 Number &Vector<Number>::operator[] (const size_type i)
1081 {
1082  return operator()(i);
1083 }
1084 
1085 
1086 
1087 template <typename Number>
1088 template <typename OtherNumber>
1089 inline
1090 void Vector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
1091  std::vector<OtherNumber> &values) const
1092 {
1093  for (size_type i = 0; i < indices.size(); ++i)
1094  values[i] = operator()(indices[i]);
1095 }
1096 
1097 
1098 
1099 template <typename Number>
1100 template <typename ForwardIterator, typename OutputIterator>
1101 inline
1102 void Vector<Number>::extract_subvector_to (ForwardIterator indices_begin,
1103  const ForwardIterator indices_end,
1104  OutputIterator values_begin) const
1105 {
1106  while (indices_begin != indices_end)
1107  {
1108  *values_begin = operator()(*indices_begin);
1109  indices_begin++;
1110  values_begin++;
1111  }
1112 }
1113 
1114 
1115 
1116 template <typename Number>
1117 inline
1119 Vector<Number>::operator /= (const Number factor)
1120 {
1121  AssertIsFinite(factor);
1122  Assert (factor != Number(0.), ExcZero() );
1123 
1124  this->operator *= (Number(1.)/factor);
1125  return *this;
1126 }
1127 
1128 
1129 
1130 template <typename Number>
1131 template <typename OtherNumber>
1132 inline
1133 void
1134 Vector<Number>::add (const std::vector<size_type> &indices,
1135  const std::vector<OtherNumber> &values)
1136 {
1137  Assert (indices.size() == values.size(),
1138  ExcDimensionMismatch(indices.size(), values.size()));
1139  add (indices.size(), indices.data(), values.data());
1140 }
1141 
1142 
1143 
1144 template <typename Number>
1145 template <typename OtherNumber>
1146 inline
1147 void
1148 Vector<Number>::add (const std::vector<size_type> &indices,
1149  const Vector<OtherNumber> &values)
1150 {
1151  Assert (indices.size() == values.size(),
1152  ExcDimensionMismatch(indices.size(), values.size()));
1153  add (indices.size(), indices.data(), values.values.get());
1154 }
1155 
1156 
1157 
1158 template <typename Number>
1159 template <typename OtherNumber>
1160 inline
1161 void
1162 Vector<Number>::add (const size_type n_indices,
1163  const size_type *indices,
1164  const OtherNumber *values)
1165 {
1166  for (size_type i=0; i<n_indices; ++i)
1167  {
1168  Assert (indices[i] < vec_size, ExcIndexRange(indices[i],0,vec_size));
1169  Assert (numbers::is_finite(values[i]),
1170  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
1171 
1172  this->values[indices[i]] += values[i];
1173  }
1174 }
1175 
1176 
1177 
1178 template <typename Number>
1179 template <typename Number2>
1180 inline
1181 bool
1183 {
1184  return ! (*this == v);
1185 }
1186 
1187 
1188 
1189 template <typename Number>
1190 inline
1191 void
1193 {}
1194 
1195 
1196 
1197 template <typename Number>
1198 inline
1199 void
1201 {}
1202 
1203 
1204 
1205 // Moved from vector.templates.h as an inline function by Luca Heltai
1206 // on 2009/04/12 to prevent strange compiling errors, after making
1207 // swap virtual.
1208 template <typename Number>
1209 inline
1210 void
1212 {
1213  std::swap (vec_size, v.vec_size);
1214  std::swap (max_vec_size, v.max_vec_size);
1215  std::swap (values, v.values);
1216 }
1217 
1218 
1219 
1220 template <typename Number>
1221 template <class Archive>
1222 inline
1223 void
1224 Vector<Number>::save (Archive &ar, const unsigned int) const
1225 {
1226  // forward to serialization function in the base class.
1227  ar &static_cast<const Subscriptor &>(*this);
1228 
1229  ar &vec_size &max_vec_size ;
1230  ar &boost::serialization::make_array(values.get(), max_vec_size);
1231 }
1232 
1233 
1234 
1235 template <typename Number>
1236 template <class Archive>
1237 inline
1238 void
1239 Vector<Number>::load (Archive &ar, const unsigned int)
1240 {
1241  // get rid of previous content
1242  values.reset();
1243 
1244  // the load stuff again from the archive
1245  ar &static_cast<Subscriptor &>(*this);
1246  ar &vec_size &max_vec_size ;
1247 
1248  allocate();
1249  ar &boost::serialization::make_array(values.get(), max_vec_size);
1250 }
1251 
1252 #endif
1253 
1254 
1268 template <typename Number>
1269 inline
1271 {
1272  u.swap (v);
1273 }
1274 
1275 
1279 template <typename number>
1280 inline
1281 std::ostream &
1282 operator << (std::ostream &os, const Vector<number> &v)
1283 {
1284  v.print(os);
1285  return os;
1286 }
1287 
1291 template <typename number>
1292 inline
1293 LogStream &
1294 operator << (LogStream &os, const Vector<number> &v)
1295 {
1296  v.print(os);
1297  return os;
1298 }
1299 
1308 template <typename Number>
1309 struct is_serial_vector< Vector<Number> > : std::true_type
1310 {
1311 };
1312 
1313 
1314 DEAL_II_NAMESPACE_CLOSE
1315 
1316 #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:110
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)
virtual ~Vector()=default
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
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)
void allocate()
bool is_finite(const double x)
Definition: numbers.h:293
Number operator*(const Vector< Number2 > &V) const
size_type max_vec_size
Definition: vector.h:893
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:128
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:337
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
Number operator[](const size_type i) const
std::size_t size() const
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:902
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1270
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:885
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:908
void save(Archive &ar, const unsigned int version) const
Number operator()(const size_type i) const
static::ExceptionBase & ExcZero()
void print(const char *format=nullptr) const
#define AssertIsFinite(number)
Definition: exceptions.h:1219
std::size_t memory_consumption() const