1*1d1367b7SBarry Smith /*$Id: snesmfj.c,v 1.107 2000/08/01 20:57:20 bsmith Exp bsmith $*/ 281e6777dSBarry Smith 39a6cb015SBarry Smith #include "src/snes/snesimpl.h" 4e090d566SSatish Balay #include "src/snes/mf/snesmfj.h" /*I "petscsnes.h" I*/ 581e6777dSBarry Smith 65a655dc6SBarry Smith FList MatSNESMFList = 0; 74c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE; 8a4d4d686SBarry Smith 95615d1e5SSatish Balay #undef __FUNC__ 10b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetType" 11fd4bdd07SBarry Smith /*@C 1265f2ba5bSLois Curfman McInnes MatSNESMFSetType - Sets the method that is used to compute the 1365f2ba5bSLois Curfman McInnes differencing parameter for finite difference matrix-free formulations. 149a6cb015SBarry Smith 159a6cb015SBarry Smith Input Parameters: 16ef4ad1fdSLois Curfman McInnes + mat - the "matrix-free" matrix created via MatCreateSNESMF() 179a6cb015SBarry Smith - ftype - the type requested 189a6cb015SBarry Smith 1915091d37SBarry Smith Level: advanced 2015091d37SBarry Smith 2165f2ba5bSLois Curfman McInnes Notes: 2265f2ba5bSLois Curfman McInnes For example, such routines can compute h for use in 2365f2ba5bSLois Curfman McInnes Jacobian-vector products of the form 2465f2ba5bSLois Curfman McInnes 2565f2ba5bSLois Curfman McInnes F(x+ha) - F(x) 26ef4ad1fdSLois Curfman McInnes F'(u)a ~= ---------------- 2765f2ba5bSLois Curfman McInnes h 2865f2ba5bSLois Curfman McInnes 29f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic) 309a6cb015SBarry Smith @*/ 31f6a0df18SBarry Smith int MatSNESMFSetType(Mat mat,MatSNESMFType ftype) 32b9fa9cd0SBarry Smith { 335a655dc6SBarry Smith int ierr,(*r)(MatSNESMFCtx); 345a655dc6SBarry Smith MatSNESMFCtx ctx; 356831982aSBarry Smith PetscTruth match; 36a4d4d686SBarry Smith 37a4d4d686SBarry Smith PetscFunctionBegin; 380f5bd95cSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 390f5bd95cSBarry Smith PetscValidCharPointer(ftype); 400f5bd95cSBarry Smith 41a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 42a4d4d686SBarry Smith 439a6cb015SBarry Smith /* already set, so just return */ 446831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr); 450f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 46a4d4d686SBarry Smith 479a6cb015SBarry Smith /* destroy the old one if it exists */ 489a6cb015SBarry Smith if (ctx->ops->destroy) { 499a6cb015SBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 509a6cb015SBarry Smith } 519a6cb015SBarry Smith 5265f2ba5bSLois Curfman McInnes /* Get the function pointers for the requrested method */ 535a655dc6SBarry Smith if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 549a6cb015SBarry Smith 555a655dc6SBarry Smith ierr = FListFind(ctx->comm,MatSNESMFList,ftype,(int (**)(void *)) &r);CHKERRQ(ierr); 569a6cb015SBarry Smith 575a655dc6SBarry Smith if (!r) SETERRQ(1,1,"Unknown MatSNESMF type given"); 589a6cb015SBarry Smith 599a6cb015SBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 606831982aSBarry Smith 616831982aSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr); 629a6cb015SBarry Smith 639a6cb015SBarry Smith PetscFunctionReturn(0); 649a6cb015SBarry Smith } 659a6cb015SBarry Smith 669a6cb015SBarry Smith /*MC 67f1af5d2fSBarry Smith MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry. 689a6cb015SBarry Smith 699a6cb015SBarry Smith Synopsis: 70f1af5d2fSBarry Smith MatSNESMFRegisterDynamicchar *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF)) 719a6cb015SBarry Smith 729a6cb015SBarry Smith Not Collective 739a6cb015SBarry Smith 749a6cb015SBarry Smith Input Parameters: 759a6cb015SBarry Smith + name_solver - name of a new user-defined compute-h module 769a6cb015SBarry Smith . path - path (either absolute or relative) the library containing this solver 779a6cb015SBarry Smith . name_create - name of routine to create method context 789a6cb015SBarry Smith - routine_create - routine to create method context 799a6cb015SBarry Smith 8015091d37SBarry Smith Level: developer 8115091d37SBarry Smith 829a6cb015SBarry Smith Notes: 83f1af5d2fSBarry Smith MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers. 849a6cb015SBarry Smith 859a6cb015SBarry Smith If dynamic libraries are used, then the fourth input argument (routine_create) 869a6cb015SBarry Smith is ignored. 879a6cb015SBarry Smith 889a6cb015SBarry Smith Sample usage: 899a6cb015SBarry Smith .vb 90f1af5d2fSBarry Smith MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a, 919a6cb015SBarry Smith "MyHCreate",MyHCreate); 929a6cb015SBarry Smith .ve 939a6cb015SBarry Smith 949a6cb015SBarry Smith Then, your solver can be chosen with the procedural interface via 955a655dc6SBarry Smith $ MatSNESMFSetType(mfctx,"my_h") 969a6cb015SBarry Smith or at runtime via the option 979a6cb015SBarry Smith $ -snes_mf_type my_h 989a6cb015SBarry Smith 995a655dc6SBarry Smith .keywords: MatSNESMF, register 1009a6cb015SBarry Smith 1015a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy() 1029a6cb015SBarry Smith M*/ 1039a6cb015SBarry Smith 1049a6cb015SBarry Smith #undef __FUNC__ 105b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFRegister" 106f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx)) 1079a6cb015SBarry Smith { 1089a6cb015SBarry Smith int ierr; 1099a6cb015SBarry Smith char fullname[256]; 1109a6cb015SBarry Smith 1119a6cb015SBarry Smith PetscFunctionBegin; 112f1af5d2fSBarry Smith ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 113f1af5d2fSBarry Smith ierr = FListAdd(&MatSNESMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 1149a6cb015SBarry Smith PetscFunctionReturn(0); 1159a6cb015SBarry Smith } 1169a6cb015SBarry Smith 1179a6cb015SBarry Smith 1189a6cb015SBarry Smith #undef __FUNC__ 119b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFRegisterDestroy" 1209a6cb015SBarry Smith /*@C 1215a655dc6SBarry Smith MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were 122f1af5d2fSBarry Smith registered by MatSNESMFRegisterDynamic). 1239a6cb015SBarry Smith 1249a6cb015SBarry Smith Not Collective 1259a6cb015SBarry Smith 12615091d37SBarry Smith Level: developer 12715091d37SBarry Smith 1285a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy 1299a6cb015SBarry Smith 130f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll() 1319a6cb015SBarry Smith @*/ 1325a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void) 1339a6cb015SBarry Smith { 1349a6cb015SBarry Smith int ierr; 1359a6cb015SBarry Smith 1369a6cb015SBarry Smith PetscFunctionBegin; 1375a655dc6SBarry Smith if (MatSNESMFList) { 138*1d1367b7SBarry Smith ierr = FListDestroy(&MatSNESMFList);CHKERRQ(ierr); 1395a655dc6SBarry Smith MatSNESMFList = 0; 1409a6cb015SBarry Smith } 1414c49b128SBarry Smith MatSNESMFRegisterAllCalled = PETSC_FALSE; 1429a6cb015SBarry Smith PetscFunctionReturn(0); 1439a6cb015SBarry Smith } 1449a6cb015SBarry Smith 1459a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/ 146a4d4d686SBarry Smith #undef __FUNC__ 147b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFDestroy_Private" 1485a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat) 149a4d4d686SBarry Smith { 150a4d4d686SBarry Smith int ierr; 1515a655dc6SBarry Smith MatSNESMFCtx ctx; 152fae171e0SBarry Smith 1533a40ed3dSBarry Smith PetscFunctionBegin; 1540a25c783SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 155b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 1569a6cb015SBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 15774637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 1586831982aSBarry Smith PetscHeaderDestroy(ctx); 1593a40ed3dSBarry Smith PetscFunctionReturn(0); 160b9fa9cd0SBarry Smith } 16150361f65SLois Curfman McInnes 1625615d1e5SSatish Balay #undef __FUNC__ 163b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFView_Private" 16439e2f89bSBarry Smith /* 1655a655dc6SBarry Smith MatSNESMFView_Private - Views matrix-free parameters. 1668f6e3e37SBarry Smith 16739e2f89bSBarry Smith */ 1685a655dc6SBarry Smith int MatSNESMFView_Private(Mat J,Viewer viewer) 169eb9086c3SLois Curfman McInnes { 170eb9086c3SLois Curfman McInnes int ierr; 1715a655dc6SBarry Smith MatSNESMFCtx ctx; 1726831982aSBarry Smith PetscTruth isascii; 173eb9086c3SLois Curfman McInnes 1743a40ed3dSBarry Smith PetscFunctionBegin; 175eb9086c3SLois Curfman McInnes ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 1766831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr); 1770f5bd95cSBarry Smith if (isascii) { 1786831982aSBarry Smith ierr = ViewerASCIIPrintf(viewer," SNES matrix-free approximation:\n");CHKERRQ(ierr); 1796831982aSBarry Smith ierr = ViewerASCIIPrintf(viewer," err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 1806831982aSBarry Smith ierr = ViewerASCIIPrintf(viewer," Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr); 1819a6cb015SBarry Smith if (ctx->ops->view) { 1829a6cb015SBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 1839a6cb015SBarry Smith } 1845cd90555SBarry Smith } else { 1850f5bd95cSBarry Smith SETERRQ1(1,1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name); 186eb9086c3SLois Curfman McInnes } 1873a40ed3dSBarry Smith PetscFunctionReturn(0); 188eb9086c3SLois Curfman McInnes } 189eb9086c3SLois Curfman McInnes 190be726c96SBarry Smith #undef __FUNC__ 191b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFAssemblyEnd_Private" 192be726c96SBarry Smith /* 1935a655dc6SBarry Smith MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This 19465f2ba5bSLois Curfman McInnes allows the user to indicate the beginning of a new linear solve by calling 195be726c96SBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 19665f2ba5bSLois Curfman McInnes MatSNESMFCreate_WP() to properly compute ||U|| only the first time 19765f2ba5bSLois Curfman McInnes in the linear solver rather than every time. 198be726c96SBarry Smith */ 1995a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J) 200be726c96SBarry Smith { 201be726c96SBarry Smith int ierr; 202*1d1367b7SBarry Smith MatSNESMFCtx j; 203be726c96SBarry Smith 204be726c96SBarry Smith PetscFunctionBegin; 2055a655dc6SBarry Smith ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr); 206*1d1367b7SBarry Smith ierr = MatShellGetContext(J,(void **)&j);CHKERRQ(ierr); 207*1d1367b7SBarry Smith if (j->snes) { 208*1d1367b7SBarry Smith ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr); 209*1d1367b7SBarry Smith if (j->snes->method_class == SNES_NONLINEAR_EQUATIONS) { 210*1d1367b7SBarry Smith ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 211*1d1367b7SBarry Smith } else if (j->snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 212*1d1367b7SBarry Smith ierr = SNESGetGradient(j->snes,&j->current_f,PETSC_NULL);CHKERRQ(ierr); 213*1d1367b7SBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class"); 214*1d1367b7SBarry Smith } 215be726c96SBarry Smith PetscFunctionReturn(0); 216be726c96SBarry Smith } 217be726c96SBarry Smith 218c481317fSBarry Smith 2195615d1e5SSatish Balay #undef __FUNC__ 220b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFMult_Private" 221eb9086c3SLois Curfman McInnes /* 2225a655dc6SBarry Smith MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector 223eb9086c3SLois Curfman McInnes product, y = F'(u)*a: 224a4d4d686SBarry Smith 2259a6cb015SBarry Smith y ~= (F(u + ha) - F(u))/h, 226eb9086c3SLois Curfman McInnes where F = nonlinear function, as set by SNESSetFunction() 227eb9086c3SLois Curfman McInnes u = current iterate 228eb9086c3SLois Curfman McInnes h = difference interval 229eb9086c3SLois Curfman McInnes */ 2305a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y) 23139e2f89bSBarry Smith { 2325a655dc6SBarry Smith MatSNESMFCtx ctx; 233fae171e0SBarry Smith SNES snes; 234a4d4d686SBarry Smith Scalar h,mone = -1.0; 235fae171e0SBarry Smith Vec w,U,F; 236a305c92eSSatish Balay int ierr,(*eval_fct)(SNES,Vec,Vec)=0; 23739e2f89bSBarry Smith 2383a40ed3dSBarry Smith PetscFunctionBegin; 2399a6cb015SBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 2409a6cb015SBarry Smith separate the performance monitoring from the cases that use conventional 2419a6cb015SBarry Smith storage. We may eventually modify event logging to associate events 2429a6cb015SBarry Smith with particular objects, hence alleviating the more general problem. */ 24356cd22aeSBarry Smith PLogEventBegin(MAT_MatrixFreeMult,a,y,0,0); 24456cd22aeSBarry Smith 2456e38a044SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 246fae171e0SBarry Smith snes = ctx->snes; 247fae171e0SBarry Smith w = ctx->w; 248*1d1367b7SBarry Smith U = ctx->current_u; 24950361f65SLois Curfman McInnes 25085614651SBarry Smith /* 25185614651SBarry Smith Compute differencing parameter 25285614651SBarry Smith */ 2539a6cb015SBarry Smith if (!ctx->ops->compute) { 254b7fd4e64SBarry Smith ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr); 2555a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr); 2569a6cb015SBarry Smith } 2579a6cb015SBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr); 258a4d4d686SBarry Smith 259a4d4d686SBarry Smith /* keep a record of the current differencing parameter h */ 260a4d4d686SBarry Smith ctx->currenth = h; 261aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 262329f5518SBarry Smith PLogInfo(mat,"Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h)); 263a4d4d686SBarry Smith #else 264a4d4d686SBarry Smith PLogInfo(mat,"Current differencing parameter: %g\n",h); 265a4d4d686SBarry Smith #endif 266a4d4d686SBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 26785614651SBarry Smith ctx->historyh[ctx->ncurrenth] = h; 268a4d4d686SBarry Smith } 26985614651SBarry Smith ctx->ncurrenth++; 270a4d4d686SBarry Smith 27185614651SBarry Smith /* w = u + ha */ 272a4d4d686SBarry Smith ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr); 27385614651SBarry Smith 27485614651SBarry Smith 27585614651SBarry Smith if (!ctx->func) { 27685614651SBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 27785614651SBarry Smith eval_fct = SNESComputeFunction; 27885614651SBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 27985614651SBarry Smith eval_fct = SNESComputeGradient; 28085614651SBarry Smith } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class"); 281*1d1367b7SBarry Smith F = ctx->current_f; 282*1d1367b7SBarry Smith if (!F) SETERRQ(1,1,"You must call MatAssembly() even on matrix-free matrices"); 283a4d4d686SBarry Smith ierr = eval_fct(snes,w,y);CHKERRQ(ierr); 28485614651SBarry Smith } else { 28585614651SBarry Smith F = ctx->funcvec; 28685614651SBarry Smith /* compute func(U) as base for differencing */ 28785614651SBarry Smith if (ctx->ncurrenth == 1) { 28885614651SBarry Smith ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr); 28985614651SBarry Smith } 29085614651SBarry Smith ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr); 29185614651SBarry Smith } 292a4d4d686SBarry Smith 293a4d4d686SBarry Smith ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr); 294a4d4d686SBarry Smith h = 1.0/h; 295a4d4d686SBarry Smith ierr = VecScale(&h,y);CHKERRQ(ierr); 29674637425SBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 297a4d4d686SBarry Smith 298a4d4d686SBarry Smith PLogEventEnd(MAT_MatrixFreeMult,a,y,0,0); 299a4d4d686SBarry Smith PetscFunctionReturn(0); 300a4d4d686SBarry Smith } 301a4d4d686SBarry Smith 302a4d4d686SBarry Smith #undef __FUNC__ 303b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatCreateSNESMF" 304a4d4d686SBarry Smith /*@C 30565f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 30665f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 30765f2ba5bSLois Curfman McInnes the routine SNESSetJacobian(). 308a4d4d686SBarry Smith 309a4d4d686SBarry Smith Collective on SNES and Vec 310a4d4d686SBarry Smith 311a4d4d686SBarry Smith Input Parameters: 312a4d4d686SBarry Smith + snes - the SNES context 313a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 314a4d4d686SBarry Smith 315a4d4d686SBarry Smith Output Parameter: 316a4d4d686SBarry Smith . J - the matrix-free matrix 317a4d4d686SBarry Smith 31815091d37SBarry Smith Level: advanced 31915091d37SBarry Smith 320a4d4d686SBarry Smith Notes: 321a4d4d686SBarry Smith The matrix-free matrix context merely contains the function pointers 322a4d4d686SBarry Smith and work space for performing finite difference approximations of 32365f2ba5bSLois Curfman McInnes Jacobian-vector products, F'(u)*a, 3249a6cb015SBarry Smith 3259a6cb015SBarry Smith The default code uses the following approach to compute h 326a4d4d686SBarry Smith 327a4d4d686SBarry Smith .vb 32865f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 329a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 330a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 331a4d4d686SBarry Smith where 332a4d4d686SBarry Smith error_rel = square root of relative error in function evaluation 333a4d4d686SBarry Smith umin = minimum iterate parameter 334a4d4d686SBarry Smith .ve 335a4d4d686SBarry Smith 3365a655dc6SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 33765f2ba5bSLois Curfman McInnes umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 33865f2ba5bSLois Curfman McInnes of the users manual for details. 339a4d4d686SBarry Smith 340a4d4d686SBarry Smith The user should call MatDestroy() when finished with the matrix-free 341a4d4d686SBarry Smith matrix context. 342a4d4d686SBarry Smith 343a4d4d686SBarry Smith Options Database Keys: 344a4d4d686SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 3459a6cb015SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 346a4d4d686SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 347a4d4d686SBarry Smith 348a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix 349a4d4d686SBarry Smith 3505a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 351*1d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(), 352*1d1367b7SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFFormJacobian() 353a4d4d686SBarry Smith 354a4d4d686SBarry Smith @*/ 3555a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J) 356a4d4d686SBarry Smith { 357*1d1367b7SBarry Smith MatSNESMFCtx mfctx; 358*1d1367b7SBarry Smith int ierr; 359*1d1367b7SBarry Smith 360*1d1367b7SBarry Smith PetscFunctionBegin; 361*1d1367b7SBarry Smith ierr = MatCreateMF(x,J);CHKERRQ(ierr); 362*1d1367b7SBarry Smith ierr = MatShellGetContext(*J,(void **)&mfctx);CHKERRQ(ierr); 363*1d1367b7SBarry Smith mfctx->snes = snes; 364*1d1367b7SBarry Smith PLogObjectParent(snes,*J); 365*1d1367b7SBarry Smith PetscFunctionReturn(0); 366*1d1367b7SBarry Smith } 367*1d1367b7SBarry Smith 368*1d1367b7SBarry Smith #undef __FUNC__ 369*1d1367b7SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatCreateMF" 370*1d1367b7SBarry Smith /*@C 371*1d1367b7SBarry Smith MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF() 372*1d1367b7SBarry Smith 373*1d1367b7SBarry Smith Collective on Vec 374*1d1367b7SBarry Smith 375*1d1367b7SBarry Smith Input Parameters: 376*1d1367b7SBarry Smith . x - vector that defines layout of the vectors and matrices 377*1d1367b7SBarry Smith 378*1d1367b7SBarry Smith Output Parameter: 379*1d1367b7SBarry Smith . J - the matrix-free matrix 380*1d1367b7SBarry Smith 381*1d1367b7SBarry Smith Level: advanced 382*1d1367b7SBarry Smith 383*1d1367b7SBarry Smith Notes: 384*1d1367b7SBarry Smith The matrix-free matrix context merely contains the function pointers 385*1d1367b7SBarry Smith and work space for performing finite difference approximations of 386*1d1367b7SBarry Smith Jacobian-vector products, F'(u)*a, 387*1d1367b7SBarry Smith 388*1d1367b7SBarry Smith The default code uses the following approach to compute h 389*1d1367b7SBarry Smith 390*1d1367b7SBarry Smith .vb 391*1d1367b7SBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 392*1d1367b7SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 393*1d1367b7SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 394*1d1367b7SBarry Smith where 395*1d1367b7SBarry Smith error_rel = square root of relative error in function evaluation 396*1d1367b7SBarry Smith umin = minimum iterate parameter 397*1d1367b7SBarry Smith .ve 398*1d1367b7SBarry Smith 399*1d1367b7SBarry Smith The user can set the error_rel via MatSNESMFSetFunctionError() and 400*1d1367b7SBarry Smith umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter 401*1d1367b7SBarry Smith of the users manual for details. 402*1d1367b7SBarry Smith 403*1d1367b7SBarry Smith The user should call MatDestroy() when finished with the matrix-free 404*1d1367b7SBarry Smith matrix context. 405*1d1367b7SBarry Smith 406*1d1367b7SBarry Smith Options Database Keys: 407*1d1367b7SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 408*1d1367b7SBarry Smith . -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only) 409*1d1367b7SBarry Smith - -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h 410*1d1367b7SBarry Smith 411*1d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix 412*1d1367b7SBarry Smith 413*1d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin() 414*1d1367b7SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(), 415*1d1367b7SBarry Smith MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFFormJacobian() 416*1d1367b7SBarry Smith 417*1d1367b7SBarry Smith @*/ 418*1d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J) 419*1d1367b7SBarry Smith { 420a4d4d686SBarry Smith MPI_Comm comm; 4215a655dc6SBarry Smith MatSNESMFCtx mfctx; 4229a6cb015SBarry Smith int n,nloc,ierr; 423a4d4d686SBarry Smith 424a4d4d686SBarry Smith PetscFunctionBegin; 425*1d1367b7SBarry Smith ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr); 426*1d1367b7SBarry Smith PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",comm,MatSNESMFDestroy_Private,MatSNESMFView_Private); 4276831982aSBarry Smith PLogObjectCreate(mfctx); 428a4d4d686SBarry Smith mfctx->sp = 0; 429*1d1367b7SBarry Smith mfctx->snes = 0; 430329f5518SBarry Smith mfctx->error_rel = 1.e-8; /* assumes PetscReal precision */ 431329f5518SBarry Smith mfctx->recomputeperiod = 1; 432329f5518SBarry Smith mfctx->count = 0; 433a4d4d686SBarry Smith mfctx->currenth = 0.0; 434a4d4d686SBarry Smith mfctx->historyh = PETSC_NULL; 435a4d4d686SBarry Smith mfctx->ncurrenth = 0; 436a4d4d686SBarry Smith mfctx->maxcurrenth = 0; 4376831982aSBarry Smith mfctx->type_name = 0; 438a4d4d686SBarry Smith 4399a6cb015SBarry Smith /* 4409a6cb015SBarry Smith Create the empty data structure to contain compute-h routines. 4419a6cb015SBarry Smith These will be filled in below from the command line options or 4425a655dc6SBarry Smith a later call with MatSNESMFSetType() or if that is not called 4435a655dc6SBarry Smith then it will default in the first use of MatSNESMFMult_private() 4449a6cb015SBarry Smith */ 4459a6cb015SBarry Smith mfctx->ops->compute = 0; 4469a6cb015SBarry Smith mfctx->ops->destroy = 0; 4479a6cb015SBarry Smith mfctx->ops->view = 0; 4489a6cb015SBarry Smith mfctx->ops->printhelp = 0; 4499a6cb015SBarry Smith mfctx->ops->setfromoptions = 0; 4509a6cb015SBarry Smith mfctx->hctx = 0; 4519a6cb015SBarry Smith 45285614651SBarry Smith mfctx->func = 0; 45385614651SBarry Smith mfctx->funcctx = 0; 45485614651SBarry Smith mfctx->funcvec = 0; 45585614651SBarry Smith 456a4d4d686SBarry Smith ierr = VecDuplicate(x,&mfctx->w);CHKERRQ(ierr); 457*1d1367b7SBarry Smith ierr = VecGetSize(mfctx->w,&n);CHKERRQ(ierr); 458*1d1367b7SBarry Smith ierr = VecGetLocalSize(mfctx->w,&nloc);CHKERRQ(ierr); 459ffa0fd9eSBarry Smith ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J);CHKERRQ(ierr); 4605a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr); 4615a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr); 4625a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private);CHKERRQ(ierr); 4635a655dc6SBarry Smith ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr); 464a4d4d686SBarry Smith PLogObjectParent(*J,mfctx->w); 4659a6cb015SBarry Smith 4669a6cb015SBarry Smith mfctx->mat = *J; 4679a6cb015SBarry Smith PetscFunctionReturn(0); 4689a6cb015SBarry Smith } 4699a6cb015SBarry Smith 4709a6cb015SBarry Smith #undef __FUNC__ 471b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetFromOptions" 4729a6cb015SBarry Smith /*@ 4735a655dc6SBarry Smith MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line 4749a6cb015SBarry Smith parameter. 4759a6cb015SBarry Smith 4769a6cb015SBarry Smith Collective on Mat 4779a6cb015SBarry Smith 4789a6cb015SBarry Smith Input Parameters: 4795a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 4805a655dc6SBarry Smith 4815a655dc6SBarry Smith Options Database Keys: 4825a655dc6SBarry Smith + -snes_mf_type - <default,wp> 4835a655dc6SBarry Smith - -snes_mf_err - square root of estimated relative error in function evaluation 484329f5518SBarry Smith - -snes_mf_period - how often h is recomputed, defaults to 1, everytime 4859a6cb015SBarry Smith 48615091d37SBarry Smith Level: advanced 48715091d37SBarry Smith 4889a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters 4899a6cb015SBarry Smith 4905a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 4915a655dc6SBarry Smith MatSNESMFResetHHistory(), MatSNESMFKSPMonitor() 4929a6cb015SBarry Smith @*/ 4935a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat) 4949a6cb015SBarry Smith { 4955a655dc6SBarry Smith MatSNESMFCtx mfctx; 496f1af5d2fSBarry Smith int ierr; 497f1af5d2fSBarry Smith PetscTruth flg; 498*1d1367b7SBarry Smith char ftype[256],p[64],*prefix; 499*1d1367b7SBarry Smith MPI_Comm comm; 5009a6cb015SBarry Smith 5019a6cb015SBarry Smith PetscFunctionBegin; 5029a6cb015SBarry Smith ierr = MatShellGetContext(mat,(void **)&mfctx);CHKERRQ(ierr); 5039a6cb015SBarry Smith if (mfctx) { 504*1d1367b7SBarry Smith comm = mfctx->comm; 505*1d1367b7SBarry Smith prefix = mfctx->prefix; 5069a6cb015SBarry Smith /* allow user to set the type */ 507*1d1367b7SBarry Smith ierr = OptionsGetString(prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr); 5089a6cb015SBarry Smith if (flg) { 5095a655dc6SBarry Smith ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr); 5109a6cb015SBarry Smith } 5119a6cb015SBarry Smith 512*1d1367b7SBarry Smith ierr = OptionsGetDouble(prefix,"-snes_mf_err",&mfctx->error_rel,PETSC_NULL);CHKERRQ(ierr); 513*1d1367b7SBarry Smith ierr = OptionsGetInt(prefix,"-snes_mf_period",&mfctx->recomputeperiod,PETSC_NULL);CHKERRQ(ierr); 5149a6cb015SBarry Smith if (mfctx->ops->setfromoptions) { 5159a6cb015SBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 5169a6cb015SBarry Smith } 5179a6cb015SBarry Smith 5189a6cb015SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 519d132466eSBarry Smith ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 520*1d1367b7SBarry Smith if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);} 5219a6cb015SBarry Smith if (flg) { 522*1d1367b7SBarry Smith ierr = (*PetscHelpPrintf)(comm," %ssnes_mf_err <err>: set sqrt rel error in function (default %g)\n",p,mfctx->error_rel);CHKERRQ(ierr); 523*1d1367b7SBarry Smith ierr = (*PetscHelpPrintf)(comm," %ssnes_mf_period <p>: how often h is recomputed (default 1, everytime)\n",p);CHKERRQ(ierr); 5249a6cb015SBarry Smith if (mfctx->ops->printhelp) { 525d132466eSBarry Smith ierr = (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr); 5269a6cb015SBarry Smith } 5279a6cb015SBarry Smith } 5289a6cb015SBarry Smith } 529a4d4d686SBarry Smith PetscFunctionReturn(0); 530a4d4d686SBarry Smith } 531a4d4d686SBarry Smith 532a4d4d686SBarry Smith #undef __FUNC__ 533b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFGetH" 534a4d4d686SBarry Smith /*@ 53565f2ba5bSLois Curfman McInnes MatSNESMFGetH - Gets the last value that was used as the differencing 536a4d4d686SBarry Smith parameter. 537a4d4d686SBarry Smith 538a4d4d686SBarry Smith Not Collective 539a4d4d686SBarry Smith 540a4d4d686SBarry Smith Input Parameters: 5415a655dc6SBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 542a4d4d686SBarry Smith 543a4d4d686SBarry Smith Output Paramter: 544a4d4d686SBarry Smith . h - the differencing step size 545a4d4d686SBarry Smith 54615091d37SBarry Smith Level: advanced 54715091d37SBarry Smith 548a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 549a4d4d686SBarry Smith 5505a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(), 5515a655dc6SBarry Smith MatSNESMFResetHHistory(),MatSNESMFKSPMonitor() 552a4d4d686SBarry Smith @*/ 5535a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h) 554a4d4d686SBarry Smith { 5555a655dc6SBarry Smith MatSNESMFCtx ctx; 556a4d4d686SBarry Smith int ierr; 557a4d4d686SBarry Smith 558a4d4d686SBarry Smith PetscFunctionBegin; 559a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 560a4d4d686SBarry Smith if (ctx) { 561a4d4d686SBarry Smith *h = ctx->currenth; 562a4d4d686SBarry Smith } 563a4d4d686SBarry Smith PetscFunctionReturn(0); 564a4d4d686SBarry Smith } 565a4d4d686SBarry Smith 566a4d4d686SBarry Smith #undef __FUNC__ 567b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFKSPMonitor" 568a4d4d686SBarry Smith /* 5695a655dc6SBarry Smith MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc 57065f2ba5bSLois Curfman McInnes SNES matrix free routines. Prints the differencing parameter used at 57165f2ba5bSLois Curfman McInnes each step. 572a4d4d686SBarry Smith */ 573329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy) 574a4d4d686SBarry Smith { 575a4d4d686SBarry Smith PC pc; 5765a655dc6SBarry Smith MatSNESMFCtx ctx; 577a4d4d686SBarry Smith int ierr; 578a4d4d686SBarry Smith Mat mat; 579a4d4d686SBarry Smith MPI_Comm comm; 580a4d4d686SBarry Smith PetscTruth nonzeroinitialguess; 581a4d4d686SBarry Smith 582a4d4d686SBarry Smith PetscFunctionBegin; 583a4d4d686SBarry Smith ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 584a4d4d686SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 585a4d4d686SBarry Smith ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr); 586a4d4d686SBarry Smith ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 587a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 5889a6cb015SBarry Smith if (!ctx) { 5899a6cb015SBarry Smith SETERRQ(1,1,"Matrix is not a matrix free shell matrix"); 5909a6cb015SBarry Smith } 591a4d4d686SBarry Smith if (n > 0 || nonzeroinitialguess) { 592aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 593d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm, 594329f5518SBarry Smith PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr); 595a4d4d686SBarry Smith #else 596d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr); 597a4d4d686SBarry Smith #endif 598a4d4d686SBarry Smith } else { 599d132466eSBarry Smith ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr); 600a4d4d686SBarry Smith } 601a4d4d686SBarry Smith PetscFunctionReturn(0); 602a4d4d686SBarry Smith } 603a4d4d686SBarry Smith 604a4d4d686SBarry Smith #undef __FUNC__ 605b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetFunction" 60685614651SBarry Smith /*@C 60785614651SBarry Smith MatSNESMFSetFunction - Sets the function used in applying the matrix free. 60885614651SBarry Smith 60985614651SBarry Smith Collective on Mat 61085614651SBarry Smith 61185614651SBarry Smith Input Parameters: 61285614651SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 61385614651SBarry Smith . v - workspace vector 61485614651SBarry Smith . func - the function to use 61585614651SBarry Smith - funcctx - optional function context passed to function 61685614651SBarry Smith 61785614651SBarry Smith Level: advanced 61885614651SBarry Smith 61985614651SBarry Smith Notes: 62085614651SBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 62185614651SBarry Smith matrix inside your compute Jacobian routine 62285614651SBarry Smith 62385614651SBarry Smith If this is not set then it will use the function set with SNESSetFunction() 62485614651SBarry Smith 62585614651SBarry Smith .keywords: SNES, matrix-free, function 62685614651SBarry Smith 62785614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 62885614651SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 62985614651SBarry Smith MatSNESMFKSPMonitor(), SNESetFunction() 63085614651SBarry Smith @*/ 63185614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx) 63285614651SBarry Smith { 63385614651SBarry Smith MatSNESMFCtx ctx; 63485614651SBarry Smith int ierr; 63585614651SBarry Smith 63685614651SBarry Smith PetscFunctionBegin; 63785614651SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 63885614651SBarry Smith if (ctx) { 63985614651SBarry Smith ctx->func = func; 64085614651SBarry Smith ctx->funcctx = funcctx; 64185614651SBarry Smith ctx->funcvec = v; 64285614651SBarry Smith } 64385614651SBarry Smith PetscFunctionReturn(0); 64485614651SBarry Smith } 64585614651SBarry Smith 64685614651SBarry Smith 64785614651SBarry Smith #undef __FUNC__ 648b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetPeriod" 649329f5518SBarry Smith /*@ 650329f5518SBarry Smith MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime 651329f5518SBarry Smith 652329f5518SBarry Smith Collective on Mat 653329f5518SBarry Smith 654329f5518SBarry Smith Input Parameters: 655329f5518SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 656329f5518SBarry Smith - period - 1 for everytime, 2 for every second etc 657329f5518SBarry Smith 658329f5518SBarry Smith Options Database Keys: 659329f5518SBarry Smith + -snes_mf_period <period> 660329f5518SBarry Smith 661329f5518SBarry Smith Level: advanced 662329f5518SBarry Smith 663329f5518SBarry Smith 664329f5518SBarry Smith .keywords: SNES, matrix-free, parameters 665329f5518SBarry Smith 666329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 667329f5518SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 668329f5518SBarry Smith MatSNESMFKSPMonitor() 669329f5518SBarry Smith @*/ 670329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period) 671329f5518SBarry Smith { 672329f5518SBarry Smith MatSNESMFCtx ctx; 673329f5518SBarry Smith int ierr; 674329f5518SBarry Smith 675329f5518SBarry Smith PetscFunctionBegin; 676329f5518SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 677329f5518SBarry Smith if (ctx) { 678329f5518SBarry Smith ctx->recomputeperiod = period; 679329f5518SBarry Smith } 680329f5518SBarry Smith PetscFunctionReturn(0); 681329f5518SBarry Smith } 682329f5518SBarry Smith 683329f5518SBarry Smith #undef __FUNC__ 684b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetFunctionError" 685a4d4d686SBarry Smith /*@ 6865a655dc6SBarry Smith MatSNESMFSetFunctionError - Sets the error_rel for the approximation of 687a4d4d686SBarry Smith matrix-vector products using finite differences. 688a4d4d686SBarry Smith 689a4d4d686SBarry Smith Collective on Mat 690a4d4d686SBarry Smith 691a4d4d686SBarry Smith Input Parameters: 6925a655dc6SBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 6939a6cb015SBarry Smith - error_rel - relative error (should be set to the square root of 694a4d4d686SBarry Smith the relative error in the function evaluations) 695a4d4d686SBarry Smith 69615091d37SBarry Smith Options Database Keys: 69715091d37SBarry Smith + -snes_mf_err <error_rel> - Sets error_rel 69815091d37SBarry Smith 69915091d37SBarry Smith Level: advanced 70015091d37SBarry Smith 701a4d4d686SBarry Smith Notes: 702a4d4d686SBarry Smith The default matrix-free matrix-vector product routine computes 703a4d4d686SBarry Smith .vb 70465f2ba5bSLois Curfman McInnes F'(u)*a = [F(u+h*a) - F(u)]/h where 705a4d4d686SBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 706a4d4d686SBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 707a4d4d686SBarry Smith .ve 708a4d4d686SBarry Smith 709a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters 710a4d4d686SBarry Smith 7115a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(), 7125a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 7135a655dc6SBarry Smith MatSNESMFKSPMonitor() 714a4d4d686SBarry Smith @*/ 715329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error) 716a4d4d686SBarry Smith { 7175a655dc6SBarry Smith MatSNESMFCtx ctx; 718a4d4d686SBarry Smith int ierr; 719a4d4d686SBarry Smith 720a4d4d686SBarry Smith PetscFunctionBegin; 721a4d4d686SBarry Smith ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); 722a4d4d686SBarry Smith if (ctx) { 723a4d4d686SBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 724a4d4d686SBarry Smith } 725a4d4d686SBarry Smith PetscFunctionReturn(0); 726a4d4d686SBarry Smith } 727a4d4d686SBarry Smith 728a4d4d686SBarry Smith #undef __FUNC__ 729b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFAddNullSpace" 730a4d4d686SBarry Smith /*@ 73165f2ba5bSLois Curfman McInnes MatSNESMFAddNullSpace - Provides a null space that an operator is 73265f2ba5bSLois Curfman McInnes supposed to have. Since roundoff will create a small component in 73365f2ba5bSLois Curfman McInnes the null space, if you know the null space you may have it 73465f2ba5bSLois Curfman McInnes automatically removed. 735a4d4d686SBarry Smith 736a4d4d686SBarry Smith Collective on Mat 737a4d4d686SBarry Smith 738a4d4d686SBarry Smith Input Parameters: 739a4d4d686SBarry Smith + J - the matrix-free matrix context 74074637425SBarry Smith - nullsp - object created with MatNullSpaceCreate() 741a4d4d686SBarry Smith 74215091d37SBarry Smith Level: advanced 74315091d37SBarry Smith 744a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space 745a4d4d686SBarry Smith 74674637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(), 7475a655dc6SBarry Smith MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), 7485a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFErrorRel() 749a4d4d686SBarry Smith @*/ 75074637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp) 751a4d4d686SBarry Smith { 752a4d4d686SBarry Smith int ierr; 7535a655dc6SBarry Smith MatSNESMFCtx ctx; 754a4d4d686SBarry Smith MPI_Comm comm; 755a4d4d686SBarry Smith 756a4d4d686SBarry Smith PetscFunctionBegin; 7572d0c0e3bSBarry Smith ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr); 758a4d4d686SBarry Smith 759a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 760a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 761a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 76285614651SBarry Smith ctx->sp = nullsp; 76385614651SBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 764a4d4d686SBarry Smith PetscFunctionReturn(0); 765a4d4d686SBarry Smith } 766a4d4d686SBarry Smith 767a4d4d686SBarry Smith #undef __FUNC__ 768b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetHHistory" 769a4d4d686SBarry Smith /*@ 77065f2ba5bSLois Curfman McInnes MatSNESMFSetHHistory - Sets an array to collect a history of the 77165f2ba5bSLois Curfman McInnes differencing values (h) computed for the matrix-free product. 772a4d4d686SBarry Smith 773a4d4d686SBarry Smith Collective on Mat 774a4d4d686SBarry Smith 775a4d4d686SBarry Smith Input Parameters: 776a4d4d686SBarry Smith + J - the matrix-free matrix context 77765f2ba5bSLois Curfman McInnes . histroy - space to hold the history 77865f2ba5bSLois Curfman McInnes - nhistory - number of entries in history, if more entries are generated than 77965f2ba5bSLois Curfman McInnes nhistory, then the later ones are discarded 780a4d4d686SBarry Smith 78115091d37SBarry Smith Level: advanced 78215091d37SBarry Smith 783a4d4d686SBarry Smith Notes: 78465f2ba5bSLois Curfman McInnes Use MatSNESMFResetHHistory() to reset the history counter and collect 78565f2ba5bSLois Curfman McInnes a new batch of differencing parameters, h. 786a4d4d686SBarry Smith 787a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 788a4d4d686SBarry Smith 7895a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 7905a655dc6SBarry Smith MatSNESMFResetHHistory(), 7915a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 792a4d4d686SBarry Smith 793a4d4d686SBarry Smith @*/ 7945a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory) 795a4d4d686SBarry Smith { 796a4d4d686SBarry Smith int ierr; 7975a655dc6SBarry Smith MatSNESMFCtx ctx; 798a4d4d686SBarry Smith 799a4d4d686SBarry Smith PetscFunctionBegin; 800a4d4d686SBarry Smith 801a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 802a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 803a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 804a4d4d686SBarry Smith ctx->historyh = history; 805a4d4d686SBarry Smith ctx->maxcurrenth = nhistory; 806a4d4d686SBarry Smith ctx->currenth = 0; 807a4d4d686SBarry Smith 808a4d4d686SBarry Smith PetscFunctionReturn(0); 809a4d4d686SBarry Smith } 810a4d4d686SBarry Smith 811a4d4d686SBarry Smith #undef __FUNC__ 812b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFResetHHistory" 813a4d4d686SBarry Smith /*@ 8145a655dc6SBarry Smith MatSNESMFResetHHistory - Resets the counter to zero to begin 815a4d4d686SBarry Smith collecting a new set of differencing histories. 816a4d4d686SBarry Smith 817a4d4d686SBarry Smith Collective on Mat 818a4d4d686SBarry Smith 819a4d4d686SBarry Smith Input Parameters: 820a4d4d686SBarry Smith . J - the matrix-free matrix context 821a4d4d686SBarry Smith 82215091d37SBarry Smith Level: advanced 82315091d37SBarry Smith 824a4d4d686SBarry Smith Notes: 82565f2ba5bSLois Curfman McInnes Use MatSNESMFSetHHistory() to create the original history counter. 826a4d4d686SBarry Smith 827a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history 828a4d4d686SBarry Smith 8295a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(), 8305a655dc6SBarry Smith MatSNESMFSetHHistory(), 8315a655dc6SBarry Smith MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError() 832a4d4d686SBarry Smith 833a4d4d686SBarry Smith @*/ 8345a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J) 835a4d4d686SBarry Smith { 836a4d4d686SBarry Smith int ierr; 8375a655dc6SBarry Smith MatSNESMFCtx ctx; 838a4d4d686SBarry Smith 839a4d4d686SBarry Smith PetscFunctionBegin; 840a4d4d686SBarry Smith 841a4d4d686SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 842a4d4d686SBarry Smith /* no context indicates that it is not the "matrix free" matrix type */ 843a4d4d686SBarry Smith if (!ctx) PetscFunctionReturn(0); 844be726c96SBarry Smith ctx->ncurrenth = 0; 845a4d4d686SBarry Smith 846a4d4d686SBarry Smith PetscFunctionReturn(0); 847a4d4d686SBarry Smith } 848a4d4d686SBarry Smith 849*1d1367b7SBarry Smith #undef __FUNC__ 850*1d1367b7SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFFormJacobian" 851*1d1367b7SBarry Smith int MatSNESMFFormJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 852*1d1367b7SBarry Smith { 853*1d1367b7SBarry Smith int ierr; 854*1d1367b7SBarry Smith PetscFunctionBegin; 855*1d1367b7SBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 856*1d1367b7SBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 857*1d1367b7SBarry Smith PetscFunctionReturn(0); 858*1d1367b7SBarry Smith } 859*1d1367b7SBarry Smith 860*1d1367b7SBarry Smith #undef __FUNC__ 861*1d1367b7SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatSNESMFSetBase" 862*1d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U) 863*1d1367b7SBarry Smith { 864*1d1367b7SBarry Smith int ierr; 865*1d1367b7SBarry Smith MatSNESMFCtx ctx; 866*1d1367b7SBarry Smith 867*1d1367b7SBarry Smith PetscFunctionBegin; 868*1d1367b7SBarry Smith PetscValidHeaderSpecific(J,MAT_COOKIE); 869*1d1367b7SBarry Smith PetscValidHeaderSpecific(U,VEC_COOKIE); 870*1d1367b7SBarry Smith 871*1d1367b7SBarry Smith ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr); 872*1d1367b7SBarry Smith ctx->current_u = U; 873*1d1367b7SBarry Smith PetscFunctionReturn(0); 874*1d1367b7SBarry Smith } 875