16 #ifndef dealii_trilinos_vector_h 17 #define dealii_trilinos_vector_h 20 #include <deal.II/base/config.h> 22 #ifdef DEAL_II_WITH_TRILINOS 23 # include <deal.II/base/subscriptor.h> 24 # include <deal.II/base/index_set.h> 25 # include <deal.II/base/utilities.h> 26 # include <deal.II/base/mpi.h> 27 # include <deal.II/lac/exceptions.h> 28 # include <deal.II/lac/vector.h> 29 # include <deal.II/lac/vector_operation.h> 30 # include <deal.II/lac/vector_type_traits.h> 36 # include <Epetra_ConfigDefs.h> 37 # ifdef DEAL_II_WITH_MPI // only if MPI is installed 39 # include <Epetra_MpiComm.h> 41 # include <Epetra_SerialComm.h> 43 # include <Epetra_FEVector.h> 44 # include <Epetra_Map.h> 45 # include <Epetra_LocalMap.h> 47 DEAL_II_NAMESPACE_OPEN
79 typedef ::types::global_dof_index
size_type;
97 VectorReference (MPI::Vector &vector,
98 const size_type index);
113 const VectorReference &
114 operator= (
const VectorReference &r)
const;
120 operator= (
const VectorReference &r);
125 const VectorReference &
126 operator= (
const TrilinosScalar &s)
const;
131 const VectorReference &
132 operator+= (
const TrilinosScalar &s)
const;
137 const VectorReference &
138 operator-= (
const TrilinosScalar &s)
const;
143 const VectorReference &
144 operator*= (
const TrilinosScalar &s)
const;
149 const VectorReference &
150 operator/= (
const TrilinosScalar &s)
const;
156 operator TrilinosScalar ()
const;
163 <<
"An error with error number " << arg1
164 <<
" occurred while calling a Trilinos function");
181 friend class ::TrilinosWrappers::MPI::Vector;
190 #ifndef DEAL_II_WITH_64BIT_INDICES 195 int gid(
const Epetra_BlockMap &map,
int i)
204 long long int gid(
const Epetra_BlockMap &map,
int i)
406 typedef TrilinosScalar real_type;
407 typedef ::types::global_dof_index size_type;
448 const MPI_Comm &communicator = MPI_COMM_WORLD);
463 const MPI_Comm &communicator = MPI_COMM_WORLD);
481 const MPI_Comm &communicator = MPI_COMM_WORLD);
495 template <
typename Number>
497 const ::Vector<Number> &v,
498 const MPI_Comm &communicator = MPI_COMM_WORLD);
541 const bool omit_zeroing_entries =
false,
542 const bool allow_different_maps =
false);
567 const MPI_Comm &communicator = MPI_COMM_WORLD,
568 const bool omit_zeroing_entries =
false);
597 const MPI_Comm &communicator = MPI_COMM_WORLD,
598 const bool vector_writable =
false);
604 const bool import_data =
false);
658 template <
typename Number>
679 (const ::TrilinosWrappers::SparseMatrix &matrix,
699 size_type
size ()
const;
735 std::pair<size_type, size_type>
local_range ()
const;
797 TrilinosScalar
min ()
const;
802 TrilinosScalar
max ()
const;
819 real_type
lp_norm (
const TrilinosScalar p)
const;
844 TrilinosScalar
add_and_dot (
const TrilinosScalar a,
921 std::vector<TrilinosScalar> &values)
const;
950 template <
typename ForwardIterator,
typename OutputIterator>
952 const ForwardIterator indices_end,
953 OutputIterator values_begin)
const;
971 const_iterator
begin ()
const;
983 const_iterator
end ()
const;
999 void set (
const std::vector<size_type> &indices,
1000 const std::vector<TrilinosScalar> &values);
1006 void set (
const std::vector<size_type> &indices,
1007 const ::Vector<TrilinosScalar> &values);
1014 void set (
const size_type n_elements,
1015 const size_type *indices,
1016 const TrilinosScalar *values);
1022 void add (
const std::vector<size_type> &indices,
1023 const std::vector<TrilinosScalar> &values);
1029 void add (
const std::vector<size_type> &indices,
1030 const ::Vector<TrilinosScalar> &values);
1037 void add (
const size_type n_elements,
1038 const size_type *indices,
1039 const TrilinosScalar *values);
1065 void add (
const TrilinosScalar s);
1080 const bool allow_different_maps =
false);
1085 void add (
const TrilinosScalar a,
1091 void add (
const TrilinosScalar a,
1093 const TrilinosScalar b,
1100 void sadd (
const TrilinosScalar s,
1106 void sadd (
const TrilinosScalar s,
1107 const TrilinosScalar a,
1120 void equ (
const TrilinosScalar a,
1154 void print (std::ostream &out,
1155 const unsigned int precision = 3,
1156 const bool scientific =
true,
1157 const bool across =
true)
const;
1196 <<
"An error with error number " << arg1
1197 <<
" occurred while calling a Trilinos function");
1203 size_type, size_type, size_type, size_type,
1204 <<
"You tried to access element " << arg1
1205 <<
" of a distributed vector, but this element is not stored " 1206 <<
"on the current processor. Note: There are " 1207 << arg2 <<
" elements stored " 1208 <<
"on the current processor from within the range " 1209 << arg3 <<
" through " << arg4
1210 <<
" but Trilinos vectors need not store contiguous " 1211 <<
"ranges on each processor, and not every element in " 1212 <<
"this range may in fact be stored locally.");
1291 VectorReference::VectorReference (MPI::Vector &vector,
1292 const size_type index)
1300 const VectorReference &
1301 VectorReference::operator= (
const VectorReference &r)
const 1307 *
this =
static_cast<TrilinosScalar
> (r);
1316 VectorReference::operator= (
const VectorReference &r)
1319 *
this =
static_cast<TrilinosScalar
> (r);
1326 const VectorReference &
1327 VectorReference::operator= (
const TrilinosScalar &value)
const 1329 vector.set (1, &index, &value);
1336 const VectorReference &
1337 VectorReference::operator+= (
const TrilinosScalar &value)
const 1339 vector.add (1, &index, &value);
1346 const VectorReference &
1347 VectorReference::operator-= (
const TrilinosScalar &value)
const 1349 TrilinosScalar new_value = -value;
1350 vector.add (1, &index, &new_value);
1357 const VectorReference &
1358 VectorReference::operator*= (
const TrilinosScalar &value)
const 1360 TrilinosScalar new_value =
static_cast<TrilinosScalar
>(*this) * value;
1361 vector.set (1, &index, &new_value);
1368 const VectorReference &
1369 VectorReference::operator/= (
const TrilinosScalar &value)
const 1371 TrilinosScalar new_value =
static_cast<TrilinosScalar
>(*this) / value;
1372 vector.set (1, &index, &new_value);
1383 std::pair<size_type, size_type> range = local_range();
1385 return ((index >= range.first) && (index < range.second));
1394 Assert(owned_elements.size()==size(),
1395 ExcMessage(
"The locally owned elements have not been properly initialized!" 1396 " This happens for example if this object has been initialized" 1397 " with exactly one overlapping IndexSet."));
1398 return owned_elements;
1405 Vector::has_ghost_elements()
const 1420 internal::VectorReference
1423 return internal::VectorReference (*
this, index);
1429 internal::VectorReference
1432 return operator() (index);
1441 return operator() (index);
1448 std::vector<TrilinosScalar> &values)
const 1450 for (size_type i = 0; i < indices.size(); ++i)
1451 values[i] =
operator()(indices[i]);
1456 template <
typename ForwardIterator,
typename OutputIterator>
1459 const ForwardIterator indices_end,
1460 OutputIterator values_begin)
const 1462 while (indices_begin != indices_end)
1464 *values_begin = operator()(*indices_begin);
1476 return (*vector)[0];
1485 return (*vector)[0]+local_size();
1491 Vector::const_iterator
1494 return (*vector)[0];
1500 Vector::const_iterator
1503 return (*vector)[0]+local_size();
1510 Vector::set (
const std::vector<size_type> &indices,
1511 const std::vector<TrilinosScalar> &values)
1517 Assert (indices.size() == values.size(),
1520 set (indices.size(), indices.data(), values.data());
1527 Vector::set (
const std::vector<size_type> &indices,
1528 const ::Vector<TrilinosScalar> &values)
1534 Assert (indices.size() == values.size(),
1537 set (indices.size(), indices.data(), values.begin());
1544 Vector::set (
const size_type n_elements,
1545 const size_type *indices,
1546 const TrilinosScalar *values)
1552 if (last_action == Add)
1554 const int ierr = vector->GlobalAssemble(Add);
1558 if (last_action != Insert)
1559 last_action = Insert;
1561 for (size_type i=0; i<n_elements; ++i)
1564 const TrilinosWrappers::types::int_type local_row = vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1565 if (local_row != -1)
1566 (*vector)[0][local_row] = values[i];
1569 const int ierr = vector->ReplaceGlobalValues (1,
1570 (
const TrilinosWrappers::types::int_type *)(&row),
1587 Vector::add (
const std::vector<size_type> &indices,
1588 const std::vector<TrilinosScalar> &values)
1593 Assert (indices.size() == values.size(),
1596 add (indices.size(), indices.data(), values.data());
1603 Vector::add (
const std::vector<size_type> &indices,
1604 const ::Vector<TrilinosScalar> &values)
1609 Assert (indices.size() == values.size(),
1612 add (indices.size(), indices.data(), values.begin());
1620 const size_type *indices,
1621 const TrilinosScalar *values)
1627 if (last_action != Add)
1629 if (last_action == Insert)
1631 const int ierr = vector->GlobalAssemble(Insert);
1637 for (size_type i=0; i<n_elements; ++i)
1640 const TrilinosWrappers::types::int_type local_row = vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1641 if (local_row != -1)
1642 (*vector)[0][local_row] += values[i];
1643 else if (nonlocal_vector.get() ==
nullptr)
1645 const int ierr = vector->SumIntoGlobalValues (1,
1646 (
const TrilinosWrappers::types::int_type *)(&row),
1655 const TrilinosWrappers::types::int_type my_row = nonlocal_vector->Map().LID(static_cast<TrilinosWrappers::types::int_type>(row));
1657 ExcMessage(
"Attempted to write into off-processor vector entry " 1658 "that has not be specified as being writable upon " 1660 (*nonlocal_vector)[0][my_row] += values[i];
1672 #ifndef DEAL_II_WITH_64BIT_INDICES 1673 return (size_type) (vector->Map().MaxAllGID() + 1 - vector->Map().MinAllGID());
1675 return (size_type) (vector->Map().MaxAllGID64() + 1 - vector->Map().MinAllGID64());
1683 Vector::local_size ()
const 1685 return (size_type) vector->Map().NumMyElements();
1691 std::pair<Vector::size_type, Vector::size_type>
1692 Vector::local_range ()
const 1694 #ifndef DEAL_II_WITH_64BIT_INDICES 1695 const TrilinosWrappers::types::int_type begin = vector->Map().MinMyGID();
1696 const TrilinosWrappers::types::int_type end = vector->Map().MaxMyGID()+1;
1698 const TrilinosWrappers::types::int_type begin = vector->Map().MinMyGID64();
1699 const TrilinosWrappers::types::int_type end = vector->Map().MaxMyGID64()+1;
1702 Assert (end-begin == vector->Map().NumMyElements(),
1703 ExcMessage (
"This function only makes sense if the elements that this " 1704 "vector stores on the current processor form a contiguous range. " 1705 "This does not appear to be the case for the current vector."));
1707 return std::make_pair (begin, end);
1716 Assert (vector->Map().SameAs(vec.vector->Map()),
1717 ExcDifferentParallelPartitioning());
1720 TrilinosScalar result;
1722 const int ierr = vector->Dot(*(vec.vector), &result);
1734 const TrilinosScalar
d = l2_norm();
1746 TrilinosScalar
mean;
1747 const int ierr = vector->MeanValue (&mean);
1757 Vector::min ()
const 1759 TrilinosScalar min_value;
1760 const int ierr = vector->MinValue (&min_value);
1770 Vector::max ()
const 1772 TrilinosScalar max_value;
1773 const int ierr = vector->MaxValue (&max_value);
1788 const int ierr = vector->Norm1 (&d);
1803 const int ierr = vector->Norm2 (&d);
1817 TrilinosScalar
norm = 0;
1818 TrilinosScalar
sum=0;
1823 for (size_type i=0; i<n_local; i++)
1824 sum += std::pow(std::fabs((*vector)[0][i]), p);
1826 norm = std::pow(sum, static_cast<TrilinosScalar>(1./p));
1843 const int ierr = vector->NormInf (&d);
1874 const int ierr = vector->Scale(a);
1888 const TrilinosScalar factor = 1./a;
1892 const int ierr = vector->Scale(factor);
1904 Assert (size() == v.size(),
1906 Assert (vector->Map().SameAs(v.vector->Map()),
1907 ExcDifferentParallelPartitioning());
1909 const int ierr = vector->Update (1.0, *(v.vector), 1.0);
1921 Assert (size() == v.size(),
1923 Assert (vector->Map().SameAs(v.vector->Map()),
1924 ExcDifferentParallelPartitioning());
1926 const int ierr = vector->Update (-1.0, *(v.vector), 1.0);
1944 for (size_type i=0; i<n_local; i++)
1945 (*vector)[0][i] += s;
1958 Assert (local_size() == v.local_size(),
1963 const int ierr = vector->Update(a, *(v.vector), 1.);
1973 const TrilinosScalar b,
1979 Assert (local_size() == v.local_size(),
1981 Assert (local_size() ==
w.local_size(),
1987 const int ierr = vector->Update(a, *(v.vector), b, *(
w.vector), 1.);
2002 Assert (size() == v.size(),
2009 if (local_size() == v.local_size() && !v.has_ghost_elements())
2011 Assert (this->vector->Map().SameAs(v.vector->Map())==
true,
2012 ExcDifferentParallelPartitioning());
2013 const int ierr = vector->Update(1., *(v.vector), s);
2028 const TrilinosScalar a,
2034 Assert (size() == v.size(),
2041 if (local_size() == v.local_size() && !v.has_ghost_elements())
2043 Assert (this->vector->Map().SameAs(v.vector->Map())==
true,
2044 ExcDifferentParallelPartitioning());
2045 const int ierr = vector->Update(a, *(v.vector), s);
2053 this->add(tmp,
true);
2066 Assert (local_size() == factors.local_size(),
2069 const int ierr = vector->Multiply (1.0, *(factors.vector), *vector, 0.0);
2086 if (vector->Map().SameAs(v.vector->Map())==
false)
2088 this->sadd(0., a, v);
2093 int ierr = vector->Update(a, *v.vector, 0.0);
2104 const Epetra_MultiVector &
2105 Vector::trilinos_vector ()
const 2107 return static_cast<const Epetra_MultiVector &
>(*vector);
2114 Vector::trilinos_vector ()
2123 Vector::vector_partitioner ()
const 2125 return static_cast<const Epetra_Map &
>(vector->Map());
2132 Vector::get_mpi_communicator ()
const 2134 static MPI_Comm comm;
2136 #ifdef DEAL_II_WITH_MPI 2138 const Epetra_MpiComm *mpi_comm
2139 =
dynamic_cast<const Epetra_MpiComm *
>(&vector->Map().Comm());
2140 comm = mpi_comm->Comm();
2144 comm = MPI_COMM_SELF;
2151 template <
typename number>
2153 const ::Vector<number> &v,
2154 const MPI_Comm &communicator)
2158 owned_elements = parallel_partitioner;
2169 int ierr = vector->PutScalar(s);
2172 if (nonlocal_vector.get() !=
nullptr)
2174 ierr = nonlocal_vector->PutScalar(0.);
2191 namespace LinearOperatorImplementation
2193 template <
typename>
class ReinitHelper;
2203 template <
typename Matrix>
2207 bool omit_zeroing_entries)
2209 v.
reinit(matrix.locally_owned_range_indices(), matrix.get_mpi_communicator(), omit_zeroing_entries);
2212 template <
typename Matrix>
2216 bool omit_zeroing_entries)
2218 v.
reinit(matrix.locally_owned_domain_indices(), matrix.get_mpi_communicator(), omit_zeroing_entries);
2238 DEAL_II_NAMESPACE_CLOSE
2240 #endif // DEAL_II_WITH_TRILINOS 2244 #endif // dealii_trilinos_vector_h Vector & operator=(const TrilinosScalar s)
std::unique_ptr< Epetra_MultiVector > nonlocal_vector
static ::ExceptionBase & ExcTrilinosError(int arg1)
void update_ghost_values() const
types::global_dof_index size_type
void sadd(const TrilinosScalar s, const Vector &V)
TrilinosScalar mean_value() const
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
real_type l2_norm() const
reference operator[](const size_type index)
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
void reinit(const Vector &v, const bool omit_zeroing_entries=false, const bool allow_different_maps=false)
void add(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
void equ(const Number a, const Vector< Number > &u)
real_type l1_norm() const
std::pair< size_type, size_type > local_range() const
void equ(const TrilinosScalar a, const Vector &V)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< TrilinosScalar > &values) const
#define AssertThrow(cond, exc)
TrilinosScalar operator*(const Vector &vec) const
Vector< Number > & operator=(const Number s)
static void reinit_range_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
Epetra_Map make_trilinos_map(const MPI_Comm &communicator=MPI_COMM_WORLD, const bool overlapping=false) const
TrilinosScalar min() const
static void reinit_domain_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
bool operator==(const Vector &v) const
static ::ExceptionBase & ExcMessage(std::string arg1)
numbers::NumberTraits< Number >::real_type real_type
reference operator()(const size_type index)
bool in_local_range(const size_type global_index) const
void swap(BlockVector &u, BlockVector &v)
void scale(const Vector< Number > &scaling_factors)
#define DeclException1(Exception1, type1, outsequence)
Vector< Number > & operator+=(const Vector< Number > &V)
T sum(const T &t, const MPI_Comm &mpi_communicator)
#define Assert(cond, exc)
bool is_non_negative() const
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
std::unique_ptr< Epetra_FEVector > vector
real_type norm_sqr() const
real_type lp_norm(const real_type p) const
void update_ghost_values() const
void compress(::VectorOperation::values operation)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
#define DeclException0(Exception0)
std::size_t memory_consumption() const
Vector & operator/=(const TrilinosScalar factor)
Vector< Number > & operator-=(const Vector< Number > &V)
Vector< Number > & operator*=(const Number factor)
friend class internal::VectorReference
void import_nonlocal_data_for_fe(const ::TrilinosWrappers::SparseMatrix &matrix, const Vector &vector)
bool has_ghost_elements() const
const Epetra_Map & vector_partitioner() const
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
TrilinosScalar max() const
bool in_local_range(const size_type index) const
Number operator*(const Vector< Number2 > &V) const
IndexSet locally_owned_elements() const
static ::ExceptionBase & ExcGhostsPresent()
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
double norm(const FEValuesBase< dim > &fe, const VectorSlice< const std::vector< std::vector< Tensor< 1, dim > > > > &Du)
real_type linfty_norm() const
Vector< Number > & operator/=(const Number factor)
size_type local_size() const
IndexSet locally_owned_elements() const
const MPI_Comm & get_mpi_communicator() const
void scale(const Vector &scaling_factors)
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Vector & operator-=(const Vector &V)
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
real_type norm_sqr() const
TrilinosScalar add_and_dot(const TrilinosScalar a, const Vector &V, const Vector &W)
Epetra_CombineMode last_action
bool operator!=(const Vector &v) const
TrilinosScalar value_type
Vector & operator+=(const Vector &V)
static ::ExceptionBase & ExcDifferentParallelPartitioning()
real_type lp_norm(const TrilinosScalar p) const
Vector & operator*=(const TrilinosScalar factor)
const Epetra_MultiVector & trilinos_vector() const
real_type l2_norm() const
static ::ExceptionBase & ExcTrilinosError(int arg1)
#define AssertIsFinite(number)
Number mean_value() const