1e884886fSBarry Smith #define PETSCMAT_DLL 2e884886fSBarry Smith 37c4f633dSBarry Smith #include "private/matimpl.h" 47c4f633dSBarry Smith #include "../src/mat/impls/mffd/mffdimpl.h" /*I "petscmat.h" I*/ 5e884886fSBarry Smith 6b022a5c1SBarry Smith PetscFList MatMFFDList = 0; 7e884886fSBarry Smith PetscTruth MatMFFDRegisterAllCalled = PETSC_FALSE; 8e884886fSBarry Smith 90700a824SBarry Smith PetscClassId PETSCMAT_DLLEXPORT MATMFFD_CLASSID; 10166c7f25SBarry Smith PetscLogEvent MATMFFD_Mult; 11e884886fSBarry Smith 12b022a5c1SBarry Smith static PetscTruth MatMFFDPackageInitialized = PETSC_FALSE; 13b022a5c1SBarry Smith #undef __FUNCT__ 14b022a5c1SBarry Smith #define __FUNCT__ "MatMFFDFinalizePackage" 15b022a5c1SBarry Smith /*@C 162390153bSJed Brown MatMFFDFinalizePackage - This function destroys everything in the MatMFFD package. It is 17b022a5c1SBarry Smith called from PetscFinalize(). 18b022a5c1SBarry Smith 19b022a5c1SBarry Smith Level: developer 20b022a5c1SBarry Smith 212390153bSJed Brown .keywords: Petsc, destroy, package 22b022a5c1SBarry Smith .seealso: PetscFinalize() 23b022a5c1SBarry Smith @*/ 24b022a5c1SBarry Smith PetscErrorCode PETSC_DLLEXPORT MatMFFDFinalizePackage(void) 25b022a5c1SBarry Smith { 26b022a5c1SBarry Smith PetscFunctionBegin; 27b022a5c1SBarry Smith MatMFFDPackageInitialized = PETSC_FALSE; 28b022a5c1SBarry Smith MatMFFDRegisterAllCalled = PETSC_FALSE; 29b022a5c1SBarry Smith MatMFFDList = PETSC_NULL; 30b022a5c1SBarry Smith PetscFunctionReturn(0); 31b022a5c1SBarry Smith } 32b022a5c1SBarry Smith 33e884886fSBarry Smith #undef __FUNCT__ 343ec795f1SBarry Smith #define __FUNCT__ "MatMFFDInitializePackage" 353ec795f1SBarry Smith /*@C 363ec795f1SBarry Smith MatMFFDInitializePackage - This function initializes everything in the MatMFFD package. It is called 373ec795f1SBarry Smith from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to MatCreate_MFFD() 383ec795f1SBarry Smith when using static libraries. 393ec795f1SBarry Smith 403ec795f1SBarry Smith Input Parameter: 413ec795f1SBarry Smith . path - The dynamic library path, or PETSC_NULL 423ec795f1SBarry Smith 433ec795f1SBarry Smith Level: developer 443ec795f1SBarry Smith 453ec795f1SBarry Smith .keywords: Vec, initialize, package 463ec795f1SBarry Smith .seealso: PetscInitialize() 473ec795f1SBarry Smith @*/ 483ec795f1SBarry Smith PetscErrorCode PETSCVEC_DLLEXPORT MatMFFDInitializePackage(const char path[]) 493ec795f1SBarry Smith { 503ec795f1SBarry Smith char logList[256]; 513ec795f1SBarry Smith char *className; 523ec795f1SBarry Smith PetscTruth opt; 533ec795f1SBarry Smith PetscErrorCode ierr; 543ec795f1SBarry Smith 553ec795f1SBarry Smith PetscFunctionBegin; 56b022a5c1SBarry Smith if (MatMFFDPackageInitialized) PetscFunctionReturn(0); 57b022a5c1SBarry Smith MatMFFDPackageInitialized = PETSC_TRUE; 583ec795f1SBarry Smith /* Register Classes */ 590700a824SBarry Smith ierr = PetscClassIdRegister("MatMFFD",&MATMFFD_CLASSID);CHKERRQ(ierr); 603ec795f1SBarry Smith /* Register Constructors */ 613ec795f1SBarry Smith ierr = MatMFFDRegisterAll(path);CHKERRQ(ierr); 623ec795f1SBarry Smith /* Register Events */ 630700a824SBarry Smith ierr = PetscLogEventRegister("MatMult MF", MATMFFD_CLASSID,&MATMFFD_Mult);CHKERRQ(ierr); 643ec795f1SBarry Smith 653ec795f1SBarry Smith /* Process info exclusions */ 663ec795f1SBarry Smith ierr = PetscOptionsGetString(PETSC_NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); 673ec795f1SBarry Smith if (opt) { 683ec795f1SBarry Smith ierr = PetscStrstr(logList, "matmffd", &className);CHKERRQ(ierr); 693ec795f1SBarry Smith if (className) { 700700a824SBarry Smith ierr = PetscInfoDeactivateClass(MATMFFD_CLASSID);CHKERRQ(ierr); 713ec795f1SBarry Smith } 723ec795f1SBarry Smith } 733ec795f1SBarry Smith /* Process summary exclusions */ 743ec795f1SBarry Smith ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr); 753ec795f1SBarry Smith if (opt) { 763ec795f1SBarry Smith ierr = PetscStrstr(logList, "matmffd", &className);CHKERRQ(ierr); 773ec795f1SBarry Smith if (className) { 780700a824SBarry Smith ierr = PetscLogEventDeactivateClass(MATMFFD_CLASSID);CHKERRQ(ierr); 793ec795f1SBarry Smith } 803ec795f1SBarry Smith } 81b022a5c1SBarry Smith ierr = PetscRegisterFinalize(MatMFFDFinalizePackage);CHKERRQ(ierr); 823ec795f1SBarry Smith PetscFunctionReturn(0); 833ec795f1SBarry Smith } 843ec795f1SBarry Smith 853ec795f1SBarry Smith #undef __FUNCT__ 86e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetType" 87e884886fSBarry Smith /*@C 88e884886fSBarry Smith MatMFFDSetType - Sets the method that is used to compute the 89e884886fSBarry Smith differencing parameter for finite differene matrix-free formulations. 90e884886fSBarry Smith 91e884886fSBarry Smith Input Parameters: 92e884886fSBarry Smith + mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMFFD() 93e884886fSBarry Smith or MatSetType(mat,MATMFFD); 94e884886fSBarry Smith - ftype - the type requested, either MATMFFD_WP or MATMFFD_DS 95e884886fSBarry Smith 96e884886fSBarry Smith Level: advanced 97e884886fSBarry Smith 98e884886fSBarry Smith Notes: 99e884886fSBarry Smith For example, such routines can compute h for use in 100e884886fSBarry Smith Jacobian-vector products of the form 101e884886fSBarry Smith 102e884886fSBarry Smith F(x+ha) - F(x) 103e884886fSBarry Smith F'(u)a ~= ---------------- 104e884886fSBarry Smith h 105e884886fSBarry Smith 106d2d6cebeSBarry Smith .seealso: MatCreateSNESMF(), MatMFFDRegisterDynamic(), MatMFFDSetFunction() 107e884886fSBarry Smith @*/ 108a313700dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetType(Mat mat,const MatMFFDType ftype) 109e884886fSBarry Smith { 110e884886fSBarry Smith PetscErrorCode ierr,(*r)(MatMFFD); 111e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 112e884886fSBarry Smith PetscTruth match; 113e884886fSBarry Smith 114e884886fSBarry Smith PetscFunctionBegin; 1150700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 116e884886fSBarry Smith PetscValidCharPointer(ftype,2); 117e884886fSBarry Smith 118e884886fSBarry Smith /* already set, so just return */ 119e884886fSBarry Smith ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr); 120e884886fSBarry Smith if (match) PetscFunctionReturn(0); 121e884886fSBarry Smith 122e884886fSBarry Smith /* destroy the old one if it exists */ 123e884886fSBarry Smith if (ctx->ops->destroy) { 124e884886fSBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 125e884886fSBarry Smith } 126e884886fSBarry Smith 127b022a5c1SBarry Smith ierr = PetscFListFind(MatMFFDList,((PetscObject)ctx)->comm,ftype,(void (**)(void)) &r);CHKERRQ(ierr); 128*e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatMFFD type %s given",ftype); 129e884886fSBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 130e884886fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr); 131e884886fSBarry Smith PetscFunctionReturn(0); 132e884886fSBarry Smith } 133e884886fSBarry Smith 134e884886fSBarry Smith typedef PetscErrorCode (*FCN1)(void*,Vec); /* force argument to next function to not be extern C*/ 135e884886fSBarry Smith EXTERN_C_BEGIN 136e884886fSBarry Smith #undef __FUNCT__ 137c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase_MFFD" 138c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase_MFFD(Mat mat,FCN1 func) 139e884886fSBarry Smith { 140e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 141e884886fSBarry Smith 142e884886fSBarry Smith PetscFunctionBegin; 143e884886fSBarry Smith ctx->funcisetbase = func; 144e884886fSBarry Smith PetscFunctionReturn(0); 145e884886fSBarry Smith } 146e884886fSBarry Smith EXTERN_C_END 147e884886fSBarry Smith 148e884886fSBarry Smith typedef PetscErrorCode (*FCN2)(void*,PetscInt,Vec,PetscScalar*); /* force argument to next function to not be extern C*/ 149e884886fSBarry Smith EXTERN_C_BEGIN 150e884886fSBarry Smith #undef __FUNCT__ 151c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni_MFFD" 152c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni_MFFD(Mat mat,FCN2 funci) 153e884886fSBarry Smith { 154e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 155e884886fSBarry Smith 156e884886fSBarry Smith PetscFunctionBegin; 157e884886fSBarry Smith ctx->funci = funci; 158e884886fSBarry Smith PetscFunctionReturn(0); 159e884886fSBarry Smith } 160e884886fSBarry Smith EXTERN_C_END 161e884886fSBarry Smith 162e884886fSBarry Smith 163e884886fSBarry Smith #undef __FUNCT__ 164e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegister" 165e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatMFFD)) 166e884886fSBarry Smith { 167e884886fSBarry Smith PetscErrorCode ierr; 168e884886fSBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 169e884886fSBarry Smith 170e884886fSBarry Smith PetscFunctionBegin; 171e884886fSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 172b022a5c1SBarry Smith ierr = PetscFListAdd(&MatMFFDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 173e884886fSBarry Smith PetscFunctionReturn(0); 174e884886fSBarry Smith } 175e884886fSBarry Smith 176e884886fSBarry Smith 177e884886fSBarry Smith #undef __FUNCT__ 178e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegisterDestroy" 179e884886fSBarry Smith /*@C 180e884886fSBarry Smith MatMFFDRegisterDestroy - Frees the list of MatMFFD methods that were 181e884886fSBarry Smith registered by MatMFFDRegisterDynamic). 182e884886fSBarry Smith 183e884886fSBarry Smith Not Collective 184e884886fSBarry Smith 185e884886fSBarry Smith Level: developer 186e884886fSBarry Smith 187e884886fSBarry Smith .keywords: MatMFFD, register, destroy 188e884886fSBarry Smith 189e884886fSBarry Smith .seealso: MatMFFDRegisterDynamic), MatMFFDRegisterAll() 190e884886fSBarry Smith @*/ 191e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegisterDestroy(void) 192e884886fSBarry Smith { 193e884886fSBarry Smith PetscErrorCode ierr; 194e884886fSBarry Smith 195e884886fSBarry Smith PetscFunctionBegin; 196b022a5c1SBarry Smith ierr = PetscFListDestroy(&MatMFFDList);CHKERRQ(ierr); 197e884886fSBarry Smith MatMFFDRegisterAllCalled = PETSC_FALSE; 198e884886fSBarry Smith PetscFunctionReturn(0); 199e884886fSBarry Smith } 200e884886fSBarry Smith 201e884886fSBarry Smith /* ----------------------------------------------------------------------------------------*/ 202e884886fSBarry Smith #undef __FUNCT__ 203e884886fSBarry Smith #define __FUNCT__ "MatDestroy_MFFD" 204e884886fSBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat) 205e884886fSBarry Smith { 206e884886fSBarry Smith PetscErrorCode ierr; 207e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 208e884886fSBarry Smith 209e884886fSBarry Smith PetscFunctionBegin; 210e884886fSBarry Smith if (ctx->w) { 211e884886fSBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 212e884886fSBarry Smith } 213cfe22f5eSBarry Smith if (ctx->current_f_allocated) { 214cfe22f5eSBarry Smith ierr = VecDestroy(ctx->current_f); 215cfe22f5eSBarry Smith } 216e884886fSBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 217e884886fSBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 218e884886fSBarry Smith ierr = PetscHeaderDestroy(ctx);CHKERRQ(ierr); 219e884886fSBarry Smith 220e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetBase_C","",PETSC_NULL);CHKERRQ(ierr); 221e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C","",PETSC_NULL);CHKERRQ(ierr); 222e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioni_C","",PETSC_NULL);CHKERRQ(ierr); 223e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetCheckh_C","",PETSC_NULL);CHKERRQ(ierr); 224e884886fSBarry Smith 225e884886fSBarry Smith PetscFunctionReturn(0); 226e884886fSBarry Smith } 227e884886fSBarry Smith 228e884886fSBarry Smith #undef __FUNCT__ 229e884886fSBarry Smith #define __FUNCT__ "MatView_MFFD" 230e884886fSBarry Smith /* 231e884886fSBarry Smith MatMFFDView_MFFD - Views matrix-free parameters. 232e884886fSBarry Smith 233e884886fSBarry Smith */ 234e884886fSBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer) 235e884886fSBarry Smith { 236e884886fSBarry Smith PetscErrorCode ierr; 237e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 238e884886fSBarry Smith PetscTruth iascii; 239e884886fSBarry Smith 240e884886fSBarry Smith PetscFunctionBegin; 241e884886fSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 242e884886fSBarry Smith if (iascii) { 243e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," matrix-free approximation:\n");CHKERRQ(ierr); 244e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," err=%G (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 2457adad957SLisandro Dalcin if (!((PetscObject)ctx)->type_name) { 246e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," The compute h routine has not yet been set\n");CHKERRQ(ierr); 247e884886fSBarry Smith } else { 2487adad957SLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," Using %s compute h routine\n",((PetscObject)ctx)->type_name);CHKERRQ(ierr); 249e884886fSBarry Smith } 250e884886fSBarry Smith if (ctx->ops->view) { 251e884886fSBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 252e884886fSBarry Smith } 253e884886fSBarry Smith } else { 254*e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for matrix-free matrix",((PetscObject)viewer)->type_name); 255e884886fSBarry Smith } 256e884886fSBarry Smith PetscFunctionReturn(0); 257e884886fSBarry Smith } 258e884886fSBarry Smith 259e884886fSBarry Smith #undef __FUNCT__ 260e884886fSBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD" 261e884886fSBarry Smith /* 262e884886fSBarry Smith MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This 263e884886fSBarry Smith allows the user to indicate the beginning of a new linear solve by calling 264e884886fSBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 2651d0fab5eSBarry Smith MatCreateMFFD_WP() to properly compute ||U|| only the first time 266e884886fSBarry Smith in the linear solver rather than every time. 267e884886fSBarry Smith */ 268e884886fSBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt) 269e884886fSBarry Smith { 270e884886fSBarry Smith PetscErrorCode ierr; 271e884886fSBarry Smith MatMFFD j = (MatMFFD)J->data; 272e884886fSBarry Smith 273e884886fSBarry Smith PetscFunctionBegin; 274e884886fSBarry Smith ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr); 275e884886fSBarry Smith j->vshift = 0.0; 276e884886fSBarry Smith j->vscale = 1.0; 277e884886fSBarry Smith PetscFunctionReturn(0); 278e884886fSBarry Smith } 279e884886fSBarry Smith 280e884886fSBarry Smith #undef __FUNCT__ 281e884886fSBarry Smith #define __FUNCT__ "MatMult_MFFD" 282e884886fSBarry Smith /* 283e884886fSBarry Smith MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a: 284e884886fSBarry Smith 285e884886fSBarry Smith y ~= (F(u + ha) - F(u))/h, 286e884886fSBarry Smith where F = nonlinear function, as set by SNESSetFunction() 287e884886fSBarry Smith u = current iterate 288e884886fSBarry Smith h = difference interval 289e884886fSBarry Smith */ 290e884886fSBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y) 291e884886fSBarry Smith { 292e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 293e884886fSBarry Smith PetscScalar h; 294e884886fSBarry Smith Vec w,U,F; 295e884886fSBarry Smith PetscErrorCode ierr; 296e884886fSBarry Smith PetscTruth zeroa; 297e884886fSBarry Smith 298e884886fSBarry Smith PetscFunctionBegin; 299e884886fSBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 300e884886fSBarry Smith separate the performance monitoring from the cases that use conventional 301e884886fSBarry Smith storage. We may eventually modify event logging to associate events 302e884886fSBarry Smith with particular objects, hence alleviating the more general problem. */ 303e884886fSBarry Smith ierr = PetscLogEventBegin(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr); 304e884886fSBarry Smith 305e884886fSBarry Smith w = ctx->w; 306e884886fSBarry Smith U = ctx->current_u; 3073ec795f1SBarry Smith F = ctx->current_f; 308e884886fSBarry Smith /* 309e884886fSBarry Smith Compute differencing parameter 310e884886fSBarry Smith */ 311e884886fSBarry Smith if (!ctx->ops->compute) { 312e884886fSBarry Smith ierr = MatMFFDSetType(mat,MATMFFD_WP);CHKERRQ(ierr); 313e884886fSBarry Smith ierr = MatMFFDSetFromOptions(mat);CHKERRQ(ierr); 314e884886fSBarry Smith } 315e884886fSBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h,&zeroa);CHKERRQ(ierr); 316e884886fSBarry Smith if (zeroa) { 317e884886fSBarry Smith ierr = VecSet(y,0.0);CHKERRQ(ierr); 318e884886fSBarry Smith PetscFunctionReturn(0); 319e884886fSBarry Smith } 320e884886fSBarry Smith 321*e32f2f54SBarry Smith if (PetscIsInfOrNanScalar(h)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed Nan differencing parameter h"); 322e884886fSBarry Smith if (ctx->checkh) { 323e884886fSBarry Smith ierr = (*ctx->checkh)(ctx->checkhctx,U,a,&h);CHKERRQ(ierr); 324e884886fSBarry Smith } 325e884886fSBarry Smith 326e884886fSBarry Smith /* keep a record of the current differencing parameter h */ 327e884886fSBarry Smith ctx->currenth = h; 328e884886fSBarry Smith #if defined(PETSC_USE_COMPLEX) 329e884886fSBarry Smith ierr = PetscInfo2(mat,"Current differencing parameter: %G + %G i\n",PetscRealPart(h),PetscImaginaryPart(h));CHKERRQ(ierr); 330e884886fSBarry Smith #else 331e884886fSBarry Smith ierr = PetscInfo1(mat,"Current differencing parameter: %15.12e\n",h);CHKERRQ(ierr); 332e884886fSBarry Smith #endif 333e884886fSBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 334e884886fSBarry Smith ctx->historyh[ctx->ncurrenth] = h; 335e884886fSBarry Smith } 336e884886fSBarry Smith ctx->ncurrenth++; 337e884886fSBarry Smith 338e884886fSBarry Smith /* w = u + ha */ 339e884886fSBarry Smith ierr = VecWAXPY(w,h,a,U);CHKERRQ(ierr); 340e884886fSBarry Smith 341bcddec3dSBarry Smith /* compute func(U) as base for differencing; only needed first time in and not when provided by user */ 342bcddec3dSBarry Smith if (ctx->ncurrenth == 1 && ctx->current_f_allocated) { 343e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,U,F);CHKERRQ(ierr); 34452121784SBarry Smith } 345e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,w,y);CHKERRQ(ierr); 346e884886fSBarry Smith 347e884886fSBarry Smith ierr = VecAXPY(y,-1.0,F);CHKERRQ(ierr); 348e884886fSBarry Smith ierr = VecScale(y,1.0/h);CHKERRQ(ierr); 349e884886fSBarry Smith 350e884886fSBarry Smith ierr = VecAXPBY(y,ctx->vshift,ctx->vscale,a);CHKERRQ(ierr); 351e884886fSBarry Smith 352e884886fSBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 353e884886fSBarry Smith 354e884886fSBarry Smith ierr = PetscLogEventEnd(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr); 355e884886fSBarry Smith PetscFunctionReturn(0); 356e884886fSBarry Smith } 357e884886fSBarry Smith 358e884886fSBarry Smith #undef __FUNCT__ 359e884886fSBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD" 360e884886fSBarry Smith /* 361e884886fSBarry Smith MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix 362e884886fSBarry Smith 363e884886fSBarry Smith y ~= (F(u + ha) - F(u))/h, 364e884886fSBarry Smith where F = nonlinear function, as set by SNESSetFunction() 365e884886fSBarry Smith u = current iterate 366e884886fSBarry Smith h = difference interval 367e884886fSBarry Smith */ 368e884886fSBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a) 369e884886fSBarry Smith { 370e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 371e884886fSBarry Smith PetscScalar h,*aa,*ww,v; 372e884886fSBarry Smith PetscReal epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON; 373e884886fSBarry Smith Vec w,U; 374e884886fSBarry Smith PetscErrorCode ierr; 375e884886fSBarry Smith PetscInt i,rstart,rend; 376e884886fSBarry Smith 377e884886fSBarry Smith PetscFunctionBegin; 378e884886fSBarry Smith if (!ctx->funci) { 379*e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Requires calling MatMFFDSetFunctioni() first"); 380e884886fSBarry Smith } 381e884886fSBarry Smith 382e884886fSBarry Smith w = ctx->w; 383e884886fSBarry Smith U = ctx->current_u; 384e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,U,a);CHKERRQ(ierr); 385e884886fSBarry Smith ierr = (*ctx->funcisetbase)(ctx->funcctx,U);CHKERRQ(ierr); 386e884886fSBarry Smith ierr = VecCopy(U,w);CHKERRQ(ierr); 387e884886fSBarry Smith 388e884886fSBarry Smith ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr); 389e884886fSBarry Smith ierr = VecGetArray(a,&aa);CHKERRQ(ierr); 390e884886fSBarry Smith for (i=rstart; i<rend; i++) { 391e884886fSBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 392e884886fSBarry Smith h = ww[i-rstart]; 393e884886fSBarry Smith if (h == 0.0) h = 1.0; 394e884886fSBarry Smith #if !defined(PETSC_USE_COMPLEX) 395e884886fSBarry Smith if (h < umin && h >= 0.0) h = umin; 396e884886fSBarry Smith else if (h < 0.0 && h > -umin) h = -umin; 397e884886fSBarry Smith #else 398e884886fSBarry Smith if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin; 399e884886fSBarry Smith else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin; 400e884886fSBarry Smith #endif 401e884886fSBarry Smith h *= epsilon; 402e884886fSBarry Smith 403e884886fSBarry Smith ww[i-rstart] += h; 404e884886fSBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 405e884886fSBarry Smith ierr = (*ctx->funci)(ctx->funcctx,i,w,&v);CHKERRQ(ierr); 406e884886fSBarry Smith aa[i-rstart] = (v - aa[i-rstart])/h; 407e884886fSBarry Smith 408e884886fSBarry Smith /* possibly shift and scale result */ 409e884886fSBarry Smith aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart]; 410e884886fSBarry Smith 411e884886fSBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 412e884886fSBarry Smith ww[i-rstart] -= h; 413e884886fSBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 414e884886fSBarry Smith } 415e884886fSBarry Smith ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr); 416e884886fSBarry Smith PetscFunctionReturn(0); 417e884886fSBarry Smith } 418e884886fSBarry Smith 419e884886fSBarry Smith #undef __FUNCT__ 420e884886fSBarry Smith #define __FUNCT__ "MatShift_MFFD" 421e884886fSBarry Smith PetscErrorCode MatShift_MFFD(Mat Y,PetscScalar a) 422e884886fSBarry Smith { 423e884886fSBarry Smith MatMFFD shell = (MatMFFD)Y->data; 424e884886fSBarry Smith PetscFunctionBegin; 425e884886fSBarry Smith shell->vshift += a; 426e884886fSBarry Smith PetscFunctionReturn(0); 427e884886fSBarry Smith } 428e884886fSBarry Smith 429e884886fSBarry Smith #undef __FUNCT__ 430e884886fSBarry Smith #define __FUNCT__ "MatScale_MFFD" 431e884886fSBarry Smith PetscErrorCode MatScale_MFFD(Mat Y,PetscScalar a) 432e884886fSBarry Smith { 433e884886fSBarry Smith MatMFFD shell = (MatMFFD)Y->data; 434e884886fSBarry Smith PetscFunctionBegin; 435e884886fSBarry Smith shell->vscale *= a; 436e884886fSBarry Smith PetscFunctionReturn(0); 437e884886fSBarry Smith } 438e884886fSBarry Smith 439e884886fSBarry Smith EXTERN_C_BEGIN 440e884886fSBarry Smith #undef __FUNCT__ 441c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetBase_MFFD" 442c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_MFFD(Mat J,Vec U,Vec F) 443e884886fSBarry Smith { 444e884886fSBarry Smith PetscErrorCode ierr; 445e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 446e884886fSBarry Smith 447e884886fSBarry Smith PetscFunctionBegin; 448e884886fSBarry Smith ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr); 449e884886fSBarry Smith ctx->current_u = U; 45052121784SBarry Smith if (F) { 451cfe22f5eSBarry Smith if (ctx->current_f_allocated) {ierr = VecDestroy(ctx->current_f);CHKERRQ(ierr);} 4523ec795f1SBarry Smith ctx->current_f = F; 453cfe22f5eSBarry Smith ctx->current_f_allocated = PETSC_FALSE; 454cfe22f5eSBarry Smith } else if (!ctx->current_f_allocated) { 45552121784SBarry Smith ierr = VecDuplicate(ctx->current_u, &ctx->current_f);CHKERRQ(ierr); 456cfe22f5eSBarry Smith ctx->current_f_allocated = PETSC_TRUE; 45752121784SBarry Smith } 458e884886fSBarry Smith if (!ctx->w) { 459e884886fSBarry Smith ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr); 460e884886fSBarry Smith } 461e884886fSBarry Smith J->assembled = PETSC_TRUE; 462e884886fSBarry Smith PetscFunctionReturn(0); 463e884886fSBarry Smith } 464e884886fSBarry Smith EXTERN_C_END 465e884886fSBarry Smith typedef PetscErrorCode (*FCN3)(void*,Vec,Vec,PetscScalar*); /* force argument to next function to not be extern C*/ 466e884886fSBarry Smith EXTERN_C_BEGIN 467e884886fSBarry Smith #undef __FUNCT__ 468c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh_MFFD" 469c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh_MFFD(Mat J,FCN3 fun,void*ectx) 470e884886fSBarry Smith { 471e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 472e884886fSBarry Smith 473e884886fSBarry Smith PetscFunctionBegin; 474e884886fSBarry Smith ctx->checkh = fun; 475e884886fSBarry Smith ctx->checkhctx = ectx; 476e884886fSBarry Smith PetscFunctionReturn(0); 477e884886fSBarry Smith } 478e884886fSBarry Smith EXTERN_C_END 479e884886fSBarry Smith 480e884886fSBarry Smith #undef __FUNCT__ 4816aa9148fSLisandro Dalcin #define __FUNCT__ "MatMFFDSetOptionsPrefix" 4826aa9148fSLisandro Dalcin /*@C 4836aa9148fSLisandro Dalcin MatMFFDSetOptionsPrefix - Sets the prefix used for searching for all 4846aa9148fSLisandro Dalcin MatMFFD options in the database. 4856aa9148fSLisandro Dalcin 4866aa9148fSLisandro Dalcin Collective on Mat 4876aa9148fSLisandro Dalcin 4886aa9148fSLisandro Dalcin Input Parameter: 4896aa9148fSLisandro Dalcin + A - the Mat context 4906aa9148fSLisandro Dalcin - prefix - the prefix to prepend to all option names 4916aa9148fSLisandro Dalcin 4926aa9148fSLisandro Dalcin Notes: 4936aa9148fSLisandro Dalcin A hyphen (-) must NOT be given at the beginning of the prefix name. 4946aa9148fSLisandro Dalcin The first character of all runtime options is AUTOMATICALLY the hyphen. 4956aa9148fSLisandro Dalcin 4966aa9148fSLisandro Dalcin Level: advanced 4976aa9148fSLisandro Dalcin 4986aa9148fSLisandro Dalcin .keywords: SNES, matrix-free, parameters 4996aa9148fSLisandro Dalcin 5006aa9148fSLisandro Dalcin .seealso: MatMFFDSetFromOptions(), MatCreateSNESMF() 5016aa9148fSLisandro Dalcin @*/ 5026aa9148fSLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetOptionsPrefix(Mat mat,const char prefix[]) 5036aa9148fSLisandro Dalcin 5046aa9148fSLisandro Dalcin { 5056aa9148fSLisandro Dalcin MatMFFD mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL; 5066aa9148fSLisandro Dalcin PetscErrorCode ierr; 5076aa9148fSLisandro Dalcin PetscFunctionBegin; 5080700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5090700a824SBarry Smith PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1); 5106aa9148fSLisandro Dalcin ierr = PetscObjectSetOptionsPrefix((PetscObject)mfctx,prefix);CHKERRQ(ierr); 5116aa9148fSLisandro Dalcin PetscFunctionReturn(0); 5126aa9148fSLisandro Dalcin } 5136aa9148fSLisandro Dalcin 5146aa9148fSLisandro Dalcin #undef __FUNCT__ 515e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFromOptions" 516e884886fSBarry Smith /*@ 517e884886fSBarry Smith MatMFFDSetFromOptions - Sets the MatMFFD options from the command line 518e884886fSBarry Smith parameter. 519e884886fSBarry Smith 520e884886fSBarry Smith Collective on Mat 521e884886fSBarry Smith 522e884886fSBarry Smith Input Parameters: 523e884886fSBarry Smith . mat - the matrix obtained with MatCreateMFFD() or MatCreateSNESMF() 524e884886fSBarry Smith 525e884886fSBarry Smith Options Database Keys: 526e884886fSBarry Smith + -mat_mffd_type - wp or ds (see MATMFFD_WP or MATMFFD_DS) 527e884886fSBarry Smith - -mat_mffd_err - square root of estimated relative error in function evaluation 528e884886fSBarry Smith - -mat_mffd_period - how often h is recomputed, defaults to 1, everytime 529e884886fSBarry Smith 530e884886fSBarry Smith Level: advanced 531e884886fSBarry Smith 532e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 533e884886fSBarry Smith 5341d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatMFFDResetHHistory() 535e884886fSBarry Smith @*/ 536e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFromOptions(Mat mat) 537e884886fSBarry Smith { 5386aa9148fSLisandro Dalcin MatMFFD mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL; 539e884886fSBarry Smith PetscErrorCode ierr; 540e884886fSBarry Smith PetscTruth flg; 541e884886fSBarry Smith char ftype[256]; 542e884886fSBarry Smith 543e884886fSBarry Smith PetscFunctionBegin; 5440700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5450700a824SBarry Smith PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1); 5467adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)mfctx)->comm,((PetscObject)mfctx)->prefix,"Set matrix free computation parameters","MatMFFD");CHKERRQ(ierr); 547b022a5c1SBarry Smith ierr = PetscOptionsList("-mat_mffd_type","Matrix free type","MatMFFDSetType",MatMFFDList,((PetscObject)mfctx)->type_name,ftype,256,&flg);CHKERRQ(ierr); 548e884886fSBarry Smith if (flg) { 549e884886fSBarry Smith ierr = MatMFFDSetType(mat,ftype);CHKERRQ(ierr); 550e884886fSBarry Smith } 551e884886fSBarry Smith 552e884886fSBarry Smith ierr = PetscOptionsReal("-mat_mffd_err","set sqrt relative error in function","MatMFFDSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr); 553e884886fSBarry Smith ierr = PetscOptionsInt("-mat_mffd_period","how often h is recomputed","MatMFFDSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr); 554e884886fSBarry Smith 55590d69ab7SBarry Smith flg = PETSC_FALSE; 55690d69ab7SBarry Smith ierr = PetscOptionsTruth("-mat_mffd_check_positivity","Insure that U + h*a is nonnegative","MatMFFDSetCheckh",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 557e884886fSBarry Smith if (flg) { 558e884886fSBarry Smith ierr = MatMFFDSetCheckh(mat,MatMFFDCheckPositivity,0);CHKERRQ(ierr); 559e884886fSBarry Smith } 560e884886fSBarry Smith if (mfctx->ops->setfromoptions) { 561e884886fSBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 562e884886fSBarry Smith } 563e884886fSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 564e884886fSBarry Smith PetscFunctionReturn(0); 565e884886fSBarry Smith } 566e884886fSBarry Smith 567e884886fSBarry Smith /*MC 568e884886fSBarry Smith MATMFFD - MATMFFD = "mffd" - A matrix free matrix type. 569e884886fSBarry Smith 570e884886fSBarry Smith Level: advanced 571e884886fSBarry Smith 572d2d6cebeSBarry Smith .seealso: MatCreateMFFD(), MatCreateSNESMF(), MatMFFDSetFunction() 573e884886fSBarry Smith M*/ 574e884886fSBarry Smith EXTERN_C_BEGIN 575e884886fSBarry Smith #undef __FUNCT__ 576e884886fSBarry Smith #define __FUNCT__ "MatCreate_MFFD" 577e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MFFD(Mat A) 578e884886fSBarry Smith { 579e884886fSBarry Smith MatMFFD mfctx; 580e884886fSBarry Smith PetscErrorCode ierr; 581e884886fSBarry Smith 582e884886fSBarry Smith PetscFunctionBegin; 5833ec795f1SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES 5843ec795f1SBarry Smith ierr = MatMFFDInitializePackage(PETSC_NULL);CHKERRQ(ierr); 5853ec795f1SBarry Smith #endif 586e884886fSBarry Smith 5870700a824SBarry Smith ierr = PetscHeaderCreate(mfctx,_p_MatMFFD,struct _MFOps,MATMFFD_CLASSID,0,"MatMFFD",((PetscObject)A)->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr); 588e884886fSBarry Smith mfctx->sp = 0; 589e884886fSBarry Smith mfctx->error_rel = PETSC_SQRT_MACHINE_EPSILON; 590e884886fSBarry Smith mfctx->recomputeperiod = 1; 591e884886fSBarry Smith mfctx->count = 0; 592e884886fSBarry Smith mfctx->currenth = 0.0; 593e884886fSBarry Smith mfctx->historyh = PETSC_NULL; 594e884886fSBarry Smith mfctx->ncurrenth = 0; 595e884886fSBarry Smith mfctx->maxcurrenth = 0; 5967adad957SLisandro Dalcin ((PetscObject)mfctx)->type_name = 0; 597e884886fSBarry Smith 598e884886fSBarry Smith mfctx->vshift = 0.0; 599e884886fSBarry Smith mfctx->vscale = 1.0; 600e884886fSBarry Smith 601e884886fSBarry Smith /* 602e884886fSBarry Smith Create the empty data structure to contain compute-h routines. 603e884886fSBarry Smith These will be filled in below from the command line options or 604e884886fSBarry Smith a later call with MatMFFDSetType() or if that is not called 605e884886fSBarry Smith then it will default in the first use of MatMult_MFFD() 606e884886fSBarry Smith */ 607e884886fSBarry Smith mfctx->ops->compute = 0; 608e884886fSBarry Smith mfctx->ops->destroy = 0; 609e884886fSBarry Smith mfctx->ops->view = 0; 610e884886fSBarry Smith mfctx->ops->setfromoptions = 0; 611e884886fSBarry Smith mfctx->hctx = 0; 612e884886fSBarry Smith 613e884886fSBarry Smith mfctx->func = 0; 614e884886fSBarry Smith mfctx->funcctx = 0; 615e884886fSBarry Smith mfctx->w = PETSC_NULL; 616e884886fSBarry Smith 617e884886fSBarry Smith A->data = mfctx; 618e884886fSBarry Smith 619e884886fSBarry Smith A->ops->mult = MatMult_MFFD; 620e884886fSBarry Smith A->ops->destroy = MatDestroy_MFFD; 621e884886fSBarry Smith A->ops->view = MatView_MFFD; 622e884886fSBarry Smith A->ops->assemblyend = MatAssemblyEnd_MFFD; 623e884886fSBarry Smith A->ops->getdiagonal = MatGetDiagonal_MFFD; 624e884886fSBarry Smith A->ops->scale = MatScale_MFFD; 625e884886fSBarry Smith A->ops->shift = MatShift_MFFD; 626e884886fSBarry Smith A->ops->setfromoptions = MatMFFDSetFromOptions; 627e884886fSBarry Smith A->assembled = PETSC_TRUE; 628e884886fSBarry Smith 62926283091SBarry Smith ierr = PetscLayoutSetBlockSize(A->rmap,1);CHKERRQ(ierr); 63026283091SBarry Smith ierr = PetscLayoutSetBlockSize(A->cmap,1);CHKERRQ(ierr); 63126283091SBarry Smith ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 63226283091SBarry Smith ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 633ee1cef2cSJed Brown 634c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetBase_C","MatMFFDSetBase_MFFD",MatMFFDSetBase_MFFD);CHKERRQ(ierr); 635c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioniBase_C","MatMFFDSetFunctioniBase_MFFD",MatMFFDSetFunctioniBase_MFFD);CHKERRQ(ierr); 636c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioni_C","MatMFFDSetFunctioni_MFFD",MatMFFDSetFunctioni_MFFD);CHKERRQ(ierr); 637c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetCheckh_C","MatMFFDSetCheckh_MFFD",MatMFFDSetCheckh_MFFD);CHKERRQ(ierr); 638e884886fSBarry Smith mfctx->mat = A; 639e884886fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)A,MATMFFD);CHKERRQ(ierr); 640e884886fSBarry Smith PetscFunctionReturn(0); 641e884886fSBarry Smith } 642e884886fSBarry Smith EXTERN_C_END 643e884886fSBarry Smith 644e884886fSBarry Smith #undef __FUNCT__ 645e884886fSBarry Smith #define __FUNCT__ "MatCreateMFFD" 646e884886fSBarry Smith /*@ 647e884886fSBarry Smith MatCreateMFFD - Creates a matrix-free matrix. See also MatCreateSNESMF() 648e884886fSBarry Smith 649e884886fSBarry Smith Collective on Vec 650e884886fSBarry Smith 651e884886fSBarry Smith Input Parameters: 652fef1beadSBarry Smith + comm - MPI communicator 653fef1beadSBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 654fef1beadSBarry Smith This value should be the same as the local size used in creating the 655fef1beadSBarry Smith y vector for the matrix-vector product y = Ax. 656fef1beadSBarry Smith . n - This value should be the same as the local size used in creating the 657fef1beadSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 658fef1beadSBarry Smith calculated if N is given) For square matrices n is almost always m. 659fef1beadSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 660fef1beadSBarry Smith - N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 661fef1beadSBarry Smith 662e884886fSBarry Smith 663e884886fSBarry Smith Output Parameter: 664e884886fSBarry Smith . J - the matrix-free matrix 665e884886fSBarry Smith 666e884886fSBarry Smith Level: advanced 667e884886fSBarry Smith 668e884886fSBarry Smith Notes: 669e884886fSBarry Smith The matrix-free matrix context merely contains the function pointers 670e884886fSBarry Smith and work space for performing finite difference approximations of 671e884886fSBarry Smith Jacobian-vector products, F'(u)*a, 672e884886fSBarry Smith 673e884886fSBarry Smith The default code uses the following approach to compute h 674e884886fSBarry Smith 675e884886fSBarry Smith .vb 676e884886fSBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 677e884886fSBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 678e884886fSBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 679e884886fSBarry Smith where 680e884886fSBarry Smith error_rel = square root of relative error in function evaluation 681e884886fSBarry Smith umin = minimum iterate parameter 682e884886fSBarry Smith .ve 683e884886fSBarry Smith 684e884886fSBarry Smith The user can set the error_rel via MatMFFDSetFunctionError() and 6851d0fab5eSBarry Smith umin via MatMFFDDSSetUmin(); see the nonlinear solvers chapter 686e884886fSBarry Smith of the users manual for details. 687e884886fSBarry Smith 688e884886fSBarry Smith The user should call MatDestroy() when finished with the matrix-free 689e884886fSBarry Smith matrix context. 690e884886fSBarry Smith 691e884886fSBarry Smith Options Database Keys: 692e884886fSBarry Smith + -mat_mffd_err <error_rel> - Sets error_rel 693e884886fSBarry Smith . -mat_mffd_unim <umin> - Sets umin (for default PETSc routine that computes h only) 694e884886fSBarry Smith - -mat_mffd_check_positivity 695e884886fSBarry Smith 696e884886fSBarry Smith .keywords: default, matrix-free, create, matrix 697e884886fSBarry Smith 6981d0fab5eSBarry Smith .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDSSetUmin(), MatMFFDSetFunction() 699e884886fSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateSNESMF(), 7001d0fab5eSBarry Smith MatMFFDGetH(), MatMFFDRegisterDynamic), MatMFFDComputeJacobian() 701e884886fSBarry Smith 702e884886fSBarry Smith @*/ 703fef1beadSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMFFD(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,Mat *J) 704e884886fSBarry Smith { 705e884886fSBarry Smith PetscErrorCode ierr; 706e884886fSBarry Smith 707e884886fSBarry Smith PetscFunctionBegin; 708e884886fSBarry Smith ierr = MatCreate(comm,J);CHKERRQ(ierr); 709fef1beadSBarry Smith ierr = MatSetSizes(*J,m,n,M,N);CHKERRQ(ierr); 710e884886fSBarry Smith ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr); 711e884886fSBarry Smith PetscFunctionReturn(0); 712e884886fSBarry Smith } 713e884886fSBarry Smith 714e884886fSBarry Smith 715e884886fSBarry Smith #undef __FUNCT__ 716e884886fSBarry Smith #define __FUNCT__ "MatMFFDGetH" 717e884886fSBarry Smith /*@ 718e884886fSBarry Smith MatMFFDGetH - Gets the last value that was used as the differencing 719e884886fSBarry Smith parameter. 720e884886fSBarry Smith 721e884886fSBarry Smith Not Collective 722e884886fSBarry Smith 723e884886fSBarry Smith Input Parameters: 724e884886fSBarry Smith . mat - the matrix obtained with MatCreateSNESMF() 725e884886fSBarry Smith 726e884886fSBarry Smith Output Paramter: 727e884886fSBarry Smith . h - the differencing step size 728e884886fSBarry Smith 729e884886fSBarry Smith Level: advanced 730e884886fSBarry Smith 731e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 732e884886fSBarry Smith 7331d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatCreateMFFD(), MATMFFD, MatMFFDResetHHistory() 734e884886fSBarry Smith @*/ 735e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDGetH(Mat mat,PetscScalar *h) 736e884886fSBarry Smith { 737e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 738e884886fSBarry Smith 739e884886fSBarry Smith PetscFunctionBegin; 740e884886fSBarry Smith *h = ctx->currenth; 741e884886fSBarry Smith PetscFunctionReturn(0); 742e884886fSBarry Smith } 743e884886fSBarry Smith 744e884886fSBarry Smith 745e884886fSBarry Smith #undef __FUNCT__ 746e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunction" 747e884886fSBarry Smith /*@C 748e884886fSBarry Smith MatMFFDSetFunction - Sets the function used in applying the matrix free. 749e884886fSBarry Smith 750e884886fSBarry Smith Collective on Mat 751e884886fSBarry Smith 752e884886fSBarry Smith Input Parameters: 753e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 754e884886fSBarry Smith . func - the function to use 755e884886fSBarry Smith - funcctx - optional function context passed to function 756e884886fSBarry Smith 757e884886fSBarry Smith Level: advanced 758e884886fSBarry Smith 759e884886fSBarry Smith Notes: 760e884886fSBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 761e884886fSBarry Smith matrix inside your compute Jacobian routine 762e884886fSBarry Smith 7633ec795f1SBarry Smith If this is not set then it will use the function set with SNESSetFunction() if MatCreateSNESMF() was used. 764e884886fSBarry Smith 765e884886fSBarry Smith .keywords: SNES, matrix-free, function 766e884886fSBarry Smith 7671d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD, 7681d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction() 769e884886fSBarry Smith @*/ 7703ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunction(Mat mat,PetscErrorCode (*func)(void*,Vec,Vec),void *funcctx) 771e884886fSBarry Smith { 772e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 773e884886fSBarry Smith 774e884886fSBarry Smith PetscFunctionBegin; 775e884886fSBarry Smith ctx->func = func; 776e884886fSBarry Smith ctx->funcctx = funcctx; 777e884886fSBarry Smith PetscFunctionReturn(0); 778e884886fSBarry Smith } 779e884886fSBarry Smith 780e884886fSBarry Smith #undef __FUNCT__ 781e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni" 782e884886fSBarry Smith /*@C 783e884886fSBarry Smith MatMFFDSetFunctioni - Sets the function for a single component 784e884886fSBarry Smith 785e884886fSBarry Smith Collective on Mat 786e884886fSBarry Smith 787e884886fSBarry Smith Input Parameters: 788e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 789e884886fSBarry Smith - funci - the function to use 790e884886fSBarry Smith 791e884886fSBarry Smith Level: advanced 792e884886fSBarry Smith 793e884886fSBarry Smith Notes: 794e884886fSBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 795e884886fSBarry Smith matrix inside your compute Jacobian routine 796e884886fSBarry Smith 797e884886fSBarry Smith 798e884886fSBarry Smith .keywords: SNES, matrix-free, function 799e884886fSBarry Smith 8001d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction() 8011d0fab5eSBarry Smith 802e884886fSBarry Smith @*/ 803e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni(Mat mat,PetscErrorCode (*funci)(void*,PetscInt,Vec,PetscScalar*)) 804e884886fSBarry Smith { 805e884886fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,PetscInt,Vec,PetscScalar*)); 806e884886fSBarry Smith 807e884886fSBarry Smith PetscFunctionBegin; 8080700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 809e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr); 810e884886fSBarry Smith if (f) { 811e884886fSBarry Smith ierr = (*f)(mat,funci);CHKERRQ(ierr); 812e884886fSBarry Smith } 813e884886fSBarry Smith PetscFunctionReturn(0); 814e884886fSBarry Smith } 815e884886fSBarry Smith 816e884886fSBarry Smith 817e884886fSBarry Smith #undef __FUNCT__ 818e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase" 819e884886fSBarry Smith /*@C 820e884886fSBarry Smith MatMFFDSetFunctioniBase - Sets the base vector for a single component function evaluation 821e884886fSBarry Smith 822e884886fSBarry Smith Collective on Mat 823e884886fSBarry Smith 824e884886fSBarry Smith Input Parameters: 825e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 826e884886fSBarry Smith - func - the function to use 827e884886fSBarry Smith 828e884886fSBarry Smith Level: advanced 829e884886fSBarry Smith 830e884886fSBarry Smith Notes: 831e884886fSBarry Smith If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free 832e884886fSBarry Smith matrix inside your compute Jacobian routine 833e884886fSBarry Smith 834e884886fSBarry Smith 835e884886fSBarry Smith .keywords: SNES, matrix-free, function 836e884886fSBarry Smith 837e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD 8381d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction() 839e884886fSBarry Smith @*/ 840e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase(Mat mat,PetscErrorCode (*func)(void*,Vec)) 841e884886fSBarry Smith { 842e884886fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec)); 843e884886fSBarry Smith 844e884886fSBarry Smith PetscFunctionBegin; 8450700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 846e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr); 847e884886fSBarry Smith if (f) { 848e884886fSBarry Smith ierr = (*f)(mat,func);CHKERRQ(ierr); 849e884886fSBarry Smith } 850e884886fSBarry Smith PetscFunctionReturn(0); 851e884886fSBarry Smith } 852e884886fSBarry Smith 853e884886fSBarry Smith 854e884886fSBarry Smith #undef __FUNCT__ 855e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetPeriod" 856e884886fSBarry Smith /*@ 857e884886fSBarry Smith MatMFFDSetPeriod - Sets how often h is recomputed, by default it is everytime 858e884886fSBarry Smith 859e884886fSBarry Smith Collective on Mat 860e884886fSBarry Smith 861e884886fSBarry Smith Input Parameters: 862e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateSNESMF() 863e884886fSBarry Smith - period - 1 for everytime, 2 for every second etc 864e884886fSBarry Smith 865e884886fSBarry Smith Options Database Keys: 866e884886fSBarry Smith + -mat_mffd_period <period> 867e884886fSBarry Smith 868e884886fSBarry Smith Level: advanced 869e884886fSBarry Smith 870e884886fSBarry Smith 871e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 872e884886fSBarry Smith 873e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), 8741d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory() 875e884886fSBarry Smith @*/ 876e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetPeriod(Mat mat,PetscInt period) 877e884886fSBarry Smith { 878e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 879e884886fSBarry Smith 880e884886fSBarry Smith PetscFunctionBegin; 881e884886fSBarry Smith ctx->recomputeperiod = period; 882e884886fSBarry Smith PetscFunctionReturn(0); 883e884886fSBarry Smith } 884e884886fSBarry Smith 885e884886fSBarry Smith #undef __FUNCT__ 886e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctionError" 887e884886fSBarry Smith /*@ 888e884886fSBarry Smith MatMFFDSetFunctionError - Sets the error_rel for the approximation of 889e884886fSBarry Smith matrix-vector products using finite differences. 890e884886fSBarry Smith 891e884886fSBarry Smith Collective on Mat 892e884886fSBarry Smith 893e884886fSBarry Smith Input Parameters: 894e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateMFFD() or MatCreateSNESMF() 895e884886fSBarry Smith - error_rel - relative error (should be set to the square root of 896e884886fSBarry Smith the relative error in the function evaluations) 897e884886fSBarry Smith 898e884886fSBarry Smith Options Database Keys: 899e884886fSBarry Smith + -mat_mffd_err <error_rel> - Sets error_rel 900e884886fSBarry Smith 901e884886fSBarry Smith Level: advanced 902e884886fSBarry Smith 903e884886fSBarry Smith Notes: 904e884886fSBarry Smith The default matrix-free matrix-vector product routine computes 905e884886fSBarry Smith .vb 906e884886fSBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 907e884886fSBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 908e884886fSBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 909e884886fSBarry Smith .ve 910e884886fSBarry Smith 911e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 912e884886fSBarry Smith 913e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD 9141d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory() 915e884886fSBarry Smith @*/ 916e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctionError(Mat mat,PetscReal error) 917e884886fSBarry Smith { 918e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 919e884886fSBarry Smith 920e884886fSBarry Smith PetscFunctionBegin; 921e884886fSBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 922e884886fSBarry Smith PetscFunctionReturn(0); 923e884886fSBarry Smith } 924e884886fSBarry Smith 925e884886fSBarry Smith #undef __FUNCT__ 926e884886fSBarry Smith #define __FUNCT__ "MatMFFDAddNullSpace" 927e884886fSBarry Smith /*@ 928e884886fSBarry Smith MatMFFDAddNullSpace - Provides a null space that an operator is 929e884886fSBarry Smith supposed to have. Since roundoff will create a small component in 930e884886fSBarry Smith the null space, if you know the null space you may have it 931e884886fSBarry Smith automatically removed. 932e884886fSBarry Smith 933e884886fSBarry Smith Collective on Mat 934e884886fSBarry Smith 935e884886fSBarry Smith Input Parameters: 936e884886fSBarry Smith + J - the matrix-free matrix context 937e884886fSBarry Smith - nullsp - object created with MatNullSpaceCreate() 938e884886fSBarry Smith 939e884886fSBarry Smith Level: advanced 940e884886fSBarry Smith 941e884886fSBarry Smith .keywords: SNES, matrix-free, null space 942e884886fSBarry Smith 943e884886fSBarry Smith .seealso: MatNullSpaceCreate(), MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD 9441d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory() 945e884886fSBarry Smith @*/ 946e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDAddNullSpace(Mat J,MatNullSpace nullsp) 947e884886fSBarry Smith { 948e884886fSBarry Smith PetscErrorCode ierr; 949e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 950e884886fSBarry Smith 951e884886fSBarry Smith PetscFunctionBegin; 952e884886fSBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 953e884886fSBarry Smith if (ctx->sp) { ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr); } 954e884886fSBarry Smith ctx->sp = nullsp; 955e884886fSBarry Smith PetscFunctionReturn(0); 956e884886fSBarry Smith } 957e884886fSBarry Smith 958e884886fSBarry Smith #undef __FUNCT__ 959e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetHHistory" 960e884886fSBarry Smith /*@ 961e884886fSBarry Smith MatMFFDSetHHistory - Sets an array to collect a history of the 962e884886fSBarry Smith differencing values (h) computed for the matrix-free product. 963e884886fSBarry Smith 964e884886fSBarry Smith Collective on Mat 965e884886fSBarry Smith 966e884886fSBarry Smith Input Parameters: 967e884886fSBarry Smith + J - the matrix-free matrix context 968e884886fSBarry Smith . histroy - space to hold the history 969e884886fSBarry Smith - nhistory - number of entries in history, if more entries are generated than 970e884886fSBarry Smith nhistory, then the later ones are discarded 971e884886fSBarry Smith 972e884886fSBarry Smith Level: advanced 973e884886fSBarry Smith 974e884886fSBarry Smith Notes: 975e884886fSBarry Smith Use MatMFFDResetHHistory() to reset the history counter and collect 976e884886fSBarry Smith a new batch of differencing parameters, h. 977e884886fSBarry Smith 978e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history 979e884886fSBarry Smith 980e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), 9811d0fab5eSBarry Smith MatMFFDResetHHistory(), MatMFFDSetFunctionError() 982e884886fSBarry Smith 983e884886fSBarry Smith @*/ 984e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory) 985e884886fSBarry Smith { 986e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 987e884886fSBarry Smith 988e884886fSBarry Smith PetscFunctionBegin; 989e884886fSBarry Smith ctx->historyh = history; 990e884886fSBarry Smith ctx->maxcurrenth = nhistory; 99175567043SBarry Smith ctx->currenth = 0.; 992e884886fSBarry Smith PetscFunctionReturn(0); 993e884886fSBarry Smith } 994e884886fSBarry Smith 995e884886fSBarry Smith #undef __FUNCT__ 996e884886fSBarry Smith #define __FUNCT__ "MatMFFDResetHHistory" 997e884886fSBarry Smith /*@ 998e884886fSBarry Smith MatMFFDResetHHistory - Resets the counter to zero to begin 999e884886fSBarry Smith collecting a new set of differencing histories. 1000e884886fSBarry Smith 1001e884886fSBarry Smith Collective on Mat 1002e884886fSBarry Smith 1003e884886fSBarry Smith Input Parameters: 1004e884886fSBarry Smith . J - the matrix-free matrix context 1005e884886fSBarry Smith 1006e884886fSBarry Smith Level: advanced 1007e884886fSBarry Smith 1008e884886fSBarry Smith Notes: 1009e884886fSBarry Smith Use MatMFFDSetHHistory() to create the original history counter. 1010e884886fSBarry Smith 1011e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history 1012e884886fSBarry Smith 1013e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), 10141d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDSetFunctionError() 1015e884886fSBarry Smith 1016e884886fSBarry Smith @*/ 1017e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDResetHHistory(Mat J) 1018e884886fSBarry Smith { 1019e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 1020e884886fSBarry Smith 1021e884886fSBarry Smith PetscFunctionBegin; 1022e884886fSBarry Smith ctx->ncurrenth = 0; 1023e884886fSBarry Smith PetscFunctionReturn(0); 1024e884886fSBarry Smith } 1025e884886fSBarry Smith 1026e884886fSBarry Smith 1027e884886fSBarry Smith #undef __FUNCT__ 1028e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetBase" 1029e884886fSBarry Smith /*@ 1030e884886fSBarry Smith MatMFFDSetBase - Sets the vector U at which matrix vector products of the 1031e884886fSBarry Smith Jacobian are computed 1032e884886fSBarry Smith 1033e884886fSBarry Smith Collective on Mat 1034e884886fSBarry Smith 1035e884886fSBarry Smith Input Parameters: 1036e884886fSBarry Smith + J - the MatMFFD matrix 10373ec795f1SBarry Smith . U - the vector 1038bcddec3dSBarry Smith - F - (optional) vector that contains F(u) if it has been already computed 1039e884886fSBarry Smith 1040e884886fSBarry Smith Notes: This is rarely used directly 1041e884886fSBarry Smith 10428af5ae88SBarry Smith If F is provided then it is not recomputed. Otherwise the function is evaluated at the base 10438af5ae88SBarry Smith point during the first MatMult() after each call to MatMFFDSetBase(). 1044dff2f722SBarry Smith 1045e884886fSBarry Smith Level: advanced 1046e884886fSBarry Smith 1047e884886fSBarry Smith @*/ 10483ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase(Mat J,Vec U,Vec F) 1049e884886fSBarry Smith { 10503ec795f1SBarry Smith PetscErrorCode ierr,(*f)(Mat,Vec,Vec); 1051e884886fSBarry Smith 1052e884886fSBarry Smith PetscFunctionBegin; 10530700a824SBarry Smith PetscValidHeaderSpecific(J,MAT_CLASSID,1); 10540700a824SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 10550700a824SBarry Smith if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,3); 1056e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetBase_C",(void (**)(void))&f);CHKERRQ(ierr); 1057e884886fSBarry Smith if (f) { 10583ec795f1SBarry Smith ierr = (*f)(J,U,F);CHKERRQ(ierr); 1059e884886fSBarry Smith } 1060e884886fSBarry Smith PetscFunctionReturn(0); 1061e884886fSBarry Smith } 1062e884886fSBarry Smith 1063e884886fSBarry Smith #undef __FUNCT__ 1064e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh" 1065e884886fSBarry Smith /*@C 1066e884886fSBarry Smith MatMFFDSetCheckh - Sets a function that checks the computed h and adjusts 1067e884886fSBarry Smith it to satisfy some criteria 1068e884886fSBarry Smith 1069e884886fSBarry Smith Collective on Mat 1070e884886fSBarry Smith 1071e884886fSBarry Smith Input Parameters: 1072e884886fSBarry Smith + J - the MatMFFD matrix 1073e884886fSBarry Smith . fun - the function that checks h 1074e884886fSBarry Smith - ctx - any context needed by the function 1075e884886fSBarry Smith 1076e884886fSBarry Smith Options Database Keys: 1077e884886fSBarry Smith . -mat_mffd_check_positivity 1078e884886fSBarry Smith 1079e884886fSBarry Smith Level: advanced 1080e884886fSBarry Smith 1081e884886fSBarry Smith Notes: For example, MatMFFDSetCheckPositivity() insures that all entries 1082e884886fSBarry Smith of U + h*a are non-negative 1083e884886fSBarry Smith 1084e884886fSBarry Smith .seealso: MatMFFDSetCheckPositivity() 1085e884886fSBarry Smith @*/ 1086e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh(Mat J,PetscErrorCode (*fun)(void*,Vec,Vec,PetscScalar*),void* ctx) 1087e884886fSBarry Smith { 1088e884886fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec,Vec,PetscScalar*),void*); 1089e884886fSBarry Smith 1090e884886fSBarry Smith PetscFunctionBegin; 10910700a824SBarry Smith PetscValidHeaderSpecific(J,MAT_CLASSID,1); 1092e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr); 1093e884886fSBarry Smith if (f) { 1094e884886fSBarry Smith ierr = (*f)(J,fun,ctx);CHKERRQ(ierr); 1095e884886fSBarry Smith } 1096e884886fSBarry Smith PetscFunctionReturn(0); 1097e884886fSBarry Smith } 1098e884886fSBarry Smith 1099e884886fSBarry Smith #undef __FUNCT__ 1100e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckPositivity" 1101e884886fSBarry Smith /*@ 1102e884886fSBarry Smith MatMFFDCheckPositivity - Checks that all entries in U + h*a are positive or 1103e884886fSBarry Smith zero, decreases h until this is satisfied. 1104e884886fSBarry Smith 1105e884886fSBarry Smith Collective on Vec 1106e884886fSBarry Smith 1107e884886fSBarry Smith Input Parameters: 1108e884886fSBarry Smith + U - base vector that is added to 1109e884886fSBarry Smith . a - vector that is added 1110e884886fSBarry Smith . h - scaling factor on a 1111e884886fSBarry Smith - dummy - context variable (unused) 1112e884886fSBarry Smith 1113e884886fSBarry Smith Options Database Keys: 1114e884886fSBarry Smith . -mat_mffd_check_positivity 1115e884886fSBarry Smith 1116e884886fSBarry Smith Level: advanced 1117e884886fSBarry Smith 1118e884886fSBarry Smith Notes: This is rarely used directly, rather it is passed as an argument to 1119e884886fSBarry Smith MatMFFDSetCheckh() 1120e884886fSBarry Smith 1121e884886fSBarry Smith .seealso: MatMFFDSetCheckh() 1122e884886fSBarry Smith @*/ 1123e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDCheckPositivity(void* dummy,Vec U,Vec a,PetscScalar *h) 1124e884886fSBarry Smith { 1125e884886fSBarry Smith PetscReal val, minval; 1126e884886fSBarry Smith PetscScalar *u_vec, *a_vec; 1127e884886fSBarry Smith PetscErrorCode ierr; 1128e884886fSBarry Smith PetscInt i,n; 1129e884886fSBarry Smith MPI_Comm comm; 1130e884886fSBarry Smith 1131e884886fSBarry Smith PetscFunctionBegin; 1132e884886fSBarry Smith ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr); 1133e884886fSBarry Smith ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr); 1134e884886fSBarry Smith ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr); 1135e884886fSBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 1136e884886fSBarry Smith minval = PetscAbsScalar(*h*1.01); 1137e884886fSBarry Smith for(i=0;i<n;i++) { 1138e884886fSBarry Smith if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) { 1139e884886fSBarry Smith val = PetscAbsScalar(u_vec[i]/a_vec[i]); 1140e884886fSBarry Smith if (val < minval) minval = val; 1141e884886fSBarry Smith } 1142e884886fSBarry Smith } 1143e884886fSBarry Smith ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr); 1144e884886fSBarry Smith ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr); 1145e884886fSBarry Smith ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr); 1146e884886fSBarry Smith if (val <= PetscAbsScalar(*h)) { 1147e884886fSBarry Smith ierr = PetscInfo2(U,"Scaling back h from %G to %G\n",PetscRealPart(*h),.99*val);CHKERRQ(ierr); 1148e884886fSBarry Smith if (PetscRealPart(*h) > 0.0) *h = 0.99*val; 1149e884886fSBarry Smith else *h = -0.99*val; 1150e884886fSBarry Smith } 1151e884886fSBarry Smith PetscFunctionReturn(0); 1152e884886fSBarry Smith } 1153e884886fSBarry Smith 1154e884886fSBarry Smith 1155e884886fSBarry Smith 1156e884886fSBarry Smith 1157e884886fSBarry Smith 1158e884886fSBarry Smith 1159e884886fSBarry Smith 1160