source: issm/trunk/src/c/Solverx/Solverx.cpp@ 2042

Last change on this file since 2042 was 2042, checked in by Eric.Larour, 15 years ago

New petsc and new mpich2, with opteron optimization, sounds a lot faster, may be...

File size: 2.9 KB
Line 
1/*!\file Solverx
2 * \brief solver
3 */
4
5#include "./Solverx.h"
6
7#undef __FUNCT__
8#define __FUNCT__ "Solverx"
9
10#include "../shared/shared.h"
11
12void Solverx( Vec* puf, Mat Kff, Vec pf, Vec uf0, char* solver_string){
13
14 /*output: */
15 Vec uf=NULL;
16
17 /*intermediary: */
18 int local_m,local_n;
19
20 /*Solver*/
21 KSP ksp=NULL;
22 PC pc=NULL;
23 int iteration_number;
24 PetscTruth flag;
25 int solver_type;
26
27 /*If initial guess for solution exists, use it to create uf, otherwise,
28 * duplicate right hand side so that solution vector has same structure*/
29 if(uf0){
30 VecDuplicate(uf0,&uf); VecCopy(uf0,uf);
31 }
32 else{
33 MatGetLocalSize(Kff,&local_m,&local_n);uf=NewVecFromLocalSize(local_n);
34 }
35
36 /*Before preparing the solver, add options to the options database*/
37 PetscOptionsInsertMultipleString(solver_string);
38
39 /*Process solver_string to see if we are not using special types of external solvers: */
40 PetscOptionsDetermineSolverType(&solver_type,solver_string);
41
42 /*obsolete? : */
43// if (solver_type==MUMPSPACKAGE_LU){
44// /*Convert Kff to MATTAIJMUMPS: */
45// MatConvert(Kff,MATAIJMUMPS,MAT_REUSE_MATRIX,&Kff);
46// }
47// if (solver_type==MUMPSPACKAGE_CHOL){
48// /*Convert Kff to MATTSBAIJMUMPS: */
49// MatConvert(Kff,MATSBAIJMUMPS,MAT_REUSE_MATRIX,&Kff);
50// }
51// if (solver_type==SPOOLESPACKAGE_LU){
52// /*Convert Kff to MATTSBAIJMUMPS: */
53// MatConvert(Kff,MATAIJSPOOLES,MAT_REUSE_MATRIX,&Kff);
54// }
55// if (solver_type==SPOOLESPACKAGE_CHOL){
56// /*Convert Kff to MATTSBAIJMUMPS: */
57// MatConvert(Kff,MATSBAIJSPOOLES,MAT_REUSE_MATRIX,&Kff);
58// }
59// if (solver_type==SUPERLUDISTPACKAGE){
60// /*Convert Kff to MATTSBAIJMUMPS: */
61// MatConvert(Kff,MATSUPERLU_DIST,MAT_REUSE_MATRIX,&Kff);
62// }
63//
64
65 /*Prepare solver*/
66 KSPCreate(MPI_COMM_WORLD,&ksp);
67 KSPSetOperators(ksp,Kff,Kff,DIFFERENT_NONZERO_PATTERN);
68 KSPSetFromOptions(ksp);
69
70 /*specific solver?: */
71 KSPGetPC(ksp,&pc);
72 if (solver_type==MUMPSPACKAGE_LU){
73 PCFactorSetMatSolverPackage(pc,MAT_SOLVER_MUMPS);
74 }
75
76 /*If initial guess for solution, use it, except if we are using the MUMPS direct solver, where any initial
77 * guess will crash Petsc: */
78 if (uf0){
79 if( (solver_type!=MUMPSPACKAGE_LU) && (solver_type!=MUMPSPACKAGE_CHOL) && (solver_type!=SPOOLESPACKAGE_LU)&& (solver_type!=SPOOLESPACKAGE_CHOL)&& (solver_type!=SUPERLUDISTPACKAGE)){
80 KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
81 }
82 }
83
84
85 #ifdef _ISSM_DEBUG_
86 KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
87 KSPGetInitialGuessNonzero(ksp,&flag);
88 _printf_("User provided initial guess solution: %i\n",flag);
89 #endif
90
91 KSPSolve(ksp,pf,uf);
92
93 /*Check convergence*/
94 KSPGetIterationNumber(ksp,&iteration_number);
95 if (iteration_number<0){
96 throw ErrorException(__FUNCT__,exprintf("%s%i"," Solver diverged at iteration number: ",-iteration_number));
97 }
98 else{
99 #ifdef _ISSM_DEBUG_
100 _printf_("%s%i%s\n","Solver converged after ",iteration_number," iterations");
101 #endif
102 }
103
104 /*Free ressources:*/
105 KSPFree(&ksp);
106
107
108 /*Assign output pointers:*/
109 *puf=uf;
110}
Note: See TracBrowser for help on using the repository browser.