Reference documentation for deal.II version 8.4.2
trilinos_vector_base.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2015 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__trilinos_vector_base_h
17 #define dealii__trilinos_vector_base_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_TRILINOS
23 
24 #include <deal.II/base/utilities.h>
25 # include <deal.II/base/std_cxx11/shared_ptr.h>
26 # include <deal.II/base/subscriptor.h>
27 # include <deal.II/lac/exceptions.h>
28 # include <deal.II/lac/vector.h>
29 
30 # include <vector>
31 # include <utility>
32 # include <memory>
33 
34 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
35 # define TrilinosScalar double
36 # include "Epetra_ConfigDefs.h"
37 # ifdef DEAL_II_WITH_MPI // only if MPI is installed
38 # include "mpi.h"
39 # include "Epetra_MpiComm.h"
40 # else
41 # include "Epetra_SerialComm.h"
42 # endif
43 # include "Epetra_FEVector.h"
45 
46 DEAL_II_NAMESPACE_OPEN
47 
48 // forward declaration
49 template <typename number> class Vector;
50 
51 
56 namespace TrilinosWrappers
57 {
58  // forward declaration
59  class VectorBase;
60 
71  namespace internal
72  {
76  typedef ::types::global_dof_index size_type;
77 
87  class VectorReference
88  {
89  private:
94  VectorReference (VectorBase &vector,
95  const size_type index);
96 
97  public:
98 
110  const VectorReference &
111  operator = (const VectorReference &r) const;
112 
116  const VectorReference &
117  operator = (const VectorReference &r);
118 
122  const VectorReference &
123  operator = (const TrilinosScalar &s) const;
124 
128  const VectorReference &
129  operator += (const TrilinosScalar &s) const;
130 
134  const VectorReference &
135  operator -= (const TrilinosScalar &s) const;
136 
140  const VectorReference &
141  operator *= (const TrilinosScalar &s) const;
142 
146  const VectorReference &
147  operator /= (const TrilinosScalar &s) const;
148 
153  operator TrilinosScalar () const;
154 
158  DeclException1 (ExcTrilinosError,
159  int,
160  << "An error with error number " << arg1
161  << " occurred while calling a Trilinos function");
162 
163  private:
167  VectorBase &vector;
168 
172  const size_type index;
173 
178  friend class ::TrilinosWrappers::VectorBase;
179  };
180  }
216  class VectorBase : public Subscriptor
217  {
218  public:
224  typedef TrilinosScalar value_type;
225  typedef TrilinosScalar real_type;
226  typedef ::types::global_dof_index size_type;
227  typedef value_type *iterator;
228  typedef const value_type *const_iterator;
229  typedef internal::VectorReference reference;
230  typedef const internal::VectorReference const_reference;
231 
236 
242  VectorBase ();
243 
248  VectorBase (const VectorBase &v);
249 
253  virtual ~VectorBase ();
254 
259  void clear ();
260 
266  void reinit (const VectorBase &v,
267  const bool omit_zeroing_entries = false);
268 
285  void compress (::VectorOperation::values operation);
286 
293  bool is_compressed () const DEAL_II_DEPRECATED;
294 
307  VectorBase &
308  operator = (const TrilinosScalar s);
309 
315  VectorBase &
316  operator = (const VectorBase &v);
317 
326  template <typename Number>
327  VectorBase &
328  operator = (const ::Vector<Number> &v);
329 
335  bool operator == (const VectorBase &v) const;
336 
342  bool operator != (const VectorBase &v) const;
343 
347  size_type size () const;
348 
360  size_type local_size () const;
361 
383  std::pair<size_type, size_type> local_range () const;
384 
392  bool in_local_range (const size_type index) const;
393 
407  IndexSet locally_owned_elements () const;
408 
416  bool has_ghost_elements() const;
417 
422  TrilinosScalar operator * (const VectorBase &vec) const;
423 
427  real_type norm_sqr () const;
428 
432  TrilinosScalar mean_value () const;
433 
439  TrilinosScalar minimal_value () const DEAL_II_DEPRECATED;
440 
444  TrilinosScalar min () const;
445 
449  TrilinosScalar max () const;
450 
454  real_type l1_norm () const;
455 
460  real_type l2_norm () const;
461 
466  real_type lp_norm (const TrilinosScalar p) const;
467 
471  real_type linfty_norm () const;
472 
488  TrilinosScalar add_and_dot (const TrilinosScalar a,
489  const VectorBase &V,
490  const VectorBase &W);
491 
497  bool all_zero () const;
498 
504  bool is_non_negative () const;
506 
507 
512 
522  reference
523  operator () (const size_type index);
524 
534  TrilinosScalar
535  operator () (const size_type index) const;
536 
542  reference
543  operator [] (const size_type index);
544 
550  TrilinosScalar
551  operator [] (const size_type index) const;
552 
559  void extract_subvector_to (const std::vector<size_type> &indices,
560  std::vector<TrilinosScalar> &values) const;
561 
566  template <typename ForwardIterator, typename OutputIterator>
567  void extract_subvector_to (ForwardIterator indices_begin,
568  const ForwardIterator indices_end,
569  OutputIterator values_begin) const;
570 
581  TrilinosScalar el (const size_type index) const DEAL_II_DEPRECATED;
582 
593  iterator begin ();
594 
599  const_iterator begin () const;
600 
605  iterator end ();
606 
611  const_iterator end () const;
612 
614 
615 
620 
627  void set (const std::vector<size_type> &indices,
628  const std::vector<TrilinosScalar> &values);
629 
634  void set (const std::vector<size_type> &indices,
635  const ::Vector<TrilinosScalar> &values);
636 
642  void set (const size_type n_elements,
643  const size_type *indices,
644  const TrilinosScalar *values);
645 
650  void add (const std::vector<size_type> &indices,
651  const std::vector<TrilinosScalar> &values);
652 
657  void add (const std::vector<size_type> &indices,
658  const ::Vector<TrilinosScalar> &values);
659 
665  void add (const size_type n_elements,
666  const size_type *indices,
667  const TrilinosScalar *values);
668 
672  VectorBase &operator *= (const TrilinosScalar factor);
673 
677  VectorBase &operator /= (const TrilinosScalar factor);
678 
682  VectorBase &operator += (const VectorBase &V);
683 
687  VectorBase &operator -= (const VectorBase &V);
688 
693  void add (const TrilinosScalar s);
694 
707  void add (const VectorBase &V,
708  const bool allow_different_maps = false);
709 
713  void add (const TrilinosScalar a,
714  const VectorBase &V);
715 
719  void add (const TrilinosScalar a,
720  const VectorBase &V,
721  const TrilinosScalar b,
722  const VectorBase &W);
723 
728  void sadd (const TrilinosScalar s,
729  const VectorBase &V);
730 
734  void sadd (const TrilinosScalar s,
735  const TrilinosScalar a,
736  const VectorBase &V);
737 
743  void sadd (const TrilinosScalar s,
744  const TrilinosScalar a,
745  const VectorBase &V,
746  const TrilinosScalar b,
747  const VectorBase &W) DEAL_II_DEPRECATED;
748 
755  void sadd (const TrilinosScalar s,
756  const TrilinosScalar a,
757  const VectorBase &V,
758  const TrilinosScalar b,
759  const VectorBase &W,
760  const TrilinosScalar c,
761  const VectorBase &X) DEAL_II_DEPRECATED;
762 
768  void scale (const VectorBase &scaling_factors);
769 
773  void equ (const TrilinosScalar a,
774  const VectorBase &V);
775 
781  void equ (const TrilinosScalar a,
782  const VectorBase &V,
783  const TrilinosScalar b,
784  const VectorBase &W) DEAL_II_DEPRECATED;
785 
796  void ratio (const VectorBase &a,
797  const VectorBase &b) DEAL_II_DEPRECATED;
799 
800 
805 
810  const Epetra_MultiVector &trilinos_vector () const;
811 
816  Epetra_FEVector &trilinos_vector ();
817 
822  const Epetra_Map &vector_partitioner () const;
823 
830  void print (const char *format = 0) const DEAL_II_DEPRECATED;
831 
839  void print (std::ostream &out,
840  const unsigned int precision = 3,
841  const bool scientific = true,
842  const bool across = true) const;
843 
857  void swap (VectorBase &v);
858 
862  std::size_t memory_consumption () const;
863 
868  const MPI_Comm &get_mpi_communicator () const;
870 
874  DeclException0 (ExcDifferentParallelPartitioning);
875 
879  DeclException1 (ExcTrilinosError,
880  int,
881  << "An error with error number " << arg1
882  << " occurred while calling a Trilinos function");
883 
887  DeclException4 (ExcAccessToNonLocalElement,
888  size_type, size_type, size_type, size_type,
889  << "You tried to access element " << arg1
890  << " of a distributed vector, but this element is not stored "
891  << "on the current processor. Note: There are "
892  << arg2 << " elements stored "
893  << "on the current processor from within the range "
894  << arg3 << " through " << arg4
895  << " but Trilinos vectors need not store contiguous "
896  << "ranges on each processor, and not every element in "
897  << "this range may in fact be stored locally.");
898 
899 
900  private:
912  Epetra_CombineMode last_action;
913 
918  bool compressed;
919 
924  bool has_ghosts;
925 
931  std_cxx11::shared_ptr<Epetra_FEVector> vector;
932 
938  std_cxx11::shared_ptr<Epetra_MultiVector> nonlocal_vector;
939 
943  friend class internal::VectorReference;
944  friend class Vector;
945  friend class MPI::Vector;
946  };
947 
948 
949 
950 
951 // ------------------- inline and template functions --------------
952 
961  inline
962  void swap (VectorBase &u, VectorBase &v)
963  {
964  u.swap (v);
965  }
966 
967 
968 #ifndef DOXYGEN
969 
970  namespace internal
971  {
972  inline
973  VectorReference::VectorReference (VectorBase &vector,
974  const size_type index)
975  :
976  vector (vector),
977  index (index)
978  {}
979 
980 
981  inline
982  const VectorReference &
983  VectorReference::operator = (const VectorReference &r) const
984  {
985  // as explained in the class
986  // documentation, this is not the copy
987  // operator. so simply pass on to the
988  // "correct" assignment operator
989  *this = static_cast<TrilinosScalar> (r);
990 
991  return *this;
992  }
993 
994 
995 
996  inline
997  const VectorReference &
998  VectorReference::operator = (const VectorReference &r)
999  {
1000  // as above
1001  *this = static_cast<TrilinosScalar> (r);
1002 
1003  return *this;
1004  }
1005 
1006 
1007  inline
1008  const VectorReference &
1009  VectorReference::operator = (const TrilinosScalar &value) const
1010  {
1011  vector.set (1, &index, &value);
1012  return *this;
1013  }
1014 
1015 
1016 
1017  inline
1018  const VectorReference &
1019  VectorReference::operator += (const TrilinosScalar &value) const
1020  {
1021  vector.add (1, &index, &value);
1022  return *this;
1023  }
1024 
1025 
1026 
1027  inline
1028  const VectorReference &
1029  VectorReference::operator -= (const TrilinosScalar &value) const
1030  {
1031  TrilinosScalar new_value = -value;
1032  vector.add (1, &index, &new_value);
1033  return *this;
1034  }
1035 
1036 
1037 
1038  inline
1039  const VectorReference &
1040  VectorReference::operator *= (const TrilinosScalar &value) const
1041  {
1042  TrilinosScalar new_value = static_cast<TrilinosScalar>(*this) * value;
1043  vector.set (1, &index, &new_value);
1044  return *this;
1045  }
1046 
1047 
1048 
1049  inline
1050  const VectorReference &
1051  VectorReference::operator /= (const TrilinosScalar &value) const
1052  {
1053  TrilinosScalar new_value = static_cast<TrilinosScalar>(*this) / value;
1054  vector.set (1, &index, &new_value);
1055  return *this;
1056  }
1057  }
1058 
1059 
1060 
1061  inline
1062  bool
1063  VectorBase::is_compressed () const
1064  {
1065  return compressed;
1066  }
1067 
1068 
1069 
1070  inline
1071  bool
1072  VectorBase::in_local_range (const size_type index) const
1073  {
1074  std::pair<size_type, size_type> range = local_range();
1075 
1076  return ((index >= range.first) && (index < range.second));
1077  }
1078 
1079 
1080 
1081  inline
1082  IndexSet
1084  {
1085  IndexSet is (size());
1086 
1087  // easy case: local range is contiguous
1088  if (vector->Map().LinearMap())
1089  {
1090  const std::pair<size_type, size_type> x = local_range();
1091  is.add_range (x.first, x.second);
1092  }
1093  else if (vector->Map().NumMyElements() > 0)
1094  {
1095  const size_type n_indices = vector->Map().NumMyElements();
1096 #ifndef DEAL_II_WITH_64BIT_INDICES
1097  unsigned int *vector_indices = (unsigned int *)vector->Map().MyGlobalElements();
1098 #else
1099  size_type *vector_indices = (size_type *)vector->Map().MyGlobalElements64();
1100 #endif
1101  is.add_indices(vector_indices, vector_indices+n_indices);
1102  is.compress();
1103  }
1104 
1105  return is;
1106  }
1107 
1108 
1109 
1110  inline
1111  bool
1113  {
1114  return has_ghosts;
1115  }
1116 
1117 
1118 
1119  inline
1120  internal::VectorReference
1121  VectorBase::operator () (const size_type index)
1122  {
1123  return internal::VectorReference (*this, index);
1124  }
1125 
1126 
1127 
1128  inline
1129  internal::VectorReference
1130  VectorBase::operator [] (const size_type index)
1131  {
1132  return operator() (index);
1133  }
1134 
1135 
1136  inline
1137  TrilinosScalar
1138  VectorBase::operator [] (const size_type index) const
1139  {
1140  return operator() (index);
1141  }
1142 
1143 
1144 
1145  inline
1146  void VectorBase::extract_subvector_to (const std::vector<size_type> &indices,
1147  std::vector<TrilinosScalar> &values) const
1148  {
1149  for (size_type i = 0; i < indices.size(); ++i)
1150  values[i] = operator()(indices[i]);
1151  }
1152 
1153 
1154 
1155  template <typename ForwardIterator, typename OutputIterator>
1156  inline
1157  void VectorBase::extract_subvector_to (ForwardIterator indices_begin,
1158  const ForwardIterator indices_end,
1159  OutputIterator values_begin) const
1160  {
1161  while (indices_begin != indices_end)
1162  {
1163  *values_begin = operator()(*indices_begin);
1164  indices_begin++;
1165  values_begin++;
1166  }
1167  }
1168 
1169 
1170 
1171  inline
1172  VectorBase::iterator
1174  {
1175  return (*vector)[0];
1176  }
1177 
1178 
1179 
1180  inline
1181  VectorBase::iterator
1182  VectorBase::end()
1183  {
1184  return (*vector)[0]+local_size();
1185  }
1186 
1187 
1188 
1189  inline
1190  VectorBase::const_iterator
1191  VectorBase::begin() const
1192  {
1193  return (*vector)[0];
1194  }
1195 
1196 
1197 
1198  inline
1199  VectorBase::const_iterator
1200  VectorBase::end() const
1201  {
1202  return (*vector)[0]+local_size();
1203  }
1204 
1205 
1206 
1207  inline
1208  void
1209  VectorBase::reinit (const VectorBase &v,
1210  const bool omit_zeroing_entries)
1211  {
1212  Assert (vector.get() != 0,
1213  ExcMessage("Vector has not been constructed properly."));
1214 
1215  if (omit_zeroing_entries == false ||
1216  vector_partitioner().SameAs(v.vector_partitioner())==false)
1217  vector.reset (new Epetra_FEVector(*v.vector));
1218 
1219  if (v.nonlocal_vector.get() != 0)
1220  nonlocal_vector.reset(new Epetra_MultiVector(v.nonlocal_vector->Map(), 1));
1221  }
1222 
1223 
1224 
1225  inline
1226  VectorBase &
1227  VectorBase::operator = (const TrilinosScalar s)
1228  {
1229  AssertIsFinite(s);
1230 
1231  const int ierr = vector->PutScalar(s);
1232 
1233  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1234 
1235  if (nonlocal_vector.get() != 0)
1236  nonlocal_vector->PutScalar(0.);
1237 
1238  return *this;
1239  }
1240 
1241 
1242 
1243  inline
1244  void
1245  VectorBase::set (const std::vector<size_type> &indices,
1246  const std::vector<TrilinosScalar> &values)
1247  {
1248  // if we have ghost values, do not allow
1249  // writing to this vector at all.
1250  Assert (!has_ghost_elements(), ExcGhostsPresent());
1251 
1252  Assert (indices.size() == values.size(),
1253  ExcDimensionMismatch(indices.size(),values.size()));
1254 
1255  set (indices.size(), &indices[0], &values[0]);
1256  }
1257 
1258 
1259 
1260  inline
1261  void
1262  VectorBase::set (const std::vector<size_type> &indices,
1263  const ::Vector<TrilinosScalar> &values)
1264  {
1265  // if we have ghost values, do not allow
1266  // writing to this vector at all.
1267  Assert (!has_ghost_elements(), ExcGhostsPresent());
1268 
1269  Assert (indices.size() == values.size(),
1270  ExcDimensionMismatch(indices.size(),values.size()));
1271 
1272  set (indices.size(), &indices[0], values.begin());
1273  }
1274 
1275 
1276 
1277  inline
1278  void
1279  VectorBase::set (const size_type n_elements,
1280  const size_type *indices,
1281  const TrilinosScalar *values)
1282  {
1283  // if we have ghost values, do not allow
1284  // writing to this vector at all.
1285  Assert (!has_ghost_elements(), ExcGhostsPresent());
1286 
1287  if (last_action == Add)
1288  vector->GlobalAssemble(Add);
1289 
1290  if (last_action != Insert)
1291  last_action = Insert;
1292 
1293  for (size_type i=0; i<n_elements; ++i)
1294  {
1295  const size_type row = indices[i];
1296  const TrilinosWrappers::types::int_type local_row = vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1297  if (local_row != -1)
1298  (*vector)[0][local_row] = values[i];
1299  else
1300  {
1301  const int ierr = vector->ReplaceGlobalValues (1,
1302  (const TrilinosWrappers::types::int_type *)(&row),
1303  &values[i]);
1304  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1305  compressed = false;
1306  }
1307  // in set operation, do not use the pre-allocated vector for nonlocal
1308  // entries even if it exists. This is to ensure that we really only
1309  // set the elements touched by the set() method and not all contained
1310  // in the nonlocal entries vector (there is no way to distinguish them
1311  // on the receiving processor)
1312  }
1313  }
1314 
1315 
1316 
1317  inline
1318  void
1319  VectorBase::add (const std::vector<size_type> &indices,
1320  const std::vector<TrilinosScalar> &values)
1321  {
1322  // if we have ghost values, do not allow
1323  // writing to this vector at all.
1324  Assert (!has_ghost_elements(), ExcGhostsPresent());
1325  Assert (indices.size() == values.size(),
1326  ExcDimensionMismatch(indices.size(),values.size()));
1327 
1328  add (indices.size(), &indices[0], &values[0]);
1329  }
1330 
1331 
1332 
1333  inline
1334  void
1335  VectorBase::add (const std::vector<size_type> &indices,
1336  const ::Vector<TrilinosScalar> &values)
1337  {
1338  // if we have ghost values, do not allow
1339  // writing to this vector at all.
1340  Assert (!has_ghost_elements(), ExcGhostsPresent());
1341  Assert (indices.size() == values.size(),
1342  ExcDimensionMismatch(indices.size(),values.size()));
1343 
1344  add (indices.size(), &indices[0], values.begin());
1345  }
1346 
1347 
1348 
1349  inline
1350  void
1351  VectorBase::add (const size_type n_elements,
1352  const size_type *indices,
1353  const TrilinosScalar *values)
1354  {
1355  // if we have ghost values, do not allow
1356  // writing to this vector at all.
1357  Assert (!has_ghost_elements(), ExcGhostsPresent());
1358 
1359  if (last_action != Add)
1360  {
1361  if (last_action == Insert)
1362  vector->GlobalAssemble(Insert);
1363  last_action = Add;
1364  }
1365 
1366  for (size_type i=0; i<n_elements; ++i)
1367  {
1368  const size_type row = indices[i];
1369  const TrilinosWrappers::types::int_type local_row = vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1370  if (local_row != -1)
1371  (*vector)[0][local_row] += values[i];
1372  else if (nonlocal_vector.get() == 0)
1373  {
1374  const int ierr = vector->SumIntoGlobalValues (1,
1375  (const TrilinosWrappers::types::int_type *)(&row),
1376  &values[i]);
1377  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1378  compressed = false;
1379  }
1380  else
1381  {
1382  // use pre-allocated vector for non-local entries if it exists for
1383  // addition operation
1384  const TrilinosWrappers::types::int_type my_row = nonlocal_vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1385  Assert(my_row != -1,
1386  ExcMessage("Attempted to write into off-processor vector entry "
1387  "that has not be specified as being writable upon "
1388  "initialization"));
1389  (*nonlocal_vector)[0][my_row] += values[i];
1390  compressed = false;
1391  }
1392  }
1393  }
1394 
1395 
1396 
1397  inline
1398  VectorBase::size_type
1399  VectorBase::size () const
1400  {
1401 #ifndef DEAL_II_WITH_64BIT_INDICES
1402  return (size_type) (vector->Map().MaxAllGID() + 1 - vector->Map().MinAllGID());
1403 #else
1404  return (size_type) (vector->Map().MaxAllGID64() + 1 - vector->Map().MinAllGID64());
1405 #endif
1406  }
1407 
1408 
1409 
1410  inline
1411  VectorBase::size_type
1412  VectorBase::local_size () const
1413  {
1414  return (size_type) vector->Map().NumMyElements();
1415  }
1416 
1417 
1418 
1419  inline
1420  std::pair<VectorBase::size_type, VectorBase::size_type>
1421  VectorBase::local_range () const
1422  {
1423 #ifndef DEAL_II_WITH_64BIT_INDICES
1424  const TrilinosWrappers::types::int_type begin = vector->Map().MinMyGID();
1425  const TrilinosWrappers::types::int_type end = vector->Map().MaxMyGID()+1;
1426 #else
1427  const TrilinosWrappers::types::int_type begin = vector->Map().MinMyGID64();
1428  const TrilinosWrappers::types::int_type end = vector->Map().MaxMyGID64()+1;
1429 #endif
1430 
1431  Assert (end-begin == vector->Map().NumMyElements(),
1432  ExcMessage ("This function only makes sense if the elements that this "
1433  "vector stores on the current processor form a contiguous range. "
1434  "This does not appear to be the case for the current vector."));
1435 
1436  return std::make_pair (begin, end);
1437  }
1438 
1439 
1440 
1441  inline
1442  TrilinosScalar
1443  VectorBase::operator * (const VectorBase &vec) const
1444  {
1445  Assert (vector->Map().SameAs(vec.vector->Map()),
1446  ExcDifferentParallelPartitioning());
1447  Assert (!has_ghost_elements(), ExcGhostsPresent());
1448 
1449  TrilinosScalar result;
1450 
1451  const int ierr = vector->Dot(*(vec.vector), &result);
1452  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1453 
1454  return result;
1455  }
1456 
1457 
1458 
1459  inline
1460  VectorBase::real_type
1461  VectorBase::norm_sqr () const
1462  {
1463  const TrilinosScalar d = l2_norm();
1464  return d*d;
1465  }
1466 
1467 
1468 
1469  inline
1470  TrilinosScalar
1471  VectorBase::mean_value () const
1472  {
1473  Assert (!has_ghost_elements(), ExcGhostsPresent());
1474 
1475  TrilinosScalar mean;
1476  const int ierr = vector->MeanValue (&mean);
1477  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1478 
1479  return mean;
1480  }
1481 
1482 
1483 
1484  inline
1485  TrilinosScalar
1486  VectorBase::minimal_value () const
1487  {
1488  return min();
1489  }
1490 
1491 
1492 
1493  inline
1494  TrilinosScalar
1495  VectorBase::min () const
1496  {
1497  TrilinosScalar min_value;
1498  const int ierr = vector->MinValue (&min_value);
1499  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1500 
1501  return min_value;
1502  }
1503 
1504 
1505 
1506  inline
1507  TrilinosScalar
1508  VectorBase::max () const
1509  {
1510  TrilinosScalar max_value;
1511  const int ierr = vector->MaxValue (&max_value);
1512  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1513 
1514  return max_value;
1515  }
1516 
1517 
1518 
1519  inline
1520  VectorBase::real_type
1521  VectorBase::l1_norm () const
1522  {
1523  Assert (!has_ghost_elements(), ExcGhostsPresent());
1524 
1525  TrilinosScalar d;
1526  const int ierr = vector->Norm1 (&d);
1527  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1528 
1529  return d;
1530  }
1531 
1532 
1533 
1534  inline
1535  VectorBase::real_type
1536  VectorBase::l2_norm () const
1537  {
1538  Assert (!has_ghost_elements(), ExcGhostsPresent());
1539 
1540  TrilinosScalar d;
1541  const int ierr = vector->Norm2 (&d);
1542  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1543 
1544  return d;
1545  }
1546 
1547 
1548 
1549  inline
1550  VectorBase::real_type
1551  VectorBase::lp_norm (const TrilinosScalar p) const
1552  {
1553  Assert (!has_ghost_elements(), ExcGhostsPresent());
1554 
1555  TrilinosScalar norm = 0;
1556  TrilinosScalar sum=0;
1557  const size_type n_local = local_size();
1558 
1559  // loop over all the elements because
1560  // Trilinos does not support lp norms
1561  for (size_type i=0; i<n_local; i++)
1562  sum += std::pow(std::fabs((*vector)[0][i]), p);
1563 
1564  norm = std::pow(sum, static_cast<TrilinosScalar>(1./p));
1565 
1566  return norm;
1567  }
1568 
1569 
1570 
1571  inline
1572  VectorBase::real_type
1573  VectorBase::linfty_norm () const
1574  {
1575  // while we disallow the other
1576  // norm operations on ghosted
1577  // vectors, this particular norm
1578  // is safe to run even in the
1579  // presence of ghost elements
1580  TrilinosScalar d;
1581  const int ierr = vector->NormInf (&d);
1582  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1583 
1584  return d;
1585  }
1586 
1587 
1588 
1589  inline
1590  TrilinosScalar
1591  VectorBase::add_and_dot (const TrilinosScalar a,
1592  const VectorBase &V,
1593  const VectorBase &W)
1594  {
1595  this->add(a, V);
1596  return *this * W;
1597  }
1598 
1599 
1600 
1601  // inline also scalar products, vector
1602  // additions etc. since they are all
1603  // representable by a single Trilinos
1604  // call. This reduces the overhead of the
1605  // wrapper class.
1606  inline
1607  VectorBase &
1608  VectorBase::operator *= (const TrilinosScalar a)
1609  {
1610  AssertIsFinite(a);
1611 
1612  const int ierr = vector->Scale(a);
1613  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1614 
1615  return *this;
1616  }
1617 
1618 
1619 
1620  inline
1621  VectorBase &
1622  VectorBase::operator /= (const TrilinosScalar a)
1623  {
1624  AssertIsFinite(a);
1625 
1626  const TrilinosScalar factor = 1./a;
1627 
1628  AssertIsFinite(factor);
1629 
1630  const int ierr = vector->Scale(factor);
1631  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1632 
1633  return *this;
1634  }
1635 
1636 
1637 
1638  inline
1639  VectorBase &
1641  {
1642  Assert (size() == v.size(),
1643  ExcDimensionMismatch(size(), v.size()));
1644  Assert (vector->Map().SameAs(v.vector->Map()),
1645  ExcDifferentParallelPartitioning());
1646 
1647  const int ierr = vector->Update (1.0, *(v.vector), 1.0);
1648  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1649 
1650  return *this;
1651  }
1652 
1653 
1654 
1655  inline
1656  VectorBase &
1658  {
1659  Assert (size() == v.size(),
1660  ExcDimensionMismatch(size(), v.size()));
1661  Assert (vector->Map().SameAs(v.vector->Map()),
1662  ExcDifferentParallelPartitioning());
1663 
1664  const int ierr = vector->Update (-1.0, *(v.vector), 1.0);
1665  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1666 
1667  return *this;
1668  }
1669 
1670 
1671 
1672  inline
1673  void
1674  VectorBase::add (const TrilinosScalar s)
1675  {
1676  // if we have ghost values, do not allow
1677  // writing to this vector at all.
1678  Assert (!has_ghost_elements(), ExcGhostsPresent());
1679  AssertIsFinite(s);
1680 
1681  size_type n_local = local_size();
1682  for (size_type i=0; i<n_local; i++)
1683  (*vector)[0][i] += s;
1684  }
1685 
1686 
1687 
1688  inline
1689  void
1690  VectorBase::add (const TrilinosScalar a,
1691  const VectorBase &v)
1692  {
1693  // if we have ghost values, do not allow
1694  // writing to this vector at all.
1695  Assert (!has_ghost_elements(), ExcGhostsPresent());
1696  Assert (local_size() == v.local_size(),
1697  ExcDimensionMismatch(local_size(), v.local_size()));
1698 
1699  AssertIsFinite(a);
1700 
1701  const int ierr = vector->Update(a, *(v.vector), 1.);
1702  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1703  }
1704 
1705 
1706 
1707  inline
1708  void
1709  VectorBase::add (const TrilinosScalar a,
1710  const VectorBase &v,
1711  const TrilinosScalar b,
1712  const VectorBase &w)
1713  {
1714  // if we have ghost values, do not allow
1715  // writing to this vector at all.
1716  Assert (!has_ghost_elements(), ExcGhostsPresent());
1717  Assert (local_size() == v.local_size(),
1718  ExcDimensionMismatch(local_size(), v.local_size()));
1719  Assert (local_size() == w.local_size(),
1720  ExcDimensionMismatch(local_size(), w.local_size()));
1721 
1722  AssertIsFinite(a);
1723  AssertIsFinite(b);
1724 
1725  const int ierr = vector->Update(a, *(v.vector), b, *(w.vector), 1.);
1726 
1727  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1728  }
1729 
1730 
1731 
1732  inline
1733  void
1734  VectorBase::sadd (const TrilinosScalar s,
1735  const VectorBase &v)
1736  {
1737  // if we have ghost values, do not allow
1738  // writing to this vector at all.
1739  Assert (!has_ghost_elements(), ExcGhostsPresent());
1740  Assert (size() == v.size(),
1741  ExcDimensionMismatch (size(), v.size()));
1742 
1743  AssertIsFinite(s);
1744 
1745  // We assume that the vectors have the same Map
1746  // if the local size is the same and if the vectors are not ghosted
1747  if (local_size() == v.local_size() && !v.has_ghost_elements())
1748  {
1749  Assert (this->vector->Map().SameAs(v.vector->Map())==true,
1750  ExcDifferentParallelPartitioning());
1751  const int ierr = vector->Update(1., *(v.vector), s);
1752  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1753  }
1754  else
1755  {
1756  (*this) *= s;
1757  this->add(v, true);
1758  }
1759  }
1760 
1761 
1762 
1763  inline
1764  void
1765  VectorBase::sadd (const TrilinosScalar s,
1766  const TrilinosScalar a,
1767  const VectorBase &v)
1768  {
1769  // if we have ghost values, do not allow
1770  // writing to this vector at all.
1771  Assert (!has_ghost_elements(), ExcGhostsPresent());
1772  Assert (size() == v.size(),
1773  ExcDimensionMismatch (size(), v.size()));
1774  AssertIsFinite(s);
1775  AssertIsFinite(a);
1776 
1777  // We assume that the vectors have the same Map
1778  // if the local size is the same and if the vectors are not ghosted
1779  if (local_size() == v.local_size() && !v.has_ghost_elements())
1780  {
1781  Assert (this->vector->Map().SameAs(v.vector->Map())==true,
1782  ExcDifferentParallelPartitioning());
1783  const int ierr = vector->Update(a, *(v.vector), s);
1784  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1785  }
1786  else
1787  {
1788  (*this) *= s;
1789  VectorBase tmp = v;
1790  tmp *= a;
1791  this->add(tmp, true);
1792  }
1793  }
1794 
1795 
1796 
1797  inline
1798  void
1799  VectorBase::sadd (const TrilinosScalar s,
1800  const TrilinosScalar a,
1801  const VectorBase &v,
1802  const TrilinosScalar b,
1803  const VectorBase &w)
1804  {
1805  // if we have ghost values, do not allow
1806  // writing to this vector at all.
1807  Assert (!has_ghost_elements(), ExcGhostsPresent());
1808  Assert (size() == v.size(),
1809  ExcDimensionMismatch (size(), v.size()));
1810  Assert (size() == w.size(),
1811  ExcDimensionMismatch (size(), w.size()));
1812  AssertIsFinite(s);
1813  AssertIsFinite(a);
1814  AssertIsFinite(b);
1815 
1816  // We assume that the vectors have the same Map
1817  // if the local size is the same and if the vectors are not ghosted
1818  if (local_size() == v.local_size() && !v.has_ghost_elements() &&
1819  local_size() == w.local_size() && !w.has_ghost_elements())
1820  {
1821  Assert (this->vector->Map().SameAs(v.vector->Map())==true,
1822  ExcDifferentParallelPartitioning());
1823  Assert (this->vector->Map().SameAs(w.vector->Map())==true,
1824  ExcDifferentParallelPartitioning());
1825  const int ierr = vector->Update(a, *(v.vector), b, *(w.vector), s);
1826  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1827  }
1828  else
1829  {
1830  this->sadd( s, a, v);
1831  this->sadd(1., b, w);
1832  }
1833  }
1834 
1835 
1836 
1837  inline
1838  void
1839  VectorBase::sadd (const TrilinosScalar s,
1840  const TrilinosScalar a,
1841  const VectorBase &v,
1842  const TrilinosScalar b,
1843  const VectorBase &w,
1844  const TrilinosScalar c,
1845  const VectorBase &x)
1846  {
1847  // if we have ghost values, do not allow
1848  // writing to this vector at all.
1849  Assert (!has_ghost_elements(), ExcGhostsPresent());
1850  Assert (size() == v.size(),
1851  ExcDimensionMismatch (size(), v.size()));
1852  Assert (size() == w.size(),
1853  ExcDimensionMismatch (size(), w.size()));
1854  Assert (size() == x.size(),
1855  ExcDimensionMismatch (size(), x.size()));
1856  AssertIsFinite(s);
1857  AssertIsFinite(a);
1858  AssertIsFinite(b);
1859  AssertIsFinite(c);
1860 
1861  // We assume that the vectors have the same Map
1862  // if the local size is the same and if the vectors are not ghosted
1863  if (local_size() == v.local_size() && !v.has_ghost_elements() &&
1864  local_size() == w.local_size() && !w.has_ghost_elements() &&
1865  local_size() == x.local_size() && !x.has_ghost_elements())
1866  {
1867  Assert (this->vector->Map().SameAs(v.vector->Map())==true,
1868  ExcDifferentParallelPartitioning());
1869  Assert (this->vector->Map().SameAs(w.vector->Map())==true,
1870  ExcDifferentParallelPartitioning());
1871  Assert (this->vector->Map().SameAs(x.vector->Map())==true,
1872  ExcDifferentParallelPartitioning());
1873 
1874  // Update member can only
1875  // input two other vectors so
1876  // do it in two steps
1877  const int ierr = vector->Update(a, *(v.vector), b, *(w.vector), s);
1878  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1879 
1880  const int jerr = vector->Update(c, *(x.vector), 1.);
1881  Assert (jerr == 0, ExcTrilinosError(jerr));
1882  (void)jerr; // removes -Wunused-parameter warning in optimized mode
1883  }
1884  else
1885  {
1886  this->sadd( s, a, v);
1887  this->sadd(1., b, w);
1888  this->sadd(1., c, x);
1889  }
1890  }
1891 
1892 
1893 
1894  inline
1895  void
1896  VectorBase::scale (const VectorBase &factors)
1897  {
1898  // if we have ghost values, do not allow
1899  // writing to this vector at all.
1900  Assert (!has_ghost_elements(), ExcGhostsPresent());
1901  Assert (local_size() == factors.local_size(),
1902  ExcDimensionMismatch(local_size(), factors.local_size()));
1903 
1904  const int ierr = vector->Multiply (1.0, *(factors.vector), *vector, 0.0);
1905  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1906  }
1907 
1908 
1909 
1910  inline
1911  void
1912  VectorBase::equ (const TrilinosScalar a,
1913  const VectorBase &v)
1914  {
1915  // if we have ghost values, do not allow
1916  // writing to this vector at all.
1917  Assert (!has_ghost_elements(), ExcGhostsPresent());
1918  AssertIsFinite(a);
1919 
1920  // If we don't have the same map, copy.
1921  if (vector->Map().SameAs(v.vector->Map())==false)
1922  {
1923  this->sadd(0., a, v);
1924  }
1925  else
1926  {
1927  // Otherwise, just update
1928  int ierr = vector->Update(a, *v.vector, 0.0);
1929  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1930 
1931  last_action = Zero;
1932  }
1933 
1934  }
1935 
1936 
1937 
1938  inline
1939  void
1940  VectorBase::ratio (const VectorBase &v,
1941  const VectorBase &w)
1942  {
1943  Assert (v.local_size() == w.local_size(),
1944  ExcDimensionMismatch (v.local_size(), w.local_size()));
1945  Assert (local_size() == w.local_size(),
1946  ExcDimensionMismatch (local_size(), w.local_size()));
1947 
1948  const int ierr = vector->ReciprocalMultiply(1.0, *(w.vector),
1949  *(v.vector), 0.0);
1950 
1951  AssertThrow (ierr == 0, ExcTrilinosError(ierr));
1952  }
1953 
1954 
1955 
1956  inline
1957  const Epetra_MultiVector &
1959  {
1960  return static_cast<const Epetra_MultiVector &>(*vector);
1961  }
1962 
1963 
1964 
1965  inline
1966  Epetra_FEVector &
1968  {
1969  return *vector;
1970  }
1971 
1972 
1973 
1974  inline
1975  const Epetra_Map &
1977  {
1978  return static_cast<const Epetra_Map &>(vector->Map());
1979  }
1980 
1981 
1982 
1983  inline
1984  const MPI_Comm &
1986  {
1987  static MPI_Comm comm;
1988 
1989 #ifdef DEAL_II_WITH_MPI
1990 
1991  const Epetra_MpiComm *mpi_comm
1992  = dynamic_cast<const Epetra_MpiComm *>(&vector->Map().Comm());
1993  comm = mpi_comm->Comm();
1994 
1995 #else
1996 
1997  comm = MPI_COMM_SELF;
1998 
1999 #endif
2000 
2001  return comm;
2002  }
2003 
2004 
2005 
2006 #endif // DOXYGEN
2007 
2008 }
2009 
2012 DEAL_II_NAMESPACE_CLOSE
2013 
2014 #endif // DEAL_II_WITH_TRILINOS
2015 
2016 /*---------------------------- trilinos_vector_base.h ---------------------------*/
2017 
2018 #endif
2019 /*---------------------------- trilinos_vector_base.h ---------------------------*/
reference operator()(const size_type index)
void sadd(const TrilinosScalar s, const VectorBase &V)
::ExceptionBase & ExcMessage(std::string arg1)
real_type l1_norm() const
void swap(Vector &u, Vector &v)
std_cxx11::shared_ptr< Epetra_MultiVector > nonlocal_vector
std::pair< size_type, size_type > local_range() const
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
Definition: index_set.h:1291
void scale(const VectorBase &scaling_factors)
reference operator[](const size_type index)
STL namespace.
void reinit(const VectorBase &v, const bool omit_zeroing_entries=false)
#define AssertThrow(cond, exc)
Definition: exceptions.h:358
bool is_compressed() const DEAL_II_DEPRECATED
void set(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
VectorBase & operator+=(const VectorBase &V)
BlockDynamicSparsityPattern BlockCompressedSparsityPattern DEAL_II_DEPRECATED
void add(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
real_type linfty_norm() const
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:542
VectorBase & operator/=(const TrilinosScalar factor)
std_cxx11::shared_ptr< Epetra_FEVector > vector
#define Assert(cond, exc)
Definition: exceptions.h:294
const Epetra_Map & vector_partitioner() const
#define DeclException0(Exception0)
Definition: exceptions.h:522
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< TrilinosScalar > &values) const
VectorBase & operator-=(const VectorBase &V)
TrilinosScalar operator*(const VectorBase &vec) const
void add_range(const size_type begin, const size_type end)
Definition: index_set.cc:86
TrilinosScalar mean_value() const
void compress() const
Definition: index_set.h:1256
real_type norm_sqr() const
VectorBase & operator=(const TrilinosScalar s)
TrilinosScalar minimal_value() const DEAL_II_DEPRECATED
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition: exceptions.h:572
TrilinosScalar max() const
const MPI_Comm & get_mpi_communicator() const
const Epetra_MultiVector & trilinos_vector() const
bool in_local_range(const size_type index) const
TrilinosScalar add_and_dot(const TrilinosScalar a, const VectorBase &V, const VectorBase &W)
real_type lp_norm(const TrilinosScalar p) const
IndexSet locally_owned_elements() const
VectorBase & operator*=(const TrilinosScalar factor)
#define AssertIsFinite(number)
Definition: exceptions.h:1096
size_type local_size() const
TrilinosScalar min() const
real_type l2_norm() const
void ratio(const VectorBase &a, const VectorBase &b) DEAL_II_DEPRECATED
void equ(const TrilinosScalar a, const VectorBase &V)