Reference documentation for deal.II version Git 05e4468 2017-09-21 10:18:23 +0200
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_type_traits.h>
26 
27 // boost::serialization::make_array used to be in array.hpp, but was
28 // moved to a different file in BOOST 1.64
29 #include <boost/version.hpp>
30 #if BOOST_VERSION >= 106400
31 # include <boost/serialization/array_wrapper.hpp>
32 #else
33 # include <boost/serialization/array.hpp>
34 #endif
35 #include <boost/serialization/split_member.hpp>
36 
37 #include <cstdio>
38 #include <iostream>
39 #include <cstring>
40 #include <vector>
41 
42 DEAL_II_NAMESPACE_OPEN
43 
44 
45 #ifdef DEAL_II_WITH_PETSC
46 namespace PETScWrappers
47 {
48  class VectorBase;
49 }
50 #endif
51 
52 #ifdef DEAL_II_WITH_TRILINOS
53 namespace TrilinosWrappers
54 {
55  namespace MPI
56  {
57  class Vector;
58  }
59 }
60 #endif
61 
62 template <typename number> class LAPACKFullMatrix;
63 
64 template <typename> class BlockVector;
65 
66 template <typename> class VectorView;
67 
68 namespace parallel
69 {
70  namespace internal
71  {
72  class TBBPartitioner;
73  }
74 }
75 
76 
77 
94 {
95  enum values
96  {
109  };
110 };
111 
112 
132 template <typename Number>
133 class Vector : public Subscriptor
134 {
135 public:
140  typedef Number value_type;
141  typedef value_type *pointer;
142  typedef const value_type *const_pointer;
143  typedef value_type *iterator;
144  typedef const value_type *const_iterator;
145  typedef value_type &reference;
146  typedef const value_type &const_reference;
147  typedef types::global_dof_index size_type;
148 
159 
160 public:
161 
169  Vector ();
170 
178  Vector (const Vector<Number> &v);
179 
184  Vector (Vector<Number> &&v);
185 
198  template <typename OtherNumber>
199  explicit
200  Vector (const Vector<OtherNumber> &v);
201 
202 #ifdef DEAL_II_WITH_PETSC
203 
214  explicit Vector (const PETScWrappers::VectorBase &v);
215 #endif
216 
217 #ifdef DEAL_II_WITH_TRILINOS
218 
229  explicit Vector (const TrilinosWrappers::MPI::Vector &v);
230 #endif
231 
241  explicit Vector (const size_type n);
242 
247  template <typename InputIterator>
248  Vector (const InputIterator first,
249  const InputIterator last);
250 
255  virtual ~Vector ();
256 
270  void compress (::VectorOperation::values operation
271  =::VectorOperation::unknown) const;
272 
288  virtual void reinit (const size_type N,
289  const bool omit_zeroing_entries=false);
290 
298  template <typename Number2>
299  void reinit (const Vector<Number2> &V,
300  const bool omit_zeroing_entries=false);
301 
317  virtual void swap (Vector<Number> &v);
318 
332  Vector<Number> &operator= (const Number s);
333 
340 
341 #ifdef DEAL_II_WITH_CXX11
342 
348 #endif
349 
355  template <typename Number2>
357 
362 
363 #ifdef DEAL_II_WITH_PETSC
364 
377 #endif
378 
379 
380 #ifdef DEAL_II_WITH_TRILINOS
381 
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 
508  Number add_and_dot (const Number a,
509  const Vector<Number> &V,
510  const Vector<Number> &W);
511 
513 
514 
519 
525  iterator begin ();
526 
530  const_iterator begin () const;
531 
535  iterator end ();
536 
541  const_iterator end () const;
542 
546  Number operator() (const size_type i) const;
547 
551  Number &operator() (const size_type i);
552 
558  Number operator[] (const size_type i) const;
559 
565  Number &operator[] (const size_type i);
566 
582  template <typename OtherNumber>
583  void extract_subvector_to (const std::vector<size_type> &indices,
584  std::vector<OtherNumber> &values) const;
585 
613  template <typename ForwardIterator, typename OutputIterator>
614  void extract_subvector_to (ForwardIterator indices_begin,
615  const ForwardIterator indices_end,
616  OutputIterator values_begin) const;
618 
619 
624 
631 
638 
643  template <typename OtherNumber>
644  void add (const std::vector<size_type> &indices,
645  const std::vector<OtherNumber> &values);
646 
651  template <typename OtherNumber>
652  void add (const std::vector<size_type> &indices,
653  const Vector<OtherNumber> &values);
654 
660  template <typename OtherNumber>
661  void add (const size_type n_elements,
662  const size_type *indices,
663  const OtherNumber *values);
664 
671  void add (const Number s);
672 
678  void add (const Number a, const Vector<Number> &V,
679  const Number b, const Vector<Number> &W);
680 
686  void add (const Number a, const Vector<Number> &V);
687 
693  void sadd (const Number s,
694  const Vector<Number> &V);
695 
701  void sadd (const Number s,
702  const Number a,
703  const Vector<Number> &V);
704 
710  Vector<Number> &operator *= (const Number factor);
711 
717  Vector<Number> &operator /= (const Number factor);
718 
726  void scale (const Vector<Number> &scaling_factors);
727 
733  template <typename Number2>
734  void scale (const Vector<Number2> &scaling_factors);
735 
741  void equ (const Number a, const Vector<Number> &u);
742 
746  template <typename Number2>
747  void equ (const Number a, const Vector<Number2> &u);
748 
761  void ratio (const Vector<Number> &a,
762  const Vector<Number> &b) DEAL_II_DEPRECATED;
763 
768  void update_ghost_values () const;
770 
771 
782  void print (const char *format = nullptr) const DEAL_II_DEPRECATED;
783 
790  void print (std::ostream &out,
791  const unsigned int precision = 3,
792  const bool scientific = true,
793  const bool across = true) const;
794 
803  void print (LogStream &out,
804  const unsigned int width = 6,
805  const bool across = true) const DEAL_II_DEPRECATED;
806 
812  void block_write (std::ostream &out) const;
813 
825  void block_read (std::istream &in);
826 
831  template <class Archive>
832  void save (Archive &ar, const unsigned int version) const;
833 
838  template <class Archive>
839  void load (Archive &ar, const unsigned int version);
840 
841  BOOST_SERIALIZATION_SPLIT_MEMBER()
842 
851 
857  bool in_local_range (const size_type global_index) const;
858 
875 
879  std::size_t size () const;
880 
886  bool all_zero () const;
887 
897  bool is_non_negative () const;
898 
903  std::size_t memory_consumption () const;
905 
906 protected:
907 
912  size_type vec_size;
913 
920  size_type max_vec_size;
921 
925  Number *val;
926 
931  mutable std::shared_ptr<parallel::internal::TBBPartitioner> thread_loop_partitioner;
932 
936  template <typename Number2> friend class Vector;
937 
941  template <typename Number2> friend class LAPACKFullMatrix;
942 
946  friend class VectorView<Number>;
947 
948 private:
949 
954  void allocate();
955 
959  void deallocate();
960 };
961 
963 /*----------------------- Inline functions ----------------------------------*/
964 
965 
966 #ifndef DOXYGEN
967 
968 
969 //------------------------ declarations for explicit specializations
970 template <>
972 Vector<int>::lp_norm (const real_type) const;
973 
974 
975 //------------------------ inline functions
976 
977 template <typename Number>
978 inline
980  :
981  vec_size(0),
982  max_vec_size(0),
983  val(nullptr)
984 {
985  reinit(0);
986 }
987 
988 
989 
990 template <typename Number>
991 template <typename InputIterator>
992 Vector<Number>::Vector (const InputIterator first, const InputIterator last)
993  :
994  vec_size (0),
995  max_vec_size (0),
996  val (nullptr)
997 {
998  // allocate memory. do not initialize it, as we will copy over to it in a
999  // second
1000  reinit (std::distance (first, last), true);
1001  std::copy (first, last, begin());
1002 }
1003 
1004 
1005 
1006 template <typename Number>
1007 inline
1008 Vector<Number>::Vector (const size_type n)
1009  :
1010  vec_size(0),
1011  max_vec_size(0),
1012  val(nullptr)
1013 {
1014  reinit (n, false);
1015 }
1016 
1017 
1018 
1019 template <typename Number>
1020 inline
1022 {
1023  if (val)
1024  {
1025  deallocate();
1026  val=nullptr;
1027  }
1028 }
1029 
1030 
1031 
1032 template <typename Number>
1033 inline
1034 std::size_t Vector<Number>::size () const
1035 {
1036  return vec_size;
1037 }
1038 
1039 
1040 template <typename Number>
1041 inline
1043 (const size_type) const
1044 {
1045  return true;
1046 }
1047 
1048 
1049 
1050 template <typename Number>
1051 inline
1052 typename Vector<Number>::iterator
1054 {
1055  return &val[0];
1056 }
1057 
1058 
1059 
1060 template <typename Number>
1061 inline
1062 typename Vector<Number>::const_iterator
1063 Vector<Number>::begin () const
1064 {
1065  return &val[0];
1066 }
1067 
1068 
1069 
1070 template <typename Number>
1071 inline
1072 typename Vector<Number>::iterator
1074 {
1075  return &val[vec_size];
1076 }
1077 
1078 
1079 
1080 template <typename Number>
1081 inline
1082 typename Vector<Number>::const_iterator
1083 Vector<Number>::end () const
1084 {
1085  return &val[vec_size];
1086 }
1087 
1088 
1089 
1090 template <typename Number>
1091 inline
1092 Number Vector<Number>::operator() (const size_type i) const
1093 {
1094  Assert (i<vec_size, ExcIndexRange(i,0,vec_size));
1095  return val[i];
1096 }
1097 
1098 
1099 
1100 template <typename Number>
1101 inline
1102 Number &Vector<Number>::operator() (const size_type i)
1103 {
1104  Assert (i<vec_size, ExcIndexRangeType<size_type>(i,0,vec_size));
1105  return val[i];
1106 }
1107 
1108 
1109 
1110 template <typename Number>
1111 inline
1112 Number Vector<Number>::operator[] (const size_type i) const
1113 {
1114  return operator()(i);
1115 }
1116 
1117 
1118 
1119 template <typename Number>
1120 inline
1121 Number &Vector<Number>::operator[] (const size_type i)
1122 {
1123  return operator()(i);
1124 }
1125 
1126 
1127 
1128 template <typename Number>
1129 template <typename OtherNumber>
1130 inline
1131 void Vector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
1132  std::vector<OtherNumber> &values) const
1133 {
1134  for (size_type i = 0; i < indices.size(); ++i)
1135  values[i] = operator()(indices[i]);
1136 }
1137 
1138 
1139 
1140 template <typename Number>
1141 template <typename ForwardIterator, typename OutputIterator>
1142 inline
1143 void Vector<Number>::extract_subvector_to (ForwardIterator indices_begin,
1144  const ForwardIterator indices_end,
1145  OutputIterator values_begin) const
1146 {
1147  while (indices_begin != indices_end)
1148  {
1149  *values_begin = operator()(*indices_begin);
1150  indices_begin++;
1151  values_begin++;
1152  }
1153 }
1154 
1155 
1156 
1157 template <typename Number>
1158 inline
1160 Vector<Number>::operator /= (const Number factor)
1161 {
1162  AssertIsFinite(factor);
1163  Assert (factor != Number(0.), ExcZero() );
1164 
1165  this->operator *= (Number(1.)/factor);
1166  return *this;
1167 }
1168 
1169 
1170 
1171 template <typename Number>
1172 template <typename OtherNumber>
1173 inline
1174 void
1175 Vector<Number>::add (const std::vector<size_type> &indices,
1176  const std::vector<OtherNumber> &values)
1177 {
1178  Assert (indices.size() == values.size(),
1179  ExcDimensionMismatch(indices.size(), values.size()));
1180  add (indices.size(), indices.data(), values.data());
1181 }
1182 
1183 
1184 
1185 template <typename Number>
1186 template <typename OtherNumber>
1187 inline
1188 void
1189 Vector<Number>::add (const std::vector<size_type> &indices,
1190  const Vector<OtherNumber> &values)
1191 {
1192  Assert (indices.size() == values.size(),
1193  ExcDimensionMismatch(indices.size(), values.size()));
1194  add (indices.size(), indices.data(), values.val);
1195 }
1196 
1197 
1198 
1199 template <typename Number>
1200 template <typename OtherNumber>
1201 inline
1202 void
1203 Vector<Number>::add (const size_type n_indices,
1204  const size_type *indices,
1205  const OtherNumber *values)
1206 {
1207  for (size_type i=0; i<n_indices; ++i)
1208  {
1209  Assert (indices[i] < vec_size, ExcIndexRange(indices[i],0,vec_size));
1210  Assert (numbers::is_finite(values[i]),
1211  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
1212 
1213  val[indices[i]] += values[i];
1214  }
1215 }
1216 
1217 
1218 
1219 template <typename Number>
1220 template <typename Number2>
1221 inline
1222 bool
1224 {
1225  return ! (*this == v);
1226 }
1227 
1228 
1229 
1230 template <typename Number>
1231 inline
1232 void
1234 {}
1235 
1236 
1237 
1238 template <typename Number>
1239 inline
1240 void
1242 {}
1243 
1244 
1245 
1246 // Moved from vector.templates.h as an inline function by Luca Heltai
1247 // on 2009/04/12 to prevent strange compiling errors, after making
1248 // swap virtual.
1249 template <typename Number>
1250 inline
1251 void
1253 {
1254  std::swap (vec_size, v.vec_size);
1255  std::swap (max_vec_size, v.max_vec_size);
1256  std::swap (val, v.val);
1257 }
1258 
1259 
1260 
1261 template <typename Number>
1262 template <class Archive>
1263 inline
1264 void
1265 Vector<Number>::save (Archive &ar, const unsigned int) const
1266 {
1267  // forward to serialization function in the base class.
1268  ar &static_cast<const Subscriptor &>(*this);
1269 
1270  ar &vec_size &max_vec_size ;
1271  ar &boost::serialization::make_array(val, max_vec_size);
1272 }
1273 
1274 
1275 
1276 template <typename Number>
1277 template <class Archive>
1278 inline
1279 void
1280 Vector<Number>::load (Archive &ar, const unsigned int)
1281 {
1282  // get rid of previous content
1283  deallocate();
1284 
1285  // the load stuff again from the archive
1286  ar &static_cast<Subscriptor &>(*this);
1287  ar &vec_size &max_vec_size ;
1288 
1289  allocate();
1290  ar &boost::serialization::make_array(val, max_vec_size);
1291 }
1292 
1293 #endif
1294 
1295 
1309 template <typename Number>
1310 inline
1312 {
1313  u.swap (v);
1314 }
1315 
1316 
1320 template <typename number>
1321 inline
1322 std::ostream &
1323 operator << (std::ostream &os, const Vector<number> &v)
1324 {
1325  v.print(os);
1326  return os;
1327 }
1328 
1332 template <typename number>
1333 inline
1334 LogStream &
1335 operator << (LogStream &os, const Vector<number> &v)
1336 {
1337  v.print(os);
1338  return os;
1339 }
1340 
1349 template <typename Number>
1350 struct is_serial_vector< Vector<Number> > : std::true_type
1351 {
1352 };
1353 
1354 
1355 DEAL_II_NAMESPACE_CLOSE
1356 
1357 #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:140
real_type l2_norm() const
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
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:292
Number operator*(const Vector< Number2 > &V) const
size_type max_vec_size
Definition: vector.h:920
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:158
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
virtual ~Vector()
void ratio(const Vector< Number > &a, const Vector< Number > &b) 1
Vector< Number > & operator-=(const Vector< Number > &V)
void print(const char *format=nullptr) const 1
void block_write(std::ostream &out) const
Vector< Number > & operator*=(const Number factor)
iterator begin()
real_type l1_norm() const
void deallocate()
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1311
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:912
std::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:931
void save(Archive &ar, const unsigned int version) const
Number * val
Definition: vector.h:925
Number operator()(const size_type i) const
static::ExceptionBase & ExcZero()
#define AssertIsFinite(number)
Definition: exceptions.h:1211
std::size_t memory_consumption() const