181e6777dSBarry Smith 27e9d5209SBarry Smith #include "src/mat/matimpl.h" 3325e03aeSBarry Smith #include "src/snes/mf/snesmfj.h" /*I "petscsnes.h" I*/ 481e6777dSBarry Smith 5b0a32e0cSBarry Smith PetscFList MatSNESMPetscFList = 0; 64c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE; 7a4d4d686SBarry Smith 84a2ae208SSatish Balay #undef __FUNCT__ 94a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetType" 10fd4bdd07SBarry Smith /*@C 1165f2ba5bSLois Curfman McInnes MatSNESMFSetType - Sets the method that is used to compute the 12b0a32e0cSBarry Smith differencing parameter for finite differene matrix-free formulations. 139a6cb015SBarry Smith 149a6cb015SBarry Smith Input Parameters: 157e9d5209SBarry Smith + mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMF() 167e9d5209SBarry Smith or MatSetType(mat,MATMFFD); 179a6cb015SBarry Smith - ftype - the type requested 189a6cb015SBarry Smith 1915091d37SBarry Smith Level: advanced 2015091d37SBarry Smith 2165f2ba5bSLois Curfman McInnes Notes: 2265f2ba5bSLois Curfman McInnes For example, such routines can compute h for use in 2365f2ba5bSLois Curfman McInnes Jacobian-vector products of the form 2465f2ba5bSLois Curfman McInnes 2565f2ba5bSLois Curfman McInnes F(x+ha) - F(x) 26ef4ad1fdSLois Curfman McInnes F'(u)a ~= ---------------- 2765f2ba5bSLois Curfman McInnes h 2865f2ba5bSLois Curfman McInnes 29f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic) 309a6cb015SBarry Smith @*/ 31dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetType(Mat mat,const MatSNESMFType ftype) 32b9fa9cd0SBarry Smith { 33dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(MatSNESMFCtx); 347e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 356831982aSBarry Smith PetscTruth match; 36a4d4d686SBarry Smith 37a4d4d686SBarry Smith PetscFunctionBegin; 384482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 394482741eSBarry Smith PetscValidCharPointer(ftype,2); 400f5bd95cSBarry Smith 419a6cb015SBarry Smith /* already set, so just return */ 426831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr); 430f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 44a4d4d686SBarry Smith 459a6cb015SBarry Smith /* destroy the old one if it exists */ 469a6cb015SBarry Smith if (ctx->ops->destroy) { 479a6cb015SBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 489a6cb015SBarry Smith } 499a6cb015SBarry Smith 5065f2ba5bSLois Curfman McInnes /* Get the function pointers for the requrested method */ 515a655dc6SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 52b9617806SBarry Smith ierr = PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr); 53958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatSNESMF type %s given",ftype); 549a6cb015SBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 556831982aSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr); 569a6cb015SBarry Smith PetscFunctionReturn(0); 579a6cb015SBarry Smith } 589a6cb015SBarry Smith 59*6849ba73SBarry Smith typedef PetscErrorCode (*FCN1)(Vec,void*); /* force argument to next function to not be extern C*/ 60c5c390f1SBarry Smith EXTERN_C_BEGIN 6187828ca2SBarry Smith #undef __FUNCT__ 6287828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD" 63dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctioniBase_FD(Mat mat,FCN1 func) 6487828ca2SBarry Smith { 6587828ca2SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 6687828ca2SBarry Smith 6787828ca2SBarry Smith PetscFunctionBegin; 6887828ca2SBarry Smith ctx->funcisetbase = func; 6987828ca2SBarry Smith PetscFunctionReturn(0); 7087828ca2SBarry Smith } 71c5c390f1SBarry Smith EXTERN_C_END 7287828ca2SBarry Smith 73*6849ba73SBarry Smith typedef PetscErrorCode (*FCN2)(int,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/ 74c5c390f1SBarry Smith EXTERN_C_BEGIN 7587828ca2SBarry Smith #undef __FUNCT__ 7687828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD" 77dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctioni_FD(Mat mat,FCN2 funci) 7887828ca2SBarry Smith { 7987828ca2SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 8087828ca2SBarry Smith 8187828ca2SBarry Smith PetscFunctionBegin; 8287828ca2SBarry Smith ctx->funci = funci; 8387828ca2SBarry Smith PetscFunctionReturn(0); 8487828ca2SBarry Smith } 85c5c390f1SBarry Smith EXTERN_C_END 8687828ca2SBarry Smith 879a6cb015SBarry Smith 884a2ae208SSatish Balay #undef __FUNCT__ 894a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister" 90*6849ba73SBarry Smith PetscErrorCode MatSNESMFRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatSNESMFCtx)) 919a6cb015SBarry Smith { 92dfbe8321SBarry Smith PetscErrorCode ierr; 93e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 949a6cb015SBarry Smith 959a6cb015SBarry Smith PetscFunctionBegin; 96b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 97c134de8dSSatish Balay ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 989a6cb015SBarry Smith PetscFunctionReturn(0); 999a6cb015SBarry Smith } 1009a6cb015SBarry Smith 1019a6cb015SBarry Smith 1024a2ae208SSatish Balay #undef __FUNCT__ 1034a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy" 1049a6cb015SBarry Smith /*@C 1055a655dc6SBarry Smith MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were 106f1af5d2fSBarry Smith registered by MatSNESMFRegisterDynamic). 1079a6cb015SBarry Smith 1089a6cb015SBarry Smith Not Collective 1099a6cb015SBarry Smith 11015091d37SBarry Smith Level: developer 11115091d37SBarry Smith 1125a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy 1139a6cb015SBarry Smith 114f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll() 1159a6cb015SBarry Smith @*/ 116dfbe8321SBarry Smith PetscErrorCode MatSNESMFRegisterDestroy(void) 1179a6cb015SBarry Smith { 118dfbe8321SBarry Smith PetscErrorCode ierr; 1199a6cb015SBarry Smith 1209a6cb015SBarry Smith PetscFunctionBegin; 121b0a32e0cSBarry Smith if (MatSNESMPetscFList) { 122b0a32e0cSBarry Smith ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr); 123b0a32e0cSBarry Smith MatSNESMPetscFList = 0; 1249a6cb015SBarry Smith } 1254c49b128SBarry Smith MatSNESMFRegisterAllCalled = PETSC_FALSE; 1269a6cb015SBarry Smith PetscFunctionReturn(0); 1279a6cb015SBarry Smith } 1289a6cb015SBarry Smith 1299a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/ 1304a2ae208SSatish Balay #undef __FUNCT__ 1318a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD" 132dfbe8321SBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat) 133a4d4d686SBarry Smith { 134dfbe8321SBarry Smith PetscErrorCode ierr; 1357e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 136fae171e0SBarry Smith 1373a40ed3dSBarry Smith PetscFunctionBegin; 138ba6a83e5SMatthew Knepley if (ctx->w != PETSC_NULL) { 139b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 140ba6a83e5SMatthew Knepley } 1419a6cb015SBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 14274637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 1436831982aSBarry Smith PetscHeaderDestroy(ctx); 1443a40ed3dSBarry Smith PetscFunctionReturn(0); 145b9fa9cd0SBarry Smith } 14650361f65SLois Curfman McInnes 1474a2ae208SSatish Balay #undef __FUNCT__ 1488a124369SBarry Smith #define __FUNCT__ "MatView_MFFD" 14939e2f89bSBarry Smith /* 1508a124369SBarry Smith MatSNESMFView_MFFD - Views matrix-free parameters. 1518f6e3e37SBarry Smith 15239e2f89bSBarry Smith */ 153dfbe8321SBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer) 154eb9086c3SLois Curfman McInnes { 155dfbe8321SBarry Smith PetscErrorCode ierr; 1567e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 15732077d6dSBarry Smith PetscTruth iascii; 158eb9086c3SLois Curfman McInnes 1593a40ed3dSBarry Smith PetscFunctionBegin; 16032077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 16132077d6dSBarry Smith if (iascii) { 162b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," SNES matrix-free approximation:\n");CHKERRQ(ierr); 163b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 164473c83c3SBarry Smith if (!ctx->type_name) { 165b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," The compute h routine has not yet been set\n");CHKERRQ(ierr); 166473c83c3SBarry Smith } else { 167b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr); 168473c83c3SBarry Smith } 1699a6cb015SBarry Smith if (ctx->ops->view) { 1709a6cb015SBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 1719a6cb015SBarry Smith } 1725cd90555SBarry Smith } else { 17329bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name); 174eb9086c3SLois Curfman McInnes } 1753a40ed3dSBarry Smith PetscFunctionReturn(0); 176eb9086c3SLois Curfman McInnes } 177eb9086c3SLois Curfman McInnes 1784a2ae208SSatish Balay #undef __FUNCT__ 1798a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD" 180be726c96SBarry Smith /* 1815a655dc6SBarry Smith MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This 18265f2ba5bSLois Curfman McInnes allows the user to indicate the beginning of a new linear solve by calling 183be726c96SBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 18465f2ba5bSLois Curfman McInnes MatSNESMFCreate_WP() to properly compute ||U|| only the first time 18565f2ba5bSLois Curfman McInnes in the linear solver rather than every time. 186be726c96SBarry Smith */ 187dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt) 188be726c96SBarry Smith { 189dfbe8321SBarry Smith PetscErrorCode ierr; 1907e9d5209SBarry Smith MatSNESMFCtx j = (MatSNESMFCtx)J->data; 191be726c96SBarry Smith 192be726c96SBarry Smith PetscFunctionBegin; 1935a655dc6SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 194b0a32e0cSBarry Smith if (j->usesnes) { 1951d1367b7SBarry Smith ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr); 1961d1367b7SBarry Smith ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 197958c9bccSBarry Smith if (!j->w) { 1982740c1caSMatthew Knepley ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr); 1992740c1caSMatthew Knepley } 2001d1367b7SBarry Smith } 201c5c390f1SBarry Smith j->vshift = 0.0; 202c5c390f1SBarry Smith j->vscale = 1.0; 203be726c96SBarry Smith PetscFunctionReturn(0); 204be726c96SBarry Smith } 205be726c96SBarry Smith 2064a2ae208SSatish Balay #undef __FUNCT__ 2078a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD" 208eb9086c3SLois Curfman McInnes /* 209adb62b0dSMatthew Knepley MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a: 210a4d4d686SBarry Smith 2119a6cb015SBarry Smith y ~= (F(u + ha) - F(u))/h, 212eb9086c3SLois Curfman McInnes where F = nonlinear function, as set by SNESSetFunction() 213eb9086c3SLois Curfman McInnes u = current iterate 214eb9086c3SLois Curfman McInnes h = difference interval 215eb9086c3SLois Curfman McInnes */ 216dfbe8321SBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y) 21739e2f89bSBarry Smith { 2187e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 219fae171e0SBarry Smith SNES snes; 220ea709b57SSatish Balay PetscScalar h,mone = -1.0; 221fae171e0SBarry Smith Vec w,U,F; 222dfbe8321SBarry Smith PetscErrorCode ierr,(*eval_fct)(SNES,Vec,Vec)=0; 22339e2f89bSBarry Smith 2243a40ed3dSBarry Smith PetscFunctionBegin; 2259a6cb015SBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 2269a6cb015SBarry Smith separate the performance monitoring from the cases that use conventional 2279a6cb015SBarry Smith storage. We may eventually modify event logging to associate events 2289a6cb015SBarry Smith with particular objects, hence alleviating the more general problem. */ 229d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr); 23056cd22aeSBarry Smith 231fae171e0SBarry Smith snes = ctx->snes; 232fae171e0SBarry Smith w = ctx->w; 2331d1367b7SBarry Smith U = ctx->current_u; 23450361f65SLois Curfman McInnes 23585614651SBarry Smith /* 23685614651SBarry Smith Compute differencing parameter 23785614651SBarry Smith */ 2389a6cb015SBarry Smith if (!ctx->ops->compute) { 2392f859189SBarry Smith ierr = MatSNESMFSetType(mat,MATSNESMF_WP);CHKERRQ(ierr); 2405a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr); 2419a6cb015SBarry Smith } 2429a6cb015SBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr); 243a4d4d686SBarry Smith 2445b7f0c42SBarry Smith if (ctx->checkh) { 2455b7f0c42SBarry Smith ierr = (*ctx->checkh)(U,a,&h,ctx->checkhctx);CHKERRQ(ierr); 2465b7f0c42SBarry Smith } 2475b7f0c42SBarry Smith 248a4d4d686SBarry Smith /* keep a record of the current differencing parameter h */ 249a4d4d686SBarry Smith ctx->currenth = h; 250aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 2518a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h)); 252a4d4d686SBarry Smith #else 2538a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h); 254a4d4d686SBarry Smith #endif 255a4d4d686SBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 25685614651SBarry Smith ctx->historyh[ctx->ncurrenth] = h; 257a4d4d686SBarry Smith } 25885614651SBarry Smith ctx->ncurrenth++; 259a4d4d686SBarry Smith 26085614651SBarry Smith /* w = u + ha */ 261a4d4d686SBarry Smith ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr); 26285614651SBarry Smith 263b0a32e0cSBarry Smith if (ctx->usesnes) { 26485614651SBarry Smith eval_fct = SNESComputeFunction; 2651d1367b7SBarry Smith F = ctx->current_f; 26629bbc08cSBarry Smith if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices"); 26739903ad8SBarry Smith ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr); 26885614651SBarry Smith } else { 26985614651SBarry Smith F = ctx->funcvec; 27085614651SBarry Smith /* compute func(U) as base for differencing */ 27185614651SBarry Smith if (ctx->ncurrenth == 1) { 27285614651SBarry Smith ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr); 27385614651SBarry Smith } 27485614651SBarry Smith ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr); 27585614651SBarry Smith } 276a4d4d686SBarry Smith 277a4d4d686SBarry Smith ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr); 278a4d4d686SBarry Smith h = 1.0/h; 279a4d4d686SBarry Smith ierr = VecScale(&h,y);CHKERRQ(ierr); 280c5c390f1SBarry Smith 281c5c390f1SBarry Smith ierr = VecAXPBY(&ctx->vshift,&ctx->vscale,a,y);CHKERRQ(ierr); 282c5c390f1SBarry Smith 28374637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 284a4d4d686SBarry Smith 285d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr); 286a4d4d686SBarry Smith PetscFunctionReturn(0); 287a4d4d686SBarry Smith } 288a4d4d686SBarry Smith 2894a2ae208SSatish Balay #undef __FUNCT__ 2908a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD" 291cf57b110SBarry Smith /* 2928a124369SBarry Smith MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix 293cf57b110SBarry Smith 294cf57b110SBarry Smith y ~= (F(u + ha) - F(u))/h, 295cf57b110SBarry Smith where F = nonlinear function, as set by SNESSetFunction() 296cf57b110SBarry Smith u = current iterate 297cf57b110SBarry Smith h = difference interval 298cf57b110SBarry Smith */ 299dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a) 300cf57b110SBarry Smith { 3017e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 302ea709b57SSatish Balay PetscScalar h,*aa,*ww,v; 30377d8c4bbSBarry Smith PetscReal epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON; 30465df01d8SBarry Smith Vec w,U; 305*6849ba73SBarry Smith PetscErrorCode ierr; 306*6849ba73SBarry Smith int i,rstart,rend; 307cf57b110SBarry Smith 308cf57b110SBarry Smith PetscFunctionBegin; 309cf57b110SBarry Smith if (!ctx->funci) { 310cf57b110SBarry Smith SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first"); 311cf57b110SBarry Smith } 312cf57b110SBarry Smith 313cf57b110SBarry Smith w = ctx->w; 314cf57b110SBarry Smith U = ctx->current_u; 315cf57b110SBarry Smith ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr); 316cf57b110SBarry Smith ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr); 317cf57b110SBarry Smith ierr = VecCopy(U,w);CHKERRQ(ierr); 318cf57b110SBarry Smith 319cf57b110SBarry Smith ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr); 320cf57b110SBarry Smith ierr = VecGetArray(a,&aa);CHKERRQ(ierr); 321cf57b110SBarry Smith for (i=rstart; i<rend; i++) { 322cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 323cf57b110SBarry Smith h = ww[i-rstart]; 324cf57b110SBarry Smith if (h == 0.0) h = 1.0; 325cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX) 326cf57b110SBarry Smith if (h < umin && h >= 0.0) h = umin; 327cf57b110SBarry Smith else if (h < 0.0 && h > -umin) h = -umin; 328cf57b110SBarry Smith #else 329cf57b110SBarry Smith if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin; 330cf57b110SBarry Smith else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin; 331cf57b110SBarry Smith #endif 332cf57b110SBarry Smith h *= epsilon; 333cf57b110SBarry Smith 334cf57b110SBarry Smith ww[i-rstart] += h; 335cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 336cf57b110SBarry Smith ierr = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr); 337cf57b110SBarry Smith aa[i-rstart] = (v - aa[i-rstart])/h; 338c5c390f1SBarry Smith 339c5c390f1SBarry Smith /* possibly shift and scale result */ 340c5c390f1SBarry Smith aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart]; 341c5c390f1SBarry Smith 342cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 343cf57b110SBarry Smith ww[i-rstart] -= h; 344cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 345cf57b110SBarry Smith } 346cf57b110SBarry Smith ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr); 347cf57b110SBarry Smith PetscFunctionReturn(0); 348cf57b110SBarry Smith } 349cf57b110SBarry Smith 350cf57b110SBarry Smith #undef __FUNCT__ 351c5c390f1SBarry Smith #define __FUNCT__ "MatShift_MFFD" 352dfbe8321SBarry Smith PetscErrorCode MatShift_MFFD(const PetscScalar *a,Mat Y) 353c5c390f1SBarry Smith { 354c5c390f1SBarry Smith MatSNESMFCtx shell = (MatSNESMFCtx)Y->data; 355c5c390f1SBarry Smith PetscFunctionBegin; 356c5c390f1SBarry Smith shell->vshift += *a; 357c5c390f1SBarry Smith PetscFunctionReturn(0); 358c5c390f1SBarry Smith } 359c5c390f1SBarry Smith 360c5c390f1SBarry Smith #undef __FUNCT__ 361c5c390f1SBarry Smith #define __FUNCT__ "MatScale_MFFD" 362dfbe8321SBarry Smith PetscErrorCode MatScale_MFFD(const PetscScalar *a,Mat Y) 363c5c390f1SBarry Smith { 364c5c390f1SBarry Smith MatSNESMFCtx shell = (MatSNESMFCtx)Y->data; 365c5c390f1SBarry Smith PetscFunctionBegin; 366c5c390f1SBarry Smith shell->vscale *= *a; 367c5c390f1SBarry Smith PetscFunctionReturn(0); 368c5c390f1SBarry Smith } 369c5c390f1SBarry Smith 370c5c390f1SBarry Smith 371c5c390f1SBarry Smith #undef __FUNCT__ 3724a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF" 373a4d4d686SBarry Smith /*@C 37465f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 37565f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 37665f2ba5bSLois Curfman McInnes the routine SNESSetJacobian(). 377a4d4d686SBarry Smith 378a4d4d686SBarry Smith Collective on SNES and Vec 379a4d4d686SBarry Smith 380a4d4d686SBarry Smith Input Parameters: 381a4d4d686SBarry Smith + snes - the SNES context 382a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 383a4d4d686SBarry Smith 384a4d4d686SBarry Smith Output Parameter: 385a4d4d686SBarry Smith . J - the matrix-free matrix 386a4d4d686SBarry Smith 38715091d37SBarry Smith Level: advanced 38815091d37SBarry Smith 389a4d4d686SBarry Smith Notes: 390a4d4d686SBarry Smith The matrix-free matrix context merely contains the function pointers 391a4d4d686SBarry Smith and work space for performing finite difference approximations of 39265f2ba5bSLois Curfman McInnes Jacobian-vector products, F'(u)*a, 3939a6cb015SBarry Smith 3949a6cb015SBarry Smith The default code uses the following approach to compute h 395a4d4d686SBarry Smith 396a4d4d686SBarry Smith .vb 39765f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 398a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 399a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 400a4d4d686SBarry Smith where 401a4d4d686SBarry Smith error_rel = square root of relative error in function evaluation 402a4d4d686SBarry Smith umin = minimum iterate parameter 403a4d4d686SBarry Smith .ve 404a4d4d686SBarry Smith 4055a655dc6SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 40665f2ba5bSLois Curfman McInnes umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 40765f2ba5bSLois Curfman McInnes of the users manual for details. 408a4d4d686SBarry Smith 409a4d4d686SBarry Smith The user should call MatDestroy() when finished with the matrix-free 410a4d4d686SBarry Smith matrix context. 411a4d4d686SBarry Smith 412a4d4d686SBarry Smith Options Database Keys: 413a4d4d686SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 4149a6cb015SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 415a4d4d686SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 416a4d4d686SBarry Smith 417a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix 418a4d4d686SBarry Smith 4195a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 4201d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(), 421fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian() 422a4d4d686SBarry Smith 423a4d4d686SBarry Smith @*/ 424dfbe8321SBarry Smith PetscErrorCode MatCreateSNESMF(SNES snes,Vec x,Mat *J) 425a4d4d686SBarry Smith { 4261d1367b7SBarry Smith MatSNESMFCtx mfctx; 427dfbe8321SBarry Smith PetscErrorCode ierr; 4281d1367b7SBarry Smith 4291d1367b7SBarry Smith PetscFunctionBegin; 4301d1367b7SBarry Smith ierr = MatCreateMF(x,J);CHKERRQ(ierr); 4317e9d5209SBarry Smith 4327e9d5209SBarry Smith mfctx = (MatSNESMFCtx)(*J)->data; 4331d1367b7SBarry Smith mfctx->snes = snes; 434b0a32e0cSBarry Smith mfctx->usesnes = PETSC_TRUE; 435b0a32e0cSBarry Smith PetscLogObjectParent(snes,*J); 4361d1367b7SBarry Smith PetscFunctionReturn(0); 4371d1367b7SBarry Smith } 4381d1367b7SBarry Smith 439cf3bea43SBarry Smith EXTERN_C_BEGIN 440cf3bea43SBarry Smith #undef __FUNCT__ 441cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD" 442dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetBase_FD(Mat J,Vec U) 443cf3bea43SBarry Smith { 444dfbe8321SBarry Smith PetscErrorCode ierr; 4457e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 446cf3bea43SBarry Smith 447cf3bea43SBarry Smith PetscFunctionBegin; 448cf3bea43SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 449cf3bea43SBarry Smith ctx->current_u = U; 450cf3bea43SBarry Smith ctx->usesnes = PETSC_FALSE; 451958c9bccSBarry Smith if (!ctx->w) { 452ba6a83e5SMatthew Knepley ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr); 453ba6a83e5SMatthew Knepley } 454cf3bea43SBarry Smith PetscFunctionReturn(0); 455cf3bea43SBarry Smith } 456cf3bea43SBarry Smith EXTERN_C_END 457cf3bea43SBarry Smith 458*6849ba73SBarry Smith typedef PetscErrorCode (*FCN3)(Vec,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/ 4595b7f0c42SBarry Smith EXTERN_C_BEGIN 4605b7f0c42SBarry Smith #undef __FUNCT__ 4615b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh_FD" 462dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetCheckh_FD(Mat J,FCN3 fun,void*ectx) 4635b7f0c42SBarry Smith { 4645b7f0c42SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 4655b7f0c42SBarry Smith 4665b7f0c42SBarry Smith PetscFunctionBegin; 4675b7f0c42SBarry Smith ctx->checkh = fun; 4685b7f0c42SBarry Smith ctx->checkhctx = ectx; 4695b7f0c42SBarry Smith PetscFunctionReturn(0); 4705b7f0c42SBarry Smith } 4715b7f0c42SBarry Smith EXTERN_C_END 4725b7f0c42SBarry Smith 4734a2ae208SSatish Balay #undef __FUNCT__ 4747e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions" 4757e9d5209SBarry Smith /*@ 4767e9d5209SBarry Smith MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line 4777e9d5209SBarry Smith parameter. 4787e9d5209SBarry Smith 4797e9d5209SBarry Smith Collective on Mat 4807e9d5209SBarry Smith 4817e9d5209SBarry Smith Input Parameters: 4827e9d5209SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 4837e9d5209SBarry Smith 4847e9d5209SBarry Smith Options Database Keys: 4857e9d5209SBarry Smith + -snes_mf_type - <default,wp> 4867e9d5209SBarry Smith - -snes_mf_err - square root of estimated relative error in function evaluation 4877e9d5209SBarry Smith - -snes_mf_period - how often h is recomputed, defaults to 1, everytime 4887e9d5209SBarry Smith 4897e9d5209SBarry Smith Level: advanced 4907e9d5209SBarry Smith 4917e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters 4927e9d5209SBarry Smith 4937e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4947e9d5209SBarry Smith MatSNESMFResetHHistory(), MatSNESMFKSPMonitor() 4957e9d5209SBarry Smith @*/ 496dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFromOptions(Mat mat) 4977e9d5209SBarry Smith { 4987e9d5209SBarry Smith MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data; 499dfbe8321SBarry Smith PetscErrorCode ierr; 5007e9d5209SBarry Smith PetscTruth flg; 5017e9d5209SBarry Smith char ftype[256]; 5027e9d5209SBarry Smith 5037e9d5209SBarry Smith PetscFunctionBegin; 5047e9d5209SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 5057e9d5209SBarry Smith 5067e9d5209SBarry Smith ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr); 5077e9d5209SBarry Smith ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr); 5087e9d5209SBarry Smith if (flg) { 5097e9d5209SBarry Smith ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr); 5107e9d5209SBarry Smith } 5117e9d5209SBarry Smith 51287828ca2SBarry Smith ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr); 5137e9d5209SBarry Smith ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr); 5147e9d5209SBarry Smith if (mfctx->snes) { 5157e9d5209SBarry Smith ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr); 5167e9d5209SBarry Smith if (flg) { 5177e9d5209SBarry Smith KSP ksp; 51894b7f48cSBarry Smith ierr = SNESGetKSP(mfctx->snes,&ksp);CHKERRQ(ierr); 5197e9d5209SBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 5207e9d5209SBarry Smith } 5217e9d5209SBarry Smith } 5225b7f0c42SBarry Smith ierr = PetscOptionsName("-snes_mf_check_positivity","Insure that U + h*a is nonnegative","MatSNESMFSetCheckh",&flg);CHKERRQ(ierr); 5235b7f0c42SBarry Smith if (flg) { 5245b7f0c42SBarry Smith ierr = MatSNESMFSetCheckh(mat,MatSNESMFCheckPositivity,0);CHKERRQ(ierr); 5255b7f0c42SBarry Smith } 5267e9d5209SBarry Smith if (mfctx->ops->setfromoptions) { 5277e9d5209SBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 5287e9d5209SBarry Smith } 5297e9d5209SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5307e9d5209SBarry Smith PetscFunctionReturn(0); 5317e9d5209SBarry Smith } 5327e9d5209SBarry Smith 5330bad9183SKris Buschelman /*MC 534fafad747SKris Buschelman MATMFFD - MATMFFD = "mffd" - A matrix free matrix type. 5350bad9183SKris Buschelman 5360bad9183SKris Buschelman Level: advanced 5370bad9183SKris Buschelman 5380bad9183SKris Buschelman .seealso: MatCreateMF, MatCreateSNESMF 5390bad9183SKris Buschelman M*/ 540fe93831dSBarry Smith EXTERN_C_BEGIN 5417e9d5209SBarry Smith #undef __FUNCT__ 5427e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD" 543dfbe8321SBarry Smith PetscErrorCode MatCreate_MFFD(Mat A) 5447e9d5209SBarry Smith { 5457e9d5209SBarry Smith MatSNESMFCtx mfctx; 546dfbe8321SBarry Smith PetscErrorCode ierr; 5477e9d5209SBarry Smith 5487e9d5209SBarry Smith PetscFunctionBegin; 5496e087cb5SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 5506e087cb5SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 5516e087cb5SMatthew Knepley #endif 5526e087cb5SMatthew Knepley 5538a124369SBarry Smith PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD); 5547e9d5209SBarry Smith PetscLogObjectCreate(mfctx); 5557e9d5209SBarry Smith mfctx->sp = 0; 5567e9d5209SBarry Smith mfctx->snes = 0; 55777d8c4bbSBarry Smith mfctx->error_rel = PETSC_SQRT_MACHINE_EPSILON; 5587e9d5209SBarry Smith mfctx->recomputeperiod = 1; 5597e9d5209SBarry Smith mfctx->count = 0; 5607e9d5209SBarry Smith mfctx->currenth = 0.0; 5617e9d5209SBarry Smith mfctx->historyh = PETSC_NULL; 5627e9d5209SBarry Smith mfctx->ncurrenth = 0; 5637e9d5209SBarry Smith mfctx->maxcurrenth = 0; 5647e9d5209SBarry Smith mfctx->type_name = 0; 5657e9d5209SBarry Smith mfctx->usesnes = PETSC_FALSE; 5667e9d5209SBarry Smith 567c5c390f1SBarry Smith mfctx->vshift = 0.0; 568c5c390f1SBarry Smith mfctx->vscale = 1.0; 569c5c390f1SBarry Smith 5707e9d5209SBarry Smith /* 5717e9d5209SBarry Smith Create the empty data structure to contain compute-h routines. 5727e9d5209SBarry Smith These will be filled in below from the command line options or 5737e9d5209SBarry Smith a later call with MatSNESMFSetType() or if that is not called 5748a124369SBarry Smith then it will default in the first use of MatMult_MFFD() 5757e9d5209SBarry Smith */ 5767e9d5209SBarry Smith mfctx->ops->compute = 0; 5777e9d5209SBarry Smith mfctx->ops->destroy = 0; 5787e9d5209SBarry Smith mfctx->ops->view = 0; 5797e9d5209SBarry Smith mfctx->ops->setfromoptions = 0; 5807e9d5209SBarry Smith mfctx->hctx = 0; 5817e9d5209SBarry Smith 5827e9d5209SBarry Smith mfctx->func = 0; 5837e9d5209SBarry Smith mfctx->funcctx = 0; 5847e9d5209SBarry Smith mfctx->funcvec = 0; 585ba6a83e5SMatthew Knepley mfctx->w = PETSC_NULL; 5867e9d5209SBarry Smith 58765df01d8SBarry Smith A->data = mfctx; 5887e9d5209SBarry Smith 5898a124369SBarry Smith A->ops->mult = MatMult_MFFD; 5908a124369SBarry Smith A->ops->destroy = MatDestroy_MFFD; 5918a124369SBarry Smith A->ops->view = MatView_MFFD; 5928a124369SBarry Smith A->ops->assemblyend = MatAssemblyEnd_MFFD; 5938a124369SBarry Smith A->ops->getdiagonal = MatGetDiagonal_MFFD; 594c5c390f1SBarry Smith A->ops->scale = MatScale_MFFD; 595c5c390f1SBarry Smith A->ops->shift = MatShift_MFFD; 59665df01d8SBarry Smith A->ops->setfromoptions = MatSNESMFSetFromOptions; 5977e9d5209SBarry Smith 59865df01d8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr); 599c5c390f1SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBase_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr); 60087828ca2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr); 6015b7f0c42SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetCheckh_C","MatSNESMFSetCheckh_FD",MatSNESMFSetCheckh_FD);CHKERRQ(ierr); 60265df01d8SBarry Smith mfctx->mat = A; 6037e9d5209SBarry Smith 6047e9d5209SBarry Smith PetscFunctionReturn(0); 6057e9d5209SBarry Smith } 606fe93831dSBarry Smith EXTERN_C_END 6077e9d5209SBarry Smith 6087e9d5209SBarry Smith #undef __FUNCT__ 6094a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF" 6101d1367b7SBarry Smith /*@C 6111d1367b7SBarry Smith MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF() 6121d1367b7SBarry Smith 6131d1367b7SBarry Smith Collective on Vec 6141d1367b7SBarry Smith 6151d1367b7SBarry Smith Input Parameters: 6161d1367b7SBarry Smith . x - vector that defines layout of the vectors and matrices 6171d1367b7SBarry Smith 6181d1367b7SBarry Smith Output Parameter: 6191d1367b7SBarry Smith . J - the matrix-free matrix 6201d1367b7SBarry Smith 6211d1367b7SBarry Smith Level: advanced 6221d1367b7SBarry Smith 6231d1367b7SBarry Smith Notes: 6241d1367b7SBarry Smith The matrix-free matrix context merely contains the function pointers 6251d1367b7SBarry Smith and work space for performing finite difference approximations of 6261d1367b7SBarry Smith Jacobian-vector products, F'(u)*a, 6271d1367b7SBarry Smith 6281d1367b7SBarry Smith The default code uses the following approach to compute h 6291d1367b7SBarry Smith 6301d1367b7SBarry Smith .vb 6311d1367b7SBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 6321d1367b7SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 6331d1367b7SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 6341d1367b7SBarry Smith where 6351d1367b7SBarry Smith error_rel = square root of relative error in function evaluation 6361d1367b7SBarry Smith umin = minimum iterate parameter 6371d1367b7SBarry Smith .ve 6381d1367b7SBarry Smith 6391d1367b7SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 6401d1367b7SBarry Smith umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 6411d1367b7SBarry Smith of the users manual for details. 6421d1367b7SBarry Smith 6431d1367b7SBarry Smith The user should call MatDestroy() when finished with the matrix-free 6441d1367b7SBarry Smith matrix context. 6451d1367b7SBarry Smith 6461d1367b7SBarry Smith Options Database Keys: 6471d1367b7SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 6481d1367b7SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 6495b7f0c42SBarry Smith . -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 6505b7f0c42SBarry Smith - -snes_mf_check_positivity 6511d1367b7SBarry Smith 6521d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix 6531d1367b7SBarry Smith 6541d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 6551d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(), 656fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian() 6571d1367b7SBarry Smith 6581d1367b7SBarry Smith @*/ 659dfbe8321SBarry Smith PetscErrorCode MatCreateMF(Vec x,Mat *J) 6601d1367b7SBarry Smith { 661a4d4d686SBarry Smith MPI_Comm comm; 662*6849ba73SBarry Smith PetscErrorCode ierr; 663*6849ba73SBarry Smith int n,nloc; 664a4d4d686SBarry Smith 665a4d4d686SBarry Smith PetscFunctionBegin; 6661d1367b7SBarry Smith ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 66765df01d8SBarry Smith ierr = VecGetSize(x,&n);CHKERRQ(ierr); 66865df01d8SBarry Smith ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr); 6697e9d5209SBarry Smith ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr); 670e56c5435SBarry Smith ierr = MatRegisterDynamic(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr); 67165df01d8SBarry Smith ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr); 6729a6cb015SBarry Smith PetscFunctionReturn(0); 6739a6cb015SBarry Smith } 6749a6cb015SBarry Smith 675a4d4d686SBarry Smith 6764a2ae208SSatish Balay #undef __FUNCT__ 6774a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH" 678a4d4d686SBarry Smith /*@ 67965f2ba5bSLois Curfman McInnes MatSNESMFGetH - Gets the last value that was used as the differencing 680a4d4d686SBarry Smith parameter. 681a4d4d686SBarry Smith 682a4d4d686SBarry Smith Not Collective 683a4d4d686SBarry Smith 684a4d4d686SBarry Smith Input Parameters: 6855a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 686a4d4d686SBarry Smith 687a4d4d686SBarry Smith Output Paramter: 688a4d4d686SBarry Smith . h - the differencing step size 689a4d4d686SBarry Smith 69015091d37SBarry Smith Level: advanced 69115091d37SBarry Smith 692a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 693a4d4d686SBarry Smith 6945a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 6955a655dc6SBarry Smith MatSNESMFResetHHistory(),MatSNESMFKSPMonitor() 696a4d4d686SBarry Smith @*/ 697dfbe8321SBarry Smith PetscErrorCode MatSNESMFGetH(Mat mat,PetscScalar *h) 698a4d4d686SBarry Smith { 6997e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 700a4d4d686SBarry Smith 701a4d4d686SBarry Smith PetscFunctionBegin; 702a4d4d686SBarry Smith *h = ctx->currenth; 703a4d4d686SBarry Smith PetscFunctionReturn(0); 704a4d4d686SBarry Smith } 705a4d4d686SBarry Smith 7064a2ae208SSatish Balay #undef __FUNCT__ 7074a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor" 708a4d4d686SBarry Smith /* 7095a655dc6SBarry Smith MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc 71065f2ba5bSLois Curfman McInnes SNES matrix free routines. Prints the differencing parameter used at 71165f2ba5bSLois Curfman McInnes each step. 712a4d4d686SBarry Smith */ 713dfbe8321SBarry Smith PetscErrorCode MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy) 714a4d4d686SBarry Smith { 715a4d4d686SBarry Smith PC pc; 7165a655dc6SBarry Smith MatSNESMFCtx ctx; 717dfbe8321SBarry Smith PetscErrorCode ierr; 718a4d4d686SBarry Smith Mat mat; 719a4d4d686SBarry Smith MPI_Comm comm; 720a4d4d686SBarry Smith PetscTruth nonzeroinitialguess; 721a4d4d686SBarry Smith 722a4d4d686SBarry Smith PetscFunctionBegin; 723a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 724a4d4d686SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 725a4d4d686SBarry Smith ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr); 726a4d4d686SBarry Smith ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7277e9d5209SBarry Smith ctx = (MatSNESMFCtx)mat->data; 7287e9d5209SBarry Smith 729a4d4d686SBarry Smith if (n > 0 || nonzeroinitialguess) { 730aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 731d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm, 732329f5518SBarry Smith PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr); 733a4d4d686SBarry Smith #else 734d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr); 735a4d4d686SBarry Smith #endif 736a4d4d686SBarry Smith } else { 737d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr); 738a4d4d686SBarry Smith } 739a4d4d686SBarry Smith PetscFunctionReturn(0); 740a4d4d686SBarry Smith } 741a4d4d686SBarry Smith 7424a2ae208SSatish Balay #undef __FUNCT__ 7434a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction" 74485614651SBarry Smith /*@C 74585614651SBarry Smith MatSNESMFSetFunction - Sets the function used in applying the matrix free. 74685614651SBarry Smith 74785614651SBarry Smith Collective on Mat 74885614651SBarry Smith 74985614651SBarry Smith Input Parameters: 75085614651SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 75185614651SBarry Smith . v - workspace vector 75285614651SBarry Smith . func - the function to use 75385614651SBarry Smith - funcctx - optional function context passed to function 75485614651SBarry Smith 75585614651SBarry Smith Level: advanced 75685614651SBarry Smith 75785614651SBarry Smith Notes: 75885614651SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 75985614651SBarry Smith matrix inside your compute Jacobian routine 76085614651SBarry Smith 76185614651SBarry Smith If this is not set then it will use the function set with SNESSetFunction() 76285614651SBarry Smith 76385614651SBarry Smith .keywords: SNES, matrix-free, function 76485614651SBarry Smith 76585614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 76685614651SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 76785614651SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 76885614651SBarry Smith @*/ 769*6849ba73SBarry Smith PetscErrorCode MatSNESMFSetFunction(Mat mat,Vec v,PetscErrorCode (*func)(SNES,Vec,Vec,void *),void *funcctx) 77085614651SBarry Smith { 7717e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 77285614651SBarry Smith 77385614651SBarry Smith PetscFunctionBegin; 77485614651SBarry Smith ctx->func = func; 77585614651SBarry Smith ctx->funcctx = funcctx; 77685614651SBarry Smith ctx->funcvec = v; 77785614651SBarry Smith PetscFunctionReturn(0); 77885614651SBarry Smith } 77985614651SBarry Smith 780cf57b110SBarry Smith #undef __FUNCT__ 781cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni" 782cf57b110SBarry Smith /*@C 783cf57b110SBarry Smith MatSNESMFSetFunctioni - Sets the function for a single component 784cf57b110SBarry Smith 785cf57b110SBarry Smith Collective on Mat 786cf57b110SBarry Smith 787cf57b110SBarry Smith Input Parameters: 788cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 789cf57b110SBarry Smith - funci - the function to use 790cf57b110SBarry Smith 791cf57b110SBarry Smith Level: advanced 792cf57b110SBarry Smith 793cf57b110SBarry Smith Notes: 794cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 795cf57b110SBarry Smith matrix inside your compute Jacobian routine 796cf57b110SBarry Smith 797cf57b110SBarry Smith 798cf57b110SBarry Smith .keywords: SNES, matrix-free, function 799cf57b110SBarry Smith 800cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 801cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 802cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 803cf57b110SBarry Smith @*/ 804*6849ba73SBarry Smith PetscErrorCode MatSNESMFSetFunctioni(Mat mat,PetscErrorCode (*funci)(int,Vec,PetscScalar*,void *)) 805cf57b110SBarry Smith { 806*6849ba73SBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(int,Vec,PetscScalar*,void *)); 807cf57b110SBarry Smith 808cf57b110SBarry Smith PetscFunctionBegin; 8094482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 810c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr); 81187828ca2SBarry Smith if (f) { 81287828ca2SBarry Smith ierr = (*f)(mat,funci);CHKERRQ(ierr); 81387828ca2SBarry Smith } 814cf57b110SBarry Smith PetscFunctionReturn(0); 815cf57b110SBarry Smith } 816cf57b110SBarry Smith 81787828ca2SBarry Smith 818cf57b110SBarry Smith #undef __FUNCT__ 819cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase" 820cf57b110SBarry Smith /*@C 821cf57b110SBarry Smith MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation 822cf57b110SBarry Smith 823cf57b110SBarry Smith Collective on Mat 824cf57b110SBarry Smith 825cf57b110SBarry Smith Input Parameters: 826cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 827cf57b110SBarry Smith - func - the function to use 828cf57b110SBarry Smith 829cf57b110SBarry Smith Level: advanced 830cf57b110SBarry Smith 831cf57b110SBarry Smith Notes: 832cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 833cf57b110SBarry Smith matrix inside your compute Jacobian routine 834cf57b110SBarry Smith 835cf57b110SBarry Smith 836cf57b110SBarry Smith .keywords: SNES, matrix-free, function 837cf57b110SBarry Smith 838cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 839cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 840cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 841cf57b110SBarry Smith @*/ 842*6849ba73SBarry Smith PetscErrorCode MatSNESMFSetFunctioniBase(Mat mat,PetscErrorCode (*func)(Vec,void *)) 843cf57b110SBarry Smith { 844*6849ba73SBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,void *)); 845cf57b110SBarry Smith 846cf57b110SBarry Smith PetscFunctionBegin; 8474482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 848c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr); 84987828ca2SBarry Smith if (f) { 85087828ca2SBarry Smith ierr = (*f)(mat,func);CHKERRQ(ierr); 85187828ca2SBarry Smith } 852cf57b110SBarry Smith PetscFunctionReturn(0); 853cf57b110SBarry Smith } 854cf57b110SBarry Smith 85585614651SBarry Smith 8564a2ae208SSatish Balay #undef __FUNCT__ 8574a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod" 858329f5518SBarry Smith /*@ 859329f5518SBarry Smith MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime 860329f5518SBarry Smith 861329f5518SBarry Smith Collective on Mat 862329f5518SBarry Smith 863329f5518SBarry Smith Input Parameters: 864329f5518SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 865329f5518SBarry Smith - period - 1 for everytime, 2 for every second etc 866329f5518SBarry Smith 867329f5518SBarry Smith Options Database Keys: 868329f5518SBarry Smith + -snes_mf_period <period> 869329f5518SBarry Smith 870329f5518SBarry Smith Level: advanced 871329f5518SBarry Smith 872329f5518SBarry Smith 873329f5518SBarry Smith .keywords: SNES, matrix-free, parameters 874329f5518SBarry Smith 875329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 876329f5518SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 877329f5518SBarry Smith MatSNESMFKSPMonitor() 878329f5518SBarry Smith @*/ 879dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetPeriod(Mat mat,int period) 880329f5518SBarry Smith { 8817e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 882329f5518SBarry Smith 883329f5518SBarry Smith PetscFunctionBegin; 884329f5518SBarry Smith ctx->recomputeperiod = period; 885329f5518SBarry Smith PetscFunctionReturn(0); 886329f5518SBarry Smith } 887329f5518SBarry Smith 8884a2ae208SSatish Balay #undef __FUNCT__ 8894a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError" 890a4d4d686SBarry Smith /*@ 8915a655dc6SBarry Smith MatSNESMFSetFunctionError - Sets the error_rel for the approximation of 892a4d4d686SBarry Smith matrix-vector products using finite differences. 893a4d4d686SBarry Smith 894a4d4d686SBarry Smith Collective on Mat 895a4d4d686SBarry Smith 896a4d4d686SBarry Smith Input Parameters: 8975a655dc6SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 8989a6cb015SBarry Smith - error_rel - relative error (should be set to the square root of 899a4d4d686SBarry Smith the relative error in the function evaluations) 900a4d4d686SBarry Smith 90115091d37SBarry Smith Options Database Keys: 90215091d37SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 90315091d37SBarry Smith 90415091d37SBarry Smith Level: advanced 90515091d37SBarry Smith 906a4d4d686SBarry Smith Notes: 907a4d4d686SBarry Smith The default matrix-free matrix-vector product routine computes 908a4d4d686SBarry Smith .vb 90965f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 910a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 911a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 912a4d4d686SBarry Smith .ve 913a4d4d686SBarry Smith 914a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 915a4d4d686SBarry Smith 9165a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 9175a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 9185a655dc6SBarry Smith MatSNESMFKSPMonitor() 919a4d4d686SBarry Smith @*/ 920dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctionError(Mat mat,PetscReal error) 921a4d4d686SBarry Smith { 9227e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 923a4d4d686SBarry Smith 924a4d4d686SBarry Smith PetscFunctionBegin; 925a4d4d686SBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 926a4d4d686SBarry Smith PetscFunctionReturn(0); 927a4d4d686SBarry Smith } 928a4d4d686SBarry Smith 9294a2ae208SSatish Balay #undef __FUNCT__ 9304a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace" 931a4d4d686SBarry Smith /*@ 93265f2ba5bSLois Curfman McInnes MatSNESMFAddNullSpace - Provides a null space that an operator is 93365f2ba5bSLois Curfman McInnes supposed to have. Since roundoff will create a small component in 93465f2ba5bSLois Curfman McInnes the null space, if you know the null space you may have it 93565f2ba5bSLois Curfman McInnes automatically removed. 936a4d4d686SBarry Smith 937a4d4d686SBarry Smith Collective on Mat 938a4d4d686SBarry Smith 939a4d4d686SBarry Smith Input Parameters: 940a4d4d686SBarry Smith + J - the matrix-free matrix context 94174637425SBarry Smith - nullsp - object created with MatNullSpaceCreate() 942a4d4d686SBarry Smith 94315091d37SBarry Smith Level: advanced 94415091d37SBarry Smith 945a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space 946a4d4d686SBarry Smith 94774637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(), 9485a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 9495a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFErrorRel() 950a4d4d686SBarry Smith @*/ 951dfbe8321SBarry Smith PetscErrorCode MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp) 952a4d4d686SBarry Smith { 953dfbe8321SBarry Smith PetscErrorCode ierr; 9547e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 955a4d4d686SBarry Smith MPI_Comm comm; 956a4d4d686SBarry Smith 957a4d4d686SBarry Smith PetscFunctionBegin; 9582d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 959a4d4d686SBarry Smith 96085614651SBarry Smith ctx->sp = nullsp; 96185614651SBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 962a4d4d686SBarry Smith PetscFunctionReturn(0); 963a4d4d686SBarry Smith } 964a4d4d686SBarry Smith 9654a2ae208SSatish Balay #undef __FUNCT__ 9664a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory" 967a4d4d686SBarry Smith /*@ 96865f2ba5bSLois Curfman McInnes MatSNESMFSetHHistory - Sets an array to collect a history of the 96965f2ba5bSLois Curfman McInnes differencing values (h) computed for the matrix-free product. 970a4d4d686SBarry Smith 971a4d4d686SBarry Smith Collective on Mat 972a4d4d686SBarry Smith 973a4d4d686SBarry Smith Input Parameters: 974a4d4d686SBarry Smith + J - the matrix-free matrix context 97565f2ba5bSLois Curfman McInnes . histroy - space to hold the history 97665f2ba5bSLois Curfman McInnes - nhistory - number of entries in history, if more entries are generated than 97765f2ba5bSLois Curfman McInnes nhistory, then the later ones are discarded 978a4d4d686SBarry Smith 97915091d37SBarry Smith Level: advanced 98015091d37SBarry Smith 981a4d4d686SBarry Smith Notes: 98265f2ba5bSLois Curfman McInnes Use MatSNESMFResetHHistory() to reset the history counter and collect 98365f2ba5bSLois Curfman McInnes a new batch of differencing parameters, h. 984a4d4d686SBarry Smith 985a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 986a4d4d686SBarry Smith 9875a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 9885a655dc6SBarry Smith MatSNESMFResetHHistory(), 9895a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 990a4d4d686SBarry Smith 991a4d4d686SBarry Smith @*/ 992dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetHHistory(Mat J,PetscScalar history[],int nhistory) 993a4d4d686SBarry Smith { 9947e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 995a4d4d686SBarry Smith 996a4d4d686SBarry Smith PetscFunctionBegin; 997a4d4d686SBarry Smith ctx->historyh = history; 998a4d4d686SBarry Smith ctx->maxcurrenth = nhistory; 999a4d4d686SBarry Smith ctx->currenth = 0; 1000a4d4d686SBarry Smith PetscFunctionReturn(0); 1001a4d4d686SBarry Smith } 1002a4d4d686SBarry Smith 10034a2ae208SSatish Balay #undef __FUNCT__ 10044a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory" 1005a4d4d686SBarry Smith /*@ 10065a655dc6SBarry Smith MatSNESMFResetHHistory - Resets the counter to zero to begin 1007a4d4d686SBarry Smith collecting a new set of differencing histories. 1008a4d4d686SBarry Smith 1009a4d4d686SBarry Smith Collective on Mat 1010a4d4d686SBarry Smith 1011a4d4d686SBarry Smith Input Parameters: 1012a4d4d686SBarry Smith . J - the matrix-free matrix context 1013a4d4d686SBarry Smith 101415091d37SBarry Smith Level: advanced 101515091d37SBarry Smith 1016a4d4d686SBarry Smith Notes: 101765f2ba5bSLois Curfman McInnes Use MatSNESMFSetHHistory() to create the original history counter. 1018a4d4d686SBarry Smith 1019a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 1020a4d4d686SBarry Smith 10215a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 10225a655dc6SBarry Smith MatSNESMFSetHHistory(), 10235a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 1024a4d4d686SBarry Smith 1025a4d4d686SBarry Smith @*/ 1026dfbe8321SBarry Smith PetscErrorCode MatSNESMFResetHHistory(Mat J) 1027a4d4d686SBarry Smith { 10287e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 1029a4d4d686SBarry Smith 1030a4d4d686SBarry Smith PetscFunctionBegin; 1031be726c96SBarry Smith ctx->ncurrenth = 0; 1032a4d4d686SBarry Smith PetscFunctionReturn(0); 1033a4d4d686SBarry Smith } 1034a4d4d686SBarry Smith 10354a2ae208SSatish Balay #undef __FUNCT__ 1036fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian" 1037dfbe8321SBarry Smith PetscErrorCode MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 10381d1367b7SBarry Smith { 1039dfbe8321SBarry Smith PetscErrorCode ierr; 10401d1367b7SBarry Smith PetscFunctionBegin; 10411d1367b7SBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10421d1367b7SBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10431d1367b7SBarry Smith PetscFunctionReturn(0); 10441d1367b7SBarry Smith } 10451d1367b7SBarry Smith 10464a2ae208SSatish Balay #undef __FUNCT__ 10474a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase" 10485b7f0c42SBarry Smith /*@ 10495b7f0c42SBarry Smith MatSNESMFSetBase - Sets the vector U at which matrix vector products of the 10505b7f0c42SBarry Smith Jacobian are computed 10515b7f0c42SBarry Smith 10525b7f0c42SBarry Smith Collective on Mat 10535b7f0c42SBarry Smith 10545b7f0c42SBarry Smith Input Parameters: 10555b7f0c42SBarry Smith + J - the MatSNESMF matrix 10565b7f0c42SBarry Smith - U - the vector 10575b7f0c42SBarry Smith 10585b7f0c42SBarry Smith Notes: This is rarely used directly 10595b7f0c42SBarry Smith 10605b7f0c42SBarry Smith Level: advanced 10615b7f0c42SBarry Smith 10625b7f0c42SBarry Smith @*/ 1063dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetBase(Mat J,Vec U) 10641d1367b7SBarry Smith { 1065dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat,Vec); 10661d1367b7SBarry Smith 10671d1367b7SBarry Smith PetscFunctionBegin; 10684482741eSBarry Smith PetscValidHeaderSpecific(J,MAT_COOKIE,1); 10694482741eSBarry Smith PetscValidHeaderSpecific(U,VEC_COOKIE,2); 1070c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)(void))&f);CHKERRQ(ierr); 1071cf3bea43SBarry Smith if (f) { 1072cf3bea43SBarry Smith ierr = (*f)(J,U);CHKERRQ(ierr); 107349d4803aSBarry Smith } 10741d1367b7SBarry Smith PetscFunctionReturn(0); 10751d1367b7SBarry Smith } 1076cf57b110SBarry Smith 10775b7f0c42SBarry Smith #undef __FUNCT__ 10785b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh" 107961860be5SBarry Smith /*@C 10805b7f0c42SBarry Smith MatSNESMFSetCheckh - Sets a function that checks the computed h and adjusts 10815b7f0c42SBarry Smith it to satisfy some criteria 1082cf57b110SBarry Smith 10835b7f0c42SBarry Smith Collective on Mat 10845b7f0c42SBarry Smith 10855b7f0c42SBarry Smith Input Parameters: 10865b7f0c42SBarry Smith + J - the MatSNESMF matrix 10875b7f0c42SBarry Smith . fun - the function that checks h 10885b7f0c42SBarry Smith - ctx - any context needed by the function 10895b7f0c42SBarry Smith 10905b7f0c42SBarry Smith Options Database Keys: 10915b7f0c42SBarry Smith . -snes_mf_check_positivity 10925b7f0c42SBarry Smith 10935b7f0c42SBarry Smith Level: advanced 10945b7f0c42SBarry Smith 10955b7f0c42SBarry Smith Notes: For example, MatSNESMFSetCheckPositivity() insures that all entries 10965b7f0c42SBarry Smith of U + h*a are non-negative 10975b7f0c42SBarry Smith 10985b7f0c42SBarry Smith .seealso: MatSNESMFSetCheckPositivity() 10995b7f0c42SBarry Smith @*/ 1100*6849ba73SBarry Smith PetscErrorCode MatSNESMFSetCheckh(Mat J,PetscErrorCode (*fun)(Vec,Vec,PetscScalar*,void*),void* ctx) 11015b7f0c42SBarry Smith { 1102*6849ba73SBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,Vec,PetscScalar*,void*),void*); 11035b7f0c42SBarry Smith 11045b7f0c42SBarry Smith PetscFunctionBegin; 11054482741eSBarry Smith PetscValidHeaderSpecific(J,MAT_COOKIE,1); 11065b7f0c42SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr); 11075b7f0c42SBarry Smith if (f) { 11085b7f0c42SBarry Smith ierr = (*f)(J,fun,ctx);CHKERRQ(ierr); 11095b7f0c42SBarry Smith } 11105b7f0c42SBarry Smith PetscFunctionReturn(0); 11115b7f0c42SBarry Smith } 11125b7f0c42SBarry Smith 11135b7f0c42SBarry Smith #undef __FUNCT__ 11145b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckPositivity" 11155b7f0c42SBarry Smith /*@ 11165b7f0c42SBarry Smith MatSNESMFCheckPositivity - Checks that all entries in U + h*a are positive or 11175b7f0c42SBarry Smith zero, decreases h until this is satisfied. 11185b7f0c42SBarry Smith 11195b7f0c42SBarry Smith Collective on Vec 11205b7f0c42SBarry Smith 11215b7f0c42SBarry Smith Input Parameters: 11225b7f0c42SBarry Smith + U - base vector that is added to 11235b7f0c42SBarry Smith . a - vector that is added 11245b7f0c42SBarry Smith . h - scaling factor on a 11255b7f0c42SBarry Smith - dummy - context variable (unused) 11265b7f0c42SBarry Smith 11275b7f0c42SBarry Smith Options Database Keys: 11285b7f0c42SBarry Smith . -snes_mf_check_positivity 11295b7f0c42SBarry Smith 11305b7f0c42SBarry Smith Level: advanced 11315b7f0c42SBarry Smith 11325b7f0c42SBarry Smith Notes: This is rarely used directly, rather it is passed as an argument to 11335b7f0c42SBarry Smith MatSNESMFSetCheckh() 11345b7f0c42SBarry Smith 11355b7f0c42SBarry Smith .seealso: MatSNESMFSetCheckh() 11365b7f0c42SBarry Smith @*/ 1137dfbe8321SBarry Smith PetscErrorCode MatSNESMFCheckPositivity(Vec U,Vec a,PetscScalar *h,void *dummy) 11385b7f0c42SBarry Smith { 11395b7f0c42SBarry Smith PetscReal val, minval; 11405b7f0c42SBarry Smith PetscScalar *u_vec, *a_vec; 1141dfbe8321SBarry Smith PetscErrorCode ierr; 1142dfbe8321SBarry Smith int i, size; 11435b7f0c42SBarry Smith MPI_Comm comm; 11445b7f0c42SBarry Smith 11455b7f0c42SBarry Smith PetscFunctionBegin; 11465b7f0c42SBarry Smith ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr); 11475b7f0c42SBarry Smith ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr); 11485b7f0c42SBarry Smith ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr); 11495b7f0c42SBarry Smith ierr = VecGetLocalSize(U,&size);CHKERRQ(ierr); 115061860be5SBarry Smith minval = PetscAbsScalar(*h*1.01); 11515b7f0c42SBarry Smith for(i=0;i<size;i++) { 115261860be5SBarry Smith if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) { 115361860be5SBarry Smith val = PetscAbsScalar(u_vec[i]/a_vec[i]); 11545b7f0c42SBarry Smith if (val < minval) minval = val; 11555b7f0c42SBarry Smith } 11565b7f0c42SBarry Smith } 11575b7f0c42SBarry Smith ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr); 11585b7f0c42SBarry Smith ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr); 11595b7f0c42SBarry Smith ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr); 116061860be5SBarry Smith if (val <= PetscAbsScalar(*h)) { 116161860be5SBarry Smith PetscLogInfo(U,"MatSNESMFCheckPositivity: Scaling back h from %g to %g\n",PetscRealPart(*h),.99*val); 116261860be5SBarry Smith if (PetscRealPart(*h) > 0.0) *h = 0.99*val; 11635b7f0c42SBarry Smith else *h = -0.99*val; 11645b7f0c42SBarry Smith } 11655b7f0c42SBarry Smith PetscFunctionReturn(0); 11665b7f0c42SBarry Smith } 1167cf57b110SBarry Smith 1168cf57b110SBarry Smith 1169cf57b110SBarry Smith 1170cf57b110SBarry Smith 1171cf57b110SBarry Smith 1172cf57b110SBarry Smith 1173cf57b110SBarry Smith 1174