1 2 #ifdef PETSC_RCS_HEADER 3 static char vcid[] = "$Id: fdmatrix.c,v 1.43 1999/03/17 23:23:40 bsmith Exp curfman $"; 4 #endif 5 6 /* 7 This is where the abstract matrix operations are defined that are 8 used for finite difference computations of Jacobians using coloring. 9 */ 10 11 #include "petsc.h" 12 #include "src/mat/matimpl.h" /*I "mat.h" I*/ 13 #include "src/vec/vecimpl.h" 14 15 #undef __FUNC__ 16 #define __FUNC__ "MatFDColoringView_Draw" 17 static int MatFDColoringView_Draw(MatFDColoring fd,Viewer viewer) 18 { 19 int ierr,i,j,pause; 20 PetscTruth isnull; 21 Draw draw; 22 double xr,yr,xl,yl,h,w,x,y,xc,yc,scale = 0.0; 23 DrawButton button; 24 25 PetscFunctionBegin; 26 ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr); 27 ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 28 ierr = DrawSynchronizedClear(draw); CHKERRQ(ierr); 29 30 xr = fd->N; yr = fd->M; h = yr/10.0; w = xr/10.0; 31 xr += w; yr += h; xl = -w; yl = -h; 32 ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 33 34 /* loop over colors */ 35 for (i=0; i<fd->ncolors; i++ ) { 36 for ( j=0; j<fd->nrows[i]; j++ ) { 37 y = fd->M - fd->rows[i][j] - fd->rstart; 38 x = fd->columnsforrow[i][j]; 39 ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1); CHKERRQ(ierr); 40 } 41 } 42 ierr = DrawSynchronizedFlush(draw); CHKERRQ(ierr); 43 ierr = DrawGetPause(draw,&pause); CHKERRQ(ierr); 44 if (pause >= 0) { PetscSleep(pause); PetscFunctionReturn(0);} 45 ierr = DrawCheckResizedWindow(draw); CHKERRQ(ierr); 46 ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0); CHKERRQ(ierr); 47 while (button != BUTTON_RIGHT) { 48 ierr = DrawSynchronizedClear(draw); CHKERRQ(ierr); 49 if (button == BUTTON_LEFT) scale = .5; 50 else if (button == BUTTON_CENTER) scale = 2.; 51 xl = scale*(xl + w - xc) + xc - w*scale; 52 xr = scale*(xr - w - xc) + xc + w*scale; 53 yl = scale*(yl + h - yc) + yc - h*scale; 54 yr = scale*(yr - h - yc) + yc + h*scale; 55 w *= scale; h *= scale; 56 ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 57 /* loop over colors */ 58 for (i=0; i<fd->ncolors; i++ ) { 59 for ( j=0; j<fd->nrows[i]; j++ ) { 60 y = fd->M - fd->rows[i][j] - fd->rstart; 61 x = fd->columnsforrow[i][j]; 62 ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1); CHKERRQ(ierr); 63 } 64 } 65 ierr = DrawCheckResizedWindow(draw); CHKERRQ(ierr); 66 ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0); CHKERRQ(ierr); 67 } 68 69 PetscFunctionReturn(0); 70 } 71 72 #undef __FUNC__ 73 #define __FUNC__ "MatFDColoringView" 74 /*@C 75 MatFDColoringView - Views a finite difference coloring context. 76 77 Collective on MatFDColoring unless Viewer is VIEWER_STDOUT_SELF 78 79 Input Parameters: 80 + c - the coloring context 81 - viewer - visualization context 82 83 Level: intermediate 84 85 Notes: 86 The available visualization contexts include 87 + VIEWER_STDOUT_SELF - standard output (default) 88 . VIEWER_STDOUT_WORLD - synchronized standard 89 output where only the first processor opens 90 the file. All other processors send their 91 data to the first processor to print. 92 - VIEWER_DRAW_WORLD - graphical display of nonzero structure 93 94 .seealso: MatFDColoringCreate() 95 96 .keywords: Mat, finite differences, coloring, view 97 @*/ 98 int MatFDColoringView(MatFDColoring c,Viewer viewer) 99 { 100 ViewerType vtype; 101 int i,j,format,ierr; 102 103 PetscFunctionBegin; 104 PetscValidHeaderSpecific(c,MAT_FDCOLORING_COOKIE); 105 if (viewer) {PetscValidHeader(viewer);} 106 else {viewer = VIEWER_STDOUT_SELF;} 107 108 ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 109 if (PetscTypeCompare(vtype,DRAW_VIEWER)) { 110 ierr = MatFDColoringView_Draw(c,viewer); CHKERRQ(ierr); 111 PetscFunctionReturn(0); 112 } else if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 113 ViewerASCIIPrintf(viewer,"MatFDColoring Object:\n"); 114 ViewerASCIIPrintf(viewer," Error tolerance=%g\n",c->error_rel); 115 ViewerASCIIPrintf(viewer," Umin=%g\n",c->umin); 116 ViewerASCIIPrintf(viewer," Number of colors=%d\n",c->ncolors); 117 118 ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 119 if (format != VIEWER_FORMAT_ASCII_INFO) { 120 for ( i=0; i<c->ncolors; i++ ) { 121 ViewerASCIIPrintf(viewer," Information for color %d\n",i); 122 ViewerASCIIPrintf(viewer," Number of columns %d\n",c->ncolumns[i]); 123 for ( j=0; j<c->ncolumns[i]; j++ ) { 124 ViewerASCIIPrintf(viewer," %d\n",c->columns[i][j]); 125 } 126 ViewerASCIIPrintf(viewer," Number of rows %d\n",c->nrows[i]); 127 for ( j=0; j<c->nrows[i]; j++ ) { 128 ViewerASCIIPrintf(viewer," %d %d \n",c->rows[i][j],c->columnsforrow[i][j]); 129 } 130 } 131 } 132 } else { 133 SETERRQ(1,1,"Viewer type not supported for this object"); 134 } 135 PetscFunctionReturn(0); 136 } 137 138 #undef __FUNC__ 139 #define __FUNC__ "MatFDColoringSetParameters" 140 /*@ 141 MatFDColoringSetParameters - Sets the parameters for the sparse approximation of 142 a Jacobian matrix using finite differences. 143 144 Collective on MatFDColoring 145 146 The Jacobian is estimated with the differencing approximation 147 .vb 148 F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where 149 h = error_rel*u[i] if abs(u[i]) > umin 150 = +/- error_rel*umin otherwise, with +/- determined by the sign of u[i] 151 dx_{i} = (0, ... 1, .... 0) 152 .ve 153 154 Input Parameters: 155 + coloring - the coloring context 156 . error_rel - relative error 157 - umin - minimum allowable u-value magnitude 158 159 Level: advanced 160 161 .keywords: Mat, finite differences, coloring, set, parameters 162 163 .seealso: MatFDColoringCreate() 164 @*/ 165 int MatFDColoringSetParameters(MatFDColoring matfd,double error,double umin) 166 { 167 PetscFunctionBegin; 168 PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE); 169 170 if (error != PETSC_DEFAULT) matfd->error_rel = error; 171 if (umin != PETSC_DEFAULT) matfd->umin = umin; 172 PetscFunctionReturn(0); 173 } 174 175 #undef __FUNC__ 176 #define __FUNC__ "MatFDColoringSetFrequency" 177 /*@ 178 MatFDColoringSetFrequency - Sets the frequency for computing new Jacobian 179 matrices. 180 181 Collective on MatFDColoring 182 183 Input Parameters: 184 + coloring - the coloring context 185 - freq - frequency (default is 1) 186 187 Options Database Keys: 188 . -mat_fd_coloring_freq <freq> - Sets coloring frequency 189 190 Level: advanced 191 192 Notes: 193 Using a modified Newton strategy, where the Jacobian remains fixed for several 194 iterations, can be cost effective in terms of overall nonlinear solution 195 efficiency. This parameter indicates that a new Jacobian will be computed every 196 <freq> nonlinear iterations. 197 198 .keywords: Mat, finite differences, coloring, set, frequency 199 200 .seealso: MatFDColoringCreate(), MatFDColoringGetFrequency() 201 @*/ 202 int MatFDColoringSetFrequency(MatFDColoring matfd,int freq) 203 { 204 PetscFunctionBegin; 205 PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE); 206 207 matfd->freq = freq; 208 PetscFunctionReturn(0); 209 } 210 211 #undef __FUNC__ 212 #define __FUNC__ "MatFDColoringGetFrequency" 213 /*@ 214 MatFDColoringGetFrequency - Gets the frequency for computing new Jacobian 215 matrices. 216 217 Not Collective 218 219 Input Parameters: 220 . coloring - the coloring context 221 222 Output Parameters: 223 . freq - frequency (default is 1) 224 225 Options Database Keys: 226 . -mat_fd_coloring_freq <freq> - Sets coloring frequency 227 228 Level: advanced 229 230 Notes: 231 Using a modified Newton strategy, where the Jacobian remains fixed for several 232 iterations, can be cost effective in terms of overall nonlinear solution 233 efficiency. This parameter indicates that a new Jacobian will be computed every 234 <freq> nonlinear iterations. 235 236 .keywords: Mat, finite differences, coloring, get, frequency 237 238 .seealso: MatFDColoringSetFrequency() 239 @*/ 240 int MatFDColoringGetFrequency(MatFDColoring matfd,int *freq) 241 { 242 PetscFunctionBegin; 243 PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE); 244 245 *freq = matfd->freq; 246 PetscFunctionReturn(0); 247 } 248 249 #undef __FUNC__ 250 #define __FUNC__ "MatFDColoringSetFunction" 251 /*@C 252 MatFDColoringSetFunction - Sets the function to use for computing the Jacobian. 253 254 Collective on MatFDColoring 255 256 Input Parameters: 257 + coloring - the coloring context 258 . f - the function 259 - fctx - the optional user-defined function context 260 261 Level: intermediate 262 263 Notes: 264 In Fortran you must call MatFDColoringSetFunctionSNES() for a coloring object to 265 be used with the SNES solvers and MatFDColoringSetFunctionTS() if it is to be used 266 with the TS solvers. 267 268 .keywords: Mat, Jacobian, finite differences, set, function 269 @*/ 270 int MatFDColoringSetFunction(MatFDColoring matfd,int (*f)(void),void *fctx) 271 { 272 PetscFunctionBegin; 273 PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE); 274 275 matfd->f = f; 276 matfd->fctx = fctx; 277 278 PetscFunctionReturn(0); 279 } 280 281 #undef __FUNC__ 282 #define __FUNC__ "MatFDColoringSetFromOptions" 283 /*@ 284 MatFDColoringSetFromOptions - Sets coloring finite difference parameters from 285 the options database. 286 287 Collective on MatFDColoring 288 289 The Jacobian, F'(u), is estimated with the differencing approximation 290 .vb 291 F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where 292 h = error_rel*u[i] if abs(u[i]) > umin 293 = +/- error_rel*umin otherwise, with +/- determined by the sign of u[i] 294 dx_{i} = (0, ... 1, .... 0) 295 .ve 296 297 Input Parameter: 298 . coloring - the coloring context 299 300 Options Database Keys: 301 + -mat_fd_coloring_error <err> - Sets <err> (square root 302 of relative error in the function) 303 . -mat_fd_coloring_umin <umin> - Sets umin, the minimum allowable u-value magnitude 304 . -mat_fd_coloring_freq <freq> - Sets frequency of computing a new Jacobian 305 . -mat_fd_coloring_view - Activates basic viewing 306 . -mat_fd_coloring_view_info - Activates viewing info 307 - -mat_fd_coloring_view_draw - Activates drawing 308 309 Level: intermediate 310 311 .keywords: Mat, finite differences, parameters 312 @*/ 313 int MatFDColoringSetFromOptions(MatFDColoring matfd) 314 { 315 int ierr,flag,freq = 1; 316 double error = PETSC_DEFAULT,umin = PETSC_DEFAULT; 317 318 PetscFunctionBegin; 319 PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE); 320 321 ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_err",&error,&flag);CHKERRQ(ierr); 322 ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_umin",&umin,&flag);CHKERRQ(ierr); 323 ierr = MatFDColoringSetParameters(matfd,error,umin); CHKERRQ(ierr); 324 ierr = OptionsGetInt(matfd->prefix,"-mat_fd_coloring_freq",&freq,&flag);CHKERRQ(ierr); 325 ierr = MatFDColoringSetFrequency(matfd,freq);CHKERRQ(ierr); 326 ierr = OptionsHasName(PETSC_NULL,"-help",&flag); CHKERRQ(ierr); 327 if (flag) { 328 ierr = MatFDColoringPrintHelp(matfd); CHKERRQ(ierr); 329 } 330 PetscFunctionReturn(0); 331 } 332 333 #undef __FUNC__ 334 #define __FUNC__ "MatFDColoringPrintHelp" 335 /*@ 336 MatFDColoringPrintHelp - Prints help message for matrix finite difference calculations 337 using coloring. 338 339 Collective on MatFDColoring 340 341 Input Parameter: 342 . fdcoloring - the MatFDColoring context 343 344 Level: intermediate 345 346 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringSetFromOptions() 347 @*/ 348 int MatFDColoringPrintHelp(MatFDColoring fd) 349 { 350 PetscFunctionBegin; 351 PetscValidHeaderSpecific(fd,MAT_FDCOLORING_COOKIE); 352 353 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_err <err>: set sqrt rel tol in function, defaults to %g\n",fd->error_rel); 354 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_umin <umin>: see users manual, defaults to %d\n",fd->umin); 355 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_freq <freq>: frequency that Jacobian is recomputed, defaults to %d\n",fd->freq); 356 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view\n"); 357 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_draw\n"); 358 (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_info\n"); 359 PetscFunctionReturn(0); 360 } 361 362 int MatFDColoringView_Private(MatFDColoring fd) 363 { 364 int ierr,flg; 365 366 PetscFunctionBegin; 367 ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view",&flg); CHKERRQ(ierr); 368 if (flg) { 369 ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm)); CHKERRQ(ierr); 370 } 371 ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_info",&flg); CHKERRQ(ierr); 372 if (flg) { 373 ierr = ViewerPushFormat(VIEWER_STDOUT_(fd->comm),VIEWER_FORMAT_ASCII_INFO,PETSC_NULL);CHKERRQ(ierr); 374 ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm)); CHKERRQ(ierr); 375 ierr = ViewerPopFormat(VIEWER_STDOUT_(fd->comm));CHKERRQ(ierr); 376 } 377 ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_draw",&flg); CHKERRQ(ierr); 378 if (flg) { 379 ierr = MatFDColoringView(fd,VIEWER_DRAW_(fd->comm)); CHKERRQ(ierr); 380 ierr = ViewerFlush(VIEWER_DRAW_(fd->comm)); CHKERRQ(ierr); 381 } 382 PetscFunctionReturn(0); 383 } 384 385 #undef __FUNC__ 386 #define __FUNC__ "MatFDColoringCreate" 387 /*@C 388 MatFDColoringCreate - Creates a matrix coloring context for finite difference 389 computation of Jacobians. 390 391 Collective on Mat 392 393 Input Parameters: 394 + mat - the matrix containing the nonzero structure of the Jacobian 395 - iscoloring - the coloring of the matrix 396 397 Output Parameter: 398 . color - the new coloring context 399 400 Options Database Keys: 401 + -mat_fd_coloring_view - Activates basic viewing or coloring 402 . -mat_fd_coloring_view_draw - Activates drawing of coloring 403 - -mat_fd_coloring_view_info - Activates viewing of coloring info 404 405 Level: intermediate 406 407 .seealso: MatFDColoringDestroy() 408 @*/ 409 int MatFDColoringCreate(Mat mat,ISColoring iscoloring,MatFDColoring *color) 410 { 411 MatFDColoring c; 412 MPI_Comm comm; 413 int ierr,M,N; 414 415 PetscFunctionBegin; 416 ierr = MatGetSize(mat,&M,&N); CHKERRQ(ierr); 417 if (M != N) SETERRQ(PETSC_ERR_SUP,0,"Only for square matrices"); 418 419 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 420 PetscHeaderCreate(c,_p_MatFDColoring,int,MAT_FDCOLORING_COOKIE,0,"MatFDColoring",comm, 421 MatFDColoringDestroy,MatFDColoringView); 422 PLogObjectCreate(c); 423 424 if (mat->ops->fdcoloringcreate) { 425 ierr = (*mat->ops->fdcoloringcreate)(mat,iscoloring,c); CHKERRQ(ierr); 426 } else { 427 SETERRQ(PETSC_ERR_SUP,0,"Code not yet written for this matrix type"); 428 } 429 430 c->error_rel = 1.e-8; 431 c->umin = 1.e-6; 432 c->freq = 1; 433 434 ierr = MatFDColoringView_Private(c); CHKERRQ(ierr); 435 436 *color = c; 437 438 PetscFunctionReturn(0); 439 } 440 441 #undef __FUNC__ 442 #define __FUNC__ "MatFDColoringDestroy" 443 /*@C 444 MatFDColoringDestroy - Destroys a matrix coloring context that was created 445 via MatFDColoringCreate(). 446 447 Collective on MatFDColoring 448 449 Input Parameter: 450 . c - coloring context 451 452 Level: intermediate 453 454 .seealso: MatFDColoringCreate() 455 @*/ 456 int MatFDColoringDestroy(MatFDColoring c) 457 { 458 int i,ierr; 459 460 PetscFunctionBegin; 461 if (--c->refct > 0) PetscFunctionReturn(0); 462 463 464 for ( i=0; i<c->ncolors; i++ ) { 465 if (c->columns[i]) PetscFree(c->columns[i]); 466 if (c->rows[i]) PetscFree(c->rows[i]); 467 if (c->columnsforrow[i]) PetscFree(c->columnsforrow[i]); 468 } 469 PetscFree(c->ncolumns); 470 PetscFree(c->columns); 471 PetscFree(c->nrows); 472 PetscFree(c->rows); 473 PetscFree(c->columnsforrow); 474 PetscFree(c->scale); 475 if (c->w1) { 476 ierr = VecDestroy(c->w1); CHKERRQ(ierr); 477 ierr = VecDestroy(c->w2); CHKERRQ(ierr); 478 ierr = VecDestroy(c->w3); CHKERRQ(ierr); 479 } 480 PLogObjectDestroy(c); 481 PetscHeaderDestroy(c); 482 PetscFunctionReturn(0); 483 } 484 485 #include "snes.h" 486 487 #undef __FUNC__ 488 #define __FUNC__ "MatFDColoringApply" 489 /*@ 490 MatFDColoringApply - Given a matrix for which a MatFDColoring context 491 has been created, computes the Jacobian for a function via finite differences. 492 493 Collective on MatFDColoring 494 495 Input Parameters: 496 + mat - location to store Jacobian 497 . coloring - coloring context created with MatFDColoringCreate() 498 . x1 - location at which Jacobian is to be computed 499 - sctx - optional context required by function (actually a SNES context) 500 501 Options Database Keys: 502 . -mat_fd_coloring_freq <freq> - Sets coloring frequency 503 504 Level: intermediate 505 506 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView() 507 508 .keywords: coloring, Jacobian, finite differences 509 @*/ 510 int MatFDColoringApply(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 511 { 512 int k,fg,ierr,N,start,end,l,row,col,srow; 513 Scalar dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale; 514 double epsilon = coloring->error_rel, umin = coloring->umin; 515 MPI_Comm comm = coloring->comm; 516 Vec w1,w2,w3; 517 int (*f)(void *,Vec,Vec,void *) = ( int (*)(void *,Vec,Vec,void *))coloring->f; 518 void *fctx = coloring->fctx; 519 520 PetscFunctionBegin; 521 PetscValidHeaderSpecific(J,MAT_COOKIE); 522 PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE); 523 PetscValidHeaderSpecific(x1,VEC_COOKIE); 524 525 526 if (!coloring->w1) { 527 ierr = VecDuplicate(x1,&coloring->w1); CHKERRQ(ierr); 528 PLogObjectParent(coloring,coloring->w1); 529 ierr = VecDuplicate(x1,&coloring->w2); CHKERRQ(ierr); 530 PLogObjectParent(coloring,coloring->w2); 531 ierr = VecDuplicate(x1,&coloring->w3); CHKERRQ(ierr); 532 PLogObjectParent(coloring,coloring->w3); 533 } 534 w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 535 536 ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg); CHKERRQ(ierr); 537 if (fg) { 538 PLogInfo(coloring,"MatFDColoringApply: Not calling MatZeroEntries()\n"); 539 } else { 540 ierr = MatZeroEntries(J); CHKERRQ(ierr); 541 } 542 543 ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr); 544 ierr = VecGetSize(x1,&N); CHKERRQ(ierr); 545 ierr = (*f)(sctx,x1,w1,fctx); CHKERRQ(ierr); 546 547 PetscMemzero(wscale,N*sizeof(Scalar)); 548 /* 549 Loop over each color 550 */ 551 552 ierr = VecGetArray(x1,&xx); CHKERRQ(ierr); 553 for (k=0; k<coloring->ncolors; k++) { 554 ierr = VecCopy(x1,w3); CHKERRQ(ierr); 555 /* 556 Loop over each column associated with color adding the 557 perturbation to the vector w3. 558 */ 559 for (l=0; l<coloring->ncolumns[k]; l++) { 560 col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 561 dx = xx[col-start]; 562 if (dx == 0.0) dx = 1.0; 563 #if !defined(USE_PETSC_COMPLEX) 564 if (dx < umin && dx >= 0.0) dx = umin; 565 else if (dx < 0.0 && dx > -umin) dx = -umin; 566 #else 567 if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0) dx = umin; 568 else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 569 #endif 570 dx *= epsilon; 571 wscale[col] = 1.0/dx; 572 ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES);CHKERRQ(ierr); 573 } 574 575 /* 576 Evaluate function at x1 + dx (here dx is a vector of perturbations) 577 */ 578 ierr = (*f)(sctx,w3,w2,fctx); CHKERRQ(ierr); 579 ierr = VecAXPY(&mone,w1,w2); CHKERRQ(ierr); 580 /* Communicate scale to all processors */ 581 #if !defined(USE_PETSC_COMPLEX) 582 ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr); 583 #else 584 ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr); 585 #endif 586 /* 587 Loop over rows of vector, putting results into Jacobian matrix 588 */ 589 ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 590 for (l=0; l<coloring->nrows[k]; l++) { 591 row = coloring->rows[k][l]; 592 col = coloring->columnsforrow[k][l]; 593 y[row] *= scale[col]; 594 srow = row + start; 595 ierr = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 596 } 597 ierr = VecRestoreArray(w2,&y); CHKERRQ(ierr); 598 } 599 ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 600 ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 601 ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 602 PetscFunctionReturn(0); 603 } 604 605 #include "ts.h" 606 607 #undef __FUNC__ 608 #define __FUNC__ "MatFDColoringApplyTS" 609 /*@ 610 MatFDColoringApplyTS - Given a matrix for which a MatFDColoring context 611 has been created, computes the Jacobian for a function via finite differences. 612 613 Collective on Mat, MatFDColoring, and Vec 614 615 Input Parameters: 616 + mat - location to store Jacobian 617 . coloring - coloring context created with MatFDColoringCreate() 618 . x1 - location at which Jacobian is to be computed 619 - sctx - optional context required by function (actually a SNES context) 620 621 Options Database Keys: 622 . -mat_fd_coloring_freq <freq> - Sets coloring frequency 623 624 Level: intermediate 625 626 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView() 627 628 .keywords: coloring, Jacobian, finite differences 629 @*/ 630 int MatFDColoringApplyTS(Mat J,MatFDColoring coloring,double t,Vec x1,MatStructure *flag,void *sctx) 631 { 632 int k,fg,ierr,N,start,end,l,row,col,srow; 633 Scalar dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale; 634 double epsilon = coloring->error_rel, umin = coloring->umin; 635 MPI_Comm comm = coloring->comm; 636 Vec w1,w2,w3; 637 int (*f)(void *,double,Vec,Vec,void *) = ( int (*)(void *,double,Vec,Vec,void *))coloring->f; 638 void *fctx = coloring->fctx; 639 640 PetscFunctionBegin; 641 PetscValidHeaderSpecific(J,MAT_COOKIE); 642 PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE); 643 PetscValidHeaderSpecific(x1,VEC_COOKIE); 644 645 if (!coloring->w1) { 646 ierr = VecDuplicate(x1,&coloring->w1); CHKERRQ(ierr); 647 PLogObjectParent(coloring,coloring->w1); 648 ierr = VecDuplicate(x1,&coloring->w2); CHKERRQ(ierr); 649 PLogObjectParent(coloring,coloring->w2); 650 ierr = VecDuplicate(x1,&coloring->w3); CHKERRQ(ierr); 651 PLogObjectParent(coloring,coloring->w3); 652 } 653 w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 654 655 ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg); CHKERRQ(ierr); 656 if (fg) { 657 PLogInfo(coloring,"MatFDColoringApplyTS: Not calling MatZeroEntries()\n"); 658 } else { 659 ierr = MatZeroEntries(J); CHKERRQ(ierr); 660 } 661 662 ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr); 663 ierr = VecGetSize(x1,&N); CHKERRQ(ierr); 664 ierr = (*f)(sctx,t,x1,w1,fctx); CHKERRQ(ierr); 665 666 PetscMemzero(wscale,N*sizeof(Scalar)); 667 /* 668 Loop over each color 669 */ 670 671 ierr = VecGetArray(x1,&xx); CHKERRQ(ierr); 672 for (k=0; k<coloring->ncolors; k++) { 673 ierr = VecCopy(x1,w3); CHKERRQ(ierr); 674 /* 675 Loop over each column associated with color adding the 676 perturbation to the vector w3. 677 */ 678 for (l=0; l<coloring->ncolumns[k]; l++) { 679 col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 680 dx = xx[col-start]; 681 if (dx == 0.0) dx = 1.0; 682 #if !defined(USE_PETSC_COMPLEX) 683 if (dx < umin && dx >= 0.0) dx = umin; 684 else if (dx < 0.0 && dx > -umin) dx = -umin; 685 #else 686 if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0) dx = umin; 687 else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 688 #endif 689 dx *= epsilon; 690 wscale[col] = 1.0/dx; 691 ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES); CHKERRQ(ierr); 692 } 693 /* 694 Evaluate function at x1 + dx (here dx is a vector of perturbations) 695 */ 696 ierr = (*f)(sctx,t,w3,w2,fctx); CHKERRQ(ierr); 697 ierr = VecAXPY(&mone,w1,w2); CHKERRQ(ierr); 698 /* Communicate scale to all processors */ 699 #if !defined(USE_PETSC_COMPLEX) 700 ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr); 701 #else 702 ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr); 703 #endif 704 /* 705 Loop over rows of vector, putting results into Jacobian matrix 706 */ 707 ierr = VecGetArray(w2,&y); CHKERRQ(ierr); 708 for (l=0; l<coloring->nrows[k]; l++) { 709 row = coloring->rows[k][l]; 710 col = coloring->columnsforrow[k][l]; 711 y[row] *= scale[col]; 712 srow = row + start; 713 ierr = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 714 } 715 ierr = VecRestoreArray(w2,&y); CHKERRQ(ierr); 716 } 717 ierr = VecRestoreArray(x1,&xx); CHKERRQ(ierr); 718 ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 719 ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 720 PetscFunctionReturn(0); 721 } 722 723 724 725