Flow123d  release_3.0.0-904-gff06a57
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  return MatZeroEntries(matrix_);
95  }
96 
97  PetscErrorCode rhs_zero_entries() override
98  {
99  rhs_changed_ = true;
100  return VecSet(rhs_, 0);
101  }
102 
103  void start_allocation() override;
104 
105  void start_add_assembly() override;
106 
107  void start_insert_assembly() override;
108 
109  void mat_set_values( int nrow, int *rows, int ncol, int *cols, double *vals ) override;
110 
111  void rhs_set_values( int nrow, int *rows, double *vals ) override;
112 
113  void preallocate_values(int nrow,int *rows,int ncol,int *cols);
114 
115  void preallocate_matrix();
116 
117  void finish_assembly() override;
118 
119  void finish_assembly( MatAssemblyType assembly_type );
120 
121  void apply_constrains( double scalar = 1. ) override;
122 
123  void set_initial_guess_nonzero(bool set_nonzero = true);
124 
125  LinSys::SolveInfo solve() override;
126 
127  /**
128  * Returns information on absolute solver accuracy
129  */
130  inline double get_absolute_accuracy() override {
131  return a_tol_;
132  };
133 
134  void view( ) override;
135 
136  /**
137  * Sets specific parameters of LinSys_PETSC defined by user in input file and used to calculate
138  */
139  void set_from_input(const Input::Record in_rec) override;
140 
141  double get_solution_precision() override;
142 
143  double compute_residual() override;
144 
145  ~LinSys_PETSC( );
146 
147 private:
148  /// Registrar of class to factory
149  static const int registrar;
150 
151  // make a pointer to the data array out of a std::vector
152  template<typename T>
154  {
155  if ( array.size() ) return &(array[0]);
156  return PETSC_NULL;
157  }
158 
159  // PetscScalar to double casting functor
160  struct PetscScalar2Double_ : public std::unary_function< PetscScalar, double >
161  {
162  double operator()( PetscScalar arg )
163  {
164  return static_cast<double>( arg );
165  }
166  };
167 
168 protected:
169 
170  std::string params_; //!< command-line-like options for the PETSc solver
171 
172  bool init_guess_nonzero; //!< flag for starting from nonzero guess
173 
174  Mat matrix_; //!< Petsc matrix of the problem.
175  Vec rhs_; //!< PETSc vector constructed with vx array.
177 
178  double *v_rhs_; //!< local RHS array pointing to Vec rhs_
179 
180  Vec on_vec_; //!< Vectors for counting non-zero entries in diagonal block.
181  Vec off_vec_; //!< Vectors for counting non-zero entries in off-diagonal block.
182 
183 
184  double solution_precision_; // precision of KSP system solver
185 
186  KSP system;
187  KSPConvergedReason reason;
188 
189 
190 };
191 
192 #endif /* LA_LINSYS_PETSC_HH_ */
const Distribution * get_ds()
Definition: linsys_PETSC.hh:64
const Mat * get_matrix() override
Definition: linsys_PETSC.hh:69
double * v_rhs_
local RHS array pointing to Vec rhs_
double get_absolute_accuracy() override
Abstract linear system class.
Definition: balance.hh:35
Wrappers for linear systems based on MPIAIJ and MATIS format.
Vec rhs_
PETSc vector constructed with vx array.
double operator()(PetscScalar arg)
T * makePetscPointer_(std::vector< T > &array)
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
PetscErrorCode set_matrix(Mat &matrix, MatStructure str) override
Definition: linsys_PETSC.hh:79
PetscErrorCode mat_zero_entries() override
Definition: linsys_PETSC.hh:91
LinSys FactoryBaseType
Definition: linsys_PETSC.hh:47
Vec on_vec_
Vectors for counting non-zero entries in diagonal block.
PetscErrorCode set_rhs(Vec &rhs) override
Definition: linsys_PETSC.hh:85
std::string params_
command-line-like options for the PETSc solver
KSPConvergedReason reason
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3291
const Vec * get_rhs() override
Definition: linsys_PETSC.hh:74
Vec off_vec_
Vectors for counting non-zero entries in off-diagonal block.
Mat matrix_
Petsc matrix of the problem.
static const int registrar
Registrar of class to factory.
Record type proxy class.
Definition: type_record.hh:182
PetscErrorCode rhs_zero_entries() override
Definition: linsys_PETSC.hh:97
bool init_guess_nonzero
flag for starting from nonzero guess
double solution_precision_