xref: /petsc/src/snes/impls/richardson/snesrichardson.c (revision 0b99f51463372431c8db4bb246d48fd57aaec3e2)
1 
2 #include <../src/snes/impls/richardson/snesrichardsonimpl.h>
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "SNESReset_NRichardson"
6 PetscErrorCode SNESReset_NRichardson(SNES snes)
7 {
8   PetscErrorCode ierr;
9 
10   PetscFunctionBegin;
11   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
12   PetscFunctionReturn(0);
13 }
14 
15 /*
16   SNESDestroy_NRichardson - Destroys the private SNES_NRichardson context that was created with SNESCreate_NRichardson().
17 
18   Input Parameter:
19 . snes - the SNES context
20 
21   Application Interface Routine: SNESDestroy()
22 */
23 #undef __FUNCT__
24 #define __FUNCT__ "SNESDestroy_NRichardson"
25 PetscErrorCode SNESDestroy_NRichardson(SNES snes)
26 {
27   PetscErrorCode   ierr;
28 
29   PetscFunctionBegin;
30   ierr = SNESReset_NRichardson(snes);CHKERRQ(ierr);
31   ierr = PetscFree(snes->data);CHKERRQ(ierr);
32   PetscFunctionReturn(0);
33 }
34 
35 /*
36    SNESSetUp_NRichardson - Sets up the internal data structures for the later use
37    of the SNESNRICHARDSON nonlinear solver.
38 
39    Input Parameters:
40 +  snes - the SNES context
41 -  x - the solution vector
42 
43    Application Interface Routine: SNESSetUp()
44  */
45 #undef __FUNCT__
46 #define __FUNCT__ "SNESSetUp_NRichardson"
47 PetscErrorCode SNESSetUp_NRichardson(SNES snes)
48 {
49   PetscErrorCode ierr;
50 
51   PetscFunctionBegin;
52   ierr = SNESDefaultGetWork(snes,2);CHKERRQ(ierr);
53   PetscFunctionReturn(0);
54 }
55 
56 /*
57   SNESSetFromOptions_NRichardson - Sets various parameters for the SNESLS method.
58 
59   Input Parameter:
60 . snes - the SNES context
61 
62   Application Interface Routine: SNESSetFromOptions()
63 */
64 #undef __FUNCT__
65 #define __FUNCT__ "SNESSetFromOptions_NRichardson"
66 static PetscErrorCode SNESSetFromOptions_NRichardson(SNES snes)
67 {
68   PetscErrorCode ierr;
69   PetscFunctionBegin;
70     ierr = PetscOptionsHead("SNES Richardson options");CHKERRQ(ierr);
71     ierr = PetscOptionsTail();CHKERRQ(ierr);
72   PetscFunctionReturn(0);
73 }
74 
75 /*
76   SNESView_NRichardson - Prints info from the SNESRichardson data structure.
77 
78   Input Parameters:
79 + SNES - the SNES context
80 - viewer - visualization context
81 
82   Application Interface Routine: SNESView()
83 */
84 #undef __FUNCT__
85 #define __FUNCT__ "SNESView_NRichardson"
86 static PetscErrorCode SNESView_NRichardson(SNES snes, PetscViewer viewer)
87 {
88   PetscBool        iascii;
89   PetscErrorCode   ierr;
90 
91   PetscFunctionBegin;
92   ierr = PetscTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
93   if (iascii) {
94     ierr = PetscViewerASCIIPrintf(viewer,"  richardson variant: %s\n", SNESLineSearchTypeName(snes->ls_type));CHKERRQ(ierr);
95   }
96   PetscFunctionReturn(0);
97 }
98 
99 #undef __FUNCT__
100 #define __FUNCT__ "SNESLineSearchQuadratic_NRichardson"
101 PetscErrorCode SNESLineSearchQuadratic_NRichardson(SNES snes,void *lsctx,Vec X,Vec F,Vec Y,PetscReal fnorm,PetscReal dummyXnorm,Vec G,Vec W,PetscReal *dummyYnorm,PetscReal *gnorm,PetscBool *flag)
102 {
103   PetscInt         i;
104   PetscReal        alphas[3] = {0.0, 0.5, 1.0};
105   PetscReal        norms[3];
106   PetscReal        alpha,a,b;
107   PetscErrorCode   ierr;
108 
109   PetscFunctionBegin;
110   norms[0]  = fnorm;
111   for(i=1; i < 3; ++i) {
112     ierr = VecWAXPY(W, alphas[i], Y, X);CHKERRQ(ierr);     /* W =  X^n - \alpha Y */
113     ierr = SNESComputeFunction(snes, W, G);CHKERRQ(ierr);
114     ierr = VecNorm(G, NORM_2, &norms[i]);CHKERRQ(ierr);
115   }
116   for(i = 0; i < 3; ++i) {
117     norms[i] = PetscSqr(norms[i]);
118   }
119   /* Fit a quadratic:
120        If we have x_{0,1,2} = 0, x_1, x_2 which generate norms y_{0,1,2}
121        a = (x_1 y_2 - x_2 y_1 + (x_2 - x_1) y_0)/(x^2_2 x_1 - x_2 x^2_1)
122        b = (x^2_1 y_2 - x^2_2 y_1 + (x^2_2 - x^2_1) y_0)/(x_2 x^2_1 - x^2_2 x_1)
123        c = y_0
124        x_min = -b/2a
125 
126        If we let x_{0,1,2} = 0, 0.5, 1.0
127        a = 2 y_2 - 4 y_1 + 2 y_0
128        b =  -y_2 + 4 y_1 - 3 y_0
129        c =   y_0
130   */
131   a = (alphas[1]*norms[2] - alphas[2]*norms[1] + (alphas[2] - alphas[1])*norms[0])/(PetscSqr(alphas[2])*alphas[1] - alphas[2]*PetscSqr(alphas[1]));
132   b = (PetscSqr(alphas[1])*norms[2] - PetscSqr(alphas[2])*norms[1] + (PetscSqr(alphas[2]) - PetscSqr(alphas[1]))*norms[0])/(alphas[2]*PetscSqr(alphas[1]) - PetscSqr(alphas[2])*alphas[1]);
133   /* Check for positive a (concave up) */
134   if (a >= 0.0) {
135     alpha = -b/(2.0*a);
136     alpha = PetscMin(alpha, alphas[2]);
137     alpha = PetscMax(alpha, alphas[0]);
138   } else {
139     alpha = 1.0;
140   }
141   if (snes->ls_monitor) {
142     ierr = PetscViewerASCIIAddTab(snes->ls_monitor,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
143     ierr = PetscViewerASCIIPrintf(snes->ls_monitor,"    Line search: norms[0] = %g, norms[1] = %g, norms[2] = %g alpha %g\n", sqrt(norms[0]),sqrt(norms[1]),sqrt(norms[2]),alpha);CHKERRQ(ierr);
144     ierr = PetscViewerASCIISubtractTab(snes->ls_monitor,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
145   }
146   ierr = VecCopy(X, W);CHKERRQ(ierr);
147   ierr = VecAXPY(W, alpha, Y);CHKERRQ(ierr);
148   if (alpha != 1.0) {
149     ierr = SNESComputeFunction(snes, W, G);CHKERRQ(ierr);
150     ierr = VecNorm(G, NORM_2, gnorm);CHKERRQ(ierr);
151   } else {
152     *gnorm = PetscSqrtReal(norms[2]);
153   }
154   if (alpha == 0.0) *flag = PETSC_FALSE;
155   else              *flag = PETSC_TRUE;
156   PetscFunctionReturn(0);
157 }
158 
159 /*
160   SNESSolve_NRichardson - Solves a nonlinear system with the Richardson method.
161 
162   Input Parameters:
163 . snes - the SNES context
164 
165   Output Parameter:
166 . outits - number of iterations until termination
167 
168   Application Interface Routine: SNESSolve()
169 */
170 #undef __FUNCT__
171 #define __FUNCT__ "SNESSolve_NRichardson"
172 PetscErrorCode SNESSolve_NRichardson(SNES snes)
173 {
174   Vec                 X, Y, F, W, G;
175   PetscReal           fnorm;
176   PetscInt            maxits, i;
177   PetscErrorCode      ierr;
178   SNESConvergedReason reason;
179 
180   PetscFunctionBegin;
181   snes->reason = SNES_CONVERGED_ITERATING;
182 
183   maxits = snes->max_its;        /* maximum number of iterations */
184   X      = snes->vec_sol;        /* X^n */
185   Y      = snes->vec_sol_update; /* \tilde X */
186   F      = snes->vec_func;       /* residual vector */
187   W      = snes->work[0];        /* work vector */
188   G      = snes->work[1];        /* line search function vector */
189 
190   ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
191   snes->iter = 0;
192   snes->norm = 0.;
193   ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
194   ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
195   if (snes->domainerror) {
196     snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
197     PetscFunctionReturn(0);
198   }
199   ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm <- ||F||  */
200   if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
201   ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
202   snes->norm = fnorm;
203   ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
204   SNESLogConvHistory(snes,fnorm,0);
205   ierr = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr);
206 
207   /* set parameter for default relative tolerance convergence test */
208   snes->ttol = fnorm*snes->rtol;
209   /* test convergence */
210   ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
211   if (snes->reason) PetscFunctionReturn(0);
212 
213   for(i = 0; i < maxits; i++) {
214     PetscBool  lsSuccess = PETSC_TRUE;
215     PetscReal  dummyNorm;
216 
217     /* Call general purpose update function */
218     if (snes->ops->update) {
219       ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
220     }
221     if (!snes->pc) {
222       ierr = VecCopy(F,Y);CHKERRQ(ierr);
223       ierr = VecScale(Y,-1.0);CHKERRQ(ierr);
224     } else {
225       ierr = VecCopy(X,Y);CHKERRQ(ierr);
226       ierr = SNESSolve(snes->pc, snes->vec_rhs, Y);CHKERRQ(ierr);
227       ierr = SNESGetConvergedReason(snes->pc,&reason);CHKERRQ(ierr);
228       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
229         snes->reason = SNES_DIVERGED_INNER;
230         PetscFunctionReturn(0);
231       }
232       ierr = VecAXPY(Y,-1.0,X);CHKERRQ(ierr);
233     }
234 
235       ierr = (*snes->ops->linesearch)(snes, snes->lsP, X, F, Y, fnorm, 0.0, G, W, &dummyNorm, &fnorm, &lsSuccess);CHKERRQ(ierr);
236     if (!lsSuccess) {
237       if (++snes->numFailures >= snes->maxFailures) {
238         snes->reason = SNES_DIVERGED_LINE_SEARCH;
239         break;
240       }
241     }
242     if (snes->nfuncs >= snes->max_funcs) {
243       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
244       break;
245     }
246     if (snes->domainerror) {
247       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
248       PetscFunctionReturn(0);
249     }
250     ierr = VecCopy(G, F);CHKERRQ(ierr);
251     ierr = VecCopy(W, X);CHKERRQ(ierr);
252     /* Monitor convergence */
253     ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
254     snes->iter = i+1;
255     snes->norm = fnorm;
256     ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
257     SNESLogConvHistory(snes,snes->norm,0);
258     ierr = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
259     /* Test for convergence */
260     ierr = (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
261     if (snes->reason) break;
262   }
263   if (i == maxits) {
264     ierr = PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);CHKERRQ(ierr);
265     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
266   }
267   PetscFunctionReturn(0);
268 }
269 
270 
271 EXTERN_C_BEGIN
272 #undef __FUNCT__
273 #define __FUNCT__ "SNESLineSearchSetType_NRichardson"
274 PetscErrorCode  SNESLineSearchSetType_NRichardson(SNES snes, SNESLineSearchType type)
275 {
276   PetscErrorCode ierr;
277   PetscFunctionBegin;
278 
279   switch (type) {
280   case SNES_LS_BASIC:
281     ierr = SNESLineSearchSet(snes,SNESLineSearchNo,PETSC_NULL);CHKERRQ(ierr);
282     break;
283   case SNES_LS_BASIC_NONORMS:
284     ierr = SNESLineSearchSet(snes,SNESLineSearchNoNorms,PETSC_NULL);CHKERRQ(ierr);
285     break;
286   case SNES_LS_QUADRATIC:
287     ierr = SNESLineSearchSet(snes,SNESLineSearchQuadratic_NRichardson,PETSC_NULL);CHKERRQ(ierr);
288     break;
289   case SNES_LS_SECANT:
290     ierr = SNESLineSearchSet(snes,SNESLineSearchQuadraticSecant,PETSC_NULL);CHKERRQ(ierr);
291     break;
292   default:
293     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,"Unknown line search type");
294     break;
295   }
296   snes->ls_type = type;
297   PetscFunctionReturn(0);
298 }
299 EXTERN_C_END
300 
301 /*MC
302   SNESNRICHARDSON - Richardson nonlinear solver that uses successive substitutions, also sometimes known as Picard iteration.
303 
304   Level: beginner
305 
306   Options Database:
307 +   -snes_ls_damping - damping factor to apply to F(x) (used only if -snes_ls is basic or basicnonorms)
308 -   -snes_ls <basic,basicnormnorms,quadratic>
309 
310   Notes: If no inner nonlinear preconditioner is provided then solves F(x) - b = 0 using x^{n+1} = x^{n} - lambda
311             (F(x^n) - b) where lambda is obtained either SNESLineSearchSetDamping(), -snes_damping or a line search.  If
312             an inner nonlinear preconditioner is provided (either with -npc_snes_type or SNESSetPC()) then the inner
313             solver is called an initial solution x^n and the nonlinear Richardson uses x^{n+1} = x^{n} + lambda d^{n}
314             where d^{n} = \hat{x}^{n} - x^{n} where \hat{x}^{n} is the solution returned from the inner solver.
315 
316      This uses no derivative information thus will be much slower then Newton's method obtained with -snes_type ls
317 
318 .seealso:  SNESCreate(), SNES, SNESSetType(), SNESLS, SNESTR, SNESNGMRES, SNESNQN
319 M*/
320 EXTERN_C_BEGIN
321 #undef __FUNCT__
322 #define __FUNCT__ "SNESCreate_NRichardson"
323 PetscErrorCode  SNESCreate_NRichardson(SNES snes)
324 {
325   PetscErrorCode ierr;
326   PetscFunctionBegin;
327   snes->ops->destroy         = SNESDestroy_NRichardson;
328   snes->ops->setup           = SNESSetUp_NRichardson;
329   snes->ops->setfromoptions  = SNESSetFromOptions_NRichardson;
330   snes->ops->view            = SNESView_NRichardson;
331   snes->ops->solve           = SNESSolve_NRichardson;
332   snes->ops->reset           = SNESReset_NRichardson;
333 
334   snes->usesksp              = PETSC_FALSE;
335   snes->usespc               = PETSC_TRUE;
336 
337   ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetType_C","SNESLineSearchSetType_NRichardson",SNESLineSearchSetType_NRichardson);CHKERRQ(ierr);
338   ierr = SNESLineSearchSetType(snes, SNES_LS_SECANT);CHKERRQ(ierr);
339 
340   PetscFunctionReturn(0);
341 }
342 EXTERN_C_END
343