Flow123d  release_3.0.0-973-g92f55e826
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  */
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
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
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::ds_
Distribution * ds_
Definition: schur.hh:161
SchurComplement::RHS2
Vec RHS2
Definition: schur.hh:153
SchurComplement::set_complement
void set_complement(LinSys_PETSC *ls)
Set complement LinSys object.
Definition: schur.cc:260
LinSys
Definition: la_linsys_new.hh:169
SchurComplement::get_distribution
Distribution * get_distribution() const
Definition: schur.hh:95
Input
Abstract linear system class.
Definition: balance.hh:37
LinSys::SolveInfo
Definition: linsys.hh:105
solved
@ solved
Definition: schur.hh:61
SchurState
SchurState
Schur complement class for a PETSC based linear system.
Definition: schur.hh:58
SchurComplement::solve
LinSys::SolveInfo solve() override
Definition: schur.cc:352
LinSys_PETSC
Definition: linsys_PETSC.hh:43
created
@ created
Definition: schur.hh:59
SchurComplement::make_complement_distribution
Distribution * make_complement_distribution()
get distribution of complement object if complement is defined
Definition: schur.cc:267
linsys_PETSC.hh
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
SchurComplement::Compl
LinSys_PETSC * Compl
Definition: schur.hh:159
SchurComplement
Definition: schur.hh:64
SchurComplement::set_from_input
void set_from_input(const Input::Record in_rec) override
Definition: schur.cc:113
SchurComplement::compute_residual
double compute_residual() override
Definition: schur.cc:390
Distribution
Definition: distribution.hh:50
LinSys::SchurComplement
friend class SchurComplement
Definition: linsys.hh:92
SchurComplement::IsA
IS IsA
Definition: schur.hh:152
SchurComplement::xA
Mat xA
Definition: schur.hh:149
SchurComplement::RHS1
Vec RHS1
Definition: schur.hh:153
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
SchurComplement::C
Mat C
Definition: schur.hh:148
SchurComplement::orig_lsize
int orig_lsize
Size of local vector part of original system.
Definition: schur.hh:157
formed
@ formed
Definition: schur.hh:60
SchurComplement::get_system
LinSys * get_system() const
Definition: schur.hh:90
SchurComplement::IA
Mat IA
Definition: schur.hh:145
SchurComplement::loc_size_A
int loc_size_A
Definition: schur.hh:151
SchurComplement::IsB
IS IsB
Definition: schur.hh:152
SchurComplement::set_tolerances
void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override
Definition: schur.cc:254
SchurComplement::Sol2
Vec Sol2
Definition: schur.hh:154
SchurComplement::~SchurComplement
~SchurComplement()
Definition: schur.cc:402
SchurComplement::loc_size_B
int loc_size_B
Definition: schur.hh:151
SchurComplement::form_schur
void form_schur()
Definition: schur.cc:141
SchurComplement::form_rhs
void form_rhs()
Definition: schur.cc:236
SchurComplement::IAB
Mat IAB
Definition: schur.hh:150
SchurComplement::state
SchurState state
Definition: schur.hh:156
SchurComplement
SchurComplement SchurComplement
Definition: linsys.hh:92
SchurComplement::get_solution_precision
double get_solution_precision() override
get precision of solving
Definition: schur.cc:343
SchurComplement::B
Mat B
Definition: schur.hh:147
SchurComplement::resolve
void resolve()
Definition: schur.cc:378
SchurComplement::Sol1
Vec Sol1
Definition: schur.hh:154
SchurComplement::create_inversion_matrix
void create_inversion_matrix()
create IA matrix
Definition: schur.cc:274
SchurComplement::Bt
Mat Bt
Definition: schur.hh:147