Actual source code: petscdmmg.h

  1: /*
  2:   Defines the interface functions for the DMMG object.
  3: */
  4: #ifndef __PETSCDMMG_H
 6:  #include petscsnes.h
 7:  #include petscda.h

 10: /*S
 11:      DMMG -  Data structure to easily manage multi-level non-linear solvers on grids managed by DM
 12:           
 13:    Level: intermediate

 15:   Concepts: multigrid, Newton-multigrid

 17: .seealso:  VecPackCreate(), DA, VecPack, DM, DMMGCreate(), DMMGSetKSP(), DMMGSetSNES()
 18: S*/
 19: typedef struct _n_DMMG* DMMG;
 20: struct _n_DMMG {
 21:   DM             dm;                   /* grid information for this level */
 22:   Vec            x,b,r;                /* global vectors used in multigrid preconditioner for this level*/
 23:   Mat            J;                    /* matrix on this level */
 24:   Mat            B;
 25:   Mat            R;                    /* restriction to next coarser level (not defined on level 0) */
 26:   PetscInt       nlevels;              /* number of levels above this one (total number of levels on level 0)*/
 27:   MPI_Comm       comm;
 28:   PetscErrorCode (*solve)(DMMG*,PetscInt);
 29:   void           *user;
 30:   PetscTruth     galerkin;                  /* for A_c = R*A*R^T */

 32:   /* KSP only */
 33:   KSP            ksp;
 34:   PetscErrorCode (*rhs)(DMMG,Vec);
 35:   PetscTruth     matricesset;               /* User had called DMMGSetKSP() and the matrices have been computed */

 37:   /* SNES only */
 38:   Vec            Rscale;                 /* scaling to restriction before computing Jacobian */
 39:   PetscErrorCode (*computejacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
 40:   PetscErrorCode (*computefunction)(SNES,Vec,Vec,void*);

 42:   PetscTruth     updatejacobian;         /* compute new Jacobian when DMMGComputeJacobian_Multigrid() is called */
 43:   PetscInt       updatejacobianperiod;   /* how often, inside a SNES, the Jacobian is recomputed */

 45:   MatFDColoring  fdcoloring;             /* only used with FD coloring for Jacobian */
 46:   SNES           snes;
 47:   PetscErrorCode (*initialguess)(DMMG,Vec);
 48:   Vec            w,work1,work2;         /* global vectors */
 49:   Vec            lwork1;

 51:   /* FAS only */
 52:   NLF            nlf;                   /* FAS smoother object */
 53:   VecScatter     inject;                /* inject from this level to the next coarsest */
 54:   PetscTruth     monitor,monitorall;
 55:   PetscInt       presmooth,postsmooth,coarsesmooth;
 56:   PetscReal      rtol,abstol,rrtol;       /* convergence tolerance */
 57: 
 58: };

 60: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGCreate(MPI_Comm,PetscInt,void*,DMMG**);
 61: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGDestroy(DMMG*);
 62: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetUp(DMMG*);
 63: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetKSP(DMMG*,PetscErrorCode (*)(DMMG,Vec),PetscErrorCode (*)(DMMG,Mat,Mat));
 64: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetSNES(DMMG*,PetscErrorCode (*)(SNES,Vec,Vec,void*),PetscErrorCode (*)(SNES,Vec,Mat*,Mat*,MatStructure*,void*));
 65: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetInitialGuess(DMMG*,PetscErrorCode (*)(DMMG,Vec));
 66: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGInitialGuessCurrent(DMMG,Vec);
 67: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGView(DMMG*,PetscViewer);
 68: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSolve(DMMG*);
 69: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetUseMatrixFree(DMMG*);
 70: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetDM(DMMG*,DM);
 71: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetUpLevel(DMMG*,KSP,PetscInt);
 72: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetUseGalerkinCoarse(DMMG*);
 73: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetNullSpace(DMMG*,PetscTruth,PetscInt,PetscErrorCode (*)(DMMG,Vec[]));

 75: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetSNESLocal_Private(DMMG*,DALocalFunction1,DALocalFunction1,DALocalFunction1,DALocalFunction1);
 76: #if defined(PETSC_HAVE_ADIC)
 77: #  define DMMGSetSNESLocal(dmmg,function,jacobian,ad_function,admf_function) \
 78:   DMMGSetSNESLocal_Private(dmmg,(DALocalFunction1)function,(DALocalFunction1)jacobian,(DALocalFunction1)(ad_function),(DALocalFunction1)(admf_function))
 79: #else
 80: #  define DMMGSetSNESLocal(dmmg,function,jacobian,ad_function,admf_function) DMMGSetSNESLocal_Private(dmmg,(DALocalFunction1)function,(DALocalFunction1)jacobian,(DALocalFunction1)0,(DALocalFunction1)0)
 81: #endif

 83: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetSNESLocali_Private(DMMG*,PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*),PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*),PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*));
 84: #if defined(PETSC_HAVE_ADIC)
 85: #  define DMMGSetSNESLocali(dmmg,function,ad_function,admf_function) DMMGSetSNESLocali_Private(dmmg,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*))function,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*))(ad_function),(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*))(admf_function))
 86: #else
 87: #  define DMMGSetSNESLocali(dmmg,function,ad_function,admf_function) DMMGSetSNESLocali_Private(dmmg,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*))function,0,0)
 88: #endif

 90: EXTERN PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetSNESLocalib_Private(DMMG*,PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*),PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*),PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*));
 91: #if defined(PETSC_HAVE_ADIC)
 92: #  define DMMGSetSNESLocalib(dmmg,function,ad_function,admf_function) DMMGSetSNESLocalib_Private(dmmg,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*))function,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*))(ad_function),(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,void*,void*))(admf_function))
 93: #else
 94: #  define DMMGSetSNESLocalib(dmmg,function,ad_function,admf_function) DMMGSetSNESLocalib_Private(dmmg,(PetscErrorCode(*)(DALocalInfo*,MatStencil*,void*,PetscScalar*,void*))function,0,0)
 95: #endif


 98: /*MC
 99:    DMMGGetRHS - Returns the right hand side vector from a DMMG solve on the finest grid

101:    Synopsis:
102:    Vec DMMGGetRHS(DMMG *dmmg)

104:    Not Collective, but resulting vector is parallel

106:    Input Parameters:
107: .   dmmg - DMMG solve context

109:    Level: intermediate

111:    Fortran Usage:
112: .     DMMGGetRHS(DMMG dmmg,Vec b,PetscErrorCode ierr)

114: .seealso: DMMGCreate(), DMMGSetSNES(), DMMGSetKSP(), DMMGSetSNESLocal(), DMMGGetRHS()

116: M*/
117: #define DMMGGetRHS(ctx)              (ctx)[(ctx)[0]->nlevels-1]->b

