163dd3a1aSKris Buschelman #define PETSCSNES_DLL 281e6777dSBarry Smith 37c4f633dSBarry Smith #include "private/snesimpl.h" /*I "petscsnes.h" I*/ 47c4f633dSBarry Smith #include "private/matimpl.h" 57c4f633dSBarry Smith #include "../src/mat/impls/mffd/mffdimpl.h" 681e6777dSBarry Smith 7e884886fSBarry Smith #undef __FUNCT__ 8e884886fSBarry Smith #define __FUNCT__ "MatMFFDComputeJacobian" 9*5fe378a3SBarry Smith /*@C 10e884886fSBarry Smith MatMFFDComputeJacobian - Tells the matrix-free Jacobian object the new location at which 11174415d9SBarry Smith Jacobian matrix vector products will be computed at, i.e. J(x) * a. The x is obtained 12174415d9SBarry 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 26174415d9SBarry Smith Warning: 27174415d9SBarry Smith If MatMFFDSetBase() is ever called on jac then this routine will NO longer get 28174415d9SBarry Smith the x from the SNES object and MatMFFDSetBase() must from that point on be used to 29174415d9SBarry Smith change the base vector x. 30174415d9SBarry 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*5fe378a3SBarry Smith -snes_mf but rarely used directly by users. (All this routine does is call MatAssemblyBegin/End() on 35*5fe378a3SBarry Smith the Mat jac. 36*5fe378a3SBarry Smith 37*5fe378a3SBarry Smith This is not callable or usable in Fortran 38e884886fSBarry Smith 390decc0a3SBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD, 40e884886fSBarry Smith MatMFFDSetHHistory(), 41e884886fSBarry Smith MatMFFDKSPMonitor(), MatMFFDSetFunctionError(), MatMFFDCreate(), SNESSetJacobian() 42e884886fSBarry Smith 43e884886fSBarry Smith @*/ 440decc0a3SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT MatMFFDComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy) 45e884886fSBarry Smith { 46e884886fSBarry Smith PetscErrorCode ierr; 47e884886fSBarry Smith PetscFunctionBegin; 48e884886fSBarry Smith ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 49e884886fSBarry Smith ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 50e884886fSBarry Smith PetscFunctionReturn(0); 51e884886fSBarry Smith } 52e884886fSBarry Smith 533ec795f1SBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat,MatAssemblyType); 543ec795f1SBarry Smith #undef __FUNCT__ 553ec795f1SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SNESMF" 563ec795f1SBarry Smith /* 573ec795f1SBarry Smith MatAssemblyEnd_SNESMF - Calls MatAssemblyEnd_MFFD() and then sets the 583ec795f1SBarry Smith base from the SNES context 593ec795f1SBarry Smith 603ec795f1SBarry Smith */ 613ec795f1SBarry Smith PetscErrorCode MatAssemblyEnd_SNESMF(Mat J,MatAssemblyType mt) 623ec795f1SBarry Smith { 633ec795f1SBarry Smith PetscErrorCode ierr; 643ec795f1SBarry Smith MatMFFD j = (MatMFFD)J->data; 653ec795f1SBarry Smith SNES snes = (SNES)j->funcctx; 66be4711e3SBarry Smith Vec u,f; 673ec795f1SBarry Smith 683ec795f1SBarry Smith PetscFunctionBegin; 693ec795f1SBarry Smith ierr = MatAssemblyEnd_MFFD(J,mt);CHKERRQ(ierr); 703ec795f1SBarry Smith 71be4711e3SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 72be4711e3SBarry Smith ierr = SNESGetFunction(snes,&f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 73be4711e3SBarry Smith ierr = MatMFFDSetBase(J,u,f);CHKERRQ(ierr); 743ec795f1SBarry Smith PetscFunctionReturn(0); 753ec795f1SBarry Smith } 763ec795f1SBarry Smith 77174415d9SBarry Smith EXTERN_C_BEGIN 78885877adSHong Zhang extern PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_MFFD(Mat,Vec,Vec); 79174415d9SBarry Smith /* 80174415d9SBarry Smith This routine resets the MatAssemblyEnd() for the MatMFFD created from MatCreateSNESMF() so that it NO longer 81174415d9SBarry Smith uses the solution in the SNES object to update the base. See the warning in MatCreateSNESMF(). 82174415d9SBarry Smith */ 83174415d9SBarry Smith #undef __FUNCT__ 84174415d9SBarry Smith #define __FUNCT__ "MatMFFDSetBase_SNESMF" 85174415d9SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_SNESMF(Mat J,Vec U,Vec F) 86174415d9SBarry Smith { 87174415d9SBarry Smith PetscErrorCode ierr; 88174415d9SBarry Smith 89174415d9SBarry Smith PetscFunctionBegin; 90885877adSHong Zhang ierr = MatMFFDSetBase_MFFD(J,U,F);CHKERRQ(ierr); 91174415d9SBarry Smith J->ops->assemblyend = MatAssemblyEnd_MFFD; 92174415d9SBarry Smith PetscFunctionReturn(0); 93174415d9SBarry Smith } 94174415d9SBarry Smith EXTERN_C_END 95174415d9SBarry 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 101174415d9SBarry Smith the routine SNESSetJacobian(). See MatCreateMFFD() for details on how 102174415d9SBarry Smith the finite difference computation is done. 103a4d4d686SBarry Smith 104a4d4d686SBarry Smith Collective on SNES and Vec 105a4d4d686SBarry Smith 106a4d4d686SBarry Smith Input Parameters: 107fef1beadSBarry Smith . snes - the SNES context 108a4d4d686SBarry Smith 109a4d4d686SBarry Smith Output Parameter: 110a4d4d686SBarry Smith . J - the matrix-free matrix 111a4d4d686SBarry Smith 11215091d37SBarry Smith Level: advanced 11315091d37SBarry Smith 114174415d9SBarry Smith Warning: 115174415d9SBarry Smith If MatMFFDSetBase() is ever called on jac then this routine will NO longer get 116174415d9SBarry Smith the x from the SNES object and MatMFFDSetBase() must from that point on be used to 117174415d9SBarry Smith change the base vector x. 1189a6cb015SBarry Smith 119174415d9SBarry Smith Notes: The difference between this routine and MatCreateMFFD() is that this matrix 120174415d9SBarry Smith automatically gets the current base vector from the SNES object and not from an 121174415d9SBarry Smith explicit call to MatMFFDSetBase(). 122a4d4d686SBarry Smith 1233ec795f1SBarry Smith .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDefaultSetUmin() 124174415d9SBarry Smith MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateMFFD(), 1253ec795f1SBarry Smith MatMFFDGetH(),MatMFFDKSPMonitor(), MatMFFDRegisterDynamic), MatMFFDComputeJacobian() 126a4d4d686SBarry Smith 127a4d4d686SBarry Smith @*/ 128fef1beadSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT MatCreateSNESMF(SNES snes,Mat *J) 129a4d4d686SBarry Smith { 130dfbe8321SBarry Smith PetscErrorCode ierr; 131fef1beadSBarry Smith PetscInt n,N; 1321d1367b7SBarry Smith 1331d1367b7SBarry Smith PetscFunctionBegin; 134fef1beadSBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 135fef1beadSBarry Smith 136fef1beadSBarry Smith ierr = VecGetLocalSize(snes->vec_func,&n);CHKERRQ(ierr); 137fef1beadSBarry Smith ierr = VecGetSize(snes->vec_func,&N);CHKERRQ(ierr); 1387adad957SLisandro Dalcin ierr = MatCreateMFFD(((PetscObject)snes)->comm,n,n,N,N,J);CHKERRQ(ierr); 139ece7ea46SSatish Balay ierr = MatMFFDSetFunction(*J,(PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunction,snes);CHKERRQ(ierr); 1403ec795f1SBarry Smith (*J)->ops->assemblyend = MatAssemblyEnd_SNESMF; 141174415d9SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)*J,"MatMFFDSetBase_C","MatMFFDSetBase_SNESMF",MatMFFDSetBase_SNESMF);CHKERRQ(ierr); 1421d1367b7SBarry Smith PetscFunctionReturn(0); 1431d1367b7SBarry Smith } 1441d1367b7SBarry Smith 145cf57b110SBarry Smith 146cf57b110SBarry Smith 147cf57b110SBarry Smith 148cf57b110SBarry Smith 149cf57b110SBarry Smith 150