1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*aa482453SBarry Smith static char vcid[] = "$Id: snesmfj.c,v 1.86 1999/05/04 20:35:51 balay Exp bsmith $"; 381e6777dSBarry Smith #endif 481e6777dSBarry Smith 59a6cb015SBarry Smith #include "src/snes/snesimpl.h" 69a6cb015SBarry Smith #include "src/snes/mf/snesmfj.h" /*I "snes.h" I*/ 781e6777dSBarry Smith 85a655dc6SBarry Smith FList MatSNESMFList = 0; 95a655dc6SBarry Smith int MatSNESMFRegisterAllCalled = 0; 10a4d4d686SBarry Smith 115615d1e5SSatish Balay #undef __FUNC__ 125a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetType" 13fd4bdd07SBarry Smith /*@C 1465f2ba5bSLois Curfman McInnes MatSNESMFSetType - Sets the method that is used to compute the 1565f2ba5bSLois Curfman McInnes differencing parameter for finite difference matrix-free formulations. 169a6cb015SBarry Smith 179a6cb015SBarry Smith Input Parameters: 18ef4ad1fdSLois Curfman McInnes + mat - the "matrix-free" matrix created via MatCreateSNESMF() 199a6cb015SBarry Smith - ftype - the type requested 209a6cb015SBarry Smith 2115091d37SBarry Smith Level: advanced 2215091d37SBarry Smith 2365f2ba5bSLois Curfman McInnes Notes: 2465f2ba5bSLois Curfman McInnes For example, such routines can compute h for use in 2565f2ba5bSLois Curfman McInnes Jacobian-vector products of the form 2665f2ba5bSLois Curfman McInnes 2765f2ba5bSLois Curfman McInnes F(x+ha) - F(x) 28ef4ad1fdSLois Curfman McInnes F'(u)a ~= ---------------- 2965f2ba5bSLois Curfman McInnes h 3065f2ba5bSLois Curfman McInnes 315a655dc6SBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegister() 329a6cb015SBarry Smith @*/ 335a655dc6SBarry Smith int MatSNESMFSetType(Mat mat,char *ftype) 34b9fa9cd0SBarry Smith { 355a655dc6SBarry Smith int ierr, (*r)(MatSNESMFCtx); 365a655dc6SBarry Smith MatSNESMFCtx ctx; 37a4d4d686SBarry Smith 38a4d4d686SBarry Smith PetscFunctionBegin; 39a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 40a4d4d686SBarry Smith 419a6cb015SBarry Smith /* already set, so just return */ 423f1db9ecSBarry Smith if (PetscTypeCompare(ctx->type_name,ftype)) PetscFunctionReturn(0); 43a4d4d686SBarry Smith 449a6cb015SBarry Smith /* destroy the old one if it exists */ 459a6cb015SBarry Smith if (ctx->ops->destroy) { 469a6cb015SBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 479a6cb015SBarry Smith } 489a6cb015SBarry Smith 4965f2ba5bSLois Curfman McInnes /* Get the function pointers for the requrested method */ 505a655dc6SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 519a6cb015SBarry Smith 525a655dc6SBarry Smith ierr = FListFind(ctx->comm, MatSNESMFList, ftype,(int (**)(void *)) &r );CHKERRQ(ierr); 539a6cb015SBarry Smith 545a655dc6SBarry Smith if (!r) SETERRQ(1,1,"Unknown MatSNESMF type given"); 559a6cb015SBarry Smith 569a6cb015SBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 579a6cb015SBarry Smith ierr = PetscStrncpy(ctx->type_name,ftype,256);CHKERRQ(ierr); 589a6cb015SBarry Smith 599a6cb015SBarry Smith PetscFunctionReturn(0); 609a6cb015SBarry Smith } 619a6cb015SBarry Smith 629a6cb015SBarry Smith /*MC 6365f2ba5bSLois Curfman McInnes MatSNESMFRegister - Adds a method to the MatSNESMF registry. 649a6cb015SBarry Smith 659a6cb015SBarry Smith Synopsis: 665a655dc6SBarry Smith MatSNESMFRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF)) 679a6cb015SBarry Smith 689a6cb015SBarry Smith Not Collective 699a6cb015SBarry Smith 709a6cb015SBarry Smith Input Parameters: 719a6cb015SBarry Smith + name_solver - name of a new user-defined compute-h module 729a6cb015SBarry Smith . path - path (either absolute or relative) the library containing this solver 739a6cb015SBarry Smith . name_create - name of routine to create method context 749a6cb015SBarry Smith - routine_create - routine to create method context 759a6cb015SBarry Smith 7615091d37SBarry Smith Level: developer 7715091d37SBarry Smith 789a6cb015SBarry Smith Notes: 795a655dc6SBarry Smith MatSNESMFRegister() may be called multiple times to add several user-defined solvers. 809a6cb015SBarry Smith 819a6cb015SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 829a6cb015SBarry Smith is ignored. 839a6cb015SBarry Smith 849a6cb015SBarry Smith Sample usage: 859a6cb015SBarry Smith .vb 865a655dc6SBarry Smith MatSNESMFRegister("my_h",/home/username/my_lib/lib/libO/solaris/mylib.a, 879a6cb015SBarry Smith "MyHCreate",MyHCreate); 889a6cb015SBarry Smith .ve 899a6cb015SBarry Smith 909a6cb015SBarry Smith Then, your solver can be chosen with the procedural interface via 915a655dc6SBarry Smith $ MatSNESMFSetType(mfctx,"my_h") 929a6cb015SBarry Smith or at runtime via the option 939a6cb015SBarry Smith $ -snes_mf_type my_h 949a6cb015SBarry Smith 955a655dc6SBarry Smith .keywords: MatSNESMF, register 969a6cb015SBarry Smith 975a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy() 989a6cb015SBarry Smith M*/ 999a6cb015SBarry Smith 1009a6cb015SBarry Smith #undef __FUNC__ 1015a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegister_Private" 1025a655dc6SBarry Smith int MatSNESMFRegister_Private(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx)) 1039a6cb015SBarry Smith { 1049a6cb015SBarry Smith int ierr; 1059a6cb015SBarry Smith char fullname[256]; 1069a6cb015SBarry Smith 1079a6cb015SBarry Smith PetscFunctionBegin; 108549d3d68SSatish Balay ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr); 109549d3d68SSatish Balay PetscStrcat(fullname,":");PetscStrcat(fullname,name); 1105a655dc6SBarry Smith ierr = FListAdd_Private(&MatSNESMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 1119a6cb015SBarry Smith PetscFunctionReturn(0); 1129a6cb015SBarry Smith } 1139a6cb015SBarry Smith 1149a6cb015SBarry Smith 1159a6cb015SBarry Smith #undef __FUNC__ 1165a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegisterDestroy" 1179a6cb015SBarry Smith /*@C 1185a655dc6SBarry Smith MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were 1195a655dc6SBarry Smith registered by MatSNESMFRegister(). 1209a6cb015SBarry Smith 1219a6cb015SBarry Smith Not Collective 1229a6cb015SBarry Smith 12315091d37SBarry Smith Level: developer 12415091d37SBarry Smith 1255a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy 1269a6cb015SBarry Smith 1275a655dc6SBarry Smith .seealso: MatSNESMFRegister(), MatSNESMFRegisterAll() 1289a6cb015SBarry Smith @*/ 1295a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void) 1309a6cb015SBarry Smith { 1319a6cb015SBarry Smith int ierr; 1329a6cb015SBarry Smith 1339a6cb015SBarry Smith PetscFunctionBegin; 1345a655dc6SBarry Smith if (MatSNESMFList) { 1355a655dc6SBarry Smith ierr = FListDestroy( MatSNESMFList );CHKERRQ(ierr); 1365a655dc6SBarry Smith MatSNESMFList = 0; 1379a6cb015SBarry Smith } 1385a655dc6SBarry Smith MatSNESMFRegisterAllCalled = 0; 1399a6cb015SBarry Smith PetscFunctionReturn(0); 1409a6cb015SBarry Smith } 1419a6cb015SBarry Smith 1429a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/ 143a4d4d686SBarry Smith #undef __FUNC__ 1445a655dc6SBarry Smith #define __FUNC__ "MatSNESMFDestroy_Private" 1455a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat) 146a4d4d686SBarry Smith { 147a4d4d686SBarry Smith int ierr; 1485a655dc6SBarry Smith MatSNESMFCtx ctx; 149fae171e0SBarry Smith 1503a40ed3dSBarry Smith PetscFunctionBegin; 1510a25c783SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 152b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 1539a6cb015SBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 154b4fd4287SBarry Smith if (ctx->sp) {ierr = PCNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 1559a6cb015SBarry Smith PetscFree(ctx->ops); 156fae171e0SBarry Smith PetscFree(ctx); 1573a40ed3dSBarry Smith PetscFunctionReturn(0); 158b9fa9cd0SBarry Smith } 15950361f65SLois Curfman McInnes 1605615d1e5SSatish Balay #undef __FUNC__ 1615a655dc6SBarry Smith #define __FUNC__ "MatSNESMFView_Private" 16239e2f89bSBarry Smith /* 1635a655dc6SBarry Smith MatSNESMFView_Private - Views matrix-free parameters. 1648f6e3e37SBarry Smith 16539e2f89bSBarry Smith */ 1665a655dc6SBarry Smith int MatSNESMFView_Private(Mat J,Viewer viewer) 167eb9086c3SLois Curfman McInnes { 168eb9086c3SLois Curfman McInnes int ierr; 1695a655dc6SBarry Smith MatSNESMFCtx ctx; 170eb9086c3SLois Curfman McInnes MPI_Comm comm; 171eb9086c3SLois Curfman McInnes FILE *fd; 172eb9086c3SLois Curfman McInnes ViewerType vtype; 173eb9086c3SLois Curfman McInnes 1743a40ed3dSBarry Smith PetscFunctionBegin; 1752d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 176eb9086c3SLois Curfman McInnes ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 177eb9086c3SLois Curfman McInnes ierr = ViewerGetType(viewer,&vtype);CHKERRQ(ierr); 178eb9086c3SLois Curfman McInnes ierr = ViewerASCIIGetPointer(viewer,&fd);CHKERRQ(ierr); 1793f1db9ecSBarry Smith if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 180d132466eSBarry Smith ierr = PetscFPrintf(comm,fd," SNES matrix-free approximation:\n");CHKERRQ(ierr); 181d132466eSBarry Smith ierr = PetscFPrintf(comm,fd," err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 182d132466eSBarry Smith ierr = PetscFPrintf(ctx->comm,fd," Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr); 1839a6cb015SBarry Smith if (ctx->ops->view) { 1849a6cb015SBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 1859a6cb015SBarry Smith } 1865cd90555SBarry Smith } else { 1875cd90555SBarry Smith SETERRQ(1,1,"Viewer type not supported for this object"); 188eb9086c3SLois Curfman McInnes } 1893a40ed3dSBarry Smith PetscFunctionReturn(0); 190eb9086c3SLois Curfman McInnes } 191eb9086c3SLois Curfman McInnes 192be726c96SBarry Smith #undef __FUNC__ 1935a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAssemblyEnd_Private" 194be726c96SBarry Smith /* 1955a655dc6SBarry Smith MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This 19665f2ba5bSLois Curfman McInnes allows the user to indicate the beginning of a new linear solve by calling 197be726c96SBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 19865f2ba5bSLois Curfman McInnes MatSNESMFCreate_WP() to properly compute ||U|| only the first time 19965f2ba5bSLois Curfman McInnes in the linear solver rather than every time. 200be726c96SBarry Smith */ 2015a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J) 202be726c96SBarry Smith { 203be726c96SBarry Smith int ierr; 204be726c96SBarry Smith 205be726c96SBarry Smith PetscFunctionBegin; 2065a655dc6SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 207be726c96SBarry Smith PetscFunctionReturn(0); 208be726c96SBarry Smith } 209be726c96SBarry Smith 210c481317fSBarry Smith 2115615d1e5SSatish Balay #undef __FUNC__ 2125a655dc6SBarry Smith #define __FUNC__ "MatSNESMFMult_Private" 213eb9086c3SLois Curfman McInnes /* 2145a655dc6SBarry Smith MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector 215eb9086c3SLois Curfman McInnes product, y = F'(u)*a: 216a4d4d686SBarry Smith 2179a6cb015SBarry Smith y ~= ( F(u + ha) - F(u) )/h, 218eb9086c3SLois Curfman McInnes where F = nonlinear function, as set by SNESSetFunction() 219eb9086c3SLois Curfman McInnes u = current iterate 220eb9086c3SLois Curfman McInnes h = difference interval 221eb9086c3SLois Curfman McInnes */ 2225a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y) 22339e2f89bSBarry Smith { 2245a655dc6SBarry Smith MatSNESMFCtx ctx; 225fae171e0SBarry Smith SNES snes; 226a4d4d686SBarry Smith Scalar h, mone = -1.0; 227fae171e0SBarry Smith Vec w,U,F; 228a305c92eSSatish Balay int ierr, (*eval_fct)(SNES,Vec,Vec)=0; 22939e2f89bSBarry Smith 2303a40ed3dSBarry Smith PetscFunctionBegin; 2319a6cb015SBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 2329a6cb015SBarry Smith separate the performance monitoring from the cases that use conventional 2339a6cb015SBarry Smith storage. We may eventually modify event logging to associate events 2349a6cb015SBarry Smith with particular objects, hence alleviating the more general problem. */ 23556cd22aeSBarry Smith PLogEventBegin(MAT_MatrixFreeMult,a,y,0,0); 23656cd22aeSBarry Smith 2376e38a044SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 238fae171e0SBarry Smith snes = ctx->snes; 239fae171e0SBarry Smith w = ctx->w; 240fae171e0SBarry Smith 24178b31e54SBarry Smith ierr = SNESGetSolution(snes,&U);CHKERRQ(ierr); 24283e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 24383e56afdSLois Curfman McInnes eval_fct = SNESComputeFunction; 24478b31e54SBarry Smith ierr = SNESGetFunction(snes,&F);CHKERRQ(ierr); 245a4d4d686SBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 24683e56afdSLois Curfman McInnes eval_fct = SNESComputeGradient; 24783e56afdSLois Curfman McInnes ierr = SNESGetGradient(snes,&F);CHKERRQ(ierr); 248a4d4d686SBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class"); 24950361f65SLois Curfman McInnes 2509a6cb015SBarry Smith if (!ctx->ops->compute) { 2515a655dc6SBarry Smith ierr = MatSNESMFSetType(mat,"default");CHKERRQ(ierr); 2525a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr); 2539a6cb015SBarry Smith } 2549a6cb015SBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr); 255a4d4d686SBarry Smith 256a4d4d686SBarry Smith /* keep a record of the current differencing parameter h */ 257a4d4d686SBarry Smith ctx->currenth = h; 258*aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 259a4d4d686SBarry Smith PLogInfo(mat,"Current differencing parameter: %g + %g i\n",PetscReal(h),PetscImaginary(h)); 260a4d4d686SBarry Smith #else 261a4d4d686SBarry Smith PLogInfo(mat,"Current differencing parameter: %g\n",h); 262a4d4d686SBarry Smith #endif 263a4d4d686SBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 264a4d4d686SBarry Smith ctx->historyh[ctx->ncurrenth++] = h; 265be726c96SBarry Smith } else { 266be726c96SBarry Smith ctx->ncurrenth++; 267a4d4d686SBarry Smith } 268a4d4d686SBarry Smith 269a4d4d686SBarry Smith /* Evaluate function at F(u + ha) */ 270a4d4d686SBarry Smith ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr); 271a4d4d686SBarry Smith ierr = eval_fct(snes,w,y);CHKERRQ(ierr); 272a4d4d686SBarry Smith 273a4d4d686SBarry Smith ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr); 274a4d4d686SBarry Smith h = 1.0/h; 275a4d4d686SBarry Smith ierr = VecScale(&h,y);CHKERRQ(ierr); 276a4d4d686SBarry Smith if (ctx->sp) {ierr = PCNullSpaceRemove(ctx->sp,y);CHKERRQ(ierr);} 277a4d4d686SBarry Smith 278a4d4d686SBarry Smith PLogEventEnd(MAT_MatrixFreeMult,a,y,0,0); 279a4d4d686SBarry Smith PetscFunctionReturn(0); 280a4d4d686SBarry Smith } 281a4d4d686SBarry Smith 282a4d4d686SBarry Smith #undef __FUNC__ 2835a655dc6SBarry Smith #define __FUNC__ "MatCreateSNESMF" 284a4d4d686SBarry Smith /*@C 28565f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 28665f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 28765f2ba5bSLois Curfman McInnes the routine SNESSetJacobian(). 288a4d4d686SBarry Smith 289a4d4d686SBarry Smith Collective on SNES and Vec 290a4d4d686SBarry Smith 291a4d4d686SBarry Smith Input Parameters: 292a4d4d686SBarry Smith + snes - the SNES context 293a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 294a4d4d686SBarry Smith 295a4d4d686SBarry Smith Output Parameter: 296a4d4d686SBarry Smith . J - the matrix-free matrix 297a4d4d686SBarry Smith 29815091d37SBarry Smith Level: advanced 29915091d37SBarry Smith 300a4d4d686SBarry Smith Notes: 301a4d4d686SBarry Smith The matrix-free matrix context merely contains the function pointers 302a4d4d686SBarry Smith and work space for performing finite difference approximations of 30365f2ba5bSLois Curfman McInnes Jacobian-vector products, F'(u)*a, 3049a6cb015SBarry Smith 3059a6cb015SBarry Smith The default code uses the following approach to compute h 306a4d4d686SBarry Smith 307a4d4d686SBarry Smith .vb 30865f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 309a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 310a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 311a4d4d686SBarry Smith where 312a4d4d686SBarry Smith error_rel = square root of relative error in function evaluation 313a4d4d686SBarry Smith umin = minimum iterate parameter 314a4d4d686SBarry Smith .ve 315a4d4d686SBarry Smith 3165a655dc6SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 31765f2ba5bSLois Curfman McInnes umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 31865f2ba5bSLois Curfman McInnes of the users manual for details. 319a4d4d686SBarry Smith 320a4d4d686SBarry Smith The user should call MatDestroy() when finished with the matrix-free 321a4d4d686SBarry Smith matrix context. 322a4d4d686SBarry Smith 323a4d4d686SBarry Smith Options Database Keys: 324a4d4d686SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 3259a6cb015SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 326a4d4d686SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 327a4d4d686SBarry Smith 328a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix 329a4d4d686SBarry Smith 3305a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 3315a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 3325a655dc6SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegister() 333a4d4d686SBarry Smith 334a4d4d686SBarry Smith @*/ 3355a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x, Mat *J) 336a4d4d686SBarry Smith { 337a4d4d686SBarry Smith MPI_Comm comm; 3385a655dc6SBarry Smith MatSNESMFCtx mfctx; 3399a6cb015SBarry Smith int n, nloc, ierr; 340a4d4d686SBarry Smith 341a4d4d686SBarry Smith PetscFunctionBegin; 3425a655dc6SBarry Smith mfctx = (MatSNESMFCtx) PetscMalloc(sizeof(struct _p_MatSNESMFCtx));CHKPTRQ(mfctx); 3435a655dc6SBarry Smith PLogObjectMemory(snes,sizeof(MatSNESMFCtx)); 3449a6cb015SBarry Smith mfctx->comm = snes->comm; 345a4d4d686SBarry Smith mfctx->sp = 0; 346a4d4d686SBarry Smith mfctx->snes = snes; 347a4d4d686SBarry Smith mfctx->error_rel = 1.e-8; /* assumes double precision */ 348a4d4d686SBarry Smith mfctx->currenth = 0.0; 349a4d4d686SBarry Smith mfctx->historyh = PETSC_NULL; 350a4d4d686SBarry Smith mfctx->ncurrenth = 0; 351a4d4d686SBarry Smith mfctx->maxcurrenth = 0; 352a4d4d686SBarry Smith 3539a6cb015SBarry Smith /* 3549a6cb015SBarry Smith Create the empty data structure to contain compute-h routines. 3559a6cb015SBarry Smith These will be filled in below from the command line options or 3565a655dc6SBarry Smith a later call with MatSNESMFSetType() or if that is not called 3575a655dc6SBarry Smith then it will default in the first use of MatSNESMFMult_private() 3589a6cb015SBarry Smith */ 3599a6cb015SBarry Smith mfctx->ops = (MFOps *)PetscMalloc(sizeof(MFOps));CHKPTRQ(mfctx->ops); 3609a6cb015SBarry Smith mfctx->ops->compute = 0; 3619a6cb015SBarry Smith mfctx->ops->destroy = 0; 3629a6cb015SBarry Smith mfctx->ops->view = 0; 3639a6cb015SBarry Smith mfctx->ops->printhelp = 0; 3649a6cb015SBarry Smith mfctx->ops->setfromoptions = 0; 3659a6cb015SBarry Smith mfctx->hctx = 0; 3669a6cb015SBarry Smith 367a4d4d686SBarry Smith ierr = VecDuplicate(x,&mfctx->w);CHKERRQ(ierr); 368a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 369a4d4d686SBarry Smith ierr = VecGetSize(x,&n);CHKERRQ(ierr); 370a4d4d686SBarry Smith ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr); 371ffa0fd9eSBarry Smith ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J);CHKERRQ(ierr); 3725a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr); 3735a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr); 3745a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private);CHKERRQ(ierr); 3755a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr); 376a4d4d686SBarry Smith PLogObjectParent(*J,mfctx->w); 377a4d4d686SBarry Smith PLogObjectParent(snes,*J); 3789a6cb015SBarry Smith 3799a6cb015SBarry Smith mfctx->mat = *J; 3809a6cb015SBarry Smith 3819a6cb015SBarry Smith 3829a6cb015SBarry Smith PetscFunctionReturn(0); 3839a6cb015SBarry Smith } 3849a6cb015SBarry Smith 3859a6cb015SBarry Smith #undef __FUNC__ 3865a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFromOptions" 3879a6cb015SBarry Smith /*@ 3885a655dc6SBarry Smith MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line 3899a6cb015SBarry Smith parameter. 3909a6cb015SBarry Smith 3919a6cb015SBarry Smith Collective on Mat 3929a6cb015SBarry Smith 3939a6cb015SBarry Smith Input Parameters: 3945a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 3955a655dc6SBarry Smith 3965a655dc6SBarry Smith Options Database Keys: 3975a655dc6SBarry Smith + -snes_mf_type - <default,wp> 3985a655dc6SBarry Smith - -snes_mf_err - square root of estimated relative error in function evaluation 3999a6cb015SBarry Smith 40015091d37SBarry Smith Level: advanced 40115091d37SBarry Smith 4029a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters 4039a6cb015SBarry Smith 4045a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4055a655dc6SBarry Smith MatSNESMFResetHHistory(), MatSNESMFKSPMonitor() 4069a6cb015SBarry Smith @*/ 4075a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat) 4089a6cb015SBarry Smith { 4095a655dc6SBarry Smith MatSNESMFCtx mfctx; 4109a6cb015SBarry Smith int ierr,flg; 4119a6cb015SBarry Smith char ftype[256],p[64]; 4129a6cb015SBarry Smith 4139a6cb015SBarry Smith PetscFunctionBegin; 4149a6cb015SBarry Smith ierr = MatShellGetContext(mat,(void **)&mfctx);CHKERRQ(ierr); 4159a6cb015SBarry Smith if (mfctx) { 4169a6cb015SBarry Smith /* allow user to set the type */ 4179a6cb015SBarry Smith ierr = OptionsGetString(mfctx->snes->prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr); 4189a6cb015SBarry Smith if (flg) { 4195a655dc6SBarry Smith ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr); 4209a6cb015SBarry Smith } 4219a6cb015SBarry Smith 4229a6cb015SBarry Smith ierr = OptionsGetDouble(mfctx->snes->prefix,"-snes_mf_err",&mfctx->error_rel,&flg);CHKERRQ(ierr); 4239a6cb015SBarry Smith if (mfctx->ops->setfromoptions) { 4249a6cb015SBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 4259a6cb015SBarry Smith } 4269a6cb015SBarry Smith 4279a6cb015SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 428d132466eSBarry Smith ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 429d132466eSBarry Smith if (mfctx->snes->prefix) {ierr = PetscStrcat(p,mfctx->snes->prefix);CHKERRQ(ierr);} 4309a6cb015SBarry Smith if (flg) { 431d132466eSBarry Smith ierr = (*PetscHelpPrintf)(mfctx->snes->comm," %ssnes_mf_err <err>: set sqrt rel error in function (default %g)\n",p,mfctx->error_rel);CHKERRQ(ierr); 4329a6cb015SBarry Smith if (mfctx->ops->printhelp) { 433d132466eSBarry Smith ierr = (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr); 4349a6cb015SBarry Smith } 4359a6cb015SBarry Smith } 4369a6cb015SBarry Smith } 437a4d4d686SBarry Smith PetscFunctionReturn(0); 438a4d4d686SBarry Smith } 439a4d4d686SBarry Smith 440a4d4d686SBarry Smith #undef __FUNC__ 4415a655dc6SBarry Smith #define __FUNC__ "MatSNESMFGetH" 442a4d4d686SBarry Smith /*@ 44365f2ba5bSLois Curfman McInnes MatSNESMFGetH - Gets the last value that was used as the differencing 444a4d4d686SBarry Smith parameter. 445a4d4d686SBarry Smith 446a4d4d686SBarry Smith Not Collective 447a4d4d686SBarry Smith 448a4d4d686SBarry Smith Input Parameters: 4495a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 450a4d4d686SBarry Smith 451a4d4d686SBarry Smith Output Paramter: 452a4d4d686SBarry Smith . h - the differencing step size 453a4d4d686SBarry Smith 45415091d37SBarry Smith Level: advanced 45515091d37SBarry Smith 456a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 457a4d4d686SBarry Smith 4585a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4595a655dc6SBarry Smith MatSNESMFResetHHistory(),MatSNESMFKSPMonitor() 460a4d4d686SBarry Smith @*/ 4615a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h) 462a4d4d686SBarry Smith { 4635a655dc6SBarry Smith MatSNESMFCtx ctx; 464a4d4d686SBarry Smith int ierr; 465a4d4d686SBarry Smith 466a4d4d686SBarry Smith PetscFunctionBegin; 467a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 468a4d4d686SBarry Smith if (ctx) { 469a4d4d686SBarry Smith *h = ctx->currenth; 470a4d4d686SBarry Smith } 471a4d4d686SBarry Smith PetscFunctionReturn(0); 472a4d4d686SBarry Smith } 473a4d4d686SBarry Smith 474a4d4d686SBarry Smith #undef __FUNC__ 4755a655dc6SBarry Smith #define __FUNC__ "MatSNESMFKSPMonitor" 476a4d4d686SBarry Smith /* 4775a655dc6SBarry Smith MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc 47865f2ba5bSLois Curfman McInnes SNES matrix free routines. Prints the differencing parameter used at 47965f2ba5bSLois Curfman McInnes each step. 480a4d4d686SBarry Smith */ 4815a655dc6SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,double rnorm,void *dummy) 482a4d4d686SBarry Smith { 483a4d4d686SBarry Smith PC pc; 4845a655dc6SBarry Smith MatSNESMFCtx ctx; 485a4d4d686SBarry Smith int ierr; 486a4d4d686SBarry Smith Mat mat; 487a4d4d686SBarry Smith MPI_Comm comm; 488a4d4d686SBarry Smith PetscTruth nonzeroinitialguess; 489a4d4d686SBarry Smith 490a4d4d686SBarry Smith PetscFunctionBegin; 491a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 492a4d4d686SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 493a4d4d686SBarry Smith ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr); 494a4d4d686SBarry Smith ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 495a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 4969a6cb015SBarry Smith if (!ctx) { 4979a6cb015SBarry Smith SETERRQ(1,1,"Matrix is not a matrix free shell matrix"); 4989a6cb015SBarry Smith } 499a4d4d686SBarry Smith if (n > 0 || nonzeroinitialguess) { 500*aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 501d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm, 502d132466eSBarry Smith PetscReal(ctx->currenth),PetscImaginary(ctx->currenth));CHKERRQ(ierr); 503a4d4d686SBarry Smith #else 504d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr); 505a4d4d686SBarry Smith #endif 506a4d4d686SBarry Smith } else { 507d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr); 508a4d4d686SBarry Smith } 509a4d4d686SBarry Smith PetscFunctionReturn(0); 510a4d4d686SBarry Smith } 511a4d4d686SBarry Smith 512a4d4d686SBarry Smith #undef __FUNC__ 5135a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFunctionError" 514a4d4d686SBarry Smith /*@ 5155a655dc6SBarry Smith MatSNESMFSetFunctionError - Sets the error_rel for the approximation of 516a4d4d686SBarry Smith matrix-vector products using finite differences. 517a4d4d686SBarry Smith 518a4d4d686SBarry Smith Collective on Mat 519a4d4d686SBarry Smith 520a4d4d686SBarry Smith Input Parameters: 5215a655dc6SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 5229a6cb015SBarry Smith - error_rel - relative error (should be set to the square root of 523a4d4d686SBarry Smith the relative error in the function evaluations) 524a4d4d686SBarry Smith 52515091d37SBarry Smith Options Database Keys: 52615091d37SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 52715091d37SBarry Smith 52815091d37SBarry Smith Level: advanced 52915091d37SBarry Smith 530a4d4d686SBarry Smith Notes: 531a4d4d686SBarry Smith The default matrix-free matrix-vector product routine computes 532a4d4d686SBarry Smith .vb 53365f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 534a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 535a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 536a4d4d686SBarry Smith .ve 537a4d4d686SBarry Smith 538a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 539a4d4d686SBarry Smith 5405a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 5415a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 5425a655dc6SBarry Smith MatSNESMFKSPMonitor() 543a4d4d686SBarry Smith @*/ 5445a655dc6SBarry Smith int MatSNESMFSetFunctionError(Mat mat,double error) 545a4d4d686SBarry Smith { 5465a655dc6SBarry Smith MatSNESMFCtx ctx; 547a4d4d686SBarry Smith int ierr; 548a4d4d686SBarry Smith 549a4d4d686SBarry Smith PetscFunctionBegin; 550a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 551a4d4d686SBarry Smith if (ctx) { 552a4d4d686SBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 553a4d4d686SBarry Smith } 554a4d4d686SBarry Smith PetscFunctionReturn(0); 555a4d4d686SBarry Smith } 556a4d4d686SBarry Smith 557a4d4d686SBarry Smith #undef __FUNC__ 5585a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAddNullSpace" 559a4d4d686SBarry Smith /*@ 56065f2ba5bSLois Curfman McInnes MatSNESMFAddNullSpace - Provides a null space that an operator is 56165f2ba5bSLois Curfman McInnes supposed to have. Since roundoff will create a small component in 56265f2ba5bSLois Curfman McInnes the null space, if you know the null space you may have it 56365f2ba5bSLois Curfman McInnes automatically removed. 564a4d4d686SBarry Smith 565a4d4d686SBarry Smith Collective on Mat 566a4d4d686SBarry Smith 567a4d4d686SBarry Smith Input Parameters: 568a4d4d686SBarry Smith + J - the matrix-free matrix context 56965f2ba5bSLois Curfman McInnes . has_cnst - PETSC_TRUE or PETSC_FALSE, indicating whether null space has constants 570a4d4d686SBarry Smith . n - number of vectors (excluding constant vector) in null space 571a4d4d686SBarry Smith - vecs - the vectors that span the null space (excluding the constant vector); 572a4d4d686SBarry Smith these vectors must be orthonormal 573a4d4d686SBarry Smith 57415091d37SBarry Smith Level: advanced 57515091d37SBarry Smith 576a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space 577a4d4d686SBarry Smith 5785a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 5795a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 5805a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFErrorRel() 581a4d4d686SBarry Smith 582a4d4d686SBarry Smith @*/ 5835a655dc6SBarry Smith int MatSNESMFAddNullSpace(Mat J,int has_cnst,int n,Vec *vecs) 584a4d4d686SBarry Smith { 585a4d4d686SBarry Smith int ierr; 5865a655dc6SBarry Smith MatSNESMFCtx ctx; 587a4d4d686SBarry Smith MPI_Comm comm; 588a4d4d686SBarry Smith 589a4d4d686SBarry Smith PetscFunctionBegin; 5902d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 591a4d4d686SBarry Smith 592a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 593a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 594a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 595a4d4d686SBarry Smith ierr = PCNullSpaceCreate(comm,has_cnst,n,vecs,&ctx->sp);CHKERRQ(ierr); 596a4d4d686SBarry Smith PetscFunctionReturn(0); 597a4d4d686SBarry Smith } 598a4d4d686SBarry Smith 599a4d4d686SBarry Smith #undef __FUNC__ 6005a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetHHistory" 601a4d4d686SBarry Smith /*@ 60265f2ba5bSLois Curfman McInnes MatSNESMFSetHHistory - Sets an array to collect a history of the 60365f2ba5bSLois Curfman McInnes differencing values (h) computed for the matrix-free product. 604a4d4d686SBarry Smith 605a4d4d686SBarry Smith Collective on Mat 606a4d4d686SBarry Smith 607a4d4d686SBarry Smith Input Parameters: 608a4d4d686SBarry Smith + J - the matrix-free matrix context 60965f2ba5bSLois Curfman McInnes . histroy - space to hold the history 61065f2ba5bSLois Curfman McInnes - nhistory - number of entries in history, if more entries are generated than 61165f2ba5bSLois Curfman McInnes nhistory, then the later ones are discarded 612a4d4d686SBarry Smith 61315091d37SBarry Smith Level: advanced 61415091d37SBarry Smith 615a4d4d686SBarry Smith Notes: 61665f2ba5bSLois Curfman McInnes Use MatSNESMFResetHHistory() to reset the history counter and collect 61765f2ba5bSLois Curfman McInnes a new batch of differencing parameters, h. 618a4d4d686SBarry Smith 619a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 620a4d4d686SBarry Smith 6215a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 6225a655dc6SBarry Smith MatSNESMFResetHHistory(), 6235a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 624a4d4d686SBarry Smith 625a4d4d686SBarry Smith @*/ 6265a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory) 627a4d4d686SBarry Smith { 628a4d4d686SBarry Smith int ierr; 6295a655dc6SBarry Smith MatSNESMFCtx ctx; 630a4d4d686SBarry Smith 631a4d4d686SBarry Smith PetscFunctionBegin; 632a4d4d686SBarry Smith 633a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 634a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 635a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 636a4d4d686SBarry Smith ctx->historyh = history; 637a4d4d686SBarry Smith ctx->maxcurrenth = nhistory; 638a4d4d686SBarry Smith ctx->currenth = 0; 639a4d4d686SBarry Smith 640a4d4d686SBarry Smith PetscFunctionReturn(0); 641a4d4d686SBarry Smith } 642a4d4d686SBarry Smith 643a4d4d686SBarry Smith #undef __FUNC__ 6445a655dc6SBarry Smith #define __FUNC__ "MatSNESMFResetHHistory" 645a4d4d686SBarry Smith /*@ 6465a655dc6SBarry Smith MatSNESMFResetHHistory - Resets the counter to zero to begin 647a4d4d686SBarry Smith collecting a new set of differencing histories. 648a4d4d686SBarry Smith 649a4d4d686SBarry Smith Collective on Mat 650a4d4d686SBarry Smith 651a4d4d686SBarry Smith Input Parameters: 652a4d4d686SBarry Smith . J - the matrix-free matrix context 653a4d4d686SBarry Smith 65415091d37SBarry Smith Level: advanced 65515091d37SBarry Smith 656a4d4d686SBarry Smith Notes: 65765f2ba5bSLois Curfman McInnes Use MatSNESMFSetHHistory() to create the original history counter. 658a4d4d686SBarry Smith 659a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 660a4d4d686SBarry Smith 6615a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 6625a655dc6SBarry Smith MatSNESMFSetHHistory(), 6635a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 664a4d4d686SBarry Smith 665a4d4d686SBarry Smith @*/ 6665a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J) 667a4d4d686SBarry Smith { 668a4d4d686SBarry Smith int ierr; 6695a655dc6SBarry Smith MatSNESMFCtx ctx; 670a4d4d686SBarry Smith 671a4d4d686SBarry Smith PetscFunctionBegin; 672a4d4d686SBarry Smith 673a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 674a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 675a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 676be726c96SBarry Smith ctx->ncurrenth = 0; 677a4d4d686SBarry Smith 678a4d4d686SBarry Smith PetscFunctionReturn(0); 679a4d4d686SBarry Smith } 680a4d4d686SBarry Smith 681