xref: /petsc/src/snes/impls/tr/tr.c (revision 39e2f89b4790e04d6b39c28ebf52a759d3976650)
1 #ifndef lint
2 static char vcid[] = "$Id: tr.c,v 1.5 1995/05/02 16:06:34 bsmith Exp bsmith $";
3 #endif
4 
5 #include <math.h>
6 #include "tr.h"
7 
8 
9 /*
10       Implements Newton's Method with a very simple trust region
11     approach for solving systems of nonlinear equations.
12 
13     Input parameters:
14 .   nlP - nonlinear context obtained from SNESCreate()
15 
16     The basic algorithm is taken from "The Minpack Project", by More',
17     Sorensen, Garbow, Hillstrom, pages 88-111 of "Sources and Development
18     of Mathematical Software", Wayne Cowell, editor.  See the examples
19     in nonlin/examples.
20 */
21 /*
22    This is intended as a model implementation, since it does not
23    necessarily have many of the bells and whistles of other
24    implementations.
25 
26 */
27 static int SNESSolve_TR(SNES snes, int *its )
28 {
29   SNES_TR  *neP = (SNES_TR *) snes->data;
30   Vec      X, F, Y, G, TMP, Ytmp;
31   int      maxits, i, history_len, nlconv,ierr,lits, flg;
32   double   rho, fnorm, gnorm, gpnorm, xnorm, delta,norm;
33   double   *history, ynorm;
34   Scalar   one = 1.0,cnorm;
35   double  epsmch = 1.0e-14;   /* This must be fixed */
36 
37   nlconv	= 0;			/* convergence monitor */
38   history	= snes->conv_hist;	/* convergence history */
39   history_len	= snes->conv_hist_len;	/* convergence history length */
40   maxits	= snes->max_its;	/* maximum number of iterations */
41   X		= snes->vec_sol;		/* solution vector */
42   F		= snes->vec_func;		/* residual vector */
43   Y		= snes->work[0];		/* work vectors */
44   G		= snes->work[1];
45   Ytmp          = snes->work[2];
46 
47   ierr = SNESComputeInitialGuess(snes,X); CHKERR(ierr);  /* X <- X_0 */
48   VecNorm(X, &xnorm ); 		/* xnorm = || X || */
49 
50   ierr = SNESComputeFunction(snes,X,F); CHKERR(ierr); /* (+/-) F(X) */
51   VecNorm(F, &fnorm );		/* fnorm <- || F || */
52   snes->norm = fnorm;
53   if (history && history_len > 0) history[0] = fnorm;
54   delta = neP->delta0*fnorm;
55   neP->delta = delta;
56   if (snes->Monitor)(*snes->Monitor)(snes,0,fnorm,snes->monP);
57 
58    for ( i=0; i<maxits; i++ ) {
59      snes->iter = i+1;
60 
61      (*snes->ComputeJacobian)(snes,X,&snes->jacobian,&snes->jacobian_pre,
62                                                              &flg,snes->jacP);
63      ierr = SLESSetOperators(snes->sles,snes->jacobian,snes->jacobian_pre,flg);
64      ierr = SLESSolve(snes->sles,F,Ytmp,&lits); CHKERR(ierr);
65      VecNorm( Ytmp, &norm );
66      while(1) {
67        VecCopy(Ytmp,Y);
68        /* Scale Y if need be and predict new value of F norm */
69 
70        if (norm >= delta) {
71          norm = delta/norm;
72          gpnorm = (1.0 - norm)*fnorm;
73          cnorm = norm;
74          VecScale( &cnorm, Y );
75          norm = gpnorm;
76          PLogInfo((PetscObject)snes, "Scaling direction by %g \n",norm );
77          ynorm = delta;
78        } else {
79          gpnorm = 0.0;
80          PLogInfo((PetscObject)snes,"Direction is in Trust Region \n" );
81          ynorm = norm;
82        }
83        VecAXPY(&one, X, Y );	/* Y <- X + Y */
84        ierr = SNESComputeFunction(snes,Y,G); CHKERR(ierr); /* (+/-) F(X) */
85        VecNorm( G, &gnorm );	/* gnorm <- || g || */
86        if (fnorm == gpnorm) rho = 0.0;
87        else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
88 
89        /* Update size of trust region */
90        if      (rho < neP->mu)  delta *= neP->delta1;
91        else if (rho < neP->eta) delta *= neP->delta2;
92        else                     delta *= neP->delta3;
93 
94        PLogInfo((PetscObject)snes,"%d:  f_norm=%g, g_norm=%g, ynorm=%g\n",
95                                              i, fnorm, gnorm, ynorm );
96        PLogInfo((PetscObject)snes,"gpred=%g, rho=%g, delta=%g,iters=%d\n",
97                                                gpnorm, rho, delta, lits);
98 
99        neP->delta = delta;
100        if (rho > neP->sigma) break;
101        PLogInfo((PetscObject)snes,"Trying again in smaller region\n");
102        /* check to see if progress is hopeless */
103        if (neP->delta < xnorm * epsmch)	return -1;
104      }
105      fnorm = gnorm;
106      snes->norm = fnorm;
107      if (history && history_len > i+1) history[i+1] = fnorm;
108      TMP = F; F = G; snes->vec_func_always = F; G = TMP;
109      TMP = X; X = Y;snes->vec_sol_always = X; Y = TMP;
110      VecNorm(X, &xnorm );		/* xnorm = || X || */
111      if (snes->Monitor) (*snes->Monitor)(snes,i,fnorm,snes->monP);
112 
113      /* Test for convergence */
114      if ((*snes->Converged)( snes, xnorm, ynorm, fnorm,snes->cnvP )) {
115        /* Verify solution is in corect location */
116        if (X != snes->vec_sol) {
117          VecCopy(X, snes->vec_sol );
118          snes->vec_sol_always = snes->vec_sol;
119          snes->vec_func_always = snes->vec_func;
120        }
121        break;
122      }
123    }
124    if (i == maxits) *its = i-1; else *its = i;
125    return 0;
126 }
127 /* -------------------------------------------------------------*/
128 
129 /*------------------------------------------------------------*/
130 static int SNESSetUp_TR( SNES snes )
131 {
132   int ierr;
133   snes->nwork = 3;
134   ierr = VecGetVecs(snes->vec_sol,snes->nwork,&snes->work ); CHKERR(ierr);
135   return 0;
136 }
137 /*------------------------------------------------------------*/
138 static int SNESDestroy_TR(PetscObject obj )
139 {
140   SNES snes = (SNES) obj;
141   VecFreeVecs(snes->work, snes->nwork );
142   return 0;
143 }
144 /*------------------------------------------------------------*/
145 
146 #include "options.h"
147 static int SNESSetFromOptions_TR(SNES snes)
148 {
149   SNES_TR *ctx = (SNES_TR *)snes->data;
150   double  tmp;
151 
152   if (OptionsGetDouble(0,snes->prefix,"-mu",&tmp)) {ctx->mu = tmp;}
153   if (OptionsGetDouble(0,snes->prefix,"-eta",&tmp)) {ctx->eta = tmp;}
154   if (OptionsGetDouble(0,snes->prefix,"-sigma",&tmp)) {ctx->sigma = tmp;}
155   if (OptionsGetDouble(0,snes->prefix,"-delta0",&tmp)) {ctx->delta0 = tmp;}
156   if (OptionsGetDouble(0,snes->prefix,"-delta1",&tmp)) {ctx->delta1 = tmp;}
157   if (OptionsGetDouble(0,snes->prefix,"-delta2",&tmp)) {ctx->delta2 = tmp;}
158   if (OptionsGetDouble(0,snes->prefix,"-delta3",&tmp)) {ctx->delta3 = tmp;}
159   return 0;
160 }
161 
162 static int SNESPrintHelp_TR(SNES snes)
163 {
164   SNES_TR *ctx = (SNES_TR *)snes->data;
165   char    *prefix = "-";
166   if (snes->prefix) prefix = snes->prefix;
167   fprintf(stderr,"%smu mu (default %g)\n",prefix,ctx->mu);
168   fprintf(stderr,"%seta eta (default %g)\n",prefix,ctx->eta);
169   fprintf(stderr,"%ssigma sigma (default %g)\n",prefix,ctx->sigma);
170   fprintf(stderr,"%sdelta0 delta0 (default %g)\n",prefix,ctx->delta0);
171   fprintf(stderr,"%sdelta1 delta1 (default %g)\n",prefix,ctx->delta1);
172   fprintf(stderr,"%sdelta2 delta2 (default %g)\n",prefix,ctx->delta2);
173   fprintf(stderr,"%sdelta3 delta3 (default %g)\n",prefix,ctx->delta3);
174   return 0;
175 }
176 
177 int SNESCreate_TR(SNES snes )
178 {
179   SNES_TR *neP;
180 
181   snes->type 		= SNES_NTR;
182   snes->Setup		= SNESSetUp_TR;
183   snes->Solver		= SNESSolve_TR;
184   snes->destroy		= SNESDestroy_TR;
185   snes->Converged	= SNESDefaultConverged;
186   snes->PrintHelp       = SNESPrintHelp_TR;
187   snes->SetFromOptions  = SNESSetFromOptions_TR;
188 
189   neP			= NEW(SNES_TR); CHKPTR(neP);
190   snes->data	        = (void *) neP;
191   neP->mu		= 0.25;
192   neP->eta		= 0.75;
193   neP->delta		= 0.0;
194   neP->delta0		= 0.2;
195   neP->delta1		= 0.3;
196   neP->delta2		= 0.75;
197   neP->delta3		= 2.0;
198   neP->sigma		= 0.0001;
199   neP->itflag		= 0;
200   return 0;
201 }
202