1 2 #include <petsc-private/snesimpl.h> 3 4 /* Data used by Jorge's diff parameter computation method */ 5 typedef struct { 6 Vec *workv; /* work vectors */ 7 FILE *fp; /* output file */ 8 int function_count; /* count of function evaluations for diff param estimation */ 9 double fnoise_min; /* minimim allowable noise */ 10 double hopt_min; /* minimum allowable hopt */ 11 double h_first_try; /* first try for h used in diff parameter estimate */ 12 PetscInt fnoise_resets; /* number of times we've reset the noise estimate */ 13 PetscInt hopt_resets; /* number of times we've reset the hopt estimate */ 14 } DIFFPAR_MORE; 15 16 17 extern PetscErrorCode SNESNoise_dnest_(PetscInt*,PetscScalar*,PetscScalar*,PetscScalar*,PetscScalar*,PetscScalar*,PetscInt*,PetscScalar*); 18 static PetscErrorCode JacMatMultCompare(SNES,Vec,Vec,double); 19 extern PetscErrorCode SNESDefaultMatrixFreeSetParameters2(Mat,double,double,double); 20 extern PetscErrorCode SNESUnSetMatrixFreeParameter(SNES snes); 21 22 #undef __FUNCT__ 23 #define __FUNCT__ "SNESDiffParameterCreate_More" 24 PetscErrorCode SNESDiffParameterCreate_More(SNES snes,Vec x,void **outneP) 25 { 26 DIFFPAR_MORE *neP; 27 Vec w; 28 PetscRandom rctx; /* random number generator context */ 29 PetscErrorCode ierr; 30 PetscBool flg; 31 char noise_file[PETSC_MAX_PATH_LEN]; 32 33 PetscFunctionBegin; 34 ierr = PetscNewLog(snes,DIFFPAR_MORE,&neP);CHKERRQ(ierr); 35 neP->function_count = 0; 36 neP->fnoise_min = 1.0e-20; 37 neP->hopt_min = 1.0e-8; 38 neP->h_first_try = 1.0e-3; 39 neP->fnoise_resets = 0; 40 neP->hopt_resets = 0; 41 42 /* Create work vectors */ 43 ierr = VecDuplicateVecs(x,3,&neP->workv);CHKERRQ(ierr); 44 w = neP->workv[0]; 45 46 /* Set components of vector w to random numbers */ 47 ierr = PetscRandomCreate(((PetscObject)snes)->comm,&rctx);CHKERRQ(ierr); 48 ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr); 49 ierr = VecSetRandom(w,rctx);CHKERRQ(ierr); 50 ierr = PetscRandomDestroy(&rctx);CHKERRQ(ierr); 51 52 /* Open output file */ 53 ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_mf_noise_file",noise_file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 54 if (flg) neP->fp = fopen(noise_file,"w"); 55 else neP->fp = fopen("noise.out","w"); 56 if (!neP->fp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file"); 57 ierr = PetscInfo(snes,"Creating Jorge's differencing parameter context\n");CHKERRQ(ierr); 58 59 *outneP = neP; 60 PetscFunctionReturn(0); 61 } 62 63 #undef __FUNCT__ 64 #define __FUNCT__ "SNESDiffParameterDestroy_More" 65 PetscErrorCode SNESDiffParameterDestroy_More(void *nePv) 66 { 67 DIFFPAR_MORE *neP = (DIFFPAR_MORE *)nePv; 68 PetscErrorCode ierr; 69 int err; 70 71 PetscFunctionBegin; 72 /* Destroy work vectors and close output file */ 73 ierr = VecDestroyVecs(3,&neP->workv);CHKERRQ(ierr); 74 err = fclose(neP->fp); 75 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 76 ierr = PetscFree(neP);CHKERRQ(ierr); 77 PetscFunctionReturn(0); 78 } 79 80 #undef __FUNCT__ 81 #define __FUNCT__ "SNESDiffParameterCompute_More" 82 PetscErrorCode SNESDiffParameterCompute_More(SNES snes,void *nePv,Vec x,Vec p,double *fnoise,double *hopt) 83 { 84 DIFFPAR_MORE *neP = (DIFFPAR_MORE *)nePv; 85 Vec w, xp, fvec; /* work vectors to use in computing h */ 86 double zero = 0.0, hl, hu, h, fnoise_s, fder2_s; 87 PetscScalar alpha; 88 PetscScalar fval[7], tab[7][7], eps[7], f; 89 double rerrf, fder2; 90 PetscErrorCode ierr; 91 PetscInt iter, k, i, j, info; 92 PetscInt nf = 7; /* number of function evaluations */ 93 PetscInt fcount; 94 MPI_Comm comm = ((PetscObject)snes)->comm; 95 FILE *fp; 96 PetscBool noise_test = PETSC_FALSE; 97 98 PetscFunctionBegin; 99 /* Call to SNESSetUp() just to set data structures in SNES context */ 100 if (!snes->setupcalled) {ierr = SNESSetUp(snes);CHKERRQ(ierr);} 101 102 w = neP->workv[0]; 103 xp = neP->workv[1]; 104 fvec = neP->workv[2]; 105 fp = neP->fp; 106 107 /* Initialize parameters */ 108 hl = zero; 109 hu = zero; 110 h = neP->h_first_try; 111 fnoise_s = zero; 112 fder2_s = zero; 113 fcount = neP->function_count; 114 115 /* We have 5 tries to attempt to compute a good hopt value */ 116 ierr = SNESGetIterationNumber(snes,&i);CHKERRQ(ierr); 117 ierr = PetscFPrintf(comm,fp,"\n ------- SNES iteration %D ---------\n",i);CHKERRQ(ierr); 118 for (iter=0; iter<5; iter++) { 119 neP->h_first_try = h; 120 121 /* Compute the nf function values needed to estimate the noise from 122 the difference table */ 123 for (k=0; k<nf; k++) { 124 alpha = h * (k+1 - (nf+1)/2); 125 ierr = VecWAXPY(xp,alpha,p,x);CHKERRQ(ierr); 126 ierr = SNESComputeFunction(snes,xp,fvec);CHKERRQ(ierr); 127 neP->function_count++; 128 ierr = VecDot(fvec,w,&fval[k]);CHKERRQ(ierr); 129 } 130 f = fval[(nf+1)/2 - 1]; 131 132 /* Construct the difference table */ 133 for (i=0; i<nf; i++) { 134 tab[i][0] = fval[i]; 135 } 136 for (j=0; j<6; j++) { 137 for (i=0; i<nf-j; i++) { 138 tab[i][j+1] = tab[i+1][j] - tab[i][j]; 139 } 140 } 141 142 /* Print the difference table */ 143 ierr = PetscFPrintf(comm,fp,"Difference Table: iter = %D\n",iter);CHKERRQ(ierr); 144 for (i=0; i<nf; i++) { 145 for (j=0; j<nf-i; j++) { 146 ierr = PetscFPrintf(comm,fp," %10.2e ",tab[i][j]);CHKERRQ(ierr); 147 } 148 ierr = PetscFPrintf(comm,fp,"\n");CHKERRQ(ierr); 149 } 150 151 /* Call the noise estimator */ 152 ierr = SNESNoise_dnest_(&nf,fval,&h,fnoise,&fder2,hopt,&info,eps);CHKERRQ(ierr); 153 154 /* Output statements */ 155 rerrf = *fnoise/PetscAbsScalar(f); 156 if (info == 1) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise detected");CHKERRQ(ierr);} 157 if (info == 2) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise not detected; h is too small");CHKERRQ(ierr);} 158 if (info == 3) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise not detected; h is too large");CHKERRQ(ierr);} 159 if (info == 4) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise detected, but unreliable hopt");CHKERRQ(ierr);} 160 ierr = PetscFPrintf(comm,fp,"Approximate epsfcn %G %G %G %G %G %G\n", 161 eps[0],eps[1],eps[2],eps[3],eps[4],eps[5]);CHKERRQ(ierr); 162 ierr = PetscFPrintf(comm,fp,"h = %G, fnoise = %G, fder2 = %G, rerrf = %G, hopt = %G\n\n", 163 h, *fnoise, fder2, rerrf, *hopt);CHKERRQ(ierr); 164 165 /* Save fnoise and fder2. */ 166 if (*fnoise) fnoise_s = *fnoise; 167 if (fder2) fder2_s = fder2; 168 169 /* Check for noise detection. */ 170 if (fnoise_s && fder2_s) { 171 *fnoise = fnoise_s; 172 fder2 = fder2_s; 173 *hopt = 1.68*sqrt(*fnoise/PetscAbsScalar(fder2)); 174 goto theend; 175 } else { 176 177 /* Update hl and hu, and determine new h */ 178 if (info == 2 || info == 4) { 179 hl = h; 180 if (hu == zero) h = 100*h; 181 else h = PetscMin(100*h,0.1*hu); 182 } else if (info == 3) { 183 hu = h; 184 h = PetscMax(1.0e-3,sqrt(hl/hu))*hu; 185 } 186 } 187 } 188 theend: 189 190 if (*fnoise < neP->fnoise_min) { 191 ierr = PetscFPrintf(comm,fp,"Resetting fnoise: fnoise1 = %G, fnoise_min = %G\n",*fnoise,neP->fnoise_min);CHKERRQ(ierr); 192 *fnoise = neP->fnoise_min; 193 neP->fnoise_resets++; 194 } 195 if (*hopt < neP->hopt_min) { 196 ierr = PetscFPrintf(comm,fp,"Resetting hopt: hopt1 = %G, hopt_min = %G\n",*hopt,neP->hopt_min);CHKERRQ(ierr); 197 *hopt = neP->hopt_min; 198 neP->hopt_resets++; 199 } 200 201 ierr = PetscFPrintf(comm,fp,"Errors in derivative:\n");CHKERRQ(ierr); 202 ierr = PetscFPrintf(comm,fp,"f = %G, fnoise = %G, fder2 = %G, hopt = %G\n",f,*fnoise,fder2,*hopt);CHKERRQ(ierr); 203 204 /* For now, compute h **each** MV Mult!! */ 205 /* 206 ierr = PetscOptionsHasName(PETSC_NULL,"-matrix_free_jorge_each_mvp",&flg);CHKERRQ(ierr); 207 if (!flg) { 208 Mat mat; 209 ierr = SNESGetJacobian(snes,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 210 ierr = SNESDefaultMatrixFreeSetParameters2(mat,PETSC_DEFAULT,PETSC_DEFAULT,*hopt);CHKERRQ(ierr); 211 } 212 */ 213 fcount = neP->function_count - fcount; 214 ierr = PetscInfo5(snes,"fct_now = %D, fct_cum = %D, rerrf=%G, sqrt(noise)=%G, h_more=%G\n",fcount,neP->function_count,rerrf,sqrt(*fnoise),*hopt);CHKERRQ(ierr); 215 216 ierr = PetscOptionsGetBool(PETSC_NULL,"-noise_test",&noise_test,PETSC_NULL);CHKERRQ(ierr); 217 if (noise_test) { 218 ierr = JacMatMultCompare(snes,x,p,*hopt);CHKERRQ(ierr); 219 } 220 PetscFunctionReturn(0); 221 } 222 223 #undef __FUNCT__ 224 #define __FUNCT__ "JacMatMultCompare" 225 PetscErrorCode JacMatMultCompare(SNES snes,Vec x,Vec p,double hopt) 226 { 227 Vec yy1, yy2; /* work vectors */ 228 PetscViewer view2; /* viewer */ 229 Mat J; /* analytic Jacobian (set as preconditioner matrix) */ 230 Mat Jmf; /* matrix-free Jacobian (set as true system matrix) */ 231 double h; /* differencing parameter */ 232 Vec f; 233 MatStructure sparsity = DIFFERENT_NONZERO_PATTERN; 234 PetscScalar alpha; 235 PetscReal yy1n,yy2n,enorm; 236 PetscErrorCode ierr; 237 PetscInt i; 238 PetscBool printv = PETSC_FALSE; 239 char filename[32]; 240 MPI_Comm comm = ((PetscObject)snes)->comm; 241 242 PetscFunctionBegin; 243 /* Compute function and analytic Jacobian at x */ 244 ierr = SNESGetJacobian(snes,&Jmf,&J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 245 ierr = SNESComputeJacobian(snes,x,&Jmf,&J,&sparsity);CHKERRQ(ierr); 246 ierr = SNESGetFunction(snes,&f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 247 ierr = SNESComputeFunction(snes,x,f);CHKERRQ(ierr); 248 249 /* Duplicate work vectors */ 250 ierr = VecDuplicate(x,&yy2);CHKERRQ(ierr); 251 ierr = VecDuplicate(x,&yy1);CHKERRQ(ierr); 252 253 /* Compute true matrix-vector product */ 254 ierr = MatMult(J,p,yy1);CHKERRQ(ierr); 255 ierr = VecNorm(yy1,NORM_2,&yy1n);CHKERRQ(ierr); 256 257 /* View product vector if desired */ 258 ierr = PetscOptionsGetBool(PETSC_NULL,"-print_vecs",&printv,PETSC_NULL);CHKERRQ(ierr); 259 if (printv) { 260 ierr = PetscViewerASCIIOpen(comm,"y1.out",&view2);CHKERRQ(ierr); 261 ierr = PetscViewerSetFormat(view2,PETSC_VIEWER_ASCII_COMMON);CHKERRQ(ierr); 262 ierr = VecView(yy1,view2);CHKERRQ(ierr); 263 ierr = PetscViewerDestroy(&view2);CHKERRQ(ierr); 264 } 265 266 /* Test Jacobian-vector product computation */ 267 alpha = -1.0; 268 h = 0.01 * hopt; 269 for (i=0; i<5; i++) { 270 /* Set differencing parameter for matrix-free multiplication */ 271 ierr = SNESDefaultMatrixFreeSetParameters2(Jmf,PETSC_DEFAULT,PETSC_DEFAULT,h);CHKERRQ(ierr); 272 273 /* Compute matrix-vector product via differencing approximation */ 274 ierr = MatMult(Jmf,p,yy2);CHKERRQ(ierr); 275 ierr = VecNorm(yy2,NORM_2,&yy2n);CHKERRQ(ierr); 276 277 /* View product vector if desired */ 278 if (printv) { 279 sprintf(filename,"y2.%d.out",(int)i); 280 ierr = PetscViewerASCIIOpen(comm,filename,&view2);CHKERRQ(ierr); 281 ierr = PetscViewerSetFormat(view2,PETSC_VIEWER_ASCII_COMMON);CHKERRQ(ierr); 282 ierr = VecView(yy2,view2);CHKERRQ(ierr); 283 ierr = PetscViewerDestroy(&view2);CHKERRQ(ierr); 284 } 285 286 /* Compute relative error */ 287 ierr = VecAXPY(yy2,alpha,yy1);CHKERRQ(ierr); 288 ierr = VecNorm(yy2,NORM_2,&enorm);CHKERRQ(ierr); 289 enorm = enorm/yy1n; 290 ierr = PetscFPrintf(comm,stdout,"h = %G: relative error = %G\n",h,enorm);CHKERRQ(ierr); 291 h *= 10.0; 292 } 293 PetscFunctionReturn(0); 294 } 295 296 static PetscInt lin_its_total = 0; 297 298 PetscErrorCode SNESNoiseMonitor(SNES snes,PetscInt its,double fnorm,void *dummy) 299 { 300 PetscErrorCode ierr; 301 PetscInt lin_its; 302 303 PetscFunctionBegin; 304 ierr = SNESGetLinearSolveIterations(snes,&lin_its);CHKERRQ(ierr); 305 lin_its_total += lin_its; 306 ierr = PetscPrintf(((PetscObject)snes)->comm, "iter = %D, SNES Function norm = %G, lin_its = %D, total_lin_its = %D\n",its,fnorm,lin_its,lin_its_total);CHKERRQ(ierr); 307 308 ierr = SNESUnSetMatrixFreeParameter(snes);CHKERRQ(ierr); 309 PetscFunctionReturn(0); 310 } 311