1*8a124369SBarry Smith /*$Id: snesmfj.c,v 1.126 2001/07/17 20:26:03 bsmith 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 659a6cb015SBarry Smith /*MC 66f1af5d2fSBarry Smith MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry. 679a6cb015SBarry Smith 689a6cb015SBarry Smith Synopsis: 69fed8bd04SBarry Smith int MatSNESMFRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF)) 709a6cb015SBarry Smith 719a6cb015SBarry Smith Not Collective 729a6cb015SBarry Smith 739a6cb015SBarry Smith Input Parameters: 749a6cb015SBarry Smith + name_solver - name of a new user-defined compute-h module 759a6cb015SBarry Smith . path - path (either absolute or relative) the library containing this solver 769a6cb015SBarry Smith . name_create - name of routine to create method context 779a6cb015SBarry Smith - routine_create - routine to create method context 789a6cb015SBarry Smith 7915091d37SBarry Smith Level: developer 8015091d37SBarry Smith 819a6cb015SBarry Smith Notes: 82f1af5d2fSBarry Smith MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers. 839a6cb015SBarry Smith 849a6cb015SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 859a6cb015SBarry Smith is ignored. 869a6cb015SBarry Smith 879a6cb015SBarry Smith Sample usage: 889a6cb015SBarry Smith .vb 89f1af5d2fSBarry Smith MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a, 909a6cb015SBarry Smith "MyHCreate",MyHCreate); 919a6cb015SBarry Smith .ve 929a6cb015SBarry Smith 939a6cb015SBarry Smith Then, your solver can be chosen with the procedural interface via 945a655dc6SBarry Smith $ MatSNESMFSetType(mfctx,"my_h") 959a6cb015SBarry Smith or at runtime via the option 969a6cb015SBarry Smith $ -snes_mf_type my_h 979a6cb015SBarry Smith 985a655dc6SBarry Smith .keywords: MatSNESMF, register 999a6cb015SBarry Smith 1005a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy() 1019a6cb015SBarry Smith M*/ 1029a6cb015SBarry Smith 1034a2ae208SSatish Balay #undef __FUNCT__ 1044a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister" 105f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx)) 1069a6cb015SBarry Smith { 1079a6cb015SBarry Smith int ierr; 1089a6cb015SBarry Smith char fullname[256]; 1099a6cb015SBarry Smith 1109a6cb015SBarry Smith PetscFunctionBegin; 111b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 112b9617806SBarry Smith ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)())function);CHKERRQ(ierr); 1139a6cb015SBarry Smith PetscFunctionReturn(0); 1149a6cb015SBarry Smith } 1159a6cb015SBarry Smith 1169a6cb015SBarry Smith 1174a2ae208SSatish Balay #undef __FUNCT__ 1184a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy" 1199a6cb015SBarry Smith /*@C 1205a655dc6SBarry Smith MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were 121f1af5d2fSBarry Smith registered by MatSNESMFRegisterDynamic). 1229a6cb015SBarry Smith 1239a6cb015SBarry Smith Not Collective 1249a6cb015SBarry Smith 12515091d37SBarry Smith Level: developer 12615091d37SBarry Smith 1275a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy 1289a6cb015SBarry Smith 129f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll() 1309a6cb015SBarry Smith @*/ 1315a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void) 1329a6cb015SBarry Smith { 1339a6cb015SBarry Smith int ierr; 1349a6cb015SBarry Smith 1359a6cb015SBarry Smith PetscFunctionBegin; 136b0a32e0cSBarry Smith if (MatSNESMPetscFList) { 137b0a32e0cSBarry Smith ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr); 138b0a32e0cSBarry Smith MatSNESMPetscFList = 0; 1399a6cb015SBarry Smith } 1404c49b128SBarry Smith MatSNESMFRegisterAllCalled = PETSC_FALSE; 1419a6cb015SBarry Smith PetscFunctionReturn(0); 1429a6cb015SBarry Smith } 1439a6cb015SBarry Smith 1449a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/ 1454a2ae208SSatish Balay #undef __FUNCT__ 146*8a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD" 147*8a124369SBarry Smith int MatDestroy_MFFD(Mat mat) 148a4d4d686SBarry Smith { 149a4d4d686SBarry Smith int ierr; 1507e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 151fae171e0SBarry Smith 1523a40ed3dSBarry Smith PetscFunctionBegin; 153b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 1549a6cb015SBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 15574637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 1566831982aSBarry Smith PetscHeaderDestroy(ctx); 1573a40ed3dSBarry Smith PetscFunctionReturn(0); 158b9fa9cd0SBarry Smith } 15950361f65SLois Curfman McInnes 1604a2ae208SSatish Balay #undef __FUNCT__ 161*8a124369SBarry Smith #define __FUNCT__ "MatView_MFFD" 16239e2f89bSBarry Smith /* 163*8a124369SBarry Smith MatSNESMFView_MFFD - Views matrix-free parameters. 1648f6e3e37SBarry Smith 16539e2f89bSBarry Smith */ 166*8a124369SBarry Smith int MatView_MFFD(Mat J,PetscViewer viewer) 167eb9086c3SLois Curfman McInnes { 168eb9086c3SLois Curfman McInnes int ierr; 1697e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 1706831982aSBarry Smith PetscTruth isascii; 171eb9086c3SLois Curfman McInnes 1723a40ed3dSBarry Smith PetscFunctionBegin; 173b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 1740f5bd95cSBarry Smith if (isascii) { 175b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," SNES matrix-free approximation:\n");CHKERRQ(ierr); 176b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 177473c83c3SBarry Smith if (!ctx->type_name) { 178b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," The compute h routine has not yet been set\n");CHKERRQ(ierr); 179473c83c3SBarry Smith } else { 180b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr); 181473c83c3SBarry Smith } 1829a6cb015SBarry Smith if (ctx->ops->view) { 1839a6cb015SBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 1849a6cb015SBarry Smith } 1855cd90555SBarry Smith } else { 18629bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name); 187eb9086c3SLois Curfman McInnes } 1883a40ed3dSBarry Smith PetscFunctionReturn(0); 189eb9086c3SLois Curfman McInnes } 190eb9086c3SLois Curfman McInnes 1914a2ae208SSatish Balay #undef __FUNCT__ 192*8a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD" 193be726c96SBarry Smith /* 1945a655dc6SBarry Smith MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This 19565f2ba5bSLois Curfman McInnes allows the user to indicate the beginning of a new linear solve by calling 196be726c96SBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 19765f2ba5bSLois Curfman McInnes MatSNESMFCreate_WP() to properly compute ||U|| only the first time 19865f2ba5bSLois Curfman McInnes in the linear solver rather than every time. 199be726c96SBarry Smith */ 200*8a124369SBarry Smith int MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt) 201be726c96SBarry Smith { 202be726c96SBarry Smith int ierr; 2037e9d5209SBarry Smith MatSNESMFCtx j = (MatSNESMFCtx)J->data; 20465df01d8SBarry Smith SNESProblemType type; 205be726c96SBarry Smith 206be726c96SBarry Smith PetscFunctionBegin; 2075a655dc6SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 208b0a32e0cSBarry Smith if (j->usesnes) { 2091d1367b7SBarry Smith ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr); 21065df01d8SBarry Smith ierr = SNESGetProblemType(j->snes,&type);CHKERRQ(ierr); 21165df01d8SBarry Smith if (type == SNES_NONLINEAR_EQUATIONS) { 2121d1367b7SBarry Smith ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 21365df01d8SBarry Smith } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 2141d1367b7SBarry Smith ierr = SNESGetGradient(j->snes,&j->current_f,PETSC_NULL);CHKERRQ(ierr); 21529bbc08cSBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class"); 2161d1367b7SBarry Smith } 217be726c96SBarry Smith PetscFunctionReturn(0); 218be726c96SBarry Smith } 219be726c96SBarry Smith 2204a2ae208SSatish Balay #undef __FUNCT__ 221*8a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD" 222eb9086c3SLois Curfman McInnes /* 2235a655dc6SBarry Smith MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector 224eb9086c3SLois Curfman McInnes product, y = F'(u)*a: 225a4d4d686SBarry Smith 2269a6cb015SBarry Smith y ~= (F(u + ha) - F(u))/h, 227eb9086c3SLois Curfman McInnes where F = nonlinear function, as set by SNESSetFunction() 228eb9086c3SLois Curfman McInnes u = current iterate 229eb9086c3SLois Curfman McInnes h = difference interval 230eb9086c3SLois Curfman McInnes */ 231*8a124369SBarry Smith int MatMult_MFFD(Mat mat,Vec a,Vec y) 23239e2f89bSBarry Smith { 2337e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 234fae171e0SBarry Smith SNES snes; 235a4d4d686SBarry Smith Scalar h,mone = -1.0; 236fae171e0SBarry Smith Vec w,U,F; 237a305c92eSSatish Balay int ierr,(*eval_fct)(SNES,Vec,Vec)=0; 23865df01d8SBarry Smith SNESProblemType type; 23939e2f89bSBarry Smith 2403a40ed3dSBarry Smith PetscFunctionBegin; 2419a6cb015SBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 2429a6cb015SBarry Smith separate the performance monitoring from the cases that use conventional 2439a6cb015SBarry Smith storage. We may eventually modify event logging to associate events 2449a6cb015SBarry Smith with particular objects, hence alleviating the more general problem. */ 245b0a32e0cSBarry Smith ierr = PetscLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr); 24656cd22aeSBarry Smith 247fae171e0SBarry Smith snes = ctx->snes; 248fae171e0SBarry Smith w = ctx->w; 2491d1367b7SBarry Smith U = ctx->current_u; 25050361f65SLois Curfman McInnes 25185614651SBarry Smith /* 25285614651SBarry Smith Compute differencing parameter 25385614651SBarry Smith */ 2549a6cb015SBarry Smith if (!ctx->ops->compute) { 255b7fd4e64SBarry Smith ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr); 2565a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr); 2579a6cb015SBarry Smith } 2589a6cb015SBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr); 259a4d4d686SBarry Smith 260a4d4d686SBarry Smith /* keep a record of the current differencing parameter h */ 261a4d4d686SBarry Smith ctx->currenth = h; 262aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 263*8a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h)); 264a4d4d686SBarry Smith #else 265*8a124369SBarry Smith PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h); 266a4d4d686SBarry Smith #endif 267a4d4d686SBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 26885614651SBarry Smith ctx->historyh[ctx->ncurrenth] = h; 269a4d4d686SBarry Smith } 27085614651SBarry Smith ctx->ncurrenth++; 271a4d4d686SBarry Smith 27285614651SBarry Smith /* w = u + ha */ 273a4d4d686SBarry Smith ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr); 27485614651SBarry Smith 275b0a32e0cSBarry Smith if (ctx->usesnes) { 27665df01d8SBarry Smith ierr = SNESGetProblemType(snes,&type);CHKERRQ(ierr); 27765df01d8SBarry Smith if (type == SNES_NONLINEAR_EQUATIONS) { 27885614651SBarry Smith eval_fct = SNESComputeFunction; 27965df01d8SBarry Smith } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 28085614651SBarry Smith eval_fct = SNESComputeGradient; 28129bbc08cSBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class"); 2821d1367b7SBarry Smith F = ctx->current_f; 28329bbc08cSBarry Smith if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices"); 28439903ad8SBarry Smith ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr); 28585614651SBarry Smith } else { 28685614651SBarry Smith F = ctx->funcvec; 28785614651SBarry Smith /* compute func(U) as base for differencing */ 28885614651SBarry Smith if (ctx->ncurrenth == 1) { 28985614651SBarry Smith ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr); 29085614651SBarry Smith } 29185614651SBarry Smith ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr); 29285614651SBarry Smith } 293a4d4d686SBarry Smith 294a4d4d686SBarry Smith ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr); 295a4d4d686SBarry Smith h = 1.0/h; 296a4d4d686SBarry Smith ierr = VecScale(&h,y);CHKERRQ(ierr); 29774637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 298a4d4d686SBarry Smith 299b0a32e0cSBarry Smith ierr = PetscLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr); 300a4d4d686SBarry Smith PetscFunctionReturn(0); 301a4d4d686SBarry Smith } 302a4d4d686SBarry Smith 3034a2ae208SSatish Balay #undef __FUNCT__ 304*8a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD" 305cf57b110SBarry Smith /* 306*8a124369SBarry Smith MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix 307cf57b110SBarry Smith 308cf57b110SBarry Smith y ~= (F(u + ha) - F(u))/h, 309cf57b110SBarry Smith where F = nonlinear function, as set by SNESSetFunction() 310cf57b110SBarry Smith u = current iterate 311cf57b110SBarry Smith h = difference interval 312cf57b110SBarry Smith */ 313*8a124369SBarry Smith int MatGetDiagonal_MFFD(Mat mat,Vec a) 314cf57b110SBarry Smith { 3157e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 31665df01d8SBarry Smith Scalar h,*aa,*ww,v; 31765df01d8SBarry Smith double epsilon = 1.e-8,umin = 1.e-6; 31865df01d8SBarry Smith Vec w,U; 319cf57b110SBarry Smith int i,ierr,rstart,rend; 320cf57b110SBarry Smith 321cf57b110SBarry Smith PetscFunctionBegin; 322cf57b110SBarry Smith if (!ctx->funci) { 323cf57b110SBarry Smith SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first"); 324cf57b110SBarry Smith } 325cf57b110SBarry Smith 326cf57b110SBarry Smith w = ctx->w; 327cf57b110SBarry Smith U = ctx->current_u; 328cf57b110SBarry Smith ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr); 329cf57b110SBarry Smith ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr); 330cf57b110SBarry Smith ierr = VecCopy(U,w);CHKERRQ(ierr); 331cf57b110SBarry Smith 332cf57b110SBarry Smith ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr); 333cf57b110SBarry Smith ierr = VecGetArray(a,&aa);CHKERRQ(ierr); 334cf57b110SBarry Smith for (i=rstart; i<rend; i++) { 335cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 336cf57b110SBarry Smith h = ww[i-rstart]; 337cf57b110SBarry Smith if (h == 0.0) h = 1.0; 338cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX) 339cf57b110SBarry Smith if (h < umin && h >= 0.0) h = umin; 340cf57b110SBarry Smith else if (h < 0.0 && h > -umin) h = -umin; 341cf57b110SBarry Smith #else 342cf57b110SBarry Smith if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin; 343cf57b110SBarry Smith else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin; 344cf57b110SBarry Smith #endif 345cf57b110SBarry Smith h *= epsilon; 346cf57b110SBarry Smith 347cf57b110SBarry Smith ww[i-rstart] += h; 348cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 349cf57b110SBarry Smith ierr = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr); 350cf57b110SBarry Smith aa[i-rstart] = (v - aa[i-rstart])/h; 351cf57b110SBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 352cf57b110SBarry Smith ww[i-rstart] -= h; 353cf57b110SBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 354cf57b110SBarry Smith } 355cf57b110SBarry Smith ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr); 356cf57b110SBarry Smith PetscFunctionReturn(0); 357cf57b110SBarry Smith } 358cf57b110SBarry Smith 359cf57b110SBarry Smith #undef __FUNCT__ 3604a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF" 361a4d4d686SBarry Smith /*@C 36265f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 36365f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 36465f2ba5bSLois Curfman McInnes the routine SNESSetJacobian(). 365a4d4d686SBarry Smith 366a4d4d686SBarry Smith Collective on SNES and Vec 367a4d4d686SBarry Smith 368a4d4d686SBarry Smith Input Parameters: 369a4d4d686SBarry Smith + snes - the SNES context 370a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 371a4d4d686SBarry Smith 372a4d4d686SBarry Smith Output Parameter: 373a4d4d686SBarry Smith . J - the matrix-free matrix 374a4d4d686SBarry Smith 37515091d37SBarry Smith Level: advanced 37615091d37SBarry Smith 377a4d4d686SBarry Smith Notes: 378a4d4d686SBarry Smith The matrix-free matrix context merely contains the function pointers 379a4d4d686SBarry Smith and work space for performing finite difference approximations of 38065f2ba5bSLois Curfman McInnes Jacobian-vector products, F'(u)*a, 3819a6cb015SBarry Smith 3829a6cb015SBarry Smith The default code uses the following approach to compute h 383a4d4d686SBarry Smith 384a4d4d686SBarry Smith .vb 38565f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 386a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 387a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 388a4d4d686SBarry Smith where 389a4d4d686SBarry Smith error_rel = square root of relative error in function evaluation 390a4d4d686SBarry Smith umin = minimum iterate parameter 391a4d4d686SBarry Smith .ve 392a4d4d686SBarry Smith 3935a655dc6SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 39465f2ba5bSLois Curfman McInnes umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 39565f2ba5bSLois Curfman McInnes of the users manual for details. 396a4d4d686SBarry Smith 397a4d4d686SBarry Smith The user should call MatDestroy() when finished with the matrix-free 398a4d4d686SBarry Smith matrix context. 399a4d4d686SBarry Smith 400a4d4d686SBarry Smith Options Database Keys: 401a4d4d686SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 4029a6cb015SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 403a4d4d686SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 404a4d4d686SBarry Smith 405a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix 406a4d4d686SBarry Smith 4075a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 4081d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(), 409fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian() 410a4d4d686SBarry Smith 411a4d4d686SBarry Smith @*/ 4125a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J) 413a4d4d686SBarry Smith { 4141d1367b7SBarry Smith MatSNESMFCtx mfctx; 4151d1367b7SBarry Smith int ierr; 4161d1367b7SBarry Smith 4171d1367b7SBarry Smith PetscFunctionBegin; 4181d1367b7SBarry Smith ierr = MatCreateMF(x,J);CHKERRQ(ierr); 4197e9d5209SBarry Smith 4207e9d5209SBarry Smith mfctx = (MatSNESMFCtx)(*J)->data; 4211d1367b7SBarry Smith mfctx->snes = snes; 422b0a32e0cSBarry Smith mfctx->usesnes = PETSC_TRUE; 423b0a32e0cSBarry Smith PetscLogObjectParent(snes,*J); 4241d1367b7SBarry Smith PetscFunctionReturn(0); 4251d1367b7SBarry Smith } 4261d1367b7SBarry Smith 427cf3bea43SBarry Smith EXTERN_C_BEGIN 428cf3bea43SBarry Smith #undef __FUNCT__ 429cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD" 430cf3bea43SBarry Smith int MatSNESMFSetBase_FD(Mat J,Vec U) 431cf3bea43SBarry Smith { 432cf3bea43SBarry Smith int ierr; 4337e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 434cf3bea43SBarry Smith 435cf3bea43SBarry Smith PetscFunctionBegin; 436cf3bea43SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 437cf3bea43SBarry Smith ctx->current_u = U; 438cf3bea43SBarry Smith ctx->usesnes = PETSC_FALSE; 439cf3bea43SBarry Smith PetscFunctionReturn(0); 440cf3bea43SBarry Smith } 441cf3bea43SBarry Smith EXTERN_C_END 442cf3bea43SBarry Smith 4434a2ae208SSatish Balay #undef __FUNCT__ 4447e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions" 4457e9d5209SBarry Smith /*@ 4467e9d5209SBarry Smith MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line 4477e9d5209SBarry Smith parameter. 4487e9d5209SBarry Smith 4497e9d5209SBarry Smith Collective on Mat 4507e9d5209SBarry Smith 4517e9d5209SBarry Smith Input Parameters: 4527e9d5209SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 4537e9d5209SBarry Smith 4547e9d5209SBarry Smith Options Database Keys: 4557e9d5209SBarry Smith + -snes_mf_type - <default,wp> 4567e9d5209SBarry Smith - -snes_mf_err - square root of estimated relative error in function evaluation 4577e9d5209SBarry Smith - -snes_mf_period - how often h is recomputed, defaults to 1, everytime 4587e9d5209SBarry Smith 4597e9d5209SBarry Smith Level: advanced 4607e9d5209SBarry Smith 4617e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters 4627e9d5209SBarry Smith 4637e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4647e9d5209SBarry Smith MatSNESMFResetHHistory(), MatSNESMFKSPMonitor() 4657e9d5209SBarry Smith @*/ 4667e9d5209SBarry Smith int MatSNESMFSetFromOptions(Mat mat) 4677e9d5209SBarry Smith { 4687e9d5209SBarry Smith MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data; 4697e9d5209SBarry Smith int ierr; 4707e9d5209SBarry Smith PetscTruth flg; 4717e9d5209SBarry Smith char ftype[256]; 4727e9d5209SBarry Smith 4737e9d5209SBarry Smith PetscFunctionBegin; 4747e9d5209SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 4757e9d5209SBarry Smith 4767e9d5209SBarry Smith ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr); 4777e9d5209SBarry Smith ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr); 4787e9d5209SBarry Smith if (flg) { 4797e9d5209SBarry Smith ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr); 4807e9d5209SBarry Smith } 4817e9d5209SBarry Smith 4827e9d5209SBarry Smith ierr = PetscOptionsDouble("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr); 4837e9d5209SBarry Smith ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr); 4847e9d5209SBarry Smith if (mfctx->snes) { 4857e9d5209SBarry Smith ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr); 4867e9d5209SBarry Smith if (flg) { 4877e9d5209SBarry Smith SLES sles; 4887e9d5209SBarry Smith KSP ksp; 4897e9d5209SBarry Smith ierr = SNESGetSLES(mfctx->snes,&sles);CHKERRQ(ierr); 4907e9d5209SBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 4917e9d5209SBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 4927e9d5209SBarry Smith } 4937e9d5209SBarry Smith } 4947e9d5209SBarry Smith if (mfctx->ops->setfromoptions) { 4957e9d5209SBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 4967e9d5209SBarry Smith } 4977e9d5209SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 4987e9d5209SBarry Smith PetscFunctionReturn(0); 4997e9d5209SBarry Smith } 5007e9d5209SBarry Smith 5017e9d5209SBarry Smith #undef __FUNCT__ 5027e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD" 5037e9d5209SBarry Smith EXTERN_C_BEGIN 5047e9d5209SBarry Smith int MatCreate_MFFD(Mat A) 5057e9d5209SBarry Smith { 5067e9d5209SBarry Smith MatSNESMFCtx mfctx; 50765df01d8SBarry Smith int ierr; 5087e9d5209SBarry Smith 5097e9d5209SBarry Smith PetscFunctionBegin; 510*8a124369SBarry Smith PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD); 5117e9d5209SBarry Smith PetscLogObjectCreate(mfctx); 5127e9d5209SBarry Smith mfctx->sp = 0; 5137e9d5209SBarry Smith mfctx->snes = 0; 5147e9d5209SBarry Smith mfctx->error_rel = 1.e-8; /* assumes PetscReal precision */ 5157e9d5209SBarry Smith mfctx->recomputeperiod = 1; 5167e9d5209SBarry Smith mfctx->count = 0; 5177e9d5209SBarry Smith mfctx->currenth = 0.0; 5187e9d5209SBarry Smith mfctx->historyh = PETSC_NULL; 5197e9d5209SBarry Smith mfctx->ncurrenth = 0; 5207e9d5209SBarry Smith mfctx->maxcurrenth = 0; 5217e9d5209SBarry Smith mfctx->type_name = 0; 5227e9d5209SBarry Smith mfctx->usesnes = PETSC_FALSE; 5237e9d5209SBarry Smith 5247e9d5209SBarry Smith /* 5257e9d5209SBarry Smith Create the empty data structure to contain compute-h routines. 5267e9d5209SBarry Smith These will be filled in below from the command line options or 5277e9d5209SBarry Smith a later call with MatSNESMFSetType() or if that is not called 528*8a124369SBarry Smith then it will default in the first use of MatMult_MFFD() 5297e9d5209SBarry Smith */ 5307e9d5209SBarry Smith mfctx->ops->compute = 0; 5317e9d5209SBarry Smith mfctx->ops->destroy = 0; 5327e9d5209SBarry Smith mfctx->ops->view = 0; 5337e9d5209SBarry Smith mfctx->ops->setfromoptions = 0; 5347e9d5209SBarry Smith mfctx->hctx = 0; 5357e9d5209SBarry Smith 5367e9d5209SBarry Smith mfctx->func = 0; 5377e9d5209SBarry Smith mfctx->funcctx = 0; 5387e9d5209SBarry Smith mfctx->funcvec = 0; 5397e9d5209SBarry Smith 54065df01d8SBarry Smith A->data = mfctx; 5417e9d5209SBarry Smith 542*8a124369SBarry Smith A->ops->mult = MatMult_MFFD; 543*8a124369SBarry Smith A->ops->destroy = MatDestroy_MFFD; 544*8a124369SBarry Smith A->ops->view = MatView_MFFD; 545*8a124369SBarry Smith A->ops->assemblyend = MatAssemblyEnd_MFFD; 546*8a124369SBarry Smith A->ops->getdiagonal = MatGetDiagonal_MFFD; 54765df01d8SBarry Smith A->ops->setfromoptions = MatSNESMFSetFromOptions; 5487e9d5209SBarry Smith 54965df01d8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr); 55065df01d8SBarry Smith mfctx->mat = A; 55165df01d8SBarry Smith ierr = VecCreateMPI(A->comm,A->n,A->N,&mfctx->w);CHKERRQ(ierr); 5527e9d5209SBarry Smith 5537e9d5209SBarry Smith PetscFunctionReturn(0); 5547e9d5209SBarry Smith } 5557e9d5209SBarry Smith 5567e9d5209SBarry Smith EXTERN_C_END 5577e9d5209SBarry Smith 5587e9d5209SBarry Smith #undef __FUNCT__ 5594a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF" 5601d1367b7SBarry Smith /*@C 5611d1367b7SBarry Smith MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF() 5621d1367b7SBarry Smith 5631d1367b7SBarry Smith Collective on Vec 5641d1367b7SBarry Smith 5651d1367b7SBarry Smith Input Parameters: 5661d1367b7SBarry Smith . x - vector that defines layout of the vectors and matrices 5671d1367b7SBarry Smith 5681d1367b7SBarry Smith Output Parameter: 5691d1367b7SBarry Smith . J - the matrix-free matrix 5701d1367b7SBarry Smith 5711d1367b7SBarry Smith Level: advanced 5721d1367b7SBarry Smith 5731d1367b7SBarry Smith Notes: 5741d1367b7SBarry Smith The matrix-free matrix context merely contains the function pointers 5751d1367b7SBarry Smith and work space for performing finite difference approximations of 5761d1367b7SBarry Smith Jacobian-vector products, F'(u)*a, 5771d1367b7SBarry Smith 5781d1367b7SBarry Smith The default code uses the following approach to compute h 5791d1367b7SBarry Smith 5801d1367b7SBarry Smith .vb 5811d1367b7SBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 5821d1367b7SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 5831d1367b7SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 5841d1367b7SBarry Smith where 5851d1367b7SBarry Smith error_rel = square root of relative error in function evaluation 5861d1367b7SBarry Smith umin = minimum iterate parameter 5871d1367b7SBarry Smith .ve 5881d1367b7SBarry Smith 5891d1367b7SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 5901d1367b7SBarry Smith umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 5911d1367b7SBarry Smith of the users manual for details. 5921d1367b7SBarry Smith 5931d1367b7SBarry Smith The user should call MatDestroy() when finished with the matrix-free 5941d1367b7SBarry Smith matrix context. 5951d1367b7SBarry Smith 5961d1367b7SBarry Smith Options Database Keys: 5971d1367b7SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 5981d1367b7SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 5991d1367b7SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 6001d1367b7SBarry Smith 6011d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix 6021d1367b7SBarry Smith 6031d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 6041d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(), 605fed8bd04SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian() 6061d1367b7SBarry Smith 6071d1367b7SBarry Smith @*/ 6081d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J) 6091d1367b7SBarry Smith { 610a4d4d686SBarry Smith MPI_Comm comm; 6119a6cb015SBarry Smith int n,nloc,ierr; 612a4d4d686SBarry Smith 613a4d4d686SBarry Smith PetscFunctionBegin; 6141d1367b7SBarry Smith ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 61565df01d8SBarry Smith ierr = VecGetSize(x,&n);CHKERRQ(ierr); 61665df01d8SBarry Smith ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr); 6177e9d5209SBarry Smith ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr); 61865df01d8SBarry Smith ierr = MatRegister(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr); 61965df01d8SBarry Smith ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr); 6209a6cb015SBarry Smith PetscFunctionReturn(0); 6219a6cb015SBarry Smith } 6229a6cb015SBarry Smith 623a4d4d686SBarry Smith 6244a2ae208SSatish Balay #undef __FUNCT__ 6254a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH" 626a4d4d686SBarry Smith /*@ 62765f2ba5bSLois Curfman McInnes MatSNESMFGetH - Gets the last value that was used as the differencing 628a4d4d686SBarry Smith parameter. 629a4d4d686SBarry Smith 630a4d4d686SBarry Smith Not Collective 631a4d4d686SBarry Smith 632a4d4d686SBarry Smith Input Parameters: 6335a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 634a4d4d686SBarry Smith 635a4d4d686SBarry Smith Output Paramter: 636a4d4d686SBarry Smith . h - the differencing step size 637a4d4d686SBarry Smith 63815091d37SBarry Smith Level: advanced 63915091d37SBarry Smith 640a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 641a4d4d686SBarry Smith 6425a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 6435a655dc6SBarry Smith MatSNESMFResetHHistory(),MatSNESMFKSPMonitor() 644a4d4d686SBarry Smith @*/ 6455a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h) 646a4d4d686SBarry Smith { 6477e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 648a4d4d686SBarry Smith 649a4d4d686SBarry Smith PetscFunctionBegin; 650a4d4d686SBarry Smith *h = ctx->currenth; 651a4d4d686SBarry Smith PetscFunctionReturn(0); 652a4d4d686SBarry Smith } 653a4d4d686SBarry Smith 6544a2ae208SSatish Balay #undef __FUNCT__ 6554a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor" 656a4d4d686SBarry Smith /* 6575a655dc6SBarry Smith MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc 65865f2ba5bSLois Curfman McInnes SNES matrix free routines. Prints the differencing parameter used at 65965f2ba5bSLois Curfman McInnes each step. 660a4d4d686SBarry Smith */ 661329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy) 662a4d4d686SBarry Smith { 663a4d4d686SBarry Smith PC pc; 6645a655dc6SBarry Smith MatSNESMFCtx ctx; 665a4d4d686SBarry Smith int ierr; 666a4d4d686SBarry Smith Mat mat; 667a4d4d686SBarry Smith MPI_Comm comm; 668a4d4d686SBarry Smith PetscTruth nonzeroinitialguess; 669a4d4d686SBarry Smith 670a4d4d686SBarry Smith PetscFunctionBegin; 671a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 672a4d4d686SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 673a4d4d686SBarry Smith ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr); 674a4d4d686SBarry Smith ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 6757e9d5209SBarry Smith ctx = (MatSNESMFCtx)mat->data; 6767e9d5209SBarry Smith 677a4d4d686SBarry Smith if (n > 0 || nonzeroinitialguess) { 678aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 679d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm, 680329f5518SBarry Smith PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr); 681a4d4d686SBarry Smith #else 682d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr); 683a4d4d686SBarry Smith #endif 684a4d4d686SBarry Smith } else { 685d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr); 686a4d4d686SBarry Smith } 687a4d4d686SBarry Smith PetscFunctionReturn(0); 688a4d4d686SBarry Smith } 689a4d4d686SBarry Smith 6904a2ae208SSatish Balay #undef __FUNCT__ 6914a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction" 69285614651SBarry Smith /*@C 69385614651SBarry Smith MatSNESMFSetFunction - Sets the function used in applying the matrix free. 69485614651SBarry Smith 69585614651SBarry Smith Collective on Mat 69685614651SBarry Smith 69785614651SBarry Smith Input Parameters: 69885614651SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 69985614651SBarry Smith . v - workspace vector 70085614651SBarry Smith . func - the function to use 70185614651SBarry Smith - funcctx - optional function context passed to function 70285614651SBarry Smith 70385614651SBarry Smith Level: advanced 70485614651SBarry Smith 70585614651SBarry Smith Notes: 70685614651SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 70785614651SBarry Smith matrix inside your compute Jacobian routine 70885614651SBarry Smith 70985614651SBarry Smith If this is not set then it will use the function set with SNESSetFunction() 71085614651SBarry Smith 71185614651SBarry Smith .keywords: SNES, matrix-free, function 71285614651SBarry Smith 71385614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 71485614651SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 71585614651SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 71685614651SBarry Smith @*/ 71785614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx) 71885614651SBarry Smith { 7197e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 72085614651SBarry Smith 72185614651SBarry Smith PetscFunctionBegin; 72285614651SBarry Smith ctx->func = func; 72385614651SBarry Smith ctx->funcctx = funcctx; 72485614651SBarry Smith ctx->funcvec = v; 72585614651SBarry Smith PetscFunctionReturn(0); 72685614651SBarry Smith } 72785614651SBarry Smith 728cf57b110SBarry Smith #undef __FUNCT__ 729cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni" 730cf57b110SBarry Smith /*@C 731cf57b110SBarry Smith MatSNESMFSetFunctioni - Sets the function for a single component 732cf57b110SBarry Smith 733cf57b110SBarry Smith Collective on Mat 734cf57b110SBarry Smith 735cf57b110SBarry Smith Input Parameters: 736cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 737cf57b110SBarry Smith - funci - the function to use 738cf57b110SBarry Smith 739cf57b110SBarry Smith Level: advanced 740cf57b110SBarry Smith 741cf57b110SBarry Smith Notes: 742cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 743cf57b110SBarry Smith matrix inside your compute Jacobian routine 744cf57b110SBarry Smith 745cf57b110SBarry Smith 746cf57b110SBarry Smith .keywords: SNES, matrix-free, function 747cf57b110SBarry Smith 748cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 749cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 750cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 751cf57b110SBarry Smith @*/ 752cf57b110SBarry Smith int MatSNESMFSetFunctioni(Mat mat,int (*funci)(int,Vec,Scalar*,void *)) 753cf57b110SBarry Smith { 7547e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 755cf57b110SBarry Smith 756cf57b110SBarry Smith PetscFunctionBegin; 757cf57b110SBarry Smith ctx->funci = funci; 758cf57b110SBarry Smith PetscFunctionReturn(0); 759cf57b110SBarry Smith } 760cf57b110SBarry Smith 761cf57b110SBarry Smith #undef __FUNCT__ 762cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase" 763cf57b110SBarry Smith /*@C 764cf57b110SBarry Smith MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation 765cf57b110SBarry Smith 766cf57b110SBarry Smith Collective on Mat 767cf57b110SBarry Smith 768cf57b110SBarry Smith Input Parameters: 769cf57b110SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 770cf57b110SBarry Smith - func - the function to use 771cf57b110SBarry Smith 772cf57b110SBarry Smith Level: advanced 773cf57b110SBarry Smith 774cf57b110SBarry Smith Notes: 775cf57b110SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 776cf57b110SBarry Smith matrix inside your compute Jacobian routine 777cf57b110SBarry Smith 778cf57b110SBarry Smith 779cf57b110SBarry Smith .keywords: SNES, matrix-free, function 780cf57b110SBarry Smith 781cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 782cf57b110SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 783cf57b110SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 784cf57b110SBarry Smith @*/ 785cf57b110SBarry Smith int MatSNESMFSetFunctioniBase(Mat mat,int (*func)(Vec,void *)) 786cf57b110SBarry Smith { 7877e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 788cf57b110SBarry Smith 789cf57b110SBarry Smith PetscFunctionBegin; 790cf57b110SBarry Smith ctx->funcisetbase = func; 791cf57b110SBarry Smith PetscFunctionReturn(0); 792cf57b110SBarry Smith } 793cf57b110SBarry Smith 79485614651SBarry Smith 7954a2ae208SSatish Balay #undef __FUNCT__ 7964a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod" 797329f5518SBarry Smith /*@ 798329f5518SBarry Smith MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime 799329f5518SBarry Smith 800329f5518SBarry Smith Collective on Mat 801329f5518SBarry Smith 802329f5518SBarry Smith Input Parameters: 803329f5518SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 804329f5518SBarry Smith - period - 1 for everytime, 2 for every second etc 805329f5518SBarry Smith 806329f5518SBarry Smith Options Database Keys: 807329f5518SBarry Smith + -snes_mf_period <period> 808329f5518SBarry Smith 809329f5518SBarry Smith Level: advanced 810329f5518SBarry Smith 811329f5518SBarry Smith 812329f5518SBarry Smith .keywords: SNES, matrix-free, parameters 813329f5518SBarry Smith 814329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 815329f5518SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 816329f5518SBarry Smith MatSNESMFKSPMonitor() 817329f5518SBarry Smith @*/ 818329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period) 819329f5518SBarry Smith { 8207e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 821329f5518SBarry Smith 822329f5518SBarry Smith PetscFunctionBegin; 823329f5518SBarry Smith ctx->recomputeperiod = period; 824329f5518SBarry Smith PetscFunctionReturn(0); 825329f5518SBarry Smith } 826329f5518SBarry Smith 8274a2ae208SSatish Balay #undef __FUNCT__ 8284a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError" 829a4d4d686SBarry Smith /*@ 8305a655dc6SBarry Smith MatSNESMFSetFunctionError - Sets the error_rel for the approximation of 831a4d4d686SBarry Smith matrix-vector products using finite differences. 832a4d4d686SBarry Smith 833a4d4d686SBarry Smith Collective on Mat 834a4d4d686SBarry Smith 835a4d4d686SBarry Smith Input Parameters: 8365a655dc6SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 8379a6cb015SBarry Smith - error_rel - relative error (should be set to the square root of 838a4d4d686SBarry Smith the relative error in the function evaluations) 839a4d4d686SBarry Smith 84015091d37SBarry Smith Options Database Keys: 84115091d37SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 84215091d37SBarry Smith 84315091d37SBarry Smith Level: advanced 84415091d37SBarry Smith 845a4d4d686SBarry Smith Notes: 846a4d4d686SBarry Smith The default matrix-free matrix-vector product routine computes 847a4d4d686SBarry Smith .vb 84865f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 849a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 850a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 851a4d4d686SBarry Smith .ve 852a4d4d686SBarry Smith 853a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 854a4d4d686SBarry Smith 8555a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 8565a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 8575a655dc6SBarry Smith MatSNESMFKSPMonitor() 858a4d4d686SBarry Smith @*/ 859329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error) 860a4d4d686SBarry Smith { 8617e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data; 862a4d4d686SBarry Smith 863a4d4d686SBarry Smith PetscFunctionBegin; 864a4d4d686SBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 865a4d4d686SBarry Smith PetscFunctionReturn(0); 866a4d4d686SBarry Smith } 867a4d4d686SBarry Smith 8684a2ae208SSatish Balay #undef __FUNCT__ 8694a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace" 870a4d4d686SBarry Smith /*@ 87165f2ba5bSLois Curfman McInnes MatSNESMFAddNullSpace - Provides a null space that an operator is 87265f2ba5bSLois Curfman McInnes supposed to have. Since roundoff will create a small component in 87365f2ba5bSLois Curfman McInnes the null space, if you know the null space you may have it 87465f2ba5bSLois Curfman McInnes automatically removed. 875a4d4d686SBarry Smith 876a4d4d686SBarry Smith Collective on Mat 877a4d4d686SBarry Smith 878a4d4d686SBarry Smith Input Parameters: 879a4d4d686SBarry Smith + J - the matrix-free matrix context 88074637425SBarry Smith - nullsp - object created with MatNullSpaceCreate() 881a4d4d686SBarry Smith 88215091d37SBarry Smith Level: advanced 88315091d37SBarry Smith 884a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space 885a4d4d686SBarry Smith 88674637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(), 8875a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 8885a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFErrorRel() 889a4d4d686SBarry Smith @*/ 89074637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp) 891a4d4d686SBarry Smith { 892a4d4d686SBarry Smith int ierr; 8937e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 894a4d4d686SBarry Smith MPI_Comm comm; 895a4d4d686SBarry Smith 896a4d4d686SBarry Smith PetscFunctionBegin; 8972d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 898a4d4d686SBarry Smith 89985614651SBarry Smith ctx->sp = nullsp; 90085614651SBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 901a4d4d686SBarry Smith PetscFunctionReturn(0); 902a4d4d686SBarry Smith } 903a4d4d686SBarry Smith 9044a2ae208SSatish Balay #undef __FUNCT__ 9054a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory" 906a4d4d686SBarry Smith /*@ 90765f2ba5bSLois Curfman McInnes MatSNESMFSetHHistory - Sets an array to collect a history of the 90865f2ba5bSLois Curfman McInnes differencing values (h) computed for the matrix-free product. 909a4d4d686SBarry Smith 910a4d4d686SBarry Smith Collective on Mat 911a4d4d686SBarry Smith 912a4d4d686SBarry Smith Input Parameters: 913a4d4d686SBarry Smith + J - the matrix-free matrix context 91465f2ba5bSLois Curfman McInnes . histroy - space to hold the history 91565f2ba5bSLois Curfman McInnes - nhistory - number of entries in history, if more entries are generated than 91665f2ba5bSLois Curfman McInnes nhistory, then the later ones are discarded 917a4d4d686SBarry Smith 91815091d37SBarry Smith Level: advanced 91915091d37SBarry Smith 920a4d4d686SBarry Smith Notes: 92165f2ba5bSLois Curfman McInnes Use MatSNESMFResetHHistory() to reset the history counter and collect 92265f2ba5bSLois Curfman McInnes a new batch of differencing parameters, h. 923a4d4d686SBarry Smith 924a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 925a4d4d686SBarry Smith 9265a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 9275a655dc6SBarry Smith MatSNESMFResetHHistory(), 9285a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 929a4d4d686SBarry Smith 930a4d4d686SBarry Smith @*/ 9315a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory) 932a4d4d686SBarry Smith { 9337e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 934a4d4d686SBarry Smith 935a4d4d686SBarry Smith PetscFunctionBegin; 936a4d4d686SBarry Smith ctx->historyh = history; 937a4d4d686SBarry Smith ctx->maxcurrenth = nhistory; 938a4d4d686SBarry Smith ctx->currenth = 0; 939a4d4d686SBarry Smith PetscFunctionReturn(0); 940a4d4d686SBarry Smith } 941a4d4d686SBarry Smith 9424a2ae208SSatish Balay #undef __FUNCT__ 9434a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory" 944a4d4d686SBarry Smith /*@ 9455a655dc6SBarry Smith MatSNESMFResetHHistory - Resets the counter to zero to begin 946a4d4d686SBarry Smith collecting a new set of differencing histories. 947a4d4d686SBarry Smith 948a4d4d686SBarry Smith Collective on Mat 949a4d4d686SBarry Smith 950a4d4d686SBarry Smith Input Parameters: 951a4d4d686SBarry Smith . J - the matrix-free matrix context 952a4d4d686SBarry Smith 95315091d37SBarry Smith Level: advanced 95415091d37SBarry Smith 955a4d4d686SBarry Smith Notes: 95665f2ba5bSLois Curfman McInnes Use MatSNESMFSetHHistory() to create the original history counter. 957a4d4d686SBarry Smith 958a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 959a4d4d686SBarry Smith 9605a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 9615a655dc6SBarry Smith MatSNESMFSetHHistory(), 9625a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 963a4d4d686SBarry Smith 964a4d4d686SBarry Smith @*/ 9655a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J) 966a4d4d686SBarry Smith { 9677e9d5209SBarry Smith MatSNESMFCtx ctx = (MatSNESMFCtx)J->data; 968a4d4d686SBarry Smith 969a4d4d686SBarry Smith PetscFunctionBegin; 970be726c96SBarry Smith ctx->ncurrenth = 0; 971a4d4d686SBarry Smith PetscFunctionReturn(0); 972a4d4d686SBarry Smith } 973a4d4d686SBarry Smith 9744a2ae208SSatish Balay #undef __FUNCT__ 975fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian" 976fed8bd04SBarry Smith int MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 9771d1367b7SBarry Smith { 9781d1367b7SBarry Smith int ierr; 9791d1367b7SBarry Smith PetscFunctionBegin; 9801d1367b7SBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9811d1367b7SBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9821d1367b7SBarry Smith PetscFunctionReturn(0); 9831d1367b7SBarry Smith } 9841d1367b7SBarry Smith 9854a2ae208SSatish Balay #undef __FUNCT__ 9864a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase" 9871d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U) 9881d1367b7SBarry Smith { 9893a7fca6bSBarry Smith int ierr,(*f)(Mat,Vec); 9901d1367b7SBarry Smith 9911d1367b7SBarry Smith PetscFunctionBegin; 9921d1367b7SBarry Smith PetscValidHeaderSpecific(J,MAT_COOKIE); 9931d1367b7SBarry Smith PetscValidHeaderSpecific(U,VEC_COOKIE); 994cf3bea43SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)())&f);CHKERRQ(ierr); 995cf3bea43SBarry Smith if (f) { 996cf3bea43SBarry Smith ierr = (*f)(J,U);CHKERRQ(ierr); 99749d4803aSBarry Smith } 9981d1367b7SBarry Smith PetscFunctionReturn(0); 9991d1367b7SBarry Smith } 1000cf57b110SBarry Smith 1001cf57b110SBarry Smith 1002cf57b110SBarry Smith 1003cf57b110SBarry Smith 1004cf57b110SBarry Smith 1005cf57b110SBarry Smith 1006cf57b110SBarry Smith 1007cf57b110SBarry Smith 1008cf57b110SBarry Smith 1009