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

Revision 4873 - (show annotations)
Wed Apr 16 06:38:51 2014 UTC (5 years, 5 months ago) by caltinay
File size: 4982 byte(s)
```whitespace only changes.

```
 1 2 /***************************************************************************** 3 * 4 * Copyright (c) 2003-2014 by University of Queensland 5 6 * 7 * Primary Business: Queensland, Australia 8 * Licensed under the Open Software License version 3.0 9 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 "Solver.h" 19 #include "PasoUtil.h" 20 21 namespace paso { 22 23 err_t Solver_GMRES2(Function* F, const double* f0, const double* x0, 24 double* dx, dim_t* iter, double* tolerance, 25 Performance* pp) 26 { 27 static double RENORMALIZATION_CONST=0.001; 28 const dim_t l=(*iter)+1, iter_max=*iter; 29 dim_t k=0,i,j; 30 const dim_t n=F->getLen(); 31 const double rel_tol=*tolerance; 32 double abs_tol, normf0, normv, normv2, hh, hr, nu, norm_of_residual=0.; 33 bool breakFlag=false, maxIterFlag=false, convergeFlag=false; 34 35 if (n < 0 || iter_max<=0 || l<1 || rel_tol<0) { 36 return SOLVER_INPUT_ERROR; 37 } 38 39 err_t Status=SOLVER_NO_ERROR; 40 41 double* h = new double[l*l]; 42 double** v = new double*[l]; 43 double* c = new double[l]; 44 double* s = new double[l]; 45 double* g = new double[l]; 46 double* work = new double[n]; 47 48 for (i=0; impi_info); 57 k = 0; 58 convergeFlag = (ABS(normf0)<=0); 59 if (!convergeFlag) { 60 abs_tol = rel_tol*normf0; 61 printf("GMRES2 initial residual norm %e (rel. tol=%e)\n",normf0,rel_tol); 62 v[0] = new double[n]; 63 util::zeroes(n, v[0]); 64 util::update(n, 1., v[0], -1./normf0, f0); // v = -1./normf0*f0 65 g[0] = normf0; 66 while (!breakFlag && !maxIterFlag && !convergeFlag && Status==SOLVER_NO_ERROR) { 67 k++; 68 v[k]=new double[n]; 69 /* 70 * call directional derivative function 71 */ 72 F->derivative(v[k], v[k-1], f0, x0, work, pp); 73 normv=util::l2(n,v[k],F->mpi_info); 74 /* 75 * Modified Gram-Schmidt 76 */ 77 for (j=0; jmpi_info); 79 util::update(n,1.,v[k],(-hh),v[j]); // v[k]-hh*v[j] 80 h[INDEX2(j,k-1,l)]=hh; 81 //printf("%d : %d = %e\n",k,j,hh); 82 } 83 normv2=util::l2(n,v[k],F->mpi_info); 84 h[INDEX2(k,k-1,l)]=normv2; 85 /* 86 * reorthogonalize 87 */ 88 if (!(normv + RENORMALIZATION_CONST*normv2 > normv)) { 89 // printf("GMRES2: renormalization!"); 90 for (j=0; jmpi_info); 92 93 h[INDEX2(j,k-1,l)]+=hr; 94 util::update(n,1.,v[k],(-hr),v[j]); 95 } 96 normv2=util::l2(n,v[k],F->mpi_info); 97 h[INDEX2(k,k-1,l)]=normv2; 98 } 99 /* 100 * watch out for happy breakdown 101 */ 102 if (normv2 > 0.) { 103 util::update(n,1./normv2,v[k],0.,v[k]); /* normalize v[k] */ 104 } 105 /* 106 * Form and store the information for the new Givens rotation 107 */ 108 util::applyGivensRotations(k,&h[INDEX2(0,k-1,l)],c,s); 109 110 /* 111 * Don't divide by zero if solution has been found 112 */ 113 g[k] = 0; 114 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)]); 115 if (nu > 0) { 116 c[k-1]= h[INDEX2(k-1,k-1,l)]/nu; 117 s[k-1]=-h[INDEX2(k,k-1,l)]/nu; 118 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)]; 119 h[INDEX2(k,k-1,l)]=0; 120 util::applyGivensRotations(2,&(g[k-1]),&(c[k-1]),&(s[k-1])); 121 } 122 norm_of_residual = fabs(g[k]); 123 maxIterFlag = (k >= iter_max); 124 convergeFlag = (norm_of_residual <= abs_tol); 125 printf("GMRES2 step %d: residual %e (abs. tol=%e)\n", k, fabs(g[k]), abs_tol); 126 } 127 } 128 129 // all done and ready for the forward substitution: 130 for (i=k-1;i>=0;--i) { 131 for (j=i+1;j

## 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