From 03f2c7adb24ee8d8fd18e278a4e45cb04e224d0d Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Thu, 18 Mar 2021 10:02:53 +0100 Subject: [PATCH] LinearAlgebra::ReadWriteVector accept serial vectors --- doc/news/changes/minor/20210318Munch | 5 ++ include/deal.II/lac/read_write_vector.h | 32 ++++++++++ .../deal.II/lac/read_write_vector.templates.h | 63 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 doc/news/changes/minor/20210318Munch diff --git a/doc/news/changes/minor/20210318Munch b/doc/news/changes/minor/20210318Munch new file mode 100644 index 0000000000..32227fec43 --- /dev/null +++ b/doc/news/changes/minor/20210318Munch @@ -0,0 +1,5 @@ +Improved: The class LinearAlgebra::ReadWriteVector now also can import +from Vector and LinearAlgebra::Vector. +
+(Peter Munch, 2021/03/18) + diff --git a/include/deal.II/lac/read_write_vector.h b/include/deal.II/lac/read_write_vector.h index 8f799efd4c..87cf1ada46 100644 --- a/include/deal.II/lac/read_write_vector.h +++ b/include/deal.II/lac/read_write_vector.h @@ -47,6 +47,8 @@ DEAL_II_NAMESPACE_OPEN #ifndef DOXYGEN namespace LinearAlgebra { + template + class Vector; namespace distributed { template @@ -283,6 +285,36 @@ namespace LinearAlgebra ReadWriteVector & operator=(const Number s); + /** + * Imports all the elements present in the vector's IndexSet from the + * input vector @p vec. VectorOperation::values @p operation + * is used to decide if the elements in @p V should be added to the + * current vector or replace the current elements. + * + * @note The parameter @p communication_pattern is ignored since we are + * dealing with a serial vector here. + */ + void + import(const dealii::Vector &vec, + VectorOperation::values operation, + const std::shared_ptr + &communication_pattern = {}); + + /** + * Imports all the elements present in the vector's IndexSet from the + * input vector @p vec. VectorOperation::values @p operation + * is used to decide if the elements in @p V should be added to the + * current vector or replace the current elements. + * + * @note The parameter @p communication_pattern is ignored since we are + * dealing with a serial vector here. + */ + void + import(const LinearAlgebra::Vector &vec, + VectorOperation::values operation, + const std::shared_ptr + &communication_pattern = {}); + /** * Imports all the elements present in the vector's IndexSet from the * input vector @p vec. VectorOperation::values @p operation diff --git a/include/deal.II/lac/read_write_vector.templates.h b/include/deal.II/lac/read_write_vector.templates.h index 3d87efef23..3ddfaca6f8 100644 --- a/include/deal.II/lac/read_write_vector.templates.h +++ b/include/deal.II/lac/read_write_vector.templates.h @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -409,6 +410,68 @@ namespace LinearAlgebra + namespace internal + { + template + void + import_serial_vector(const VectorType & values, + VectorOperation::values operation, + ReadWriteVector &rw_vector) + { + using size_type = types::global_dof_index; + + const IndexSet &stored = rw_vector.get_stored_elements(); + if (operation == VectorOperation::add) + for (size_type i = 0; i < stored.n_elements(); ++i) + rw_vector.local_element(i) += values(stored.nth_index_in_set(i)); + else if (operation == VectorOperation::min) + for (size_type i = 0; i < stored.n_elements(); ++i) + rw_vector.local_element(i) = + get_min(values(stored.nth_index_in_set(i)), + rw_vector.local_element(i)); + else if (operation == VectorOperation::max) + for (size_type i = 0; i < stored.n_elements(); ++i) + rw_vector.local_element(i) = + get_max(values(stored.nth_index_in_set(i)), + rw_vector.local_element(i)); + else + for (size_type i = 0; i < stored.n_elements(); ++i) + rw_vector.local_element(i) = values(stored.nth_index_in_set(i)); + } + } // namespace internal + + + + template + void + ReadWriteVector::import( + const dealii::Vector &vec, + VectorOperation::values operation, + const std::shared_ptr + &communication_pattern) + { + (void)communication_pattern; + + internal::import_serial_vector(vec, operation, *this); + } + + + + template + void + ReadWriteVector::import( + const LinearAlgebra::Vector &vec, + VectorOperation::values operation, + const std::shared_ptr + &communication_pattern) + { + (void)communication_pattern; + + internal::import_serial_vector(vec, operation, *this); + } + + + template template void -- 2.39.5