Flow123d  JB_transport-112d700
linsys_PETSC.cc
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.cc
15  * @brief Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construction
16  * @author Jakub Sistek
17  */
18 
19 // derived from base linsys
20 #include "la/linsys_PETSC.hh"
21 #include "petscvec.h"
22 #include "petscksp.h"
23 #include "petscmat.h"
24 #include "system/sys_profiler.hh"
25 #include "system/system.hh"
26 #include "fem/dofhandler.hh"
27 #include "fem/dh_cell_accessor.hh"
28 
29 
30 //#include <boost/bind.hpp>
31 
32 namespace it = Input::Type;
33 
35  return it::Record("Petsc", "PETSc solver settings.\n It provides interface to various PETSc solvers. The convergence criteria is:\n"
36  "```\n"
37  "norm( res_i ) < max( norm( res_0 ) * r_tol, a_tol )\n"
38  "```\n"
39  "where ```res_i``` is the residuum vector after i-th iteration of the solver and ```res_0``` is the estimate of the norm of the initial residual. "
40  "If the initial guess of the solution is provided (usually only for transient equations) the residual of this estimate is used, "
41  "otherwise the norm of preconditioned RHS is used. "
42  "The default norm is (($L_2$)) norm of preconditioned residual: (($ P^{-1}(Ax-b)$)), usage of other norm may be prescribed using the 'option' key. "
43  "See also PETSc documentation for KSPSetNormType.")
45  .declare_key("r_tol", it::Double(0.0, 1.0), it::Default::read_time("Default value is set by the nonlinear solver or the equation. "
46  "If not, we use the value 1.0e-7."),
47  "Residual tolerance relative to the initial error.")
48  .declare_key("a_tol", it::Double(0.0), it::Default::read_time("Default value is set by the nonlinear solver or the equation. "
49  "If not, we use the value 1.0e-11."),
50  "Absolute residual tolerance.")
51  .declare_key("max_it", it::Integer(0), it::Default::read_time("Default value is set by the nonlinear solver or the equation. "
52  "If not, we use the value 1000."),
53  "Maximum number of outer iterations of the linear solver.")
54  .declare_key("options", it::String(), it::Default("\"\""), "This options is passed to PETSC to create a particular KSP (Krylov space method).\n"
55  "If the string is left empty (by default), the internal default options is used.")
56  .close();
57 }
58 
59 
61 
62 
63 LinSys_PETSC::LinSys_PETSC( const Distribution * rows_ds, const std::string &params)
64  : LinSys( rows_ds ),
65  params_(params),
66  init_guess_nonzero(false),
67  matrix_(0)
68 {
69  // create PETSC vectors:
70  PetscErrorCode ierr;
71  // rhs
72  v_rhs_= new double[ rows_ds_->lsize() + 1 ];
73  ierr = VecCreateMPIWithArray( comm_, 1, rows_ds_->lsize(), PETSC_DECIDE, v_rhs_, &rhs_ ); CHKERRV( ierr );
74  ierr = VecZeroEntries( rhs_ ); CHKERRV( ierr );
75  VecDuplicate(rhs_, &residual_);
76 
77  matrix_ = NULL;
78  solution_precision_ = std::numeric_limits<double>::infinity();
79  matrix_changed_ = true;
80  rhs_changed_ = true;
81 }
82 
84  : LinSys(other), params_(other.params_), v_rhs_(NULL), solution_precision_(other.solution_precision_)
85 {
86  MatCopy(other.matrix_, matrix_, DIFFERENT_NONZERO_PATTERN);
87  VecCopy(other.rhs_, rhs_);
88  VecCopy(other.on_vec_, on_vec_);
89  VecCopy(other.off_vec_, off_vec_);
90 }
91 
92 void LinSys_PETSC::set_tolerances(double r_tol, double a_tol, unsigned int max_it)
93 {
94  if (! in_rec_.is_empty()) {
95  // input record is set
96  r_tol_ = in_rec_.val<double>("r_tol", r_tol);
97  a_tol_ = in_rec_.val<double>("a_tol", a_tol);
98  max_it_ = in_rec_.val<unsigned int>("max_it", max_it);
99  } else {
100  r_tol_ = r_tol;
101  a_tol_ = a_tol;
102  max_it_ = max_it;
103 
104  }
105 }
106 
107 
109 {
110  PetscErrorCode ierr;
111 
112  ierr = VecCreateMPI( comm_, rows_ds_->lsize(), PETSC_DECIDE, &(on_vec_) ); CHKERRV( ierr );
113  ierr = VecDuplicate( on_vec_, &(off_vec_) ); CHKERRV( ierr );
114  status_ = ALLOCATE;
115 }
116 
118 {
119  switch ( status_ ) {
120  case ALLOCATE:
121  this->preallocate_matrix( );
122  break;
123  case INSERT:
124  this->finish_assembly( MAT_FLUSH_ASSEMBLY );
125  break;
126  case ADD:
127  case DONE:
128  break;
129  default:
130  ASSERT_PERMANENT(false).error("Can not set values. Matrix is not preallocated.\n");
131  }
132  status_ = ADD;
133 }
134 
136 {
137  switch ( status_ ) {
138  case ALLOCATE:
139  this->preallocate_matrix();
140  break;
141  case ADD:
142  this->finish_assembly( MAT_FLUSH_ASSEMBLY );
143  break;
144  case INSERT:
145  case DONE:
146  break;
147  default:
148  ASSERT_PERMANENT(false).error("Can not set values. Matrix is not preallocated.\n");
149  }
150  status_ = INSERT;
151 }
152 
153 void LinSys_PETSC::mat_set_values( int nrow, int *rows, int ncol, int *cols, double *vals )
154 {
155  // here vals would need to be converted from double to PetscScalar if it was ever something else than double :-)
156  switch (status_) {
157  case INSERT:
158  case ADD:
159  chkerr(MatSetValues(matrix_,nrow,rows,ncol,cols,vals,(InsertMode)status_));
160  break;
161  case ALLOCATE:
162  this->preallocate_values(nrow,rows,ncol,cols);
163  break;
164  default: DebugOut() << "LS SetValues with non allowed insert mode.\n";
165  }
166 
167  matrix_changed_ = true;
168 }
169 
170 void LinSys_PETSC::rhs_set_values( int nrow, int *rows, double *vals )
171 {
172  PetscErrorCode ierr;
173 
174  switch (status_) {
175  case INSERT:
176  case ADD:
177  ierr = VecSetValues(rhs_,nrow,rows,vals,(InsertMode)status_); CHKERRV( ierr );
178  break;
179  case ALLOCATE:
180  break;
181  default: ASSERT_PERMANENT(false).error("LinSys's status disallow set values.\n");
182  }
183 
184  rhs_changed_ = true;
185 }
186 
187 void LinSys_PETSC::preallocate_values(int nrow,int *rows,int ncol,int *cols)
188 {
189  int i,j;
190  int col;
191  PetscInt row;
192 
193  for (i=0; i<nrow; i++) {
194  row=rows[i];
195  for(j=0; j<ncol; j++) {
196  col = cols[j];
197  if (rows_ds_->get_proc(row) == rows_ds_->get_proc(col))
198  VecSetValue(on_vec_,row,1.0,ADD_VALUES);
199  else
200  VecSetValue(off_vec_,row,1.0,ADD_VALUES);
201  }
202  }
203 }
204 
206 {
207  ASSERT_EQ(status_, ALLOCATE).error("Linear system has to be in ALLOCATE status.");
208 
209  PetscErrorCode ierr;
210  PetscInt *on_nz, *off_nz;
211  PetscScalar *on_array, *off_array;
212 
213  // assembly and get values from counting vectors, destroy them
214  VecAssemblyBegin(on_vec_);
215  VecAssemblyBegin(off_vec_);
216 
217  on_nz = new PetscInt[ rows_ds_->lsize() ];
218  off_nz = new PetscInt[ rows_ds_->lsize() ];
219 
220  VecAssemblyEnd(on_vec_);
221  VecAssemblyEnd(off_vec_);
222 
223  VecGetArray( on_vec_, &on_array );
224  VecGetArray( off_vec_, &off_array );
225 
226  for ( unsigned int i=0; i<rows_ds_->lsize(); i++ ) {
227  on_nz[i] = std::min( rows_ds_->lsize(), static_cast<uint>( on_array[i]+0.1 ) ); // small fraction to ensure correct rounding
228  off_nz[i] = std::min( rows_ds_->size() - rows_ds_->lsize(), static_cast<uint>( off_array[i]+0.1 ) );
229  }
230 
231  VecRestoreArray(on_vec_,&on_array);
232  VecRestoreArray(off_vec_,&off_array);
233  VecDestroy(&on_vec_);
234  VecDestroy(&off_vec_);
235 
236  // create PETSC matrix with preallocation
237  if (matrix_ != NULL)
238  {
239  chkerr(MatDestroy(&matrix_));
240  }
241  ierr = MatCreateAIJ(PETSC_COMM_WORLD, rows_ds_->lsize(), rows_ds_->lsize(), PETSC_DETERMINE, PETSC_DETERMINE,
242  0, on_nz, 0, off_nz, &matrix_); CHKERRV( ierr );
243 
244  if (symmetric_) MatSetOption(matrix_, MAT_SYMMETRIC, PETSC_TRUE);
245  MatSetOption(matrix_, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
246 
247  // This option is used in order to assembly larger local matrices with own non-zero structure.
248  // Zero entries are ignored so we must prevent adding exact zeroes.
249  // Add LocalSystem::almost_zero for entries that should not be eliminated.
250  MatSetOption(matrix_, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
251 
252 
253 
254  delete[] on_nz;
255  delete[] off_nz;
256 }
257 
258 
260 {
261  std::vector<LongIdx> dof_indices(dh.max_elem_dofs()), dof_indices_other(dh.max_elem_dofs());
262  std::vector<double> values(dh.max_elem_dofs()*dh.max_elem_dofs(), 1.);
263  LongIdx ndofs, ndofs_other;
264 
265  for (auto cell : dh.own_range())
266  {
267  ndofs = cell.get_dof_indices(dof_indices);
268  preallocate_values(ndofs, dof_indices.data(), ndofs, dof_indices.data());
269 
270  for (auto side : cell.side_range())
271  {
272  // if (side.edge_sides().begin()->element().idx() == cell.elm_idx())
273  {
274  for (auto edge : side.edge_sides())
275  {
276  auto cell_other = edge.cell();
277  ndofs_other = cell_other.get_dof_indices(dof_indices_other);
278  preallocate_values(ndofs, dof_indices.data(), ndofs_other, dof_indices_other.data());
279  preallocate_values(ndofs_other, dof_indices_other.data(), ndofs, dof_indices.data());
280  }
281  }
282  }
283 
284  for (auto neigh_side : cell.neighb_sides())
285  {
286  auto cell_other = neigh_side.cell();
287  ndofs_other = cell_other.get_dof_indices(dof_indices_other);
288  preallocate_values(ndofs, dof_indices.data(), ndofs_other, dof_indices_other.data());
289  preallocate_values(ndofs_other, dof_indices_other.data(), ndofs, dof_indices.data());
290  }
291  }
292 
294 
295  for (auto cell : dh.own_range())
296  {
297  ndofs = cell.get_dof_indices(dof_indices);
298  MatSetValues(matrix_, ndofs, dof_indices.data(), ndofs, dof_indices.data(), values.data(), INSERT_VALUES);
299 
300  for (auto side : cell.side_range())
301  {
302  // if (side.edge_sides().begin()->element().idx() == cell.elm_idx())
303  {
304  for (auto edge : side.edge_sides())
305  {
306  auto cell_other = edge.cell();
307  ndofs_other = cell_other.get_dof_indices(dof_indices_other);
308  MatSetValues(matrix_, ndofs, dof_indices.data(), ndofs_other, dof_indices_other.data(), values.data(), INSERT_VALUES);
309  MatSetValues(matrix_, ndofs_other, dof_indices_other.data(), ndofs, dof_indices.data(), values.data(), INSERT_VALUES);
310  }
311  }
312  }
313 
314  for (auto neigh_side : cell.neighb_sides())
315  {
316  auto cell_other = neigh_side.cell();
317  ndofs_other = cell_other.get_dof_indices(dof_indices_other);
318  MatSetValues(matrix_, ndofs, dof_indices.data(), ndofs_other, dof_indices_other.data(), values.data(), INSERT_VALUES);
319  MatSetValues(matrix_, ndofs_other, dof_indices_other.data(), ndofs, dof_indices.data(), values.data(), INSERT_VALUES);
320  }
321  }
322  MatAssemblyBegin(matrix_, MAT_FINAL_ASSEMBLY);
323  MatAssemblyEnd(matrix_, MAT_FINAL_ASSEMBLY);
324  MatZeroEntries(matrix_);
325 }
326 
327 
329 {
330  MatAssemblyType assemblyType = MAT_FINAL_ASSEMBLY;
331  this->finish_assembly( assemblyType );
332 }
333 
334 void LinSys_PETSC::finish_assembly( MatAssemblyType assembly_type )
335 {
336  PetscErrorCode ierr;
337 
338  if (status_ == ALLOCATE) {
339  WarningOut() << "Finalizing linear system without setting values.\n";
340  this->preallocate_matrix();
341  }
342  ierr = MatAssemblyBegin(matrix_, assembly_type); CHKERRV( ierr );
343  ierr = VecAssemblyBegin(rhs_); CHKERRV( ierr );
344  ierr = MatAssemblyEnd(matrix_, assembly_type); CHKERRV( ierr );
345  ierr = VecAssemblyEnd(rhs_); CHKERRV( ierr );
346 
347  if (assembly_type == MAT_FINAL_ASSEMBLY) status_ = DONE;
348 
349  //PetscViewerPushFormat(PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_ASCII_INDEX);
350  //MatView(matrix_, PETSC_VIEWER_STDOUT_SELF);
351  //VecView(rhs_, PETSC_VIEWER_STDOUT_SELF);
352  //this->view();
353 
354  matrix_changed_ = true;
355  rhs_changed_ = true;
356 }
357 
358 void LinSys_PETSC::apply_constrains( double scalar )
359 {
360  PetscErrorCode ierr;
361 
362  // check that system matrix is assembled
363  ASSERT_EQ(status_, DONE).error("System matrix and right-hand side are not assembled when applying constraints." );
364 
365  // number of constraints
366  PetscInt numConstraints = static_cast<PetscInt>( constraints_.size() );
367 
368  // Additional multiplier for numerical reasons (criterion to be established)
369  const PetscScalar diagScalar = static_cast<PetscScalar>( scalar );
370 
371  std::vector<PetscInt> globalDofs;
373 
374  // Constraint iterators
375  ConstraintVec_::const_iterator cIter = constraints_.begin( );
376  ConstraintVec_::const_iterator cEnd = constraints_.end( );
377  // collect global dof indices and the correpsonding values
378  for ( ; cIter != cEnd; ++cIter ) {
379  globalDofs.push_back( static_cast<PetscInt>( cIter -> first ) );
380  values.push_back( static_cast<PetscScalar>( cIter -> second ) * diagScalar );
381  }
382 
383  // prepare pointers to be passed to PETSc
384  PetscInt * globalDofPtr = this->makePetscPointer_( globalDofs );
385  PetscScalar * valuePtr = this->makePetscPointer_( values );
386 
387  // set matrix rows to zero
388  ierr = MatZeroRows( matrix_, numConstraints, globalDofPtr, diagScalar, PETSC_NULL, PETSC_NULL ); CHKERRV( ierr );
389  matrix_changed_ = true;
390 
391  // set RHS entries to values (crashes if called with NULL pointers)
392  if ( numConstraints ) {
393  ierr = VecSetValues( rhs_, numConstraints, globalDofPtr, valuePtr, INSERT_VALUES ); CHKERRV( ierr );
394  rhs_changed_ = true;
395  }
396 
397  // perform communication in the rhs vector
398  ierr = VecAssemblyBegin( rhs_ ); CHKERRV( ierr );
399  ierr = VecAssemblyEnd( rhs_ ); CHKERRV( ierr );
400 }
401 
402 
404 {
405  init_guess_nonzero = set_nonzero;
406 }
407 
408 
410 {
411 
412  const char *petsc_dflt_opt;
413  int nits;
414 
415  // -mat_no_inode ... inodes are usefull only for
416  // vector problems e.g. MH without Schur complement reduction
417 
418  /* Comment to PETSc options:
419  *
420  * -ksp_diagonal_scale scales the matrix before solution, while -ksp_diagonal_scale_fix just fixes the scaling after solution
421  * -pc_asm_type basic enforces classical Schwartz method, which seems more stable for positive definite systems.
422  * The default 'restricted' probably violates s.p.d. structure, many tests fail.
423  */
424  if (rows_ds_->np() > 1) {
425  // parallel setting
426  if (this->is_positive_definite())
427  petsc_dflt_opt="-ksp_type cg -ksp_diagonal_scale -ksp_diagonal_scale_fix -pc_type asm -pc_asm_type basic -pc_asm_overlap 4 -sub_pc_type icc -sub_pc_factor_levels 3 -sub_pc_factor_fill 6.0";
428  //petsc_dflt_opt="-ksp_type bcgs -ksp_diagonal_scale_fix -pc_type asm -pc_asm_overlap 4 -sub_pc_type ilu -sub_pc_factor_levels 3 -sub_pc_factor_fill 6.0";
429  else
430  petsc_dflt_opt="-ksp_type bcgs -ksp_diagonal_scale -ksp_diagonal_scale_fix -pc_type asm -pc_asm_overlap 4 -sub_pc_type ilu -sub_pc_factor_levels 3 -sub_pc_factor_fill 6.0";
431 
432  }
433  else {
434  // serial setting
435  if (this->is_positive_definite())
436  petsc_dflt_opt="-ksp_type cg -pc_type icc -pc_factor_levels 3 -ksp_diagonal_scale -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
437  //petsc_dflt_opt="-ksp_type bcgs -pc_type ilu -pc_factor_levels 5 -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
438  else
439  petsc_dflt_opt="-ksp_type bcgs -pc_type ilu -pc_factor_levels 5 -ksp_diagonal_scale -ksp_diagonal_scale_fix -pc_factor_fill 6.0";
440  }
441 
442  if (params_ == "") params_ = petsc_dflt_opt;
443  LogOut().fmt("inserting petsc options: {}\n",params_.c_str());
444 
445  // now takes an optional PetscOptions object as the first argument
446  // value NULL will preserve previous behaviour previous behavior.
447  PetscOptionsInsertString(NULL, params_.c_str()); // overwrites previous options values
448 
449  MatSetOption( matrix_, MAT_USE_INODES, PETSC_FALSE );
450 
451  chkerr(KSPCreate( comm_, &system ));
452  chkerr(KSPSetOperators(system, matrix_, matrix_));
453 
454 
455  // TODO take care of tolerances - shall we support both input file and command line petsc setting
456  chkerr(KSPSetTolerances(system, r_tol_, a_tol_, PETSC_DEFAULT,PETSC_DEFAULT));
457  chkerr(KSPSetTolerances(system, r_tol_, a_tol_, PETSC_DEFAULT, max_it_));
458  KSPSetFromOptions(system);
459  // We set the KSP flag set_initial_guess_nonzero
460  // unless KSP type is preonly.
461  // In such case PETSc fails (version 3.4.1)
462  if (init_guess_nonzero)
463  {
464  KSPType type;
465  KSPGetType(system, &type);
466  if (strcmp(type, KSPPREONLY) != 0)
467  KSPSetInitialGuessNonzero(system, PETSC_TRUE);
468  }
469 
470  {
471  START_TIMER("PETSC linear solver");
472  START_TIMER("PETSC linear iteration");
473  chkerr(KSPSolve(system, rhs_, solution_ ));
474  KSPGetConvergedReason(system,&reason);
475  KSPGetIterationNumber(system,&nits);
476  ADD_CALLS(nits);
477  }
478  // substitute by PETSc call for residual
479  VecNorm(rhs_, NORM_2, &residual_norm_);
480 
481  LogOut().fmt("convergence reason {}, number of iterations is {}\n", reason, nits);
482 
483  // get residual norm
484  KSPGetResidualNorm(system, &solution_precision_);
485 
486  // TODO: I do not understand this
487  //Profiler::instance()->set_timer_subframes("SOLVING MH SYSTEM", nits);
488 
489  chkerr(KSPDestroy(&system));
490 
491  return LinSys::SolveInfo(static_cast<int>(reason), static_cast<int>(nits));
492 
493 }
494 
495 void LinSys_PETSC::view(string text )
496 {
497  FilePath matFileName(text + "_flow123d_matrix.m",FilePath::FileType::output_file);
498  FilePath rhsFileName(text + "_flow123d_rhs.m",FilePath::FileType::output_file);
499  FilePath solFileName(text + "_flow123d_sol.m",FilePath::FileType::output_file);
500 
501  PetscViewer myViewer;
502 
503  if ( matrix_ != NULL ) {
504  PetscViewerASCIIOpen(comm_,((string)matFileName).c_str(),&myViewer);
505  PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
506  MatView( matrix_, myViewer );
507  PetscViewerDestroy(&myViewer);
508  }
509  else
510  WarningOut() << "PetscViewer: the matrix of LinSys is not set.\n";
511 
512  if ( rhs_ != NULL ) {
513  PetscViewerASCIIOpen(comm_,((string)rhsFileName).c_str(),&myViewer);
514  PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
515  VecView( rhs_, myViewer );
516  PetscViewerDestroy(&myViewer);
517  }
518  else
519  WarningOut() << "PetscViewer: the rhs of LinSys is not set.\n";
520 
521  if ( solution_ != NULL ) {
522  PetscViewerASCIIOpen(comm_,((string)solFileName).c_str(),&myViewer);
523  PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
524  VecView( solution_, myViewer );
525  PetscViewerDestroy(&myViewer);
526  }
527  else
528  WarningOut() << "PetscViewer: the solution of LinSys is not set.\n";
529 }
530 
532 {
533  if (matrix_ != NULL) { chkerr(MatDestroy(&matrix_)); }
534  chkerr(VecDestroy(&rhs_));
535 
536  if (residual_ != NULL) chkerr(VecDestroy(&residual_));
537  if (v_rhs_ != NULL) delete[] v_rhs_;
538 }
539 
540 
541 
543 {
544  LinSys::set_from_input( in_rec );
545 
546  // PETSC specific parameters
547  // If parameters are specified in input file, they are used,
548  // otherwise keep settings provided in constructor of LinSys_PETSC.
549  std::string user_params = in_rec.val<string>("options");
550  if (user_params != "") params_ = user_params;
551 }
552 
553 
555 {
556  return solution_precision_;
557 }
558 
559 
561 {
562  MatMult(matrix_, solution_, residual_);
563  VecAXPY(residual_,-1.0, rhs_);
564  double residual_norm;
565  VecNorm(residual_, NORM_2, &residual_norm);
566  return residual_norm;
567 }
Distribution::np
unsigned int np() const
get num of processors
Definition: distribution.hh:105
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
Distribution::lsize
unsigned int lsize(int proc) const
get local size
Definition: distribution.hh:115
LinSys_PETSC::start_insert_assembly
void start_insert_assembly() override
Definition: linsys_PETSC.cc:135
LinSys::residual_norm_
double residual_norm_
local solution array pointing into Vec solution_
Definition: linsys.hh:688
LinSys_PETSC::set_initial_guess_nonzero
void set_initial_guess_nonzero(bool set_nonzero=true)
Definition: linsys_PETSC.cc:403
LinSys::symmetric_
bool symmetric_
Definition: linsys.hh:675
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::init_guess_nonzero
bool init_guess_nonzero
flag for starting from nonzero guess
Definition: linsys_PETSC.hh:176
LinSys_PETSC::LinSys_PETSC
LinSys_PETSC(const Distribution *rows_ds, const std::string &params="")
Definition: linsys_PETSC.cc:63
Input::Type::Integer
Class for declaration of the integral input data.
Definition: type_base.hh:483
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
LinSys_PETSC::apply_constrains
void apply_constrains(double scalar=1.) override
Definition: linsys_PETSC.cc:358
LinSys_PETSC::reason
KSPConvergedReason reason
Definition: linsys_PETSC.hh:191
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
Input::Type::Double
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
chkerr
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:142
LinSys_PETSC
Definition: linsys_PETSC.hh:44
std::vector< LongIdx >
Input::Type::Default::read_time
static Default read_time(const std::string &description)
The factory function to make an default value that will be specified at the time when a key will be r...
Definition: type_record.hh:97
Distribution::size
unsigned int size() const
get global size
Definition: distribution.hh:118
system.hh
LinSys::ALLOCATE
@ ALLOCATE
Definition: linsys.hh:99
ConstraintType::cols
@ cols
LinSys_PETSC::solve
LinSys::SolveInfo solve() override
Definition: linsys_PETSC.cc:409
dofhandler.hh
Declaration of class which handles the ordering of degrees of freedom (dof) and mappings between loca...
uint
unsigned int uint
Definition: mh_dofhandler.hh:101
linsys_PETSC.hh
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
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::DONE
@ DONE
Definition: linsys.hh:100
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
Distribution::get_proc
unsigned int get_proc(unsigned int idx) const
get processor of the given index
Definition: distribution.cc:130
ConstraintType::rows
@ rows
Input::Type::Record::size
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:602
Input::Type::Default
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Input::Type::Record::derive_from
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
Distribution
Definition: distribution.hh:50
dh_cell_accessor.hh
LinSys::solution_
Vec solution_
PETSc vector constructed with vb array.
Definition: linsys.hh:683
LogOut
#define LogOut()
Macro defining 'log' record of log.
Definition: logger.hh:281
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
LinSys::a_tol_
double a_tol_
absolute tolerance of linear solver
Definition: linsys.hh:664
sys_profiler.hh
ASSERT_PERMANENT
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
LinSys::in_rec_
Input::Record in_rec_
Definition: linsys.hh:694
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
DOFHandlerMultiDim::own_range
Range< DHCellAccessor > own_range() const
Returns range of DOF handler cells (only range of own without ghost cells)
Definition: dofhandler.cc:680
LinSys::status_
SetValuesMode status_
Set value status of the linear system.
Definition: linsys.hh:668
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
ASSERT_EQ
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
Input::Type::Record::declare_key
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:503
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
LinSys::rows_ds_
const Distribution * rows_ds_
final distribution of rows of MH matrix
Definition: linsys.hh:673
Input::Type
Definition: balance.hh:41
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
LinSys::set_from_input
virtual void set_from_input(const Input::Record in_rec)
Definition: linsys.hh:640
LinSys_PETSC::start_add_assembly
void start_add_assembly() override
Definition: linsys_PETSC.cc:117
LinSys::INSERT
@ INSERT
Definition: linsys.hh:97
LinSys::r_tol_
double r_tol_
relative tolerance of linear solver
Definition: linsys.hh:663
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
LinSys_PETSC::set_from_input
void set_from_input(const Input::Record in_rec) override
Definition: linsys_PETSC.cc:542
Input::Type::String
Class for declaration of the input data that are in string format.
Definition: type_base.hh:582
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
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
ADD_CALLS
#define ADD_CALLS(n_calls)
Increase number of calls in actual timer.
Definition: sys_profiler.hh:186
LinSys_PETSC::makePetscPointer_
T * makePetscPointer_(std::vector< T > &array)
Definition: linsys_PETSC.hh:157
std::vector::data
T data
Definition: doxy_dummy_defs.hh:7
LinSys_PETSC::start_allocation
void start_allocation() override
Definition: linsys_PETSC.cc:108
LinSys::get_input_type
static Input::Type::Abstract & get_input_type()
Definition: linsys.cc:29
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::ADD
@ ADD
Definition: linsys.hh:98
edge
@ edge
Definition: generic_assembly.hh:34
LinSys::is_positive_definite
bool is_positive_definite()
Definition: linsys.hh:596
DebugOut
#define DebugOut()
Macro defining 'debug' record of log.
Definition: logger.hh:284
LinSys::comm_
MPI_Comm comm_
Definition: linsys.hh:667
START_TIMER
#define START_TIMER(tag)
Starts a timer with specified tag.
Definition: sys_profiler.hh:115
LinSys_PETSC::rhs_
Vec rhs_
PETSc vector constructed with vx array.
Definition: linsys_PETSC.hh:179
Input::Record::is_empty
bool is_empty() const
Definition: accessors.hh:365
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
LinSys::max_it_
unsigned int max_it_
maximum number of iterations of linear solver
Definition: linsys.hh:665
DOFHandlerBase::max_elem_dofs
unsigned int max_elem_dofs() const
Returns max. number of dofs on one element.
Definition: dofhandler.hh:71