Flow123d  JS_before_hm-2198-g122e1f2e2
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 namespace Input {
34  class Record;
35  namespace Type {
36  class Record;
37  }
38 }
39 namespace la {
40  class BddcmlWrapper;
41 }
42 
43 class LinSys_PETSC : public LinSys
44 {
45 
46 public:
48 
49  static const Input::Type::Record & get_input_type();
50 
51  LinSys_PETSC(const Distribution * rows_ds, const std::string &params = "");
52 
53  /**
54  * Copy constructor.
55  */
56  LinSys_PETSC( LinSys_PETSC &other );
57 
58 
59  void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override;
60 
61  /**
62  * Returns whole Distribution class for distribution of the solution.
63  */
64  inline const Distribution* get_ds( )
65  {
66  return rows_ds_;
67  }
68 
69  const Mat *get_matrix() override
70  {
71  return &matrix_;
72  }
73 
74  const Vec *get_rhs() override
75  {
76  return &rhs_;
77  }
78 
79  PetscErrorCode set_matrix(Mat &matrix, MatStructure str) override
80  {
81  matrix_changed_ = true;
82  return MatCopy(matrix, matrix_, str);
83  }
84 
85  PetscErrorCode set_rhs(Vec &rhs) override
86  {
87  rhs_changed_ = true;
88  return VecCopy(rhs, rhs_);
89  }
90 
91  PetscErrorCode mat_zero_entries() override
92  {
93  matrix_changed_ = true;
94  constraints_.clear();
95  return MatZeroEntries(matrix_);
96  }
97 
98  PetscErrorCode rhs_zero_entries() override
99  {
100  rhs_changed_ = true;
101  return VecSet(rhs_, 0);
102  }
103 
104  void start_allocation() override;
105 
106  void start_add_assembly() override;
107 
108  void start_insert_assembly() override;
109 
110  void mat_set_values( int nrow, int *rows, int ncol, int *cols, double *vals ) override;
111 
112  void rhs_set_values( int nrow, int *rows, double *vals ) override;
113 
114  void preallocate_values(int nrow,int *rows,int ncol,int *cols);
115 
116  void preallocate_matrix();
117 
118  void finish_assembly() override;
119 
120  void finish_assembly( MatAssemblyType assembly_type );
121 
122  void apply_constrains( double scalar = 1. ) override;
123 
124  void set_initial_guess_nonzero(bool set_nonzero = true);
125 
126  LinSys::SolveInfo solve() override;
127 
128  /**
129  * Returns information on absolute solver accuracy
130  */
131  inline double get_absolute_accuracy() override {
132  return a_tol_;
133  };
134 
135  void view(string text="") override;
136 
137  /**
138  * Sets specific parameters of LinSys_PETSC defined by user in input file and used to calculate
139  */
140  void set_from_input(const Input::Record in_rec) override;
141 
142  double get_solution_precision() override;
143 
144  double compute_residual() override;
145 
146  ~LinSys_PETSC( );
147 
148 private:
149  /// Registrar of class to factory
150  static const int registrar;
151 
152  // make a pointer to the data array out of a std::vector
153  template<typename T>
155  {
156  if ( array.size() ) return &(array[0]);
157  return PETSC_NULL;
158  }
159 
160  // PetscScalar to double casting functor
161  struct PetscScalar2Double_ : public std::unary_function< PetscScalar, double >
162  {
163  double operator()( PetscScalar arg )
164  {
165  return static_cast<double>( arg );
166  }
167  };
168 
169 protected:
170 
171  std::string params_; //!< command-line-like options for the PETSc solver
172 
173  bool init_guess_nonzero; //!< flag for starting from nonzero guess
174 
175  Mat matrix_; //!< Petsc matrix of the problem.
176  Vec rhs_; //!< PETSc vector constructed with vx array.
178 
179  double *v_rhs_; //!< local RHS array pointing to Vec rhs_
180 
181  Vec on_vec_; //!< Vectors for counting non-zero entries in diagonal block.
182  Vec off_vec_; //!< Vectors for counting non-zero entries in off-diagonal block.
183 
184 
185  double solution_precision_; // precision of KSP system solver
186 
187  KSP system;
188  KSPConvergedReason reason;
189 
190 
191 };
192 
193 #endif /* LA_LINSYS_PETSC_HH_ */
LinSys_PETSC::get_absolute_accuracy
double get_absolute_accuracy() override
Definition: linsys_PETSC.hh:131
LinSys_PETSC::system
KSP system
Definition: linsys_PETSC.hh:187
LinSys_PETSC::residual_
Vec residual_
Definition: linsys_PETSC.hh:177
LinSys
Definition: la_linsys_new.hh:169
LinSys_PETSC::start_insert_assembly
void start_insert_assembly() override
Definition: linsys_PETSC.cc:133
LinSys_PETSC::set_initial_guess_nonzero
void set_initial_guess_nonzero(bool set_nonzero=true)
Definition: linsys_PETSC.cc:331
Input
Abstract linear system class.
Definition: balance.hh:40
LinSys_PETSC::PetscScalar2Double_::operator()
double operator()(PetscScalar arg)
Definition: linsys_PETSC.hh:163
LinSys::SolveInfo
Definition: linsys.hh:105
LinSys_PETSC::params_
std::string params_
command-line-like options for the PETSc solver
Definition: linsys_PETSC.hh:171
LinSys_PETSC::set_rhs
PetscErrorCode set_rhs(Vec &rhs) override
Definition: linsys_PETSC.hh:85
LinSys_PETSC::init_guess_nonzero
bool init_guess_nonzero
flag for starting from nonzero guess
Definition: linsys_PETSC.hh:173
LinSys_PETSC::rhs_zero_entries
PetscErrorCode rhs_zero_entries() override
Definition: linsys_PETSC.hh:98
LinSys_PETSC::LinSys_PETSC
LinSys_PETSC(const Distribution *rows_ds, const std::string &params="")
Definition: linsys_PETSC.cc:61
LinSys_PETSC::apply_constrains
void apply_constrains(double scalar=1.) override
Definition: linsys_PETSC.cc:286
LinSys_PETSC::PetscScalar2Double_
Definition: linsys_PETSC.hh:161
LinSys_PETSC::reason
KSPConvergedReason reason
Definition: linsys_PETSC.hh:188
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:43
std::vector
Definition: doxy_dummy_defs.hh:7
LinSys_PETSC::solve
LinSys::SolveInfo solve() override
Definition: linsys_PETSC.cc:337
LinSys_PETSC::set_matrix
PetscErrorCode set_matrix(Mat &matrix, MatStructure str) override
Definition: linsys_PETSC.hh:79
LinSys_PETSC::FactoryBaseType
LinSys FactoryBaseType
Definition: linsys_PETSC.hh:47
LinSys_PETSC::set_tolerances
void set_tolerances(double r_tol, double a_tol, unsigned int max_it) override
Definition: linsys_PETSC.cc:90
LinSys_PETSC::matrix_
Mat matrix_
Petsc matrix of the problem.
Definition: linsys_PETSC.hh:175
LinSys_PETSC::registrar
static const int registrar
Registrar of class to factory.
Definition: linsys_PETSC.hh:150
LinSys_PETSC::mat_set_values
void mat_set_values(int nrow, int *rows, int ncol, int *cols, double *vals) override
Definition: linsys_PETSC.cc:151
Distribution
Definition: distribution.hh:50
LinSys_PETSC::get_rhs
const Vec * get_rhs() override
Definition: linsys_PETSC.hh:74
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:69
LinSys::a_tol_
double a_tol_
absolute tolerance of linear solver
Definition: linsys.hh:665
LinSys_PETSC::v_rhs_
double * v_rhs_
local RHS array pointing to Vec rhs_
Definition: linsys_PETSC.hh:179
LinSys_PETSC::finish_assembly
void finish_assembly() override
Definition: linsys_PETSC.cc:256
LinSys_PETSC::solution_precision_
double solution_precision_
Definition: linsys_PETSC.hh:185
LinSys::rhs_changed_
bool rhs_changed_
true if the right hand side was changed since the last solve
Definition: linsys.hh:682
LinSys_PETSC::get_solution_precision
double get_solution_precision() override
Definition: linsys_PETSC.cc:482
LinSys_PETSC::rhs_set_values
void rhs_set_values(int nrow, int *rows, double *vals) override
Definition: linsys_PETSC.cc:168
LinSys_PETSC::mat_zero_entries
PetscErrorCode mat_zero_entries() override
Definition: linsys_PETSC.hh:91
LinSys_PETSC::on_vec_
Vec on_vec_
Vectors for counting non-zero entries in diagonal block.
Definition: linsys_PETSC.hh:181
LinSys_PETSC::preallocate_matrix
void preallocate_matrix()
Definition: linsys_PETSC.cc:203
LinSys_PETSC::get_ds
const Distribution * get_ds()
Definition: linsys_PETSC.hh:64
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:674
LinSys_PETSC::preallocate_values
void preallocate_values(int nrow, int *rows, int ncol, int *cols)
Definition: linsys_PETSC.cc:185
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:115
LinSys_PETSC::set_from_input
void set_from_input(const Input::Record in_rec) override
Definition: linsys_PETSC.cc:470
LinSys::constraints_
ConstraintVec_ constraints_
Definition: linsys.hh:691
LinSys::matrix_changed_
bool matrix_changed_
true if the matrix was changed since the last solve
Definition: linsys.hh:681
LinSys_PETSC::makePetscPointer_
T * makePetscPointer_(std::vector< T > &array)
Definition: linsys_PETSC.hh:154
LinSys_PETSC::start_allocation
void start_allocation() override
Definition: linsys_PETSC.cc:106
LinSys_PETSC::off_vec_
Vec off_vec_
Vectors for counting non-zero entries in off-diagonal block.
Definition: linsys_PETSC.hh:182
LinSys_PETSC::view
void view(string text="") override
Definition: linsys_PETSC.cc:423
LinSys_PETSC::rhs_
Vec rhs_
PETSc vector constructed with vx array.
Definition: linsys_PETSC.hh:176
LinSys_PETSC::compute_residual
double compute_residual() override
Definition: linsys_PETSC.cc:488
LinSys_PETSC::~LinSys_PETSC
~LinSys_PETSC()
Definition: linsys_PETSC.cc:459
LinSys_PETSC::get_input_type
static const Input::Type::Record & get_input_type()
Definition: linsys_PETSC.cc:32