/[escript]/trunk/paso/src/FGMRES.c
ViewVC logotype

Contents of /trunk/paso/src/FGMRES.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1476 - (show annotations)
Mon Apr 7 23:38:50 2008 UTC (11 years, 6 months ago) by gross
File MIME type: text/plain
File size: 6204 byte(s)
Jacobian-free Newton method added to Paso
1 /* $Id:$ */
2
3 /*******************************************************
4 *
5 * Copyright 2008 by University of Queensland
6 *
7 * http://esscc.uq.edu.au
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13 /*
14 * Purpose
15 * =======
16 *
17 * FGMRES solves the non-linear system f0+J_0*d=0
18 * where f0=F(x0), J_0 is the jacobian of F at x0.
19 *
20 * Convergence test: norm(f0+J_0*d)<=tolerance*norm(f0)
21 *
22 * Arguments
23 * =========
24 *
25 * Paso_Function * F evaluation of F (including any preconditioner)
26 *
27 * x0 (input) current point
28 *
29 * f0 (input) function F at current point x0
30 *
31 * d (output) solution of f0+J0*d=0 with accuracy tolerance
32 *
33 * iter (input/output)
34 * On input, the maximum num_iterations to be performed.
35 * On output, actual number of num_iterations performed.
36 * tolerance (input/output)
37 * On input, the allowable convergence measure for norm(f0+J0*d)/norm(f0)
38 * On output, the final value for norm(f0+J0*d)
39 * return value
40 *
41 * =SOLVER_NO_ERROR: Successful exit. approximate solution returned.
42 * =SOLVER_MAXNUM_ITER_REACHED
43 * =SOLVER_INPUT_ERROR Illegal parameter:
44 * =SOLVER_BREAKDOWN: bad luck!
45 * =SOLVER_MEMORY_ERROR : no memory available
46 *
47 * ==============================================================
48 */
49
50 #include "Common.h"
51 #include "Solver.h"
52 #ifdef _OPENMP
53 #include <omp.h>
54 #endif
55
56 err_t Paso_Solver_NLGMRES(
57 Paso_Function * F,
58 const double* f0,
59 const double* x0,
60 double * x,
61 dim_t *iter,
62 double* tolerance,
63 Paso_Performance* pp)
64 {
65 double static RENORMALIZATION_CONST=0.001;
66 dim_t l=(*iter)+1, iter_max=*iter,k=0,n=F->local_n,i,j;
67 double rel_tol=*tolerance, abs_tol, normf0, normv, normv2, hh, hr, nu, norm_of_residual=0.;
68 bool_t breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
69 double *h=NULL, **v=NULL, *c=NULL,*s=NULL,*g=NULL, *work=NULL;
70 err_t Status=SOLVER_NO_ERROR;
71
72 /* Test the input parameters. */
73 if (n < 0 || iter_max<=0 || l<1 || rel_tol<0) {
74 return SOLVER_INPUT_ERROR;
75 }
76 Paso_zeroes(n,x);
77 /*
78 * allocate memory:
79 */
80 h=TMPMEMALLOC(l*l,double);
81 v=TMPMEMALLOC(iter_max,double*);
82 c=TMPMEMALLOC(l,double);
83 s=TMPMEMALLOC(l,double);
84 g=TMPMEMALLOC(l,double);
85 work=TMPMEMALLOC(n,double);
86
87 if (h==NULL || v ==NULL || c== NULL || s == NULL || g== NULL || work==NULL) {
88 Status=SOLVER_MEMORY_ERROR;
89 } else {
90 for (i=0;i<iter_max;i++) v[i]=NULL;
91 for (i=0;i<iter_max;i++) {
92 v[i]=TMPMEMALLOC(n,double);
93 if (v[i]==NULL) {
94 Status=SOLVER_MEMORY_ERROR;
95 break;
96 }
97 }
98 }
99 if ( Status ==SOLVER_NO_ERROR ) {
100 /*
101 * the show begins:
102 */
103 normf0=Paso_l2(n,f0,F->mpi_info);
104 k=0;
105 convergeFlag=(ABS(normf0)<=0);
106 if (! convergeFlag) {
107 abs_tol=rel_tol*normf0;
108 Paso_zeroes(n,v[0]);
109 Paso_Update(n,1.,v[0],-1./normf0,f0);
110 g[0]=normf0;
111 while(! (breakFlag || maxIterFlag || convergeFlag)) {
112 k++;
113 /*
114 * call directional derivative function
115 */
116 Paso_FunctionDerivative(v[k],v[k-1],F,f0,x0,work);
117 normv=Paso_l2(n,v[k],F->mpi_info);
118 /*
119 * Modified Gram-Schmidt
120 */
121 for (j=0;j<k;j++){
122 hh=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
123 Paso_Update(n,1.,v[k],(-hh),v[j]);
124 h[INDEX2(j,k-1,l)]=hh;
125 }
126 normv2=Paso_l2(n,v[k],F->mpi_info);
127 h[INDEX2(k,k-1,l)]=normv2;
128 /*
129 * reorthogonalize
130 */
131 if (! (normv + RENORMALIZATION_CONST*normv2 > normv)) {
132 for (j=0;j<k;j++){
133 hr=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
134 h[INDEX2(j,k-1,l)]+=hr;
135 Paso_Update(n,1.,v[k],(-hr),v[j]);
136 }
137 normv2=Paso_l2(n,v[k],F->mpi_info);
138 h[INDEX2(k,k-1,l)]=normv2;
139 }
140 /*
141 * watch out for happy breakdown
142 */
143 if(normv2 > 0.) {
144 Paso_update(n,1./normv2,v[k],0.,v[k]);
145 }
146 /*
147 * Form and store the information for the new Givens rotation
148 */
149 ApplyGivensRotations(iter,&h[INDEX2(0,k-1,l)],c,s);
150 /*
151 * Don't divide by zero if solution has been found
152 */
153 g[k]=0;
154 nu=sqrt(h[INDEX2(k-1,k-1,l)]*h[INDEX2(k-1,k-1,l)]+h[INDEX2(k,k-1,l)]*h[INDEX2(k,k-1,l)]);
155 if (nu>0) {
156 c[k-1]=h[INDEX2(k-1,k-1,l)]/nu;
157 s[k-1]=-h[k,k-1]/nu;
158 h[INDEX2(k-1,k-1,l)]=c[k-1]*h[INDEX2(k-1,k-1,l)]-s[k-1]*h[k,k-1];
159 h[INDEX2(k,k-1,l)]=0;
160 ApplyGivensRotations(2,&(g[k-1]),&(c[k-1]),&(s[k-1]));
161 }
162 norm_of_residual=abs(g[k]);
163 maxIterFlag = (k>=iter_max);
164 convergeFlag = (abs(g[k]) <= abs_tol);
165 printf("FGMRES step %d: error %e (tol=%d)\n",k,abs(g[k]),abs_tol);
166 }
167 }
168 /*
169 * all done and ready for the forward substitution:
170 */
171
172 for (i=0;i<k;i++) {
173 for (j=0;j<i;j++) {
174 g[i]-=h[INDEX2(j,i,l)]*g[j];
175 }
176 g[i]/=h[INDEX2(i,i,l)];
177 Paso_Update(n,x[k],g[i],v[i]);
178 }
179 }
180 /*
181 * clean up:
182 */
183 if ( v !=NULL) {
184 for (i=0;i<iter_max;i++) TMPMEMFREE(v);
185 }
186 TMPMEMFREE(h);
187 TMPMEMFREE(v);
188 TMPMEMFREE(c);
189 TMPMEMFREE(s);
190 TMPMEMFREE(g);
191 TMPMEMFREE(work);
192 *iter=k;
193 *tolerance=norm_of_residual;
194 return Status;
195 }
196

  ViewVC Help
Powered by ViewVC 1.1.26