MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
blockoperator.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_BLOCKOPERATOR
13#define MFEM_BLOCKOPERATOR
14
15#include "../config/config.hpp"
16#include "../general/array.hpp"
17#include "operator.hpp"
18#include "blockvector.hpp"
19#ifdef MFEM_USE_MPI
20#include "hypre.hpp"
21#endif
22
23namespace mfem
24{
25
26//! @class BlockOperator
27/**
28 * \brief A class to handle Block systems in a matrix-free implementation.
29 *
30 * Usage:
31 * - Use one of the constructors to define the block structure.
32 * - Use SetDiagonalBlock or SetBlock to fill the BlockOperator
33 * - Use the method Mult and MultTranspose to apply the operator to a vector.
34 *
35 * If a block is not set, it is assumed to be a zero block.
36 */
37class BlockOperator : public Operator
38{
39public:
40 //! Constructor for BlockOperators with the same block-structure for rows and
41 //! columns.
42 /**
43 * offsets: offsets that mark the start of each row/column block (size
44 * nRowBlocks+1).
45 */
46 BlockOperator(const Array<int> & offsets);
47 //! Constructor for general BlockOperators.
48 /**
49 * row_offsets: offsets that mark the start of each row block (size
50 * nRowBlocks+1). col_offsets: offsets that mark the start of each column
51 * block (size nColBlocks+1).
52 */
53 BlockOperator(const Array<int> & row_offsets, const Array<int> & col_offsets);
54
55 /// Copy assignment is not supported
57
58 /// Move assignment is not supported
60
61 //! Add block op in the block-entry (iblock, iblock).
62 /**
63 * iblock: The block will be inserted in location (iblock, iblock).
64 * op: the Operator to be inserted.
65 * c: optional scalar multiple for this block.
66 */
67 void SetDiagonalBlock(int iblock, Operator *op, real_t c = 1.0);
68 //! Add a block op in the block-entry (iblock, jblock).
69 /**
70 * irow, icol: The block will be inserted in location (irow, icol).
71 * op: the Operator to be inserted.
72 * c: optional scalar multiple for this block.
73 */
74 void SetBlock(int iRow, int iCol, Operator *op, real_t c = 1.0);
75
76 //! Return the number of row blocks
77 int NumRowBlocks() const { return nRowBlocks; }
78 //! Return the number of column blocks
79 int NumColBlocks() const { return nColBlocks; }
80
81 //! Check if block (i,j) is a zero block
82 int IsZeroBlock(int i, int j) const { return (op(i,j)==NULL) ? 1 : 0; }
83 //! Return a reference to block i,j
84 Operator & GetBlock(int i, int j)
85 { MFEM_VERIFY(op(i,j), ""); return *op(i,j); }
86 //! Return a reference to block i,j (const version)
87 const Operator & GetBlock(int i, int j) const
88 { MFEM_VERIFY(op(i,j), ""); return *op(i,j); }
89 //! Return the coefficient for block i,j
90 real_t GetBlockCoef(int i, int j) const
91 { MFEM_VERIFY(op(i,j), ""); return coef(i,j); }
92 //! Set the coefficient for block i,j
93 void SetBlockCoef(int i, int j, real_t c)
94 { MFEM_VERIFY(op(i,j), ""); coef(i,j) = c; }
95
96 //! Return the row offsets for block starts
97 Array<int> & RowOffsets() { return row_offsets; }
98 //! Read only access to the row offsets for block starts
99 const Array<int> & RowOffsets() const { return row_offsets; }
100 //! Return the columns offsets for block starts
101 Array<int> & ColOffsets() { return col_offsets; }
102 //! Read only access to the columns offsets for block starts
103 const Array<int> & ColOffsets() const { return col_offsets; }
104
105 /// Operator application
106 void Mult (const Vector & x, Vector & y) const override;
107
108 /// Action of the transpose operator
109 void MultTranspose (const Vector & x, Vector & y) const override;
110
111#ifdef MFEM_USE_MPI
112 /** @brief Returns a monolithic HypreParMatrix formed by merging the blocks of
113 this BlockOperator, assuming every block is a HypreParMatrix.
114
115 The returned matrix is newly allocated and owned by the caller, who is
116 responsible for deleting it. */
118#endif
119
121
122 //! Controls the ownership of the blocks: if nonzero, BlockOperator will
123 //! delete all blocks that are set (non-NULL); the default value is zero.
125
126 virtual Type GetType() const { return MFEM_Block_Operator; }
127
128private:
129 //! Number of block rows
130 int nRowBlocks;
131 //! Number of block columns
132 int nColBlocks;
133 //! Row offsets for the starting position of each block
134 Array<int> row_offsets;
135 //! Column offsets for the starting position of each block
136 Array<int> col_offsets;
137 //! 2D array that stores each block of the operator.
139 //! 2D array that stores a coefficient for each block of the operator.
140 Array2D<real_t> coef;
141
142 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose methods.
143 mutable BlockVector xblock;
144 mutable BlockVector yblock;
145 mutable Vector tmp;
146};
147
148//! @class BlockDiagonalPreconditioner
149/**
150 * \brief A class to handle Block diagonal preconditioners in a matrix-free implementation.
151 *
152 * Usage:
153 * - Use the constructors to define the block structure
154 * - Use SetDiagonalBlock to fill the BlockDiagonalPreconditioner
155 * - Use the method Mult and MultTranspose to apply the operator to a vector.
156 *
157 * If a block is not set, it is assumed to be an identity block.
158 *
159 */
161{
162public:
163 //! Constructor that specifies the block structure
165 //! Add a square block op in the block-entry (iblock, iblock).
166 /**
167 * iblock: The block will be inserted in location (iblock, iblock).
168 * op: the Operator to be inserted.
169 */
170 void SetDiagonalBlock(int iblock, Operator *op);
171 //! This method is present since required by the abstract base class Solver
172 void SetOperator(const Operator &op) override { }
173
174 //! Return the number of blocks
175 int NumBlocks() const { return nBlocks; }
176
177 //! Return a reference to block i,i.
179 { MFEM_VERIFY(ops[iblock], ""); return *ops[iblock]; }
180
181 //! Return a reference to block i,i (const version).
182 const Operator & GetDiagonalBlock(int iblock) const
183 { MFEM_VERIFY(ops[iblock], ""); return *ops[iblock]; }
184
185 //! Return the offsets for block starts
186 Array<int> & Offsets() { return offsets; }
187
188 //! Read only access to the offsets for block starts
189 const Array<int> & Offsets() const { return offsets; }
190
191 /// Operator application
192 void Mult (const Vector & x, Vector & y) const override;
193
194 /// Action of the transpose operator
195 void MultTranspose (const Vector & x, Vector & y) const override;
196
198
199 //! Controls the ownership of the blocks: if nonzero,
200 //! BlockDiagonalPreconditioner will delete all blocks that are set
201 //! (non-NULL); the default value is zero.
203
204private:
205 //! Number of Blocks
206 int nBlocks;
207 //! Offsets for the starting position of each block
208 Array<int> offsets;
209 //! 1D array that stores each block of the operator.
211 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose
212 //! methods.
213 mutable BlockVector xblock;
214 mutable BlockVector yblock;
215};
216
217//! @class BlockLowerTriangularPreconditioner
218/**
219 * \brief A class to handle Block lower triangular preconditioners in a
220 * matrix-free implementation.
221 *
222 * Usage:
223 * - Use the constructors to define the block structure
224 * - Use SetBlock() to fill the BlockLowerTriangularOperator
225 * - Diagonal blocks of the preconditioner should approximate the inverses of
226 * the diagonal block of the matrix
227 * - Off-diagonal blocks of the preconditioner should match/approximate those of
228 * the original matrix
229 * - Use the method Mult() and MultTranspose() to apply the operator to a vector.
230 *
231 * If a diagonal block is not set, it is assumed to be an identity block, if an
232 * off-diagonal block is not set, it is assumed to be a zero block.
233 *
234 */
236{
237public:
238 //! Constructor for BlockLowerTriangularPreconditioner%s with the same
239 //! block-structure for rows and columns.
240 /**
241 * @param offsets Offsets that mark the start of each row/column block
242 * (size nBlocks+1).
243 *
244 * @note BlockLowerTriangularPreconditioner will not own/copy the data
245 * contained in @a offsets.
246 */
248
249 //! Add block op in the block-entry (iblock, iblock).
250 /**
251 * @param iblock The block will be inserted in location (iblock, iblock).
252 * @param op The Operator to be inserted.
253 */
254 void SetDiagonalBlock(int iblock, Operator *op);
255 //! Add a block opt in the block-entry (iblock, jblock).
256 /**
257 * @param iRow, iCol The block will be inserted in location (iRow, iCol).
258 * @param op The Operator to be inserted.
259 */
260 void SetBlock(int iRow, int iCol, Operator *op);
261 //! This method is present since required by the abstract base class Solver
262 void SetOperator(const Operator &op) override { }
263
264 //! Return the number of blocks
265 int NumBlocks() const { return nBlocks; }
266
267 //! Return a reference to block i,j.
268 Operator & GetBlock(int iblock, int jblock)
269 { MFEM_VERIFY(ops(iblock,jblock), ""); return *ops(iblock,jblock); }
270
271 //! Return the offsets for block starts
272 Array<int> & Offsets() { return offsets; }
273
274 /// Operator application
275 void Mult (const Vector & x, Vector & y) const override;
276
277 /// Action of the transpose operator
278 void MultTranspose (const Vector & x, Vector & y) const override;
279
281
282 //! Controls the ownership of the blocks: if nonzero,
283 //! BlockLowerTriangularPreconditioner will delete all blocks that are set
284 //! (non-NULL); the default value is zero.
286
287private:
288 //! Number of block rows/columns
289 int nBlocks;
290 //! Offsets for the starting position of each block
291 Array<int> offsets;
292 //! 2D array that stores each block of the operator.
294
295 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose
296 //! methods.
297 mutable BlockVector xblock;
298 mutable BlockVector yblock;
299 mutable Vector tmp;
300 mutable Vector tmp2;
301};
302
303}
304
305#endif /* MFEM_BLOCKOPERATOR */
Dynamic 2D array using row-major layout.
Definition array.hpp:459
A class to handle Block diagonal preconditioners in a matrix-free implementation.
const Operator & GetDiagonalBlock(int iblock) const
Return a reference to block i,i (const version).
void Mult(const Vector &x, Vector &y) const override
Operator application.
const Array< int > & Offsets() const
Read only access to the offsets for block starts.
void MultTranspose(const Vector &x, Vector &y) const override
Action of the transpose operator.
Operator & GetDiagonalBlock(int iblock)
Return a reference to block i,i.
BlockDiagonalPreconditioner(const Array< int > &offsets)
Constructor that specifies the block structure.
Array< int > & Offsets()
Return the offsets for block starts.
int NumBlocks() const
Return the number of blocks.
void SetDiagonalBlock(int iblock, Operator *op)
Add a square block op in the block-entry (iblock, iblock).
void SetOperator(const Operator &op) override
This method is present since required by the abstract base class Solver.
A class to handle Block lower triangular preconditioners in a matrix-free implementation.
int NumBlocks() const
Return the number of blocks.
void SetDiagonalBlock(int iblock, Operator *op)
Add block op in the block-entry (iblock, iblock).
void Mult(const Vector &x, Vector &y) const override
Operator application.
BlockLowerTriangularPreconditioner(const Array< int > &offsets)
Operator & GetBlock(int iblock, int jblock)
Return a reference to block i,j.
void SetOperator(const Operator &op) override
This method is present since required by the abstract base class Solver.
void SetBlock(int iRow, int iCol, Operator *op)
Add a block opt in the block-entry (iblock, jblock).
Array< int > & Offsets()
Return the offsets for block starts.
void MultTranspose(const Vector &x, Vector &y) const override
Action of the transpose operator.
A class to handle Block systems in a matrix-free implementation.
const Array< int > & ColOffsets() const
Read only access to the columns offsets for block starts.
Array< int > & RowOffsets()
Return the row offsets for block starts.
const Operator & GetBlock(int i, int j) const
Return a reference to block i,j (const version)
real_t GetBlockCoef(int i, int j) const
Return the coefficient for block i,j.
const Array< int > & RowOffsets() const
Read only access to the row offsets for block starts.
int IsZeroBlock(int i, int j) const
Check if block (i,j) is a zero block.
void SetDiagonalBlock(int iblock, Operator *op, real_t c=1.0)
Add block op in the block-entry (iblock, iblock).
void Mult(const Vector &x, Vector &y) const override
Operator application.
BlockOperator(const Array< int > &offsets)
BlockOperator & operator=(BlockOperator &&)=delete
Move assignment is not supported.
void SetBlock(int iRow, int iCol, Operator *op, real_t c=1.0)
Add a block op in the block-entry (iblock, jblock).
Operator & GetBlock(int i, int j)
Return a reference to block i,j.
void MultTranspose(const Vector &x, Vector &y) const override
Action of the transpose operator.
BlockOperator & operator=(const BlockOperator &)=delete
Copy assignment is not supported.
int NumRowBlocks() const
Return the number of row blocks.
int NumColBlocks() const
Return the number of column blocks.
Array< int > & ColOffsets()
Return the columns offsets for block starts.
virtual Type GetType() const
HypreParMatrix * GetMonolithicHypreParMatrix() const
Returns a monolithic HypreParMatrix formed by merging the blocks of this BlockOperator,...
void SetBlockCoef(int i, int j, real_t c)
Set the coefficient for block i,j.
A class to handle Vectors in a block fashion.
Wrapper for hypre's ParCSR matrix class.
Definition hypre.hpp:419
Abstract operator.
Definition operator.hpp:27
Type
Enumeration defining IDs for some classes derived from Operator.
Definition operator.hpp:319
@ MFEM_Block_Operator
ID for the base class BlockOperator.
Definition operator.hpp:334
Base class for solvers.
Definition operator.hpp:855
Vector data type.
Definition vector.hpp:82
float real_t
Definition config.hpp:46