xref: /petsc/src/snes/impls/tr/tr.c (revision eafb4bcbebb1c0621d2677f4c5d850d03cbd72eb)
1 #ifndef lint
2 static char vcid[] = "$Id: tr.c,v 1.3 1995/04/15 03:29:42 bsmith Exp bsmith $";
3 #endif
4 
5 #include <math.h>
6 #include "tr.h"
7 
8 
9 /*
10       Implements Newton's Method with a 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;
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_res;		/* 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)(X,&snes->jacobian,snes->jacP);
62      ierr = SLESSetOperators(snes->sles,snes->jacobian,snes->jacobian,0);
63      ierr = SLESSolve(snes->sles,F,Ytmp,&lits); CHKERR(ierr);
64      VecNorm( Ytmp, &norm );
65      while(1) {
66        VecCopy(Ytmp,Y);
67        /* Scale Y if need be and predict new value of F norm */
68 
69        if (norm >= delta) {
70          norm = delta/norm;
71          gpnorm = (1.0 - norm)*fnorm;
72          cnorm = norm;
73          VecScale( &cnorm, Y );
74          norm = gpnorm;
75          PLogInfo((PetscObject)snes, "Scaling direction by %g \n",norm );
76          ynorm = delta;
77        } else {
78          gpnorm = 0.0;
79          PLogInfo((PetscObject)snes,"Direction is in Trust Region \n" );
80          ynorm = norm;
81        }
82        VecAXPY(&one, X, Y );	/* Y <- X + Y */
83        ierr = SNESComputeFunction(snes,Y,G); CHKERR(ierr); /* (+/-) F(X) */
84        VecNorm( G, &gnorm );	/* gnorm <- || g || */
85        if (fnorm == gpnorm) rho = 0.0;
86        else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
87 
88        /* Update size of trust region */
89        if      (rho < neP->mu)  delta *= neP->delta1;
90        else if (rho < neP->eta) delta *= neP->delta2;
91        else                     delta *= neP->delta3;
92 
93        PLogInfo((PetscObject)snes,"%d:  f_norm=%g, g_norm=%g, ynorm=%g\n",
94                                              i, fnorm, gnorm, ynorm );
95        PLogInfo((PetscObject)snes,"gpred=%g, rho=%g, delta=%g,iters=%d\n",
96                                                gpnorm, rho, delta, lits);
97 
98        neP->delta = delta;
99        if (rho > neP->sigma) break;
100        PLogInfo((PetscObject)snes,"Trying again in smaller region\n");
101        /* check to see if progress is hopeless */
102        if (neP->delta < xnorm * epsmch)	return -1;
103      }
104      fnorm = gnorm;
105      snes->norm = fnorm;
106      if (history && history_len > i+1) history[i+1] = fnorm;
107      TMP = F; F = G; G = TMP;
108      TMP = X; X = Y; Y = TMP;
109      VecNorm(X, &xnorm );		/* xnorm = || X || */
110      if (snes->Monitor) (*snes->Monitor)(snes,i,fnorm,snes->monP);
111 
112      /* Test for convergence */
113      if ((*snes->Converged)( snes, xnorm, ynorm, fnorm,snes->cnvP )) {
114        /* Verify solution is in corect location */
115        if (X != snes->vec_sol) VecCopy(X, snes->vec_sol );
116        break;
117      }
118    }
119    if (i == maxits) *its = i-1; else *its = i;
120    return 0;
121 }
122 /* -------------------------------------------------------------*/
123 
124 /*------------------------------------------------------------*/
125 static int SNESSetUp_TR( SNES snes )
126 {
127   int ierr;
128   snes->nwork = 3;
129   ierr = VecGetVecs(snes->vec_sol,snes->nwork,&snes->work ); CHKERR(ierr);
130   return 0;
131 }
132 /*------------------------------------------------------------*/
133 static int SNESDestroy_TR(PetscObject obj )
134 {
135   SNES snes = (SNES) obj;
136   VecFreeVecs(snes->work, snes->nwork );
137   return 0;
138 }
139 /*------------------------------------------------------------*/
140 
141 #include "options.h"
142 static int SNESSetFromOptions_TR(SNES snes)
143 {
144   SNES_TR *ctx = (SNES_TR *)snes->data;
145   double  tmp;
146 
147   if (OptionsGetDouble(0,snes->prefix,"-mu",&tmp)) {ctx->mu = tmp;}
148   if (OptionsGetDouble(0,snes->prefix,"-eta",&tmp)) {ctx->eta = tmp;}
149   if (OptionsGetDouble(0,snes->prefix,"-sigma",&tmp)) {ctx->sigma = tmp;}
150   if (OptionsGetDouble(0,snes->prefix,"-delta0",&tmp)) {ctx->delta0 = tmp;}
151   if (OptionsGetDouble(0,snes->prefix,"-delta1",&tmp)) {ctx->delta1 = tmp;}
152   if (OptionsGetDouble(0,snes->prefix,"-delta2",&tmp)) {ctx->delta2 = tmp;}
153   if (OptionsGetDouble(0,snes->prefix,"-delta3",&tmp)) {ctx->delta3 = tmp;}
154   return 0;
155 }
156 
157 static int SNESPrintHelp_TR(SNES snes)
158 {
159   SNES_TR *ctx = (SNES_TR *)snes->data;
160   char    *prefix = "-";
161   if (snes->prefix) prefix = snes->prefix;
162   fprintf(stderr,"%smu mu (default %g)\n",prefix,ctx->mu);
163   fprintf(stderr,"%seta eta (default %g)\n",prefix,ctx->eta);
164   fprintf(stderr,"%ssigma sigma (default %g)\n",prefix,ctx->sigma);
165   fprintf(stderr,"%sdelta0 delta0 (default %g)\n",prefix,ctx->delta0);
166   fprintf(stderr,"%sdelta1 delta1 (default %g)\n",prefix,ctx->delta1);
167   fprintf(stderr,"%sdelta2 delta2 (default %g)\n",prefix,ctx->delta2);
168   fprintf(stderr,"%sdelta3 delta3 (default %g)\n",prefix,ctx->delta3);
169   return 0;
170 }
171 
172 int SNESCreate_TR(SNES snes )
173 {
174   SNES_TR *neP;
175 
176   snes->type 		= SNES_NTR;
177   snes->Setup		= SNESSetUp_TR;
178   snes->Solver		= SNESSolve_TR;
179   snes->destroy		= SNESDestroy_TR;
180   snes->Converged	= SNESDefaultConverged;
181   snes->PrintHelp       = SNESPrintHelp_TR;
182   snes->SetFromOptions  = SNESSetFromOptions_TR;
183 
184   neP			= NEW(SNES_TR); CHKPTR(neP);
185   snes->data	        = (void *) neP;
186   neP->mu		= 0.25;
187   neP->eta		= 0.75;
188   neP->delta		= 0.0;
189   neP->delta0		= 0.2;
190   neP->delta1		= 0.3;
191   neP->delta2		= 0.75;
192   neP->delta3		= 2.0;
193   neP->sigma		= 0.0001;
194   neP->itflag		= 0;
195   return 0;
196 }
197