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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4856 - (show annotations)
Wed Apr 9 06:46:55 2014 UTC (5 years, 7 months ago) by caltinay
File size: 5016 byte(s)
some mopping in paso

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
22 namespace paso {
23
24 err_t Solver_GMRES2(Function* F, const double* f0, const double* x0,
25 double* dx, dim_t* iter, double* tolerance,
26 Paso_Performance* pp)
27 {
28 static double RENORMALIZATION_CONST=0.001;
29 const dim_t l=(*iter)+1, iter_max=*iter;
30 dim_t k=0,i,j;
31 const dim_t n=F->getLen();
32 const double rel_tol=*tolerance;
33 double abs_tol, normf0, normv, normv2, hh, hr, nu, norm_of_residual=0.;
34 bool breakFlag=FALSE, maxIterFlag=FALSE, convergeFlag=FALSE;
35
36 if (n < 0 || iter_max<=0 || l<1 || rel_tol<0) {
37 return SOLVER_INPUT_ERROR;
38 }
39
40 err_t Status=SOLVER_NO_ERROR;
41
42 double* h = new double[l*l];
43 double** v = new double*[l];
44 double* c = new double[l];
45 double* s = new double[l];
46 double* g = new double[l];
47 double* work = new double[n];
48
49 for (i=0; i<iter_max; i++)
50 v[i]=NULL;
51
52 util::zeroes(n,dx);
53
54 /*
55 * the show begins:
56 */
57 normf0 = util::l2(n,f0,F->mpi_info);
58 k = 0;
59 convergeFlag = (ABS(normf0)<=0);
60 if (!convergeFlag) {
61 abs_tol = rel_tol*normf0;
62 printf("GMRES2 initial residual norm %e (rel. tol=%e)\n",normf0,rel_tol);
63 v[0] = new double[n];
64 util::zeroes(n, v[0]);
65 util::update(n, 1., v[0], -1./normf0, f0); // v = -1./normf0*f0
66 g[0] = normf0;
67 while (!breakFlag && !maxIterFlag && !convergeFlag && Status==SOLVER_NO_ERROR) {
68 k++;
69 v[k]=new double[n];
70 /*
71 * call directional derivative function
72 */
73 F->derivative(v[k], v[k-1], f0, x0, work, pp);
74 normv=util::l2(n,v[k],F->mpi_info);
75 /*
76 * Modified Gram-Schmidt
77 */
78 for (j=0; j<k; j++){
79 hh = util::innerProduct(n,v[j],v[k],F->mpi_info);
80 util::update(n,1.,v[k],(-hh),v[j]); // v[k]-hh*v[j]
81 h[INDEX2(j,k-1,l)]=hh;
82 //printf("%d : %d = %e\n",k,j,hh);
83 }
84 normv2=util::l2(n,v[k],F->mpi_info);
85 h[INDEX2(k,k-1,l)]=normv2;
86 /*
87 * reorthogonalize
88 */
89 if (!(normv + RENORMALIZATION_CONST*normv2 > normv)) {
90 // printf("GMRES2: renormalization!");
91 for (j=0; j<k; j++) {
92 hr = util::innerProduct(n,v[j],v[k],F->mpi_info);
93
94 h[INDEX2(j,k-1,l)]+=hr;
95 util::update(n,1.,v[k],(-hr),v[j]);
96 }
97 normv2=util::l2(n,v[k],F->mpi_info);
98 h[INDEX2(k,k-1,l)]=normv2;
99 }
100 /*
101 * watch out for happy breakdown
102 */
103 if (normv2 > 0.) {
104 util::update(n,1./normv2,v[k],0.,v[k]); /* normalize v[k] */
105 }
106 /*
107 * Form and store the information for the new Givens rotation
108 */
109 util::applyGivensRotations(k,&h[INDEX2(0,k-1,l)],c,s);
110
111 /*
112 * Don't divide by zero if solution has been found
113 */
114 g[k] = 0;
115 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)]);
116 if (nu > 0) {
117 c[k-1]= h[INDEX2(k-1,k-1,l)]/nu;
118 s[k-1]=-h[INDEX2(k,k-1,l)]/nu;
119 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)];
120 h[INDEX2(k,k-1,l)]=0;
121 util::applyGivensRotations(2,&(g[k-1]),&(c[k-1]),&(s[k-1]));
122 }
123 norm_of_residual = fabs(g[k]);
124 maxIterFlag = (k >= iter_max);
125 convergeFlag = (norm_of_residual <= abs_tol);
126 printf("GMRES2 step %d: residual %e (abs. tol=%e)\n", k, fabs(g[k]), abs_tol);
127 }
128 }
129
130 // all done and ready for the forward substitution:
131 for (i=k-1;i>=0;--i) {
132 for (j=i+1;j<k;j++) {
133 g[i]-=h[INDEX2(i,j,l)]*g[j];
134 }
135 g[i] /= h[INDEX2(i,i,l)];
136 util::update(n, 1., dx, g[i], v[i]); // dx = dx+g[i]*v[i]
137 }
138 if ( v != NULL) {
139 for (i=0; i<iter_max; i++)
140 delete[] v[i];
141 }
142 delete[] h;
143 delete[] v;
144 delete[] c;
145 delete[] s;
146 delete[] g;
147 delete[] work;
148 *iter=k;
149 *tolerance=norm_of_residual;
150 return Status;
151 }
152
153 } // namespace paso
154

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