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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4657 - (show annotations)
Thu Feb 6 06:12:20 2014 UTC (5 years, 8 months ago) by jfenwick
File size: 5883 byte(s)
I changed some files.
Updated copyright notices, added GeoComp.



1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2014 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17
18 #include "Common.h"
19 #include "Solver.h"
20 #include "PasoUtil.h"
21 #ifdef _OPENMP
22 #include <omp.h>
23 #endif
24
25 err_t Paso_Solver_GMRES2(
26 Paso_Function * F,
27 const double* f0,
28 const double* x0,
29 double * dx,
30 dim_t *iter,
31 double* tolerance,
32 Paso_Performance* pp)
33 {
34 static double RENORMALIZATION_CONST=0.001;
35 const dim_t l=(*iter)+1, iter_max=*iter;
36 dim_t k=0,i,j;
37 const dim_t n=F->n;
38 const double rel_tol=*tolerance;
39 double abs_tol, normf0, normv, normv2, hh, hr, nu, norm_of_residual=0.;
40 bool breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
41 double *h=NULL, **v=NULL, *c=NULL,*s=NULL,*g=NULL, *work=NULL;
42 err_t Status=SOLVER_NO_ERROR;
43
44 /* Test the input parameters. */
45 if (n < 0 || iter_max<=0 || l<1 || rel_tol<0) {
46 return SOLVER_INPUT_ERROR;
47 }
48 Paso_zeroes(n,dx);
49 /*
50 * allocate memory:
51 */
52 h=new double[l*l];
53 v=new double*[l];
54 c=new double[l];
55 s=new double[l];
56 g=new double[l];
57 work=new double[n];
58
59 if (h==NULL || v ==NULL || c== NULL || s == NULL || g== NULL || work==NULL) {
60 Status=SOLVER_MEMORY_ERROR;
61 }
62 if ( Status ==SOLVER_NO_ERROR ) {
63 for (i=0;i<iter_max;i++) v[i]=NULL;
64 /*
65 * the show begins:
66 */
67 normf0=Paso_l2(n,f0,F->mpi_info);
68 k=0;
69 convergeFlag=(ABS(normf0)<=0);
70 if (! convergeFlag) {
71 abs_tol=rel_tol*normf0;
72 printf("GMRES2 initial residual norm %e (rel. tol=%e)\n",normf0,rel_tol);
73 v[0]=new double[n];
74 if (v[0]==NULL) {
75 Status=SOLVER_MEMORY_ERROR;
76 } else {
77 Paso_zeroes(n,v[0]);
78 Paso_Update(n,1.,v[0],-1./normf0,f0); /* v = -1./normf0*f0 */
79 g[0]=normf0;
80 }
81 while( (! (breakFlag || maxIterFlag || convergeFlag)) && (Status ==SOLVER_NO_ERROR) ) {
82 k++;
83 v[k]=new double[n];
84 if (v[k]==NULL) {
85 Status=SOLVER_MEMORY_ERROR;
86 } else {
87 /*
88 * call directional derivative function
89 */
90 Paso_FunctionDerivative(v[k],v[k-1],F,f0,x0,work,pp);
91 normv=Paso_l2(n,v[k],F->mpi_info);
92 /*
93 * Modified Gram-Schmidt
94 */
95 for (j=0;j<k;j++){
96 hh=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
97 Paso_Update(n,1.,v[k],(-hh),v[j]); /* v[k]-hh*v[j] */
98 h[INDEX2(j,k-1,l)]=hh;
99 /* printf("%d : %d = %e\n",k,j,hh); */
100 }
101 normv2=Paso_l2(n,v[k],F->mpi_info);
102 h[INDEX2(k,k-1,l)]=normv2;
103 /*
104 * reorthogonalize
105 */
106 if (! (normv + RENORMALIZATION_CONST*normv2 > normv)) {
107 /* printf("GMRES2: renormalization!"); */
108 for (j=0;j<k;j++){
109 hr=Paso_InnerProduct(n,v[j],v[k],F->mpi_info);
110
111 h[INDEX2(j,k-1,l)]+=hr;
112 Paso_Update(n,1.,v[k],(-hr),v[j]);
113 }
114 normv2=Paso_l2(n,v[k],F->mpi_info);
115 h[INDEX2(k,k-1,l)]=normv2;
116 }
117 /*
118 {int p,q;
119 for (p=0;p<k+1;p++){
120 for (q=0;q<k+1;q++)printf("%e ",h[INDEX2(p,q,l)]);
121 printf("\n");
122
123 }
124 }
125 */
126 /*
127 * watch out for happy breakdown
128 */
129 if(normv2 > 0.) {
130 Paso_Update(n,1./normv2,v[k],0.,v[k]); /* normalize v[k] */
131 }
132 /*
133 * Form and store the information for the new Givens rotation
134 */
135 ApplyGivensRotations(k,&h[INDEX2(0,k-1,l)],c,s);
136
137 /*
138 * Don't divide by zero if solution has been found
139 */
140 g[k]=0;
141 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)]);
142 if (nu>0) {
143 c[k-1]= h[INDEX2(k-1,k-1,l)]/nu;
144 s[k-1]=-h[INDEX2(k,k-1,l)]/nu;
145 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)];
146 h[INDEX2(k,k-1,l)]=0;
147 ApplyGivensRotations(2,&(g[k-1]),&(c[k-1]),&(s[k-1]));
148 }
149 norm_of_residual=fabs(g[k]);
150 maxIterFlag = (k>=iter_max);
151 convergeFlag = (norm_of_residual <= abs_tol);
152 printf("GMRES2 step %d: residual %e (abs. tol=%e)\n",k,fabs(g[k]),abs_tol);
153 }
154 }
155 }
156 /*
157 * all done and ready for the forward substitution:
158 */
159
160 for (i=k-1;i>=0;--i) {
161 for (j=i+1;j<k;j++) {
162 g[i]-=h[INDEX2(i,j,l)]*g[j];
163 }
164 g[i]/=h[INDEX2(i,i,l)];
165 Paso_Update(n,1.,dx,g[i],v[i]); /* dx = dx+g[i]*v[i] */
166 }
167 }
168 /*
169 * clean up:
170 */
171 if ( v !=NULL) {
172 for (i=0;i<iter_max;i++) delete[] v[i];
173 }
174 delete[] h;
175 delete[] v;
176 delete[] c;
177 delete[] s;
178 delete[] g;
179 delete[] work;
180 *iter=k;
181 *tolerance=norm_of_residual;
182 return Status;
183 }
184

Properties

Name Value
svn:mergeinfo /branches/amg_from_3530/paso/src/GMRES2.cpp:3531-3826 /branches/lapack2681/paso/src/GMRES2.cpp:2682-2741 /branches/pasowrap/paso/src/GMRES2.cpp:3661-3674 /branches/py3_attempt2/paso/src/GMRES2.cpp:3871-3891 /branches/restext/paso/src/GMRES2.cpp:2610-2624 /branches/ripleygmg_from_3668/paso/src/GMRES2.cpp:3669-3791 /branches/stage3.0/paso/src/GMRES2.cpp:2569-2590 /branches/symbolic_from_3470/paso/src/GMRES2.cpp:3471-3974 /branches/symbolic_from_3470/ripley/test/python/paso/src/GMRES2.cpp:3517-3974 /release/3.0/paso/src/GMRES2.cpp:2591-2601 /trunk/paso/src/GMRES2.cpp:4257-4344 /trunk/ripley/test/python/paso/src/GMRES2.cpp:3480-3515

  ViewVC Help
Powered by ViewVC 1.1.26