163dd3a1aSKris Buschelman #define PETSCSNES_DLL 281e6777dSBarry Smith 30decc0a3SBarry Smith #include "include/private/snesimpl.h" /*I "petscsnes.h" I*/ 43ec795f1SBarry Smith #include "include/private/matimpl.h" 53ec795f1SBarry Smith #include "src/mat/impls/mffd/mffdimpl.h" 681e6777dSBarry Smith 7e884886fSBarry Smith #undef __FUNCT__ 8e884886fSBarry Smith #define __FUNCT__ "MatMFFDComputeJacobian" 9e884886fSBarry Smith /*@ 10e884886fSBarry Smith MatMFFDComputeJacobian - Tells the matrix-free Jacobian object the new location at which 11*174415d9SBarry Smith Jacobian matrix vector products will be computed at, i.e. J(x) * a. The x is obtained 12*174415d9SBarry Smith from the SNES object (using SNESGetSolution()). 13e884886fSBarry Smith 14e884886fSBarry Smith Collective on SNES 15e884886fSBarry Smith 16e884886fSBarry Smith Input Parameters: 17e884886fSBarry Smith + snes - the nonlinear solver context 18e884886fSBarry Smith . x - the point at which the Jacobian vector products will be performed 19e884886fSBarry Smith . jac - the matrix-free Jacobian object 20e884886fSBarry Smith . B - either the same as jac or another matrix type (ignored) 21e884886fSBarry Smith . flag - not relevent for matrix-free form 22e884886fSBarry Smith - dummy - the user context (ignored) 23e884886fSBarry Smith 24e884886fSBarry Smith Level: developer 25e884886fSBarry Smith 26*174415d9SBarry Smith Warning: 27*174415d9SBarry Smith If MatMFFDSetBase() is ever called on jac then this routine will NO longer get 28*174415d9SBarry Smith the x from the SNES object and MatMFFDSetBase() must from that point on be used to 29*174415d9SBarry Smith change the base vector x. 30*174415d9SBarry Smith 31e884886fSBarry Smith Notes: 32e884886fSBarry Smith This can be passed into SNESSetJacobian() when using a completely matrix-free solver, 33e884886fSBarry Smith that is the B matrix is also the same matrix operator. This is used when you select 34*174415d9SBarry Smith -snes_mf but rarely used directly by users. 35e884886fSBarry Smith 360decc0a3SBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD, 37e884886fSBarry Smith MatMFFDSetHHistory(), 38e884886fSBarry Smith MatMFFDKSPMonitor(), MatMFFDSetFunctionError(), MatMFFDCreate(), SNESSetJacobian() 39e884886fSBarry Smith 40e884886fSBarry Smith @*/ 410decc0a3SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT MatMFFDComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 42e884886fSBarry Smith { 43e884886fSBarry Smith PetscErrorCode ierr; 44e884886fSBarry Smith PetscFunctionBegin; 45e884886fSBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 46e884886fSBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 47e884886fSBarry Smith PetscFunctionReturn(0); 48e884886fSBarry Smith } 49e884886fSBarry Smith 503ec795f1SBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat,MatAssemblyType); 513ec795f1SBarry Smith #undef __FUNCT__ 523ec795f1SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SNESMF" 533ec795f1SBarry Smith /* 543ec795f1SBarry Smith MatAssemblyEnd_SNESMF - Calls MatAssemblyEnd_MFFD() and then sets the 553ec795f1SBarry Smith base from the SNES context 563ec795f1SBarry Smith 573ec795f1SBarry Smith */ 583ec795f1SBarry Smith PetscErrorCode MatAssemblyEnd_SNESMF(Mat J,MatAssemblyType mt) 593ec795f1SBarry Smith { 603ec795f1SBarry Smith PetscErrorCode ierr; 613ec795f1SBarry Smith MatMFFD j = (MatMFFD)J->data; 623ec795f1SBarry Smith SNES snes = (SNES)j->funcctx; 633ec795f1SBarry Smith 643ec795f1SBarry Smith PetscFunctionBegin; 653ec795f1SBarry Smith ierr = MatAssemblyEnd_MFFD(J,mt);CHKERRQ(ierr); 663ec795f1SBarry Smith 673ec795f1SBarry Smith ierr = SNESGetSolution(snes,&j->current_u);CHKERRQ(ierr); 683ec795f1SBarry Smith ierr = SNESGetFunction(snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 693ec795f1SBarry Smith if (!j->w) { 703ec795f1SBarry Smith ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr); 713ec795f1SBarry Smith } 72*174415d9SBarry Smith printf("snesmf assembly end"); 73*174415d9SBarry Smith VecView(j->current_u,0); 743ec795f1SBarry Smith PetscFunctionReturn(0); 753ec795f1SBarry Smith } 763ec795f1SBarry Smith 77*174415d9SBarry Smith EXTERN_C_BEGIN 78*174415d9SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_FD(Mat,Vec,Vec); 79*174415d9SBarry Smith /* 80*174415d9SBarry Smith This routine resets the MatAssemblyEnd() for the MatMFFD created from MatCreateSNESMF() so that it NO longer 81*174415d9SBarry Smith uses the solution in the SNES object to update the base. See the warning in MatCreateSNESMF(). 82*174415d9SBarry Smith */ 83*174415d9SBarry Smith #undef __FUNCT__ 84*174415d9SBarry Smith #define __FUNCT__ "MatMFFDSetBase_SNESMF" 85*174415d9SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_SNESMF(Mat J,Vec U,Vec F) 86*174415d9SBarry Smith { 87*174415d9SBarry Smith PetscErrorCode ierr; 88*174415d9SBarry Smith 89*174415d9SBarry Smith PetscFunctionBegin; 90*174415d9SBarry Smith ierr = MatMFFDSetBase_FD(J,U,F);CHKERRQ(ierr); 91*174415d9SBarry Smith J->ops->assemblyend = MatAssemblyEnd_MFFD; 92*174415d9SBarry Smith PetscFunctionReturn(0); 93*174415d9SBarry Smith } 94*174415d9SBarry Smith EXTERN_C_END 95*174415d9SBarry Smith 96c5c390f1SBarry Smith #undef __FUNCT__ 974a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF" 9852baeb72SSatish Balay /*@ 9965f2ba5bSLois Curfman McInnes MatCreateSNESMF - Creates a matrix-free matrix context for use with 10065f2ba5bSLois Curfman McInnes a SNES solver. This matrix can be used as the Jacobian argument for 101*174415d9SBarry Smith the routine SNESSetJacobian(). See MatCreateMFFD() for details on how 102*174415d9SBarry Smith the finite difference computation is done. 103a4d4d686SBarry Smith 104a4d4d686SBarry Smith Collective on SNES and Vec 105a4d4d686SBarry Smith 106a4d4d686SBarry Smith Input Parameters: 107a4d4d686SBarry Smith + snes - the SNES context 108a4d4d686SBarry Smith - x - vector where SNES solution is to be stored. 109a4d4d686SBarry Smith 110a4d4d686SBarry Smith Output Parameter: 111a4d4d686SBarry Smith . J - the matrix-free matrix 112a4d4d686SBarry Smith 11315091d37SBarry Smith Level: advanced 11415091d37SBarry Smith 115*174415d9SBarry Smith Warning: 116*174415d9SBarry Smith If MatMFFDSetBase() is ever called on jac then this routine will NO longer get 117*174415d9SBarry Smith the x from the SNES object and MatMFFDSetBase() must from that point on be used to 118*174415d9SBarry Smith change the base vector x. 1199a6cb015SBarry Smith 120*174415d9SBarry Smith Notes: The difference between this routine and MatCreateMFFD() is that this matrix 121*174415d9SBarry Smith automatically gets the current base vector from the SNES object and not from an 122*174415d9SBarry Smith explicit call to MatMFFDSetBase(). 123a4d4d686SBarry Smith 1243ec795f1SBarry Smith .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDefaultSetUmin() 125*174415d9SBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateMFFD(), 1263ec795f1SBarry Smith MatMFFDGetH(),MatMFFDKSPMonitor(), MatMFFDRegisterDynamic), MatMFFDComputeJacobian() 127a4d4d686SBarry Smith 128a4d4d686SBarry Smith @*/ 12963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatCreateSNESMF(SNES snes,Vec x,Mat *J) 130a4d4d686SBarry Smith { 131dfbe8321SBarry Smith PetscErrorCode ierr; 1321d1367b7SBarry Smith 1331d1367b7SBarry Smith PetscFunctionBegin; 1340decc0a3SBarry Smith ierr = MatCreateMFFD(x,J);CHKERRQ(ierr); 1353ec795f1SBarry Smith ierr = MatMFFDSetFunction(*J,(PetscErrorCode (*)(void*, _p_Vec*, _p_Vec*))SNESComputeFunction,snes);CHKERRQ(ierr); 1363ec795f1SBarry Smith (*J)->ops->assemblyend = MatAssemblyEnd_SNESMF; 137*174415d9SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)*J,"MatMFFDSetBase_C","MatMFFDSetBase_SNESMF",MatMFFDSetBase_SNESMF);CHKERRQ(ierr); 1381d1367b7SBarry Smith PetscFunctionReturn(0); 1391d1367b7SBarry Smith } 1401d1367b7SBarry Smith 141cf57b110SBarry Smith 142cf57b110SBarry Smith 143cf57b110SBarry Smith 144cf57b110SBarry Smith 145cf57b110SBarry Smith 146