17#ifdef DEAL_II_WITH_TRILINOS
27# include <boost/io/ios_state.hpp>
29# include <Epetra_Export.h>
30# include <Epetra_Import.h>
31# include <Epetra_Vector.h>
53 vector.vector->Map().LID(
55# ifndef DEAL_II_WITH_64BIT_INDICES
59 vector.vector->Map().NumMyElements(),
60 vector.vector->Map().MinMyGID(),
61 vector.vector->Map().MaxMyGID()));
66 vector.vector->Map().NumMyElements(),
67 vector.vector->Map().MinMyGID64(),
68 vector.vector->Map().MaxMyGID64()));
72 return (*(vector.vector))[0][local_index];
83 , vector(new Epetra_FEVector(
84 Epetra_Map(0, 0, 0,
Utilities::Trilinos::comm_self())))
93 reinit(parallel_partitioning, communicator);
131 vector = std::make_unique<Epetra_FEVector>(
143 reinit(local, ghost, communicator,
false);
153 Epetra_Map map(0, 0, Epetra_MpiComm(MPI_COMM_SELF));
156 vector = std::make_unique<Epetra_FEVector>(map);
175 vector = std::make_unique<Epetra_FEVector>(map);
206 const bool omit_zeroing_entries,
207 const bool allow_different_maps)
214 if (allow_different_maps ==
false)
220 const Epetra_MpiComm *my_comm =
221 dynamic_cast<const Epetra_MpiComm *
>(&
vector->Comm());
222 const Epetra_MpiComm *v_comm =
223 dynamic_cast<const Epetra_MpiComm *
>(&v.
vector->Comm());
224 const bool same_communicators =
225 my_comm !=
nullptr && v_comm !=
nullptr &&
226 my_comm->DataPtr() == v_comm->DataPtr();
227 if (!same_communicators ||
230 vector = std::make_unique<Epetra_FEVector>(v.
vector->Map());
235 else if (omit_zeroing_entries ==
false)
244 ierr =
vector->PutScalar(0.0);
257 Assert(omit_zeroing_entries ==
false,
259 "It is not possible to exchange data with the "
260 "option 'omit_zeroing_entries' set, which would not write "
266 Epetra_Import data_exchange(
vector->Map(), v.
vector->Map());
268 const int ierr =
vector->Import(*v.
vector, data_exchange, Insert);
275 const Epetra_MpiComm *comm_ptr =
276 dynamic_cast<const Epetra_MpiComm *
>(&(v.
vector->Comm()));
301 size_type n_elements = 0, added_elements = 0, block_offset = 0;
303 n_elements += v.
block(block).vector->Map().NumMyElements();
304 std::vector<TrilinosWrappers::types::int_type> global_ids(n_elements, -1);
309 v.
block(block).trilinos_partitioner());
310 size_type vector_size = v.
block(block).vector->Map().NumMyElements();
311 for (
size_type i = 0; i < vector_size; ++i)
312 global_ids[added_elements++] = glob_elements[i] + block_offset;
315 block_offset += v.
block(block).size();
319 Epetra_Map new_map(v.
size(),
323 v.
block(0).trilinos_partitioner().Comm());
325 auto actual_vec = std::make_unique<Epetra_FEVector>(new_map);
330 v.
block(block).trilinos_vector().ExtractCopy(entries, 0);
331 entries += v.
block(block).vector->Map().NumMyElements();
334 if (import_data ==
true)
337 *actual_vec)) == v.
size(),
342 Epetra_Import data_exchange(
vector->Map(), actual_vec->Map());
344 const int ierr =
vector->Import(*actual_vec, data_exchange, Insert);
350 vector = std::move(actual_vec);
353 const Epetra_MpiComm *comm_ptr =
354 dynamic_cast<const Epetra_MpiComm *
>(&(
vector->Comm()));
369 const bool vector_writable)
373 if (vector_writable ==
false)
375 IndexSet parallel_partitioner = locally_owned_entries;
379 vector = std::make_unique<Epetra_FEVector>(map);
386 ExcMessage(
"A writable vector must not have ghost entries in "
387 "its parallel partitioning"));
389 if (
vector->Map().SameAs(map) ==
false)
390 vector = std::make_unique<Epetra_FEVector>(map);
393 const int ierr =
vector->PutScalar(0.);
398 IndexSet nonlocal_entries(ghost_entries);
402 Epetra_Map nonlocal_map =
405 std::make_unique<Epetra_MultiVector>(nonlocal_map, 1);
426 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
427 const bool make_ghosted,
428 const bool vector_writable)
432 Assert(partitioner->ghost_indices_initialized(),
433 ExcMessage(
"You asked to create a ghosted vector, but the "
434 "partitioner does not provide ghost indices."));
436 this->
reinit(partitioner->locally_owned_range(),
437 partitioner->ghost_indices(),
438 partitioner->get_mpi_communicator(),
443 this->
reinit(partitioner->locally_owned_range(),
444 partitioner->get_mpi_communicator());
454 ExcMessage(
"Vector is not constructed properly."));
458 const Epetra_MpiComm *my_comm =
459 dynamic_cast<const Epetra_MpiComm *
>(&
vector->Comm());
460 const Epetra_MpiComm *v_comm =
461 dynamic_cast<const Epetra_MpiComm *
>(&v.
vector->Comm());
462 const bool same_communicators = my_comm !=
nullptr && v_comm !=
nullptr &&
463 my_comm->DataPtr() == v_comm->DataPtr();
490 if (same_communicators && v.
vector->Map().SameAs(
vector->Map()))
503 (v.
vector->Map().UniqueGIDs() ||
vector->Map().UniqueGIDs()))
537 template <
typename number>
563 "Cannot find exchange information!"));
565 ExcMessage(
"The input vector has overlapping data, "
566 "which is not allowed."));
572 Epetra_Import data_exchange(
vector->Map(), v.
vector->Map());
573 const int ierr =
vector->Import(*v.
vector, data_exchange, Insert);
588 "Both vectors need to have the same size for import_elements() to work!"));
598 (*
this)[idx] = rwv[idx];
603 (*
this)[idx] += rwv[idx];
617 "Calling compress() is only useful if a vector "
618 "has been written into, but this is a vector with ghost "
619 "elements and consequently is read-only. It does "
620 "not make sense to call compress() for such "
639 "compress() can only be called with VectorOperation add, insert, or unknown"));
649 "The last operation on the Vector and the given last action in the compress() call do not agree!"));
658 const double double_mode = mode;
659 const Epetra_MpiComm *comm_ptr =
dynamic_cast<const Epetra_MpiComm *
>(
667 "Not all processors agree whether the last operation on "
668 "this vector was an addition or a set operation. This will "
669 "prevent the compress() operation from succeeding."));
675 const auto ierr =
vector->GlobalAssemble(mode);
705 if (trilinos_i == -1)
707# ifndef DEAL_II_WITH_64BIT_INDICES
710 vector->Map().NumMyElements(),
712 vector->Map().MaxMyGID()));
716 vector->Map().NumMyElements(),
717 vector->Map().MinMyGID64(),
718 vector->Map().MaxMyGID64()));
722 value = (*vector)[0][trilinos_i];
732 if (allow_different_maps ==
false)
740 Epetra_Import data_exchange(
vector->Map(), v.
vector->Map());
742 vector->Import(*v.
vector, data_exchange, Epetra_AddLocalAlso);
754 if (
vector->Map().NumMyElements() != v.
vector->Map().NumMyElements())
758 for (
size_type i = 0; i < vector_size; ++i)
772 return (!(*
this == v));
784 *eptr = start_ptr +
vector->Map().NumMyElements();
785 unsigned int flag = 0;
798 const Epetra_MpiComm *mpi_comm =
799 dynamic_cast<const Epetra_MpiComm *
>(&
vector->Map().Comm());
802 return num_nonzero == 0;
814 *eptr = start_ptr +
vector->Map().NumMyElements();
815 unsigned int flag = 0;
828 const auto max_n_negative =
830 return max_n_negative == 0;
837 const unsigned int precision,
838 const bool scientific,
839 const bool across)
const
842 boost::io::ios_flags_saver restore_flags(out);
845 out.precision(precision);
847 out.setf(std::ios::scientific, std::ios::floatfield);
849 out.setf(std::ios::fixed, std::ios::floatfield);
852 if (
size() != vector_size)
854 auto global_id = [&](
const size_type index) {
857 out <<
"size:" <<
size()
858 <<
" locally_owned_size:" <<
vector->Map().NumMyElements() <<
" :"
860 for (
size_type i = 0; i < vector_size; ++i)
861 out <<
"[" << global_id(i) <<
"]: " << (*(
vector))[0][i]
867 int leading_dimension;
868 int ierr =
vector->ExtractView(&val, &leading_dimension);
873 out <<
static_cast<double>(val[i]) <<
' ';
876 out <<
static_cast<double>(val[i]) << std::endl;
888 std::swap(last_action, v.last_action);
889 std::swap(compressed, v.compressed);
890 std::swap(has_ghosts, v.has_ghosts);
891 std::swap(vector, v.vector);
892 std::swap(nonlocal_vector, v.nonlocal_vector);
893 std::swap(owned_elements, v.owned_elements);
906 return sizeof(*this) +
907 this->
vector->Map().NumMyElements() *
913# include "lac/trilinos_vector.inst"
virtual size_type size() const override
unsigned int n_blocks() const
BlockType & block(const unsigned int i)
bool is_ascending_and_one_to_one(const MPI_Comm communicator) const
size_type n_elements() const
void set_size(const size_type size)
Epetra_Map make_trilinos_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
void subtract_set(const IndexSet &other)
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
size_type size() const override
const IndexSet & get_stored_elements() const
void compress(VectorOperation::values operation)
void add(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
std::unique_ptr< Epetra_MultiVector > nonlocal_vector
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
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
IndexSet locally_owned_elements() const
void reinit(const Vector &v, const bool omit_zeroing_entries=false, const bool allow_different_maps=false)
bool has_ghost_elements() const
std::pair< size_type, size_type > local_range() const
bool operator!=(const Vector &v) const
bool operator==(const Vector &v) const
Epetra_CombineMode last_action
Vector & operator=(const TrilinosScalar s)
bool is_non_negative() const
std::size_t memory_consumption() const
const Epetra_CrsMatrix & trilinos_matrix() const
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
void swap(BlockVector &u, BlockVector &v) noexcept
TrilinosWrappers::types::int_type global_length(const Epetra_MultiVector &vector)
int gid(const Epetra_BlockMap &map, int i)
TrilinosWrappers::types::int_type * my_global_elements(const Epetra_BlockMap &map)
TrilinosWrappers::types::int64_type n_global_elements(const Epetra_BlockMap &map)
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm mpi_communicator)