xref: /petsc/src/snes/impls/ncg/snesncg.c (revision bcaeba4d41d6ca6c6dc4189db20683073a9959ce)
1 #include <../src/snes/impls/ncg/snesncgimpl.h> /*I "petscsnes.h" I*/
2 const char *const SNESNCGTypes[] = {"FR","PRP","HS","DY","CD","SNESNCGType","SNES_NCG_",0};
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "SNESReset_NCG"
6 PetscErrorCode SNESReset_NCG(SNES snes)
7 {
8 
9   PetscFunctionBegin;
10   PetscFunctionReturn(0);
11 }
12 
13 #define SNESLINESEARCHNCGLINEAR "linear"
14 
15 /*
16   SNESDestroy_NCG - Destroys the private SNES_NCG context that was created with SNESCreate_NCG().
17 
18   Input Parameter:
19 . snes - the SNES context
20 
21   Application Interface Routine: SNESDestroy()
22 */
23 #undef __FUNCT__
24 #define __FUNCT__ "SNESDestroy_NCG"
25 PetscErrorCode SNESDestroy_NCG(SNES snes)
26 {
27   PetscErrorCode   ierr;
28 
29   PetscFunctionBegin;
30   ierr = PetscFree(snes->data);CHKERRQ(ierr);
31   PetscFunctionReturn(0);
32 }
33 
34 /*
35    SNESSetUp_NCG - Sets up the internal data structures for the later use
36    of the SNESNCG nonlinear solver.
37 
38    Input Parameters:
39 +  snes - the SNES context
40 -  x - the solution vector
41 
42    Application Interface Routine: SNESSetUp()
43  */
44 
45 EXTERN_C_BEGIN
46 extern PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch);
47 EXTERN_C_END
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "SNESSetUp_NCG"
51 PetscErrorCode SNESSetUp_NCG(SNES snes)
52 {
53   PetscErrorCode ierr;
54 
55   PetscFunctionBegin;
56   ierr = SNESDefaultGetWork(snes,2);CHKERRQ(ierr);
57   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
58   ierr = SNESLineSearchRegisterDynamic(SNESLINESEARCHNCGLINEAR, PETSC_NULL,"SNESLineSearchCreate_NCGLinear", SNESLineSearchCreate_NCGLinear);CHKERRQ(ierr);
59 
60   PetscFunctionReturn(0);
61 }
62 /*
63   SNESSetFromOptions_NCG - Sets various parameters for the SNESNCG method.
64 
65   Input Parameter:
66 . snes - the SNES context
67 
68   Application Interface Routine: SNESSetFromOptions()
69 */
70 #undef __FUNCT__
71 #define __FUNCT__ "SNESSetFromOptions_NCG"
72 static PetscErrorCode SNESSetFromOptions_NCG(SNES snes)
73 {
74   SNES_NCG           *ncg     = (SNES_NCG *)snes->data;
75   PetscErrorCode     ierr;
76   PetscBool          debug;
77   SNESLineSearch     linesearch;
78   SNESNCGType        ncgtype=ncg->type;
79 
80   PetscFunctionBegin;
81   ierr = PetscOptionsHead("SNES NCG options");CHKERRQ(ierr);
82   ierr = PetscOptionsBool("-snes_ncg_monitor","Monitor NCG iterations","SNES",ncg->monitor ? PETSC_TRUE: PETSC_FALSE, &debug, PETSC_NULL);CHKERRQ(ierr);
83   ierr = PetscOptionsEnum("-snes_ncg_type","NCG Beta type used","SNESNCGSetType",SNESNCGTypes,(PetscEnum)ncg->type,(PetscEnum*)&ncgtype,PETSC_NULL);CHKERRQ(ierr);
84   ierr = SNESNCGSetType(snes, ncgtype);CHKERRQ(ierr);
85   if (debug) {
86     ncg->monitor = PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm);CHKERRQ(ierr);
87   }
88   ierr = PetscOptionsTail();CHKERRQ(ierr);
89   if (!snes->linesearch) {
90     ierr = SNESGetSNESLineSearch(snes, &linesearch);CHKERRQ(ierr);
91     if (!snes->pc) {
92       ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHCP);CHKERRQ(ierr);
93     } else {
94       ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);CHKERRQ(ierr);
95     }
96   }
97   PetscFunctionReturn(0);
98 }
99 
100 /*
101   SNESView_NCG - Prints info from the SNESNCG data structure.
102 
103   Input Parameters:
104 + SNES - the SNES context
105 - viewer - visualization context
106 
107   Application Interface Routine: SNESView()
108 */
109 #undef __FUNCT__
110 #define __FUNCT__ "SNESView_NCG"
111 static PetscErrorCode SNESView_NCG(SNES snes, PetscViewer viewer)
112 {
113   PetscBool        iascii;
114   PetscErrorCode   ierr;
115 
116   PetscFunctionBegin;
117   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
118   if (iascii) {
119   }
120   PetscFunctionReturn(0);
121 }
122 
123 #undef __FUNCT__
124 #define __FUNCT__ "SNESLineSearchApply_NCGLinear"
125 PetscErrorCode SNESLineSearchApply_NCGLinear(SNESLineSearch linesearch)
126 {
127   PetscScalar      alpha, ptAp;
128   Vec              X, Y, F, W;
129   SNES             snes;
130   PetscErrorCode   ierr;
131   PetscReal        *fnorm, *xnorm, *ynorm;
132   MatStructure     flg = DIFFERENT_NONZERO_PATTERN;
133 
134   PetscFunctionBegin;
135 
136   ierr = SNESLineSearchGetSNES(linesearch, &snes);CHKERRQ(ierr);
137   X = linesearch->vec_sol;
138   W = linesearch->vec_sol_new;
139   F = linesearch->vec_func;
140   Y = linesearch->vec_update;
141   fnorm = &linesearch->fnorm;
142   xnorm = &linesearch->xnorm;
143   ynorm = &linesearch->ynorm;
144 
145   /*
146 
147    The exact step size for unpreconditioned linear CG is just:
148    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy)
149    */
150   ierr = SNESComputeJacobian(snes, X, &snes->jacobian, &snes->jacobian_pre, &flg);CHKERRQ(ierr);
151   ierr = VecDot(F, F, &alpha);CHKERRQ(ierr);
152   ierr = MatMult(snes->jacobian, Y, W);CHKERRQ(ierr);
153   ierr = VecDot(Y, W, &ptAp);CHKERRQ(ierr);
154   alpha = alpha / ptAp;
155   ierr = PetscPrintf(((PetscObject)snes)->comm, "alpha: %G\n", PetscRealPart(alpha));CHKERRQ(ierr);
156   ierr = VecAXPY(X, alpha, Y);CHKERRQ(ierr);
157   ierr = SNESComputeFunction(snes, X, F);CHKERRQ(ierr);
158 
159   ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
160   ierr = VecNorm(X, NORM_2, xnorm);CHKERRQ(ierr);
161   ierr = VecNorm(Y, NORM_2, ynorm);CHKERRQ(ierr);
162 
163  PetscFunctionReturn(0);
164 }
165 
166 EXTERN_C_BEGIN
167 #undef __FUNCT__
168 #define __FUNCT__ "SNESLineSearchCreate_NCGLinear"
169 
170 PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch linesearch)
171 {
172   PetscFunctionBegin;
173   linesearch->ops->apply          = SNESLineSearchApply_NCGLinear;
174   linesearch->ops->destroy        = PETSC_NULL;
175   linesearch->ops->setfromoptions = PETSC_NULL;
176   linesearch->ops->reset          = PETSC_NULL;
177   linesearch->ops->view           = PETSC_NULL;
178   linesearch->ops->setup          = PETSC_NULL;
179   PetscFunctionReturn(0);
180 }
181 EXTERN_C_END
182 
183 #undef __FUNCT__
184 #define __FUNCT__ "SNESNCGComputeYtJtF_Private"
185 /*
186 
187  Assuming F = SNESComputeFunction(X) compute Y^tJ^tF using a simple secant approximation of the jacobian.
188 
189  */
190 PetscErrorCode SNESNCGComputeYtJtF_Private(SNES snes, Vec X, Vec F, Vec Y, Vec W, Vec G, PetscScalar * ytJtf) {
191   PetscErrorCode ierr;
192   PetscScalar    ftf, ftg, fty, h;
193   PetscFunctionBegin;
194   ierr = VecDot(F, F, &ftf);CHKERRQ(ierr);
195   ierr = VecDot(F, Y, &fty);CHKERRQ(ierr);
196   h = 1e-5*fty / fty;
197   ierr = VecCopy(X, W);CHKERRQ(ierr);
198   ierr = VecAXPY(W, -h, Y);CHKERRQ(ierr);            /* this is arbitrary */
199   ierr = SNESComputeFunction(snes, W, G);CHKERRQ(ierr);
200   ierr = VecDot(G, F, &ftg);CHKERRQ(ierr);
201   *ytJtf = (ftg - ftf) / h;
202   PetscFunctionReturn(0);
203 }
204 
205 #undef __FUNCT__
206 #define __FUNCT__ "SNESNCGSetType"
207 /*@
208     SNESNCGSetType - Sets the conjugate update type for SNESNCG.
209 
210     Logically Collective on SNES
211 
212     Input Parameters:
213 +   snes - the iterative context
214 -   btype - update type
215 
216     Options Database:
217 .   -snes_ncg_type<prp,fr,hs,dy,cd>
218 
219     Level: intermediate
220 
221     SNESNCGSelectTypes:
222 +   SNES_NCG_FR - Fletcher-Reeves update
223 .   SNES_NCG_PRP - Polak-Ribiere-Polyak update
224 .   SNES_NCG_HS - Hestenes-Steifel update
225 .   SNES_NCG_DY - Dai-Yuan update
226 -   SNES_NCG_CD - Conjugate Descent update
227 
228    Notes:
229    PRP is the default, and the only one that tolerates generalized search directions.
230 
231 .keywords: SNES, SNESNCG, selection, type, set
232 @*/
233 PetscErrorCode SNESNCGSetType(SNES snes, SNESNCGType btype) {
234   PetscErrorCode ierr;
235   PetscFunctionBegin;
236   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
237   ierr = PetscTryMethod(snes,"SNESNCGSetType_C",(SNES,SNESNCGType),(snes,btype));CHKERRQ(ierr);
238   PetscFunctionReturn(0);
239 }
240 
241 
242 EXTERN_C_BEGIN
243 #undef __FUNCT__
244 #define __FUNCT__ "SNESNCGSetType_NCG"
245 PetscErrorCode SNESNCGSetType_NCG(SNES snes, SNESNCGType btype) {
246   SNES_NCG *ncg = (SNES_NCG *)snes->data;
247   PetscFunctionBegin;
248   ncg->type = btype;
249   PetscFunctionReturn(0);
250 }
251 EXTERN_C_END
252 
253 /*
254   SNESSolve_NCG - Solves a nonlinear system with the Nonlinear Conjugate Gradient method.
255 
256   Input Parameters:
257 . snes - the SNES context
258 
259   Output Parameter:
260 . outits - number of iterations until termination
261 
262   Application Interface Routine: SNESSolve()
263 */
264 #undef __FUNCT__
265 #define __FUNCT__ "SNESSolve_NCG"
266 PetscErrorCode SNESSolve_NCG(SNES snes)
267 {
268   SNES_NCG            *ncg = (SNES_NCG *)snes->data;
269   Vec                 X, dX, lX, F, B, Fold;
270   PetscReal           fnorm, ynorm, xnorm, beta = 0.0;
271   PetscScalar         dXdotF, dXolddotFold, dXdotFold, lXdotF, lXdotFold;
272   PetscInt            maxits, i;
273   PetscErrorCode      ierr;
274   SNESConvergedReason reason;
275   PetscBool           lsSuccess = PETSC_TRUE;
276   SNESLineSearch     linesearch;
277 
278   PetscFunctionBegin;
279   snes->reason = SNES_CONVERGED_ITERATING;
280 
281   maxits = snes->max_its;            /* maximum number of iterations */
282   X      = snes->vec_sol;            /* X^n */
283   Fold   = snes->work[0];            /* The previous iterate of X */
284   dX     = snes->work[1];            /* the preconditioned direction */
285   lX     = snes->vec_sol_update;     /* the conjugate direction */
286   F      = snes->vec_func;           /* residual vector */
287   B      = snes->vec_rhs;            /* the right hand side */
288 
289   ierr = SNESGetSNESLineSearch(snes, &linesearch);CHKERRQ(ierr);
290 
291   ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
292   snes->iter = 0;
293   snes->norm = 0.;
294   ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
295 
296   /* compute the initial function and preconditioned update dX */
297   if (!snes->vec_func_init_set) {
298     ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
299     if (snes->domainerror) {
300       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
301       PetscFunctionReturn(0);
302     }
303   } else {
304     snes->vec_func_init_set = PETSC_FALSE;
305   }
306   if (!snes->norm_init_set) {
307   /* convergence test */
308     ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm <- ||F||  */
309     if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
310   } else {
311     fnorm = snes->norm_init;
312     snes->norm_init_set = PETSC_FALSE;
313   }
314   ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
315   snes->norm = fnorm;
316   ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
317   SNESLogConvHistory(snes,fnorm,0);
318   ierr = SNESMonitor(snes,0,fnorm);CHKERRQ(ierr);
319 
320   /* set parameter for default relative tolerance convergence test */
321   snes->ttol = fnorm*snes->rtol;
322   /* test convergence */
323   ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
324   if (snes->reason) PetscFunctionReturn(0);
325 
326   /* Call general purpose update function */
327   if (snes->ops->update) {
328     ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
329  }
330 
331   /* first update -- just use the (preconditioned) residual direction for the initial conjugate direction */
332 
333   if (snes->pc && snes->pcside == PC_RIGHT) {
334     ierr = VecCopy(X, dX);CHKERRQ(ierr);
335     ierr = SNESSetInitialFunction(snes->pc, F);CHKERRQ(ierr);
336     ierr = SNESSetInitialFunctionNorm(snes->pc, fnorm);CHKERRQ(ierr);
337     ierr = SNESSolve(snes->pc, B, dX);CHKERRQ(ierr);
338     ierr = SNESGetConvergedReason(snes->pc,&reason);CHKERRQ(ierr);
339     if (reason < 0 && (reason != SNES_DIVERGED_MAX_IT)) {
340       snes->reason = SNES_DIVERGED_INNER;
341       PetscFunctionReturn(0);
342     }
343     ierr = VecAYPX(dX,-1.0,X);CHKERRQ(ierr);
344   } else {
345     ierr = VecCopy(F, dX);CHKERRQ(ierr);
346   }
347   ierr = VecCopy(dX, lX);CHKERRQ(ierr);
348   ierr = VecDot(F, dX, &dXdotF);CHKERRQ(ierr);
349   /*
350   } else {
351     ierr = SNESNCGComputeYtJtF_Private(snes, X, F, dX, W, G, &dXdotF);CHKERRQ(ierr);
352   }
353    */
354   for (i = 1; i < maxits + 1; i++) {
355     lsSuccess = PETSC_TRUE;
356     /* some update types require the old update direction or conjugate direction */
357     if (ncg->type != SNES_NCG_FR) {
358       ierr = VecCopy(F, Fold);CHKERRQ(ierr);
359     }
360     ierr = SNESLineSearchApply(linesearch, X, F, &fnorm, lX);CHKERRQ(ierr);
361     ierr = SNESLineSearchGetSuccess(linesearch, &lsSuccess);CHKERRQ(ierr);
362     if (!lsSuccess) {
363       if (++snes->numFailures >= snes->maxFailures) {
364         snes->reason = SNES_DIVERGED_LINE_SEARCH;
365         PetscFunctionReturn(0);
366       }
367     }
368     if (snes->nfuncs >= snes->max_funcs) {
369       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
370       PetscFunctionReturn(0);
371     }
372     if (snes->domainerror) {
373       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
374       PetscFunctionReturn(0);
375     }
376     ierr = SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm);CHKERRQ(ierr);
377     /* Monitor convergence */
378     ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr);
379     snes->iter = i;
380     snes->norm = fnorm;
381     ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr);
382     SNESLogConvHistory(snes,snes->norm,0);
383     ierr = SNESMonitor(snes,snes->iter,snes->norm);CHKERRQ(ierr);
384 
385     /* Test for convergence */
386     ierr = (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr);
387     if (snes->reason) PetscFunctionReturn(0);
388 
389     /* Call general purpose update function */
390     if (snes->ops->update) {
391       ierr = (*snes->ops->update)(snes, snes->iter);CHKERRQ(ierr);
392     }
393     if (snes->pc && snes->pcside == PC_RIGHT) {
394       ierr = VecCopy(X,dX);CHKERRQ(ierr);
395       ierr = SNESSetInitialFunction(snes->pc, F);CHKERRQ(ierr);
396       ierr = SNESSetInitialFunctionNorm(snes->pc, fnorm);CHKERRQ(ierr);
397       ierr = SNESSolve(snes->pc, B, dX);CHKERRQ(ierr);
398       ierr = SNESGetConvergedReason(snes->pc,&reason);CHKERRQ(ierr);
399       if (reason < 0 && (reason != SNES_DIVERGED_MAX_IT)) {
400         snes->reason = SNES_DIVERGED_INNER;
401         PetscFunctionReturn(0);
402       }
403       ierr = VecAYPX(dX,-1.0,X);CHKERRQ(ierr);
404     } else {
405       ierr = VecCopy(F, dX);CHKERRQ(ierr);
406     }
407 
408     /* compute the conjugate direction lX = dX + beta*lX with beta = ((dX, dX) / (dX_old, dX_old) (Fletcher-Reeves update)*/
409     switch(ncg->type) {
410     case SNES_NCG_FR: /* Fletcher-Reeves */
411       dXolddotFold = dXdotF;
412       ierr = VecDot(dX, dX, &dXdotF);CHKERRQ(ierr);
413       beta = PetscRealPart(dXdotF / dXolddotFold);
414       break;
415     case SNES_NCG_PRP: /* Polak-Ribiere-Poylak */
416       dXolddotFold = dXdotF;
417       ierr = VecDotBegin(F, dX, &dXdotF);CHKERRQ(ierr);
418       ierr = VecDotBegin(Fold, dX, &dXdotFold);CHKERRQ(ierr);
419       ierr = VecDotEnd(F, dX, &dXdotF);CHKERRQ(ierr);
420       ierr = VecDotEnd(Fold, dX, &dXdotFold);CHKERRQ(ierr);
421       beta = PetscRealPart(((dXdotF - dXdotFold) / dXolddotFold));
422       if (beta < 0.0) beta = 0.0; /* restart */
423       break;
424     case SNES_NCG_HS: /* Hestenes-Stiefel */
425       ierr = VecDotBegin(dX, F, &dXdotF);CHKERRQ(ierr);
426       ierr = VecDotBegin(dX, Fold, &dXdotFold);CHKERRQ(ierr);
427       ierr = VecDotBegin(lX, F, &lXdotF);CHKERRQ(ierr);
428       ierr = VecDotBegin(lX, Fold, &lXdotFold);CHKERRQ(ierr);
429       ierr = VecDotEnd(dX, F, &dXdotF);CHKERRQ(ierr);
430       ierr = VecDotEnd(dX, Fold, &dXdotFold);CHKERRQ(ierr);
431       ierr = VecDotEnd(lX, F, &lXdotF);CHKERRQ(ierr);
432       ierr = VecDotEnd(lX, Fold, &lXdotFold);CHKERRQ(ierr);
433       beta = PetscRealPart((dXdotF - dXdotFold) / (lXdotF - lXdotFold));
434       break;
435     case SNES_NCG_DY: /* Dai-Yuan */
436       ierr = VecDotBegin(dX, F, &dXdotF);CHKERRQ(ierr);
437       ierr = VecDotBegin(lX, F, &lXdotF);CHKERRQ(ierr);
438       ierr = VecDotBegin(lX, Fold, &lXdotFold);CHKERRQ(ierr);
439       ierr = VecDotEnd(dX, F, &dXdotF);CHKERRQ(ierr);
440       ierr = VecDotEnd(lX, F, &lXdotF);CHKERRQ(ierr);
441       ierr = VecDotEnd(lX, Fold, &lXdotFold);CHKERRQ(ierr);
442       beta = PetscRealPart(dXdotF / (lXdotFold - lXdotF));CHKERRQ(ierr);
443       break;
444     case SNES_NCG_CD: /* Conjugate Descent */
445       ierr = VecDotBegin(dX, F, &dXdotF);CHKERRQ(ierr);
446       ierr = VecDotBegin(lX, Fold, &lXdotFold);CHKERRQ(ierr);
447       ierr = VecDotEnd(dX, F, &dXdotF);CHKERRQ(ierr);
448       ierr = VecDotEnd(lX, Fold, &lXdotFold);CHKERRQ(ierr);
449       beta = PetscRealPart(dXdotF / lXdotFold);CHKERRQ(ierr);
450       break;
451     }
452     if (ncg->monitor) {
453       ierr = PetscViewerASCIIPrintf(ncg->monitor, "beta = %e\n", beta);CHKERRQ(ierr);
454     }
455     ierr = VecAYPX(lX, beta, dX);CHKERRQ(ierr);
456   }
457   ierr = PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);CHKERRQ(ierr);
458   if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
459   PetscFunctionReturn(0);
460 }
461 
462 
463 
464 /*MC
465   SNESNCG - Nonlinear Conjugate-Gradient method for the solution of general nonlinear systems.
466 
467   Level: beginner
468 
469   Options Database:
470 +   -snes_ncg_type <fr, prp, dy, hs, cd> - Choice of conjugate-gradient update parameter.
471 .   -snes_linesearch_type <cp,l2,basic> - Line search type.
472 -   -snes_ncg_monitor - Print relevant information about the ncg iteration.
473 
474 Notes: This solves the nonlinear system of equations F(x) = 0 using the nonlinear generalization of the conjugate
475 gradient method.  This may be used with a nonlinear preconditioner used to pick the new search directions, but otherwise
476 chooses the initial search direction as F(x) for the initial guess x.
477 
478 
479 .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESNQN
480 M*/
481 EXTERN_C_BEGIN
482 #undef __FUNCT__
483 #define __FUNCT__ "SNESCreate_NCG"
484 PetscErrorCode  SNESCreate_NCG(SNES snes)
485 {
486   PetscErrorCode   ierr;
487   SNES_NCG * neP;
488 
489   PetscFunctionBegin;
490   snes->ops->destroy         = SNESDestroy_NCG;
491   snes->ops->setup           = SNESSetUp_NCG;
492   snes->ops->setfromoptions  = SNESSetFromOptions_NCG;
493   snes->ops->view            = SNESView_NCG;
494   snes->ops->solve           = SNESSolve_NCG;
495   snes->ops->reset           = SNESReset_NCG;
496 
497   snes->usesksp              = PETSC_FALSE;
498   snes->usespc               = PETSC_TRUE;
499 
500   if (!snes->tolerancesset) {
501     snes->max_funcs = 30000;
502     snes->max_its   = 10000;
503     snes->stol      = 1e-20;
504   }
505 
506   ierr = PetscNewLog(snes, SNES_NCG, &neP);CHKERRQ(ierr);
507   snes->data = (void*) neP;
508   neP->monitor = PETSC_NULL;
509   neP->type = SNES_NCG_PRP;
510   ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESNCGSetType_C","SNESNCGSetType_NCG", SNESNCGSetType_NCG);CHKERRQ(ierr);
511   PetscFunctionReturn(0);
512 }
513 EXTERN_C_END
514