/[escript]/trunk/paso/src/GMRES2.cpp
ViewVC logotype

Annotation of /trunk/paso/src/GMRES2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2987 - (hide annotations)
Tue Mar 16 01:32:43 2010 UTC (9 years, 7 months ago) by gross
Original Path: trunk/paso/src/GMRES2.c
File MIME type: text/plain
File size: 5501 byte(s)
FCT solver rewritten
1 gross 1476
2     /*******************************************************
3     *
4 jfenwick 2881 * Copyright (c) 2003-2010 by University of Queensland
5 ksteube 1811 * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7 gross 1476 *
8 ksteube 1811 * 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 gross 1476 *
12 ksteube 1811 *******************************************************/
13 gross 1476
14 ksteube 1811
15 gross 1476 #include "Common.h"
16     #include "Solver.h"
17 gross 1639 #include "PasoUtil.h"
18 gross 1476 #ifdef _OPENMP
19     #include <omp.h>
20     #endif
21    
22 gross 1639 err_t Paso_Solver_GMRES2(
23 gross 1476 Paso_Function * F,
24     const double* f0,
25     const double* x0,
26     double * x,
27     dim_t *iter,
28     double* tolerance,
29     Paso_Performance* pp)
30     {
31     double static RENORMALIZATION_CONST=0.001;
32 gross 2987 const dim_t l=(*iter)+1, iter_max=*iter;
33     dim_t k=0,i,j;
34 gross 1639 const dim_t n=F->n;
35 gross 2987 const double rel_tol=*tolerance;
36     double abs_tol, normf0, normv, normv2, hh, hr, nu, norm_of_residual=0.;
37 gross 1476 bool_t breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
38     double *h=NULL, **v=NULL, *c=NULL,*s=NULL,*g=NULL, *work=NULL;
39     err_t Status=SOLVER_NO_ERROR;
40    
41     /* Test the input parameters. */
42     if (n < 0 || iter_max<=0 || l<1 || rel_tol<0) {
43     return SOLVER_INPUT_ERROR;
44     }
45     Paso_zeroes(n,x);
46     /*
47     * allocate memory:
48     */
49     h=TMPMEMALLOC(l*l,double);
50 gross 1804 v=TMPMEMALLOC(l,double*);
51 gross 1476 c=TMPMEMALLOC(l,double);
52     s=TMPMEMALLOC(l,double);
53     g=TMPMEMALLOC(l,double);
54     work=TMPMEMALLOC(n,double);
55    
56     if (h==NULL || v ==NULL || c== NULL || s == NULL || g== NULL || work==NULL) {
57     Status=SOLVER_MEMORY_ERROR;
58     }
59     if ( Status ==SOLVER_NO_ERROR ) {
60 gross 1639 for (i=0;i<iter_max;i++) v[i]=NULL;
61 gross 1476 /*
62     * the show begins:
63     */
64     normf0=Paso_l2(n,f0,F->mpi_info);
65     k=0;
66     convergeFlag=(ABS(normf0)<=0);
67     if (! convergeFlag) {
68     abs_tol=rel_tol*normf0;
69 gross 2987 printf("GMRES2 initial residual norm %e (rel. tol=%e)\n",normf0,rel_tol);
70 gross 1639 v[0]=TMPMEMALLOC(n,double);
71     if (v[0]==NULL) {
72     Status=SOLVER_MEMORY_ERROR;
73     } else {
74     Paso_zeroes(n,v[0]);
75     Paso_Update(n,1.,v[0],-1./normf0,f0);
76     g[0]=normf0;
77     }
78 gross 1798 while( (! (breakFlag || maxIterFlag || convergeFlag)) && (Status ==SOLVER_NO_ERROR) ) {
79 gross 1476 k++;
80 gross 1639 v[k]=TMPMEMALLOC(n,double);
81     if (v[k]==NULL) {
82     Status=SOLVER_MEMORY_ERROR;
83     } else {
84     /*
85     * call directional derivative function
86     */
87 gross 2987 Paso_FunctionDerivative(v[k],v[k-1],F,f0,x0,work,pp);
88 gross 1639 normv=Paso_l2(n,v[k],F->mpi_info);
89     /*
90     * Modified Gram-Schmidt
91     */
92     for (j=0;j<k;j++){
93     hh=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
94     Paso_Update(n,1.,v[k],(-hh),v[j]);
95     h[INDEX2(j,k-1,l)]=hh;
96     }
97     normv2=Paso_l2(n,v[k],F->mpi_info);
98     h[INDEX2(k,k-1,l)]=normv2;
99     /*
100     * reorthogonalize
101     */
102     if (! (normv + RENORMALIZATION_CONST*normv2 > normv)) {
103     for (j=0;j<k;j++){
104     hr=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
105     h[INDEX2(j,k-1,l)]+=hr;
106     Paso_Update(n,1.,v[k],(-hr),v[j]);
107     }
108     normv2=Paso_l2(n,v[k],F->mpi_info);
109     h[INDEX2(k,k-1,l)]=normv2;
110     }
111     /*
112     * watch out for happy breakdown
113     */
114     if(normv2 > 0.) {
115     Paso_Update(n,1./normv2,v[k],0.,v[k]);
116     }
117     /*
118     * Form and store the information for the new Givens rotation
119     */
120     ApplyGivensRotations(k,&h[INDEX2(0,k-1,l)],c,s);
121     /*
122     * Don't divide by zero if solution has been found
123     */
124     g[k]=0;
125     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)]);
126     if (nu>0) {
127     c[k-1]=h[INDEX2(k-1,k-1,l)]/nu;
128 gross 1798 s[k-1]=-h[INDEX2(k,k-1,l)]/nu;
129     h[INDEX2(k-1,k-1,l)]=c[k-1]*h[INDEX2(k-1,k-1,l)]-s[k-1]*h[INDEX2(k,k-1,l)];
130 gross 1639 h[INDEX2(k,k-1,l)]=0;
131     ApplyGivensRotations(2,&(g[k-1]),&(c[k-1]),&(s[k-1]));
132     }
133     norm_of_residual=fabs(g[k]);
134     maxIterFlag = (k>=iter_max);
135     convergeFlag = (fabs(g[k]) <= abs_tol);
136 gross 2987 printf("GMRES2 step %d: residual %e (abs. tol=%e)\n",k,fabs(g[k]),abs_tol);
137 gross 1639 }
138 gross 1476 }
139     }
140     /*
141     * all done and ready for the forward substitution:
142     */
143 gross 1798 for (i=k-1;i>=0;--i) {
144     for (j=i+1;j<k;j++) {
145     g[i]-=h[INDEX2(i,j,l)]*g[j];
146 gross 1476 }
147     g[i]/=h[INDEX2(i,i,l)];
148 gross 1477 Paso_Update(n,1.,x,g[i],v[i]);
149 gross 1476 }
150     }
151     /*
152     * clean up:
153     */
154     if ( v !=NULL) {
155 gross 1639 for (i=0;i<iter_max;i++) TMPMEMFREE(v[i]);
156 gross 1476 }
157     TMPMEMFREE(h);
158     TMPMEMFREE(v);
159     TMPMEMFREE(c);
160     TMPMEMFREE(s);
161     TMPMEMFREE(g);
162     TMPMEMFREE(work);
163     *iter=k;
164     *tolerance=norm_of_residual;
165     return Status;
166     }
167    

  ViewVC Help
Powered by ViewVC 1.1.26