Flow123d  jenkins-Flow123d-linux-release-multijob-282
linsys_PETSC.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id: la_linsys.hh 1299 2011-08-23 21:42:50Z jakub.sistek $
21  * $Revision: 1299 $
22  * $LastChangedBy: jakub.sistek $
23  * $LastChangedDate: 2011-08-23 23:42:50 +0200 (Tue, 23 Aug 2011) $
24  *
25  * @file
26  * @brief Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construction
27  * @author Jakub Sistek
28  *
29  *
30  */
31 
32 #ifndef LA_LINSYS_PETSC_HH_
33 #define LA_LINSYS_PETSC_HH_
34 
35 // derived from base linsys
36 #include "la/linsys.hh"
37 
38 #include "la/distribution.hh"
39 #include "input/input_type.hh"
40 #include "input/accessors.hh"
41 
42 class LinSys_PETSC : public LinSys
43 {
44 
45 public:
47 
48  LinSys_PETSC(const Distribution * rows_ds);
49 
50  /**
51  * Copy constructor.
52  */
53  LinSys_PETSC( LinSys_PETSC &other );
54 
55  /**
56  * Returns whole Distribution class for distribution of the solution.
57  */
58  inline const Distribution* get_ds( )
59  {
60  return rows_ds_;
61  }
62 
63  const Mat *get_matrix()
64  {
65  return &matrix_;
66  }
67 
68  const Vec *get_rhs()
69  {
70  return &rhs_;
71  }
72 
73  PetscErrorCode set_matrix(Mat &matrix, MatStructure str)
74  {
75  matrix_changed_ = true;
76  return MatCopy(matrix, matrix_, str);
77  }
78 
79  PetscErrorCode set_rhs(Vec &rhs)
80  {
81  rhs_changed_ = true;
82  return VecCopy(rhs, rhs_);
83  }
84 
85  PetscErrorCode mat_zero_entries()
86  {
87  matrix_changed_ = true;
88  return MatZeroEntries(matrix_);
89  }
90 
91  PetscErrorCode rhs_zero_entries()
92  {
93  rhs_changed_ = true;
94  return VecSet(rhs_, 0);
95  }
96 
97  void start_allocation();
98 
99  void start_add_assembly();
100 
101  void start_insert_assembly();
102 
103  void mat_set_values( int nrow, int *rows, int ncol, int *cols, double *vals );
104 
105  void rhs_set_values( int nrow, int *rows, double *vals );
106 
107  void preallocate_values(int nrow,int *rows,int ncol,int *cols);
108 
109  void preallocate_matrix();
110 
111  void finish_assembly();
112 
113  void finish_assembly( MatAssemblyType assembly_type );
114 
115  void apply_constrains( double scalar = 1. );
116 
117  void set_initial_guess_nonzero(bool set_nonzero = true);
118 
119  int solve();
120 
121  /**
122  * Returns information on absolute solver accuracy
123  */
124  inline double get_absolute_accuracy(){
125  return a_tol_;
126  };
127 
128  void view( );
129 
130  /**
131  * Sets specific parameters of LinSys_PETSC defined by user in input file and used to calculate
132  */
133  void set_from_input(const Input::Record in_rec);
134 
135  double get_solution_precision();
136 
137  ~LinSys_PETSC( );
138 
139 private:
140  // make a pointer to the data array out of a std::vector
141  template<typename T>
143  {
144  if ( array.size() ) return &(array[0]);
145  return PETSC_NULL;
146  }
147 
148  // PetscScalar to double casting functor
149  struct PetscScalar2Double_ : public std::unary_function< PetscScalar, double >
150  {
151  double operator()( PetscScalar arg )
152  {
153  return static_cast<double>( arg );
154  }
155  };
156 
157 protected:
158 
159  std::string params_; //!< command-line-like options for the PETSc solver
160 
161  bool init_guess_nonzero; //!< flag for starting from nonzero guess
162 
163  Mat matrix_; //!< Petsc matrix of the problem.
164  Vec rhs_; //!< PETSc vector constructed with vx array.
165 
166  double *v_rhs_; //!< local RHS array pointing to Vec rhs_
167 
168  Vec on_vec_; //!< Vectors for counting non-zero entries in diagonal block.
169  Vec off_vec_; //!< Vectors for counting non-zero entries in off-diagonal block.
170 
171  double solution_precision_; // precision of KSP system solver
172 
173 
174 };
175 
176 #endif /* LA_LINSYS_PETSC_HH_ */
const Vec * get_rhs()
Definition: linsys_PETSC.hh:68
const Distribution * get_ds()
Definition: linsys_PETSC.hh:58
PetscErrorCode set_rhs(Vec &rhs)
Definition: linsys_PETSC.hh:79
void start_insert_assembly()
void apply_constrains(double scalar=1.)
PetscErrorCode rhs_zero_entries()
Definition: linsys_PETSC.hh:91
bool matrix_changed_
true if the matrix was changed since the last solve
Definition: linsys.hh:588
double * v_rhs_
local RHS array pointing to Vec rhs_
Wrappers for linear systems based on MPIAIJ and MATIS format.
LinSys_PETSC(const Distribution *rows_ds)
Definition: linsys_PETSC.cc:50
const Mat * get_matrix()
Definition: linsys_PETSC.hh:63
PetscErrorCode set_matrix(Mat &matrix, MatStructure str)
Definition: linsys_PETSC.hh:73
Vec rhs_
PETSc vector constructed with vx array.
static Input::Type::Record input_type
Definition: linsys_PETSC.hh:46
void set_from_input(const Input::Record in_rec)
double operator()(PetscScalar arg)
void preallocate_values(int nrow, int *rows, int ncol, int *cols)
void rhs_set_values(int nrow, int *rows, double *vals)
bool rhs_changed_
true if the right hand side was changed since the last solve
Definition: linsys.hh:589
PetscErrorCode mat_zero_entries()
Definition: linsys_PETSC.hh:85
T * makePetscPointer_(std::vector< T > &array)
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
const Distribution * rows_ds_
final distribution of rows of MH matrix
Definition: linsys.hh:581
double a_tol_
Definition: linsys.hh:572
void finish_assembly()
Vec on_vec_
Vectors for counting non-zero entries in diagonal block.
void preallocate_matrix()
void start_allocation()
Definition: linsys_PETSC.cc:78
double get_absolute_accuracy()
std::string params_
command-line-like options for the PETSc solver
void mat_set_values(int nrow, int *rows, int ncol, int *cols, double *vals)
void start_add_assembly()
Definition: linsys_PETSC.cc:87
Support classes for parallel programing.
Vec off_vec_
Vectors for counting non-zero entries in off-diagonal block.
double get_solution_precision()
Abstract linear system class.
void set_initial_guess_nonzero(bool set_nonzero=true)
Mat matrix_
Petsc matrix of the problem.
Record type proxy class.
Definition: type_record.hh:169
bool init_guess_nonzero
flag for starting from nonzero guess
double solution_precision_