xref: /petsc/src/snes/impls/richardson/snesrichardson.c (revision 0a7e80ddf00b85a8bec408344d0bbd42e7f7dbd2)
1 #include <../src/snes/impls/richardson/snesrichardsonimpl.h>
2 
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "SNESReset_NRichardson"
6 PetscErrorCode SNESReset_NRichardson(SNES snes)
7 {
8   PetscFunctionBegin;
9   PetscFunctionReturn(0);
10 }
11 
12 /*
13   SNESDestroy_NRichardson - Destroys the private SNES_NRichardson context that was created with SNESCreate_NRichardson().
14 
15   Input Parameter:
16 . snes - the SNES context
17 
18   Application Interface Routine: SNESDestroy()
19 */
20 #undef __FUNCT__
21 #define __FUNCT__ "SNESDestroy_NRichardson"
22 PetscErrorCode SNESDestroy_NRichardson(SNES snes)
23 {
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   ierr = SNESReset_NRichardson(snes);CHKERRQ(ierr);
28   ierr = PetscFree(snes->data);CHKERRQ(ierr);
29   PetscFunctionReturn(0);
30 }
31 
32 /*
33    SNESSetUp_NRichardson - Sets up the internal data structures for the later use
34    of the SNESNRICHARDSON nonlinear solver.
35 
36    Input Parameters:
37 +  snes - the SNES context
38 -  x - the solution vector
39 
40    Application Interface Routine: SNESSetUp()
41  */
42 #undef __FUNCT__
43 #define __FUNCT__ "SNESSetUp_NRichardson"
44 PetscErrorCode SNESSetUp_NRichardson(SNES snes)
45 {
46   PetscFunctionBegin;
47   PetscFunctionReturn(0);
48 }
49 
50 /*
51   SNESSetFromOptions_NRichardson - Sets various parameters for the SNESNEWTONLS method.
52 
53   Input Parameter:
54 . snes - the SNES context
55 
56   Application Interface Routine: SNESSetFromOptions()
57 */
58 #undef __FUNCT__
59 #define __FUNCT__ "SNESSetFromOptions_NRichardson"
60 static PetscErrorCode SNESSetFromOptions_NRichardson(SNES snes)
61 {
62   PetscErrorCode ierr;
63   SNESLineSearch linesearch;
64 
65   PetscFunctionBegin;
66   ierr = PetscOptionsHead("SNES Richardson options");CHKERRQ(ierr);
67   ierr = PetscOptionsTail();CHKERRQ(ierr);
68   if (!snes->linesearch) {
69     ierr = SNESGetLineSearch(snes, &linesearch);CHKERRQ(ierr);
70     ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);CHKERRQ(ierr);
71   }
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 = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
93   if (iascii) {
94   }
95   PetscFunctionReturn(0);
96 }
97 
98 /*
99   SNESSolve_NRichardson - Solves a nonlinear system with the Richardson method.
100 
101   Input Parameters:
102 . snes - the SNES context
103 
104   Output Parameter:
105 . outits - number of iterations until termination
106 
107   Application Interface Routine: SNESSolve()
108 */
109 #undef __FUNCT__
110 #define __FUNCT__ "SNESSolve_NRichardson"
111 PetscErrorCode SNESSolve_NRichardson(SNES snes)
112 {
113   Vec                 X, Y, F;
114   PetscReal           xnorm, fnorm, ynorm;
115   PetscInt            maxits, i;
116   PetscErrorCode      ierr;
117   PetscBool           lsSuccess;
118   SNESConvergedReason reason;
119 
120   PetscFunctionBegin;
121   snes->reason = SNES_CONVERGED_ITERATING;
122 
123   maxits = snes->max_its;        /* maximum number of iterations */
124   X      = snes->vec_sol;        /* X^n */
125   Y      = snes->vec_sol_update; /* \tilde X */
126   F      = snes->vec_func;       /* residual vector */
127 
128   ierr       = PetscObjectAMSTakeAccess((PetscObject)snes);CHKERRQ(ierr);
129   snes->iter = 0;
130   snes->norm = 0.;
131   ierr       = PetscObjectAMSGrantAccess((PetscObject)snes);CHKERRQ(ierr);
132   if (!snes->vec_func_init_set) {
133     ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
134     if (snes->domainerror) {
135       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
136       PetscFunctionReturn(0);
137     }
138   } else snes->vec_func_init_set = PETSC_FALSE;
139 
140   if (!snes->norm_init_set) {
141     ierr = VecNorm(F,NORM_2,&fnorm);CHKERRQ(ierr); /* fnorm <- ||F||  */
142     if (PetscIsInfOrNanReal(fnorm)) {
143       snes->reason = SNES_DIVERGED_FNORM_NAN;
144       PetscFunctionReturn(0);
145     }
146   } else {
147     fnorm               = snes->norm_init;
148     snes->norm_init_set = PETSC_FALSE;
149   }
150 
151   ierr       = PetscObjectAMSTakeAccess((PetscObject)snes);CHKERRQ(ierr);
152   snes->norm = fnorm;
153   ierr       = PetscObjectAMSGrantAccess((PetscObject)snes);CHKERRQ(ierr);
154   ierr       = SNESLogConvergenceHistory(snes,fnorm,0);CHKERRQ(ierr);
155   ierr       = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr);
156 
157   /* set parameter for default relative tolerance convergence test */
158   snes->ttol = fnorm*snes->rtol;
159   /* test convergence */
160   ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
161   if (snes->reason) PetscFunctionReturn(0);
162 
163   for (i = 0; i < maxits; i++) {
164     lsSuccess = PETSC_TRUE;
165     /* Call general purpose update function */
166     if (snes->ops->update) {
167       ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
168     }
169     if (snes->pc && snes->pcside == PC_RIGHT) {
170       ierr = VecCopy(X,Y);CHKERRQ(ierr);
171       ierr = SNESSetInitialFunction(snes->pc, F);CHKERRQ(ierr);
172       ierr = SNESSetInitialFunctionNorm(snes->pc, fnorm);CHKERRQ(ierr);
173       ierr = PetscLogEventBegin(SNES_NPCSolve,snes->pc,Y,snes->vec_rhs,0);CHKERRQ(ierr);
174       ierr = SNESSolve(snes->pc, snes->vec_rhs, Y);CHKERRQ(ierr);
175       ierr = PetscLogEventEnd(SNES_NPCSolve,snes->pc,Y,snes->vec_rhs,0);CHKERRQ(ierr);
176       ierr = SNESGetConvergedReason(snes->pc,&reason);CHKERRQ(ierr);
177       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
178         snes->reason = SNES_DIVERGED_INNER;
179         PetscFunctionReturn(0);
180       }
181       ierr = VecAYPX(Y,-1.0,X);CHKERRQ(ierr);
182     } else {
183       ierr = VecCopy(F,Y);CHKERRQ(ierr);
184     }
185     ierr = SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Y);CHKERRQ(ierr);
186     ierr = SNESLineSearchGetNorms(snes->linesearch, &xnorm, &fnorm, &ynorm);CHKERRQ(ierr);
187     ierr = SNESLineSearchGetSuccess(snes->linesearch, &lsSuccess);CHKERRQ(ierr);
188     if (!lsSuccess) {
189       if (++snes->numFailures >= snes->maxFailures) {
190         snes->reason = SNES_DIVERGED_LINE_SEARCH;
191         break;
192       }
193     }
194     if (snes->nfuncs >= snes->max_funcs) {
195       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
196       break;
197     }
198     if (snes->domainerror) {
199       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
200       PetscFunctionReturn(0);
201     }
202     /* Monitor convergence */
203     ierr       = PetscObjectAMSTakeAccess((PetscObject)snes);CHKERRQ(ierr);
204     snes->iter = i+1;
205     snes->norm = fnorm;
206     ierr       = PetscObjectAMSGrantAccess((PetscObject)snes);CHKERRQ(ierr);
207     ierr       = SNESLogConvergenceHistory(snes,snes->norm,0);CHKERRQ(ierr);
208     ierr       = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
209     /* Test for convergence */
210     ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
211     if (snes->reason) break;
212   }
213   if (i == maxits) {
214     ierr = PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);CHKERRQ(ierr);
215     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
216   }
217   PetscFunctionReturn(0);
218 }
219 
220 /*MC
221   SNESNRICHARDSON - Richardson nonlinear solver that uses successive substitutions, also sometimes known as Picard iteration.
222 
223   Level: beginner
224 
225   Options Database:
226 +   -snes_linesearch_type <l2,cp,basic> Line search type.
227 -   -snes_linesearch_damping<1.0> Damping for the line search.
228 
229   Notes: If no inner nonlinear preconditioner is provided then solves F(x) - b = 0 using x^{n+1} = x^{n} - lambda
230             (F(x^n) - b) where lambda is obtained either SNESLineSearchSetDamping(), -snes_damping or a line search.  If
231             an inner nonlinear preconditioner is provided (either with -npc_snes_type or SNESSetPC()) then the inner
232             solver is called an initial solution x^n and the nonlinear Richardson uses x^{n+1} = x^{n} + lambda d^{n}
233             where d^{n} = \hat{x}^{n} - x^{n} where \hat{x}^{n} is the solution returned from the inner solver.
234 
235             The update, especially without inner nonlinear preconditioner, may be ill-scaled.  If using the basic
236             linesearch, one may have to scale the update with -snes_linesearch_damping
237 
238      This uses no derivative information thus will be much slower then Newton's method obtained with -snes_type ls
239 
240 .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESQN, SNESNCG
241 M*/
242 #undef __FUNCT__
243 #define __FUNCT__ "SNESCreate_NRichardson"
244 PETSC_EXTERN PetscErrorCode SNESCreate_NRichardson(SNES snes)
245 {
246   PetscErrorCode   ierr;
247   SNES_NRichardson *neP;
248 
249   PetscFunctionBegin;
250   snes->ops->destroy        = SNESDestroy_NRichardson;
251   snes->ops->setup          = SNESSetUp_NRichardson;
252   snes->ops->setfromoptions = SNESSetFromOptions_NRichardson;
253   snes->ops->view           = SNESView_NRichardson;
254   snes->ops->solve          = SNESSolve_NRichardson;
255   snes->ops->reset          = SNESReset_NRichardson;
256 
257   snes->usesksp = PETSC_FALSE;
258   snes->usespc  = PETSC_TRUE;
259 
260   ierr       = PetscNewLog(snes, SNES_NRichardson, &neP);CHKERRQ(ierr);
261   snes->data = (void*) neP;
262 
263   if (!snes->tolerancesset) {
264     snes->max_funcs = 30000;
265     snes->max_its   = 10000;
266     snes->stol      = 1e-20;
267   }
268   PetscFunctionReturn(0);
269 }
270