Actual source code: snesmfj.c

petsc-master 2018-05-20
Report Typos and Errors

  2:  #include <petsc/private/snesimpl.h>
  3:  #include <petscdm.h>
  4:  #include <../src/mat/impls/mffd/mffdimpl.h>
  5:  #include <petsc/private/matimpl.h>

  7: /*@C
  8:    MatMFFDComputeJacobian - Tells the matrix-free Jacobian object the new location at which
  9:        Jacobian matrix vector products will be computed at, i.e. J(x) * a. The x is obtained
 10:        from the SNES object (using SNESGetSolution()).

 12:    Logically Collective on SNES

 14:    Input Parameters:
 15: +   snes - the nonlinear solver context
 16: .   x - the point at which the Jacobian vector products will be performed
 17: .   jac - the matrix-free Jacobian object
 18: .   B - either the same as jac or another matrix type (ignored)
 19: .   flag - not relevent for matrix-free form
 20: -   dummy - the user context (ignored)

 22:    Level: developer

 24:    Warning:
 25:       If MatMFFDSetBase() is ever called on jac then this routine will NO longer get
 26:     the x from the SNES object and MatMFFDSetBase() must from that point on be used to
 27:     change the base vector x.

 29:    Notes:
 30:      This can be passed into SNESSetJacobian() as the Jacobian evaluation function argument
 31:      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.)

 36: .seealso: MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD,
 37:           MatMFFDSetHHistory(), MatMFFDSetFunctionError(), MatCreateMFFD(), SNESSetJacobian()

 39: @*/
 40: PetscErrorCode  MatMFFDComputeJacobian(SNES snes,Vec x,Mat jac,Mat B,void *dummy)
 41: {

 45:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
 46:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
 47:   return(0);
 48: }

 50: PETSC_EXTERN PetscErrorCode MatAssemblyEnd_MFFD(Mat,MatAssemblyType);
 51: PETSC_EXTERN PetscErrorCode MatMFFDSetBase_MFFD(Mat,Vec,Vec);

 53: /*@
 54:     MatSNESMFGetSNES - returns the SNES associated with a matrix created with MatCreateSNESMF()

 56:     Not collective

 58:     Input Parameter:
 59: .   J - the matrix

 61:     Output Parameter:
 62: .   snes - the SNES object

 64: .seealso: MatCreateSNESMF()
 65: @*/
 66: PetscErrorCode MatSNESMFGetSNES(Mat J,SNES *snes)
 67: {
 68:   MatMFFD        j    = (MatMFFD)J->data;

 71:   *snes = (SNES)j->ctx;
 72:   return(0);
 73: }

 75: /*
 76:    MatAssemblyEnd_SNESMF - Calls MatAssemblyEnd_MFFD() and then sets the
 77:     base from the SNES context

 79: */
 80: static PetscErrorCode MatAssemblyEnd_SNESMF(Mat J,MatAssemblyType mt)
 81: {
 83:   MatMFFD        j    = (MatMFFD)J->data;
 84:   SNES           snes = (SNES)j->ctx;
 85:   Vec            u,f;

 88:   MatAssemblyEnd_MFFD(J,mt);

 90:   SNESGetSolution(snes,&u);
 91:   if (j->func == (PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunction) {
 92:     SNESGetFunction(snes,&f,NULL,NULL);
 93:     MatMFFDSetBase_MFFD(J,u,f);
 94:   } else {
 95:     /* f value known by SNES is not correct for other differencing function */
 96:     MatMFFDSetBase_MFFD(J,u,NULL);
 97:   }
 98:   return(0);
 99: }

101: /*
102:    MatAssemblyEnd_SNESMF_UseBase - Calls MatAssemblyEnd_MFFD() and then sets the
103:     base from the SNES context. This version will cause the base to be used for differencing
104:     even if the func is not SNESComputeFunction. See: MatSNESMFUseBase()


107: */
108: static PetscErrorCode MatAssemblyEnd_SNESMF_UseBase(Mat J,MatAssemblyType mt)
109: {
111:   MatMFFD        j    = (MatMFFD)J->data;
112:   SNES           snes = (SNES)j->ctx;
113:   Vec            u,f;

116:   MatAssemblyEnd_MFFD(J,mt);

118:   SNESGetSolution(snes,&u);
119:   SNESGetFunction(snes,&f,NULL,NULL);
120:   MatMFFDSetBase_MFFD(J,u,f);
121:   return(0);
122: }

124: /*
125:     This routine resets the MatAssemblyEnd() for the MatMFFD created from MatCreateSNESMF() so that it NO longer
126:   uses the solution in the SNES object to update the base. See the warning in MatCreateSNESMF().
127: */
128: static PetscErrorCode  MatMFFDSetBase_SNESMF(Mat J,Vec U,Vec F)
129: {

133:   MatMFFDSetBase_MFFD(J,U,F);
134:   J->ops->assemblyend = MatAssemblyEnd_MFFD;
135:   return(0);
136: }

138: static PetscErrorCode  MatSNESMFSetReuseBase_SNESMF(Mat J,PetscBool use)
139: {
141:   if (use) {
142:     J->ops->assemblyend = MatAssemblyEnd_SNESMF_UseBase;
143:   } else {
144:     J->ops->assemblyend = MatAssemblyEnd_SNESMF;
145:   }
146:   return(0);
147: }

149: /*@
150:     MatSNESMFSetReuseBase - Causes the base vector to be used for differencing even if the function provided to SNESSetFunction() is not the
151:                        same as that provided to MatMFFDSetFunction().

153:     Logically Collective on Mat

155:     Input Parameters:
156: +   J - the MatMFFD matrix
157: -   use - if true always reuse the base vector instead of recomputing f(u) even if the function in the MatSNESMF is 
158:           not SNESComputeFunction()

160:     Notes:
161:     Care must be taken when using this routine to insure that the function provided to MatMFFDSetFunction(), call it F_MF() is compatible with
162:            with that provided to SNESSetFunction(), call it F_SNES(). That is, (F_MF(u + h*d) - F_SNES(u))/h has to approximate the derivative

164:     Developer Notes:
165:     This was provided for the MOOSE team who desired to have a SNESSetFunction() function that could change configurations due
166:                      to contacts while the function provided to MatMFFDSetFunction() cannot. Except for the possibility of changing the configuration
167:                      both functions compute the same mathematical function so the differencing makes sense.

169:     Level: advanced

171: .seealso: MatCreateSNESMF(), MatSNESMFGetReuseBase()
172: @*/
173: PetscErrorCode  MatSNESMFSetReuseBase(Mat J,PetscBool use)
174: {

179:   PetscTryMethod(J,"MatSNESMFSetReuseBase_C",(Mat,PetscBool),(J,use));
180:   return(0);
181: }

183: static PetscErrorCode  MatSNESMFGetReuseBase_SNESMF(Mat J,PetscBool *use)
184: {
186:   if (J->ops->assemblyend == MatAssemblyEnd_SNESMF_UseBase) *use = PETSC_TRUE;
187:   else *use = PETSC_FALSE;
188:   return(0);
189: }

191: /*@
192:     MatSNESMFGetReuseBase - Determines if the base vector is to be used for differencing even if the function provided to SNESSetFunction() is not the
193:                        same as that provided to MatMFFDSetFunction().

195:     Logically Collective on Mat

197:     Input Parameter:
198: .   J - the MatMFFD matrix

200:     Output Parameter:
201: .   use - if true always reuse the base vector instead of recomputing f(u) even if the function in the MatSNESMF is 
202:           not SNESComputeFunction()

204:     Notes:
205:     Care must be taken when using this routine to insure that the function provided to MatMFFDSetFunction(), call it F_MF() is compatible with
206:            with that provided to SNESSetFunction(), call it F_SNES(). That is, (F_MF(u + h*d) - F_SNES(u))/h has to approximate the derivative

208:     Developer Notes:
209:     This was provided for the MOOSE team who desired to have a SNESSetFunction() function that could change configurations due
210:                      to contacts while the function provided to MatMFFDSetFunction() cannot. Except for the possibility of changing the configuration
211:                      both functions compute the same mathematical function so the differencing makes sense.

213:     Level: advanced

215: .seealso: MatCreateSNESMF(), MatSNESMFSetReuseBase()
216: @*/
217: PetscErrorCode  MatSNESMFGetReuseBase(Mat J,PetscBool *use)
218: {

223:   PetscUseMethod(J,"MatSNESMFGetReuseBase_C",(Mat,PetscBool*),(J,use));
224:   return(0);
225: }

227: /*@
228:    MatCreateSNESMF - Creates a matrix-free matrix context for use with
229:    a SNES solver.  This matrix can be used as the Jacobian argument for
230:    the routine SNESSetJacobian(). See MatCreateMFFD() for details on how
231:    the finite difference computation is done.

233:    Collective on SNES and Vec

235:    Input Parameters:
236: .  snes - the SNES context

238:    Output Parameter:
239: .  J - the matrix-free matrix

241:    Level: advanced


244:    Notes:
245:      You can call SNESSetJacobian() with MatMFFDComputeJacobian() if you are using matrix and not a different
246:      preconditioner matrix

248:      If you wish to provide a different function to do differencing on to compute the matrix-free operator than
249:      that provided to SNESSetFunction() then call MatMFFDSetFunction() with your function after this call.

251:      The difference between this routine and MatCreateMFFD() is that this matrix
252:      automatically gets the current base vector from the SNES object and not from an
253:      explicit call to MatMFFDSetBase().

255:    Warning:
256:      If MatMFFDSetBase() is ever called on jac then this routine will NO longer get
257:      the x from the SNES object and MatMFFDSetBase() must from that point on be used to
258:      change the base vector x.

260:    Warning:
261:      Using a different function for the differencing will not work if you are using non-linear left preconditioning.


264: .seealso: MatDestroy(), MatMFFDSetFunction(), MatMFFDSetFunctionError(), MatMFFDDSSetUmin()
265:           MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateMFFD(),
266:           MatMFFDGetH(), MatMFFDRegister(), MatMFFDComputeJacobian(), MatSNESMFSetReuseBase(), MatSNESMFGetReuseBase()

268: @*/
269: PetscErrorCode  MatCreateSNESMF(SNES snes,Mat *J)
270: {
272:   PetscInt       n,N;
273:   MatMFFD        mf;

276:   if (snes->vec_func) {
277:     VecGetLocalSize(snes->vec_func,&n);
278:     VecGetSize(snes->vec_func,&N);
279:   } else if (snes->dm) {
280:     Vec tmp;
281:     DMGetGlobalVector(snes->dm,&tmp);
282:     VecGetLocalSize(tmp,&n);
283:     VecGetSize(tmp,&N);
284:     DMRestoreGlobalVector(snes->dm,&tmp);
285:   } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() or SNESSetDM() first");
286:   MatCreateMFFD(PetscObjectComm((PetscObject)snes),n,n,N,N,J);

288:   mf      = (MatMFFD)(*J)->data;
289:   mf->ctx = snes;

291:   if (snes->npc && snes->npcside== PC_LEFT) {
292:     MatMFFDSetFunction(*J,(PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunctionDefaultNPC,snes);
293:   } else {
294:     MatMFFDSetFunction(*J,(PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunction,snes);
295:   }

297:   (*J)->ops->assemblyend = MatAssemblyEnd_SNESMF;

299:   PetscObjectComposeFunction((PetscObject)*J,"MatMFFDSetBase_C",MatMFFDSetBase_SNESMF);
300:   PetscObjectComposeFunction((PetscObject)*J,"MatSNESMFSetReuseBase_C",MatSNESMFSetReuseBase_SNESMF);
301:   PetscObjectComposeFunction((PetscObject)*J,"MatSNESMFGetReuseBase_C",MatSNESMFGetReuseBase_SNESMF);
302:   return(0);
303: }