Flow123d  JB_transport-112d700
linsys_PETSC.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 linsys_PETSC.hh
15  * @brief Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construction
16  * @author Jakub Sistek
17  */
18 
19 #ifndef LA_LINSYS_PETSC_HH_
20 #define LA_LINSYS_PETSC_HH_
21 
22 #include <functional> // for unary_function
23 #include <string> // for string
24 #include <vector> // for vector
25 #include "la/linsys.hh" // for LinSys
26 #include "petscksp.h" // for KSP, KSPConvergedReason, _p_KSP
27 #include "petscmat.h" // for Mat, MatCopy, MatZeroEntries, MatAssemblyType
28 #include "petscmath.h" // for PetscScalar
29 #include "petscsys.h" // for PetscErrorCode, PETSC_NULL
30 #include "petscvec.h" // for Vec, _p_Vec, VecCopy, VecSet
31 
32 class Distribution;
33 class DOFHandlerMultiDim;
34 namespace Input {
35  class Record;
36  namespace Type {
37  class Record;
38  }
39 }
40 namespace la {
41  class BddcmlWrapper;
42 }
43 
44 class LinSys_PETSC : public LinSys
45 {
46 
47 public:
49 
50  static const Input::Type::Record & get_input_type();
51 
52  LinSys_PETSC(const Distribution * rows_ds, const std::string &params = "");
53 
54  /**
55  * Copy constructor.
56  */
57  LinSys_PETSC( LinSys_PETSC &other );
58 
59 
60  void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override;
61 
62  /**
63  * Returns whole Distribution class for distribution of the solution.
64  */
65  inline const Distribution* get_ds( )
66  {
67  return rows_ds_;
68  }
69 
70  const Mat *get_matrix() override
71  {
72  return &matrix_;
73  }
74 
75  const Vec *get_rhs() override
76  {
77  return &rhs_;
78  }
79 
80  PetscErrorCode set_matrix(Mat &matrix, MatStructure str) override
81  {
82  matrix_changed_ = true;
83  return MatCopy(matrix, matrix_, str);
84  }
85 
86  PetscErrorCode set_rhs(Vec &rhs) override
87  {
88  rhs_changed_ = true;
89  return VecCopy(rhs, rhs_);
90  }
91 
92  PetscErrorCode mat_zero_entries() override
93  {
94  matrix_changed_ = true;
95  constraints_.clear();
96  return MatZeroEntries(matrix_);
97  }
98 
99  PetscErrorCode rhs_zero_entries() override
100  {
101  rhs_changed_ = true;
102  return VecSet(rhs_, 0);
103  }
104 
105  void start_allocation() override;
106 
107  void start_add_assembly() override;
108 
109  void start_insert_assembly() override;
110 
111  void mat_set_values( int nrow, int *rows, int ncol, int *cols, double *vals ) override;
112 
113  void rhs_set_values( int nrow, int *rows, double *vals ) override;
114 
115  void preallocate_values(int nrow,int *rows,int ncol,int *cols);
116 
117  void preallocate_matrix();
118 
120 
121  void finish_assembly() override;
122 
123  void finish_assembly( MatAssemblyType assembly_type );
124 
125  void apply_constrains( double scalar = 1. ) override;
126 
127  void set_initial_guess_nonzero(bool set_nonzero = true);
128 
129  LinSys::SolveInfo solve() override;
130 
131  /**
132  * Returns information on absolute solver accuracy
133  */
134  inline double get_absolute_accuracy() override {
135  return a_tol_;
136  };
137 
138  void view(string text="") override;
139 
140  /**
141  * Sets specific parameters of LinSys_PETSC defined by user in input file and used to calculate
142  */
143  void set_from_input(const Input::Record in_rec) override;
144 
145  double get_solution_precision() override;
146 
147  double compute_residual() override;
148 
149  ~LinSys_PETSC( );
150 
151 private:
152  /// Registrar of class to factory
153  static const int registrar;
154 
155  // make a pointer to the data array out of a std::vector
156  template<typename T>
158  {
159  if ( array.size() ) return &(array[0]);
160  return PETSC_NULL;
161  }
162 
163  // PetscScalar to double casting functor
164  struct PetscScalar2Double_ : public std::unary_function< PetscScalar, double >
165  {
166  double operator()( PetscScalar arg )
167  {
168  return static_cast<double>( arg );
169  }
170  };
171 
172 protected:
173 
174  std::string params_; //!< command-line-like options for the PETSc solver
175 
176  bool init_guess_nonzero; //!< flag for starting from nonzero guess
177 
178  Mat matrix_; //!< Petsc matrix of the problem.
179  Vec rhs_; //!< PETSc vector constructed with vx array.
181 
182  double *v_rhs_; //!< local RHS array pointing to Vec rhs_
183 
184  Vec on_vec_; //!< Vectors for counting non-zero entries in diagonal block.
185  Vec off_vec_; //!< Vectors for counting non-zero entries in off-diagonal block.
186 
187 
188  double solution_precision_; // precision of KSP system solver
189 
190  KSP system;
191  KSPConvergedReason reason;
192 
193 
194 };
195 
196 #endif /* LA_LINSYS_PETSC_HH_ */
LinSys_PETSC::get_absolute_accuracy
double get_absolute_accuracy() override
Definition: linsys_PETSC.hh:134
LinSys_PETSC::system
KSP system
Definition: linsys_PETSC.hh:190
LinSys_PETSC::residual_
Vec residual_
Definition: linsys_PETSC.hh:180
LinSys
Definition: la_linsys_new.hh:169
LinSys_PETSC::start_insert_assembly
void start_insert_assembly() override
Definition: linsys_PETSC.cc:135
LinSys_PETSC::set_initial_guess_nonzero
void set_initial_guess_nonzero(bool set_nonzero=true)
Definition: linsys_PETSC.cc:403
Input
Abstract linear system class.
Definition: balance.hh:40
LinSys_PETSC::PetscScalar2Double_::operator()
double operator()(PetscScalar arg)
Definition: linsys_PETSC.hh:166
LinSys::SolveInfo
Definition: linsys.hh:104
LinSys_PETSC::params_
std::string params_
command-line-like options for the PETSc solver
Definition: linsys_PETSC.hh:174
LinSys_PETSC::set_rhs
PetscErrorCode set_rhs(Vec &rhs) override
Definition: linsys_PETSC.hh:86
LinSys_PETSC::init_guess_nonzero
bool init_guess_nonzero
flag for starting from nonzero guess
Definition: linsys_PETSC.hh:176
LinSys_PETSC::rhs_zero_entries
PetscErrorCode rhs_zero_entries() override
Definition: linsys_PETSC.hh:99
LinSys_PETSC::LinSys_PETSC
LinSys_PETSC(const Distribution *rows_ds, const std::string &params="")
Definition: linsys_PETSC.cc:63
LinSys_PETSC::apply_constrains
void apply_constrains(double scalar=1.) override
Definition: linsys_PETSC.cc:358
LinSys_PETSC::PetscScalar2Double_
Definition: linsys_PETSC.hh:164
LinSys_PETSC::reason
KSPConvergedReason reason
Definition: linsys_PETSC.hh:191
linsys.hh
Wrappers for linear systems based on MPIAIJ and MATIS format.
Armor::array
Array< double > array
Definition: armor.hh:890
LinSys_PETSC
Definition: linsys_PETSC.hh:44
std::vector
Definition: doxy_dummy_defs.hh:7
ConstraintType::cols
@ cols
LinSys_PETSC::solve
LinSys::SolveInfo solve() override
Definition: linsys_PETSC.cc:409
LinSys_PETSC::set_matrix
PetscErrorCode set_matrix(Mat &matrix, MatStructure str) override
Definition: linsys_PETSC.hh:80
LinSys_PETSC::FactoryBaseType
LinSys FactoryBaseType
Definition: linsys_PETSC.hh:48
LinSys_PETSC::set_tolerances
void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override
Definition: linsys_PETSC.cc:92
LinSys_PETSC::matrix_
Mat matrix_
Petsc matrix of the problem.
Definition: linsys_PETSC.hh:178
LinSys_PETSC::registrar
static const int registrar
Registrar of class to factory.
Definition: linsys_PETSC.hh:153
LinSys_PETSC::mat_set_values
void mat_set_values(int nrow, int *rows, int ncol, int *cols, double *vals) override
Definition: linsys_PETSC.cc:153
ConstraintType::rows
@ rows
Distribution
Definition: distribution.hh:50
LinSys_PETSC::get_rhs
const Vec * get_rhs() override
Definition: linsys_PETSC.hh:75
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
LinSys_PETSC::get_matrix
const Mat * get_matrix() override
Definition: linsys_PETSC.hh:70
LinSys::a_tol_
double a_tol_
absolute tolerance of linear solver
Definition: linsys.hh:664
LinSys_PETSC::v_rhs_
double * v_rhs_
local RHS array pointing to Vec rhs_
Definition: linsys_PETSC.hh:182
LinSys_PETSC::finish_assembly
void finish_assembly() override
Definition: linsys_PETSC.cc:328
LinSys_PETSC::solution_precision_
double solution_precision_
Definition: linsys_PETSC.hh:188
DOFHandlerMultiDim
Provides the numbering of the finite element degrees of freedom on the computational mesh.
Definition: dofhandler.hh:151
LinSys::rhs_changed_
bool rhs_changed_
true if the right hand side was changed since the last solve
Definition: linsys.hh:681
LinSys_PETSC::get_solution_precision
double get_solution_precision() override
Definition: linsys_PETSC.cc:554
LinSys_PETSC::rhs_set_values
void rhs_set_values(int nrow, int *rows, double *vals) override
Definition: linsys_PETSC.cc:170
LinSys_PETSC::mat_zero_entries
PetscErrorCode mat_zero_entries() override
Definition: linsys_PETSC.hh:92
LinSys_PETSC::on_vec_
Vec on_vec_
Vectors for counting non-zero entries in diagonal block.
Definition: linsys_PETSC.hh:184
LinSys_PETSC::preallocate_matrix
void preallocate_matrix()
Definition: linsys_PETSC.cc:205
LinSys_PETSC::get_ds
const Distribution * get_ds()
Definition: linsys_PETSC.hh:65
la
Definition: bddcml_wrapper.hh:41
Armor::Array::size
unsigned int size() const
Definition: armor.hh:728
LinSys::rows_ds_
const Distribution * rows_ds_
final distribution of rows of MH matrix
Definition: linsys.hh:673
LinSys_PETSC::preallocate_values
void preallocate_values(int nrow, int *rows, int ncol, int *cols)
Definition: linsys_PETSC.cc:187
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
fmt::arg
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3291
LinSys_PETSC::start_add_assembly
void start_add_assembly() override
Definition: linsys_PETSC.cc:117
LinSys_PETSC::set_from_input
void set_from_input(const Input::Record in_rec) override
Definition: linsys_PETSC.cc:542
LinSys::constraints_
ConstraintVec_ constraints_
Definition: linsys.hh:690
LinSys::matrix_changed_
bool matrix_changed_
true if the matrix was changed since the last solve
Definition: linsys.hh:680
LinSys_PETSC::makePetscPointer_
T * makePetscPointer_(std::vector< T > &array)
Definition: linsys_PETSC.hh:157
LinSys_PETSC::start_allocation
void start_allocation() override
Definition: linsys_PETSC.cc:108
LinSys_PETSC::off_vec_
Vec off_vec_
Vectors for counting non-zero entries in off-diagonal block.
Definition: linsys_PETSC.hh:185
LinSys_PETSC::view
void view(string text="") override
Definition: linsys_PETSC.cc:495
LinSys_PETSC::rhs_
Vec rhs_
PETSc vector constructed with vx array.
Definition: linsys_PETSC.hh:179
LinSys_PETSC::compute_residual
double compute_residual() override
Definition: linsys_PETSC.cc:560
LinSys_PETSC::~LinSys_PETSC
~LinSys_PETSC()
Definition: linsys_PETSC.cc:531
LinSys_PETSC::get_input_type
static const Input::Type::Record & get_input_type()
Definition: linsys_PETSC.cc:34