Actual source code: picard.c
2: #include <../src/snes/impls/picard/picard.h>
6: PetscErrorCode SNESReset_Picard(SNES snes)
7: {
11: if (snes->work) {VecDestroyVecs(snes->nwork,&snes->work);}
12: return(0);
13: }
15: /*
16: SNESDestroy_Picard - Destroys the private SNES_Picard context that was created with SNESCreate_Picard().
18: Input Parameter:
19: . snes - the SNES context
21: Application Interface Routine: SNESDestroy()
22: */
25: PetscErrorCode SNESDestroy_Picard(SNES snes)
26: {
30: SNESReset_Picard(snes);
31: PetscFree(snes->data);
32: return(0);
33: }
35: /*
36: SNESSetUp_Picard - Sets up the internal data structures for the later use
37: of the SNESPICARD nonlinear solver.
39: Input Parameters:
40: + snes - the SNES context
41: - x - the solution vector
43: Application Interface Routine: SNESSetUp()
44: */
47: PetscErrorCode SNESSetUp_Picard(SNES snes)
48: {
52: SNESDefaultGetWork(snes,1);
53: return(0);
54: }
56: PetscErrorCode PicardLineSearchQuadratic(SNES snes, void *lsctx, Vec X, Vec F, Vec dummyG, Vec Y, Vec dummyW, PetscReal fnorm, PetscReal dummyXnorm, PetscReal *dummyYnorm, PetscReal *gnorm, PetscBool *flag);
57: /*
58: SNESSetFromOptions_Picard - Sets various parameters for the SNESLS method.
60: Input Parameter:
61: . snes - the SNES context
63: Application Interface Routine: SNESSetFromOptions()
64: */
67: static PetscErrorCode SNESSetFromOptions_Picard(SNES snes)
68: {
69: SNES_Picard *ls = (SNES_Picard *)snes->data;
70: const char *types[] = {"basic", "quadratic", "cubic"};
71: PetscInt indx = 0;
72: PetscBool flg;
76: PetscOptionsHead("SNES Picard options");
77: PetscOptionsEList("-snes_picard","Picard Type","SNESLineSearchSet",types,3,"basic",&indx,&flg);
78: ls->type = indx;
79: if (flg) {
80: switch (indx) {
81: case 0:
82: SNESLineSearchSet(snes,SNESLineSearchNo,PETSC_NULL);
83: break;
84: case 1:
85: SNESLineSearchSet(snes,PicardLineSearchQuadratic,PETSC_NULL);
86: break;
87: case 2:
88: SNESLineSearchSet(snes,SNESLineSearchNo,PETSC_NULL);
89: break;
90: }
91: }
92: ls->alpha = 1.0;
93: PetscOptionsReal("-snes_picard_alpha","Momentum parameter","SNES",ls->alpha,&ls->alpha,&flg);
94: PetscOptionsTail();
95: return(0);
96: }
98: /*
99: SNESView_Picard - Prints info from the SNESPICARD data structure.
101: Input Parameters:
102: + SNES - the SNES context
103: - viewer - visualization context
105: Application Interface Routine: SNESView()
106: */
109: static PetscErrorCode SNESView_Picard(SNES snes, PetscViewer viewer)
110: {
111: SNES_Picard *ls = (SNES_Picard *)snes->data;
112: const char *cstr;
113: PetscBool iascii;
117: PetscTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
118: if (iascii) {
119: switch(ls->type) {
120: case 0:
121: cstr = "basic";
122: break;
123: default:
124: cstr = "unknown";
125: }
126: PetscViewerASCIIPrintf(viewer," picard variant: %s\n", cstr);
127: }
128: return(0);
129: }
133: PetscErrorCode PicardLineSearchQuadratic(SNES snes, void *lsctx, Vec X, Vec F, Vec dummyG, Vec Y, Vec W, PetscReal fnorm, PetscReal dummyXnorm, PetscReal *dummyYnorm, PetscReal *gnorm, PetscBool *flag)
134: {
135: PetscInt i;
136: PetscReal alphas[3] = {0.0, 0.5, 1.0};
137: PetscReal norms[3];
138: PetscReal alpha,a,b;
142: norms[0] = fnorm;
143: /* Calculate trial solutions */
144: for(i = 1; i < 3; ++i) {
145: /* Calculate X^{n+1} = (1 - \alpha) X^n + \alpha Y */
146: VecCopy(X, W);
147: VecAXPBY(W, alphas[i], 1 - alphas[i], Y);
148: SNESComputeFunction(snes, W, F);
149: VecNorm(F, NORM_2, &norms[i]);
150: }
151: for(i = 0; i < 3; ++i) {
152: norms[i] = PetscSqr(norms[i]);
153: }
154: /* Fit a quadratic:
155: If we have x_{0,1,2} = 0, x_1, x_2 which generate norms y_{0,1,2}
156: 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)
157: 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)
158: c = y_0
159: x_min = -b/2a
161: If we let x_{0,1,2} = 0, 0.5, 1.0
162: a = 2 y_2 - 4 y_1 + 2 y_0
163: b = -y_2 + 4 y_1 - 3 y_0
164: c = y_0
165: */
166: a = (alphas[1]*norms[2] - alphas[2]*norms[1] + (alphas[2] - alphas[1])*norms[0])/
167: (PetscSqr(alphas[2])*alphas[1] - alphas[2]*PetscSqr(alphas[1]));
168: b = (PetscSqr(alphas[1])*norms[2] - PetscSqr(alphas[2])*norms[1] + (PetscSqr(alphas[2]) - PetscSqr(alphas[1]))*norms[0])/
169: (alphas[2]*PetscSqr(alphas[1]) - PetscSqr(alphas[2])*alphas[1]);
170: /* Check for positive a (concave up) */
171: if (a >= 0.0) {
172: alpha = -b/(2.0*a);
173: alpha = PetscMin(alpha, alphas[2]);
174: alpha = PetscMax(alpha, alphas[0]);
175: } else {
176: alpha = 1.0;
177: }
178: PetscPrintf(snes->hdr.comm, "norms[0] = %g, norms[1] = %g, norms[2] = %g\n", sqrt(norms[0]), sqrt(norms[1]), sqrt(norms[2]));
179: PetscPrintf(snes->hdr.comm, "Choose alpha = %g\n", alpha);
180: VecAXPBY(X, alpha, 1 - alpha, Y);
181: SNESComputeFunction(snes, X, F);
182: VecNorm(F, NORM_2, gnorm);
183: *flag = PETSC_TRUE;
184: return(0);
185: }
187: /*
188: SNESSolve_Picard - Solves a nonlinear system with the Picard method.
190: Input Parameters:
191: . snes - the SNES context
193: Output Parameter:
194: . outits - number of iterations until termination
196: Application Interface Routine: SNESSolve()
197: */
200: PetscErrorCode SNESSolve_Picard(SNES snes)
201: {
202: SNES_Picard *neP = (SNES_Picard *) snes->data;
203: Vec X, Y, F, W;
204: PetscReal alpha = neP->alpha;
205: PetscReal fnorm;
206: PetscInt maxits, i;
210: snes->reason = SNES_CONVERGED_ITERATING;
212: maxits = snes->max_its; /* maximum number of iterations */
213: X = snes->vec_sol; /* X^n */
214: Y = snes->vec_sol_update; /* \tilde X */
215: F = snes->vec_func; /* residual vector */
216: W = snes->work[0]; /* work vector */
218: PetscObjectTakeAccess(snes);
219: snes->iter = 0;
220: snes->norm = 0.;
221: PetscObjectGrantAccess(snes);
222: SNESComputeFunction(snes,X,F);
223: if (snes->domainerror) {
224: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
225: return(0);
226: }
227: VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F|| */
228: if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
229: PetscObjectTakeAccess(snes);
230: snes->norm = fnorm;
231: PetscObjectGrantAccess(snes);
232: SNESLogConvHistory(snes,fnorm,0);
233: SNESMonitor(snes,0,fnorm);
235: /* set parameter for default relative tolerance convergence test */
236: snes->ttol = fnorm*snes->rtol;
237: /* test convergence */
238: (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
239: if (snes->reason) return(0);
241: for(i = 0; i < maxits; i++) {
242: PetscBool lsSuccess = PETSC_TRUE;
244: /* Call general purpose update function */
245: if (snes->ops->update) {
246: (*snes->ops->update)(snes, snes->iter);
247: }
248: if (neP->type == 0) {
249: PetscPrintf(snes->hdr.comm, "Fixed alpha = %g\n", alpha);
250: /* Update guess Y = X^n - F(X^n) */
251: VecWAXPY(Y, -1.0, F, X);
252: /* X^{n+1} = (1 - \alpha) X^n + \alpha Y */
253: VecAXPBY(X, alpha, 1 - alpha, Y);
254: /* Compute F(X^{new}) */
255: SNESComputeFunction(snes, X, F);
256: VecNorm(F, NORM_2, &fnorm);
257: if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Infinite or not-a-number generated norm");
258: } else {
259: PetscReal dummyNorm;
260: /* Compute a (scaled) negative update in the line search routine:
261: Y <- X - lambda*Y
262: and evaluate G = function(Y) (depends on the line search). */
263: #if 1
264: /* Calculate the solution increment, Y = X^n - F(X^n) */
265: VecWAXPY(Y, -1.0, F, X);
266: (*neP->LineSearch)(snes, neP->lsP, X, F, F/*G*/, Y, W, fnorm, 0.0, &dummyNorm, &fnorm, &lsSuccess);
267: #else
268: /* Put this in function after it works */
269: PetscReal alphas[3] = {0.0, 0.5, 1.0};
270: PetscReal norms[3];
272: norms[0] = fnorm;
273: /* Calculate the solution increment, Y = X^n - F(X^n) */
274: VecWAXPY(Y, -1.0, F, X);
275: {
276: PetscReal norm0, norm1;
278: VecNorm(n_3(X), NORM_INFINITY, &norm0);
279: VecNorm(n_3(Y), NORM_INFINITY, &norm1);
280: if (norm1 > 0.9) {
281: alpha[2] = (norm1 - 0.9)/(norm1 - norm0);
282: }
283: }
284: alpha[1] = 0.5*alpha[2];
285: /* Calculate trial solutions */
286: for(PetscInt i = 1; i < 3; ++i) {
287: /* Calculate X^{n+1} = (1 - \alpha) X^n + \alpha Y */
288: VecCopy(X, W);
289: VecAXPBY(W, alphas[i], 1 - alphas[i], Y);
290: SNESComputeFunction(snes, W, F);
291: VecNorm(F, NORM_2, &norms[i]);
292: }
293: for(PetscInt i = 0; i < 3; ++i) {
294: norms[i] = PetscSqr(norms[i]);
295: }
296: /* Fit a quadratic:
297: If we have x_{0,1,2} = 0, x_1, x_2 which generate norms y_{0,1,2}
298: 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)
299: 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)
300: c = y_0
301: x_min = -b/2a
303: If we let x_{0,1,2} = 0, 0.5, 1.0
304: a = 2 y_2 - 4 y_1 + 2 y_0
305: b = -y_2 + 4 y_1 - 3 y_0
306: c = y_0
307: */
308: const PetscReal a = (alphas[1]*norms[2] - alphas[2]*norms[1] + (alphas[2] - alphas[1])*norms[0])/
309: (PetscSqr(alphas[2])*alphas[1] - alphas[2]*PetscSqr(alphas[1]));
310: const PetscReal b = (PetscSqr(alphas[1])*norms[2] - PetscSqr(alphas[2])*norms[1] + (PetscSqr(alphas[2]) - PetscSqr(alphas[1]))*norms[0])/
311: (alphas[2]*PetscSqr(alphas[1]) - PetscSqr(alphas[2])*alphas[1]);
312: /* Check for positive a (concave up) */
313: if (a >= 0.0) {
314: alpha = -b/(2.0*a);
315: alpha = PetscMin(alpha, alphas[2]);
316: alpha = PetscMax(alpha, alphas[0]);
317: } else {
318: alpha = 1.0;
319: }
320: PetscPrintf(snes->hdr.comm, "norms[0] = %g, norms[1] = %g, norms[2] = %g\n", norms[0], norms[1], norms[2]);
321: PetscPrintf(snes->hdr.comm, "Choose alpha = %g\n", alpha);
322: VecAXPBY(X, alpha, 1 - alpha, Y);
323: SNESComputeFunction(snes, X, F);
324: VecNorm(F, NORM_2, &fnorm);
325: #endif
326: }
327: if (!lsSuccess) {
328: if (++snes->numFailures >= snes->maxFailures) {
329: snes->reason = SNES_DIVERGED_LINE_SEARCH;
330: break;
331: }
332: }
333: if (snes->nfuncs >= snes->max_funcs) {
334: snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
335: break;
336: }
337: if (snes->domainerror) {
338: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
339: return(0);
340: }
341: /* Monitor convergence */
342: PetscObjectTakeAccess(snes);
343: snes->iter = i+1;
344: snes->norm = fnorm;
345: PetscObjectGrantAccess(snes);
346: SNESLogConvHistory(snes,snes->norm,0);
347: SNESMonitor(snes,snes->iter,snes->norm);
348: /* Test for convergence */
349: (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
350: if (snes->reason) break;
351: }
352: if (i == maxits) {
353: PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
354: if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
355: }
356: return(0);
357: }
363: PetscErrorCode SNESLineSearchSetPreCheck_Picard(SNES snes, FCN1 func, void *checkctx)
364: {
366: ((SNES_Picard *)(snes->data))->precheckstep = func;
367: ((SNES_Picard *)(snes->data))->precheck = checkctx;
368: return(0);
369: }
376: PetscErrorCode SNESLineSearchSet_Picard(SNES snes, FCN2 func, void *lsctx)
377: {
379: ((SNES_Picard *)(snes->data))->LineSearch = func;
380: ((SNES_Picard *)(snes->data))->lsP = lsctx;
381: return(0);
382: }
389: PetscErrorCode SNESLineSearchSetPostCheck_Picard(SNES snes, FCN3 func, void *checkctx)
390: {
392: ((SNES_Picard *)(snes->data))->postcheckstep = func;
393: ((SNES_Picard *)(snes->data))->postcheck = checkctx;
394: return(0);
395: }
398: /*MC
399: SNESPICARD - Picard nonlinear solver that uses successive substitutions
401: Level: beginner
403: .seealso: SNESCreate(), SNES, SNESSetType(), SNESLS, SNESTR
404: M*/
408: PetscErrorCode SNESCreate_Picard(SNES snes)
409: {
410: SNES_Picard *neP;
414: snes->ops->destroy = SNESDestroy_Picard;
415: snes->ops->setup = SNESSetUp_Picard;
416: snes->ops->setfromoptions = SNESSetFromOptions_Picard;
417: snes->ops->view = SNESView_Picard;
418: snes->ops->solve = SNESSolve_Picard;
419: snes->ops->reset = SNESReset_Picard;
421: PetscNewLog(snes, SNES_Picard, &neP);
422: snes->data = (void*) neP;
423: neP->type = 0;
424: neP->alpha = 1.e-4;
425: neP->maxstep = 1.e8;
426: neP->steptol = 1.e-12;
427: neP->LineSearch = SNESLineSearchNo;
428: neP->lsP = PETSC_NULL;
429: neP->postcheckstep = PETSC_NULL;
430: neP->postcheck = PETSC_NULL;
431: neP->precheckstep = PETSC_NULL;
432: neP->precheck = PETSC_NULL;
434: PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSet_C",
435: "SNESLineSearchSet_Picard",
436: SNESLineSearchSet_Picard);
437: PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPostCheck_C",
438: "SNESLineSearchSetPostCheck_Picard",
439: SNESLineSearchSetPostCheck_Picard);
440: PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESLineSearchSetPreCheck_C",
441: "SNESLineSearchSetPreCheck_Picard",
442: SNESLineSearchSetPreCheck_Picard);
443: return(0);
444: }