Flow123d  release_3.0.0-922-g9fcbba1
schur.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file schur.hh
15  * @brief Assembly explicit Schur complement for the given linear system.
16  * Provides method for resolution of the full original vector of unknowns.
17  */
18 
19 #ifndef LA_SCHUR_HH_
20 #define LA_SCHUR_HH_
21 
22 #include <petscmat.h> // for Mat, _p_Mat
23 #include "la/linsys_PETSC.hh" // for LinSys_PETSC
24 #include "petscistypes.h" // for IS, _p_IS
25 #include "petscvec.h" // for Vec, _p_Vec
26 
27 class Distribution;
28 class LinSys;
29 
30 namespace Input { class Record; }
31 
32 
33 /**
34  * @brief Schur complement class for a PETSC based linear system
35  *
36  * Linear system consists of Matrix, inversion matrix, RHS, solution and pointer to Complement.
37  * It provides methods for:
38  * - set complement system
39  * - create distribution of complement system
40  * - create inversion matrix of system
41  * - form Schur complement and rhs
42  * - solve and resolve system
43  *
44  * Usage example:
45  * @CODE
46  * SchurComplement * schur = new SchurComplement(is, &distr);
47  *
48  * ... // allocation and assembly of matrix
49  *
50  * LinSys_PETSC * ls = new LinSys_PETSC( schur->make_complement_distribution() );
51  * schur->set_complement( ls );
52  * schur->solve();
53  * @ENDCODE
54  *
55  * Input record is passed to the complement system.
56  */
57 
58 typedef enum SchurState {
59  created, // need creation of all temporal matrixes ...
60  formed, // Schur complement ready to solve
61  solved // Resolved original Schur system
62 } SchurState;
63 
64 typedef class SchurComplement : public LinSys_PETSC {
65 public:
66  /**
67  * Constructor
68  *
69  * Gets linear system with original matrix A and creates its inversion (IA matrix)
70  *
71  * In current implementation the index set IsA has to be continuous sequence at the beginning of the local block of indices.
72  */
73  SchurComplement(IS ia, Distribution *ds);
74 
75  /**
76  * Copy constructor.
77  */
79 
80  void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override;
81 
82  /**
83  * Sets specific parameters defined by user in input file and used to calculate. Call set_from_input of complement
84  */
85  void set_from_input(const Input::Record in_rec) override;
86 
87  /**
88  * Returns pointer to LinSys object representing the schur complement.
89  */
90  LinSys *get_system() const {return (Compl);}
91 
92  /**
93  * Returns distribution of the original system (solved by class SchurComplement).
94  */
95  Distribution *get_distribution() const {return (ds_);}
96 
97  /**
98  * Destructor. In particular it also delete complement linear system if it was passed in
99  * through the @p set_complement() method.
100  */
101  ~SchurComplement();
102 
103  /** Compute only right hand side.
104  * This is useful when you change only rhs of the original system.
105  * TODO: We should ask original system if the matrix has changed (using LazyDependency) and
106  * possibly call only form_rhs, then this can be protected
107  */
108  void form_rhs();
109  /// Set complement LinSys object.
110  void set_complement(LinSys_PETSC *ls);
111  /// get distribution of complement object if complement is defined
112  Distribution *make_complement_distribution();
113  /// get precision of solving
114  double get_solution_precision() override;
115 
116  /**
117  * Solve the system.
118  *
119  * - If matrix and/or rhs is changed the Schur complement is formed.
120  * - The inner linear solver is called for the Schur complement
121  * - Resolve is called to reconstruct eliminated part of the solution vector.
122  */
123  LinSys::SolveInfo solve() override;
124 
125  /**
126  * Only resolve the system with current solution vector. This is necessary for nonlinear solvers.
127  * - If matrix and/or rhs is changed the Schur complement is formed.
128  * - Resolve is called to reconstruct eliminated part of the solution vector.
129  */
130  void resolve();
131 
132  /**
133  * The solve or resolve must be called prior to computing the residual.
134  */
135  double compute_residual() override;
136 
137 protected:
138  /// create IA matrix
139  void create_inversion_matrix();
140 
141  void form_schur();
142 
143 
144 
145  Mat IA; // Inverse of block A
146 
147  Mat B, Bt; // B and B' block (could be different from real B transpose)
148  Mat C; // Sub matrix.
149  Mat xA; // Bt*IA*B
150  Mat IAB; // reconstruction matrix IA * B
151  int loc_size_A, loc_size_B; // loc size of the A and B block
152  IS IsA, IsB; // parallel index sets of the A and B block
153  Vec RHS1, RHS2; // A and B - part of the RHS
154  Vec Sol1, Sol2; // A and B part of solution
155 
156  SchurState state; // object internal state
157  int orig_lsize; ///< Size of local vector part of original system
158 
159  LinSys_PETSC *Compl; // Schur complement system: (C - B' IA B) * Sol2 = (B' * IA * RHS1 - RHS2)
160 
161  Distribution *ds_; // Distribution of B block
163 
164 #endif /* LA_SCHUR_HH_ */
SchurComplement SchurComplement
Definition: linsys.hh:92
int orig_lsize
Size of local vector part of original system.
Definition: schur.hh:157
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
Abstract linear system class.
Definition: balance.hh:37
Definition: schur.hh:59
Distribution * get_distribution() const
Definition: schur.hh:95
LinSys_PETSC * Compl
Definition: schur.hh:159
int loc_size_B
Definition: schur.hh:151
Distribution * ds_
Definition: schur.hh:161
SchurState state
Definition: schur.hh:156
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
SchurState
Schur complement class for a PETSC based linear system.
Definition: schur.hh:58
Definition: schur.hh:60
Definition: schur.hh:61
LinSys * get_system() const
Definition: schur.hh:90