38 #include <boost/bind.hpp>
40 namespace it = Input::Type;
50 init_guess_nonzero(false),
61 ierr = VecZeroEntries(
rhs_ ); CHKERRV( ierr );
71 :
LinSys(other), params_(other.params_), v_rhs_(NULL), solution_precision_(other.solution_precision_)
101 ASSERT(
false,
"Can not set values. Matrix is not preallocated.\n");
119 ASSERT(
false,
"Can not set values. Matrix is not preallocated.\n");
132 ierr = MatSetValues(
matrix_,nrow,rows,ncol,cols,vals,(InsertMode)
status_); CHKERRV( ierr );
137 default:
DBGMSG(
"LS SetValues with non allowed insert mode.\n");
150 ierr = VecSetValues(
rhs_,nrow,rows,vals,(InsertMode)
status_); CHKERRV( ierr );
154 default:
ASSERT(
false,
"LinSys's status disallow set values.\n");
166 for (i=0; i<nrow; i++) {
168 for(j=0; j<ncol; j++) {
171 VecSetValue(
on_vec_,row,1.0,ADD_VALUES);
173 VecSetValue(
off_vec_,row,1.0,ADD_VALUES);
183 PetscInt *on_nz, *off_nz;
184 PetscScalar *on_array, *off_array;
196 VecGetArray(
on_vec_, &on_array );
197 VecGetArray(
off_vec_, &off_array );
200 on_nz[i] =
static_cast<PetscInt
>( on_array[i]+0.1 );
201 off_nz[i] =
static_cast<PetscInt
>( off_array[i]+0.1 );
204 VecRestoreArray(
on_vec_,&on_array);
205 VecRestoreArray(
off_vec_,&off_array);
212 ierr = MatDestroy(&
matrix_); CHKERRV( ierr );
215 0, on_nz, 0, off_nz, &
matrix_); CHKERRV( ierr );
218 MatSetOption(
matrix_, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
226 MatAssemblyType assemblyType = MAT_FINAL_ASSEMBLY;
235 xprintf(
Warn,
"Finalizing linear system without setting values.\n");
238 ierr = MatAssemblyBegin(
matrix_, assembly_type); CHKERRV( ierr );
239 ierr = VecAssemblyBegin(
rhs_); CHKERRV( ierr );
240 ierr = MatAssemblyEnd(
matrix_, assembly_type); CHKERRV( ierr );
241 ierr = VecAssemblyEnd(
rhs_); CHKERRV( ierr );
243 if (assembly_type == MAT_FINAL_ASSEMBLY)
status_ =
DONE;
254 ASSERT (
status_ ==
DONE,
"System matrix and right-hand side are not assembled when applying constraints." );
257 PetscInt numConstraints =
static_cast<PetscInt
>(
constraints_.size() );
260 const PetscScalar diagScalar =
static_cast<PetscScalar
>( scalar );
266 ConstraintVec_::const_iterator cIter =
constraints_.begin( );
267 ConstraintVec_::const_iterator cEnd =
constraints_.end( );
269 for ( ; cIter != cEnd; ++cIter ) {
270 globalDofs.push_back( static_cast<PetscInt>( cIter -> first ) );
271 values.push_back( static_cast<PetscScalar>( cIter -> second ) * diagScalar );
279 ierr = MatZeroRows(
matrix_, numConstraints, globalDofPtr, diagScalar, PETSC_NULL, PETSC_NULL ); CHKERRV( ierr );
283 if ( numConstraints ) {
284 ierr = VecSetValues(
rhs_, numConstraints, globalDofPtr, valuePtr, INSERT_VALUES ); CHKERRV( ierr );
289 ierr = VecAssemblyBegin(
rhs_ ); CHKERRV( ierr );
290 ierr = VecAssemblyEnd(
rhs_ ); CHKERRV( ierr );
303 KSPConvergedReason reason;
305 const char *petsc_dflt_opt;
314 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";
318 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";
324 petsc_dflt_opt=
"-ksp_type cg -pc_type ilu -pc_factor_levels 3 -ksp_diagonal_scale_fix -pc_factor_shift_positive_definite -pc_factor_fill 6.0";
326 petsc_dflt_opt=
"-ksp_type bcgs -pc_type ilu -pc_factor_levels 5 -ksp_diagonal_scale_fix -pc_factor_shift_positive_definite";
332 PetscOptionsInsertString(
params_.c_str());
335 MatSetOption(
matrix_, MAT_USE_INODES, PETSC_FALSE );
337 KSPCreate(
comm_, &system );
338 KSPSetOperators(system,
matrix_,
matrix_, DIFFERENT_NONZERO_PATTERN);
345 KSPSetTolerances(system,
r_tol_,
a_tol_, PETSC_DEFAULT,PETSC_DEFAULT);
346 KSPSetFromOptions(system);
353 KSPGetType(system, &type);
354 if (strcmp(type, KSPPREONLY) != 0)
355 KSPSetInitialGuessNonzero(system, PETSC_TRUE);
359 KSPGetConvergedReason(system,&reason);
360 KSPGetIterationNumber(system,&nits);
365 xprintf(
MsgLog,
"convergence reason %d, number of iterations is %d\n", reason, nits);
375 return static_cast<int>(reason);
388 std::string matFileName =
"flow123d_matrix.m";
389 std::string rhsFileName =
"flow123d_rhs.m";
390 std::string solFileName =
"flow123d_sol.m";
392 PetscViewer myViewer;
394 PetscViewerASCIIOpen(
comm_,matFileName.c_str(),&myViewer);
395 PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
397 PetscViewerDestroy(&myViewer);
399 PetscViewerASCIIOpen(
comm_,rhsFileName.c_str(),&myViewer);
400 PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
401 VecView(
rhs_, myViewer );
402 PetscViewerDestroy(&myViewer);
405 PetscViewerASCIIOpen(
comm_,solFileName.c_str(),&myViewer);
406 PetscViewerSetFormat(myViewer,PETSC_VIEWER_ASCII_MATLAB);
408 PetscViewerDestroy(&myViewer);
416 if (
matrix_ != NULL) { ierr = MatDestroy(&
matrix_); CHKERRV( ierr ); }
417 ierr = VecDestroy(&
rhs_); CHKERRV( ierr );
429 Vec solutionGathered;
433 VecScatter VSdistToLarge;
434 ierr = VecScatterCreateToAll(
solution_, &VSdistToLarge, &solutionGathered ); CHKERRV( ierr );
438 ierr = VecScatterBegin( VSdistToLarge,
solution_, solutionGathered, INSERT_VALUES, SCATTER_FORWARD ); CHKERRV( ierr );
439 ierr = VecScatterEnd( VSdistToLarge,
solution_, solutionGathered, INSERT_VALUES, SCATTER_FORWARD ); CHKERRV( ierr );
442 PetscScalar *solutionGatheredArray;
443 ierr = VecGetArray( solutionGathered, &solutionGatheredArray );
451 for (
unsigned int i = 0; i < globalSize; i++ ) {
456 ierr = VecRestoreArray( solutionGathered, &solutionGatheredArray );
459 ierr = VecDestroy( &solutionGathered ); CHKERRV( ierr );
460 ierr = VecScatterDestroy( &VSdistToLarge ); CHKERRV( ierr );
std::vector< double > globalSolution_
global solution in numbering for linear system
unsigned int size() const
get global size
unsigned int get_proc(unsigned int idx) const
get processor of the given index
SetValuesMode status_
Set value status of the linear system.
void start_insert_assembly()
void apply_constrains(double scalar=1.)
Solver based on the original PETSc solver using MPIAIJ matrix and succesive Schur complement construc...
virtual void set_from_input(const Input::Record in_rec)
bool matrix_changed_
true if the matrix was changed since the last solve
double * v_rhs_
local RHS array pointing to Vec rhs_
LinSys_PETSC(const Distribution *rows_ds)
ConstraintVec_ constraints_
Vec rhs_
PETSc vector constructed with vx array.
static Input::Type::Record input_type
void set_from_input(const Input::Record in_rec)
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
static Input::Type::AbstractRecord input_type
T * makePetscPointer_(std::vector< T > &array)
const Distribution * rows_ds_
final distribution of rows of MH matrix
unsigned int np() const
get num of processors
double residual_norm_
local solution array pointing into Vec solution_
Vec on_vec_
Vectors for counting non-zero entries in diagonal block.
void preallocate_matrix()
void get_whole_solution(std::vector< double > &globalSolution)
LinSysType type
Particular type of the linear system.
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()
Vec off_vec_
Vectors for counting non-zero entries in off-diagonal block.
double get_solution_precision()
Abstract linear system class.
bool is_positive_definite()
void set_initial_guess_nonzero(bool set_nonzero=true)
Mat matrix_
Petsc matrix of the problem.
bool init_guess_nonzero
flag for starting from nonzero guess
Vec solution_
PETSc vector constructed with vb array.
double solution_precision_
unsigned int lsize(int proc) const
get local size