1be1d678aSKris Buschelman 2bbf0e169SBarry Smith /* 3639f9d9dSBarry Smith This is where the abstract matrix operations are defined that are 4639f9d9dSBarry Smith used for finite difference computations of Jacobians using coloring. 5bbf0e169SBarry Smith */ 6bbf0e169SBarry Smith 7af0996ceSBarry Smith #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 8af0996ceSBarry Smith #include <petsc/private/isimpl.h> 9bbf0e169SBarry Smith 107087cfbeSBarry Smith PetscErrorCode MatFDColoringSetF(MatFDColoring fd,Vec F) 113a7fca6bSBarry Smith { 123a7fca6bSBarry Smith PetscFunctionBegin; 134e269d77SPeter Brune if (F) { 145f80ce2aSJacob Faibussowitsch CHKERRQ(VecCopy(F,fd->w1)); 154e269d77SPeter Brune fd->fset = PETSC_TRUE; 164e269d77SPeter Brune } else { 174e269d77SPeter Brune fd->fset = PETSC_FALSE; 184e269d77SPeter Brune } 193a7fca6bSBarry Smith PetscFunctionReturn(0); 203a7fca6bSBarry Smith } 213a7fca6bSBarry Smith 229804daf3SBarry Smith #include <petscdraw.h> 236849ba73SBarry Smith static PetscErrorCode MatFDColoringView_Draw_Zoom(PetscDraw draw,void *Aa) 249194eea9SBarry Smith { 259194eea9SBarry Smith MatFDColoring fd = (MatFDColoring)Aa; 26b312708bSHong Zhang PetscInt i,j,nz,row; 279194eea9SBarry Smith PetscReal x,y; 28b312708bSHong Zhang MatEntry *Jentry=fd->matentry; 299194eea9SBarry Smith 309194eea9SBarry Smith PetscFunctionBegin; 319194eea9SBarry Smith /* loop over colors */ 32b312708bSHong Zhang nz = 0; 339194eea9SBarry Smith for (i=0; i<fd->ncolors; i++) { 349194eea9SBarry Smith for (j=0; j<fd->nrows[i]; j++) { 35b312708bSHong Zhang row = Jentry[nz].row; 36b312708bSHong Zhang y = fd->M - row - fd->rstart; 37b312708bSHong Zhang x = (PetscReal)Jentry[nz++].col; 385f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1)); 399194eea9SBarry Smith } 409194eea9SBarry Smith } 419194eea9SBarry Smith PetscFunctionReturn(0); 429194eea9SBarry Smith } 439194eea9SBarry Smith 446849ba73SBarry Smith static PetscErrorCode MatFDColoringView_Draw(MatFDColoring fd,PetscViewer viewer) 45005c665bSBarry Smith { 46ace3abfcSBarry Smith PetscBool isnull; 47b0a32e0cSBarry Smith PetscDraw draw; 4854d96fa3SBarry Smith PetscReal xr,yr,xl,yl,h,w; 49005c665bSBarry Smith 503a40ed3dSBarry Smith PetscFunctionBegin; 515f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerDrawGetDraw(viewer,0,&draw)); 525f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDrawIsNull(draw,&isnull)); 53832b7cebSLisandro Dalcin if (isnull) PetscFunctionReturn(0); 54005c665bSBarry Smith 55005c665bSBarry Smith xr = fd->N; yr = fd->M; h = yr/10.0; w = xr/10.0; 56005c665bSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 575f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDrawSetCoordinates(draw,xl,yl,xr,yr)); 585f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectCompose((PetscObject)fd,"Zoomviewer",(PetscObject)viewer)); 595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDrawZoom(draw,MatFDColoringView_Draw_Zoom,fd)); 605f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectCompose((PetscObject)fd,"Zoomviewer",NULL)); 615f80ce2aSJacob Faibussowitsch CHKERRQ(PetscDrawSave(draw)); 623a40ed3dSBarry Smith PetscFunctionReturn(0); 63005c665bSBarry Smith } 64005c665bSBarry Smith 65bbf0e169SBarry Smith /*@C 66639f9d9dSBarry Smith MatFDColoringView - Views a finite difference coloring context. 67bbf0e169SBarry Smith 684c49b128SBarry Smith Collective on MatFDColoring 69fee21e36SBarry Smith 70ef5ee4d1SLois Curfman McInnes Input Parameters: 71ef5ee4d1SLois Curfman McInnes + c - the coloring context 72ef5ee4d1SLois Curfman McInnes - viewer - visualization context 73ef5ee4d1SLois Curfman McInnes 7415091d37SBarry Smith Level: intermediate 7515091d37SBarry Smith 76b4fc646aSLois Curfman McInnes Notes: 77b4fc646aSLois Curfman McInnes The available visualization contexts include 78b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 79b0a32e0cSBarry Smith . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 80ef5ee4d1SLois Curfman McInnes output where only the first processor opens 81ef5ee4d1SLois Curfman McInnes the file. All other processors send their 82ef5ee4d1SLois Curfman McInnes data to the first processor to print. 83b0a32e0cSBarry Smith - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 84bbf0e169SBarry Smith 859194eea9SBarry Smith Notes: 869194eea9SBarry Smith Since PETSc uses only a small number of basic colors (currently 33), if the coloring 87b0a32e0cSBarry Smith involves more than 33 then some seemingly identical colors are displayed making it look 889194eea9SBarry Smith like an illegal coloring. This is just a graphical artifact. 899194eea9SBarry Smith 90639f9d9dSBarry Smith .seealso: MatFDColoringCreate() 91005c665bSBarry Smith 92bbf0e169SBarry Smith @*/ 937087cfbeSBarry Smith PetscErrorCode MatFDColoringView(MatFDColoring c,PetscViewer viewer) 94bbf0e169SBarry Smith { 9538baddfdSBarry Smith PetscInt i,j; 96ace3abfcSBarry Smith PetscBool isdraw,iascii; 97f3ef73ceSBarry Smith PetscViewerFormat format; 98bbf0e169SBarry Smith 993a40ed3dSBarry Smith PetscFunctionBegin; 1000700a824SBarry Smith PetscValidHeaderSpecific(c,MAT_FDCOLORING_CLASSID,1); 1013050cee2SBarry Smith if (!viewer) { 1025f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)c),&viewer)); 1033050cee2SBarry Smith } 1040700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 105c9780b6fSBarry Smith PetscCheckSameComm(c,1,viewer,2); 106bbf0e169SBarry Smith 1075f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw)); 1085f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 1090f5bd95cSBarry Smith if (isdraw) { 1105f80ce2aSJacob Faibussowitsch CHKERRQ(MatFDColoringView_Draw(c,viewer)); 11132077d6dSBarry Smith } else if (iascii) { 1125f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectPrintClassNamePrefixType((PetscObject)c,viewer)); 1135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Error tolerance=%g\n",(double)c->error_rel)); 1145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Umin=%g\n",(double)c->umin)); 1155f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Number of colors=%" PetscInt_FMT "\n",c->ncolors)); 116ae09f205SBarry Smith 1175f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerGetFormat(viewer,&format)); 118fb9695e5SSatish Balay if (format != PETSC_VIEWER_ASCII_INFO) { 119b312708bSHong Zhang PetscInt row,col,nz; 120b312708bSHong Zhang nz = 0; 121b4fc646aSLois Curfman McInnes for (i=0; i<c->ncolors; i++) { 1225f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Information for color %" PetscInt_FMT "\n",i)); 1235f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Number of columns %" PetscInt_FMT "\n",c->ncolumns[i])); 124b4fc646aSLois Curfman McInnes for (j=0; j<c->ncolumns[i]; j++) { 1255f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," %" PetscInt_FMT "\n",c->columns[i][j])); 126639f9d9dSBarry Smith } 1275f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," Number of rows %" PetscInt_FMT "\n",c->nrows[i])); 1285bdb020cSBarry Smith if (c->matentry) { 129b4fc646aSLois Curfman McInnes for (j=0; j<c->nrows[i]; j++) { 130b312708bSHong Zhang row = c->matentry[nz].row; 131b312708bSHong Zhang col = c->matentry[nz++].col; 1325f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerASCIIPrintf(viewer," %" PetscInt_FMT " %" PetscInt_FMT " \n",row,col)); 133b4fc646aSLois Curfman McInnes } 134bbf0e169SBarry Smith } 135bbf0e169SBarry Smith } 1365bdb020cSBarry Smith } 1375f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerFlush(viewer)); 138bbf0e169SBarry Smith } 1393a40ed3dSBarry Smith PetscFunctionReturn(0); 140639f9d9dSBarry Smith } 141639f9d9dSBarry Smith 142639f9d9dSBarry Smith /*@ 143b4fc646aSLois Curfman McInnes MatFDColoringSetParameters - Sets the parameters for the sparse approximation of 144b4fc646aSLois Curfman McInnes a Jacobian matrix using finite differences. 145639f9d9dSBarry Smith 1463f9fe445SBarry Smith Logically Collective on MatFDColoring 147ef5ee4d1SLois Curfman McInnes 148ef5ee4d1SLois Curfman McInnes The Jacobian is estimated with the differencing approximation 149ef5ee4d1SLois Curfman McInnes .vb 15065f2ba5bSLois Curfman McInnes F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where 151d461c19dSHong Zhang htype = 'ds': 152f23b5b22SLois Curfman McInnes h = error_rel*u[i] if abs(u[i]) > umin 153f23b5b22SLois Curfman McInnes = +/- error_rel*umin otherwise, with +/- determined by the sign of u[i] 154ef5ee4d1SLois Curfman McInnes dx_{i} = (0, ... 1, .... 0) 155d461c19dSHong Zhang 156d461c19dSHong Zhang htype = 'wp': 157d461c19dSHong Zhang h = error_rel * sqrt(1 + ||u||) 158ef5ee4d1SLois Curfman McInnes .ve 159639f9d9dSBarry Smith 160639f9d9dSBarry Smith Input Parameters: 161ef5ee4d1SLois Curfman McInnes + coloring - the coloring context 162639f9d9dSBarry Smith . error_rel - relative error 163f23b5b22SLois Curfman McInnes - umin - minimum allowable u-value magnitude 164fee21e36SBarry Smith 16515091d37SBarry Smith Level: advanced 16615091d37SBarry Smith 16795b89fc3SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringSetFromOptions() 16895b89fc3SBarry Smith 169639f9d9dSBarry Smith @*/ 1707087cfbeSBarry Smith PetscErrorCode MatFDColoringSetParameters(MatFDColoring matfd,PetscReal error,PetscReal umin) 171639f9d9dSBarry Smith { 1723a40ed3dSBarry Smith PetscFunctionBegin; 1730700a824SBarry Smith PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 174c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(matfd,error,2); 175c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(matfd,umin,3); 176639f9d9dSBarry Smith if (error != PETSC_DEFAULT) matfd->error_rel = error; 177639f9d9dSBarry Smith if (umin != PETSC_DEFAULT) matfd->umin = umin; 1783a40ed3dSBarry Smith PetscFunctionReturn(0); 179639f9d9dSBarry Smith } 180639f9d9dSBarry Smith 181f86b9fbaSHong Zhang /*@ 182c8a9c622SHong Zhang MatFDColoringSetBlockSize - Sets block size for efficient inserting entries of Jacobian matrix. 183005c665bSBarry Smith 184f86b9fbaSHong Zhang Logically Collective on MatFDColoring 185f86b9fbaSHong Zhang 186f86b9fbaSHong Zhang Input Parameters: 187f86b9fbaSHong Zhang + coloring - the coloring context 188f86b9fbaSHong Zhang . brows - number of rows in the block 189f86b9fbaSHong Zhang - bcols - number of columns in the block 190f86b9fbaSHong Zhang 191f86b9fbaSHong Zhang Level: intermediate 192f86b9fbaSHong Zhang 193f86b9fbaSHong Zhang .seealso: MatFDColoringCreate(), MatFDColoringSetFromOptions() 194f86b9fbaSHong Zhang 195f86b9fbaSHong Zhang @*/ 196f86b9fbaSHong Zhang PetscErrorCode MatFDColoringSetBlockSize(MatFDColoring matfd,PetscInt brows,PetscInt bcols) 197f86b9fbaSHong Zhang { 198f86b9fbaSHong Zhang PetscFunctionBegin; 199f86b9fbaSHong Zhang PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 200f86b9fbaSHong Zhang PetscValidLogicalCollectiveInt(matfd,brows,2); 201f86b9fbaSHong Zhang PetscValidLogicalCollectiveInt(matfd,bcols,3); 202f86b9fbaSHong Zhang if (brows != PETSC_DEFAULT) matfd->brows = brows; 203f86b9fbaSHong Zhang if (bcols != PETSC_DEFAULT) matfd->bcols = bcols; 204f86b9fbaSHong Zhang PetscFunctionReturn(0); 205f86b9fbaSHong Zhang } 206f86b9fbaSHong Zhang 207f86b9fbaSHong Zhang /*@ 208f86b9fbaSHong Zhang MatFDColoringSetUp - Sets up the internal data structures of matrix coloring context for the later use. 209f86b9fbaSHong Zhang 210f86b9fbaSHong Zhang Collective on Mat 211f86b9fbaSHong Zhang 212f86b9fbaSHong Zhang Input Parameters: 213f86b9fbaSHong Zhang + mat - the matrix containing the nonzero structure of the Jacobian 214f86b9fbaSHong Zhang . iscoloring - the coloring of the matrix; usually obtained with MatGetColoring() or DMCreateColoring() 215f86b9fbaSHong Zhang - color - the matrix coloring context 216f86b9fbaSHong Zhang 217f86b9fbaSHong Zhang Level: beginner 218f86b9fbaSHong Zhang 219bdaf1daeSBarry Smith Notes: When the coloring type is IS_COLORING_LOCAL the coloring is in the local ordering of the unknowns. 220bdaf1daeSBarry Smith 221f86b9fbaSHong Zhang .seealso: MatFDColoringCreate(), MatFDColoringDestroy() 222f86b9fbaSHong Zhang @*/ 223f86b9fbaSHong Zhang PetscErrorCode MatFDColoringSetUp(Mat mat,ISColoring iscoloring,MatFDColoring color) 224f86b9fbaSHong Zhang { 225bdaf1daeSBarry Smith PetscBool eq; 226f86b9fbaSHong Zhang 227f86b9fbaSHong Zhang PetscFunctionBegin; 228c8a9c622SHong Zhang PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 229c8a9c622SHong Zhang PetscValidHeaderSpecific(color,MAT_FDCOLORING_CLASSID,3); 230c8a9c622SHong Zhang if (color->setupcalled) PetscFunctionReturn(0); 2315f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectCompareId((PetscObject)mat,color->matid,&eq)); 232*28b400f6SJacob Faibussowitsch PetscCheck(eq,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix used with MatFDColoringSetUp() must be that used with MatFDColoringCreate()"); 233c8a9c622SHong Zhang 2345f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventBegin(MAT_FDColoringSetUp,mat,0,0,0)); 235f86b9fbaSHong Zhang if (mat->ops->fdcoloringsetup) { 2365f80ce2aSJacob Faibussowitsch CHKERRQ((*mat->ops->fdcoloringsetup)(mat,iscoloring,color)); 23798921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name); 238c8a9c622SHong Zhang 239c8a9c622SHong Zhang color->setupcalled = PETSC_TRUE; 2405f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventEnd(MAT_FDColoringSetUp,mat,0,0,0)); 241f86b9fbaSHong Zhang PetscFunctionReturn(0); 242f86b9fbaSHong Zhang } 243ff0cfa39SBarry Smith 2444a9d489dSBarry Smith /*@C 2454a9d489dSBarry Smith MatFDColoringGetFunction - Gets the function to use for computing the Jacobian. 2464a9d489dSBarry Smith 2473f9fe445SBarry Smith Not Collective 2484a9d489dSBarry Smith 249f899ff85SJose E. Roman Input Parameter: 2504a9d489dSBarry Smith . coloring - the coloring context 2514a9d489dSBarry Smith 2524a9d489dSBarry Smith Output Parameters: 2534a9d489dSBarry Smith + f - the function 2544a9d489dSBarry Smith - fctx - the optional user-defined function context 2554a9d489dSBarry Smith 2564a9d489dSBarry Smith Level: intermediate 2574a9d489dSBarry Smith 25895b89fc3SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringSetFunction(), MatFDColoringSetFromOptions() 25995b89fc3SBarry Smith 2604a9d489dSBarry Smith @*/ 2617087cfbeSBarry Smith PetscErrorCode MatFDColoringGetFunction(MatFDColoring matfd,PetscErrorCode (**f)(void),void **fctx) 2624a9d489dSBarry Smith { 2634a9d489dSBarry Smith PetscFunctionBegin; 2640700a824SBarry Smith PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 2654a9d489dSBarry Smith if (f) *f = matfd->f; 2664a9d489dSBarry Smith if (fctx) *fctx = matfd->fctx; 2674a9d489dSBarry Smith PetscFunctionReturn(0); 2684a9d489dSBarry Smith } 2694a9d489dSBarry Smith 270d64ed03dSBarry Smith /*@C 271005c665bSBarry Smith MatFDColoringSetFunction - Sets the function to use for computing the Jacobian. 272005c665bSBarry Smith 2733f9fe445SBarry Smith Logically Collective on MatFDColoring 274fee21e36SBarry Smith 275ef5ee4d1SLois Curfman McInnes Input Parameters: 276ef5ee4d1SLois Curfman McInnes + coloring - the coloring context 277ef5ee4d1SLois Curfman McInnes . f - the function 278ef5ee4d1SLois Curfman McInnes - fctx - the optional user-defined function context 279ef5ee4d1SLois Curfman McInnes 2807850c7c0SBarry Smith Calling sequence of (*f) function: 2817850c7c0SBarry Smith For SNES: PetscErrorCode (*f)(SNES,Vec,Vec,void*) 282ab637aeaSJed Brown If not using SNES: PetscErrorCode (*f)(void *dummy,Vec,Vec,void*) and dummy is ignored 28315091d37SBarry Smith 2847850c7c0SBarry Smith Level: advanced 2857850c7c0SBarry Smith 28695452b02SPatrick Sanan Notes: 28795452b02SPatrick Sanan This function is usually used automatically by SNES (when one uses SNESSetJacobian() with the argument 2888d359177SBarry Smith SNESComputeJacobianDefaultColor()) and only needs to be used by someone computing a matrix via coloring directly by 289ab637aeaSJed Brown calling MatFDColoringApply() 2907850c7c0SBarry Smith 2917850c7c0SBarry Smith Fortran Notes: 2927850c7c0SBarry Smith In Fortran you must call MatFDColoringSetFunction() for a coloring object to 293ab637aeaSJed Brown be used without SNES or within the SNES solvers. 294f881d145SBarry Smith 29595b89fc3SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringGetFunction(), MatFDColoringSetFromOptions() 29695b89fc3SBarry Smith 297005c665bSBarry Smith @*/ 2987087cfbeSBarry Smith PetscErrorCode MatFDColoringSetFunction(MatFDColoring matfd,PetscErrorCode (*f)(void),void *fctx) 299005c665bSBarry Smith { 3003a40ed3dSBarry Smith PetscFunctionBegin; 3010700a824SBarry Smith PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 302005c665bSBarry Smith matfd->f = f; 303005c665bSBarry Smith matfd->fctx = fctx; 3043a40ed3dSBarry Smith PetscFunctionReturn(0); 305005c665bSBarry Smith } 306005c665bSBarry Smith 307639f9d9dSBarry Smith /*@ 308b4fc646aSLois Curfman McInnes MatFDColoringSetFromOptions - Sets coloring finite difference parameters from 309639f9d9dSBarry Smith the options database. 310639f9d9dSBarry Smith 311fee21e36SBarry Smith Collective on MatFDColoring 312fee21e36SBarry Smith 31365f2ba5bSLois Curfman McInnes The Jacobian, F'(u), is estimated with the differencing approximation 314ef5ee4d1SLois Curfman McInnes .vb 31565f2ba5bSLois Curfman McInnes F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where 316f23b5b22SLois Curfman McInnes h = error_rel*u[i] if abs(u[i]) > umin 317f23b5b22SLois Curfman McInnes = +/- error_rel*umin otherwise, with +/- determined by the sign of u[i] 318ef5ee4d1SLois Curfman McInnes dx_{i} = (0, ... 1, .... 0) 319ef5ee4d1SLois Curfman McInnes .ve 320ef5ee4d1SLois Curfman McInnes 321ef5ee4d1SLois Curfman McInnes Input Parameter: 322ef5ee4d1SLois Curfman McInnes . coloring - the coloring context 323ef5ee4d1SLois Curfman McInnes 324b4fc646aSLois Curfman McInnes Options Database Keys: 3255620d6dcSBarry Smith + -mat_fd_coloring_err <err> - Sets <err> (square root of relative error in the function) 326f23b5b22SLois Curfman McInnes . -mat_fd_coloring_umin <umin> - Sets umin, the minimum allowable u-value magnitude 3273ec795f1SBarry Smith . -mat_fd_type - "wp" or "ds" (see MATMFFD_WP or MATMFFD_DS) 328ef5ee4d1SLois Curfman McInnes . -mat_fd_coloring_view - Activates basic viewing 329e350db43SBarry Smith . -mat_fd_coloring_view ::ascii_info - Activates viewing info 330e350db43SBarry Smith - -mat_fd_coloring_view draw - Activates drawing 331639f9d9dSBarry Smith 33215091d37SBarry Smith Level: intermediate 33315091d37SBarry Smith 334d1c39f55SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringView(), MatFDColoringSetParameters() 335d1c39f55SBarry Smith 336639f9d9dSBarry Smith @*/ 3377087cfbeSBarry Smith PetscErrorCode MatFDColoringSetFromOptions(MatFDColoring matfd) 338639f9d9dSBarry Smith { 339dfbe8321SBarry Smith PetscErrorCode ierr; 340ace3abfcSBarry Smith PetscBool flg; 341efb30889SBarry Smith char value[3]; 3423a40ed3dSBarry Smith 3433a40ed3dSBarry Smith PetscFunctionBegin; 3440700a824SBarry Smith PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 345639f9d9dSBarry Smith 3463194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)matfd);CHKERRQ(ierr); 3475f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsReal("-mat_fd_coloring_err","Square root of relative error in function","MatFDColoringSetParameters",matfd->error_rel,&matfd->error_rel,NULL)); 3485f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsReal("-mat_fd_coloring_umin","Minimum allowable u magnitude","MatFDColoringSetParameters",matfd->umin,&matfd->umin,NULL)); 3495f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsString("-mat_fd_type","Algorithm to compute h, wp or ds","MatFDColoringCreate",matfd->htype,value,sizeof(value),&flg)); 350efb30889SBarry Smith if (flg) { 351efb30889SBarry Smith if (value[0] == 'w' && value[1] == 'p') matfd->htype = "wp"; 352efb30889SBarry Smith else if (value[0] == 'd' && value[1] == 's') matfd->htype = "ds"; 35398921bdaSJacob Faibussowitsch else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown finite differencing type %s",value); 354efb30889SBarry Smith } 3555f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsInt("-mat_fd_coloring_brows","Number of block rows","MatFDColoringSetBlockSize",matfd->brows,&matfd->brows,NULL)); 3565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsInt("-mat_fd_coloring_bcols","Number of block columns","MatFDColoringSetBlockSize",matfd->bcols,&matfd->bcols,&flg)); 35793dfae19SHong Zhang if (flg && matfd->bcols > matfd->ncolors) { 35893dfae19SHong Zhang /* input bcols cannot be > matfd->ncolors, thus set it as ncolors */ 35993dfae19SHong Zhang matfd->bcols = matfd->ncolors; 36093dfae19SHong Zhang } 361f86b9fbaSHong Zhang 3625d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 3635f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)matfd)); 3642f613bf5SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3653a40ed3dSBarry Smith PetscFunctionReturn(0); 366005c665bSBarry Smith } 367005c665bSBarry Smith 36861ab5df0SBarry Smith /*@C 36961ab5df0SBarry Smith MatFDColoringSetType - Sets the approach for computing the finite difference parameter 37061ab5df0SBarry Smith 37161ab5df0SBarry Smith Collective on MatFDColoring 37261ab5df0SBarry Smith 37361ab5df0SBarry Smith Input Parameters: 37461ab5df0SBarry Smith + coloring - the coloring context 37561ab5df0SBarry Smith - type - either MATMFFD_WP or MATMFFD_DS 37661ab5df0SBarry Smith 37761ab5df0SBarry Smith Options Database Keys: 37861ab5df0SBarry Smith . -mat_fd_type - "wp" or "ds" 37961ab5df0SBarry Smith 38061ab5df0SBarry Smith Note: It is goofy that the argument type is MatMFFDType since the MatFDColoring actually computes the matrix entries 38161ab5df0SBarry Smith but the process of computing the entries is the same as as with the MatMFFD operation so we should reuse the names instead of 38261ab5df0SBarry Smith introducing another one. 38361ab5df0SBarry Smith 38461ab5df0SBarry Smith Level: intermediate 38561ab5df0SBarry Smith 38661ab5df0SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringView(), MatFDColoringSetParameters() 38761ab5df0SBarry Smith 38861ab5df0SBarry Smith @*/ 38961ab5df0SBarry Smith PetscErrorCode MatFDColoringSetType(MatFDColoring matfd,MatMFFDType type) 39061ab5df0SBarry Smith { 39161ab5df0SBarry Smith PetscFunctionBegin; 39261ab5df0SBarry Smith PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_CLASSID,1); 39361ab5df0SBarry Smith /* 39461ab5df0SBarry Smith It is goofy to handle the strings this way but currently there is no code to free a dynamically created matfd->htype 39561ab5df0SBarry Smith and this function is being provided as patch for a release so it shouldn't change the implementaton 39661ab5df0SBarry Smith */ 39761ab5df0SBarry Smith if (type[0] == 'w' && type[1] == 'p') matfd->htype = "wp"; 39861ab5df0SBarry Smith else if (type[0] == 'd' && type[1] == 's') matfd->htype = "ds"; 39998921bdaSJacob Faibussowitsch else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown finite differencing type %s",type); 40061ab5df0SBarry Smith PetscFunctionReturn(0); 40161ab5df0SBarry Smith } 40261ab5df0SBarry Smith 403146574abSBarry Smith PetscErrorCode MatFDColoringViewFromOptions(MatFDColoring fd,const char prefix[],const char optionname[]) 404005c665bSBarry Smith { 405e350db43SBarry Smith PetscBool flg; 4063050cee2SBarry Smith PetscViewer viewer; 407cffb1e40SBarry Smith PetscViewerFormat format; 408005c665bSBarry Smith 4093a40ed3dSBarry Smith PetscFunctionBegin; 410146574abSBarry Smith if (prefix) { 4115f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsGetViewer(PetscObjectComm((PetscObject)fd),((PetscObject)fd)->options,prefix,optionname,&viewer,&format,&flg)); 412146574abSBarry Smith } else { 4135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsGetViewer(PetscObjectComm((PetscObject)fd),((PetscObject)fd)->options,((PetscObject)fd)->prefix,optionname,&viewer,&format,&flg)); 414146574abSBarry Smith } 415005c665bSBarry Smith if (flg) { 4165f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerPushFormat(viewer,format)); 4175f80ce2aSJacob Faibussowitsch CHKERRQ(MatFDColoringView(fd,viewer)); 4185f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerPopFormat(viewer)); 4195f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerDestroy(&viewer)); 420005c665bSBarry Smith } 4213a40ed3dSBarry Smith PetscFunctionReturn(0); 422bbf0e169SBarry Smith } 423bbf0e169SBarry Smith 42405869f15SSatish Balay /*@ 425639f9d9dSBarry Smith MatFDColoringCreate - Creates a matrix coloring context for finite difference 426639f9d9dSBarry Smith computation of Jacobians. 427bbf0e169SBarry Smith 428ef5ee4d1SLois Curfman McInnes Collective on Mat 429ef5ee4d1SLois Curfman McInnes 430639f9d9dSBarry Smith Input Parameters: 431ef5ee4d1SLois Curfman McInnes + mat - the matrix containing the nonzero structure of the Jacobian 4327086a01eSPeter Brune - iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring() 433bbf0e169SBarry Smith 434bbf0e169SBarry Smith Output Parameter: 435639f9d9dSBarry Smith . color - the new coloring context 436bbf0e169SBarry Smith 43715091d37SBarry Smith Level: intermediate 43815091d37SBarry Smith 4398d359177SBarry Smith .seealso: MatFDColoringDestroy(),SNESComputeJacobianDefaultColor(), ISColoringCreate(), 440d1c39f55SBarry Smith MatFDColoringSetFunction(), MatFDColoringSetFromOptions(), MatFDColoringApply(), 4416a9b8d82SBarry Smith MatFDColoringView(), MatFDColoringSetParameters(), MatColoringCreate(), DMCreateColoring(), MatFDColoringSetValues() 442bbf0e169SBarry Smith @*/ 4437087cfbeSBarry Smith PetscErrorCode MatFDColoringCreate(Mat mat,ISColoring iscoloring,MatFDColoring *color) 444bbf0e169SBarry Smith { 445639f9d9dSBarry Smith MatFDColoring c; 446639f9d9dSBarry Smith MPI_Comm comm; 44738baddfdSBarry Smith PetscInt M,N; 448639f9d9dSBarry Smith 4493a40ed3dSBarry Smith PetscFunctionBegin; 450c8a9c622SHong Zhang PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 451*28b400f6SJacob Faibussowitsch PetscCheck(mat->assembled,PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled by calls to MatAssemblyBegin/End();"); 4525f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventBegin(MAT_FDColoringCreate,mat,0,0,0)); 4535f80ce2aSJacob Faibussowitsch CHKERRQ(MatGetSize(mat,&M,&N)); 4542c71b3e2SJacob Faibussowitsch PetscCheckFalse(M != N,PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Only for square matrices"); 4555f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectGetComm((PetscObject)mat,&comm)); 4565f80ce2aSJacob Faibussowitsch CHKERRQ(PetscHeaderCreate(c,MAT_FDCOLORING_CLASSID,"MatFDColoring","Jacobian computation via finite differences with coloring","Mat",comm,MatFDColoringDestroy,MatFDColoringView)); 457639f9d9dSBarry Smith 458b8f8c88eSHong Zhang c->ctype = iscoloring->ctype; 4595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectGetId((PetscObject)mat,&c->matid)); 460b8f8c88eSHong Zhang 46193dfae19SHong Zhang if (mat->ops->fdcoloringcreate) { 4625f80ce2aSJacob Faibussowitsch CHKERRQ((*mat->ops->fdcoloringcreate)(mat,iscoloring,c)); 46398921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name); 46493dfae19SHong Zhang 4655f80ce2aSJacob Faibussowitsch CHKERRQ(MatCreateVecs(mat,NULL,&c->w1)); 466ce911d59SRichard Tran Mills /* Vec is used intensively in particular piece of scalar CPU code; won't benefit from bouncing back and forth to the GPU */ 4675f80ce2aSJacob Faibussowitsch CHKERRQ(VecBindToCPU(c->w1,PETSC_TRUE)); 4685f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogObjectParent((PetscObject)c,(PetscObject)c->w1)); 4695f80ce2aSJacob Faibussowitsch CHKERRQ(VecDuplicate(c->w1,&c->w2)); 470ce911d59SRichard Tran Mills /* Vec is used intensively in particular piece of scalar CPU code; won't benefit from bouncing back and forth to the GPU */ 4715f80ce2aSJacob Faibussowitsch CHKERRQ(VecBindToCPU(c->w2,PETSC_TRUE)); 4725f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogObjectParent((PetscObject)c,(PetscObject)c->w2)); 473b8f8c88eSHong Zhang 47477d8c4bbSBarry Smith c->error_rel = PETSC_SQRT_MACHINE_EPSILON; 47577d8c4bbSBarry Smith c->umin = 100.0*PETSC_SQRT_MACHINE_EPSILON; 47649b058dcSBarry Smith c->currentcolor = -1; 477efb30889SBarry Smith c->htype = "wp"; 4784e269d77SPeter Brune c->fset = PETSC_FALSE; 479c8a9c622SHong Zhang c->setupcalled = PETSC_FALSE; 480639f9d9dSBarry Smith 481639f9d9dSBarry Smith *color = c; 4825f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectCompose((PetscObject)mat,"SNESMatFDColoring",(PetscObject)c)); 4835f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventEnd(MAT_FDColoringCreate,mat,0,0,0)); 4843a40ed3dSBarry Smith PetscFunctionReturn(0); 485bbf0e169SBarry Smith } 486bbf0e169SBarry Smith 48705869f15SSatish Balay /*@ 488639f9d9dSBarry Smith MatFDColoringDestroy - Destroys a matrix coloring context that was created 489639f9d9dSBarry Smith via MatFDColoringCreate(). 490bbf0e169SBarry Smith 491ef5ee4d1SLois Curfman McInnes Collective on MatFDColoring 492ef5ee4d1SLois Curfman McInnes 493b4fc646aSLois Curfman McInnes Input Parameter: 494639f9d9dSBarry Smith . c - coloring context 495bbf0e169SBarry Smith 49615091d37SBarry Smith Level: intermediate 49715091d37SBarry Smith 498639f9d9dSBarry Smith .seealso: MatFDColoringCreate() 499bbf0e169SBarry Smith @*/ 5006bf464f9SBarry Smith PetscErrorCode MatFDColoringDestroy(MatFDColoring *c) 501bbf0e169SBarry Smith { 50238baddfdSBarry Smith PetscInt i; 5030df34763SHong Zhang MatFDColoring color = *c; 504bbf0e169SBarry Smith 5053a40ed3dSBarry Smith PetscFunctionBegin; 5066bf464f9SBarry Smith if (!*c) PetscFunctionReturn(0); 507f4259b30SLisandro Dalcin if (--((PetscObject)color)->refct > 0) {*c = NULL; PetscFunctionReturn(0);} 508d4bb536fSBarry Smith 509071fcb05SBarry Smith /* we do not free the column arrays since their entries are owned by the ISs in color->isa */ 5100df34763SHong Zhang for (i=0; i<color->ncolors; i++) { 5115f80ce2aSJacob Faibussowitsch CHKERRQ(ISDestroy(&color->isa[i])); 512bbf0e169SBarry Smith } 5135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(color->isa)); 5145f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree2(color->ncolumns,color->columns)); 5155f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(color->nrows)); 5160df34763SHong Zhang if (color->htype[0] == 'w') { 5175f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(color->matentry2)); 5180df34763SHong Zhang } else { 5195f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(color->matentry)); 5200df34763SHong Zhang } 5215f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(color->dy)); 5225f80ce2aSJacob Faibussowitsch if (color->vscale) CHKERRQ(VecDestroy(&color->vscale)); 5235f80ce2aSJacob Faibussowitsch CHKERRQ(VecDestroy(&color->w1)); 5245f80ce2aSJacob Faibussowitsch CHKERRQ(VecDestroy(&color->w2)); 5255f80ce2aSJacob Faibussowitsch CHKERRQ(VecDestroy(&color->w3)); 5265f80ce2aSJacob Faibussowitsch CHKERRQ(PetscHeaderDestroy(c)); 5273a40ed3dSBarry Smith PetscFunctionReturn(0); 528bbf0e169SBarry Smith } 52943a90d84SBarry Smith 53049b058dcSBarry Smith /*@C 53149b058dcSBarry Smith MatFDColoringGetPerturbedColumns - Returns the indices of the columns that 53249b058dcSBarry Smith that are currently being perturbed. 53349b058dcSBarry Smith 53449b058dcSBarry Smith Not Collective 53549b058dcSBarry Smith 536f899ff85SJose E. Roman Input Parameter: 53749b058dcSBarry Smith . coloring - coloring context created with MatFDColoringCreate() 53849b058dcSBarry Smith 53949b058dcSBarry Smith Output Parameters: 54049b058dcSBarry Smith + n - the number of local columns being perturbed 54149b058dcSBarry Smith - cols - the column indices, in global numbering 54249b058dcSBarry Smith 54321fcc2ddSBarry Smith Level: advanced 54449b058dcSBarry Smith 545e66d0d93SMatthew Knepley Note: IF the matrix type is BAIJ, then the block column indices are returned 546e66d0d93SMatthew Knepley 547edaa7c33SBarry Smith Fortran Note: 548edaa7c33SBarry Smith This routine has a different interface for Fortran 54921fcc2ddSBarry Smith $ #include <petsc/finclude/petscmat.h> 55021fcc2ddSBarry Smith $ use petscmat 551edaa7c33SBarry Smith $ PetscInt, pointer :: array(:) 552edaa7c33SBarry Smith $ PetscErrorCode ierr 553edaa7c33SBarry Smith $ MatFDColoring i 554edaa7c33SBarry Smith $ call MatFDColoringGetPerturbedColumnsF90(i,array,ierr) 555edaa7c33SBarry Smith $ use the entries of array ... 556edaa7c33SBarry Smith $ call MatFDColoringRestorePerturbedColumnsF90(i,array,ierr) 557edaa7c33SBarry Smith 55849b058dcSBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView(), MatFDColoringApply() 55949b058dcSBarry Smith 56049b058dcSBarry Smith @*/ 561edaa7c33SBarry Smith PetscErrorCode MatFDColoringGetPerturbedColumns(MatFDColoring coloring,PetscInt *n,const PetscInt *cols[]) 56249b058dcSBarry Smith { 56349b058dcSBarry Smith PetscFunctionBegin; 56449b058dcSBarry Smith if (coloring->currentcolor >= 0) { 56549b058dcSBarry Smith *n = coloring->ncolumns[coloring->currentcolor]; 56649b058dcSBarry Smith *cols = coloring->columns[coloring->currentcolor]; 56749b058dcSBarry Smith } else { 56849b058dcSBarry Smith *n = 0; 56949b058dcSBarry Smith } 57049b058dcSBarry Smith PetscFunctionReturn(0); 57149b058dcSBarry Smith } 57249b058dcSBarry Smith 57343a90d84SBarry Smith /*@ 574e0907662SLois Curfman McInnes MatFDColoringApply - Given a matrix for which a MatFDColoring context 575e0907662SLois Curfman McInnes has been created, computes the Jacobian for a function via finite differences. 57643a90d84SBarry Smith 577fee21e36SBarry Smith Collective on MatFDColoring 578fee21e36SBarry Smith 579ef5ee4d1SLois Curfman McInnes Input Parameters: 580ef5ee4d1SLois Curfman McInnes + mat - location to store Jacobian 581ef5ee4d1SLois Curfman McInnes . coloring - coloring context created with MatFDColoringCreate() 582ef5ee4d1SLois Curfman McInnes . x1 - location at which Jacobian is to be computed 5837850c7c0SBarry Smith - sctx - context required by function, if this is being used with the SNES solver then it is SNES object, otherwise it is null 584ef5ee4d1SLois Curfman McInnes 5858bba8e72SBarry Smith Options Database Keys: 586ebd3b9afSBarry Smith + -mat_fd_type - "wp" or "ds" (see MATMFFD_WP or MATMFFD_DS) 587b9722508SLois Curfman McInnes . -mat_fd_coloring_view - Activates basic viewing or coloring 588e350db43SBarry Smith . -mat_fd_coloring_view draw - Activates drawing of coloring 589e350db43SBarry Smith - -mat_fd_coloring_view ::ascii_info - Activates viewing of coloring info 5908bba8e72SBarry Smith 59115091d37SBarry Smith Level: intermediate 59298d79826SSatish Balay 5936a9b8d82SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView(), MatFDColoringSetFunction(), MatFDColoringSetValues() 59443a90d84SBarry Smith 59543a90d84SBarry Smith @*/ 596d1e9a80fSBarry Smith PetscErrorCode MatFDColoringApply(Mat J,MatFDColoring coloring,Vec x1,void *sctx) 59743a90d84SBarry Smith { 598bdaf1daeSBarry Smith PetscBool eq; 5993acb8795SBarry Smith 6003acb8795SBarry Smith PetscFunctionBegin; 6010700a824SBarry Smith PetscValidHeaderSpecific(J,MAT_CLASSID,1); 6020700a824SBarry Smith PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2); 6030700a824SBarry Smith PetscValidHeaderSpecific(x1,VEC_CLASSID,3); 6045f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectCompareId((PetscObject)J,coloring->matid,&eq)); 605*28b400f6SJacob Faibussowitsch PetscCheck(eq,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_WRONG,"Matrix used with MatFDColoringApply() must be that used with MatFDColoringCreate()"); 606*28b400f6SJacob Faibussowitsch PetscCheck(coloring->f,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_WRONGSTATE,"Must call MatFDColoringSetFunction()"); 607*28b400f6SJacob Faibussowitsch PetscCheck(J->ops->fdcoloringapply,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)J)->type_name); 608*28b400f6SJacob Faibussowitsch PetscCheck(coloring->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatFDColoringSetUp()"); 609684f2004SHong Zhang 6105f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetUnfactored(J)); 6115f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventBegin(MAT_FDColoringApply,coloring,J,x1,0)); 6125f80ce2aSJacob Faibussowitsch CHKERRQ((*J->ops->fdcoloringapply)(J,coloring,x1,sctx)); 6135f80ce2aSJacob Faibussowitsch CHKERRQ(PetscLogEventEnd(MAT_FDColoringApply,coloring,J,x1,0)); 6145bdb020cSBarry Smith if (!coloring->viewed) { 6155f80ce2aSJacob Faibussowitsch CHKERRQ(MatFDColoringViewFromOptions(coloring,NULL,"-mat_fd_coloring_view")); 6165bdb020cSBarry Smith coloring->viewed = PETSC_TRUE; 6175bdb020cSBarry Smith } 6183acb8795SBarry Smith PetscFunctionReturn(0); 6193acb8795SBarry Smith } 620