# Contents of /trunk/paso/src/Solver.c

Revision 2156 - (show annotations)
Mon Dec 15 05:09:02 2008 UTC (11 years, 1 month ago) by gross
File MIME type: text/plain
File size: 13500 byte(s)
```some modifications to the iterative solver to make them easier to use.
There are also improved versions of the Darcy flux solver and the incompressible solver.

```
 1 2 /******************************************************* 3 * 4 * Copyright (c) 2003-2008 by University of Queensland 5 * Earth Systems Science Computational Center (ESSCC) 6 * http://www.uq.edu.au/esscc 7 * 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 15 /**************************************************************/ 16 17 /* Paso: SystemMatrix: controls iterative linear system solvers */ 18 19 /**************************************************************/ 20 21 /* Copyrights by ACcESS Australia 2003/04 */ 22 /* Author: gross@access.edu.au */ 23 24 /**************************************************************/ 25 26 #include "Paso.h" 27 #include "SystemMatrix.h" 28 #include "Solver.h" 29 #include "esysUtils/blocktimer.h" 30 31 /***********************************************************************************/ 32 33 /* free space */ 34 35 void Paso_Solver_free(Paso_SystemMatrix* A) { 36 Paso_Preconditioner_free(A->solver); 37 A->solver=NULL; 38 } 39 /* call the iterative solver: */ 40 41 void Paso_Solver(Paso_SystemMatrix* A,double* x,double* b, 42 Paso_Options* options,Paso_Performance* pp) { 43 44 double norm2_of_b,tol,tolerance,time_iter; 45 double *r=NULL,norm2_of_residual,last_norm2_of_residual,norm_max_of_b; 46 double norm2_of_b_local,norm_max_of_b_local,norm2_of_residual_local; 47 double norm_max_of_residual_local,norm_max_of_residual; 48 double last_norm_max_of_residual,*scaling; 49 #ifdef PASO_MPI 50 double loc_norm; 51 #endif 52 dim_t i,totIter,cntIter,method; 53 bool_t finalizeIteration; 54 err_t errorCode=NO_ERROR; 55 dim_t numSol = Paso_SystemMatrix_getTotalNumCols(A); 56 dim_t numEqua = Paso_SystemMatrix_getTotalNumRows(A); 57 double blocktimer_precond, blocktimer_start = blocktimer_time(); 58 59 Paso_resetError(); 60 tolerance=options->tolerance; 61 if (tolerance < 100.* EPSILON) { 62 Paso_setError(VALUE_ERROR,"Paso_Solver: Tolerance is too small."); 63 } 64 if (tolerance >1.) { 65 Paso_setError(VALUE_ERROR,"Paso_Solver: Tolerance mut be less than one."); 66 } 67 method=Paso_Options_getSolver(options->method,PASO_PASO,options->symmetric); 68 /* check matrix type */ 69 if ((A->type & MATRIX_FORMAT_CSC) || (A->type & MATRIX_FORMAT_OFFSET1) || (A->type & MATRIX_FORMAT_SYM) ) { 70 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires CSR format with unsymmetric storage scheme and index offset 0."); 71 } 72 if (A->col_block_size != A->row_block_size) { 73 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires row and column block sizes to be equal."); 74 } 75 if (Paso_SystemMatrix_getGlobalNumCols(A) != Paso_SystemMatrix_getGlobalNumRows(A)) { 76 Paso_setError(TYPE_ERROR,"Paso_Solver: Iterative solver requires a square matrix."); 77 return; 78 } 79 /* this for testing only */ 80 if (method==PASO_NONLINEAR_GMRES) { 81 Paso_Function* F=NULL; 82 F=Paso_Function_LinearSystem_alloc(A,b,options); 83 Paso_Solver_solvePreconditioner(A,x,b); 84 errorCode=Paso_Solver_NewtonGMRES(F,x,options,pp); 85 if (errorCode!=NO_ERROR) { 86 Paso_setError(SYSTEM_ERROR,"Paso_Solver_NewtonGMRES: an error has occured."); 87 } 88 Paso_Function_LinearSystem_free(F); 89 return; 90 } 91 /* ========================= */ 92 Performance_startMonitor(pp,PERFORMANCE_ALL); 93 if (Paso_noError()) { 94 /* get normalization */ 95 scaling=Paso_SystemMatrix_borrowNormalization(A); 96 if (Paso_noError()) { 97 /* get the norm of the right hand side */ 98 norm2_of_b=0.; 99 norm_max_of_b=0.; 100 #pragma omp parallel private(norm2_of_b_local,norm_max_of_b_local) 101 { 102 norm2_of_b_local=0.; 103 norm_max_of_b_local=0.; 104 #pragma omp for private(i) schedule(static) 105 for (i = 0; i < numEqua ; ++i) { 106 norm2_of_b_local += b[i] * b[i]; 107 norm_max_of_b_local = MAX(ABS(scaling[i]*b[i]),norm_max_of_b_local); 108 } 109 #pragma omp critical 110 { 111 norm2_of_b += norm2_of_b_local; 112 norm_max_of_b = MAX(norm_max_of_b_local,norm_max_of_b); 113 } 114 } 115 /* TODO: use one call */ 116 #ifdef PASO_MPI 117 { 118 loc_norm = norm2_of_b; 119 MPI_Allreduce(&loc_norm,&norm2_of_b, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm); 120 loc_norm = norm_max_of_b; 121 MPI_Allreduce(&loc_norm,&norm_max_of_b, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm); 122 } 123 #endif 124 norm2_of_b=sqrt(norm2_of_b); 125 126 /* if norm2_of_b==0 we are ready: x=0 */ 127 if (norm2_of_b <=0.) { 128 #pragma omp parallel for private(i) schedule(static) 129 for (i = 0; i < numSol; i++) x[i]=0.; 130 if (options->verbose) printf("right hand side is identical zero.\n"); 131 } else { 132 if (options->verbose) { 133 printf("Paso_Solver: l2/lmax-norm of right hand side is %e/%e.\n",norm2_of_b,norm_max_of_b); 134 printf("Paso_Solver: l2/lmax-stopping criterion is %e/%e.\n",norm2_of_b*tolerance,norm_max_of_b*tolerance); 135 switch (method) { 136 case PASO_BICGSTAB: 137 printf("Paso_Solver: Iterative method is BiCGStab.\n"); 138 break; 139 case PASO_PCG: 140 printf("Paso_Solver: Iterative method is PCG.\n"); 141 break; 142 case PASO_TFQMR: 143 printf("Paso_Solver: Iterative method is TFQMR.\n"); 144 break; 145 case PASO_MINRES: 146 printf("Paso_Solver: Iterative method is MINRES.\n"); 147 break; 148 case PASO_PRES20: 149 printf("Paso_Solver: Iterative method is PRES20.\n"); 150 break; 151 case PASO_GMRES: 152 if (options->restart>0) { 153 printf("Paso_Solver: Iterative method is GMRES(%d,%d)\n",options->truncation,options->restart); 154 } else { 155 printf("Paso_Solver: Iterative method is GMRES(%d)\n",options->truncation); 156 } 157 break; 158 } 159 } 160 /* construct the preconditioner */ 161 162 blocktimer_precond = blocktimer_time(); 163 Performance_startMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT); 164 Paso_Solver_setPreconditioner(A,options); 165 Performance_stopMonitor(pp,PERFORMANCE_PRECONDITIONER_INIT); 166 blocktimer_increment("Paso_Solver_setPreconditioner()", blocktimer_precond); 167 if (! Paso_noError()) return; 168 time_iter=Paso_timer(); 169 /* get an initial guess by evaluating the preconditioner */ 170 Paso_Solver_solvePreconditioner(A,x,b); 171 /* start the iteration process :*/ 172 r=TMPMEMALLOC(numEqua,double); 173 Paso_checkPtr(r); 174 if (Paso_noError()) { 175 totIter = 0; 176 finalizeIteration = FALSE; 177 last_norm2_of_residual=norm2_of_b; 178 last_norm_max_of_residual=norm_max_of_b; 179 /* Loop */ 180 while (! finalizeIteration) { 181 cntIter = options->iter_max - totIter; 182 finalizeIteration = TRUE; 183 /* Set initial residual. */ 184 norm2_of_residual = 0; 185 norm_max_of_residual = 0; 186 #pragma omp parallel for private(i) schedule(static) 187 for (i = 0; i < numEqua; i++) r[i]=b[i]; 188 Paso_SystemMatrix_MatrixVector_CSR_OFFSET0(DBLE(-1), A, x, DBLE(1), r); 189 190 #pragma omp parallel private(norm2_of_residual_local,norm_max_of_residual_local) 191 { 192 norm2_of_residual_local = 0; 193 norm_max_of_residual_local = 0; 194 #pragma omp for private(i) schedule(static) 195 for (i = 0; i < numEqua; i++) { 196 norm2_of_residual_local+= r[i] * r[i]; 197 norm_max_of_residual_local=MAX(ABS(scaling[i]*r[i]),norm_max_of_residual_local); 198 } 199 #pragma omp critical 200 { 201 norm2_of_residual += norm2_of_residual_local; 202 norm_max_of_residual = MAX(norm_max_of_residual_local,norm_max_of_residual); 203 } 204 } 205 /* TODO: use one call */ 206 #ifdef PASO_MPI 207 loc_norm = norm2_of_residual; 208 MPI_Allreduce(&loc_norm,&norm2_of_residual, 1, MPI_DOUBLE, MPI_SUM, A->mpi_info->comm); 209 loc_norm = norm_max_of_residual; 210 MPI_Allreduce(&loc_norm,&norm_max_of_residual, 1, MPI_DOUBLE, MPI_MAX, A->mpi_info->comm); 211 #endif 212 norm2_of_residual =sqrt(norm2_of_residual); 213 214 if (options->verbose) printf("Paso_Solver: Step %5d: l2/lmax-norm of residual is %e/%e",totIter,norm2_of_residual,norm_max_of_residual); 215 if (totIter>0 && norm2_of_residual>=last_norm2_of_residual && norm_max_of_residual>=last_norm_max_of_residual) { 216 if (options->verbose) printf(" divergence!\n"); 217 Paso_setError(WARNING, "Paso_Solver: No improvement during iteration. Iterative solver gives up."); 218 } else { 219 /* */ 220 if (norm2_of_residual>tolerance*norm2_of_b || norm_max_of_residual>tolerance*norm_max_of_b ) { 221 tol=tolerance*MIN(norm2_of_b,0.1*norm2_of_residual/norm_max_of_residual*norm_max_of_b); 222 if (options->verbose) printf(" (new tolerance = %e).\n",tol); 223 last_norm2_of_residual=norm2_of_residual; 224 last_norm_max_of_residual=norm_max_of_residual; 225 /* call the solver */ 226 switch (method) { 227 case PASO_BICGSTAB: 228 errorCode = Paso_Solver_BiCGStab(A, r, x, &cntIter, &tol, pp); 229 break; 230 case PASO_PCG: 231 errorCode = Paso_Solver_PCG(A, r, x, &cntIter, &tol, pp); 232 break; 233 case PASO_TFQMR: 234 errorCode = Paso_Solver_TFQMR(A, r, x, &cntIter, &tol, pp); 235 break; 236 case PASO_MINRES: 237 errorCode = Paso_Solver_MINRES(A, r, x, &cntIter, &tol, pp); 238 break; 239 case PASO_PRES20: 240 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,5,20, pp); 241 break; 242 case PASO_GMRES: 243 errorCode = Paso_Solver_GMRES(A, r, x, &cntIter, &tol,options->truncation,options->restart, pp); 244 break; 245 } 246 totIter += cntIter; 247 /* error handling */ 248 if (errorCode==NO_ERROR) { 249 finalizeIteration = FALSE; 250 } else if (errorCode==SOLVER_MAXITER_REACHED) { 251 Paso_setError(WARNING,"Paso_Solver: maximum number of iteration step reached.\nReturned solution does not fulfil stopping criterion."); 252 if (options->verbose) printf("Paso_Solver: Maximum number of iterations reached.!\n"); 253 } else if (errorCode == SOLVER_INPUT_ERROR ) { 254 Paso_setError(SYSTEM_ERROR,"Paso_Solver: illegal dimension in iterative solver."); 255 if (options->verbose) printf("Paso_Solver: Internal error!\n"); 256 } else if ( errorCode == SOLVER_BREAKDOWN ) { 257 if (cntIter <= 1) { 258 Paso_setError(ZERO_DIVISION_ERROR, "Paso_Solver: fatal break down in iterative solver."); 259 if (options->verbose) printf("Paso_Solver: Uncurable break down!\n"); 260 } else { 261 if (options->verbose) printf("Paso_Solver: Breakdown at iter %d (residual = %e). Restarting ...\n", cntIter+totIter, tol); 262 finalizeIteration = FALSE; 263 } 264 } else { 265 if (options->verbose) printf(". convergence! \n"); 266 } 267 } 268 } /* while */ 269 } 270 MEMFREE(r); 271 time_iter=Paso_timer()-time_iter; 272 if (options->verbose) { 273 printf("\ntiming: Paso_Solver: %.4e sec\n",time_iter); 274 if (totIter>0) printf("timing: per iteration step: %.4e sec\n",time_iter/totIter); 275 } 276 } 277 } 278 } 279 } 280 Performance_stopMonitor(pp,PERFORMANCE_ALL); 281 blocktimer_increment("Paso_Solver()", blocktimer_start); 282 }

## Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision