xref: /petsc/src/snes/mf/snesmfj.c (revision d55644ed203c782412c7dfb5a2203bbb25bde2a5)
1 
2 #include <private/snesimpl.h>  /*I  "petscsnes.h" I*/
3 #include <../src/mat/impls/mffd/mffdimpl.h>
4 #include <private/matimpl.h>
5 
6 #undef __FUNCT__
7 #define __FUNCT__ "MatMFFDComputeJacobian"
8 /*@C
9    MatMFFDComputeJacobian - Tells the matrix-free Jacobian object the new location at which
10        Jacobian matrix vector products will be computed at, i.e. J(x) * a. The x is obtained
11        from the SNES object (using SNESGetSolution()).
12 
13    Logically Collective on SNES
14 
15    Input Parameters:
16 +   snes - the nonlinear solver context
17 .   x - the point at which the Jacobian vector products will be performed
18 .   jac - the matrix-free Jacobian object
19 .   B - either the same as jac or another matrix type (ignored)
20 .   flag - not relevent for matrix-free form
21 -   dummy - the user context (ignored)
22 
23    Level: developer
24 
25    Warning:
26       If MatMFFDSetBase() is ever called on jac then this routine will NO longer get
27     the x from the SNES object and MatMFFDSetBase() must from that point on be used to
28     change the base vector x.
29 
30    Notes:
31      This can be passed into SNESSetJacobian() when using a completely matrix-free solver,
32      that is the B matrix is also the same matrix operator. This is used when you select
33      -snes_mf but rarely used directly by users. (All this routine does is call MatAssemblyBegin/End() on
34      the Mat jac.
35 
36 .seealso: MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD,
37           MatMFFDSetHHistory(), MatMFFDSetFunctionError(), MatCreateMFFD(), SNESSetJacobian()
38 
39 @*/
40 PetscErrorCode  MatMFFDComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
41 {
42   PetscErrorCode ierr;
43   PetscFunctionBegin;
44   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
45   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
46   PetscFunctionReturn(0);
47 }
48 
49 PetscErrorCode MatAssemblyEnd_MFFD(Mat,MatAssemblyType);
50 EXTERN_C_BEGIN
51 PetscErrorCode MatMFFDSetBase_MFFD(Mat,Vec,Vec);
52 EXTERN_C_END
53 #undef __FUNCT__
54 #define __FUNCT__ "MatAssemblyEnd_SNESMF"
55 /*
56    MatAssemblyEnd_SNESMF - Calls MatAssemblyEnd_MFFD() and then sets the
57     base from the SNES context
58 
59 */
60 PetscErrorCode MatAssemblyEnd_SNESMF(Mat J,MatAssemblyType mt)
61 {
62   PetscErrorCode ierr;
63   MatMFFD        j = (MatMFFD)J->data;
64   SNES           snes = (SNES)j->funcctx;
65   Vec            u,f;
66 
67   PetscFunctionBegin;
68   ierr = MatAssemblyEnd_MFFD(J,mt);CHKERRQ(ierr);
69 
70   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
71   ierr = SNESGetFunction(snes,&f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
72   ierr = MatMFFDSetBase_MFFD(J,u,f);CHKERRQ(ierr);
73   PetscFunctionReturn(0);
74 }
75 
76 EXTERN_C_BEGIN
77 /*
78     This routine resets the MatAssemblyEnd() for the MatMFFD created from MatCreateSNESMF() so that it NO longer
79   uses the solution in the SNES object to update the base. See the warning in MatCreateSNESMF().
80 */
81 #undef __FUNCT__
82 #define __FUNCT__ "MatMFFDSetBase_SNESMF"
83 PetscErrorCode  MatMFFDSetBase_SNESMF(Mat J,Vec U,Vec F)
84 {
85   PetscErrorCode ierr;
86 
87   PetscFunctionBegin;
88   ierr = MatMFFDSetBase_MFFD(J,U,F);CHKERRQ(ierr);
89   J->ops->assemblyend = MatAssemblyEnd_MFFD;
90   PetscFunctionReturn(0);
91 }
92 EXTERN_C_END
93 
94 #undef __FUNCT__
95 #define __FUNCT__ "MatCreateSNESMF"
96 /*@
97    MatCreateSNESMF - Creates a matrix-free matrix context for use with
98    a SNES solver.  This matrix can be used as the Jacobian argument for
99    the routine SNESSetJacobian(). See MatCreateMFFD() for details on how
100    the finite difference computation is done.
101 
102    Collective on SNES and Vec
103 
104    Input Parameters:
105 .  snes - the SNES context
106 
107    Output Parameter:
108 .  J - the matrix-free matrix
109 
110    Level: advanced
111 
112    Warning:
113       If MatMFFDSetBase() is ever called on jac then this routine will NO longer get
114     the x from the SNES object and MatMFFDSetBase() must from that point on be used to
115     change the base vector x.
116 
117    Notes: The difference between this routine and MatCreateMFFD() is that this matrix
118      automatically gets the current base vector from the SNES object and not from an
119      explicit call to MatMFFDSetBase().
120 
121 .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDSSetUmin()
122           MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateMFFD(),
123           MatMFFDGetH(), MatMFFDRegisterDynamic), MatMFFDComputeJacobian()
124 
125 @*/
126 PetscErrorCode  MatCreateSNESMF(SNES snes,Mat *J)
127 {
128   PetscErrorCode ierr;
129   PetscInt       n,N;
130 
131   PetscFunctionBegin;
132   if (snes->vec_func) {
133     ierr = VecGetLocalSize(snes->vec_func,&n);CHKERRQ(ierr);
134     ierr = VecGetSize(snes->vec_func,&N);CHKERRQ(ierr);
135   } else if (snes->dm) {
136     Vec tmp;
137     ierr = DMGetGlobalVector(snes->dm,&tmp);CHKERRQ(ierr);
138     ierr = VecGetLocalSize(tmp,&n);CHKERRQ(ierr);
139     ierr = VecGetSize(tmp,&N);CHKERRQ(ierr);
140     ierr = DMRestoreGlobalVector(snes->dm,&tmp);CHKERRQ(ierr);
141   } else SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() or SNESSetDM() first");
142   ierr = MatCreateMFFD(((PetscObject)snes)->comm,n,n,N,N,J);CHKERRQ(ierr);
143   ierr = MatMFFDSetFunction(*J,(PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunction,snes);CHKERRQ(ierr);
144   (*J)->ops->assemblyend = MatAssemblyEnd_SNESMF;
145   ierr = PetscObjectComposeFunctionDynamic((PetscObject)*J,"MatMFFDSetBase_C","MatMFFDSetBase_SNESMF",MatMFFDSetBase_SNESMF);CHKERRQ(ierr);
146   PetscFunctionReturn(0);
147 }
148 
149 
150 
151 
152 
153 
154