xref: /petsc/src/snes/impls/composite/snescomposite.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
1 
2 /*
3       Defines a SNES that can consist of a collection of SNESes
4 */
5 #include <petsc/private/snesimpl.h> /*I "petscsnes.h" I*/
6 #include <petscblaslapack.h>
7 
8 const char *const        SNESCompositeTypes[]   = {"ADDITIVE","MULTIPLICATIVE","ADDITIVEOPTIMAL","SNESCompositeType","SNES_COMPOSITE",0};
9 
10 typedef struct _SNES_CompositeLink *SNES_CompositeLink;
11 struct _SNES_CompositeLink {
12   SNES               snes;
13   PetscReal          dmp;
14   Vec                X;
15   SNES_CompositeLink next;
16   SNES_CompositeLink previous;
17 };
18 
19 typedef struct {
20   SNES_CompositeLink head;
21   PetscInt           nsnes;
22   SNESCompositeType  type;
23   Vec                Xorig;
24   PetscInt           innerFailures; /* the number of inner failures we've seen */
25 
26   /* context for ADDITIVEOPTIMAL */
27   Vec                *Xes,*Fes;      /* solution and residual vectors for the subsolvers */
28   PetscReal          *fnorms;        /* norms of the solutions */
29   PetscScalar        *h;             /* the matrix formed as q_ij = (rdot_i, rdot_j) */
30   PetscScalar        *g;             /* the dotproducts of the previous function with the candidate functions */
31   PetscBLASInt       n;              /* matrix dimension -- nsnes */
32   PetscBLASInt       nrhs;           /* the number of right hand sides */
33   PetscBLASInt       lda;            /* the padded matrix dimension */
34   PetscBLASInt       ldb;            /* the padded vector dimension */
35   PetscReal          *s;             /* the singular values */
36   PetscScalar        *beta;          /* the RHS and combination */
37   PetscReal          rcond;          /* the exit condition */
38   PetscBLASInt       rank;           /* the effective rank */
39   PetscScalar        *work;          /* the work vector */
40   PetscReal          *rwork;         /* the real work vector used for complex */
41   PetscBLASInt       lwork;          /* the size of the work vector */
42   PetscBLASInt       info;           /* the output condition */
43 
44   PetscReal          rtol;           /* restart tolerance for accepting the combination */
45   PetscReal          stol;           /* restart tolerance for the combination */
46 } SNES_Composite;
47 
48 #undef __FUNCT__
49 #define __FUNCT__ "SNESCompositeApply_Multiplicative"
50 static PetscErrorCode SNESCompositeApply_Multiplicative(SNES snes,Vec X,Vec B,Vec F,PetscReal *fnorm)
51 {
52   PetscErrorCode      ierr;
53   SNES_Composite      *jac = (SNES_Composite*)snes->data;
54   SNES_CompositeLink  next = jac->head;
55   Vec                 FSub;
56   SNESConvergedReason reason;
57 
58   PetscFunctionBegin;
59   if (!next) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"No composite SNESes supplied via SNESCompositeAddSNES() or -snes_composite_sneses");
60   if (snes->normschedule == SNES_NORM_ALWAYS) {
61     ierr = SNESSetInitialFunction(next->snes,F);CHKERRQ(ierr);
62   }
63   ierr = SNESSolve(next->snes,B,X);CHKERRQ(ierr);
64   ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
65   if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
66     jac->innerFailures++;
67     if (jac->innerFailures >= snes->maxFailures) {
68       snes->reason = SNES_DIVERGED_INNER;
69       PetscFunctionReturn(0);
70     }
71   }
72 
73   while (next->next) {
74     /* only copy the function over in the case where the functions correspond */
75     if (next->snes->pcside == PC_RIGHT && next->snes->normschedule != SNES_NORM_NONE) {
76       ierr = SNESGetFunction(next->snes,&FSub,NULL,NULL);CHKERRQ(ierr);
77       next = next->next;
78       ierr = SNESSetInitialFunction(next->snes,FSub);CHKERRQ(ierr);
79     } else {
80       next = next->next;
81     }
82     ierr = SNESSolve(next->snes,B,X);CHKERRQ(ierr);
83     ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
84     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
85       jac->innerFailures++;
86       if (jac->innerFailures >= snes->maxFailures) {
87         snes->reason = SNES_DIVERGED_INNER;
88         PetscFunctionReturn(0);
89       }
90     }
91   }
92   if (next->snes->pcside == PC_RIGHT) {
93     ierr = SNESGetFunction(next->snes,&FSub,NULL,NULL);CHKERRQ(ierr);
94     ierr = VecCopy(FSub,F);CHKERRQ(ierr);
95     if (fnorm) {
96       if (snes->xl && snes->xu) {
97         ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, fnorm);CHKERRQ(ierr);
98       } else {
99         ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
100       }
101       SNESCheckFunctionNorm(snes,*fnorm);
102     }
103   } else if (snes->normschedule == SNES_NORM_ALWAYS) {
104     ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
105     if (fnorm) {
106       if (snes->xl && snes->xu) {
107         ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, fnorm);CHKERRQ(ierr);
108       } else {
109         ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
110       }
111       SNESCheckFunctionNorm(snes,*fnorm);
112     }
113   }
114   PetscFunctionReturn(0);
115 }
116 
117 #undef __FUNCT__
118 #define __FUNCT__ "SNESCompositeApply_Additive"
119 static PetscErrorCode SNESCompositeApply_Additive(SNES snes,Vec X,Vec B,Vec F,PetscReal *fnorm)
120 {
121   PetscErrorCode      ierr;
122   SNES_Composite      *jac = (SNES_Composite*)snes->data;
123   SNES_CompositeLink  next = jac->head;
124   Vec                 Y,Xorig;
125   SNESConvergedReason reason;
126 
127   PetscFunctionBegin;
128   Y = snes->vec_sol_update;
129   if (!jac->Xorig) {ierr = VecDuplicate(X,&jac->Xorig);CHKERRQ(ierr);}
130   Xorig = jac->Xorig;
131   ierr = VecCopy(X,Xorig);CHKERRQ(ierr);
132   if (!next) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"No composite SNESes supplied via SNESCompositeAddSNES() or -snes_composite_sneses");
133   if (snes->normschedule == SNES_NORM_ALWAYS) {
134     ierr = SNESSetInitialFunction(next->snes,F);CHKERRQ(ierr);
135     while (next->next) {
136       next = next->next;
137       ierr = SNESSetInitialFunction(next->snes,F);CHKERRQ(ierr);
138     }
139   }
140   next = jac->head;
141   ierr = VecCopy(Xorig,Y);CHKERRQ(ierr);
142   ierr = SNESSolve(next->snes,B,Y);CHKERRQ(ierr);
143   ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
144   if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
145     jac->innerFailures++;
146     if (jac->innerFailures >= snes->maxFailures) {
147       snes->reason = SNES_DIVERGED_INNER;
148       PetscFunctionReturn(0);
149     }
150   }
151   ierr = VecAXPY(Y,-1.0,Xorig);CHKERRQ(ierr);
152   ierr = VecAXPY(X,next->dmp,Y);CHKERRQ(ierr);
153   while (next->next) {
154     next = next->next;
155     ierr = VecCopy(Xorig,Y);CHKERRQ(ierr);
156     ierr = SNESSolve(next->snes,B,Y);CHKERRQ(ierr);
157     ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
158     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
159       jac->innerFailures++;
160       if (jac->innerFailures >= snes->maxFailures) {
161         snes->reason = SNES_DIVERGED_INNER;
162         PetscFunctionReturn(0);
163       }
164     }
165     ierr = VecAXPY(Y,-1.0,Xorig);CHKERRQ(ierr);
166     ierr = VecAXPY(X,next->dmp,Y);CHKERRQ(ierr);
167   }
168   if (snes->normschedule == SNES_NORM_ALWAYS) {
169     ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
170     if (fnorm) {
171       if (snes->xl && snes->xu) {
172         ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, fnorm);CHKERRQ(ierr);
173       } else {
174         ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
175       }
176       SNESCheckFunctionNorm(snes,*fnorm);
177     }
178   }
179   PetscFunctionReturn(0);
180 }
181 
182 #undef __FUNCT__
183 #define __FUNCT__ "SNESCompositeApply_AdditiveOptimal"
184 /* approximately solve the overdetermined system:
185 
186  2*F(x_i)\cdot F(\x_j)\alpha_i = 0
187  \alpha_i                      = 1
188 
189  Which minimizes the L2 norm of the linearization of:
190  ||F(\sum_i \alpha_i*x_i)||^2
191 
192  With the constraint that \sum_i\alpha_i = 1
193  Where x_i is the solution from the ith subsolver.
194  */
195 static PetscErrorCode SNESCompositeApply_AdditiveOptimal(SNES snes,Vec X,Vec B,Vec F,PetscReal *fnorm)
196 {
197   PetscErrorCode      ierr;
198   SNES_Composite      *jac = (SNES_Composite*)snes->data;
199   SNES_CompositeLink  next = jac->head;
200   Vec                 *Xes = jac->Xes,*Fes = jac->Fes;
201   PetscInt            i,j;
202   PetscScalar         tot,total,ftf;
203   PetscReal           min_fnorm;
204   PetscInt            min_i;
205   SNESConvergedReason reason;
206 
207   PetscFunctionBegin;
208   if (!next) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"No composite SNESes supplied via SNESCompositeAddSNES() or -snes_composite_sneses");
209 
210   if (snes->normschedule == SNES_NORM_ALWAYS) {
211     next = jac->head;
212     ierr = SNESSetInitialFunction(next->snes,F);CHKERRQ(ierr);
213     while (next->next) {
214       next = next->next;
215       ierr = SNESSetInitialFunction(next->snes,F);CHKERRQ(ierr);
216     }
217   }
218 
219   next = jac->head;
220   i = 0;
221   ierr = VecCopy(X,Xes[i]);CHKERRQ(ierr);
222   ierr = SNESSolve(next->snes,B,Xes[i]);CHKERRQ(ierr);
223   ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
224   if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
225     jac->innerFailures++;
226     if (jac->innerFailures >= snes->maxFailures) {
227       snes->reason = SNES_DIVERGED_INNER;
228       PetscFunctionReturn(0);
229     }
230   }
231   while (next->next) {
232     i++;
233     next = next->next;
234     ierr = VecCopy(X,Xes[i]);CHKERRQ(ierr);
235     ierr = SNESSolve(next->snes,B,Xes[i]);CHKERRQ(ierr);
236     ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
237     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
238       jac->innerFailures++;
239       if (jac->innerFailures >= snes->maxFailures) {
240         snes->reason = SNES_DIVERGED_INNER;
241         PetscFunctionReturn(0);
242       }
243     }
244   }
245 
246   /* all the solutions are collected; combine optimally */
247   for (i=0;i<jac->n;i++) {
248     for (j=0;j<i+1;j++) {
249       ierr = VecDotBegin(Fes[i],Fes[j],&jac->h[i + j*jac->n]);CHKERRQ(ierr);
250     }
251     ierr = VecDotBegin(Fes[i],F,&jac->g[i]);CHKERRQ(ierr);
252   }
253 
254   for (i=0;i<jac->n;i++) {
255     for (j=0;j<i+1;j++) {
256       ierr = VecDotEnd(Fes[i],Fes[j],&jac->h[i + j*jac->n]);CHKERRQ(ierr);
257       if (i == j) jac->fnorms[i] = PetscSqrtReal(PetscRealPart(jac->h[i + j*jac->n]));
258     }
259     ierr = VecDotEnd(Fes[i],F,&jac->g[i]);CHKERRQ(ierr);
260   }
261 
262   ftf = (*fnorm)*(*fnorm);
263 
264   for (i=0; i<jac->n; i++) {
265     for (j=i+1;j<jac->n;j++) {
266       jac->h[i + j*jac->n] = jac->h[j + i*jac->n];
267     }
268   }
269 
270   for (i=0; i<jac->n; i++) {
271     for (j=0; j<jac->n; j++) {
272       jac->h[i + j*jac->n] = jac->h[i + j*jac->n] - jac->g[j] - jac->g[i] + ftf;
273     }
274     jac->beta[i] = ftf - jac->g[i];
275   }
276 
277 #if defined(PETSC_MISSING_LAPACK_GELSS)
278   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"SNESCOMPOSITE with ADDITIVEOPTIMAL requires the LAPACK GELSS routine.");
279 #else
280   jac->info  = 0;
281   jac->rcond = -1.;
282   ierr          = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
283 #if defined(PETSC_USE_COMPLEX)
284   PetscStackCall("LAPACKgelss",LAPACKgelss_(&jac->n,&jac->n,&jac->nrhs,jac->h,&jac->lda,jac->beta,&jac->lda,jac->s,&jac->rcond,&jac->rank,jac->work,&jac->lwork,jac->rwork,&jac->info));
285 #else
286   PetscStackCall("LAPACKgelss",LAPACKgelss_(&jac->n,&jac->n,&jac->nrhs,jac->h,&jac->lda,jac->beta,&jac->lda,jac->s,&jac->rcond,&jac->rank,jac->work,&jac->lwork,&jac->info));
287 #endif
288   ierr = PetscFPTrapPop();CHKERRQ(ierr);
289   if (jac->info < 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"Bad argument to GELSS");
290   if (jac->info > 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD failed to converge");
291 #endif
292   tot = 0.;
293   total = 0.;
294   for (i=0; i<jac->n; i++) {
295     if (snes->errorifnotconverged && PetscIsInfOrNanScalar(jac->beta[i])) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD generated inconsistent output");
296     ierr = PetscInfo2(snes,"%D: %g\n",i,(double)PetscRealPart(jac->beta[i]));CHKERRQ(ierr);
297     tot += jac->beta[i];
298     total += PetscAbsScalar(jac->beta[i]);
299   }
300   ierr = VecScale(X,(1. - tot));CHKERRQ(ierr);
301   ierr = VecMAXPY(X,jac->n,jac->beta,Xes);CHKERRQ(ierr);
302   ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
303 
304   if (snes->xl && snes->xu) {
305     ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, fnorm);CHKERRQ(ierr);
306   } else {
307     ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
308   }
309 
310   /* take the minimum-normed candidate if it beats the combination by a factor of rtol or the combination has stagnated */
311   min_fnorm = jac->fnorms[0];
312   min_i     = 0;
313   for (i=0; i<jac->n; i++) {
314     if (jac->fnorms[i] < min_fnorm) {
315       min_fnorm = jac->fnorms[i];
316       min_i     = i;
317     }
318   }
319 
320   /* stagnation or divergence restart to the solution of the solver that failed the least */
321   if (PetscRealPart(total) < jac->stol || min_fnorm*jac->rtol < *fnorm) {
322     ierr = VecCopy(jac->Xes[min_i],X);CHKERRQ(ierr);
323     ierr = VecCopy(jac->Fes[min_i],F);CHKERRQ(ierr);
324     *fnorm = min_fnorm;
325   }
326   PetscFunctionReturn(0);
327 }
328 
329 #undef __FUNCT__
330 #define __FUNCT__ "SNESSetUp_Composite"
331 static PetscErrorCode SNESSetUp_Composite(SNES snes)
332 {
333   PetscErrorCode     ierr;
334   DM                 dm;
335   SNES_Composite     *jac = (SNES_Composite*)snes->data;
336   SNES_CompositeLink next = jac->head;
337   PetscInt           n=0,i;
338   Vec                F;
339 
340   PetscFunctionBegin;
341   ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
342 
343   if (snes->ops->computevariablebounds) {
344     /* SNESVI only ever calls computevariablebounds once, so calling it once here is justified */
345     if (!snes->xl) {ierr = VecDuplicate(snes->vec_sol,&snes->xl);CHKERRQ(ierr);}
346     if (!snes->xu) {ierr = VecDuplicate(snes->vec_sol,&snes->xu);CHKERRQ(ierr);}
347     ierr = (*snes->ops->computevariablebounds)(snes,snes->xl,snes->xu);CHKERRQ(ierr);
348   }
349 
350   while (next) {
351     n++;
352     ierr = SNESSetDM(next->snes,dm);CHKERRQ(ierr);
353     ierr = SNESSetFromOptions(next->snes);CHKERRQ(ierr);
354     ierr = SNESSetApplicationContext(next->snes, snes->user);CHKERRQ(ierr);
355     if (snes->xl && snes->xu) {
356       if (snes->ops->computevariablebounds) {
357         ierr = SNESVISetComputeVariableBounds(next->snes, snes->ops->computevariablebounds);CHKERRQ(ierr);
358       } else {
359         ierr = SNESVISetVariableBounds(next->snes,snes->xl,snes->xu);CHKERRQ(ierr);
360       }
361     }
362 
363     next = next->next;
364   }
365   jac->nsnes = n;
366   ierr = SNESGetFunction(snes,&F,NULL,NULL);CHKERRQ(ierr);
367   if (jac->type == SNES_COMPOSITE_ADDITIVEOPTIMAL) {
368     ierr = VecDuplicateVecs(F,jac->nsnes,&jac->Xes);CHKERRQ(ierr);
369     ierr = PetscMalloc1(n,&jac->Fes);CHKERRQ(ierr);
370     ierr = PetscMalloc1(n,&jac->fnorms);CHKERRQ(ierr);
371     next = jac->head;
372     i = 0;
373     while (next) {
374       ierr = SNESGetFunction(next->snes,&F,NULL,NULL);CHKERRQ(ierr);
375       jac->Fes[i] = F;
376       ierr = PetscObjectReference((PetscObject)F);CHKERRQ(ierr);
377       next = next->next;
378       i++;
379     }
380     /* allocate the subspace direct solve area */
381     jac->nrhs  = 1;
382     jac->lda   = jac->nsnes;
383     jac->ldb   = jac->nsnes;
384     jac->n     = jac->nsnes;
385 
386     ierr = PetscMalloc1(jac->n*jac->n,&jac->h);CHKERRQ(ierr);
387     ierr = PetscMalloc1(jac->n,&jac->beta);CHKERRQ(ierr);
388     ierr = PetscMalloc1(jac->n,&jac->s);CHKERRQ(ierr);
389     ierr = PetscMalloc1(jac->n,&jac->g);CHKERRQ(ierr);
390     jac->lwork = 12*jac->n;
391 #if PETSC_USE_COMPLEX
392     ierr = PetscMalloc1(jac->lwork,&jac->rwork);CHKERRQ(ierr);
393 #endif
394     ierr = PetscMalloc1(jac->lwork,&jac->work);CHKERRQ(ierr);
395   }
396 
397   PetscFunctionReturn(0);
398 }
399 
400 #undef __FUNCT__
401 #define __FUNCT__ "SNESReset_Composite"
402 static PetscErrorCode SNESReset_Composite(SNES snes)
403 {
404   SNES_Composite     *jac = (SNES_Composite*)snes->data;
405   PetscErrorCode   ierr;
406   SNES_CompositeLink next = jac->head;
407 
408   PetscFunctionBegin;
409   while (next) {
410     ierr = SNESReset(next->snes);CHKERRQ(ierr);
411     next = next->next;
412   }
413   ierr = VecDestroy(&jac->Xorig);CHKERRQ(ierr);
414   if (jac->Xes) {ierr = VecDestroyVecs(jac->nsnes,&jac->Xes);CHKERRQ(ierr);}
415   if (jac->Fes) {ierr = VecDestroyVecs(jac->nsnes,&jac->Fes);CHKERRQ(ierr);}
416   ierr = PetscFree(jac->fnorms);CHKERRQ(ierr);
417   ierr = PetscFree(jac->h);CHKERRQ(ierr);
418   ierr = PetscFree(jac->s);CHKERRQ(ierr);
419   ierr = PetscFree(jac->g);CHKERRQ(ierr);
420   ierr = PetscFree(jac->beta);CHKERRQ(ierr);
421   ierr = PetscFree(jac->work);CHKERRQ(ierr);
422   ierr = PetscFree(jac->rwork);CHKERRQ(ierr);
423   PetscFunctionReturn(0);
424 }
425 
426 #undef __FUNCT__
427 #define __FUNCT__ "SNESDestroy_Composite"
428 static PetscErrorCode SNESDestroy_Composite(SNES snes)
429 {
430   SNES_Composite     *jac = (SNES_Composite*)snes->data;
431   PetscErrorCode   ierr;
432   SNES_CompositeLink next = jac->head,next_tmp;
433 
434   PetscFunctionBegin;
435   ierr = SNESReset_Composite(snes);CHKERRQ(ierr);
436   while (next) {
437     ierr     = SNESDestroy(&next->snes);CHKERRQ(ierr);
438     next_tmp = next;
439     next     = next->next;
440     ierr     = PetscFree(next_tmp);CHKERRQ(ierr);
441   }
442   ierr = PetscFree(snes->data);CHKERRQ(ierr);
443   PetscFunctionReturn(0);
444 }
445 
446 #undef __FUNCT__
447 #define __FUNCT__ "SNESSetFromOptions_Composite"
448 static PetscErrorCode SNESSetFromOptions_Composite(PetscOptions *PetscOptionsObject,SNES snes)
449 {
450   SNES_Composite     *jac = (SNES_Composite*)snes->data;
451   PetscErrorCode     ierr;
452   PetscInt           nmax = 8,i;
453   SNES_CompositeLink next;
454   char               *sneses[8];
455   PetscReal          dmps[8];
456   PetscBool          flg;
457 
458   PetscFunctionBegin;
459   ierr = PetscOptionsHead(PetscOptionsObject,"Composite preconditioner options");CHKERRQ(ierr);
460   ierr = PetscOptionsEnum("-snes_composite_type","Type of composition","SNESCompositeSetType",SNESCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&jac->type,&flg);CHKERRQ(ierr);
461   if (flg) {
462     ierr = SNESCompositeSetType(snes,jac->type);CHKERRQ(ierr);
463   }
464   ierr = PetscOptionsStringArray("-snes_composite_sneses","List of composite solvers","SNESCompositeAddSNES",sneses,&nmax,&flg);CHKERRQ(ierr);
465   if (flg) {
466     for (i=0; i<nmax; i++) {
467       ierr = SNESCompositeAddSNES(snes,sneses[i]);CHKERRQ(ierr);
468       ierr = PetscFree(sneses[i]);CHKERRQ(ierr);   /* deallocate string sneses[i], which is allocated in PetscOptionsStringArray() */
469     }
470   }
471   ierr = PetscOptionsRealArray("-snes_composite_damping","Damping of the additive composite solvers","SNESCompositeSetDamping",dmps,&nmax,&flg);CHKERRQ(ierr);
472   if (flg) {
473     for (i=0; i<nmax; i++) {
474       ierr = SNESCompositeSetDamping(snes,i,dmps[i]);CHKERRQ(ierr);
475     }
476   }
477   ierr = PetscOptionsReal("-snes_composite_stol","Step tolerance for restart on the additive composite solvers","",jac->stol,&jac->stol,NULL);CHKERRQ(ierr);
478   ierr = PetscOptionsReal("-snes_composite_rtol","Residual tolerance for the additive composite solvers","",jac->rtol,&jac->rtol,NULL);CHKERRQ(ierr);
479   ierr = PetscOptionsTail();CHKERRQ(ierr);
480 
481   next = jac->head;
482   while (next) {
483     ierr = SNESSetFromOptions(next->snes);CHKERRQ(ierr);
484     next = next->next;
485   }
486   PetscFunctionReturn(0);
487 }
488 
489 #undef __FUNCT__
490 #define __FUNCT__ "SNESView_Composite"
491 static PetscErrorCode SNESView_Composite(SNES snes,PetscViewer viewer)
492 {
493   SNES_Composite     *jac = (SNES_Composite*)snes->data;
494   PetscErrorCode   ierr;
495   SNES_CompositeLink next = jac->head;
496   PetscBool        iascii;
497 
498   PetscFunctionBegin;
499   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
500   if (iascii) {
501     ierr = PetscViewerASCIIPrintf(viewer,"Composite SNES type - %s\n",SNESCompositeTypes[jac->type]);CHKERRQ(ierr);
502     ierr = PetscViewerASCIIPrintf(viewer,"SNESes on composite preconditioner follow\n");CHKERRQ(ierr);
503     ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr);
504   }
505   if (iascii) {
506     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
507   }
508   while (next) {
509     ierr = SNESView(next->snes,viewer);CHKERRQ(ierr);
510     next = next->next;
511   }
512   if (iascii) {
513     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
514     ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr);
515   }
516   PetscFunctionReturn(0);
517 }
518 
519 /* ------------------------------------------------------------------------------*/
520 
521 #undef __FUNCT__
522 #define __FUNCT__ "SNESCompositeSetType_Composite"
523 static PetscErrorCode  SNESCompositeSetType_Composite(SNES snes,SNESCompositeType type)
524 {
525   SNES_Composite *jac = (SNES_Composite*)snes->data;
526 
527   PetscFunctionBegin;
528   jac->type = type;
529   PetscFunctionReturn(0);
530 }
531 
532 #undef __FUNCT__
533 #define __FUNCT__ "SNESCompositeAddSNES_Composite"
534 static PetscErrorCode  SNESCompositeAddSNES_Composite(SNES snes,SNESType type)
535 {
536   SNES_Composite     *jac;
537   SNES_CompositeLink next,ilink;
538   PetscErrorCode   ierr;
539   PetscInt         cnt = 0;
540   const char       *prefix;
541   char             newprefix[8];
542   DM               dm;
543 
544   PetscFunctionBegin;
545   ierr        = PetscNewLog(snes,&ilink);CHKERRQ(ierr);
546   ilink->next = 0;
547   ierr        = SNESCreate(PetscObjectComm((PetscObject)snes),&ilink->snes);CHKERRQ(ierr);
548   ierr        = PetscLogObjectParent((PetscObject)snes,(PetscObject)ilink->snes);CHKERRQ(ierr);
549   ierr        = SNESGetDM(snes,&dm);CHKERRQ(ierr);
550   ierr        = SNESSetDM(ilink->snes,dm);CHKERRQ(ierr);
551   ierr        = SNESSetTolerances(ilink->snes,ilink->snes->abstol,ilink->snes->rtol,ilink->snes->stol,1,ilink->snes->max_funcs);CHKERRQ(ierr);
552   jac  = (SNES_Composite*)snes->data;
553   next = jac->head;
554   if (!next) {
555     jac->head       = ilink;
556     ilink->previous = NULL;
557   } else {
558     cnt++;
559     while (next->next) {
560       next = next->next;
561       cnt++;
562     }
563     next->next      = ilink;
564     ilink->previous = next;
565   }
566   ierr = SNESGetOptionsPrefix(snes,&prefix);CHKERRQ(ierr);
567   ierr = SNESSetOptionsPrefix(ilink->snes,prefix);CHKERRQ(ierr);
568   sprintf(newprefix,"sub_%d_",(int)cnt);
569   ierr = SNESAppendOptionsPrefix(ilink->snes,newprefix);CHKERRQ(ierr);
570   ierr = PetscObjectIncrementTabLevel((PetscObject)ilink->snes,(PetscObject)snes,1);CHKERRQ(ierr);
571   ierr = SNESSetType(ilink->snes,type);CHKERRQ(ierr);
572   ierr = SNESSetNormSchedule(ilink->snes, SNES_NORM_FINAL_ONLY);CHKERRQ(ierr);
573 
574   ilink->dmp = 1.0;
575   jac->nsnes++;
576   PetscFunctionReturn(0);
577 }
578 
579 #undef __FUNCT__
580 #define __FUNCT__ "SNESCompositeGetSNES_Composite"
581 static PetscErrorCode  SNESCompositeGetSNES_Composite(SNES snes,PetscInt n,SNES *subsnes)
582 {
583   SNES_Composite     *jac;
584   SNES_CompositeLink next;
585   PetscInt         i;
586 
587   PetscFunctionBegin;
588   jac  = (SNES_Composite*)snes->data;
589   next = jac->head;
590   for (i=0; i<n; i++) {
591     if (!next->next) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"Not enough SNESes in composite preconditioner");
592     next = next->next;
593   }
594   *subsnes = next->snes;
595   PetscFunctionReturn(0);
596 }
597 
598 /* -------------------------------------------------------------------------------- */
599 #undef __FUNCT__
600 #define __FUNCT__ "SNESCompositeSetType"
601 /*@C
602    SNESCompositeSetType - Sets the type of composite preconditioner.
603 
604    Logically Collective on SNES
605 
606    Input Parameter:
607 +  snes - the preconditioner context
608 -  type - SNES_COMPOSITE_ADDITIVE (default), SNES_COMPOSITE_MULTIPLICATIVE
609 
610    Options Database Key:
611 .  -snes_composite_type <type: one of multiplicative, additive, special> - Sets composite preconditioner type
612 
613    Level: Developer
614 
615 .keywords: SNES, set, type, composite preconditioner, additive, multiplicative
616 @*/
617 PetscErrorCode  SNESCompositeSetType(SNES snes,SNESCompositeType type)
618 {
619   PetscErrorCode ierr;
620 
621   PetscFunctionBegin;
622   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
623   PetscValidLogicalCollectiveEnum(snes,type,2);
624   ierr = PetscTryMethod(snes,"SNESCompositeSetType_C",(SNES,SNESCompositeType),(snes,type));CHKERRQ(ierr);
625   PetscFunctionReturn(0);
626 }
627 
628 #undef __FUNCT__
629 #define __FUNCT__ "SNESCompositeAddSNES"
630 /*@C
631    SNESCompositeAddSNES - Adds another SNES to the composite SNES.
632 
633    Collective on SNES
634 
635    Input Parameters:
636 +  snes - the preconditioner context
637 -  type - the type of the new preconditioner
638 
639    Level: Developer
640 
641 .keywords: SNES, composite preconditioner, add
642 @*/
643 PetscErrorCode  SNESCompositeAddSNES(SNES snes,SNESType type)
644 {
645   PetscErrorCode ierr;
646 
647   PetscFunctionBegin;
648   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
649   ierr = PetscTryMethod(snes,"SNESCompositeAddSNES_C",(SNES,SNESType),(snes,type));CHKERRQ(ierr);
650   PetscFunctionReturn(0);
651 }
652 #undef __FUNCT__
653 #define __FUNCT__ "SNESCompositeGetSNES"
654 /*@
655    SNESCompositeGetSNES - Gets one of the SNES objects in the composite SNES.
656 
657    Not Collective
658 
659    Input Parameter:
660 +  snes - the preconditioner context
661 -  n - the number of the snes requested
662 
663    Output Parameters:
664 .  subsnes - the SNES requested
665 
666    Level: Developer
667 
668 .keywords: SNES, get, composite preconditioner, sub preconditioner
669 
670 .seealso: SNESCompositeAddSNES()
671 @*/
672 PetscErrorCode  SNESCompositeGetSNES(SNES snes,PetscInt n,SNES *subsnes)
673 {
674   PetscErrorCode ierr;
675 
676   PetscFunctionBegin;
677   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
678   PetscValidPointer(subsnes,3);
679   ierr = PetscUseMethod(snes,"SNESCompositeGetSNES_C",(SNES,PetscInt,SNES*),(snes,n,subsnes));CHKERRQ(ierr);
680   PetscFunctionReturn(0);
681 }
682 
683 #undef __FUNCT__
684 #define __FUNCT__ "SNESCompositeGetNumber"
685 /*@
686    SNESCompositeGetNumber - Get the number of subsolvers in the composite SNES.
687 
688    Logically Collective on SNES
689 
690    Input Parameter:
691    snes - the preconditioner context
692 
693    Output Parameter:
694    n - the number of subsolvers
695 
696    Level: Developer
697 
698 .keywords: SNES, composite preconditioner
699 @*/
700 PetscErrorCode  SNESCompositeGetNumber(SNES snes,PetscInt *n)
701 {
702   SNES_Composite     *jac;
703   SNES_CompositeLink next;
704 
705   PetscFunctionBegin;
706   jac  = (SNES_Composite*)snes->data;
707   next = jac->head;
708 
709   *n = 0;
710   while (next) {
711     *n = *n + 1;
712     next = next->next;
713   }
714   PetscFunctionReturn(0);
715 }
716 
717 #undef __FUNCT__
718 #define __FUNCT__ "SNESCompositeSetDamping_Composite"
719 static PetscErrorCode  SNESCompositeSetDamping_Composite(SNES snes,PetscInt n,PetscReal dmp)
720 {
721   SNES_Composite     *jac;
722   SNES_CompositeLink next;
723   PetscInt         i;
724 
725   PetscFunctionBegin;
726   jac  = (SNES_Composite*)snes->data;
727   next = jac->head;
728   for (i=0; i<n; i++) {
729     if (!next->next) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"Not enough SNESes in composite preconditioner");
730     next = next->next;
731   }
732   next->dmp = dmp;
733   PetscFunctionReturn(0);
734 }
735 
736 #undef __FUNCT__
737 #define __FUNCT__ "SNESCompositeSetDamping"
738 /*@
739    SNESCompositeSetDamping - Sets the damping of a subsolver when using additive composite SNES.
740 
741    Not Collective
742 
743    Input Parameter:
744 +  snes - the preconditioner context
745 .  n - the number of the snes requested
746 -  dmp - the damping
747 
748    Level: Developer
749 
750 .keywords: SNES, get, composite preconditioner, sub preconditioner
751 
752 .seealso: SNESCompositeAddSNES()
753 @*/
754 PetscErrorCode  SNESCompositeSetDamping(SNES snes,PetscInt n,PetscReal dmp)
755 {
756   PetscErrorCode ierr;
757 
758   PetscFunctionBegin;
759   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
760   ierr = PetscUseMethod(snes,"SNESCompositeSetDamping_C",(SNES,PetscInt,PetscReal),(snes,n,dmp));CHKERRQ(ierr);
761   PetscFunctionReturn(0);
762 }
763 
764 #undef __FUNCT__
765 #define __FUNCT__ "SNESSolve_Composite"
766 PetscErrorCode SNESSolve_Composite(SNES snes)
767 {
768   Vec            F;
769   Vec            X;
770   Vec            B;
771   PetscInt       i;
772   PetscReal      fnorm = 0.0, xnorm = 0.0, snorm = 0.0;
773   PetscErrorCode ierr;
774   SNESNormSchedule normtype;
775   SNES_Composite *comp = (SNES_Composite*)snes->data;
776 
777   PetscFunctionBegin;
778   X = snes->vec_sol;
779   F = snes->vec_func;
780   B = snes->vec_rhs;
781 
782   ierr         = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
783   snes->iter   = 0;
784   snes->norm   = 0.;
785   comp->innerFailures = 0;
786   ierr         = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
787   ierr         = SNESSetWorkVecs(snes, 1);CHKERRQ(ierr);
788   snes->reason = SNES_CONVERGED_ITERATING;
789   ierr         = SNESGetNormSchedule(snes, &normtype);CHKERRQ(ierr);
790   if (normtype == SNES_NORM_ALWAYS || normtype == SNES_NORM_INITIAL_ONLY || normtype == SNES_NORM_INITIAL_FINAL_ONLY) {
791     if (!snes->vec_func_init_set) {
792       ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
793     } else snes->vec_func_init_set = PETSC_FALSE;
794 
795     if (snes->xl && snes->xu) {
796       ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, &fnorm);CHKERRQ(ierr);
797     } else {
798       ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm <- ||F||  */
799     }
800     SNESCheckFunctionNorm(snes,fnorm);
801     ierr       = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
802     snes->iter = 0;
803     snes->norm = fnorm;
804     ierr       = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
805     ierr       = SNESLogConvergenceHistory(snes,snes->norm,0);CHKERRQ(ierr);
806     ierr       = SNESMonitor(snes,0,snes->norm);CHKERRQ(ierr);
807 
808     /* test convergence */
809     ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
810     if (snes->reason) PetscFunctionReturn(0);
811   } else {
812     ierr = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
813     ierr = SNESLogConvergenceHistory(snes,snes->norm,0);CHKERRQ(ierr);
814     ierr = SNESMonitor(snes,0,snes->norm);CHKERRQ(ierr);
815   }
816 
817   /* Call general purpose update function */
818   if (snes->ops->update) {
819     ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
820   }
821 
822   for (i = 0; i < snes->max_its; i++) {
823     /* Copy the state before modification by application of the composite solver;
824        we will subtract the new state after application */
825     ierr = VecCopy(X, snes->work[0]);CHKERRQ(ierr);
826 
827     if (comp->type == SNES_COMPOSITE_ADDITIVE) {
828       ierr = SNESCompositeApply_Additive(snes,X,B,F,&fnorm);CHKERRQ(ierr);
829     } else if (comp->type == SNES_COMPOSITE_MULTIPLICATIVE) {
830       ierr = SNESCompositeApply_Multiplicative(snes,X,B,F,&fnorm);CHKERRQ(ierr);
831     } else if (comp->type == SNES_COMPOSITE_ADDITIVEOPTIMAL) {
832       ierr = SNESCompositeApply_AdditiveOptimal(snes,X,B,F,&fnorm);CHKERRQ(ierr);
833     } else {
834       SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Unsupported SNESComposite type");
835     }
836     if (snes->reason < 0) break;
837 
838     /* Compute the solution update for convergence testing */
839     ierr = VecAXPY(snes->work[0], -1.0, X);CHKERRQ(ierr);
840     ierr = VecScale(snes->work[0], -1.0);CHKERRQ(ierr);
841 
842     if ((i == snes->max_its - 1) && (normtype == SNES_NORM_INITIAL_FINAL_ONLY || normtype == SNES_NORM_FINAL_ONLY)) {
843       ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
844 
845       if (snes->xl && snes->xu) {
846         ierr = VecNormBegin(X, NORM_2, &xnorm);CHKERRQ(ierr);
847         ierr = VecNormBegin(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
848         ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, &fnorm);CHKERRQ(ierr);
849         ierr = VecNormEnd(X, NORM_2, &xnorm);CHKERRQ(ierr);
850         ierr = VecNormEnd(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
851       } else {
852         ierr = VecNormBegin(F, NORM_2, &fnorm);CHKERRQ(ierr);
853         ierr = VecNormBegin(X, NORM_2, &xnorm);CHKERRQ(ierr);
854         ierr = VecNormBegin(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
855 
856         ierr = VecNormEnd(F, NORM_2, &fnorm);CHKERRQ(ierr);
857         ierr = VecNormEnd(X, NORM_2, &xnorm);CHKERRQ(ierr);
858         ierr = VecNormEnd(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
859       }
860       SNESCheckFunctionNorm(snes,fnorm);
861     } else if (normtype == SNES_NORM_ALWAYS) {
862       ierr = VecNormBegin(X, NORM_2, &xnorm);CHKERRQ(ierr);
863       ierr = VecNormBegin(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
864       ierr = VecNormEnd(X, NORM_2, &xnorm);CHKERRQ(ierr);
865       ierr = VecNormEnd(snes->work[0], NORM_2, &snorm);CHKERRQ(ierr);
866     }
867     /* Monitor convergence */
868     ierr       = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr);
869     snes->iter = i+1;
870     snes->norm = fnorm;
871     ierr       = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr);
872     ierr       = SNESLogConvergenceHistory(snes,snes->norm,0);CHKERRQ(ierr);
873     ierr       = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
874     /* Test for convergence */
875     if (normtype == SNES_NORM_ALWAYS) {ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,snorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);}
876     if (snes->reason) break;
877     /* Call general purpose update function */
878     if (snes->ops->update) {ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);}
879   }
880   if (normtype == SNES_NORM_ALWAYS) {
881     if (i == snes->max_its) {
882       ierr = PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",snes->max_its);CHKERRQ(ierr);
883       if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
884     }
885   } else if (!snes->reason) snes->reason = SNES_CONVERGED_ITS;
886   PetscFunctionReturn(0);
887 }
888 
889 /* -------------------------------------------------------------------------------------------*/
890 
891 /*MC
892      SNESCOMPOSITE - Build a preconditioner by composing together several nonlinear solvers
893 
894    Options Database Keys:
895 +  -snes_composite_type <type: one of multiplicative, additive, symmetric_multiplicative, special> - Sets composite preconditioner type
896 -  -snes_composite_sneses - <snes0,snes1,...> list of SNESes to compose
897 
898    Level: intermediate
899 
900    Concepts: composing solvers
901 
902 .seealso:  SNESCreate(), SNESSetType(), SNESType (for list of available types), SNES,
903            SNESSHELL, SNESCompositeSetType(), SNESCompositeSpecialSetAlpha(), SNESCompositeAddSNES(),
904            SNESCompositeGetSNES()
905 
906 M*/
907 
908 #undef __FUNCT__
909 #define __FUNCT__ "SNESCreate_Composite"
910 PETSC_EXTERN PetscErrorCode SNESCreate_Composite(SNES snes)
911 {
912   PetscErrorCode ierr;
913   SNES_Composite   *jac;
914 
915   PetscFunctionBegin;
916   ierr = PetscNewLog(snes,&jac);CHKERRQ(ierr);
917 
918   snes->ops->solve           = SNESSolve_Composite;
919   snes->ops->setup           = SNESSetUp_Composite;
920   snes->ops->reset           = SNESReset_Composite;
921   snes->ops->destroy         = SNESDestroy_Composite;
922   snes->ops->setfromoptions  = SNESSetFromOptions_Composite;
923   snes->ops->view            = SNESView_Composite;
924 
925   snes->data = (void*)jac;
926   jac->type  = SNES_COMPOSITE_ADDITIVEOPTIMAL;
927   jac->Fes   = NULL;
928   jac->Xes   = NULL;
929   jac->fnorms = NULL;
930   jac->nsnes = 0;
931   jac->head  = 0;
932   jac->stol  = 0.1;
933   jac->rtol  = 1.1;
934 
935   jac->h     = NULL;
936   jac->s     = NULL;
937   jac->beta  = NULL;
938   jac->work  = NULL;
939   jac->rwork = NULL;
940 
941   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESCompositeSetType_C",SNESCompositeSetType_Composite);CHKERRQ(ierr);
942   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESCompositeAddSNES_C",SNESCompositeAddSNES_Composite);CHKERRQ(ierr);
943   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESCompositeGetSNES_C",SNESCompositeGetSNES_Composite);CHKERRQ(ierr);
944   ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESCompositeSetDamping_C",SNESCompositeSetDamping_Composite);CHKERRQ(ierr);
945   PetscFunctionReturn(0);
946 }
947 
948