181e6777dSBarry Smith 281e6777dSBarry Smith #ifndef lint 3*464493b3SBarry Smith static char vcid[] = "$Id: snesmfj.c,v 1.16 1995/08/14 19:39:37 bsmith Exp bsmith $"; 481e6777dSBarry Smith #endif 581e6777dSBarry Smith 6b1f0a012SBarry Smith #include "draw.h" /*I "draw.h" I*/ 7b1f0a012SBarry Smith #include "snes.h" /*I "snes.h" I*/ 881e6777dSBarry Smith 939e2f89bSBarry Smith typedef struct { 1039e2f89bSBarry Smith SNES snes; 1139e2f89bSBarry Smith Vec w; 1239e2f89bSBarry Smith } MFCtx_Private; 1339e2f89bSBarry Smith 14b9fa9cd0SBarry Smith int SNESMatrixFreeDestroy_Private(void *ptr) 15b9fa9cd0SBarry Smith { 16b9fa9cd0SBarry Smith int ierr; 17b9fa9cd0SBarry Smith MFCtx_Private *ctx = (MFCtx_Private* ) ptr; 18b9fa9cd0SBarry Smith ierr = VecDestroy(ctx->w); CHKERRQ(ierr); 19b9fa9cd0SBarry Smith PETSCFREE(ptr); 20b9fa9cd0SBarry Smith return 0; 21b9fa9cd0SBarry Smith } 2239e2f89bSBarry Smith /* 2339e2f89bSBarry Smith SNESMatrixFreeMult_Private - Default matrix free form of A*u. 2439e2f89bSBarry Smith 2539e2f89bSBarry Smith */ 2639e2f89bSBarry Smith int SNESMatrixFreeMult_Private(void *ptr,Vec dx,Vec y) 2739e2f89bSBarry Smith { 2839e2f89bSBarry Smith MFCtx_Private *ctx = (MFCtx_Private* ) ptr; 2939e2f89bSBarry Smith SNES snes = ctx->snes; 3039e2f89bSBarry Smith double norm,epsilon = 1.e-8; /* assumes double precision */ 3119a167f6SBarry Smith Scalar h,dot; 3219a167f6SBarry Smith double sum; 3339e2f89bSBarry Smith Scalar mone = -1.0; 3439e2f89bSBarry Smith Vec w = ctx->w,U,F; 3539e2f89bSBarry Smith int ierr; 3639e2f89bSBarry Smith 3778b31e54SBarry Smith ierr = SNESGetSolution(snes,&U); CHKERRQ(ierr); 3878b31e54SBarry Smith ierr = SNESGetFunction(snes,&F); CHKERRQ(ierr); 3939e2f89bSBarry Smith /* determine a "good" step size */ 4039e2f89bSBarry Smith VecDot(U,dx,&dot); VecASum(dx,&sum); VecNorm(dx,&norm); 41edd2f0e1SBarry Smith if (sum == 0.0) {dot = 1.0; norm = 1.0;} 4219a167f6SBarry Smith #if defined(PETSC_COMPLEX) 4319a167f6SBarry Smith else if (abs(dot) < 1.e-16*sum && real(dot) >= 0.0) dot = 1.e-16*sum; 4419a167f6SBarry Smith else if (abs(dot) < 0.0 && real(dot) > 1.e-16*sum) dot = -1.e-16*sum; 4519a167f6SBarry Smith #else 46edd2f0e1SBarry Smith else if (dot < 1.e-16*sum && dot >= 0.0) dot = 1.e-16*sum; 4739e2f89bSBarry Smith else if (dot < 0.0 && dot > 1.e-16*sum) dot = -1.e-16*sum; 4819a167f6SBarry Smith #endif 4939e2f89bSBarry Smith h = epsilon*dot/(norm*norm); 5039e2f89bSBarry Smith 5139e2f89bSBarry Smith /* evaluate function at F(x + dx) */ 5239e2f89bSBarry Smith VecWAXPY(&h,dx,U,w); 5378b31e54SBarry Smith ierr = SNESComputeFunction(snes,w,y); CHKERRQ(ierr); 5439e2f89bSBarry Smith VecAXPY(&mone,F,y); 5539e2f89bSBarry Smith h = -1.0/h; 5639e2f89bSBarry Smith VecScale(&h,y); 5739e2f89bSBarry Smith return 0; 5839e2f89bSBarry Smith } 5981e6777dSBarry Smith /*@ 605392566eSBarry Smith SNESDefaultMatrixFreeMatCreate - Creates a matrix-free matrix 6165afa06eSLois Curfman McInnes context for use with a SNES solver. You can use this matrix as the 6265afa06eSLois Curfman McInnes Jacobian argument for the routine SNESSetJacobian(). 635392566eSBarry Smith 645392566eSBarry Smith Input Parameters: 655392566eSBarry Smith . x - vector where SNES solution is to be stored. 665392566eSBarry Smith 675392566eSBarry Smith Output Parameters: 685392566eSBarry Smith . J - the matrix-free matrix 695392566eSBarry Smith 7065afa06eSLois Curfman McInnes Notes: 7165afa06eSLois Curfman McInnes The matrix-free matrix context merely contains the function pointers 7265afa06eSLois Curfman McInnes and work space for performing finite difference approximations of 7365afa06eSLois Curfman McInnes matrix operations such as matrix-vector products. 7465afa06eSLois Curfman McInnes 7565afa06eSLois Curfman McInnes The user should call MatDestroy() when finished with the matrix-free 7665afa06eSLois Curfman McInnes matrix context. 7765afa06eSLois Curfman McInnes 7865afa06eSLois Curfman McInnes .keywords: SNES, default, matrix-free, create, matrix 7965afa06eSLois Curfman McInnes 8065afa06eSLois Curfman McInnes .seealso: MatDestroy() 815392566eSBarry Smith @*/ 825392566eSBarry Smith int SNESDefaultMatrixFreeMatCreate(SNES snes,Vec x, Mat *J) 835392566eSBarry Smith { 845392566eSBarry Smith MPI_Comm comm; 855392566eSBarry Smith MFCtx_Private *mfctx; 865392566eSBarry Smith int n,ierr; 875392566eSBarry Smith 885392566eSBarry Smith mfctx = (MFCtx_Private *) PETSCMALLOC(sizeof(MFCtx_Private));CHKPTRQ(mfctx); 89*464493b3SBarry Smith PLogObjectMemory(snes,sizeof(MFCtx_Private)); 905392566eSBarry Smith mfctx->snes = snes; 915392566eSBarry Smith ierr = VecDuplicate(x,&mfctx->w); CHKERRQ(ierr); 925392566eSBarry Smith PetscObjectGetComm((PetscObject)x,&comm); 935392566eSBarry Smith VecGetSize(x,&n); 945392566eSBarry Smith ierr = MatShellCreate(comm,n,n,(void*)mfctx,J); CHKERRQ(ierr); 955392566eSBarry Smith MatShellSetMult(*J,SNESMatrixFreeMult_Private); 96b9fa9cd0SBarry Smith MatShellSetDestroy(*J,SNESMatrixFreeDestroy_Private); 97b9fa9cd0SBarry Smith PLogObjectParent(*J,mfctx->w); 98d370d78aSBarry Smith PLogObjectParent(snes,*J); 9981e6777dSBarry Smith return 0; 10081e6777dSBarry Smith } 10181e6777dSBarry Smith 102