119: #define DMMGGetr(ctx)              (ctx)[(ctx)[0]->nlevels-1]->r

121: /*MC
122:    DMMGGetx - Returns the solution vector from a DMMG solve on the finest grid

124:    Synopsis:
125:    Vec DMMGGetx(DMMG *dmmg)

127:    Not Collective, but resulting vector is parallel

129:    Input Parameters:
130: .   dmmg - DMMG solve context

132:    Level: intermediate

134:    Fortran Usage:
135: .     DMMGGetx(DMMG dmmg,Vec x,PetscErrorCode ierr)

137: .seealso: DMMGCreate(), DMMGSetSNES(), DMMGSetKSP(), DMMGSetSNESLocal()

139: M*/
140: #define DMMGGetx(ctx)              (ctx)[(ctx)[0]->nlevels-1]->x

142: /*MC
143:    DMMGGetJ - Returns the Jacobian (matrix) for the finest level

145:    Synopsis:
146:    Mat DMMGGetJ(DMMG *dmmg)

148:    Not Collective

150:    Input Parameter:
151: .   dmmg - DMMG solve context

153:    Level: intermediate

155: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetB(), DMMGGetRHS()

157: M*/
158: #define DMMGGetJ(ctx)              (ctx)[(ctx)[0]->nlevels-1]->J

160: /*MC
161:    DMMGGetComm - Returns the MPI_Comm for the finest level

163:    Synopsis:
164:    MPI_Comm DMMGGetJ(DMMG *dmmg)

166:    Not Collective

168:    Input Parameter:
169: .   dmmg - DMMG solve context

171:    Level: intermediate

173: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ()

175: M*/
176: #define DMMGGetComm(ctx)           (ctx)[(ctx)[0]->nlevels-1]->comm

178: /*MC
179:    DMMGGetB - Returns the matrix for the finest level used to construct the preconditioner; usually 
180:               the same as the Jacobian

182:    Synopsis:
183:    Mat DMMGGetJ(DMMG *dmmg)

185:    Not Collective

187:    Input Parameter:
188: .   dmmg - DMMG solve context

190:    Level: intermediate

192: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ()

194: M*/
195: #define DMMGGetB(ctx)              (ctx)[(ctx)[0]->nlevels-1]->B

197: /*MC
198:    DMMGGetFine - Returns the DMMG associated with the finest level

200:    Synopsis:
201:    DMMG DMMGGetFine(DMMG *dmmg)

203:    Not Collective

205:    Input Parameter:
206: .   dmmg - DMMG solve context

208:    Level: intermediate

210: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ()

212: M*/
213: #define DMMGGetFine(ctx)           (ctx)[(ctx)[0]->nlevels-1]


