1*77d8c4bbSBarry Smith /*$Id: snesmfj.c,v 1.129 2001/08/07 03:04:10 balay Exp bsmith $*/ 281e6777dSBarry Smith 3e090d566SSatish Balay #include "src/snes/mf/snesmfj.h" /*I "petscsnes.h" I*/ 47e9d5209SBarry Smith #include "src/mat/matimpl.h" 581e6777dSBarry Smith 6b0a32e0cSBarry Smith PetscFList MatSNESMPetscFList = 0; 74c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE; 8a4d4d686SBarry Smith 94a2ae208SSatish Balay #undef __FUNCT__ 104a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetType" 11fd4bdd07SBarry Smith /*@C 1265f2ba5bSLois Curfman McInnes MatSNESMFSetType - Sets the method that is used to compute the 13b0a32e0cSBarry Smith differencing parameter for finite differene matrix-free formulations. 149a6cb015SBarry Smith 159a6cb015SBarry Smith Input Parameters: 167e9d5209SBarry Smith + mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMF() 177e9d5209SBarry Smith or MatSetType(mat,MATMFFD); 189a6cb015SBarry Smith - ftype - the type requested 199a6cb015SBarry Smith 2015091d37SBarry Smith Level: advanced 2115091d37SBarry Smith 2265f2ba5bSLois Curfman McInnes Notes: 2365f2ba5bSLois Curfman McInnes For example, such routines can compute h for use in 2465f2ba5bSLois Curfman McInnes Jacobian-vector products of the form 2565f2ba5bSLois Curfman McInnes 2665f2ba5bSLois Curfman McInnes F(x+ha) - F(x) 27ef4ad1fdSLois Curfman McInnes F'(u)a ~= ---------------- 2865f2ba5bSLois Curfman McInnes h 2965f2ba5bSLois Curfman McInnes 30f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic) 319a6cb015SBarry Smith @*/ 32f6a0df18SBarry Smith int MatSNESMFSetType(Mat mat,MatSNESMFType ftype) 33b9fa9cd0SBarry Smith { 345a655dc6SBarry Smith int ierr,(*r)(MatSNESMFCtx); 357e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 366831982aSBarry Smith PetscTruth match; 37a4d4d686SBarry Smith 38a4d4d686SBarry Smith PetscFunctionBegin; 390f5bd95cSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 400f5bd95cSBarry Smith PetscValidCharPointer(ftype); 410f5bd95cSBarry Smith 429a6cb015SBarry Smith /* already set, so just return */ 436831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr); 440f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 45a4d4d686SBarry Smith 469a6cb015SBarry Smith /* destroy the old one if it exists */ 479a6cb015SBarry Smith if (ctx->ops->destroy) { 489a6cb015SBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 499a6cb015SBarry Smith } 509a6cb015SBarry Smith 5165f2ba5bSLois Curfman McInnes /* Get the function pointers for the requrested method */ 525a655dc6SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 539a6cb015SBarry Smith 54b9617806SBarry Smith ierr = PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr); 559a6cb015SBarry Smith 5629bbc08cSBarry Smith if (!r) SETERRQ(1,"Unknown MatSNESMF type given"); 579a6cb015SBarry Smith 589a6cb015SBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 596831982aSBarry Smith 606831982aSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr); 619a6cb015SBarry Smith 629a6cb015SBarry Smith PetscFunctionReturn(0); 639a6cb015SBarry Smith } 649a6cb015SBarry Smith 6587828ca2SBarry Smith #undef __FUNCT__ 6687828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD" 6787828ca2SBarry Smith int MatSNESMFSetFunctioniBase_FD(Mat mat,int (*func)(Vec,void *)) 6887828ca2SBarry Smith { 6987828ca2SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 7087828ca2SBarry Smith 7187828ca2SBarry Smith PetscFunctionBegin; 7287828ca2SBarry Smith ctx->funcisetbase = func; 7387828ca2SBarry Smith PetscFunctionReturn(0); 7487828ca2SBarry Smith } 7587828ca2SBarry Smith 7687828ca2SBarry Smith #undef __FUNCT__ 7787828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD" 7887828ca2SBarry Smith int MatSNESMFSetFunctioni_FD(Mat mat,int (*funci)(int,Vec,PetscScalar*,void *)) 7987828ca2SBarry Smith { 8087828ca2SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 8187828ca2SBarry Smith 8287828ca2SBarry Smith PetscFunctionBegin; 8387828ca2SBarry Smith ctx->funci = funci; 8487828ca2SBarry Smith PetscFunctionReturn(0); 8587828ca2SBarry Smith } 8687828ca2SBarry Smith 8787828ca2SBarry Smith 889a6cb015SBarry Smith /*MC 89f1af5d2fSBarry Smith MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry. 909a6cb015SBarry Smith 919a6cb015SBarry Smith Synopsis: 92fed8bd04SBarry Smith int MatSNESMFRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF)) 939a6cb015SBarry Smith 949a6cb015SBarry Smith Not Collective 959a6cb015SBarry Smith 969a6cb015SBarry Smith Input Parameters: 979a6cb015SBarry Smith + name_solver - name of a new user-defined compute-h module 989a6cb015SBarry Smith . path - path (either absolute or relative) the library containing this solver 999a6cb015SBarry Smith . name_create - name of routine to create method context 1009a6cb015SBarry Smith - routine_create - routine to create method context 1019a6cb015SBarry Smith 10215091d37SBarry Smith Level: developer 10315091d37SBarry Smith 1049a6cb015SBarry Smith Notes: 105f1af5d2fSBarry Smith MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers. 1069a6cb015SBarry Smith 1079a6cb015SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 1089a6cb015SBarry Smith is ignored. 1099a6cb015SBarry Smith 1109a6cb015SBarry Smith Sample usage: 1119a6cb015SBarry Smith .vb 112f1af5d2fSBarry Smith MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a, 1139a6cb015SBarry Smith "MyHCreate",MyHCreate); 1149a6cb015SBarry Smith .ve 1159a6cb015SBarry Smith 1169a6cb015SBarry Smith Then, your solver can be chosen with the procedural interface via 1175a655dc6SBarry Smith $ MatSNESMFSetType(mfctx,"my_h") 1189a6cb015SBarry Smith or at runtime via the option 1199a6cb015SBarry Smith $ -snes_mf_type my_h 1209a6cb015SBarry Smith 1215a655dc6SBarry Smith .keywords: MatSNESMF, register 1229a6cb015SBarry Smith 1235a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy() 1249a6cb015SBarry Smith M*/ 1259a6cb015SBarry Smith 1264a2ae208SSatish Balay #undef __FUNCT__ 1274a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister" 128f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx)) 1299a6cb015SBarry Smith { 1309a6cb015SBarry Smith int ierr; 1319a6cb015SBarry Smith char fullname[256]; 1329a6cb015SBarry Smith 1339a6cb015SBarry Smith PetscFunctionBegin; 134b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 135b9617806SBarry Smith ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)())function);CHKERRQ(ierr); 1369a6cb015SBarry Smith PetscFunctionReturn(0); 1379a6cb015SBarry Smith } 1389a6cb015SBarry Smith 1399a6cb015SBarry Smith 1404a2ae208SSatish Balay #undef __FUNCT__ 1414a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy" 1429a6cb015SBarry Smith /*@C 1435a655dc6SBarry Smith MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were 144f1af5d2fSBarry Smith registered by MatSNESMFRegisterDynamic). 1459a6cb015SBarry Smith 1469a6cb015SBarry Smith Not Collective 1479a6cb015SBarry Smith 14815091d37SBarry Smith Level: developer 14915091d37SBarry Smith 1505a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy 1519a6cb015SBarry Smith 152f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll() 1539a6cb015SBarry Smith @*/ 1545a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void) 1559a6cb015SBarry Smith { 1569a6cb015SBarry Smith int ierr; 1579a6cb015SBarry Smith 1589a6cb015SBarry Smith PetscFunctionBegin; 159b0a32e0cSBarry Smith if (MatSNESMPetscFList) { 160b0a32e0cSBarry Smith ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr); 161b0a32e0cSBarry Smith MatSNESMPetscFList = 0; 1629a6cb015SBarry Smith } 1634c49b128SBarry Smith MatSNESMFRegisterAllCalled = PETSC_FALSE; 1649a6cb015SBarry Smith PetscFunctionReturn(0); 1659a6cb015SBarry Smith } 1669a6cb015SBarry Smith 1679a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/ 1684a2ae208SSatish Balay #undef __FUNCT__ 1698a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD" 1708a124369SBarry Smith int MatDestroy_MFFD(Mat mat) 171a4d4d686SBarry Smith { 172a4d4d686SBarry Smith int ierr; 1737e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 174fae171e0SBarry Smith 1753a40ed3dSBarry Smith PetscFunctionBegin; 176b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 1779a6cb015SBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 17874637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 1796831982aSBarry Smith PetscHeaderDestroy(ctx); 1803a40ed3dSBarry Smith PetscFunctionReturn(0); 181b9fa9cd0SBarry Smith } 18250361f65SLois Curfman McInnes 1834a2ae208SSatish Balay #undef __FUNCT__ 1848a124369SBarry Smith #define __FUNCT__ "MatView_MFFD" 18539e2f89bSBarry Smith /* 1868a124369SBarry Smith MatSNESMFView_MFFD - Views matrix-free parameters. 1878f6e3e37SBarry Smith 18839e2f89bSBarry Smith */ 1898a124369SBarry Smith int MatView_MFFD(Mat J,PetscViewer viewer) 190eb9086c3SLois Curfman McInnes { 191eb9086c3SLois Curfman McInnes int ierr; 1927e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 1936831982aSBarry Smith PetscTruth isascii; 194eb9086c3SLois Curfman McInnes 1953a40ed3dSBarry Smith PetscFunctionBegin; 196b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 1970f5bd95cSBarry Smith if (isascii) { 198b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," SNES matrix-free approximation:\n");CHKERRQ(ierr); 199b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 200473c83c3SBarry Smith if (!ctx->type_name) { 201b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," The compute h routine has not yet been set\n");CHKERRQ(ierr); 202473c83c3SBarry Smith } else { 203b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr); 204473c83c3SBarry Smith } 2059a6cb015SBarry Smith if (ctx->ops->view) { 2069a6cb015SBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 2079a6cb015SBarry Smith } 2085cd90555SBarry Smith } else { 20929bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name); 210eb9086c3SLois Curfman McInnes } 2113a40ed3dSBarry Smith PetscFunctionReturn(0); 212eb9086c3SLois Curfman McInnes } 213eb9086c3SLois Curfman McInnes 2144a2ae208SSatish Balay #undef __FUNCT__ 2158a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD" 216be726c96SBarry Smith /* 2175a655dc6SBarry Smith MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This 21865f2ba5bSLois Curfman McInnes allows the user to indicate the beginning of a new linear solve by calling 219be726c96SBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 22065f2ba5bSLois Curfman McInnes MatSNESMFCreate_WP() to properly compute ||U|| only the first time 22165f2ba5bSLois Curfman McInnes in the linear solver rather than every time. 222be726c96SBarry Smith */ 2238a124369SBarry Smith int MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt) 224be726c96SBarry Smith { 225be726c96SBarry Smith int ierr; 2267e9d5209SBarry Smith MatSNESMFCtx j = (MatSNESMFCtx)J->data; 22765df01d8SBarry Smith SNESProblemType type; 228be726c96SBarry Smith 229be726c96SBarry Smith PetscFunctionBegin; 2305a655dc6SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 231b0a32e0cSBarry Smith if (j->usesnes) { 2321d1367b7SBarry Smith ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr); 23365df01d8SBarry Smith ierr = SNESGetProblemType(j->snes,&type);CHKERRQ(ierr); 23465df01d8SBarry Smith if (type == SNES_NONLINEAR_EQUATIONS) { 2351d1367b7SBarry Smith ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 23665df01d8SBarry Smith } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 2371d1367b7SBarry Smith ierr = SNESGetGradient(j->snes,&j->current_f,PETSC_NULL);CHKERRQ(ierr); 23829bbc08cSBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class"); 2391d1367b7SBarry Smith } 240be726c96SBarry Smith PetscFunctionReturn(0); 241be726c96SBarry Smith } 242be726c96SBarry Smith 2434a2ae208SSatish Balay #undef __FUNCT__ 2448a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD" 245eb9086c3SLois Curfman McInnes /* 2465a655dc6SBarry Smith MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector 247eb9086c3SLois Curfman McInnes product, y = F'(u)*a: 248a4d4d686SBarry Smith 2499a6cb015SBarry Smith y ~= (F(u + ha) - F(u))/h, 250eb9086c3SLois Curfman McInnes where F = nonlinear function, as set by SNESSetFunction() 251eb9086c3SLois Curfman McInnes u = current iterate 252eb9086c3SLois Curfman McInnes h = difference interval 253eb9086c3SLois Curfman McInnes */ 2548a124369SBarry Smith int MatMult_MFFD(Mat mat,Vec a,Vec y) 25539e2f89bSBarry Smith { 2567e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 257fae171e0SBarry Smith SNES snes; 258ea709b57SSatish Balay PetscScalar h,mone = -1.0; 259fae171e0SBarry Smith Vec w,U,F; 260a305c92eSSatish Balay int ierr,(*eval_fct)(SNES,Vec,Vec)=0; 26165df01d8SBarry Smith SNESProblemType type; 26239e2f89bSBarry Smith 2633a40ed3dSBarry Smith PetscFunctionBegin; 2649a6cb015SBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 2659a6cb015SBarry Smith separate the performance monitoring from the cases that use conventional 2669a6cb015SBarry Smith storage. We may eventually modify event logging to associate events 2679a6cb015SBarry Smith with particular objects, hence alleviating the more general problem. */ 268b0a32e0cSBarry Smith ierr = PetscLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr); 26956cd22aeSBarry Smith 270fae171e0SBarry Smith snes = ctx->snes; 271fae171e0SBarry Smith w = ctx->w; 2721d1367b7SBarry Smith U = ctx->current_u; 27350361f65SLois Curfman McInnes 27485614651SBarry Smith /* 27585614651SBarry Smith Compute differencing parameter 27685614651SBarry Smith */ 2779a6cb015SBarry Smith if (!ctx->ops->compute) { 278b7fd4e64SBarry Smith ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr); 2795a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr); 2809a6cb015SBarry Smith } 2819a6cb015SBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr); 282a4d4d686SBarry Smith 283a4d4d686SBarry Smith /* keep a record of the current differencing parameter h */ 284a4d4d686SBarry Smith ctx->currenth = h; 285aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 2868a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h)); 287a4d4d686SBarry Smith #else 2888a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h); 289a4d4d686SBarry Smith #endif 290a4d4d686SBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 29185614651SBarry Smith ctx->historyh[ctx->ncurrenth] = h; 292a4d4d686SBarry Smith } 29385614651SBarry Smith ctx->ncurrenth++; 294a4d4d686SBarry Smith 29585614651SBarry Smith /* w = u + ha */ 296a4d4d686SBarry Smith ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr); 29785614651SBarry Smith 298b0a32e0cSBarry Smith if (ctx->usesnes) { 29965df01d8SBarry Smith ierr = SNESGetProblemType(snes,&type);CHKERRQ(ierr); 30065df01d8SBarry Smith if (type == SNES_NONLINEAR_EQUATIONS) { 30185614651SBarry Smith eval_fct = SNESComputeFunction; 30265df01d8SBarry Smith } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 30385614651SBarry Smith eval_fct = SNESComputeGradient; 30429bbc08cSBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class"); 3051d1367b7SBarry Smith F = ctx->current_f; 30629bbc08cSBarry Smith if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices"); 30739903ad8SBarry Smith ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr); 30885614651SBarry Smith } else { 30985614651SBarry Smith F = ctx->funcvec; 31085614651SBarry Smith /* compute func(U) as base for differencing */ 31185614651SBarry Smith if (ctx->ncurrenth == 1) { 31285614651SBarry Smith ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr); 31385614651SBarry Smith } 31485614651SBarry Smith ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr); 31585614651SBarry Smith } 316a4d4d686SBarry Smith 317a4d4d686SBarry Smith ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr); 318a4d4d686SBarry Smith h = 1.0/h; 319a4d4d686SBarry Smith ierr = VecScale(&h,y);CHKERRQ(ierr); 32074637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 321a4d4d686SBarry Smith 322b0a32e0cSBarry Smith ierr = PetscLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr); 323a4d4d686SBarry Smith PetscFunctionReturn(0); 324a4d4d686SBarry Smith } 325a4d4d686SBarry Smith 3264a2ae208SSatish Balay #undef __FUNCT__ 3278a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD" 328cf57b110SBarry Smith /* 3298a124369SBarry Smith MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix 330cf57b110SBarry Smith 331cf57b110SBarry Smith y ~= (F(u + ha) - F(u))/h, 332cf57b110SBarry Smith where F = nonlinear function, as set by SNESSetFunction() 333cf57b110SBarry Smith u = current iterate 334cf57b110SBarry Smith h = difference interval 335cf57b110SBarry Smith */ 3368a124369SBarry Smith int MatGetDiagonal_MFFD(Mat mat,Vec a) 337cf57b110SBarry Smith { 3387e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 339ea709b57SSatish Balay PetscScalar h,*aa,*ww,v; 340*77d8c4bbSBarry Smith PetscReal epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON; 34165df01d8SBarry Smith Vec w,U; 342cf57b110SBarry Smith int i,ierr,rstart,rend; 343cf57b110SBarry Smith 344cf57b110SBarry Smith PetscFunctionBegin; 345cf57b110SBarry Smith if (!ctx->funci) { 346cf57b110SBarry Smith SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first"); 347cf57b110SBarry Smith } 348cf57b110SBarry Smith 349cf57b110SBarry Smith w = ctx->w; 350cf57b110SBarry Smith U = ctx->current_u; 351cf57b110SBarry Smith ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr); 352cf57b110SBarry Smith ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr); 353cf57b110SBarry Smith ierr = VecCopy(U,w);CHKERRQ(ierr); 354cf57b110SBarry Smith 355cf57b110SBarry Smith ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr); 356cf57b110SBarry Smith ierr = VecGetArray(a,&aa);CHKERRQ(ierr); 357cf57b110SBarry Smith for (i=rstart; i<rend; i++) { 358cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 359cf57b110SBarry Smith h = ww[i-rstart]; 360cf57b110SBarry Smith if (h == 0.0) h = 1.0; 361cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX) 362cf57b110SBarry Smith if (h < umin && h >= 0.0) h = umin; 363cf57b110SBarry Smith else if (h < 0.0 && h > -umin) h = -umin; 364cf57b110SBarry Smith #else 365cf57b110SBarry Smith if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin; 366cf57b110SBarry Smith else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin; 367cf57b110SBarry Smith #endif 368cf57b110SBarry Smith h *= epsilon; 369cf57b110SBarry Smith 370cf57b110SBarry Smith ww[i-rstart] += h; 371cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 372cf57b110SBarry Smith ierr = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr); 373cf57b110SBarry Smith aa[i-rstart] = (v - aa[i-rstart])/h; 374cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 375cf57b110SBarry Smith ww[i-rstart] -= h; 376cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 377cf57b110SBarry Smith } 378cf57b110SBarry Smith ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr); 379cf57b110SBarry Smith PetscFunctionReturn(0); 380cf57b110SBarry Smith } 381cf57b110SBarry Smith 382cf57b110SBarry Smith #undef __FUNCT__ 3834a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF" 384a4d4d686SBarry Smith /*@C 38565f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 38665f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 38765f2ba5bSLois Curfman McInnes the routine SNESSetJacobian(). 388a4d4d686SBarry Smith 389a4d4d686SBarry Smith Collective on SNES and Vec 390a4d4d686SBarry Smith 391a4d4d686SBarry Smith Input Parameters: 392a4d4d686SBarry Smith + snes - the SNES context 393a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 394a4d4d686SBarry Smith 395a4d4d686SBarry Smith Output Parameter: 396a4d4d686SBarry Smith . J - the matrix-free matrix 397a4d4d686SBarry Smith 39815091d37SBarry Smith Level: advanced 39915091d37SBarry Smith 400a4d4d686SBarry Smith Notes: 401a4d4d686SBarry Smith The matrix-free matrix context merely contains the function pointers 402a4d4d686SBarry Smith and work space for performing finite difference approximations of 40365f2ba5bSLois Curfman McInnes Jacobian-vector products, F'(u)*a, 4049a6cb015SBarry Smith 4059a6cb015SBarry Smith The default code uses the following approach to compute h 406a4d4d686SBarry Smith 407a4d4d686SBarry Smith .vb 40865f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 409a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 410a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 411a4d4d686SBarry Smith where 412a4d4d686SBarry Smith error_rel = square root of relative error in function evaluation 413a4d4d686SBarry Smith umin = minimum iterate parameter 414a4d4d686SBarry Smith .ve 415a4d4d686SBarry Smith 4165a655dc6SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 41765f2ba5bSLois Curfman McInnes umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 41865f2ba5bSLois Curfman McInnes of the users manual for details. 419a4d4d686SBarry Smith 420a4d4d686SBarry Smith The user should call MatDestroy() when finished with the matrix-free 421a4d4d686SBarry Smith matrix context. 422a4d4d686SBarry Smith 423a4d4d686SBarry Smith Options Database Keys: 424a4d4d686SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 4259a6cb015SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 426a4d4d686SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 427a4d4d686SBarry Smith 428a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix 429a4d4d686SBarry Smith 4305a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 4311d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(), 432fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian() 433a4d4d686SBarry Smith 434a4d4d686SBarry Smith @*/ 4355a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J) 436a4d4d686SBarry Smith { 4371d1367b7SBarry Smith MatSNESMFCtx mfctx; 4381d1367b7SBarry Smith int ierr; 4391d1367b7SBarry Smith 4401d1367b7SBarry Smith PetscFunctionBegin; 4411d1367b7SBarry Smith ierr = MatCreateMF(x,J);CHKERRQ(ierr); 4427e9d5209SBarry Smith 4437e9d5209SBarry Smith mfctx = (MatSNESMFCtx)(*J)->data; 4441d1367b7SBarry Smith mfctx->snes = snes; 445b0a32e0cSBarry Smith mfctx->usesnes = PETSC_TRUE; 446b0a32e0cSBarry Smith PetscLogObjectParent(snes,*J); 4471d1367b7SBarry Smith PetscFunctionReturn(0); 4481d1367b7SBarry Smith } 4491d1367b7SBarry Smith 450cf3bea43SBarry Smith EXTERN_C_BEGIN 451cf3bea43SBarry Smith #undef __FUNCT__ 452cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD" 453cf3bea43SBarry Smith int MatSNESMFSetBase_FD(Mat J,Vec U) 454cf3bea43SBarry Smith { 455cf3bea43SBarry Smith int ierr; 4567e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 457cf3bea43SBarry Smith 458cf3bea43SBarry Smith PetscFunctionBegin; 459cf3bea43SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 460cf3bea43SBarry Smith ctx->current_u = U; 461cf3bea43SBarry Smith ctx->usesnes = PETSC_FALSE; 462cf3bea43SBarry Smith PetscFunctionReturn(0); 463cf3bea43SBarry Smith } 464cf3bea43SBarry Smith EXTERN_C_END 465cf3bea43SBarry Smith 4664a2ae208SSatish Balay #undef __FUNCT__ 4677e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions" 4687e9d5209SBarry Smith /*@ 4697e9d5209SBarry Smith MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line 4707e9d5209SBarry Smith parameter. 4717e9d5209SBarry Smith 4727e9d5209SBarry Smith Collective on Mat 4737e9d5209SBarry Smith 4747e9d5209SBarry Smith Input Parameters: 4757e9d5209SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 4767e9d5209SBarry Smith 4777e9d5209SBarry Smith Options Database Keys: 4787e9d5209SBarry Smith + -snes_mf_type - <default,wp> 4797e9d5209SBarry Smith - -snes_mf_err - square root of estimated relative error in function evaluation 4807e9d5209SBarry Smith - -snes_mf_period - how often h is recomputed, defaults to 1, everytime 4817e9d5209SBarry Smith 4827e9d5209SBarry Smith Level: advanced 4837e9d5209SBarry Smith 4847e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters 4857e9d5209SBarry Smith 4867e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4877e9d5209SBarry Smith MatSNESMFResetHHistory(), MatSNESMFKSPMonitor() 4887e9d5209SBarry Smith @*/ 4897e9d5209SBarry Smith int MatSNESMFSetFromOptions(Mat mat) 4907e9d5209SBarry Smith { 4917e9d5209SBarry Smith MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data; 4927e9d5209SBarry Smith int ierr; 4937e9d5209SBarry Smith PetscTruth flg; 4947e9d5209SBarry Smith char ftype[256]; 4957e9d5209SBarry Smith 4967e9d5209SBarry Smith PetscFunctionBegin; 4977e9d5209SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 4987e9d5209SBarry Smith 4997e9d5209SBarry Smith ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr); 5007e9d5209SBarry Smith ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr); 5017e9d5209SBarry Smith if (flg) { 5027e9d5209SBarry Smith ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr); 5037e9d5209SBarry Smith } 5047e9d5209SBarry Smith 50587828ca2SBarry Smith ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr); 5067e9d5209SBarry Smith ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr); 5077e9d5209SBarry Smith if (mfctx->snes) { 5087e9d5209SBarry Smith ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr); 5097e9d5209SBarry Smith if (flg) { 5107e9d5209SBarry Smith SLES sles; 5117e9d5209SBarry Smith KSP ksp; 5127e9d5209SBarry Smith ierr = SNESGetSLES(mfctx->snes,&sles);CHKERRQ(ierr); 5137e9d5209SBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 5147e9d5209SBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 5157e9d5209SBarry Smith } 5167e9d5209SBarry Smith } 5177e9d5209SBarry Smith if (mfctx->ops->setfromoptions) { 5187e9d5209SBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 5197e9d5209SBarry Smith } 5207e9d5209SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5217e9d5209SBarry Smith PetscFunctionReturn(0); 5227e9d5209SBarry Smith } 5237e9d5209SBarry Smith 5247e9d5209SBarry Smith #undef __FUNCT__ 5257e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD" 5267e9d5209SBarry Smith EXTERN_C_BEGIN 5277e9d5209SBarry Smith int MatCreate_MFFD(Mat A) 5287e9d5209SBarry Smith { 5297e9d5209SBarry Smith MatSNESMFCtx mfctx; 53065df01d8SBarry Smith int ierr; 5317e9d5209SBarry Smith 5327e9d5209SBarry Smith PetscFunctionBegin; 5338a124369SBarry Smith PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD); 5347e9d5209SBarry Smith PetscLogObjectCreate(mfctx); 5357e9d5209SBarry Smith mfctx->sp = 0; 5367e9d5209SBarry Smith mfctx->snes = 0; 537*77d8c4bbSBarry Smith mfctx->error_rel = PETSC_SQRT_MACHINE_EPSILON; 5387e9d5209SBarry Smith mfctx->recomputeperiod = 1; 5397e9d5209SBarry Smith mfctx->count = 0; 5407e9d5209SBarry Smith mfctx->currenth = 0.0; 5417e9d5209SBarry Smith mfctx->historyh = PETSC_NULL; 5427e9d5209SBarry Smith mfctx->ncurrenth = 0; 5437e9d5209SBarry Smith mfctx->maxcurrenth = 0; 5447e9d5209SBarry Smith mfctx->type_name = 0; 5457e9d5209SBarry Smith mfctx->usesnes = PETSC_FALSE; 5467e9d5209SBarry Smith 5477e9d5209SBarry Smith /* 5487e9d5209SBarry Smith Create the empty data structure to contain compute-h routines. 5497e9d5209SBarry Smith These will be filled in below from the command line options or 5507e9d5209SBarry Smith a later call with MatSNESMFSetType() or if that is not called 5518a124369SBarry Smith then it will default in the first use of MatMult_MFFD() 5527e9d5209SBarry Smith */ 5537e9d5209SBarry Smith mfctx->ops->compute = 0; 5547e9d5209SBarry Smith mfctx->ops->destroy = 0; 5557e9d5209SBarry Smith mfctx->ops->view = 0; 5567e9d5209SBarry Smith mfctx->ops->setfromoptions = 0; 5577e9d5209SBarry Smith mfctx->hctx = 0; 5587e9d5209SBarry Smith 5597e9d5209SBarry Smith mfctx->func = 0; 5607e9d5209SBarry Smith mfctx->funcctx = 0; 5617e9d5209SBarry Smith mfctx->funcvec = 0; 5627e9d5209SBarry Smith 56365df01d8SBarry Smith A->data = mfctx; 5647e9d5209SBarry Smith 5658a124369SBarry Smith A->ops->mult = MatMult_MFFD; 5668a124369SBarry Smith A->ops->destroy = MatDestroy_MFFD; 5678a124369SBarry Smith A->ops->view = MatView_MFFD; 5688a124369SBarry Smith A->ops->assemblyend = MatAssemblyEnd_MFFD; 5698a124369SBarry Smith A->ops->getdiagonal = MatGetDiagonal_MFFD; 57065df01d8SBarry Smith A->ops->setfromoptions = MatSNESMFSetFromOptions; 5717e9d5209SBarry Smith 57265df01d8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr); 57387828ca2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBasei_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr); 57487828ca2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr); 57565df01d8SBarry Smith mfctx->mat = A; 57665df01d8SBarry Smith ierr = VecCreateMPI(A->comm,A->n,A->N,&mfctx->w);CHKERRQ(ierr); 5777e9d5209SBarry Smith 5787e9d5209SBarry Smith PetscFunctionReturn(0); 5797e9d5209SBarry Smith } 5807e9d5209SBarry Smith 5817e9d5209SBarry Smith EXTERN_C_END 5827e9d5209SBarry Smith 5837e9d5209SBarry Smith #undef __FUNCT__ 5844a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF" 5851d1367b7SBarry Smith /*@C 5861d1367b7SBarry Smith MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF() 5871d1367b7SBarry Smith 5881d1367b7SBarry Smith Collective on Vec 5891d1367b7SBarry Smith 5901d1367b7SBarry Smith Input Parameters: 5911d1367b7SBarry Smith . x - vector that defines layout of the vectors and matrices 5921d1367b7SBarry Smith 5931d1367b7SBarry Smith Output Parameter: 5941d1367b7SBarry Smith . J - the matrix-free matrix 5951d1367b7SBarry Smith 5961d1367b7SBarry Smith Level: advanced 5971d1367b7SBarry Smith 5981d1367b7SBarry Smith Notes: 5991d1367b7SBarry Smith The matrix-free matrix context merely contains the function pointers 6001d1367b7SBarry Smith and work space for performing finite difference approximations of 6011d1367b7SBarry Smith Jacobian-vector products, F'(u)*a, 6021d1367b7SBarry Smith 6031d1367b7SBarry Smith The default code uses the following approach to compute h 6041d1367b7SBarry Smith 6051d1367b7SBarry Smith .vb 6061d1367b7SBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 6071d1367b7SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 6081d1367b7SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 6091d1367b7SBarry Smith where 6101d1367b7SBarry Smith error_rel = square root of relative error in function evaluation 6111d1367b7SBarry Smith umin = minimum iterate parameter 6121d1367b7SBarry Smith .ve 6131d1367b7SBarry Smith 6141d1367b7SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 6151d1367b7SBarry Smith umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 6161d1367b7SBarry Smith of the users manual for details. 6171d1367b7SBarry Smith 6181d1367b7SBarry Smith The user should call MatDestroy() when finished with the matrix-free 6191d1367b7SBarry Smith matrix context. 6201d1367b7SBarry Smith 6211d1367b7SBarry Smith Options Database Keys: 6221d1367b7SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 6231d1367b7SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 6241d1367b7SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 6251d1367b7SBarry Smith 6261d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix 6271d1367b7SBarry Smith 6281d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 6291d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(), 630fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian() 6311d1367b7SBarry Smith 6321d1367b7SBarry Smith @*/ 6331d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J) 6341d1367b7SBarry Smith { 635a4d4d686SBarry Smith MPI_Comm comm; 6369a6cb015SBarry Smith int n,nloc,ierr; 637a4d4d686SBarry Smith 638a4d4d686SBarry Smith PetscFunctionBegin; 6391d1367b7SBarry Smith ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 64065df01d8SBarry Smith ierr = VecGetSize(x,&n);CHKERRQ(ierr); 64165df01d8SBarry Smith ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr); 6427e9d5209SBarry Smith ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr); 64365df01d8SBarry Smith ierr = MatRegister(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr); 64465df01d8SBarry Smith ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr); 6459a6cb015SBarry Smith PetscFunctionReturn(0); 6469a6cb015SBarry Smith } 6479a6cb015SBarry Smith 648a4d4d686SBarry Smith 6494a2ae208SSatish Balay #undef __FUNCT__ 6504a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH" 651a4d4d686SBarry Smith /*@ 65265f2ba5bSLois Curfman McInnes MatSNESMFGetH - Gets the last value that was used as the differencing 653a4d4d686SBarry Smith parameter. 654a4d4d686SBarry Smith 655a4d4d686SBarry Smith Not Collective 656a4d4d686SBarry Smith 657a4d4d686SBarry Smith Input Parameters: 6585a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 659a4d4d686SBarry Smith 660a4d4d686SBarry Smith Output Paramter: 661a4d4d686SBarry Smith . h - the differencing step size 662a4d4d686SBarry Smith 66315091d37SBarry Smith Level: advanced 66415091d37SBarry Smith 665a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 666a4d4d686SBarry Smith 6675a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 6685a655dc6SBarry Smith MatSNESMFResetHHistory(),MatSNESMFKSPMonitor() 669a4d4d686SBarry Smith @*/ 67087828ca2SBarry Smith int MatSNESMFGetH(Mat mat,PetscScalar *h) 671a4d4d686SBarry Smith { 6727e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 673a4d4d686SBarry Smith 674a4d4d686SBarry Smith PetscFunctionBegin; 675a4d4d686SBarry Smith *h = ctx->currenth; 676a4d4d686SBarry Smith PetscFunctionReturn(0); 677a4d4d686SBarry Smith } 678a4d4d686SBarry Smith 6794a2ae208SSatish Balay #undef __FUNCT__ 6804a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor" 681a4d4d686SBarry Smith /* 6825a655dc6SBarry Smith MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc 68365f2ba5bSLois Curfman McInnes SNES matrix free routines. Prints the differencing parameter used at 68465f2ba5bSLois Curfman McInnes each step. 685a4d4d686SBarry Smith */ 686329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy) 687a4d4d686SBarry Smith { 688a4d4d686SBarry Smith PC pc; 6895a655dc6SBarry Smith MatSNESMFCtx ctx; 690a4d4d686SBarry Smith int ierr; 691a4d4d686SBarry Smith Mat mat; 692a4d4d686SBarry Smith MPI_Comm comm; 693a4d4d686SBarry Smith PetscTruth nonzeroinitialguess; 694a4d4d686SBarry Smith 695a4d4d686SBarry Smith PetscFunctionBegin; 696a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 697a4d4d686SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 698a4d4d686SBarry Smith ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr); 699a4d4d686SBarry Smith ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 7007e9d5209SBarry Smith ctx = (MatSNESMFCtx)mat->data; 7017e9d5209SBarry Smith 702a4d4d686SBarry Smith if (n > 0 || nonzeroinitialguess) { 703aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 704d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm, 705329f5518SBarry Smith PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr); 706a4d4d686SBarry Smith #else 707d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr); 708a4d4d686SBarry Smith #endif 709a4d4d686SBarry Smith } else { 710d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr); 711a4d4d686SBarry Smith } 712a4d4d686SBarry Smith PetscFunctionReturn(0); 713a4d4d686SBarry Smith } 714a4d4d686SBarry Smith 7154a2ae208SSatish Balay #undef __FUNCT__ 7164a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction" 71785614651SBarry Smith /*@C 71885614651SBarry Smith MatSNESMFSetFunction - Sets the function used in applying the matrix free. 71985614651SBarry Smith 72085614651SBarry Smith Collective on Mat 72185614651SBarry Smith 72285614651SBarry Smith Input Parameters: 72385614651SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 72485614651SBarry Smith . v - workspace vector 72585614651SBarry Smith . func - the function to use 72685614651SBarry Smith - funcctx - optional function context passed to function 72785614651SBarry Smith 72885614651SBarry Smith Level: advanced 72985614651SBarry Smith 73085614651SBarry Smith Notes: 73185614651SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 73285614651SBarry Smith matrix inside your compute Jacobian routine 73385614651SBarry Smith 73485614651SBarry Smith If this is not set then it will use the function set with SNESSetFunction() 73585614651SBarry Smith 73685614651SBarry Smith .keywords: SNES, matrix-free, function 73785614651SBarry Smith 73885614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 73985614651SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 74085614651SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 74185614651SBarry Smith @*/ 74285614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx) 74385614651SBarry Smith { 7447e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 74585614651SBarry Smith 74685614651SBarry Smith PetscFunctionBegin; 74785614651SBarry Smith ctx->func = func; 74885614651SBarry Smith ctx->funcctx = funcctx; 74985614651SBarry Smith ctx->funcvec = v; 75085614651SBarry Smith PetscFunctionReturn(0); 75185614651SBarry Smith } 75285614651SBarry Smith 753cf57b110SBarry Smith #undef __FUNCT__ 754cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni" 755cf57b110SBarry Smith /*@C 756cf57b110SBarry Smith MatSNESMFSetFunctioni - Sets the function for a single component 757cf57b110SBarry Smith 758cf57b110SBarry Smith Collective on Mat 759cf57b110SBarry Smith 760cf57b110SBarry Smith Input Parameters: 761cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 762cf57b110SBarry Smith - funci - the function to use 763cf57b110SBarry Smith 764cf57b110SBarry Smith Level: advanced 765cf57b110SBarry Smith 766cf57b110SBarry Smith Notes: 767cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 768cf57b110SBarry Smith matrix inside your compute Jacobian routine 769cf57b110SBarry Smith 770cf57b110SBarry Smith 771cf57b110SBarry Smith .keywords: SNES, matrix-free, function 772cf57b110SBarry Smith 773cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 774cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 775cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 776cf57b110SBarry Smith @*/ 77787828ca2SBarry Smith int MatSNESMFSetFunctioni(Mat mat,int (*funci)(int,Vec,PetscScalar*,void *)) 778cf57b110SBarry Smith { 77987828ca2SBarry Smith int ierr,(*f)(Mat,int (*)(int,Vec,PetscScalar*,void *)); 780cf57b110SBarry Smith 781cf57b110SBarry Smith PetscFunctionBegin; 78287828ca2SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 78387828ca2SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)())&f);CHKERRQ(ierr); 78487828ca2SBarry Smith if (f) { 78587828ca2SBarry Smith ierr = (*f)(mat,funci);CHKERRQ(ierr); 78687828ca2SBarry Smith } 787cf57b110SBarry Smith PetscFunctionReturn(0); 788cf57b110SBarry Smith } 789cf57b110SBarry Smith 79087828ca2SBarry Smith 791cf57b110SBarry Smith #undef __FUNCT__ 792cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase" 793cf57b110SBarry Smith /*@C 794cf57b110SBarry Smith MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation 795cf57b110SBarry Smith 796cf57b110SBarry Smith Collective on Mat 797cf57b110SBarry Smith 798cf57b110SBarry Smith Input Parameters: 799cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 800cf57b110SBarry Smith - func - the function to use 801cf57b110SBarry Smith 802cf57b110SBarry Smith Level: advanced 803cf57b110SBarry Smith 804cf57b110SBarry Smith Notes: 805cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 806cf57b110SBarry Smith matrix inside your compute Jacobian routine 807cf57b110SBarry Smith 808cf57b110SBarry Smith 809cf57b110SBarry Smith .keywords: SNES, matrix-free, function 810cf57b110SBarry Smith 811cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 812cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 813cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 814cf57b110SBarry Smith @*/ 815cf57b110SBarry Smith int MatSNESMFSetFunctioniBase(Mat mat,int (*func)(Vec,void *)) 816cf57b110SBarry Smith { 81787828ca2SBarry Smith int ierr,(*f)(Mat,int (*)(Vec,void *)); 818cf57b110SBarry Smith 819cf57b110SBarry Smith PetscFunctionBegin; 82087828ca2SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 82187828ca2SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)())&f);CHKERRQ(ierr); 82287828ca2SBarry Smith if (f) { 82387828ca2SBarry Smith ierr = (*f)(mat,func);CHKERRQ(ierr); 82487828ca2SBarry Smith } 825cf57b110SBarry Smith PetscFunctionReturn(0); 826cf57b110SBarry Smith } 827cf57b110SBarry Smith 82885614651SBarry Smith 8294a2ae208SSatish Balay #undef __FUNCT__ 8304a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod" 831329f5518SBarry Smith /*@ 832329f5518SBarry Smith MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime 833329f5518SBarry Smith 834329f5518SBarry Smith Collective on Mat 835329f5518SBarry Smith 836329f5518SBarry Smith Input Parameters: 837329f5518SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 838329f5518SBarry Smith - period - 1 for everytime, 2 for every second etc 839329f5518SBarry Smith 840329f5518SBarry Smith Options Database Keys: 841329f5518SBarry Smith + -snes_mf_period <period> 842329f5518SBarry Smith 843329f5518SBarry Smith Level: advanced 844329f5518SBarry Smith 845329f5518SBarry Smith 846329f5518SBarry Smith .keywords: SNES, matrix-free, parameters 847329f5518SBarry Smith 848329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 849329f5518SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 850329f5518SBarry Smith MatSNESMFKSPMonitor() 851329f5518SBarry Smith @*/ 852329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period) 853329f5518SBarry Smith { 8547e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 855329f5518SBarry Smith 856329f5518SBarry Smith PetscFunctionBegin; 857329f5518SBarry Smith ctx->recomputeperiod = period; 858329f5518SBarry Smith PetscFunctionReturn(0); 859329f5518SBarry Smith } 860329f5518SBarry Smith 8614a2ae208SSatish Balay #undef __FUNCT__ 8624a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError" 863a4d4d686SBarry Smith /*@ 8645a655dc6SBarry Smith MatSNESMFSetFunctionError - Sets the error_rel for the approximation of 865a4d4d686SBarry Smith matrix-vector products using finite differences. 866a4d4d686SBarry Smith 867a4d4d686SBarry Smith Collective on Mat 868a4d4d686SBarry Smith 869a4d4d686SBarry Smith Input Parameters: 8705a655dc6SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 8719a6cb015SBarry Smith - error_rel - relative error (should be set to the square root of 872a4d4d686SBarry Smith the relative error in the function evaluations) 873a4d4d686SBarry Smith 87415091d37SBarry Smith Options Database Keys: 87515091d37SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 87615091d37SBarry Smith 87715091d37SBarry Smith Level: advanced 87815091d37SBarry Smith 879a4d4d686SBarry Smith Notes: 880a4d4d686SBarry Smith The default matrix-free matrix-vector product routine computes 881a4d4d686SBarry Smith .vb 88265f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 883a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 884a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 885a4d4d686SBarry Smith .ve 886a4d4d686SBarry Smith 887a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 888a4d4d686SBarry Smith 8895a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 8905a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 8915a655dc6SBarry Smith MatSNESMFKSPMonitor() 892a4d4d686SBarry Smith @*/ 893329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error) 894a4d4d686SBarry Smith { 8957e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 896a4d4d686SBarry Smith 897a4d4d686SBarry Smith PetscFunctionBegin; 898a4d4d686SBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 899a4d4d686SBarry Smith PetscFunctionReturn(0); 900a4d4d686SBarry Smith } 901a4d4d686SBarry Smith 9024a2ae208SSatish Balay #undef __FUNCT__ 9034a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace" 904a4d4d686SBarry Smith /*@ 90565f2ba5bSLois Curfman McInnes MatSNESMFAddNullSpace - Provides a null space that an operator is 90665f2ba5bSLois Curfman McInnes supposed to have. Since roundoff will create a small component in 90765f2ba5bSLois Curfman McInnes the null space, if you know the null space you may have it 90865f2ba5bSLois Curfman McInnes automatically removed. 909a4d4d686SBarry Smith 910a4d4d686SBarry Smith Collective on Mat 911a4d4d686SBarry Smith 912a4d4d686SBarry Smith Input Parameters: 913a4d4d686SBarry Smith + J - the matrix-free matrix context 91474637425SBarry Smith - nullsp - object created with MatNullSpaceCreate() 915a4d4d686SBarry Smith 91615091d37SBarry Smith Level: advanced 91715091d37SBarry Smith 918a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space 919a4d4d686SBarry Smith 92074637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(), 9215a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 9225a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFErrorRel() 923a4d4d686SBarry Smith @*/ 92474637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp) 925a4d4d686SBarry Smith { 926a4d4d686SBarry Smith int ierr; 9277e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 928a4d4d686SBarry Smith MPI_Comm comm; 929a4d4d686SBarry Smith 930a4d4d686SBarry Smith PetscFunctionBegin; 9312d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 932a4d4d686SBarry Smith 93385614651SBarry Smith ctx->sp = nullsp; 93485614651SBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 935a4d4d686SBarry Smith PetscFunctionReturn(0); 936a4d4d686SBarry Smith } 937a4d4d686SBarry Smith 9384a2ae208SSatish Balay #undef __FUNCT__ 9394a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory" 940a4d4d686SBarry Smith /*@ 94165f2ba5bSLois Curfman McInnes MatSNESMFSetHHistory - Sets an array to collect a history of the 94265f2ba5bSLois Curfman McInnes differencing values (h) computed for the matrix-free product. 943a4d4d686SBarry Smith 944a4d4d686SBarry Smith Collective on Mat 945a4d4d686SBarry Smith 946a4d4d686SBarry Smith Input Parameters: 947a4d4d686SBarry Smith + J - the matrix-free matrix context 94865f2ba5bSLois Curfman McInnes . histroy - space to hold the history 94965f2ba5bSLois Curfman McInnes - nhistory - number of entries in history, if more entries are generated than 95065f2ba5bSLois Curfman McInnes nhistory, then the later ones are discarded 951a4d4d686SBarry Smith 95215091d37SBarry Smith Level: advanced 95315091d37SBarry Smith 954a4d4d686SBarry Smith Notes: 95565f2ba5bSLois Curfman McInnes Use MatSNESMFResetHHistory() to reset the history counter and collect 95665f2ba5bSLois Curfman McInnes a new batch of differencing parameters, h. 957a4d4d686SBarry Smith 958a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 959a4d4d686SBarry Smith 9605a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 9615a655dc6SBarry Smith MatSNESMFResetHHistory(), 9625a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 963a4d4d686SBarry Smith 964a4d4d686SBarry Smith @*/ 96587828ca2SBarry Smith int MatSNESMFSetHHistory(Mat J,PetscScalar *history,int nhistory) 966a4d4d686SBarry Smith { 9677e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 968a4d4d686SBarry Smith 969a4d4d686SBarry Smith PetscFunctionBegin; 970a4d4d686SBarry Smith ctx->historyh = history; 971a4d4d686SBarry Smith ctx->maxcurrenth = nhistory; 972a4d4d686SBarry Smith ctx->currenth = 0; 973a4d4d686SBarry Smith PetscFunctionReturn(0); 974a4d4d686SBarry Smith } 975a4d4d686SBarry Smith 9764a2ae208SSatish Balay #undef __FUNCT__ 9774a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory" 978a4d4d686SBarry Smith /*@ 9795a655dc6SBarry Smith MatSNESMFResetHHistory - Resets the counter to zero to begin 980a4d4d686SBarry Smith collecting a new set of differencing histories. 981a4d4d686SBarry Smith 982a4d4d686SBarry Smith Collective on Mat 983a4d4d686SBarry Smith 984a4d4d686SBarry Smith Input Parameters: 985a4d4d686SBarry Smith . J - the matrix-free matrix context 986a4d4d686SBarry Smith 98715091d37SBarry Smith Level: advanced 98815091d37SBarry Smith 989a4d4d686SBarry Smith Notes: 99065f2ba5bSLois Curfman McInnes Use MatSNESMFSetHHistory() to create the original history counter. 991a4d4d686SBarry Smith 992a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 993a4d4d686SBarry Smith 9945a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 9955a655dc6SBarry Smith MatSNESMFSetHHistory(), 9965a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 997a4d4d686SBarry Smith 998a4d4d686SBarry Smith @*/ 9995a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J) 1000a4d4d686SBarry Smith { 10017e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 1002a4d4d686SBarry Smith 1003a4d4d686SBarry Smith PetscFunctionBegin; 1004be726c96SBarry Smith ctx->ncurrenth = 0; 1005a4d4d686SBarry Smith PetscFunctionReturn(0); 1006a4d4d686SBarry Smith } 1007a4d4d686SBarry Smith 10084a2ae208SSatish Balay #undef __FUNCT__ 1009fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian" 1010fed8bd04SBarry Smith int MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 10111d1367b7SBarry Smith { 10121d1367b7SBarry Smith int ierr; 10131d1367b7SBarry Smith PetscFunctionBegin; 10141d1367b7SBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10151d1367b7SBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 10161d1367b7SBarry Smith PetscFunctionReturn(0); 10171d1367b7SBarry Smith } 10181d1367b7SBarry Smith 10194a2ae208SSatish Balay #undef __FUNCT__ 10204a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase" 10211d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U) 10221d1367b7SBarry Smith { 10233a7fca6bSBarry Smith int ierr,(*f)(Mat,Vec); 10241d1367b7SBarry Smith 10251d1367b7SBarry Smith PetscFunctionBegin; 10261d1367b7SBarry Smith PetscValidHeaderSpecific(J,MAT_COOKIE); 10271d1367b7SBarry Smith PetscValidHeaderSpecific(U,VEC_COOKIE); 1028cf3bea43SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)())&f);CHKERRQ(ierr); 1029cf3bea43SBarry Smith if (f) { 1030cf3bea43SBarry Smith ierr = (*f)(J,U);CHKERRQ(ierr); 103149d4803aSBarry Smith } 10321d1367b7SBarry Smith PetscFunctionReturn(0); 10331d1367b7SBarry Smith } 1034cf57b110SBarry Smith 1035cf57b110SBarry Smith 1036cf57b110SBarry Smith 1037cf57b110SBarry Smith 1038cf57b110SBarry Smith 1039cf57b110SBarry Smith 1040cf57b110SBarry Smith 1041cf57b110SBarry Smith 1042cf57b110SBarry Smith 1043