source: issm/trunk-jpl/src/c/toolkits/petsc/objects/PetscSolver.cpp@ 14950

Last change on this file since 14950 was 14950, checked in by Mathieu Morlighem, 12 years ago

CHG: moved io to shared/io

File size: 6.2 KB
Line 
1/*!\file SolverxPetsc
2 * \brief Petsc implementation of solver
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include <config.h>
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
11#include "./PetscSolver.h"
12#include "../../../shared/Numerics/Verbosity.h"
13#include "../../../shared/MemOps/MemOps.h"
14#include "../../../shared/Exceptions/exceptions.h"
15#include "../../../shared/io/Comm/Comm.h"
16#include "../../../EnumDefinitions/EnumDefinitions.h"
17
18void PetscSolve(PetscVec** puf, PetscMat* Kff, PetscVec* pf, PetscVec* uf0,PetscVec* df, Parameters* parameters){ /*{{{*/
19
20 PetscVec* uf=new PetscVec();
21
22 Vec uf0_vector = NULL;
23 Vec df_vector = NULL;
24
25 if(uf0) uf0_vector = uf0->vector;
26 if(df) df_vector = df->vector;
27
28 SolverxPetsc(&uf->vector, Kff->matrix, pf->vector, uf0_vector, df_vector, parameters);
29
30 *puf=uf;
31
32}
33/*}}}*/
34void SolverxPetsc(Vec* puf, Mat Kff, Vec pf, Vec uf0,Vec df, Parameters* parameters){ /*{{{*/
35
36 /*Output: */
37 Vec uf = NULL;
38
39 /*Intermediary: */
40 int local_m,local_n,global_m,global_n;
41
42 /*Solver */
43 KSP ksp = NULL;
44 PC pc = NULL;
45 int iteration_number;
46 int solver_type;
47 bool fromlocalsize = true;
48 #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
49 PetscTruth flag,flg;
50 #else
51 PetscBool flag,flg;
52 #endif
53
54 /*Stokes: */
55 IS isv=NULL;
56 IS isp=NULL;
57
58 #if _PETSC_MAJOR_ >= 3
59 char ksp_type[50];
60 #endif
61
62 /*Display message*/
63 #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
64 if(VerboseSolver())PetscOptionsPrint(stdout);
65 #else
66 if(VerboseSolver())PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD);
67 #endif
68
69 /*First, check that f-set is not NULL, i.e. model is fully constrained:*/
70 _assert_(Kff);
71 MatGetSize(Kff,&global_m,&global_n); _assert_(global_m==global_n);
72 if(!global_n){
73 *puf=NewVec(0,IssmComm::GetComm()); return;
74 }
75
76 /*Initial guess */
77 /*Now, check that we are not giving an initial guess to the solver, if we are running a direct solver: */
78 #if _PETSC_MAJOR_ >= 3
79 PetscOptionsGetString(PETSC_NULL,"-ksp_type",ksp_type,49,&flg);
80 if (strcmp(ksp_type,"preonly")==0)uf0=NULL;
81 #endif
82
83 /*If initial guess for the solution exists, use it to create uf, otherwise,
84 * duplicate the right hand side so that the solution vector has the same structure*/
85 if(uf0){
86 VecDuplicate(uf0,&uf); VecCopy(uf0,uf);
87 }
88 else{
89 MatGetLocalSize(Kff,&local_m,&local_n);uf=NewVec(local_n,IssmComm::GetComm(),fromlocalsize);
90 }
91
92 /*Process petsc options to see if we are using special types of external solvers*/
93 PetscOptionsDetermineSolverType(&solver_type);
94
95 /*Check the solver is available*/
96 if(solver_type==MUMPSPACKAGE_LU || solver_type==MUMPSPACKAGE_CHOL){
97 #if _PETSC_MAJOR_ >=3
98 #ifndef _HAVE_MUMPS_
99 _error_("requested MUMPS solver, which was not compiled into ISSM!\n");
100 #endif
101 #endif
102 }
103
104 /*Prepare solver*/
105 KSPCreate(IssmComm::GetComm(),&ksp);
106 KSPSetOperators(ksp,Kff,Kff,DIFFERENT_NONZERO_PATTERN);
107 KSPSetFromOptions(ksp);
108
109 #if _PETSC_MAJOR_==3
110 /*Specific solver?: */
111 KSPGetPC(ksp,&pc);
112 if (solver_type==MUMPSPACKAGE_LU){
113 #if _PETSC_MINOR_==1
114 PCFactorSetMatSolverPackage(pc,MAT_SOLVER_MUMPS);
115 #else
116 PCFactorSetMatSolverPackage(pc,MATSOLVERMUMPS);
117 #endif
118 }
119
120 /*Stokes: */
121 if (solver_type==StokesSolverEnum){
122 /*Make indices out of doftypes: */
123 if(!df)_error_("need doftypes for Stokes solver!\n");
124 DofTypesToIndexSet(&isv,&isp,df,StokesSolverEnum);
125
126 /*Set field splits: */
127 KSPGetPC(ksp,&pc);
128 #if _PETSC_MINOR_==1
129 PCFieldSplitSetIS(pc,isv);
130 PCFieldSplitSetIS(pc,isp);
131 #else
132 PCFieldSplitSetIS(pc,PETSC_NULL,isv);
133 PCFieldSplitSetIS(pc,PETSC_NULL,isp);
134 #endif
135
136 }
137 #endif
138
139 /*If there is an initial guess for the solution, use it
140 * except if we are using the MUMPS direct solver
141 * where any initial guess will crash Petsc*/
142 if (uf0){
143 if((solver_type!=MUMPSPACKAGE_LU) && (solver_type!=MUMPSPACKAGE_CHOL) && (solver_type!=SPOOLESPACKAGE_LU) && (solver_type!=SPOOLESPACKAGE_CHOL) && (solver_type!=SUPERLUDISTPACKAGE)){
144 KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
145 }
146 }
147
148 /*Solve: */
149 if(VerboseSolver())KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
150 KSPSolve(ksp,pf,uf);
151
152 /*Check convergence*/
153 KSPGetIterationNumber(ksp,&iteration_number);
154 if (iteration_number<0) _error_("Solver diverged at iteration number: " << -iteration_number);
155
156 /*Free resources:*/
157 KSPFree(&ksp);
158
159 /*Assign output pointers:*/
160 *puf=uf;
161}
162/*}}}*/
163void DofTypesToIndexSet(IS* pisv, IS* pisp, Vec df,int typeenum){ /*{{{*/
164
165 /*output: */
166 IS isv=NULL;
167 IS isp=NULL;
168
169 int start,end;
170 IssmDouble* df_local=NULL;
171 int df_local_size;
172 int i;
173
174 int* pressure_indices=NULL;
175 int* velocity_indices=NULL;
176 int pressure_num=0;
177 int velocity_num=0;
178 int pressure_count=0;
179 int velocity_count=0;
180
181 if(typeenum==StokesSolverEnum){
182
183 /*Ok, recover doftypes vector values and indices: */
184 VecGetOwnershipRange(df,&start,&end);
185 VecGetLocalSize(df,&df_local_size);
186 VecGetArray(df,&df_local);
187
188 pressure_num=0;
189 velocity_num=0;
190 for(i=0;i<df_local_size;i++){
191 if (df_local[i]==PressureEnum)pressure_num++;
192 else velocity_num++;
193 }
194
195 /*Allocate indices: */
196 if(pressure_num)pressure_indices=xNew<int>(pressure_num);
197 if(velocity_num)velocity_indices=xNew<int>(velocity_num);
198
199 pressure_count=0;
200 velocity_count=0;
201 for(i=0;i<df_local_size;i++){
202 if (df_local[i]==PressureEnum){
203 pressure_indices[pressure_count]=start+i;
204 pressure_count++;
205 }
206 if (df_local[i]==VelocityEnum){
207 velocity_indices[velocity_count]=start+i;
208 velocity_count++;
209 }
210 }
211 VecRestoreArray(df,&df_local);
212
213 /*Create indices sets: */
214 #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
215 ISCreateGeneral(IssmComm::GetComm(),pressure_num,pressure_indices,&isp);
216 ISCreateGeneral(IssmComm::GetComm(),velocity_num,velocity_indices,&isv);
217 #else
218 ISCreateGeneral(IssmComm::GetComm(),pressure_num,pressure_indices,PETSC_COPY_VALUES,&isp);
219 ISCreateGeneral(IssmComm::GetComm(),velocity_num,velocity_indices,PETSC_COPY_VALUES,&isv);
220 #endif
221 }
222
223 /*Free ressources:*/
224 xDelete<int>(pressure_indices);
225 xDelete<int>(velocity_indices);
226
227 /*Assign output pointers:*/
228 *pisv=isv;
229 *pisp=isp;
230}
231/*}}}*/
Note: See TracBrowser for help on using the repository browser.