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