216: /*MC
217:    DMMGGetKSP - Gets the KSP object (linear solver object) for the finest level

219:    Synopsis:
220:    KSP DMMGGetKSP(DMMG *dmmg)

222:    Not Collective

224:    Input Parameter:
225: .   dmmg - DMMG solve context

227:    Level: intermediate

229:    Notes: If this is a linear problem (i.e. DMMGSetKSP() was used) then this is the 
230:      master linear solver. If this is a nonlinear problem (i.e. DMMGSetSNES() was used) this
231:      returns the KSP (linear solver) that is associated with the SNES (nonlinear solver)

233: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ(), KSPGetSNES()

235: M*/
236: #define DMMGGetKSP(ctx)            (ctx)[(ctx)[0]->nlevels-1]->ksp

238: /*MC
239:    DMMGGetSNES - Gets the SNES object (nonlinear solver) for the finest level

241:    Synopsis:
242:    SNES DMMGGetSNES(DMMG *dmmg)

244:    Not Collective

246:    Input Parameter:
247: .   dmmg - DMMG solve context

249:    Level: intermediate

251:    Notes: If this is a linear problem (i.e. DMMGSetKSP() was used) then this returns PETSC_NULL

253: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ(), KSPGetKSP()

255: M*/
256: #define DMMGGetSNES(ctx)           (ctx)[(ctx)[0]->nlevels-1]->snes

258: /*MC
259:    DMMGGetDA - Gets the DA object on the finest level

261:    Synopsis:
262:    DA DMMGGetDA(DMMG *dmmg)

264:    Not Collective

266:    Input Parameter:
267: .   dmmg - DMMG solve context

269:    Level: intermediate

271:    Notes: Use only if the DMMG was created with a DA, not a VecPack

273: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ(), KSPGetKSP(), DMMGGetVecPack()

275: M*/
276: #define DMMGGetDA(ctx)             (DA)((ctx)[(ctx)[0]->nlevels-1]->dm)

278: /*MC
279:    DMMGGetVecPack - Gets the VecPack object on the finest level

281:    Synopsis:
282:    VecPack DMMGGetVecPack(DMMG *dmmg)

284:    Not Collective

286:    Input Parameter:
287: .   dmmg - DMMG solve context

289:    Level: intermediate

291:    Notes: Use only if the DMMG was created with a DA, not a VecPack

293: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetJ(), KSPGetKSP(), DMMGGetDA()

295: M*/
296: #define DMMGGetVecPack(ctx)        (VecPack)((ctx)[(ctx)[0]->nlevels-1]->dm)

298: /*MC
299:    DMMGGetUser - Returns the user context for a particular level

301:    Synopsis:
302:    void* DMMGGetUser(DMMG *dmmg,PetscInt level)

304:    Not Collective

306:    Input Parameters:
307: +   dmmg - DMMG solve context
308: -   level - the number of the level you want the context for

310:    Level: intermediate

312: .seealso: DMMGCreate(), DMMGSetUser()

314: M*/
315: #define DMMGGetUser(ctx,level)     ((ctx)[level]->user)

317: /*MC
318:    DMMGSetUser - Sets the user context for a particular level

320:    Synopsis:
321:    PetscErrorCode DMMGSetUser(DMMG *dmmg,PetscInt level,void *ctx)

323:    Not Collective

325:    Input Parameters:
326: +   dmmg - DMMG solve context
327: .   level - the number of the level you want the context for
328: -   ctx - the context

330:    Level: intermediate

332:    Note: if the context is the same for each level just pass it in with 
333:          DMMGCreate() and don't call this macro

335: .seealso: DMMGCreate(), DMMGGetUser()

337: M*/
338: #define DMMGSetUser(ctx,level,usr) ((ctx)[level]->user = usr,0)

340: /*MC
341:    DMMGGetLevels - Gets the number of levels in a DMMG object

343:    Synopsis:
344:    PetscInt DMMGGetLevels(DMMG *dmmg)

346:    Not Collective

348:    Input Parameter:
349: .   dmmg - DMMG solve context

351:    Level: intermediate

353: .seealso: DMMGCreate(), DMMGGetUser()

355: M*/
356: #define DMMGGetLevels(ctx)         (ctx)[0]->nlevels

358: /*MC
359:    DMMGGetDMMG - Returns the DMMG struct for the finest level

361:    Synopsis:
362:    DMMG DMMGGetDMMG(DMMG *dmmg)

364:    Not Collective

366:    Input Parameter:
367: .   dmmg - DMMG solve context

369:    Level: intermediate

371: .seealso: DMMGCreate(), DMMGSetUser(), DMMGGetB()

373: M*/
374: #define DMMGGetDMMG(ctx)              (ctx)[(ctx)[0]->nlevels-1]

377: #endif