Flow123d  last_with_con_2.0.0-4-g42e6930
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  /**
77  * Returns pointer to LinSys object representing the schur complement.
78  */
79  LinSys *get_system() const {return (Compl);}
80 
81  /**
82  * Returns distribution of the original system (solved by class SchurComplement).
83  */
84  Distribution *get_distribution() const {return (ds_);}
85 
86  /**
87  * Destructor. In particular it also delete complement linear system if it was passed in
88  * through the @p set_complement() method.
89  */
91 
92  /** Compute only right hand side.
93  * This is useful when you change only rhs of the original system.
94  * TODO: We should ask original system if the matrix has changed (using LazyDependency) and
95  * possibly call only form_rhs, then this can be protected
96  */
97  void form_rhs();
98  /// Set complement LinSys object.
99  void set_complement(LinSys_PETSC *ls);
100  /// get distribution of complement object if complement is defined
102  /// get precision of solving
103  double get_solution_precision() override;
104 
105  /**
106  * Solve the system.
107  *
108  * - If matrix and/or rhs is changed the Schur complement is formed.
109  * - The inner linear solver is called for the Schur complement
110  * - Resolve is called to reconstruct eliminated part of the solution vector.
111  */
112  int solve() override;
113 
114  /**
115  * Only resolve the system with current solution vector. This is necessary for nonlinear solvers.
116  * - If matrix and/or rhs is changed the Schur complement is formed.
117  * - Resolve is called to reconstruct eliminated part of the solution vector.
118  */
119  void resolve();
120 
121  /**
122  * The solve or resolve must be called prior to computing the residual.
123  */
124  double compute_residual() override;
125 
126 protected:
127  /// create IA matrix
129 
130  void form_schur();
131 
132 
133 
134  Mat IA; // Inverse of block A
135 
136  Mat B, Bt; // B and B' block (could be different from real B transpose)
137  Mat xA; // Bt*IA*B
138  Mat IAB; // reconstruction matrix IA * B
139  int loc_size_A, loc_size_B; // loc size of the A and B block
140  IS IsA, IsB; // parallel index sets of the A and B block
141  Vec RHS1, RHS2; // A and B - part of the RHS
142  Vec Sol1, Sol2; // A and B part of solution
143 
144  SchurState state; // object internal state
145  int orig_lsize; ///< Size of local vector part of original system
146 
147  LinSys_PETSC *Compl; // Schur complement system: (C - B' IA B) * Sol2 = (B' * IA * RHS1 - RHS2)
148 
149  Distribution *ds_; // Distribution of B block
151 
152 #endif /* LA_SCHUR_HH_ */
void set_complement(LinSys_PETSC *ls)
Set complement LinSys object.
Definition: schur.cc:223
int orig_lsize
Size of local vector part of original system.
Definition: schur.hh:145
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
void resolve()
Definition: schur.cc:333
Definition: schur.hh:55
Distribution * get_distribution() const
Definition: schur.hh:84
LinSys_PETSC * Compl
Definition: schur.hh:147
int loc_size_B
Definition: schur.hh:139
Distribution * ds_
Definition: schur.hh:149
~SchurComplement()
Definition: schur.cc:356
void form_rhs()
Definition: schur.cc:205
SchurState state
Definition: schur.hh:144
double compute_residual() override
Definition: schur.cc:345
void form_schur()
Definition: schur.cc:129
int loc_size_A
Definition: schur.hh:139
int solve() override
Definition: schur.cc:317
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:239
Support classes for parallel programing.
double get_solution_precision() override
get precision of solving
Definition: schur.cc:308
Distribution * make_complement_distribution()
get distribution of complement object if complement is defined
Definition: schur.cc:233
Definition: schur.hh:56
Abstract linear system class.
Definition: schur.hh:57
LinSys * get_system() const
Definition: schur.hh:79