Flow123d  release_2.1.0-84-g6a13a75
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 <la/distribution.hh>
23 #include "la/linsys_PETSC.hh"
24 #include <petscmat.h>
25 
26 struct Solver;
27 class LinSys;
28 
29 /**
30  * @brief Schur complement class for a PETSC based linear system
31  *
32  * Linear system consists of Matrix, inversion matrix, RHS, solution and pointer to Complement.
33  * It provides methods for:
34  * - set complement system
35  * - create distribution of complement system
36  * - create inversion matrix of system
37  * - form Schur complement and rhs
38  * - solve and resolve system
39  *
40  * Usage example:
41  * @CODE
42  * SchurComplement * schur = new SchurComplement(is, &distr);
43  *
44  * ... // allocation and assembly of matrix
45  *
46  * LinSys_PETSC * ls = new LinSys_PETSC( schur->make_complement_distribution() );
47  * schur->set_complement( ls );
48  * schur->solve();
49  * @ENDCODE
50  *
51  * Input record is passed to the complement system.
52  */
53 
54 typedef enum SchurState {
55  created, // need creation of all temporal matrixes ...
56  formed, // Schur complement ready to solve
57  solved // Resolved original Schur system
58 } SchurState;
59 
60 typedef class SchurComplement : public LinSys_PETSC {
61 public:
62  /**
63  * Constructor
64  *
65  * Gets linear system with original matrix A and creates its inversion (IA matrix)
66  *
67  * In current implementation the index set IsA has to be continuous sequence at the beginning of the local block of indices.
68  */
69  SchurComplement(IS ia, Distribution *ds);
70 
71  /**
72  * Copy constructor.
73  */
75 
76  void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override;
77 
78  /**
79  * Sets specific parameters defined by user in input file and used to calculate. Call set_from_input of complement
80  */
81  void set_from_input(const Input::Record in_rec) override;
82 
83  /**
84  * Returns pointer to LinSys object representing the schur complement.
85  */
86  LinSys *get_system() const {return (Compl);}
87 
88  /**
89  * Returns distribution of the original system (solved by class SchurComplement).
90  */
91  Distribution *get_distribution() const {return (ds_);}
92 
93  /**
94  * Destructor. In particular it also delete complement linear system if it was passed in
95  * through the @p set_complement() method.
96  */
98 
99  /** Compute only right hand side.
100  * This is useful when you change only rhs of the original system.
101  * TODO: We should ask original system if the matrix has changed (using LazyDependency) and
102  * possibly call only form_rhs, then this can be protected
103  */
104  void form_rhs();
105  /// Set complement LinSys object.
106  void set_complement(LinSys_PETSC *ls);
107  /// get distribution of complement object if complement is defined
109  /// get precision of solving
110  double get_solution_precision() override;
111 
112  /**
113  * Solve the system.
114  *
115  * - If matrix and/or rhs is changed the Schur complement is formed.
116  * - The inner linear solver is called for the Schur complement
117  * - Resolve is called to reconstruct eliminated part of the solution vector.
118  */
119  int solve() override;
120 
121  /**
122  * Only resolve the system with current solution vector. This is necessary for nonlinear solvers.
123  * - If matrix and/or rhs is changed the Schur complement is formed.
124  * - Resolve is called to reconstruct eliminated part of the solution vector.
125  */
126  void resolve();
127 
128  /**
129  * The solve or resolve must be called prior to computing the residual.
130  */
131  double compute_residual() override;
132 
133 protected:
134  /// create IA matrix
136 
137  void form_schur();
138 
139 
140 
141  Mat IA; // Inverse of block A
142 
143  Mat B, Bt; // B and B' block (could be different from real B transpose)
144  Mat xA; // Bt*IA*B
145  Mat IAB; // reconstruction matrix IA * B
146  int loc_size_A, loc_size_B; // loc size of the A and B block
147  IS IsA, IsB; // parallel index sets of the A and B block
148  Vec RHS1, RHS2; // A and B - part of the RHS
149  Vec Sol1, Sol2; // A and B part of solution
150 
151  SchurState state; // object internal state
152  int orig_lsize; ///< Size of local vector part of original system
153 
154  LinSys_PETSC *Compl; // Schur complement system: (C - B' IA B) * Sol2 = (B' * IA * RHS1 - RHS2)
155 
156  Distribution *ds_; // Distribution of B block
158 
159 #endif /* LA_SCHUR_HH_ */
void set_complement(LinSys_PETSC *ls)
Set complement LinSys object.
Definition: schur.cc:238
int orig_lsize
Size of local vector part of original system.
Definition: schur.hh:152
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
void resolve()
Definition: schur.cc:345
void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override
Definition: schur.cc:232
Definition: schur.hh:55
Distribution * get_distribution() const
Definition: schur.hh:91
LinSys_PETSC * Compl
Definition: schur.hh:154
int loc_size_B
Definition: schur.hh:146
Distribution * ds_
Definition: schur.hh:156
~SchurComplement()
Definition: schur.cc:369
void form_rhs()
Definition: schur.cc:214
SchurState state
Definition: schur.hh:151
Accessor to the data with type Type::Record.
Definition: accessors.hh:286
void set_from_input(const Input::Record in_rec) override
Definition: schur.cc:110
double compute_residual() override
Definition: schur.cc:357
void form_schur()
Definition: schur.cc:138
int loc_size_A
Definition: schur.hh:146
int solve() override
Definition: schur.cc:329
SchurState
Schur complement class for a PETSC based linear system.
Definition: schur.hh:54
SchurComplement(IS ia, Distribution *ds)
Definition: schur.cc:61
void create_inversion_matrix()
create IA matrix
Definition: schur.cc:251
Support classes for parallel programing.
double get_solution_precision() override
get precision of solving
Definition: schur.cc:320
Distribution * make_complement_distribution()
get distribution of complement object if complement is defined
Definition: schur.cc:245
Definition: schur.hh:56
Abstract linear system class.
Definition: schur.hh:57
LinSys * get_system() const
Definition: schur.hh:86