Actual source code: snesrichardson.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1:  #include <../src/snes/impls/richardson/snesrichardsonimpl.h>


  4: PetscErrorCode SNESReset_NRichardson(SNES snes)
  5: {
  7:   return(0);
  8: }

 10: /*
 11:   SNESDestroy_NRichardson - Destroys the private SNES_NRichardson context that was created with SNESCreate_NRichardson().

 13:   Input Parameter:
 14: . snes - the SNES context

 16:   Application Interface Routine: SNESDestroy()
 17: */
 18: PetscErrorCode SNESDestroy_NRichardson(SNES snes)
 19: {

 23:   SNESReset_NRichardson(snes);
 24:   PetscFree(snes->data);
 25:   return(0);
 26: }

 28: /*
 29:    SNESSetUp_NRichardson - Sets up the internal data structures for the later use
 30:    of the SNESNRICHARDSON nonlinear solver.

 32:    Input Parameters:
 33: +  snes - the SNES context
 34: -  x - the solution vector

 36:    Application Interface Routine: SNESSetUp()
 37:  */
 38: PetscErrorCode SNESSetUp_NRichardson(SNES snes)
 39: {
 41:   if (snes->npcside== PC_RIGHT) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"NRichardson only supports left preconditioning");}
 42:   if (snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_UNPRECONDITIONED;
 43:   return(0);
 44: }

 46: /*
 47:   SNESSetFromOptions_NRichardson - Sets various parameters for the SNESNEWTONLS method.

 49:   Input Parameter:
 50: . snes - the SNES context

 52:   Application Interface Routine: SNESSetFromOptions()
 53: */
 54: static PetscErrorCode SNESSetFromOptions_NRichardson(PetscOptionItems *PetscOptionsObject,SNES snes)
 55: {
 57:   SNESLineSearch linesearch;

 60:   PetscOptionsHead(PetscOptionsObject,"SNES Richardson options");
 61:   PetscOptionsTail();
 62:   if (!snes->linesearch) {
 63:     SNESGetLineSearch(snes, &linesearch);
 64:     SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
 65:   }
 66:   return(0);
 67: }

 69: /*
 70:   SNESView_NRichardson - Prints info from the SNESRichardson data structure.

 72:   Input Parameters:
 73: + SNES - the SNES context
 74: - viewer - visualization context

 76:   Application Interface Routine: SNESView()
 77: */
 78: static PetscErrorCode SNESView_NRichardson(SNES snes, PetscViewer viewer)
 79: {
 80:   PetscBool      iascii;

 84:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
 85:   if (iascii) {
 86:   }
 87:   return(0);
 88: }

 90: /*
 91:   SNESSolve_NRichardson - Solves a nonlinear system with the Richardson method.

 93:   Input Parameters:
 94: . snes - the SNES context

 96:   Output Parameter:
 97: . outits - number of iterations until termination

 99:   Application Interface Routine: SNESSolve()
100: */
101: PetscErrorCode SNESSolve_NRichardson(SNES snes)
102: {
103:   Vec                  X, Y, F;
104:   PetscReal            xnorm, fnorm, ynorm;
105:   PetscInt             maxits, i;
106:   PetscErrorCode       ierr;
107:   SNESLineSearchReason lsresult;
108:   SNESConvergedReason  reason;

111:   if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

113:   snes->reason = SNES_CONVERGED_ITERATING;

115:   maxits = snes->max_its;        /* maximum number of iterations */
116:   X      = snes->vec_sol;        /* X^n */
117:   Y      = snes->vec_sol_update; /* \tilde X */
118:   F      = snes->vec_func;       /* residual vector */

120:   PetscObjectSAWsTakeAccess((PetscObject)snes);
121:   snes->iter = 0;
122:   snes->norm = 0.;
123:   PetscObjectSAWsGrantAccess((PetscObject)snes);

125:   if (snes->npc && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
126:     SNESApplyNPC(snes,X,NULL,F);
127:     SNESGetConvergedReason(snes->npc,&reason);
128:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
129:       snes->reason = SNES_DIVERGED_INNER;
130:       return(0);
131:     }
132:     VecNorm(F,NORM_2,&fnorm);
133:   } else {
134:     if (!snes->vec_func_init_set) {
135:       SNESComputeFunction(snes,X,F);
136:     } else snes->vec_func_init_set = PETSC_FALSE;

138:     VecNorm(F,NORM_2,&fnorm);
139:     SNESCheckFunctionNorm(snes,fnorm);
140:   }
141:   if (snes->npc && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
142:       SNESApplyNPC(snes,X,F,Y);
143:       SNESGetConvergedReason(snes->npc,&reason);
144:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
145:         snes->reason = SNES_DIVERGED_INNER;
146:         return(0);
147:       }
148:   } else {
149:     VecCopy(F,Y);
150:   }

