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 @*/ 24*cfb32e20SJed Brown PetscErrorCode PETSCMAT_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 @*/ 48*cfb32e20SJed Brown PetscErrorCode PETSCMAT_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 1189a13eae7SJed Brown ierr = PetscTypeCompare((PetscObject)mat,MATMFFD,&match);CHKERRQ(ierr); 1199a13eae7SJed Brown if (!match) PetscFunctionReturn(0); 1209a13eae7SJed Brown 121e884886fSBarry Smith /* already set, so just return */ 122e884886fSBarry Smith ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr); 123e884886fSBarry Smith if (match) PetscFunctionReturn(0); 124e884886fSBarry Smith 125e884886fSBarry Smith /* destroy the old one if it exists */ 126e884886fSBarry Smith if (ctx->ops->destroy) { 127e884886fSBarry Smith ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr); 128e884886fSBarry Smith } 129e884886fSBarry Smith 130b022a5c1SBarry Smith ierr = PetscFListFind(MatMFFDList,((PetscObject)ctx)->comm,ftype,(void (**)(void)) &r);CHKERRQ(ierr); 131e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatMFFD type %s given",ftype); 132e884886fSBarry Smith ierr = (*r)(ctx);CHKERRQ(ierr); 133e884886fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr); 134e884886fSBarry Smith PetscFunctionReturn(0); 135e884886fSBarry Smith } 136e884886fSBarry Smith 137e884886fSBarry Smith typedef PetscErrorCode (*FCN1)(void*,Vec); /* force argument to next function to not be extern C*/ 138e884886fSBarry Smith EXTERN_C_BEGIN 139e884886fSBarry Smith #undef __FUNCT__ 140c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase_MFFD" 141c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase_MFFD(Mat mat,FCN1 func) 142e884886fSBarry Smith { 143e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 144e884886fSBarry Smith 145e884886fSBarry Smith PetscFunctionBegin; 146e884886fSBarry Smith ctx->funcisetbase = func; 147e884886fSBarry Smith PetscFunctionReturn(0); 148e884886fSBarry Smith } 149e884886fSBarry Smith EXTERN_C_END 150e884886fSBarry Smith 151e884886fSBarry Smith typedef PetscErrorCode (*FCN2)(void*,PetscInt,Vec,PetscScalar*); /* force argument to next function to not be extern C*/ 152e884886fSBarry Smith EXTERN_C_BEGIN 153e884886fSBarry Smith #undef __FUNCT__ 154c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni_MFFD" 155c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni_MFFD(Mat mat,FCN2 funci) 156e884886fSBarry Smith { 157e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 158e884886fSBarry Smith 159e884886fSBarry Smith PetscFunctionBegin; 160e884886fSBarry Smith ctx->funci = funci; 161e884886fSBarry Smith PetscFunctionReturn(0); 162e884886fSBarry Smith } 163e884886fSBarry Smith EXTERN_C_END 164e884886fSBarry Smith 165e884886fSBarry Smith 166e884886fSBarry Smith #undef __FUNCT__ 167e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegister" 168e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatMFFD)) 169e884886fSBarry Smith { 170e884886fSBarry Smith PetscErrorCode ierr; 171e884886fSBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 172e884886fSBarry Smith 173e884886fSBarry Smith PetscFunctionBegin; 174e884886fSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 175b022a5c1SBarry Smith ierr = PetscFListAdd(&MatMFFDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 176e884886fSBarry Smith PetscFunctionReturn(0); 177e884886fSBarry Smith } 178e884886fSBarry Smith 179e884886fSBarry Smith 180e884886fSBarry Smith #undef __FUNCT__ 181e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegisterDestroy" 182e884886fSBarry Smith /*@C 183e884886fSBarry Smith MatMFFDRegisterDestroy - Frees the list of MatMFFD methods that were 184e884886fSBarry Smith registered by MatMFFDRegisterDynamic). 185e884886fSBarry Smith 186e884886fSBarry Smith Not Collective 187e884886fSBarry Smith 188e884886fSBarry Smith Level: developer 189e884886fSBarry Smith 190e884886fSBarry Smith .keywords: MatMFFD, register, destroy 191e884886fSBarry Smith 192e884886fSBarry Smith .seealso: MatMFFDRegisterDynamic), MatMFFDRegisterAll() 193e884886fSBarry Smith @*/ 194e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegisterDestroy(void) 195e884886fSBarry Smith { 196e884886fSBarry Smith PetscErrorCode ierr; 197e884886fSBarry Smith 198e884886fSBarry Smith PetscFunctionBegin; 199b022a5c1SBarry Smith ierr = PetscFListDestroy(&MatMFFDList);CHKERRQ(ierr); 200e884886fSBarry Smith MatMFFDRegisterAllCalled = PETSC_FALSE; 201e884886fSBarry Smith PetscFunctionReturn(0); 202e884886fSBarry Smith } 203e884886fSBarry Smith 204e884886fSBarry Smith /* ----------------------------------------------------------------------------------------*/ 205e884886fSBarry Smith #undef __FUNCT__ 206e884886fSBarry Smith #define __FUNCT__ "MatDestroy_MFFD" 207e884886fSBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat) 208e884886fSBarry Smith { 209e884886fSBarry Smith PetscErrorCode ierr; 210e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 211e884886fSBarry Smith 212e884886fSBarry Smith PetscFunctionBegin; 213e884886fSBarry Smith if (ctx->w) { 214e884886fSBarry Smith ierr = VecDestroy(ctx->w);CHKERRQ(ierr); 215e884886fSBarry Smith } 216cfe22f5eSBarry Smith if (ctx->current_f_allocated) { 217cfe22f5eSBarry Smith ierr = VecDestroy(ctx->current_f); 218cfe22f5eSBarry Smith } 219e884886fSBarry Smith if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);} 220e884886fSBarry Smith if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);} 221e884886fSBarry Smith ierr = PetscHeaderDestroy(ctx);CHKERRQ(ierr); 222e884886fSBarry Smith 223e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetBase_C","",PETSC_NULL);CHKERRQ(ierr); 224e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C","",PETSC_NULL);CHKERRQ(ierr); 225e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioni_C","",PETSC_NULL);CHKERRQ(ierr); 226e884886fSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetCheckh_C","",PETSC_NULL);CHKERRQ(ierr); 227e884886fSBarry Smith 228e884886fSBarry Smith PetscFunctionReturn(0); 229e884886fSBarry Smith } 230e884886fSBarry Smith 231e884886fSBarry Smith #undef __FUNCT__ 232e884886fSBarry Smith #define __FUNCT__ "MatView_MFFD" 233e884886fSBarry Smith /* 234e884886fSBarry Smith MatMFFDView_MFFD - Views matrix-free parameters. 235e884886fSBarry Smith 236e884886fSBarry Smith */ 237e884886fSBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer) 238e884886fSBarry Smith { 239e884886fSBarry Smith PetscErrorCode ierr; 240e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 241e884886fSBarry Smith PetscTruth iascii; 242e884886fSBarry Smith 243e884886fSBarry Smith PetscFunctionBegin; 2442692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 245e884886fSBarry Smith if (iascii) { 246e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," matrix-free approximation:\n");CHKERRQ(ierr); 247e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," err=%G (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr); 2487adad957SLisandro Dalcin if (!((PetscObject)ctx)->type_name) { 249e884886fSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," The compute h routine has not yet been set\n");CHKERRQ(ierr); 250e884886fSBarry Smith } else { 2517adad957SLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," Using %s compute h routine\n",((PetscObject)ctx)->type_name);CHKERRQ(ierr); 252e884886fSBarry Smith } 253e884886fSBarry Smith if (ctx->ops->view) { 254e884886fSBarry Smith ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr); 255e884886fSBarry Smith } 256e884886fSBarry Smith } else { 257e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for matrix-free matrix",((PetscObject)viewer)->type_name); 258e884886fSBarry Smith } 259e884886fSBarry Smith PetscFunctionReturn(0); 260e884886fSBarry Smith } 261e884886fSBarry Smith 262e884886fSBarry Smith #undef __FUNCT__ 263e884886fSBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD" 264e884886fSBarry Smith /* 265e884886fSBarry Smith MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This 266e884886fSBarry Smith allows the user to indicate the beginning of a new linear solve by calling 267e884886fSBarry Smith MatAssemblyXXX() on the matrix free matrix. This then allows the 2681d0fab5eSBarry Smith MatCreateMFFD_WP() to properly compute ||U|| only the first time 269e884886fSBarry Smith in the linear solver rather than every time. 270e884886fSBarry Smith */ 271e884886fSBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt) 272e884886fSBarry Smith { 273e884886fSBarry Smith PetscErrorCode ierr; 274e884886fSBarry Smith MatMFFD j = (MatMFFD)J->data; 275e884886fSBarry Smith 276e884886fSBarry Smith PetscFunctionBegin; 277e884886fSBarry Smith ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr); 278e884886fSBarry Smith j->vshift = 0.0; 279e884886fSBarry Smith j->vscale = 1.0; 280e884886fSBarry Smith PetscFunctionReturn(0); 281e884886fSBarry Smith } 282e884886fSBarry Smith 283e884886fSBarry Smith #undef __FUNCT__ 284e884886fSBarry Smith #define __FUNCT__ "MatMult_MFFD" 285e884886fSBarry Smith /* 286e884886fSBarry Smith MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a: 287e884886fSBarry Smith 288e884886fSBarry Smith y ~= (F(u + ha) - F(u))/h, 289e884886fSBarry Smith where F = nonlinear function, as set by SNESSetFunction() 290e884886fSBarry Smith u = current iterate 291e884886fSBarry Smith h = difference interval 292e884886fSBarry Smith */ 293e884886fSBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y) 294e884886fSBarry Smith { 295e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 296e884886fSBarry Smith PetscScalar h; 297e884886fSBarry Smith Vec w,U,F; 298e884886fSBarry Smith PetscErrorCode ierr; 299e884886fSBarry Smith PetscTruth zeroa; 300e884886fSBarry Smith 301e884886fSBarry Smith PetscFunctionBegin; 302e884886fSBarry Smith /* We log matrix-free matrix-vector products separately, so that we can 303e884886fSBarry Smith separate the performance monitoring from the cases that use conventional 304e884886fSBarry Smith storage. We may eventually modify event logging to associate events 305e884886fSBarry Smith with particular objects, hence alleviating the more general problem. */ 306e884886fSBarry Smith ierr = PetscLogEventBegin(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr); 307e884886fSBarry Smith 308e884886fSBarry Smith w = ctx->w; 309e884886fSBarry Smith U = ctx->current_u; 3103ec795f1SBarry Smith F = ctx->current_f; 311e884886fSBarry Smith /* 312e884886fSBarry Smith Compute differencing parameter 313e884886fSBarry Smith */ 314e884886fSBarry Smith if (!ctx->ops->compute) { 315e884886fSBarry Smith ierr = MatMFFDSetType(mat,MATMFFD_WP);CHKERRQ(ierr); 316e884886fSBarry Smith ierr = MatMFFDSetFromOptions(mat);CHKERRQ(ierr); 317e884886fSBarry Smith } 318e884886fSBarry Smith ierr = (*ctx->ops->compute)(ctx,U,a,&h,&zeroa);CHKERRQ(ierr); 319e884886fSBarry Smith if (zeroa) { 320e884886fSBarry Smith ierr = VecSet(y,0.0);CHKERRQ(ierr); 321e884886fSBarry Smith PetscFunctionReturn(0); 322e884886fSBarry Smith } 323e884886fSBarry Smith 324e32f2f54SBarry Smith if (PetscIsInfOrNanScalar(h)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed Nan differencing parameter h"); 325e884886fSBarry Smith if (ctx->checkh) { 326e884886fSBarry Smith ierr = (*ctx->checkh)(ctx->checkhctx,U,a,&h);CHKERRQ(ierr); 327e884886fSBarry Smith } 328e884886fSBarry Smith 329e884886fSBarry Smith /* keep a record of the current differencing parameter h */ 330e884886fSBarry Smith ctx->currenth = h; 331e884886fSBarry Smith #if defined(PETSC_USE_COMPLEX) 332e884886fSBarry Smith ierr = PetscInfo2(mat,"Current differencing parameter: %G + %G i\n",PetscRealPart(h),PetscImaginaryPart(h));CHKERRQ(ierr); 333e884886fSBarry Smith #else 334e884886fSBarry Smith ierr = PetscInfo1(mat,"Current differencing parameter: %15.12e\n",h);CHKERRQ(ierr); 335e884886fSBarry Smith #endif 336e884886fSBarry Smith if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) { 337e884886fSBarry Smith ctx->historyh[ctx->ncurrenth] = h; 338e884886fSBarry Smith } 339e884886fSBarry Smith ctx->ncurrenth++; 340e884886fSBarry Smith 341e884886fSBarry Smith /* w = u + ha */ 342e884886fSBarry Smith ierr = VecWAXPY(w,h,a,U);CHKERRQ(ierr); 343e884886fSBarry Smith 344bcddec3dSBarry Smith /* compute func(U) as base for differencing; only needed first time in and not when provided by user */ 345bcddec3dSBarry Smith if (ctx->ncurrenth == 1 && ctx->current_f_allocated) { 346e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,U,F);CHKERRQ(ierr); 34752121784SBarry Smith } 348e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,w,y);CHKERRQ(ierr); 349e884886fSBarry Smith 350e884886fSBarry Smith ierr = VecAXPY(y,-1.0,F);CHKERRQ(ierr); 351e884886fSBarry Smith ierr = VecScale(y,1.0/h);CHKERRQ(ierr); 352e884886fSBarry Smith 353e884886fSBarry Smith ierr = VecAXPBY(y,ctx->vshift,ctx->vscale,a);CHKERRQ(ierr); 354e884886fSBarry Smith 355e884886fSBarry Smith if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);} 356e884886fSBarry Smith 357e884886fSBarry Smith ierr = PetscLogEventEnd(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr); 358e884886fSBarry Smith PetscFunctionReturn(0); 359e884886fSBarry Smith } 360e884886fSBarry Smith 361e884886fSBarry Smith #undef __FUNCT__ 362e884886fSBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD" 363e884886fSBarry Smith /* 364e884886fSBarry Smith MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix 365e884886fSBarry Smith 366e884886fSBarry Smith y ~= (F(u + ha) - F(u))/h, 367e884886fSBarry Smith where F = nonlinear function, as set by SNESSetFunction() 368e884886fSBarry Smith u = current iterate 369e884886fSBarry Smith h = difference interval 370e884886fSBarry Smith */ 371e884886fSBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a) 372e884886fSBarry Smith { 373e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 374e884886fSBarry Smith PetscScalar h,*aa,*ww,v; 375e884886fSBarry Smith PetscReal epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON; 376e884886fSBarry Smith Vec w,U; 377e884886fSBarry Smith PetscErrorCode ierr; 378e884886fSBarry Smith PetscInt i,rstart,rend; 379e884886fSBarry Smith 380e884886fSBarry Smith PetscFunctionBegin; 381e7e72b3dSBarry Smith if (!ctx->funci) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Requires calling MatMFFDSetFunctioni() first"); 382e884886fSBarry Smith 383e884886fSBarry Smith w = ctx->w; 384e884886fSBarry Smith U = ctx->current_u; 385e884886fSBarry Smith ierr = (*ctx->func)(ctx->funcctx,U,a);CHKERRQ(ierr); 386e884886fSBarry Smith ierr = (*ctx->funcisetbase)(ctx->funcctx,U);CHKERRQ(ierr); 387e884886fSBarry Smith ierr = VecCopy(U,w);CHKERRQ(ierr); 388e884886fSBarry Smith 389e884886fSBarry Smith ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr); 390e884886fSBarry Smith ierr = VecGetArray(a,&aa);CHKERRQ(ierr); 391e884886fSBarry Smith for (i=rstart; i<rend; i++) { 392e884886fSBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 393e884886fSBarry Smith h = ww[i-rstart]; 394e884886fSBarry Smith if (h == 0.0) h = 1.0; 395e884886fSBarry Smith #if !defined(PETSC_USE_COMPLEX) 396e884886fSBarry Smith if (h < umin && h >= 0.0) h = umin; 397e884886fSBarry Smith else if (h < 0.0 && h > -umin) h = -umin; 398e884886fSBarry Smith #else 399e884886fSBarry Smith if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin; 400e884886fSBarry Smith else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin; 401e884886fSBarry Smith #endif 402e884886fSBarry Smith h *= epsilon; 403e884886fSBarry Smith 404e884886fSBarry Smith ww[i-rstart] += h; 405e884886fSBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 406e884886fSBarry Smith ierr = (*ctx->funci)(ctx->funcctx,i,w,&v);CHKERRQ(ierr); 407e884886fSBarry Smith aa[i-rstart] = (v - aa[i-rstart])/h; 408e884886fSBarry Smith 409e884886fSBarry Smith /* possibly shift and scale result */ 410e884886fSBarry Smith aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart]; 411e884886fSBarry Smith 412e884886fSBarry Smith ierr = VecGetArray(w,&ww);CHKERRQ(ierr); 413e884886fSBarry Smith ww[i-rstart] -= h; 414e884886fSBarry Smith ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr); 415e884886fSBarry Smith } 416e884886fSBarry Smith ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr); 417e884886fSBarry Smith PetscFunctionReturn(0); 418e884886fSBarry Smith } 419e884886fSBarry Smith 420e884886fSBarry Smith #undef __FUNCT__ 421e884886fSBarry Smith #define __FUNCT__ "MatShift_MFFD" 422e884886fSBarry Smith PetscErrorCode MatShift_MFFD(Mat Y,PetscScalar a) 423e884886fSBarry Smith { 424e884886fSBarry Smith MatMFFD shell = (MatMFFD)Y->data; 425e884886fSBarry Smith PetscFunctionBegin; 426e884886fSBarry Smith shell->vshift += a; 427e884886fSBarry Smith PetscFunctionReturn(0); 428e884886fSBarry Smith } 429e884886fSBarry Smith 430e884886fSBarry Smith #undef __FUNCT__ 431e884886fSBarry Smith #define __FUNCT__ "MatScale_MFFD" 432e884886fSBarry Smith PetscErrorCode MatScale_MFFD(Mat Y,PetscScalar a) 433e884886fSBarry Smith { 434e884886fSBarry Smith MatMFFD shell = (MatMFFD)Y->data; 435e884886fSBarry Smith PetscFunctionBegin; 436e884886fSBarry Smith shell->vscale *= a; 437e884886fSBarry Smith PetscFunctionReturn(0); 438e884886fSBarry Smith } 439e884886fSBarry Smith 440e884886fSBarry Smith EXTERN_C_BEGIN 441e884886fSBarry Smith #undef __FUNCT__ 442c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetBase_MFFD" 443c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_MFFD(Mat J,Vec U,Vec F) 444e884886fSBarry Smith { 445e884886fSBarry Smith PetscErrorCode ierr; 446e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 447e884886fSBarry Smith 448e884886fSBarry Smith PetscFunctionBegin; 449e884886fSBarry Smith ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr); 450e884886fSBarry Smith ctx->current_u = U; 45152121784SBarry Smith if (F) { 452cfe22f5eSBarry Smith if (ctx->current_f_allocated) {ierr = VecDestroy(ctx->current_f);CHKERRQ(ierr);} 4533ec795f1SBarry Smith ctx->current_f = F; 454cfe22f5eSBarry Smith ctx->current_f_allocated = PETSC_FALSE; 455cfe22f5eSBarry Smith } else if (!ctx->current_f_allocated) { 45652121784SBarry Smith ierr = VecDuplicate(ctx->current_u, &ctx->current_f);CHKERRQ(ierr); 457cfe22f5eSBarry Smith ctx->current_f_allocated = PETSC_TRUE; 45852121784SBarry Smith } 459e884886fSBarry Smith if (!ctx->w) { 460e884886fSBarry Smith ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr); 461e884886fSBarry Smith } 462e884886fSBarry Smith J->assembled = PETSC_TRUE; 463e884886fSBarry Smith PetscFunctionReturn(0); 464e884886fSBarry Smith } 465e884886fSBarry Smith EXTERN_C_END 466e884886fSBarry Smith typedef PetscErrorCode (*FCN3)(void*,Vec,Vec,PetscScalar*); /* force argument to next function to not be extern C*/ 467e884886fSBarry Smith EXTERN_C_BEGIN 468e884886fSBarry Smith #undef __FUNCT__ 469c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh_MFFD" 470c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh_MFFD(Mat J,FCN3 fun,void*ectx) 471e884886fSBarry Smith { 472e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 473e884886fSBarry Smith 474e884886fSBarry Smith PetscFunctionBegin; 475e884886fSBarry Smith ctx->checkh = fun; 476e884886fSBarry Smith ctx->checkhctx = ectx; 477e884886fSBarry Smith PetscFunctionReturn(0); 478e884886fSBarry Smith } 479e884886fSBarry Smith EXTERN_C_END 480e884886fSBarry Smith 481e884886fSBarry Smith #undef __FUNCT__ 4826aa9148fSLisandro Dalcin #define __FUNCT__ "MatMFFDSetOptionsPrefix" 4836aa9148fSLisandro Dalcin /*@C 4846aa9148fSLisandro Dalcin MatMFFDSetOptionsPrefix - Sets the prefix used for searching for all 4856aa9148fSLisandro Dalcin MatMFFD options in the database. 4866aa9148fSLisandro Dalcin 4876aa9148fSLisandro Dalcin Collective on Mat 4886aa9148fSLisandro Dalcin 4896aa9148fSLisandro Dalcin Input Parameter: 4906aa9148fSLisandro Dalcin + A - the Mat context 4916aa9148fSLisandro Dalcin - prefix - the prefix to prepend to all option names 4926aa9148fSLisandro Dalcin 4936aa9148fSLisandro Dalcin Notes: 4946aa9148fSLisandro Dalcin A hyphen (-) must NOT be given at the beginning of the prefix name. 4956aa9148fSLisandro Dalcin The first character of all runtime options is AUTOMATICALLY the hyphen. 4966aa9148fSLisandro Dalcin 4976aa9148fSLisandro Dalcin Level: advanced 4986aa9148fSLisandro Dalcin 4996aa9148fSLisandro Dalcin .keywords: SNES, matrix-free, parameters 5006aa9148fSLisandro Dalcin 5016aa9148fSLisandro Dalcin .seealso: MatMFFDSetFromOptions(), MatCreateSNESMF() 5026aa9148fSLisandro Dalcin @*/ 5036aa9148fSLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetOptionsPrefix(Mat mat,const char prefix[]) 5046aa9148fSLisandro Dalcin 5056aa9148fSLisandro Dalcin { 5066aa9148fSLisandro Dalcin MatMFFD mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL; 5076aa9148fSLisandro Dalcin PetscErrorCode ierr; 5086aa9148fSLisandro Dalcin PetscFunctionBegin; 5090700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5100700a824SBarry Smith PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1); 5116aa9148fSLisandro Dalcin ierr = PetscObjectSetOptionsPrefix((PetscObject)mfctx,prefix);CHKERRQ(ierr); 5126aa9148fSLisandro Dalcin PetscFunctionReturn(0); 5136aa9148fSLisandro Dalcin } 5146aa9148fSLisandro Dalcin 5156aa9148fSLisandro Dalcin #undef __FUNCT__ 516e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFromOptions" 517e884886fSBarry Smith /*@ 518e884886fSBarry Smith MatMFFDSetFromOptions - Sets the MatMFFD options from the command line 519e884886fSBarry Smith parameter. 520e884886fSBarry Smith 521e884886fSBarry Smith Collective on Mat 522e884886fSBarry Smith 523e884886fSBarry Smith Input Parameters: 524e884886fSBarry Smith . mat - the matrix obtained with MatCreateMFFD() or MatCreateSNESMF() 525e884886fSBarry Smith 526e884886fSBarry Smith Options Database Keys: 527e884886fSBarry Smith + -mat_mffd_type - wp or ds (see MATMFFD_WP or MATMFFD_DS) 528e884886fSBarry Smith - -mat_mffd_err - square root of estimated relative error in function evaluation 529e884886fSBarry Smith - -mat_mffd_period - how often h is recomputed, defaults to 1, everytime 530e884886fSBarry Smith 531e884886fSBarry Smith Level: advanced 532e884886fSBarry Smith 533e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 534e884886fSBarry Smith 5351d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatMFFDResetHHistory() 536e884886fSBarry Smith @*/ 537e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFromOptions(Mat mat) 538e884886fSBarry Smith { 53971f08665SBarry Smith MatMFFD mfctx = (MatMFFD)mat->data; 540e884886fSBarry Smith PetscErrorCode ierr; 541e884886fSBarry Smith PetscTruth flg; 542e884886fSBarry Smith char ftype[256]; 543e884886fSBarry Smith 544e884886fSBarry Smith PetscFunctionBegin; 5450700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 5460700a824SBarry Smith PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1); 5477adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)mfctx)->comm,((PetscObject)mfctx)->prefix,"Set matrix free computation parameters","MatMFFD");CHKERRQ(ierr); 548b022a5c1SBarry Smith ierr = PetscOptionsList("-mat_mffd_type","Matrix free type","MatMFFDSetType",MatMFFDList,((PetscObject)mfctx)->type_name,ftype,256,&flg);CHKERRQ(ierr); 549e884886fSBarry Smith if (flg) { 550e884886fSBarry Smith ierr = MatMFFDSetType(mat,ftype);CHKERRQ(ierr); 551e884886fSBarry Smith } 552e884886fSBarry Smith 553e884886fSBarry Smith ierr = PetscOptionsReal("-mat_mffd_err","set sqrt relative error in function","MatMFFDSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr); 554e884886fSBarry Smith ierr = PetscOptionsInt("-mat_mffd_period","how often h is recomputed","MatMFFDSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr); 555e884886fSBarry Smith 55690d69ab7SBarry Smith flg = PETSC_FALSE; 55790d69ab7SBarry Smith ierr = PetscOptionsTruth("-mat_mffd_check_positivity","Insure that U + h*a is nonnegative","MatMFFDSetCheckh",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 558e884886fSBarry Smith if (flg) { 559e884886fSBarry Smith ierr = MatMFFDSetCheckh(mat,MatMFFDCheckPositivity,0);CHKERRQ(ierr); 560e884886fSBarry Smith } 561e884886fSBarry Smith if (mfctx->ops->setfromoptions) { 562e884886fSBarry Smith ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr); 563e884886fSBarry Smith } 564e884886fSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 565e884886fSBarry Smith PetscFunctionReturn(0); 566e884886fSBarry Smith } 567e884886fSBarry Smith 568e884886fSBarry Smith /*MC 569e884886fSBarry Smith MATMFFD - MATMFFD = "mffd" - A matrix free matrix type. 570e884886fSBarry Smith 571e884886fSBarry Smith Level: advanced 572e884886fSBarry Smith 573d2d6cebeSBarry Smith .seealso: MatCreateMFFD(), MatCreateSNESMF(), MatMFFDSetFunction() 574e884886fSBarry Smith M*/ 575e884886fSBarry Smith EXTERN_C_BEGIN 576e884886fSBarry Smith #undef __FUNCT__ 577e884886fSBarry Smith #define __FUNCT__ "MatCreate_MFFD" 578e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MFFD(Mat A) 579e884886fSBarry Smith { 580e884886fSBarry Smith MatMFFD mfctx; 581e884886fSBarry Smith PetscErrorCode ierr; 582e884886fSBarry Smith 583e884886fSBarry Smith PetscFunctionBegin; 5843ec795f1SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES 5853ec795f1SBarry Smith ierr = MatMFFDInitializePackage(PETSC_NULL);CHKERRQ(ierr); 5863ec795f1SBarry Smith #endif 587e884886fSBarry Smith 5880700a824SBarry Smith ierr = PetscHeaderCreate(mfctx,_p_MatMFFD,struct _MFOps,MATMFFD_CLASSID,0,"MatMFFD",((PetscObject)A)->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr); 589e884886fSBarry Smith mfctx->sp = 0; 590e884886fSBarry Smith mfctx->error_rel = PETSC_SQRT_MACHINE_EPSILON; 591e884886fSBarry Smith mfctx->recomputeperiod = 1; 592e884886fSBarry Smith mfctx->count = 0; 593e884886fSBarry Smith mfctx->currenth = 0.0; 594e884886fSBarry Smith mfctx->historyh = PETSC_NULL; 595e884886fSBarry Smith mfctx->ncurrenth = 0; 596e884886fSBarry Smith mfctx->maxcurrenth = 0; 5977adad957SLisandro Dalcin ((PetscObject)mfctx)->type_name = 0; 598e884886fSBarry Smith 599e884886fSBarry Smith mfctx->vshift = 0.0; 600e884886fSBarry Smith mfctx->vscale = 1.0; 601e884886fSBarry Smith 602e884886fSBarry Smith /* 603e884886fSBarry Smith Create the empty data structure to contain compute-h routines. 604e884886fSBarry Smith These will be filled in below from the command line options or 605e884886fSBarry Smith a later call with MatMFFDSetType() or if that is not called 606e884886fSBarry Smith then it will default in the first use of MatMult_MFFD() 607e884886fSBarry Smith */ 608e884886fSBarry Smith mfctx->ops->compute = 0; 609e884886fSBarry Smith mfctx->ops->destroy = 0; 610e884886fSBarry Smith mfctx->ops->view = 0; 611e884886fSBarry Smith mfctx->ops->setfromoptions = 0; 612e884886fSBarry Smith mfctx->hctx = 0; 613e884886fSBarry Smith 614e884886fSBarry Smith mfctx->func = 0; 615e884886fSBarry Smith mfctx->funcctx = 0; 616e884886fSBarry Smith mfctx->w = PETSC_NULL; 617e884886fSBarry Smith 618e884886fSBarry Smith A->data = mfctx; 619e884886fSBarry Smith 620e884886fSBarry Smith A->ops->mult = MatMult_MFFD; 621e884886fSBarry Smith A->ops->destroy = MatDestroy_MFFD; 622e884886fSBarry Smith A->ops->view = MatView_MFFD; 623e884886fSBarry Smith A->ops->assemblyend = MatAssemblyEnd_MFFD; 624e884886fSBarry Smith A->ops->getdiagonal = MatGetDiagonal_MFFD; 625e884886fSBarry Smith A->ops->scale = MatScale_MFFD; 626e884886fSBarry Smith A->ops->shift = MatShift_MFFD; 627e884886fSBarry Smith A->ops->setfromoptions = MatMFFDSetFromOptions; 628e884886fSBarry Smith A->assembled = PETSC_TRUE; 629e884886fSBarry Smith 63026283091SBarry Smith ierr = PetscLayoutSetBlockSize(A->rmap,1);CHKERRQ(ierr); 63126283091SBarry Smith ierr = PetscLayoutSetBlockSize(A->cmap,1);CHKERRQ(ierr); 63226283091SBarry Smith ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 63326283091SBarry Smith ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 634ee1cef2cSJed Brown 635c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetBase_C","MatMFFDSetBase_MFFD",MatMFFDSetBase_MFFD);CHKERRQ(ierr); 636c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioniBase_C","MatMFFDSetFunctioniBase_MFFD",MatMFFDSetFunctioniBase_MFFD);CHKERRQ(ierr); 637c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioni_C","MatMFFDSetFunctioni_MFFD",MatMFFDSetFunctioni_MFFD);CHKERRQ(ierr); 638c879e56bSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetCheckh_C","MatMFFDSetCheckh_MFFD",MatMFFDSetCheckh_MFFD);CHKERRQ(ierr); 639e884886fSBarry Smith mfctx->mat = A; 640e884886fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)A,MATMFFD);CHKERRQ(ierr); 641e884886fSBarry Smith PetscFunctionReturn(0); 642e884886fSBarry Smith } 643e884886fSBarry Smith EXTERN_C_END 644e884886fSBarry Smith 645e884886fSBarry Smith #undef __FUNCT__ 646e884886fSBarry Smith #define __FUNCT__ "MatCreateMFFD" 647e884886fSBarry Smith /*@ 648e884886fSBarry Smith MatCreateMFFD - Creates a matrix-free matrix. See also MatCreateSNESMF() 649e884886fSBarry Smith 650e884886fSBarry Smith Collective on Vec 651e884886fSBarry Smith 652e884886fSBarry Smith Input Parameters: 653fef1beadSBarry Smith + comm - MPI communicator 654fef1beadSBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 655fef1beadSBarry Smith This value should be the same as the local size used in creating the 656fef1beadSBarry Smith y vector for the matrix-vector product y = Ax. 657fef1beadSBarry Smith . n - This value should be the same as the local size used in creating the 658fef1beadSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 659fef1beadSBarry Smith calculated if N is given) For square matrices n is almost always m. 660fef1beadSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 661fef1beadSBarry Smith - N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 662fef1beadSBarry Smith 663e884886fSBarry Smith 664e884886fSBarry Smith Output Parameter: 665e884886fSBarry Smith . J - the matrix-free matrix 666e884886fSBarry Smith 667e884886fSBarry Smith Level: advanced 668e884886fSBarry Smith 669e884886fSBarry Smith Notes: 670e884886fSBarry Smith The matrix-free matrix context merely contains the function pointers 671e884886fSBarry Smith and work space for performing finite difference approximations of 672e884886fSBarry Smith Jacobian-vector products, F'(u)*a, 673e884886fSBarry Smith 674e884886fSBarry Smith The default code uses the following approach to compute h 675e884886fSBarry Smith 676e884886fSBarry Smith .vb 677e884886fSBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 678e884886fSBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 679e884886fSBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise 680e884886fSBarry Smith where 681e884886fSBarry Smith error_rel = square root of relative error in function evaluation 682e884886fSBarry Smith umin = minimum iterate parameter 683e884886fSBarry Smith .ve 684e884886fSBarry Smith 685e884886fSBarry Smith The user can set the error_rel via MatMFFDSetFunctionError() and 6860598bfebSBarry Smith umin via MatMFFDDSSetUmin(); see the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A> 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 7503f9fe445SBarry Smith Logically 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 7853f9fe445SBarry Smith Logically 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 8223f9fe445SBarry Smith Logically 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 8593f9fe445SBarry Smith Logically 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; 881c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(mat,period,2); 882e884886fSBarry Smith ctx->recomputeperiod = period; 883e884886fSBarry Smith PetscFunctionReturn(0); 884e884886fSBarry Smith } 885e884886fSBarry Smith 886e884886fSBarry Smith #undef __FUNCT__ 887e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctionError" 888e884886fSBarry Smith /*@ 889e884886fSBarry Smith MatMFFDSetFunctionError - Sets the error_rel for the approximation of 890e884886fSBarry Smith matrix-vector products using finite differences. 891e884886fSBarry Smith 8923f9fe445SBarry Smith Logically Collective on Mat 893e884886fSBarry Smith 894e884886fSBarry Smith Input Parameters: 895e884886fSBarry Smith + mat - the matrix free matrix created via MatCreateMFFD() or MatCreateSNESMF() 896e884886fSBarry Smith - error_rel - relative error (should be set to the square root of 897e884886fSBarry Smith the relative error in the function evaluations) 898e884886fSBarry Smith 899e884886fSBarry Smith Options Database Keys: 900e884886fSBarry Smith + -mat_mffd_err <error_rel> - Sets error_rel 901e884886fSBarry Smith 902e884886fSBarry Smith Level: advanced 903e884886fSBarry Smith 904e884886fSBarry Smith Notes: 905e884886fSBarry Smith The default matrix-free matrix-vector product routine computes 906e884886fSBarry Smith .vb 907e884886fSBarry Smith F'(u)*a = [F(u+h*a) - F(u)]/h where 908e884886fSBarry Smith h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1} 909e884886fSBarry Smith = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else 910e884886fSBarry Smith .ve 911e884886fSBarry Smith 912e884886fSBarry Smith .keywords: SNES, matrix-free, parameters 913e884886fSBarry Smith 914e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD 9151d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory() 916e884886fSBarry Smith @*/ 917e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctionError(Mat mat,PetscReal error) 918e884886fSBarry Smith { 919e884886fSBarry Smith MatMFFD ctx = (MatMFFD)mat->data; 920e884886fSBarry Smith 921e884886fSBarry Smith PetscFunctionBegin; 922c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(mat,error,2); 923e884886fSBarry Smith if (error != PETSC_DEFAULT) ctx->error_rel = error; 924e884886fSBarry Smith PetscFunctionReturn(0); 925e884886fSBarry Smith } 926e884886fSBarry Smith 927e884886fSBarry Smith #undef __FUNCT__ 928e884886fSBarry Smith #define __FUNCT__ "MatMFFDAddNullSpace" 929e884886fSBarry Smith /*@ 930e884886fSBarry Smith MatMFFDAddNullSpace - Provides a null space that an operator is 931e884886fSBarry Smith supposed to have. Since roundoff will create a small component in 932e884886fSBarry Smith the null space, if you know the null space you may have it 933e884886fSBarry Smith automatically removed. 934e884886fSBarry Smith 9353f9fe445SBarry Smith Logically Collective on Mat 936e884886fSBarry Smith 937e884886fSBarry Smith Input Parameters: 938e884886fSBarry Smith + J - the matrix-free matrix context 939e884886fSBarry Smith - nullsp - object created with MatNullSpaceCreate() 940e884886fSBarry Smith 941e884886fSBarry Smith Level: advanced 942e884886fSBarry Smith 943e884886fSBarry Smith .keywords: SNES, matrix-free, null space 944e884886fSBarry Smith 945e884886fSBarry Smith .seealso: MatNullSpaceCreate(), MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD 9461d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory() 947e884886fSBarry Smith @*/ 948e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDAddNullSpace(Mat J,MatNullSpace nullsp) 949e884886fSBarry Smith { 950e884886fSBarry Smith PetscErrorCode ierr; 951e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 952e884886fSBarry Smith 953e884886fSBarry Smith PetscFunctionBegin; 954e884886fSBarry Smith ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 955e884886fSBarry Smith if (ctx->sp) { ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr); } 956e884886fSBarry Smith ctx->sp = nullsp; 957e884886fSBarry Smith PetscFunctionReturn(0); 958e884886fSBarry Smith } 959e884886fSBarry Smith 960e884886fSBarry Smith #undef __FUNCT__ 961e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetHHistory" 962e884886fSBarry Smith /*@ 963e884886fSBarry Smith MatMFFDSetHHistory - Sets an array to collect a history of the 964e884886fSBarry Smith differencing values (h) computed for the matrix-free product. 965e884886fSBarry Smith 9663f9fe445SBarry Smith Logically Collective on Mat 967e884886fSBarry Smith 968e884886fSBarry Smith Input Parameters: 969e884886fSBarry Smith + J - the matrix-free matrix context 970e884886fSBarry Smith . histroy - space to hold the history 971e884886fSBarry Smith - nhistory - number of entries in history, if more entries are generated than 972e884886fSBarry Smith nhistory, then the later ones are discarded 973e884886fSBarry Smith 974e884886fSBarry Smith Level: advanced 975e884886fSBarry Smith 976e884886fSBarry Smith Notes: 977e884886fSBarry Smith Use MatMFFDResetHHistory() to reset the history counter and collect 978e884886fSBarry Smith a new batch of differencing parameters, h. 979e884886fSBarry Smith 980e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history 981e884886fSBarry Smith 982e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), 9831d0fab5eSBarry Smith MatMFFDResetHHistory(), MatMFFDSetFunctionError() 984e884886fSBarry Smith 985e884886fSBarry Smith @*/ 986e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory) 987e884886fSBarry Smith { 988e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 989e884886fSBarry Smith 990e884886fSBarry Smith PetscFunctionBegin; 991e884886fSBarry Smith ctx->historyh = history; 992e884886fSBarry Smith ctx->maxcurrenth = nhistory; 99375567043SBarry Smith ctx->currenth = 0.; 994e884886fSBarry Smith PetscFunctionReturn(0); 995e884886fSBarry Smith } 996e884886fSBarry Smith 997e884886fSBarry Smith #undef __FUNCT__ 998e884886fSBarry Smith #define __FUNCT__ "MatMFFDResetHHistory" 999e884886fSBarry Smith /*@ 1000e884886fSBarry Smith MatMFFDResetHHistory - Resets the counter to zero to begin 1001e884886fSBarry Smith collecting a new set of differencing histories. 1002e884886fSBarry Smith 10033f9fe445SBarry Smith Logically Collective on Mat 1004e884886fSBarry Smith 1005e884886fSBarry Smith Input Parameters: 1006e884886fSBarry Smith . J - the matrix-free matrix context 1007e884886fSBarry Smith 1008e884886fSBarry Smith Level: advanced 1009e884886fSBarry Smith 1010e884886fSBarry Smith Notes: 1011e884886fSBarry Smith Use MatMFFDSetHHistory() to create the original history counter. 1012e884886fSBarry Smith 1013e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history 1014e884886fSBarry Smith 1015e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), 10161d0fab5eSBarry Smith MatMFFDSetHHistory(), MatMFFDSetFunctionError() 1017e884886fSBarry Smith 1018e884886fSBarry Smith @*/ 1019e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDResetHHistory(Mat J) 1020e884886fSBarry Smith { 1021e884886fSBarry Smith MatMFFD ctx = (MatMFFD)J->data; 1022e884886fSBarry Smith 1023e884886fSBarry Smith PetscFunctionBegin; 1024e884886fSBarry Smith ctx->ncurrenth = 0; 1025e884886fSBarry Smith PetscFunctionReturn(0); 1026e884886fSBarry Smith } 1027e884886fSBarry Smith 1028e884886fSBarry Smith 1029e884886fSBarry Smith #undef __FUNCT__ 1030e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetBase" 1031e884886fSBarry Smith /*@ 1032e884886fSBarry Smith MatMFFDSetBase - Sets the vector U at which matrix vector products of the 1033e884886fSBarry Smith Jacobian are computed 1034e884886fSBarry Smith 10353f9fe445SBarry Smith Logically Collective on Mat 1036e884886fSBarry Smith 1037e884886fSBarry Smith Input Parameters: 1038e884886fSBarry Smith + J - the MatMFFD matrix 10393ec795f1SBarry Smith . U - the vector 1040bcddec3dSBarry Smith - F - (optional) vector that contains F(u) if it has been already computed 1041e884886fSBarry Smith 1042e884886fSBarry Smith Notes: This is rarely used directly 1043e884886fSBarry Smith 10448af5ae88SBarry Smith If F is provided then it is not recomputed. Otherwise the function is evaluated at the base 10458af5ae88SBarry Smith point during the first MatMult() after each call to MatMFFDSetBase(). 1046dff2f722SBarry Smith 1047e884886fSBarry Smith Level: advanced 1048e884886fSBarry Smith 1049e884886fSBarry Smith @*/ 10503ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase(Mat J,Vec U,Vec F) 1051e884886fSBarry Smith { 10523ec795f1SBarry Smith PetscErrorCode ierr,(*f)(Mat,Vec,Vec); 1053e884886fSBarry Smith 1054e884886fSBarry Smith PetscFunctionBegin; 10550700a824SBarry Smith PetscValidHeaderSpecific(J,MAT_CLASSID,1); 10560700a824SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 10570700a824SBarry Smith if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,3); 1058e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetBase_C",(void (**)(void))&f);CHKERRQ(ierr); 1059e884886fSBarry Smith if (f) { 10603ec795f1SBarry Smith ierr = (*f)(J,U,F);CHKERRQ(ierr); 1061e884886fSBarry Smith } 1062e884886fSBarry Smith PetscFunctionReturn(0); 1063e884886fSBarry Smith } 1064e884886fSBarry Smith 1065e884886fSBarry Smith #undef __FUNCT__ 1066e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh" 1067e884886fSBarry Smith /*@C 1068e884886fSBarry Smith MatMFFDSetCheckh - Sets a function that checks the computed h and adjusts 1069e884886fSBarry Smith it to satisfy some criteria 1070e884886fSBarry Smith 10713f9fe445SBarry Smith Logically Collective on Mat 1072e884886fSBarry Smith 1073e884886fSBarry Smith Input Parameters: 1074e884886fSBarry Smith + J - the MatMFFD matrix 1075e884886fSBarry Smith . fun - the function that checks h 1076e884886fSBarry Smith - ctx - any context needed by the function 1077e884886fSBarry Smith 1078e884886fSBarry Smith Options Database Keys: 1079e884886fSBarry Smith . -mat_mffd_check_positivity 1080e884886fSBarry Smith 1081e884886fSBarry Smith Level: advanced 1082e884886fSBarry Smith 1083e884886fSBarry Smith Notes: For example, MatMFFDSetCheckPositivity() insures that all entries 1084e884886fSBarry Smith of U + h*a are non-negative 1085e884886fSBarry Smith 1086e884886fSBarry Smith .seealso: MatMFFDSetCheckPositivity() 1087e884886fSBarry Smith @*/ 1088e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh(Mat J,PetscErrorCode (*fun)(void*,Vec,Vec,PetscScalar*),void* ctx) 1089e884886fSBarry Smith { 1090e884886fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec,Vec,PetscScalar*),void*); 1091e884886fSBarry Smith 1092e884886fSBarry Smith PetscFunctionBegin; 10930700a824SBarry Smith PetscValidHeaderSpecific(J,MAT_CLASSID,1); 1094e884886fSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr); 1095e884886fSBarry Smith if (f) { 1096e884886fSBarry Smith ierr = (*f)(J,fun,ctx);CHKERRQ(ierr); 1097e884886fSBarry Smith } 1098e884886fSBarry Smith PetscFunctionReturn(0); 1099e884886fSBarry Smith } 1100e884886fSBarry Smith 1101e884886fSBarry Smith #undef __FUNCT__ 1102e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckPositivity" 1103e884886fSBarry Smith /*@ 1104e884886fSBarry Smith MatMFFDCheckPositivity - Checks that all entries in U + h*a are positive or 1105e884886fSBarry Smith zero, decreases h until this is satisfied. 1106e884886fSBarry Smith 11073f9fe445SBarry Smith Logically Collective on Vec 1108e884886fSBarry Smith 1109e884886fSBarry Smith Input Parameters: 1110e884886fSBarry Smith + U - base vector that is added to 1111e884886fSBarry Smith . a - vector that is added 1112e884886fSBarry Smith . h - scaling factor on a 1113e884886fSBarry Smith - dummy - context variable (unused) 1114e884886fSBarry Smith 1115e884886fSBarry Smith Options Database Keys: 1116e884886fSBarry Smith . -mat_mffd_check_positivity 1117e884886fSBarry Smith 1118e884886fSBarry Smith Level: advanced 1119e884886fSBarry Smith 1120e884886fSBarry Smith Notes: This is rarely used directly, rather it is passed as an argument to 1121e884886fSBarry Smith MatMFFDSetCheckh() 1122e884886fSBarry Smith 1123e884886fSBarry Smith .seealso: MatMFFDSetCheckh() 1124e884886fSBarry Smith @*/ 1125e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDCheckPositivity(void* dummy,Vec U,Vec a,PetscScalar *h) 1126e884886fSBarry Smith { 1127e884886fSBarry Smith PetscReal val, minval; 1128e884886fSBarry Smith PetscScalar *u_vec, *a_vec; 1129e884886fSBarry Smith PetscErrorCode ierr; 1130e884886fSBarry Smith PetscInt i,n; 1131e884886fSBarry Smith MPI_Comm comm; 1132e884886fSBarry Smith 1133e884886fSBarry Smith PetscFunctionBegin; 1134e884886fSBarry Smith ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr); 1135e884886fSBarry Smith ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr); 1136e884886fSBarry Smith ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr); 1137e884886fSBarry Smith ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 1138e884886fSBarry Smith minval = PetscAbsScalar(*h*1.01); 1139e884886fSBarry Smith for(i=0;i<n;i++) { 1140e884886fSBarry Smith if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) { 1141e884886fSBarry Smith val = PetscAbsScalar(u_vec[i]/a_vec[i]); 1142e884886fSBarry Smith if (val < minval) minval = val; 1143e884886fSBarry Smith } 1144e884886fSBarry Smith } 1145e884886fSBarry Smith ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr); 1146e884886fSBarry Smith ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr); 1147334eac09SBarry Smith ierr = MPI_Allreduce(&minval,&val,1,MPIU_REAL,MPI_MIN,comm);CHKERRQ(ierr); 1148e884886fSBarry Smith if (val <= PetscAbsScalar(*h)) { 1149e884886fSBarry Smith ierr = PetscInfo2(U,"Scaling back h from %G to %G\n",PetscRealPart(*h),.99*val);CHKERRQ(ierr); 1150e884886fSBarry Smith if (PetscRealPart(*h) > 0.0) *h = 0.99*val; 1151e884886fSBarry Smith else *h = -0.99*val; 1152e884886fSBarry Smith } 1153e884886fSBarry Smith PetscFunctionReturn(0); 1154e884886fSBarry Smith } 1155e884886fSBarry Smith 1156e884886fSBarry Smith 1157e884886fSBarry Smith 1158e884886fSBarry Smith 1159e884886fSBarry Smith 1160e884886fSBarry Smith 1161e884886fSBarry Smith 1162