xref: /petsc/src/snes/mf/snesmfj.c (revision 0decc0a32d11c9048faef123c7738214ee57a3d4)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
281e6777dSBarry Smith 
3*0decc0a3SBarry Smith #include "include/private/snesimpl.h"  /*I  "petscsnes.h" I*/
481e6777dSBarry Smith 
5e884886fSBarry Smith #undef __FUNCT__
6e884886fSBarry Smith #define __FUNCT__ "MatMFFDComputeJacobian"
7e884886fSBarry Smith /*@
8e884886fSBarry Smith    MatMFFDComputeJacobian - Tells the matrix-free Jacobian object the new location at which
9e884886fSBarry Smith        Jacobian matrix vector products will be computed at, i.e. J(x) * a.
10e884886fSBarry Smith 
11e884886fSBarry Smith    Collective on SNES
12e884886fSBarry Smith 
13e884886fSBarry Smith    Input Parameters:
14e884886fSBarry Smith +   snes - the nonlinear solver context
15e884886fSBarry Smith .   x - the point at which the Jacobian vector products will be performed
16e884886fSBarry Smith .   jac - the matrix-free Jacobian object
17e884886fSBarry Smith .   B - either the same as jac or another matrix type (ignored)
18e884886fSBarry Smith .   flag - not relevent for matrix-free form
19e884886fSBarry Smith -   dummy - the user context (ignored)
20e884886fSBarry Smith 
21e884886fSBarry Smith    Level: developer
22e884886fSBarry Smith 
23e884886fSBarry Smith    Notes:
24e884886fSBarry Smith      This can be passed into SNESSetJacobian() when using a completely matrix-free solver,
25e884886fSBarry Smith      that is the B matrix is also the same matrix operator. This is used when you select
26e884886fSBarry Smith      -mat_mffd but rarely used directly by users.
27e884886fSBarry Smith 
28*0decc0a3SBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD,
29e884886fSBarry Smith           MatMFFDSetHHistory(),
30e884886fSBarry Smith           MatMFFDKSPMonitor(), MatMFFDSetFunctionError(), MatMFFDCreate(), SNESSetJacobian()
31e884886fSBarry Smith 
32e884886fSBarry Smith @*/
33*0decc0a3SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT MatMFFDComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
34e884886fSBarry Smith {
35e884886fSBarry Smith   PetscErrorCode ierr;
36e884886fSBarry Smith   PetscFunctionBegin;
37e884886fSBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38e884886fSBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
39e884886fSBarry Smith   PetscFunctionReturn(0);
40e884886fSBarry Smith }
41e884886fSBarry Smith 
42c5c390f1SBarry Smith #undef __FUNCT__
434a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
4452baeb72SSatish Balay /*@
4565f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
4665f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
4765f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
48a4d4d686SBarry Smith 
49a4d4d686SBarry Smith    Collective on SNES and Vec
50a4d4d686SBarry Smith 
51a4d4d686SBarry Smith    Input Parameters:
52a4d4d686SBarry Smith +  snes - the SNES context
53a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
54a4d4d686SBarry Smith 
55a4d4d686SBarry Smith    Output Parameter:
56a4d4d686SBarry Smith .  J - the matrix-free matrix
57a4d4d686SBarry Smith 
5815091d37SBarry Smith    Level: advanced
5915091d37SBarry Smith 
60a4d4d686SBarry Smith    Notes:
61a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
62a4d4d686SBarry Smith    and work space for performing finite difference approximations of
6365f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
649a6cb015SBarry Smith 
659a6cb015SBarry Smith    The default code uses the following approach to compute h
66a4d4d686SBarry Smith 
67a4d4d686SBarry Smith .vb
6865f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
69a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
70a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
71a4d4d686SBarry Smith  where
72a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
73a4d4d686SBarry Smith      umin = minimum iterate parameter
74a4d4d686SBarry Smith .ve
75efb30889SBarry Smith    (see MATSNESMF_WP or MATSNESMF_DS)
76a4d4d686SBarry Smith 
775a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
7865f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
7965f2ba5bSLois Curfman McInnes    of the users manual for details.
80a4d4d686SBarry Smith 
81a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
82a4d4d686SBarry Smith    matrix context.
83a4d4d686SBarry Smith 
84a4d4d686SBarry Smith    Options Database Keys:
85*0decc0a3SBarry Smith +  -mat_mffd_err <error_rel> - Sets error_rel
86*0decc0a3SBarry Smith +  -mat_mffd_type - wp or ds (see MATSNESMF_WP or MATSNESMF_DS)
87*0decc0a3SBarry Smith .  -mat_mffd_unim <umin> - Sets umin (for default PETSc routine that computes h only)
88*0decc0a3SBarry Smith -  -mat_mffd_ksp_monitor - KSP monitor routine that prints differencing h
89a4d4d686SBarry Smith 
90a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
91a4d4d686SBarry Smith 
925a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
931d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
94fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
95a4d4d686SBarry Smith 
96a4d4d686SBarry Smith @*/
9763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatCreateSNESMF(SNES snes,Vec x,Mat *J)
98a4d4d686SBarry Smith {
99dfbe8321SBarry Smith   PetscErrorCode ierr;
1001d1367b7SBarry Smith 
1011d1367b7SBarry Smith   PetscFunctionBegin;
102*0decc0a3SBarry Smith   ierr = MatCreateMFFD(x,J);CHKERRQ(ierr);
103*0decc0a3SBarry Smith   ierr = MatMFFDSetFunction(*J,snes->vec_sol,(PetscErrorCode (*)(void*, _p_Vec*, _p_Vec*))SNESComputeFunction,snes);CHKERRQ(ierr);
1041d1367b7SBarry Smith   PetscFunctionReturn(0);
1051d1367b7SBarry Smith }
1061d1367b7SBarry Smith 
107cf57b110SBarry Smith 
108cf57b110SBarry Smith 
109cf57b110SBarry Smith 
110cf57b110SBarry Smith 
111cf57b110SBarry Smith 
112