// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 1999 - 2025 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_block_vector_h #define dealii_block_vector_h #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN // Forward declaration #ifndef DOXYGEN # ifdef DEAL_II_WITH_TRILINOS namespace TrilinosWrappers { namespace MPI { class BlockVector; } } // namespace TrilinosWrappers # endif #endif /** * @addtogroup Vectors * @{ */ /** * An implementation of block vectors based on deal.II vectors. While the base * class provides for most of the interface, this class handles the actual * allocation of vectors and provides functions that are specific to the * underlying vector type. * * @note Instantiations for this template are provided for @ and * @; others can be generated in application programs (see the * section on * @ref Instantiations * in the manual). * * @see * @ref GlossBlockLA "Block (linear algebra)" */ template class BlockVector : public BlockVectorBase> { public: /** * Typedef the base class for simpler access to its own alias. */ using BaseClass = BlockVectorBase>; /** * Typedef the type of the underlying vector. */ using BlockType = typename BaseClass::BlockType; /** * Import the alias from the base class. */ using value_type = typename BaseClass::value_type; using real_type = typename BaseClass::real_type; using pointer = typename BaseClass::pointer; using const_pointer = typename BaseClass::const_pointer; using reference = typename BaseClass::reference; using const_reference = typename BaseClass::const_reference; using size_type = typename BaseClass::size_type; using iterator = typename BaseClass::iterator; using const_iterator = typename BaseClass::const_iterator; /** * Constructor. There are three ways to use this constructor. First, without * any arguments, it generates an object with no blocks. Given one argument, * it initializes n_blocks blocks, but these blocks have size zero. * The third variant finally initializes all blocks to the same size * block_size. * * Confer the other constructor further down if you intend to use blocks of * different sizes. */ explicit BlockVector(const unsigned int n_blocks = 0, const size_type block_size = 0); /** * Copy Constructor. Dimension set to that of @p v, all components are * copied from @p v. */ BlockVector(const BlockVector &V); /** * Move constructor. Creates a new vector by stealing the internal data of * the given argument vector. */ BlockVector(BlockVector && /*v*/) noexcept = default; /** * Copy constructor taking a BlockVector of another data type. This will * fail if there is no conversion path from OtherNumber to * Number. Note that you may lose accuracy when copying to a * BlockVector with data elements with less accuracy. */ template explicit BlockVector(const BlockVector &v); #ifdef DEAL_II_WITH_TRILINOS /** * A copy constructor taking a (parallel) Trilinos block vector and copying * it into the deal.II own format. */ BlockVector(const TrilinosWrappers::MPI::BlockVector &v); #endif /** * Constructor. Set the number of blocks to block_sizes.size() and * initialize each block with block_sizes[i] zero elements. */ BlockVector(const std::vector &block_sizes); /** * Constructor. Initialize vector to the structure found in the BlockIndices * argument. */ BlockVector(const BlockIndices &block_indices); /** * Constructor. Set the number of blocks to block_sizes.size(). * Initialize the vector with the elements pointed to by the range of * iterators given as second and third argument. Apart from the first * argument, this constructor is in complete analogy to the respective * constructor of the std::vector class, but the first argument is * needed in order to know how to subdivide the block vector into different * blocks. */ template BlockVector(const std::vector &block_sizes, const InputIterator first, const InputIterator end); /** * Destructor. Clears memory */ ~BlockVector() override = default; /** * Call the compress() function on all the subblocks. * * This functionality only needs to be called if using MPI based vectors and * exists in other objects for compatibility. * * See * @ref GlossCompress "Compressing distributed objects" * for more information. */ void compress(VectorOperation::values operation = VectorOperation::unknown); /** * Returns `false` as this is a serial block vector. * * This functionality only needs to be called if using MPI based vectors and * exists in other objects for compatibility. */ bool has_ghost_elements() const; /** * Copy operator: fill all components of the vector with the given scalar * value. */ BlockVector & operator=(const value_type s); /** * Copy operator for arguments of the same type. Resize the present vector * if necessary to the correct number of blocks, then copy the individual * blocks from `v` using the copy-assignment operator of the class that * represents the individual blocks. */ BlockVector & operator=(const BlockVector &v); /** * Move the given vector. This operator replaces the present vector with * the contents of the given argument vector. */ BlockVector & operator=(BlockVector && /*v*/) = default; // NOLINT /** * Copy operator for template arguments of different types. Resize the * present vector if necessary. */ template BlockVector & operator=(const BlockVector &V); /** * Copy a regular vector into a block vector. */ BlockVector & operator=(const Vector &V); #ifdef DEAL_II_WITH_TRILINOS /** * A copy constructor from a Trilinos block vector to a deal.II block * vector. */ BlockVector & operator=(const TrilinosWrappers::MPI::BlockVector &V); #endif /** * Reinitialize the BlockVector to contain n_blocks blocks of size * block_size each. * * If the second argument is left at its default value, then the block * vector allocates the specified number of blocks but leaves them at zero * size. You then need to later reinitialize the individual blocks, and call * collect_sizes() to update the block system's knowledge of its individual * block's sizes. * * If omit_zeroing_entries==false, the vector is filled with zeros. */ void reinit(const unsigned int n_blocks, const size_type block_size = 0, const bool omit_zeroing_entries = false); /** * Reinitialize the BlockVector such that it contains * block_sizes.size() blocks. Each block is reinitialized to * dimension block_sizes[i]. * * If the number of blocks is the same as before this function was called, * all vectors remain the same and reinit() is called for each vector. * * If omit_zeroing_entries==false, the vector is filled with zeros. * * Note that you must call this (or the other reinit() functions) function, * rather than calling the reinit() functions of an individual block, to * allow the block vector to update its caches of vector sizes. If you call * reinit() on one of the blocks, then subsequent actions on this object may * yield unpredictable results since they may be routed to the wrong block. */ void reinit(const std::vector &block_sizes, const bool omit_zeroing_entries = false); /** * Reinitialize the BlockVector to reflect the structure found in * BlockIndices. * * If the number of blocks is the same as before this function was called, * all vectors remain the same and reinit() is called for each vector. * * If omit_zeroing_entries==false, the vector is filled with zeros. */ void reinit(const BlockIndices &block_indices, const bool omit_zeroing_entries = false); /** * Change the dimension to that of the vector V. The same applies * as for the other reinit() function. * * The elements of V are not copied, i.e. this function is the * same as calling reinit (V.size(), omit_zeroing_entries). * * Note that you must call this (or the other reinit() functions) function, * rather than calling the reinit() functions of an individual block, to * allow the block vector to update its caches of vector sizes. If you call * reinit() of one of the blocks, then subsequent actions of this object may * yield unpredictable results since they may be routed to the wrong block. */ template void reinit(const BlockVector &V, const bool omit_zeroing_entries = false); /** * Multiply each element of this vector by the corresponding element of * v. */ template void scale(const BlockVector2 &v); /** * Swap the contents of this vector and the other vector v. One * could do this operation with a temporary variable and copying over the * data elements, but this function is significantly more efficient since it * only swaps the pointers to the data of the two vectors and therefore does * not need to allocate temporary storage and move data around. * * This function is analogous to the swap() function of all C++ standard * containers. Also, there is a global function swap(u,v) that simply calls * u.swap(v), again in analogy to standard functions. */ void swap(BlockVector &v) noexcept; /** * Print to a stream. */ void print(std::ostream &out, const unsigned int precision = 3, const bool scientific = true, const bool across = true) const; /** * Write the vector en bloc to a stream. This is done in a binary mode, so * the output is neither readable by humans nor (probably) by other * computers using a different operating system or number format. */ void block_write(std::ostream &out) const; /** * Read a vector en block from a file. This is done using the inverse * operations to the above function, so it is reasonably fast because the * bitstream is not interpreted. * * The vector is resized if necessary. * * A primitive form of error checking is performed which will recognize the * bluntest attempts to interpret some data as a vector stored bitwise to a * file, but not more. */ void block_read(std::istream &in); /** * @addtogroup Exceptions * @{ */ /** * Exception */ DeclException0(ExcIteratorRangeDoesNotMatchVectorSize); /** @} */ }; /** @} */ #ifndef DOXYGEN /*----------------------- Inline functions ----------------------------------*/ template template BlockVector::BlockVector(const std::vector &block_sizes, const InputIterator first, const InputIterator end) { // first set sizes of blocks, but // don't initialize them as we will // copy elements soon reinit(block_sizes, true); InputIterator start = first; for (size_type b = 0; b < block_sizes.size(); ++b) { InputIterator end = start; std::advance(end, static_cast(block_sizes[b])); std::copy(start, end, this->block(b).begin()); start = end; }; Assert(start == end, ExcIteratorRangeDoesNotMatchVectorSize()); } template inline BlockVector & BlockVector::operator=(const value_type s) { AssertIsFinite(s); BaseClass::operator=(s); return *this; } template inline BlockVector & BlockVector::operator=(const BlockVector &v) { reinit(v, true); BaseClass::operator=(v); return *this; } template inline BlockVector & BlockVector::operator=(const Vector &v) { BaseClass::operator=(v); return *this; } template template inline BlockVector & BlockVector::operator=(const BlockVector &v) { reinit(v, true); BaseClass::operator=(v); return *this; } template inline void BlockVector::compress(VectorOperation::values operation) { for (size_type i = 0; i < this->n_blocks(); ++i) this->components[i].compress(operation); } template inline bool BlockVector::has_ghost_elements() const { return false; } template template void BlockVector::scale(const BlockVector2 &v) { BaseClass::scale(v); } #endif // DOXYGEN /** * Global function which overloads the default implementation of the C++ * standard library which uses a temporary object. The function simply * exchanges the data of the two vectors. * * @relatesalso BlockVector */ template inline void swap(BlockVector &u, BlockVector &v) noexcept { u.swap(v); } namespace internal { namespace LinearOperatorImplementation { template class ReinitHelper; /** * A helper class used internally in linear_operator.h. Specialization for * BlockVector. */ template class ReinitHelper> { public: template static void reinit_range_vector(const Matrix &matrix, BlockVector &v, bool omit_zeroing_entries) { v.reinit(matrix.get_row_indices(), omit_zeroing_entries); } template static void reinit_domain_vector(const Matrix &matrix, BlockVector &v, bool omit_zeroing_entries) { v.reinit(matrix.get_column_indices(), omit_zeroing_entries); } }; } // namespace LinearOperatorImplementation } /* namespace internal */ /** * Declare dealii::BlockVector as serial vector. */ template struct is_serial_vector> : std::true_type {}; DEAL_II_NAMESPACE_CLOSE #endif