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 |
|
---|
12 | void 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 | int iteration_number;
|
---|
23 | PetscTruth flag;
|
---|
24 | int solver_type;
|
---|
25 |
|
---|
26 | /*If initial guess for solution exists, use it to create uf, otherwise,
|
---|
27 | * duplicate right hand side so that solution vector has same structure*/
|
---|
28 | if(uf0){
|
---|
29 | VecDuplicate(uf0,&uf); VecCopy(uf0,uf);
|
---|
30 | }
|
---|
31 | else{
|
---|
32 | MatGetLocalSize(Kff,&local_m,&local_n);uf=NewVecFromLocalSize(local_n);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /*Before preparing the solver, add options to the options database*/
|
---|
36 | PetscOptionsInsertMultipleString(solver_string);
|
---|
37 |
|
---|
38 | /*Process solver_string to see if we are not using special types of external solvers: */
|
---|
39 | PetscOptionsDetermineSolverType(&solver_type,solver_string);
|
---|
40 |
|
---|
41 | if (solver_type==MUMPSPACKAGE_LU){
|
---|
42 | /*Convert Kff to MATTAIJMUMPS: */
|
---|
43 | MatConvert(Kff,MATAIJMUMPS,MAT_REUSE_MATRIX,&Kff);
|
---|
44 | }
|
---|
45 | if (solver_type==MUMPSPACKAGE_CHOL){
|
---|
46 | /*Convert Kff to MATTSBAIJMUMPS: */
|
---|
47 | MatConvert(Kff,MATSBAIJMUMPS,MAT_REUSE_MATRIX,&Kff);
|
---|
48 | }
|
---|
49 | if (solver_type==SPOOLESPACKAGE_LU){
|
---|
50 | /*Convert Kff to MATTSBAIJMUMPS: */
|
---|
51 | MatConvert(Kff,MATAIJSPOOLES,MAT_REUSE_MATRIX,&Kff);
|
---|
52 | }
|
---|
53 | if (solver_type==SPOOLESPACKAGE_CHOL){
|
---|
54 | /*Convert Kff to MATTSBAIJMUMPS: */
|
---|
55 | MatConvert(Kff,MATSBAIJSPOOLES,MAT_REUSE_MATRIX,&Kff);
|
---|
56 | }
|
---|
57 | if (solver_type==SUPERLUDISTPACKAGE){
|
---|
58 | /*Convert Kff to MATTSBAIJMUMPS: */
|
---|
59 | MatConvert(Kff,MATSUPERLU_DIST,MAT_REUSE_MATRIX,&Kff);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /*Prepare solver*/
|
---|
64 | KSPCreate(MPI_COMM_WORLD,&ksp);
|
---|
65 | KSPSetOperators(ksp,Kff,Kff,DIFFERENT_NONZERO_PATTERN);
|
---|
66 | KSPSetFromOptions(ksp);
|
---|
67 |
|
---|
68 | /*If initial guess for solution, use it, except if we are using the MUMPS direct solver, where any initial
|
---|
69 | * guess will crash Petsc: */
|
---|
70 | if (uf0){
|
---|
71 | if( (solver_type!=MUMPSPACKAGE_LU) && (solver_type!=MUMPSPACKAGE_CHOL) && (solver_type!=SPOOLESPACKAGE_LU)&& (solver_type!=SPOOLESPACKAGE_CHOL)&& (solver_type!=SUPERLUDISTPACKAGE)){
|
---|
72 | KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | #ifdef _ISSM_DEBUG_
|
---|
78 | KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
|
---|
79 | KSPGetInitialGuessNonzero(ksp,&flag);
|
---|
80 | _printf_("User provided initial guess solution: %i\n",flag);
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | KSPSolve(ksp,pf,uf);
|
---|
84 |
|
---|
85 | /*Check convergence*/
|
---|
86 | KSPGetIterationNumber(ksp,&iteration_number);
|
---|
87 | if (iteration_number<0){
|
---|
88 | throw ErrorException(__FUNCT__,exprintf("%s%i"," Solver diverged at iteration number: ",-iteration_number));
|
---|
89 | }
|
---|
90 | else{
|
---|
91 | #ifdef _ISSM_DEBUG_
|
---|
92 | _printf_("%s%i%s\n","Solver converged after ",iteration_number," iterations");
|
---|
93 | #endif
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*Free ressources:*/
|
---|
97 | KSPFree(&ksp);
|
---|
98 |
|
---|
99 |
|
---|
100 | /*Assign output pointers:*/
|
---|
101 | *puf=uf;
|
---|
102 | }
|
---|