deal.II version GIT relicensing-1982-gbb401394a0 2024-10-13 08:40:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
trilinos_vector.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2008 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_trilinos_vector_h
16#define dealii_trilinos_vector_h
17
18
19#include <deal.II/base/config.h>
20
21#ifdef DEAL_II_WITH_TRILINOS
26
29# include <deal.II/lac/vector.h>
32
33# include <Epetra_ConfigDefs.h>
34# include <Epetra_FEVector.h>
35# include <Epetra_LocalMap.h>
36# include <Epetra_Map.h>
37# include <Epetra_MpiComm.h>
38
39# include <memory>
40# include <utility>
41# include <vector>
42
44
45// Forward declarations
46# ifndef DOXYGEN
47namespace LinearAlgebra
48{
49 // Forward declaration
50 template <typename Number>
51 class ReadWriteVector;
52} // namespace LinearAlgebra
53# endif
54
65namespace TrilinosWrappers
66{
67 class SparseMatrix;
68
74 {
75 public:
77 };
78
89 namespace internal
90 {
100 class VectorReference
101 {
102 private:
103 using size_type = VectorTraits::size_type;
104
109 VectorReference(MPI::Vector &vector, const size_type index);
110
111 public:
115 VectorReference(const VectorReference &) = default;
116
128 const VectorReference &
129 operator=(const VectorReference &r) const;
130
134 VectorReference &
135 operator=(const VectorReference &r);
136
140 const VectorReference &
141 operator=(const TrilinosScalar &s) const;
142
146 const VectorReference &
147 operator+=(const TrilinosScalar &s) const;
148
152 const VectorReference &
153 operator-=(const TrilinosScalar &s) const;
154
158 const VectorReference &
159 operator*=(const TrilinosScalar &s) const;
160
164 const VectorReference &
165 operator/=(const TrilinosScalar &s) const;
166
171 operator TrilinosScalar() const;
172
177 int,
178 << "An error with error number " << arg1
179 << " occurred while calling a Trilinos function");
180
181 private:
185 MPI::Vector &vector;
186
190 const size_type index;
191
192 // Make the vector class a friend, so that it can create objects of the
193 // present type.
194 friend class ::TrilinosWrappers::MPI::Vector;
195 };
196 } // namespace internal
201# ifndef DEAL_II_WITH_64BIT_INDICES
202 // define a helper function that queries the global ID of local ID of
203 // an Epetra_BlockMap object by calling either the 32- or 64-bit
204 // function necessary.
205 inline int
206 gid(const Epetra_BlockMap &map, int i)
207 {
208 return map.GID(i);
209 }
210# else
211 // define a helper function that queries the global ID of local ID of
212 // an Epetra_BlockMap object by calling either the 32- or 64-bit
213 // function necessary.
214 inline long long int
215 gid(const Epetra_BlockMap &map, int i)
216 {
217 return map.GID64(i);
218 }
219# endif
220
226 namespace MPI
227 {
228 class BlockVector;
229
405 public ReadVector<TrilinosScalar>
406 {
407 public:
417 using const_iterator = const value_type *;
420
430 Vector();
431
435 Vector(const Vector &v);
436
455 explicit Vector(const IndexSet &parallel_partitioning,
456 const MPI_Comm communicator = MPI_COMM_WORLD);
457
469 Vector(const IndexSet &local,
470 const IndexSet &ghost,
471 const MPI_Comm communicator = MPI_COMM_WORLD);
472
488 const Vector &v,
489 const MPI_Comm communicator = MPI_COMM_WORLD);
490
503 template <typename Number>
505 const ::Vector<Number> &v,
506 const MPI_Comm communicator = MPI_COMM_WORLD);
507
516 Vector(Vector &&v); // NOLINT
517
521 ~Vector() override = default;
522
527 void
528 clear();
529
553 void
554 reinit(const Vector &v,
555 const bool omit_zeroing_entries = false,
556 const bool allow_different_maps = false);
557
580 void
582 const MPI_Comm communicator = MPI_COMM_WORLD,
583 const bool omit_zeroing_entries = false);
584
619 void
622 const MPI_Comm communicator = MPI_COMM_WORLD,
623 const bool vector_writable = false);
624
635 void
636 reinit(
637 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
638 const bool make_ghosted = true,
639 const bool vector_writable = false);
640
644 void
645 reinit(const BlockVector &v, const bool import_data = false);
646
663 void
665
678 Vector &
680
710 Vector &
711 operator=(const Vector &v);
712
717 Vector &
718 operator=(Vector &&v) noexcept;
719
727 template <typename Number>
728 Vector &
729 operator=(const ::Vector<Number> &v);
730
748 void
750 const ::TrilinosWrappers::SparseMatrix &matrix,
751 const Vector &vector);
752
759 void
762
767 void
773
774
780 bool
781 operator==(const Vector &v) const;
782
788 bool
789 operator!=(const Vector &v) const;
790
795 size() const override;
796
803
826 std::pair<size_type, size_type>
827 local_range() const;
828
836 bool
837 in_local_range(const size_type index) const;
838
854
862 bool
864
871 void
873
879 operator*(const Vector &vec) const;
880
885 norm_sqr() const;
886
891 mean_value() const;
892
897 min() const;
898
903 max() const;
904
909 l1_norm() const;
910
916 l2_norm() const;
917
923 lp_norm(const TrilinosScalar p) const;
924
929 linfty_norm() const;
930
951 add_and_dot(const TrilinosScalar a, const Vector &V, const Vector &W);
952
958 bool
959 all_zero() const;
960
966 bool
967 is_non_negative() const;
984 operator()(const size_type index);
985
994 operator()(const size_type index) const;
995
1001 reference
1002 operator[](const size_type index);
1003
1010 operator[](const size_type index) const;
1011
1027 void
1028 extract_subvector_to(const std::vector<size_type> &indices,
1029 std::vector<TrilinosScalar> &values) const;
1030
1034 virtual void
1036 ArrayView<TrilinosScalar> &elements) const override;
1037
1065 template <typename ForwardIterator, typename OutputIterator>
1066 void
1070
1079 iterator
1081
1087 begin() const;
1088
1093 iterator
1095
1101 end() const;
1102
1117 void
1118 set(const std::vector<size_type> &indices,
1119 const std::vector<TrilinosScalar> &values);
1120
1125 void
1126 set(const std::vector<size_type> &indices,
1127 const ::Vector<TrilinosScalar> &values);
1128
1134 void
1135 set(const size_type n_elements,
1136 const size_type *indices,
1137 const TrilinosScalar *values);
1138
1143 void
1144 add(const std::vector<size_type> &indices,
1145 const std::vector<TrilinosScalar> &values);
1146
1151 void
1152 add(const std::vector<size_type> &indices,
1153 const ::Vector<TrilinosScalar> &values);
1154
1160 void
1161 add(const size_type n_elements,
1162 const size_type *indices,
1163 const TrilinosScalar *values);
1164
1168 Vector &
1170
1174 Vector &
1176
1180 Vector &
1182
1186 Vector &
1188
1193 void
1195
1208 void
1209 add(const Vector &V, const bool allow_different_maps = false);
1210
1214 void
1215 add(const TrilinosScalar a, const Vector &V);
1216
1220 void
1222 const Vector &V,
1223 const TrilinosScalar b,
1224 const Vector &W);
1225
1230 void
1231 sadd(const TrilinosScalar s, const Vector &V);
1232
1236 void
1237 sadd(const TrilinosScalar s, const TrilinosScalar a, const Vector &V);
1238
1244 void
1246
1250 void
1251 equ(const TrilinosScalar a, const Vector &V);
1263 const Epetra_MultiVector &
1265
1270 Epetra_FEVector &
1272
1277 const Epetra_BlockMap &
1279
1287 void
1288 print(std::ostream &out,
1289 const unsigned int precision = 3,
1290 const bool scientific = true,
1291 const bool across = true) const;
1292
1306 void
1307 swap(Vector &v) noexcept;
1308
1312 std::size_t
1313 memory_consumption() const;
1314
1318 MPI_Comm
1326
1331 int,
1332 << "An error with error number " << arg1
1333 << " occurred while calling a Trilinos function");
1334
1340 size_type,
1341 size_type,
1342 size_type,
1343 size_type,
1344 << "You are trying to access element " << arg1
1345 << " of a distributed vector, but this element is not stored "
1346 << "on the current processor. Note: There are " << arg2
1347 << " elements stored "
1348 << "on the current processor from within the range [" << arg3 << ','
1349 << arg4 << "] but Trilinos vectors need not store contiguous "
1350 << "ranges on each processor, and not every element in "
1351 << "this range may in fact be stored locally."
1352 << "\n\n"
1353 << "A common source for this kind of problem is that you "
1354 << "are passing a 'fully distributed' vector into a function "
1355 << "that needs read access to vector elements that correspond "
1356 << "to degrees of freedom on ghost cells (or at least to "
1357 << "'locally active' degrees of freedom that are not also "
1358 << "'locally owned'). You need to pass a vector that has these "
1359 << "elements as ghost entries.");
1360
1361 private:
1373 Epetra_CombineMode last_action;
1374
1380
1386
1392 std::unique_ptr<Epetra_FEVector> vector;
1393
1399 std::unique_ptr<Epetra_MultiVector> nonlocal_vector;
1400
1405
1406 // Make the reference class a friend.
1408 };
1409
1410
1411
1412 // ------------------- inline and template functions --------------
1413
1414
1422 inline void
1423 swap(Vector &u, Vector &v) noexcept
1424 {
1425 u.swap(v);
1426 }
1427 } // namespace MPI
1428
1429# ifndef DOXYGEN
1430
1431 namespace internal
1432 {
1433 inline VectorReference::VectorReference(MPI::Vector &vector,
1434 const size_type index)
1435 : vector(vector)
1436 , index(index)
1437 {}
1438
1439
1440 inline const VectorReference &
1441 VectorReference::operator=(const VectorReference &r) const
1442 {
1443 // as explained in the class
1444 // documentation, this is not the copy
1445 // operator. so simply pass on to the
1446 // "correct" assignment operator
1447 *this = static_cast<TrilinosScalar>(r);
1448
1449 return *this;
1450 }
1451
1452
1453
1454 inline VectorReference &
1455 VectorReference::operator=(const VectorReference &r)
1456 {
1457 // as above
1458 *this = static_cast<TrilinosScalar>(r);
1459
1460 return *this;
1461 }
1462
1463
1464 inline const VectorReference &
1465 VectorReference::operator=(const TrilinosScalar &value) const
1466 {
1467 vector.set(1, &index, &value);
1468 return *this;
1469 }
1470
1471
1472
1473 inline const VectorReference &
1474 VectorReference::operator+=(const TrilinosScalar &value) const
1475 {
1476 vector.add(1, &index, &value);
1477 return *this;
1478 }
1479
1480
1481
1482 inline const VectorReference &
1483 VectorReference::operator-=(const TrilinosScalar &value) const
1484 {
1485 TrilinosScalar new_value = -value;
1486 vector.add(1, &index, &new_value);
1487 return *this;
1488 }
1489
1490
1491
1492 inline const VectorReference &
1493 VectorReference::operator*=(const TrilinosScalar &value) const
1494 {
1495 TrilinosScalar new_value = static_cast<TrilinosScalar>(*this) * value;
1496 vector.set(1, &index, &new_value);
1497 return *this;
1498 }
1499
1500
1501
1502 inline const VectorReference &
1503 VectorReference::operator/=(const TrilinosScalar &value) const
1504 {
1505 TrilinosScalar new_value = static_cast<TrilinosScalar>(*this) / value;
1506 vector.set(1, &index, &new_value);
1507 return *this;
1508 }
1509 } // namespace internal
1510
1511 namespace MPI
1512 {
1513 inline bool
1514 Vector::in_local_range(const size_type index) const
1515 {
1516 std::pair<size_type, size_type> range = local_range();
1517
1518 return ((index >= range.first) && (index < range.second));
1519 }
1520
1521
1522
1523 inline IndexSet
1525 {
1527 ExcMessage(
1528 "The locally owned elements have not been properly initialized!"
1529 " This happens for example if this object has been initialized"
1530 " with exactly one overlapping IndexSet."));
1531 return owned_elements;
1532 }
1533
1534
1535
1536 inline bool
1538 {
1539 return has_ghosts;
1540 }
1541
1542
1543
1544 inline void
1546 {}
1547
1548
1549
1550 inline internal::VectorReference
1551 Vector::operator()(const size_type index)
1552 {
1553 return internal::VectorReference(*this, index);
1554 }
1555
1556
1557
1558 inline internal::VectorReference
1559 Vector::operator[](const size_type index)
1560 {
1561 return operator()(index);
1562 }
1563
1564
1565
1566 inline TrilinosScalar
1567 Vector::operator[](const size_type index) const
1568 {
1569 return operator()(index);
1570 }
1571
1572
1573
1574 inline void
1575 Vector::extract_subvector_to(const std::vector<size_type> &indices,
1576 std::vector<TrilinosScalar> &values) const
1577 {
1578 for (size_type i = 0; i < indices.size(); ++i)
1579 values[i] = operator()(indices[i]);
1580 }
1581
1582
1583
1584 inline void
1586 ArrayView<TrilinosScalar> &elements) const
1587 {
1588 AssertDimension(indices.size(), elements.size());
1589 for (unsigned int i = 0; i < indices.size(); ++i)
1590 {
1591 AssertIndexRange(indices[i], size());
1592 elements[i] = (*this)[indices[i]];
1593 }
1594 }
1595
1596
1597
1598 template <typename ForwardIterator, typename OutputIterator>
1599 inline void
1600 Vector::extract_subvector_to(ForwardIterator indices_begin,
1601 const ForwardIterator indices_end,
1602 OutputIterator values_begin) const
1603 {
1604 while (indices_begin != indices_end)
1605 {
1607 ++indices_begin;
1608 ++values_begin;
1609 }
1610 }
1611
1612
1613
1614 inline Vector::iterator
1616 {
1617 return (*vector)[0];
1618 }
1619
1620
1621
1622 inline Vector::iterator
1623 Vector::end()
1624 {
1625 return (*vector)[0] + locally_owned_size();
1626 }
1627
1628
1629
1631 Vector::begin() const
1632 {
1633 return (*vector)[0];
1634 }
1635
1636
1637
1639 Vector::end() const
1640 {
1641 return (*vector)[0] + locally_owned_size();
1642 }
1643
1644
1645
1646 inline void
1647 Vector::set(const std::vector<size_type> &indices,
1648 const std::vector<TrilinosScalar> &values)
1649 {
1650 // if we have ghost values, do not allow
1651 // writing to this vector at all.
1653
1654 AssertDimension(indices.size(), values.size());
1655
1656 set(indices.size(), indices.data(), values.data());
1657 }
1658
1659
1660
1661 inline void
1662 Vector::set(const std::vector<size_type> &indices,
1663 const ::Vector<TrilinosScalar> &values)
1664 {
1665 // if we have ghost values, do not allow
1666 // writing to this vector at all.
1668
1669 AssertDimension(indices.size(), values.size());
1670
1671 set(indices.size(), indices.data(), values.begin());
1672 }
1673
1674
1675
1676 inline void
1677 Vector::set(const size_type n_elements,
1678 const size_type *indices,
1679 const TrilinosScalar *values)
1680 {
1681 // if we have ghost values, do not allow
1682 // writing to this vector at all.
1684
1685 if (last_action == Add)
1686 {
1687 const int ierr = vector->GlobalAssemble(Add);
1689 }
1690
1691 if (last_action != Insert)
1693
1694 for (size_type i = 0; i < n_elements; ++i)
1695 {
1696 const TrilinosWrappers::types::int_type row = indices[i];
1698 vector->Map().LID(row);
1699 if (local_row != -1)
1700 (*vector)[0][local_row] = values[i];
1701 else
1702 {
1703 const int ierr = vector->ReplaceGlobalValues(1, &row, &values[i]);
1705 compressed = false;
1706 }
1707 // in set operation, do not use the pre-allocated vector for nonlocal
1708 // entries even if it exists. This is to ensure that we really only
1709 // set the elements touched by the set() method and not all contained
1710 // in the nonlocal entries vector (there is no way to distinguish them
1711 // on the receiving processor)
1712 }
1713 }
1714
1715
1716
1717 inline void
1718 Vector::add(const std::vector<size_type> &indices,
1719 const std::vector<TrilinosScalar> &values)
1720 {
1721 // if we have ghost values, do not allow
1722 // writing to this vector at all.
1724 AssertDimension(indices.size(), values.size());
1725
1726 add(indices.size(), indices.data(), values.data());
1727 }
1728
1729
1730
1731 inline void
1732 Vector::add(const std::vector<size_type> &indices,
1733 const ::Vector<TrilinosScalar> &values)
1734 {
1735 // if we have ghost values, do not allow
1736 // writing to this vector at all.
1738 AssertDimension(indices.size(), values.size());
1739
1740 add(indices.size(), indices.data(), values.begin());
1741 }
1742
1743
1744
1745 inline void
1746 Vector::add(const size_type n_elements,
1747 const size_type *indices,
1748 const TrilinosScalar *values)
1749 {
1750 // if we have ghost values, do not allow
1751 // writing to this vector at all.
1753
1754 if (last_action != Add)
1755 {
1756 if (last_action == Insert)
1757 {
1758 const int ierr = vector->GlobalAssemble(Insert);
1760 }
1761 last_action = Add;
1762 }
1763
1764 for (size_type i = 0; i < n_elements; ++i)
1765 {
1766 const size_type row = indices[i];
1768 static_cast<TrilinosWrappers::types::int_type>(row));
1769 if (local_row != -1)
1770 (*vector)[0][local_row] += values[i];
1771 else if (nonlocal_vector.get() == nullptr)
1772 {
1773 const int ierr = vector->SumIntoGlobalValues(
1774 1,
1775 reinterpret_cast<const TrilinosWrappers::types::int_type *>(
1776 &row),
1777 &values[i]);
1779 compressed = false;
1780 }
1781 else
1782 {
1783 // use pre-allocated vector for non-local entries if it exists for
1784 // addition operation
1786 nonlocal_vector->Map().LID(
1787 static_cast<TrilinosWrappers::types::int_type>(row));
1788 Assert(my_row != -1,
1789 ExcMessage(
1790 "Attempted to write into off-processor vector entry "
1791 "that has not be specified as being writable upon "
1792 "initialization"));
1793 (*nonlocal_vector)[0][my_row] += values[i];
1794 compressed = false;
1795 }
1796 }
1797 }
1798
1799
1800
1801 inline Vector::size_type
1802 Vector::size() const
1803 {
1804# ifndef DEAL_II_WITH_64BIT_INDICES
1805 return vector->Map().MaxAllGID() + 1 - vector->Map().MinAllGID();
1806# else
1807 return vector->Map().MaxAllGID64() + 1 - vector->Map().MinAllGID64();
1808# endif
1809 }
1810
1811
1812
1813 inline Vector::size_type
1815 {
1816 return owned_elements.n_elements();
1817 }
1818
1819
1820
1821 inline std::pair<Vector::size_type, Vector::size_type>
1822 Vector::local_range() const
1823 {
1824# ifndef DEAL_II_WITH_64BIT_INDICES
1825 const TrilinosWrappers::types::int_type begin = vector->Map().MinMyGID();
1827 vector->Map().MaxMyGID() + 1;
1828# else
1830 vector->Map().MinMyGID64();
1832 vector->Map().MaxMyGID64() + 1;
1833# endif
1834
1835 Assert(
1836 end - begin == vector->Map().NumMyElements(),
1837 ExcMessage(
1838 "This function only makes sense if the elements that this "
1839 "vector stores on the current processor form a contiguous range. "
1840 "This does not appear to be the case for the current vector."));
1841
1842 return std::make_pair(begin, end);
1843 }
1844
1845
1846
1847 inline TrilinosScalar
1848 Vector::operator*(const Vector &vec) const
1849 {
1850 Assert(vector->Map().SameAs(vec.vector->Map()),
1853
1855
1856 const int ierr = vector->Dot(*(vec.vector), &result);
1858
1859 return result;
1860 }
1861
1862
1863
1864 inline Vector::real_type
1865 Vector::norm_sqr() const
1866 {
1867 const TrilinosScalar d = l2_norm();
1868 return d * d;
1869 }
1870
1871
1872
1873 inline TrilinosScalar
1874 Vector::mean_value() const
1875 {
1877
1879 const int ierr = vector->MeanValue(&mean);
1881
1882 return mean;
1883 }
1884
1885
1886
1887 inline TrilinosScalar
1888 Vector::min() const
1889 {
1891 const int ierr = vector->MinValue(&min_value);
1893
1894 return min_value;
1895 }
1896
1897
1898
1899 inline TrilinosScalar
1900 Vector::max() const
1901 {
1903 const int ierr = vector->MaxValue(&max_value);
1905
1906 return max_value;
1907 }
1908
1909
1910
1911 inline Vector::real_type
1912 Vector::l1_norm() const
1913 {
1915
1917 const int ierr = vector->Norm1(&d);
1919
1920 return d;
1921 }
1922
1923
1924
1925 inline Vector::real_type
1926 Vector::l2_norm() const
1927 {
1929
1931 const int ierr = vector->Norm2(&d);
1933
1934 return d;
1935 }
1936
1937
1938
1939 inline Vector::real_type
1940 Vector::lp_norm(const TrilinosScalar p) const
1941 {
1943
1944 TrilinosScalar norm = 0;
1945 TrilinosScalar sum = 0;
1947
1948 // loop over all the elements because
1949 // Trilinos does not support lp norms
1950 for (size_type i = 0; i < n_local; ++i)
1951 sum += std::pow(std::fabs((*vector)[0][i]), p);
1952
1953 norm = std::pow(sum, static_cast<TrilinosScalar>(1. / p));
1954
1955 return norm;
1956 }
1957
1958
1959
1960 inline Vector::real_type
1961 Vector::linfty_norm() const
1962 {
1963 // while we disallow the other
1964 // norm operations on ghosted
1965 // vectors, this particular norm
1966 // is safe to run even in the
1967 // presence of ghost elements
1969 const int ierr = vector->NormInf(&d);
1971
1972 return d;
1973 }
1974
1975
1976
1977 inline TrilinosScalar
1979 const Vector &V,
1980 const Vector &W)
1981 {
1982 this->add(a, V);
1983 return *this * W;
1984 }
1985
1986
1987
1988 // inline also scalar products, vector
1989 // additions etc. since they are all
1990 // representable by a single Trilinos
1991 // call. This reduces the overhead of the
1992 // wrapper class.
1993 inline Vector &
1995 {
1996 AssertIsFinite(a);
1997
1998 const int ierr = vector->Scale(a);
2000
2001 return *this;
2002 }
2003
2004
2005
2006 inline Vector &
2008 {
2009 AssertIsFinite(a);
2010
2011 const TrilinosScalar factor = 1. / a;
2012
2013 AssertIsFinite(factor);
2014
2015 const int ierr = vector->Scale(factor);
2017
2018 return *this;
2019 }
2020
2021
2022
2023 inline Vector &
2024 Vector::operator+=(const Vector &v)
2025 {
2026 AssertDimension(size(), v.size());
2027 Assert(vector->Map().SameAs(v.vector->Map()),
2029
2030 const int ierr = vector->Update(1.0, *(v.vector), 1.0);
2032
2033 return *this;
2034 }
2035
2036
2037
2038 inline Vector &
2039 Vector::operator-=(const Vector &v)
2040 {
2041 AssertDimension(size(), v.size());
2042 Assert(vector->Map().SameAs(v.vector->Map()),
2044
2045 const int ierr = vector->Update(-1.0, *(v.vector), 1.0);
2047
2048 return *this;
2049 }
2050
2051
2052
2053 inline void
2055 {
2056 // if we have ghost values, do not allow
2057 // writing to this vector at all.
2059 AssertIsFinite(s);
2060
2062 for (size_type i = 0; i < n_local; ++i)
2063 (*vector)[0][i] += s;
2064 }
2065
2066
2067
2068 inline void
2069 Vector::add(const TrilinosScalar a, const Vector &v)
2070 {
2071 // if we have ghost values, do not allow
2072 // writing to this vector at all.
2075
2076 AssertIsFinite(a);
2077
2078 const int ierr = vector->Update(a, *(v.vector), 1.);
2080 }
2081
2082
2083
2084 inline void
2086 const Vector &v,
2087 const TrilinosScalar b,
2088 const Vector &w)
2089 {
2090 // if we have ghost values, do not allow
2091 // writing to this vector at all.
2094 AssertDimension(locally_owned_size(), w.locally_owned_size());
2095
2096 AssertIsFinite(a);
2097 AssertIsFinite(b);
2098
2099 const int ierr = vector->Update(a, *(v.vector), b, *(w.vector), 1.);
2100
2102 }
2103
2104
2105
2106 inline void
2107 Vector::sadd(const TrilinosScalar s, const Vector &v)
2108 {
2109 // if we have ghost values, do not allow
2110 // writing to this vector at all.
2112 AssertDimension(size(), v.size());
2113
2114 AssertIsFinite(s);
2115
2116 // We assume that the vectors have the same Map
2117 // if the local size is the same and if the vectors are not ghosted
2119 !v.has_ghost_elements())
2120 {
2121 Assert(this->vector->Map().SameAs(v.vector->Map()) == true,
2123 const int ierr = vector->Update(1., *(v.vector), s);
2125 }
2126 else
2127 {
2128 (*this) *= s;
2129 this->add(v, true);
2130 }
2131 }
2132
2133
2134
2135 inline void
2137 const TrilinosScalar a,
2138 const Vector &v)
2139 {
2140 // if we have ghost values, do not allow
2141 // writing to this vector at all.
2143 AssertDimension(size(), v.size());
2144 AssertIsFinite(s);
2145 AssertIsFinite(a);
2146
2147 // We assume that the vectors have the same Map
2148 // if the local size is the same and if the vectors are not ghosted
2150 !v.has_ghost_elements())
2151 {
2152 Assert(this->vector->Map().SameAs(v.vector->Map()) == true,
2154 const int ierr = vector->Update(a, *(v.vector), s);
2156 }
2157 else
2158 {
2159 (*this) *= s;
2160 Vector tmp = v;
2161 tmp *= a;
2162 this->add(tmp, true);
2163 }
2164 }
2165
2166
2167
2168 inline void
2169 Vector::scale(const Vector &factors)
2170 {
2171 // if we have ghost values, do not allow
2172 // writing to this vector at all.
2175
2176 const int ierr = vector->Multiply(1.0, *(factors.vector), *vector, 0.0);
2178 }
2179
2180
2181
2182 inline void
2183 Vector::equ(const TrilinosScalar a, const Vector &v)
2184 {
2185 // if we have ghost values, do not allow
2186 // writing to this vector at all.
2188 AssertIsFinite(a);
2189
2190 // If we don't have the same map, copy.
2191 if (vector->Map().SameAs(v.vector->Map()) == false)
2192 {
2193 this->sadd(0., a, v);
2194 }
2195 else
2196 {
2197 // Otherwise, just update
2198 int ierr = vector->Update(a, *v.vector, 0.0);
2200
2201 last_action = Zero;
2202 }
2203 }
2204
2205
2206
2207 inline const Epetra_MultiVector &
2209 {
2210 return static_cast<const Epetra_MultiVector &>(*vector);
2211 }
2212
2213
2214
2215 inline Epetra_FEVector &
2217 {
2218 return *vector;
2219 }
2220
2221
2222
2223 inline const Epetra_BlockMap &
2225 {
2226 return vector->Map();
2227 }
2228
2229
2230
2231 inline MPI_Comm
2233 {
2234 const Epetra_MpiComm *mpi_comm =
2235 dynamic_cast<const Epetra_MpiComm *>(&vector->Map().Comm());
2236 return mpi_comm->Comm();
2237 }
2238
2239
2240
2241 template <typename number>
2242 Vector::Vector(const IndexSet &parallel_partitioner,
2243 const ::Vector<number> &v,
2244 const MPI_Comm communicator)
2245 {
2246 *this =
2247 Vector(parallel_partitioner.make_trilinos_map(communicator, true), v);
2249 }
2250
2251
2252
2253 inline Vector &
2255 {
2256 AssertIsFinite(s);
2257
2258 int ierr = vector->PutScalar(s);
2260
2261 if (nonlocal_vector.get() != nullptr)
2262 {
2263 ierr = nonlocal_vector->PutScalar(0.);
2265 }
2266
2267 return *this;
2268 }
2269 } /* end of namespace MPI */
2270
2271# endif /* DOXYGEN */
2272
2273} /* end of namespace TrilinosWrappers */
2274
2278namespace internal
2279{
2280 namespace LinearOperatorImplementation
2281 {
2282 template <typename>
2283 class ReinitHelper;
2284
2289 template <>
2290 class ReinitHelper<TrilinosWrappers::MPI::Vector>
2291 {
2292 public:
2293 template <typename Matrix>
2294 static void
2295 reinit_range_vector(const Matrix &matrix,
2297 bool omit_zeroing_entries)
2298 {
2299 v.reinit(matrix.locally_owned_range_indices(),
2300 matrix.get_mpi_communicator(),
2301 omit_zeroing_entries);
2302 }
2303
2304 template <typename Matrix>
2305 static void
2306 reinit_domain_vector(const Matrix &matrix,
2308 bool omit_zeroing_entries)
2309 {
2310 v.reinit(matrix.locally_owned_domain_indices(),
2311 matrix.get_mpi_communicator(),
2312 omit_zeroing_entries);
2313 }
2314 };
2315
2316 } // namespace LinearOperatorImplementation
2317} /* namespace internal */
2318
2319
2320
2324template <>
2325struct is_serial_vector<TrilinosWrappers::MPI::Vector> : std::false_type
2326{};
2327
2328
2330
2331#endif
2332
2333#endif
std::size_t size() const
Definition array_view.h:684
size_type size() const
Definition index_set.h:1776
size_type n_elements() const
Definition index_set.h:1934
Vector & operator/=(const TrilinosScalar factor)
void compress(VectorOperation::values operation)
Vector(const IndexSet &parallel_partitioning, const ::Vector< Number > &v, const MPI_Comm communicator=MPI_COMM_WORLD)
void sadd(const TrilinosScalar s, const Vector &V)
TrilinosScalar mean_value() const
void add(const size_type n_elements, const size_type *indices, const TrilinosScalar *values)
VectorTraits::size_type size_type
void add(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
std::unique_ptr< Epetra_MultiVector > nonlocal_vector
void add(const TrilinosScalar s)
void import_elements(const LinearAlgebra::ReadWriteVector< double > &rwv, const VectorOperation::values operation)
MPI_Comm get_mpi_communicator() const
void swap(Vector &v) noexcept
const Epetra_BlockMap & trilinos_partitioner() const
reference operator()(const size_type index)
void import_nonlocal_data_for_fe(const ::TrilinosWrappers::SparseMatrix &matrix, const Vector &vector)
std::unique_ptr< Epetra_FEVector > vector
size_type size() const override
bool in_local_range(const size_type index) const
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
friend class internal::VectorReference
real_type lp_norm(const TrilinosScalar p) const
IndexSet locally_owned_elements() const
virtual void extract_subvector_to(const ArrayView< const size_type > &indices, ArrayView< TrilinosScalar > &elements) const override
Vector & operator-=(const Vector &V)
Vector & operator+=(const Vector &V)
const Epetra_MultiVector & trilinos_vector() const
const internal::VectorReference const_reference
void reinit(const Vector &v, const bool omit_zeroing_entries=false, const bool allow_different_maps=false)
std::pair< size_type, size_type > local_range() const
real_type norm_sqr() const
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< TrilinosScalar > &values) const
bool operator!=(const Vector &v) const
~Vector() override=default
Epetra_FEVector & trilinos_vector()
const_iterator begin() const
real_type linfty_norm() const
internal::VectorReference reference
TrilinosScalar min() const
void set(const std::vector< size_type > &indices, const ::Vector< TrilinosScalar > &values)
void set(const size_type n_elements, const size_type *indices, const TrilinosScalar *values)
void scale(const Vector &scaling_factors)
void equ(const TrilinosScalar a, const Vector &V)
bool operator==(const Vector &v) const
void sadd(const TrilinosScalar s, const TrilinosScalar a, const Vector &V)
reference operator[](const size_type index)
const value_type * const_iterator
void swap(Vector &u, Vector &v) noexcept
size_type locally_owned_size() const
TrilinosScalar operator[](const size_type index) const
TrilinosScalar add_and_dot(const TrilinosScalar a, const Vector &V, const Vector &W)
Vector & operator=(const TrilinosScalar s)
Vector & operator*=(const TrilinosScalar factor)
TrilinosScalar operator*(const Vector &vec) const
void add(const std::vector< size_type > &indices, const ::Vector< TrilinosScalar > &values)
void extract_subvector_to(ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
std::size_t memory_consumption() const
void add(const TrilinosScalar a, const Vector &V, const TrilinosScalar b, const Vector &W)
void add(const TrilinosScalar a, const Vector &V)
void set(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
TrilinosScalar max() const
Vector & operator=(const ::Vector< Number > &v)
const_iterator end() const
::types::global_dof_index size_type
typename numbers::NumberTraits< Number >::real_type real_type
Definition vector.h:156
bool has_ghost_elements() const
const value_type * const_iterator
Definition vector.h:142
virtual size_type size() const override
value_type * iterator
Definition vector.h:141
size_type locally_owned_size() const
static void reinit_domain_vector(const Matrix &matrix, TrilinosWrappers::MPI::Vector &v, bool omit_zeroing_entries)
static void reinit_range_vector(const Matrix &matrix, TrilinosWrappers::MPI::Vector &v, bool omit_zeroing_entries)
#define DEAL_II_DEPRECATED
Definition config.h:205
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define DeclException0(Exception0)
Definition exceptions.h:466
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition exceptions.h:580
static ::ExceptionBase & ExcDifferentParallelPartitioning()
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcTrilinosError(int arg1)
#define DeclException1(Exception1, type1, outsequence)
Definition exceptions.h:511
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition divergence.h:471
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
int gid(const Epetra_BlockMap &map, int i)
T sum(const T &t, const MPI_Comm mpi_communicator)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
unsigned int global_dof_index
Definition types.h:81
double TrilinosScalar
Definition types.h:178