MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
blockoperator.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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
20namespace mfem
21{
22
23//! @class BlockOperator
24/**
25 * \brief A class to handle Block systems in a matrix-free implementation.
26 *
27 * Usage:
28 * - Use one of the constructors to define the block structure.
29 * - Use SetDiagonalBlock or SetBlock to fill the BlockOperator
30 * - Use the method Mult and MultTranspose to apply the operator to a vector.
31 *
32 * If a block is not set, it is assumed to be a zero block.
33 */
34class BlockOperator : public Operator
35{
36public:
37 //! Constructor for BlockOperators with the same block-structure for rows and
38 //! columns.
39 /**
40 * offsets: offsets that mark the start of each row/column block (size
41 * nRowBlocks+1).
42 */
43 BlockOperator(const Array<int> & offsets);
44 //! Constructor for general BlockOperators.
45 /**
46 * row_offsets: offsets that mark the start of each row block (size
47 * nRowBlocks+1). col_offsets: offsets that mark the start of each column
48 * block (size nColBlocks+1).
49 */
50 BlockOperator(const Array<int> & row_offsets, const Array<int> & col_offsets);
51
52 /// Copy assignment is not supported
54
55 /// Move assignment is not supported
57
58 //! Add block op in the block-entry (iblock, iblock).
59 /**
60 * iblock: The block will be inserted in location (iblock, iblock).
61 * op: the Operator to be inserted.
62 * c: optional scalar multiple for this block.
63 */
64 void SetDiagonalBlock(int iblock, Operator *op, real_t c = 1.0);
65 //! Add a block op in the block-entry (iblock, jblock).
66 /**
67 * irow, icol: The block will be inserted in location (irow, icol).
68 * op: the Operator to be inserted.
69 * c: optional scalar multiple for this block.
70 */
71 void SetBlock(int iRow, int iCol, Operator *op, real_t c = 1.0);
72
73 //! Return the number of row blocks
74 int NumRowBlocks() const { return nRowBlocks; }
75 //! Return the number of column blocks
76 int NumColBlocks() const { return nColBlocks; }
77
78 //! Check if block (i,j) is a zero block
79 int IsZeroBlock(int i, int j) const { return (op(i,j)==NULL) ? 1 : 0; }
80 //! Return a reference to block i,j
81 Operator & GetBlock(int i, int j)
82 { MFEM_VERIFY(op(i,j), ""); return *op(i,j); }
83 //! Return a reference to block i,j (const version)
84 const Operator & GetBlock(int i, int j) const
85 { MFEM_VERIFY(op(i,j), ""); return *op(i,j); }
86 //! Return the coefficient for block i,j
87 real_t GetBlockCoef(int i, int j) const
88 { MFEM_VERIFY(op(i,j), ""); return coef(i,j); }
89 //! Set the coefficient for block i,j
90 void SetBlockCoef(int i, int j, real_t c)
91 { MFEM_VERIFY(op(i,j), ""); coef(i,j) = c; }
92
93 //! Return the row offsets for block starts
94 Array<int> & RowOffsets() { return row_offsets; }
95 //! Read only access to the row offsets for block starts
96 const Array<int> & RowOffsets() const { return row_offsets; }
97 //! Return the columns offsets for block starts
98 Array<int> & ColOffsets() { return col_offsets; }
99 //! Read only access to the columns offsets for block starts
100 const Array<int> & ColOffsets() const { return col_offsets; }
101
102 /// Operator application
103 virtual void Mult (const Vector & x, Vector & y) const;
104
105 /// Action of the transpose operator
106 virtual void MultTranspose (const Vector & x, Vector & y) const;
107
109
110 //! Controls the ownership of the blocks: if nonzero, BlockOperator will
111 //! delete all blocks that are set (non-NULL); the default value is zero.
113
114 virtual Type GetType() const { return MFEM_Block_Operator; }
115
116private:
117 //! Number of block rows
118 int nRowBlocks;
119 //! Number of block columns
120 int nColBlocks;
121 //! Row offsets for the starting position of each block
122 Array<int> row_offsets;
123 //! Column offsets for the starting position of each block
124 Array<int> col_offsets;
125 //! 2D array that stores each block of the operator.
127 //! 2D array that stores a coefficient for each block of the operator.
128 Array2D<real_t> coef;
129
130 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose methods.
131 mutable BlockVector xblock;
132 mutable BlockVector yblock;
133 mutable Vector tmp;
134};
135
136//! @class BlockDiagonalPreconditioner
137/**
138 * \brief A class to handle Block diagonal preconditioners in a matrix-free implementation.
139 *
140 * Usage:
141 * - Use the constructors to define the block structure
142 * - Use SetDiagonalBlock to fill the BlockDiagonalPreconditioner
143 * - Use the method Mult and MultTranspose to apply the operator to a vector.
144 *
145 * If a block is not set, it is assumed to be an identity block.
146 *
147 */
149{
150public:
151 //! Constructor that specifies the block structure
153 //! Add a square block op in the block-entry (iblock, iblock).
154 /**
155 * iblock: The block will be inserted in location (iblock, iblock).
156 * op: the Operator to be inserted.
157 */
158 void SetDiagonalBlock(int iblock, Operator *op);
159 //! This method is present since required by the abstract base class Solver
160 virtual void SetOperator(const Operator &op) { }
161
162 //! Return the number of blocks
163 int NumBlocks() const { return nBlocks; }
164
165 //! Return a reference to block i,i.
167 { MFEM_VERIFY(ops[iblock], ""); return *ops[iblock]; }
168
169 //! Return a reference to block i,i (const version).
170 const Operator & GetDiagonalBlock(int iblock) const
171 { MFEM_VERIFY(ops[iblock], ""); return *ops[iblock]; }
172
173 //! Return the offsets for block starts
174 Array<int> & Offsets() { return offsets; }
175
176 //! Read only access to the offsets for block starts
177 const Array<int> & Offsets() const { return offsets; }
178
179 /// Operator application
180 virtual void Mult (const Vector & x, Vector & y) const;
181
182 /// Action of the transpose operator
183 virtual void MultTranspose (const Vector & x, Vector & y) const;
184
186
187 //! Controls the ownership of the blocks: if nonzero,
188 //! BlockDiagonalPreconditioner will delete all blocks that are set
189 //! (non-NULL); the default value is zero.
191
192private:
193 //! Number of Blocks
194 int nBlocks;
195 //! Offsets for the starting position of each block
196 Array<int> offsets;
197 //! 1D array that stores each block of the operator.
199 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose
200 //! methods.
201 mutable BlockVector xblock;
202 mutable BlockVector yblock;
203};
204
205//! @class BlockLowerTriangularPreconditioner
206/**
207 * \brief A class to handle Block lower triangular preconditioners in a
208 * matrix-free implementation.
209 *
210 * Usage:
211 * - Use the constructors to define the block structure
212 * - Use SetBlock() to fill the BlockLowerTriangularOperator
213 * - Diagonal blocks of the preconditioner should approximate the inverses of
214 * the diagonal block of the matrix
215 * - Off-diagonal blocks of the preconditioner should match/approximate those of
216 * the original matrix
217 * - Use the method Mult() and MultTranspose() to apply the operator to a vector.
218 *
219 * If a diagonal block is not set, it is assumed to be an identity block, if an
220 * off-diagonal block is not set, it is assumed to be a zero block.
221 *
222 */
224{
225public:
226 //! Constructor for BlockLowerTriangularPreconditioner%s with the same
227 //! block-structure for rows and columns.
228 /**
229 * @param offsets Offsets that mark the start of each row/column block
230 * (size nBlocks+1).
231 *
232 * @note BlockLowerTriangularPreconditioner will not own/copy the data
233 * contained in @a offsets.
234 */
236
237 //! Add block op in the block-entry (iblock, iblock).
238 /**
239 * @param iblock The block will be inserted in location (iblock, iblock).
240 * @param op The Operator to be inserted.
241 */
242 void SetDiagonalBlock(int iblock, Operator *op);
243 //! Add a block opt in the block-entry (iblock, jblock).
244 /**
245 * @param iRow, iCol The block will be inserted in location (iRow, iCol).
246 * @param op The Operator to be inserted.
247 */
248 void SetBlock(int iRow, int iCol, Operator *op);
249 //! This method is present since required by the abstract base class Solver
250 virtual void SetOperator(const Operator &op) { }
251
252 //! Return the number of blocks
253 int NumBlocks() const { return nBlocks; }
254
255 //! Return a reference to block i,j.
256 Operator & GetBlock(int iblock, int jblock)
257 { MFEM_VERIFY(ops(iblock,jblock), ""); return *ops(iblock,jblock); }
258
259 //! Return the offsets for block starts
260 Array<int> & Offsets() { return offsets; }
261
262 /// Operator application
263 virtual void Mult (const Vector & x, Vector & y) const;
264
265 /// Action of the transpose operator
266 virtual void MultTranspose (const Vector & x, Vector & y) const;
267
269
270 //! Controls the ownership of the blocks: if nonzero,
271 //! BlockLowerTriangularPreconditioner will delete all blocks that are set
272 //! (non-NULL); the default value is zero.
274
275private:
276 //! Number of block rows/columns
277 int nBlocks;
278 //! Offsets for the starting position of each block
279 Array<int> offsets;
280 //! 2D array that stores each block of the operator.
282
283 //! Temporary Vectors used to efficiently apply the Mult and MultTranspose
284 //! methods.
285 mutable BlockVector xblock;
286 mutable BlockVector yblock;
287 mutable Vector tmp;
288 mutable Vector tmp2;
289};
290
291}
292
293#endif /* MFEM_BLOCKOPERATOR */
Dynamic 2D array using row-major layout.
Definition array.hpp:372
A class to handle Block diagonal preconditioners in a matrix-free implementation.
virtual void MultTranspose(const Vector &x, Vector &y) const
Action of the transpose operator.
const Operator & GetDiagonalBlock(int iblock) const
Return a reference to block i,i (const version).
const Array< int > & Offsets() const
Read only access to the offsets for block starts.
virtual void Mult(const Vector &x, Vector &y) const
Operator application.
Operator & GetDiagonalBlock(int iblock)
Return a reference to block i,i.
BlockDiagonalPreconditioner(const Array< int > &offsets)
Constructor that specifies the block structure.
virtual void SetOperator(const Operator &op)
This method is present since required by the abstract base class Solver.
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).
A class to handle Block lower triangular preconditioners in a matrix-free implementation.
virtual void MultTranspose(const Vector &x, Vector &y) const
Action of the transpose operator.
int NumBlocks() const
Return the number of blocks.
void SetDiagonalBlock(int iblock, Operator *op)
Add block op in the block-entry (iblock, iblock).
BlockLowerTriangularPreconditioner(const Array< int > &offsets)
Operator & GetBlock(int iblock, int jblock)
Return a reference to block i,j.
virtual void Mult(const Vector &x, Vector &y) const
Operator application.
void SetBlock(int iRow, int iCol, Operator *op)
Add a block opt in the block-entry (iblock, jblock).
virtual void SetOperator(const Operator &op)
This method is present since required by the abstract base class Solver.
Array< int > & Offsets()
Return the offsets for block starts.
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).
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.
BlockOperator & operator=(const BlockOperator &)=delete
Copy assignment is not supported.
int NumRowBlocks() const
Return the number of row blocks.
virtual void Mult(const Vector &x, Vector &y) const
Operator application.
int NumColBlocks() const
Return the number of column blocks.
Array< int > & ColOffsets()
Return the columns offsets for block starts.
virtual Type GetType() const
virtual void MultTranspose(const Vector &x, Vector &y) const
Action of the transpose operator.
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.
Abstract operator.
Definition operator.hpp:25
Type
Enumeration defining IDs for some classes derived from Operator.
Definition operator.hpp:284
@ MFEM_Block_Operator
ID for the base class BlockOperator.
Definition operator.hpp:299
Base class for solvers.
Definition operator.hpp:683
Vector data type.
Definition vector.hpp:80
float real_t
Definition config.hpp:43