MFEM  v3.3
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
blockoperator.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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 
20 namespace 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  */
34 class BlockOperator : public Operator
35 {
36 public:
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). Note: BlockOperator will not own/copy the data contained
42  * in offsets.
43  */
44  BlockOperator(const Array<int> & offsets);
45  //! Constructor for general BlockOperators.
46  /**
47  * row_offsets: offsets that mark the start of each row block (size
48  * nRowBlocks+1). col_offsets: offsets that mark the start of each column
49  * block (size nColBlocks+1). Note: BlockOperator will not own/copy the
50  * data contained in offsets.
51  */
52  BlockOperator(const Array<int> & row_offsets, const Array<int> & col_offsets);
53 
54  //! Add block op in the block-entry (iblock, iblock).
55  /**
56  * iblock: The block will be inserted in location (iblock, iblock).
57  * op: the Operator to be inserted.
58  */
59  void SetDiagonalBlock(int iblock, Operator *op);
60  //! Add a block op in the block-entry (iblock, jblock).
61  /**
62  * irow, icol: The block will be inserted in location (irow, icol).
63  * op: the Operator to be inserted.
64  */
65  void SetBlock(int iRow, int iCol, Operator *op);
66 
67  //! Return the number of row blocks
68  int NumRowBlocks() const { return nRowBlocks; }
69  //! Return the number of column blocks
70  int NumColBlocks() const { return nColBlocks; }
71 
72  //! Check if block (i,j) is a zero block
73  int IsZeroBlock(int i, int j) const { return (op(i,j)==NULL) ? 1 : 0; }
74  //! Return a reference to block i,j
75  Operator & GetBlock(int i, int j)
76  { MFEM_VERIFY(op(i,j), ""); return *op(i,j); }
77 
78  //! Return the row offsets for block starts
79  Array<int> & RowOffsets() { return row_offsets; }
80  //! Return the columns offsets for block starts
81  Array<int> & ColOffsets() { return col_offsets; }
82 
83  /// Operator application
84  virtual void Mult (const Vector & x, Vector & y) const;
85 
86  /// Action of the transpose operator
87  virtual void MultTranspose (const Vector & x, Vector & y) const;
88 
90 
91  //! Controls the ownership of the blocks: if nonzero, BlockOperator will
92  //! delete all blocks that are set (non-NULL); the default value is zero.
94 
95 private:
96  //! Number of block rows
97  int nRowBlocks;
98  //! Number of block columns
99  int nColBlocks;
100  //! Row offsets for the starting position of each block
101  Array<int> row_offsets;
102  //! Column offsets for the starting position of each block
103  Array<int> col_offsets;
104  //! 2D array that stores each block of the operator.
106 
107  //! Temporary Vectors used to efficiently apply the Mult and MultTranspose methods.
108  mutable BlockVector xblock;
109  mutable BlockVector yblock;
110  mutable Vector tmp;
111 };
112 
113 //! @class BlockDiagonalPreconditioner
114 /**
115  * \brief A class to handle Block diagonal preconditioners in a matrix-free implementation.
116  *
117  * Usage:
118  * - Use the constructors to define the block structure
119  * - Use SetDiagonalBlock to fill the BlockOperator
120  * - Use the method Mult and MultTranspose to apply the operator to a vector.
121  *
122  * If a block is not set, it is assumed it is an identity block
123  *
124  */
126 {
127 public:
128  //! Constructor that specifies the block structure
129  BlockDiagonalPreconditioner(const Array<int> & offsets);
130  //! Add a square block op in the block-entry (iblock, iblock).
131  /**
132  * iblock: The block will be inserted in location (iblock, iblock).
133  * op: the Operator to be inserted.
134  */
135  void SetDiagonalBlock(int iblock, Operator *op);
136  //! This method is present since required by the abstract base class Solver
137  virtual void SetOperator(const Operator &op) { }
138 
139  //! Return the number of blocks
140  int NumBlocks() const { return nBlocks; }
141 
142  //! Return a reference to block i,i.
144  { MFEM_VERIFY(op[iblock], ""); return *op[iblock]; }
145 
146  //! Return the offsets for block starts
147  Array<int> & Offsets() { return offsets; }
148 
149  /// Operator application
150  virtual void Mult (const Vector & x, Vector & y) const;
151 
152  /// Action of the transpose operator
153  virtual void MultTranspose (const Vector & x, Vector & y) const;
154 
156 
157  //! Controls the ownership of the blocks: if nonzero,
158  //! BlockDiagonalPreconditioner will delete all blocks that are set
159  //! (non-NULL); the default value is zero.
161 
162 private:
163  //! Number of Blocks
164  int nBlocks;
165  //! Offsets for the starting position of each block
166  Array<int> offsets;
167  //! 1D array that stores each block of the operator.
169  //! Temporary Vectors used to efficiently apply the Mult and MultTranspose
170  //! methods.
171  mutable BlockVector xblock;
172  mutable BlockVector yblock;
173 };
174 
175 }
176 
177 #endif /* MFEM_BLOCKOPERATOR */
Array< int > & RowOffsets()
Return the row offsets for block starts.
Array< int > & Offsets()
Return the offsets for block starts.
virtual void MultTranspose(const Vector &x, Vector &y) const
Action of the transpose operator.
int IsZeroBlock(int i, int j) const
Check if block (i,j) is a zero block.
void SetBlock(int iRow, int iCol, Operator *op)
Add a block op in the block-entry (iblock, jblock).
Operator & GetBlock(int i, int j)
Return a reference to block i,j.
int NumBlocks() const
Return the number of blocks.
Operator & GetDiagonalBlock(int iblock)
Return a reference to block i,i.
A class to handle Block diagonal preconditioners in a matrix-free implementation. ...
void SetDiagonalBlock(int iblock, Operator *op)
Add block op in the block-entry (iblock, iblock).
BlockDiagonalPreconditioner(const Array< int > &offsets)
Constructor that specifies the block structure.
BlockOperator(const Array< int > &offsets)
virtual void Mult(const Vector &x, Vector &y) const
Operator application.
virtual void SetOperator(const Operator &op)
This method is present since required by the abstract base class Solver.
virtual void MultTranspose(const Vector &x, Vector &y) const
Action of the transpose operator.
virtual void Mult(const Vector &x, Vector &y) const
Operator application.
int NumRowBlocks() const
Return the number of row blocks.
Vector data type.
Definition: vector.hpp:36
Base class for solvers.
Definition: operator.hpp:257
Abstract operator.
Definition: operator.hpp:21
A class to handle Block systems in a matrix-free implementation.
int NumColBlocks() const
Return the number of column blocks.
void SetDiagonalBlock(int iblock, Operator *op)
Add a square block op in the block-entry (iblock, iblock).
Array< int > & ColOffsets()
Return the columns offsets for block starts.