152:   PetscObjectSAWsTakeAccess((PetscObject)snes);
153:   snes->norm = fnorm;
154:   PetscObjectSAWsGrantAccess((PetscObject)snes);
155:   SNESLogConvergenceHistory(snes,fnorm,0);
156:   SNESMonitor(snes,0,fnorm);

158:   /* test convergence */
159:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
160:   if (snes->reason) return(0);

162:   /* Call general purpose update function */
163:   if (snes->ops->update) {
164:     (*snes->ops->update)(snes, snes->iter);
165:   }

167:   /* set parameter for default relative tolerance convergence test */
168:   snes->ttol = fnorm*snes->rtol;
169:   /* test convergence */
170:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
171:   if (snes->reason) return(0);

173:   for (i = 1; i < maxits+1; i++) {
174:     SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Y);
175:     SNESLineSearchGetReason(snes->linesearch, &lsresult);
176:     SNESLineSearchGetNorms(snes->linesearch, &xnorm, &fnorm, &ynorm);
177:     if (lsresult) {
178:       if (++snes->numFailures >= snes->maxFailures) {
179:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
180:         break;
181:       }
182:     }
183:     if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
184:       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
185:       break;
186:     }

188:     /* Monitor convergence */
189:     PetscObjectSAWsTakeAccess((PetscObject)snes);
190:     snes->iter = i;
191:     snes->norm = fnorm;
192:     snes->xnorm = xnorm;
193:     snes->ynorm = ynorm;
194:     PetscObjectSAWsGrantAccess((PetscObject)snes);
195:     SNESLogConvergenceHistory(snes,snes->norm,0);
196:     SNESMonitor(snes,snes->iter,snes->norm);
197:     /* Test for convergence */
198:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
199:     if (snes->reason) break;

201:     /* Call general purpose update function */
202:     if (snes->ops->update) {
203:       (*snes->ops->update)(snes, snes->iter);
204:     }

206:     if (snes->npc) {
207:       if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
208:         SNESApplyNPC(snes,X,NULL,Y);
209:         VecNorm(F,NORM_2,&fnorm);
210:         VecCopy(Y,F);
211:       } else {
212:         SNESApplyNPC(snes,X,F,Y);
213:       }
214:       SNESGetConvergedReason(snes->npc,&reason);
215:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
216:         snes->reason = SNES_DIVERGED_INNER;
217:         return(0);
218:       }
219:     } else {
220:       VecCopy(F,Y);
221:     }
222:   }
223:   if (i == maxits+1) {
224:     PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
225:     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
226:   }
227:   return(0);
228: }

230: /*MC
231:   SNESNRICHARDSON - Richardson nonlinear solver that uses successive substitutions, also sometimes known as Picard iteration.

233:   Level: beginner

235:   Options Database:
236: +   -snes_linesearch_type <l2,cp,basic> Line search type.
237: -   -snes_linesearch_damping<1.0> Damping for the line search.

239:   Notes:
240:     If no inner nonlinear preconditioner is provided then solves F(x) - b = 0 using x^{n+1} = x^{n} - lambda
241:             (F(x^n) - b) where lambda is obtained either SNESLineSearchSetDamping(), -snes_damping or a line search.  If
242:             an inner nonlinear preconditioner is provided (either with -npc_snes_type or SNESSetNPC()) then the inner
243:             solver is called an initial solution x^n and the nonlinear Richardson uses x^{n+1} = x^{n} + lambda d^{n}
244:             where d^{n} = \hat{x}^{n} - x^{n} where \hat{x}^{n} is the solution returned from the inner solver.

246:             The update, especially without inner nonlinear preconditioner, may be ill-scaled.  If using the basic
247:             linesearch, one may have to scale the update with -snes_linesearch_damping

249:      This uses no derivative information thus will be much slower then Newton's method obtained with -snes_type ls

251:      Only supports left non-linear preconditioning.

253: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESQN, SNESNCG
254: M*/
255: PETSC_EXTERN PetscErrorCode SNESCreate_NRichardson(SNES snes)
256: {
257:   PetscErrorCode   ierr;
258:   SNES_NRichardson *neP;

261:   snes->ops->destroy        = SNESDestroy_NRichardson;
262:   snes->ops->setup          = SNESSetUp_NRichardson;
263:   snes->ops->setfromoptions = SNESSetFromOptions_NRichardson;
264:   snes->ops->view           = SNESView_NRichardson;
265:   snes->ops->solve          = SNESSolve_NRichardson;
266:   snes->ops->reset          = SNESReset_NRichardson;

268:   snes->usesksp = PETSC_FALSE;
269:   snes->usesnpc = PETSC_TRUE;

271:   snes->npcside= PC_LEFT;

273:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

275:   PetscNewLog(snes,&neP);
276:   snes->data = (void*) neP;

278:   if (!snes->tolerancesset) {
279:     snes->max_funcs = 30000;
280:     snes->max_its   = 10000;
281:     snes->stol      = 1e-20;
282:   }
283:   return(0);
284: }