Actual source code: pcset.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: /*
  3:     Routines to set PC methods and options.
  4: */

  6: #include <petsc-private/pcimpl.h>      /*I "petscpc.h" I*/
  7: #include <petscdm.h>

  9: PetscBool PCRegisterAllCalled = PETSC_FALSE;
 10: /*
 11:    Contains the list of registered KSP routines
 12: */
 13: PetscFunctionList PCList = 0;

 17: /*@C
 18:    PCSetType - Builds PC for a particular preconditioner type

 20:    Collective on PC

 22:    Input Parameter:
 23: +  pc - the preconditioner context.
 24: -  type - a known method

 26:    Options Database Key:
 27: .  -pc_type <type> - Sets PC type

 29:    Use -help for a list of available methods (for instance,
 30:    jacobi or bjacobi)

 32:   Notes:
 33:   See "petsc/include/petscpc.h" for available methods (for instance,
 34:   PCJACOBI, PCILU, or PCBJACOBI).

 36:   Normally, it is best to use the KSPSetFromOptions() command and
 37:   then set the PC type from the options database rather than by using
 38:   this routine.  Using the options database provides the user with
 39:   maximum flexibility in evaluating the many different preconditioners.
 40:   The PCSetType() routine is provided for those situations where it
 41:   is necessary to set the preconditioner independently of the command
 42:   line or options database.  This might be the case, for example, when
 43:   the choice of preconditioner changes during the execution of the
 44:   program, and the user's application is taking responsibility for
 45:   choosing the appropriate preconditioner.  In other words, this
 46:   routine is not for beginners.

 48:   Level: intermediate

 50:   Developer Note: PCRegister() is used to add preconditioner types to PCList from which they
 51:   are accessed by PCSetType().

 53: .keywords: PC, set, method, type

 55: .seealso: KSPSetType(), PCType, PCRegister(), PCCreate(), KSPGetPC()

 57: @*/
 58: PetscErrorCode  PCSetType(PC pc,PCType type)
 59: {
 60:   PetscErrorCode ierr,(*r)(PC);
 61:   PetscBool      match;


 67:   PetscObjectTypeCompare((PetscObject)pc,type,&match);
 68:   if (match) return(0);

 70:    PetscFunctionListFind(PCList,type,&r);
 71:   if (!r) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PC type %s",type);
 72:   /* Destroy the previous private PC context */
 73:   if (pc->ops->destroy) {
 74:      (*pc->ops->destroy)(pc);
 75:     pc->ops->destroy = NULL;
 76:     pc->data         = 0;
 77:   }
 78:   PetscFunctionListDestroy(&((PetscObject)pc)->qlist);
 79:   /* Reinitialize function pointers in PCOps structure */
 80:   PetscMemzero(pc->ops,sizeof(struct _PCOps));
 81:   /* XXX Is this OK?? */
 82:   pc->modifysubmatrices  = 0;
 83:   pc->modifysubmatricesP = 0;
 84:   /* Call the PCCreate_XXX routine for this particular preconditioner */
 85:   pc->setupcalled = 0;

 87:   PetscObjectChangeTypeName((PetscObject)pc,type);
 88:   (*r)(pc);
 89:   return(0);
 90: }

 94: /*@C
 95:    PCGetType - Gets the PC method type and name (as a string) from the PC
 96:    context.

 98:    Not Collective

100:    Input Parameter:
101: .  pc - the preconditioner context

103:    Output Parameter:
104: .  type - name of preconditioner method

106:    Level: intermediate

108: .keywords: PC, get, method, name, type

110: .seealso: PCSetType()

112: @*/
113: PetscErrorCode  PCGetType(PC pc,PCType *type)
114: {
118:   *type = ((PetscObject)pc)->type_name;
119:   return(0);
120: }

122: extern PetscErrorCode PCGetDefaultType_Private(PC,const char*[]);

126: /*@
127:    PCSetFromOptions - Sets PC options from the options database.
128:    This routine must be called before PCSetUp() if the user is to be
129:    allowed to set the preconditioner method.

131:    Collective on PC

133:    Input Parameter:
134: .  pc - the preconditioner context

136:    Options Database:
137: .   -pc_use_amat true,false see PCSetUseAmat()

139:    Level: developer

141: .keywords: PC, set, from, options, database

143: .seealso: PCSetUseAmat()

145: @*/
146: PetscErrorCode  PCSetFromOptions(PC pc)
147: {
149:   char           type[256];
150:   const char     *def;
151:   PetscBool      flg;


156:   if (!PCRegisterAllCalled) {PCRegisterAll();}
157:   PetscObjectOptionsBegin((PetscObject)pc);
158:   if (!((PetscObject)pc)->type_name) {
159:     PCGetDefaultType_Private(pc,&def);
160:   } else {
161:     def = ((PetscObject)pc)->type_name;
162:   }

164:   PetscOptionsFList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);
165:   if (flg) {
166:     PCSetType(pc,type);
167:   } else if (!((PetscObject)pc)->type_name) {
168:     PCSetType(pc,def);
169:   }

171:   PetscOptionsBool("-pc_use_amat","use Amat (instead of Pmat) to define preconditioner in nested inner solves","PCSetUseAmat",pc->useAmat,&pc->useAmat,NULL);

173:   if (pc->ops->setfromoptions) {
174:     (*pc->ops->setfromoptions)(pc);
175:   }

177:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
178:   PetscObjectProcessOptionsHandlers((PetscObject)pc);
179:   PetscOptionsEnd();
180:   pc->setfromoptionscalled++;
181:   return(0);
182: }

186: /*@
187:    PCSetDM - Sets the DM that may be used by some preconditioners

189:    Logically Collective on PC

191:    Input Parameters:
192: +  pc - the preconditioner context
193: -  dm - the dm

195:    Level: intermediate


198: .seealso: PCGetDM(), KSPSetDM(), KSPGetDM()
199: @*/
200: PetscErrorCode  PCSetDM(PC pc,DM dm)
201: {

206:   if (dm) {PetscObjectReference((PetscObject)dm);}
207:   DMDestroy(&pc->dm);
208:   pc->dm = dm;
209:   return(0);
210: }

214: /*@
215:    PCGetDM - Gets the DM that may be used by some preconditioners

217:    Not Collective

219:    Input Parameter:
220: . pc - the preconditioner context

222:    Output Parameter:
223: .  dm - the dm

225:    Level: intermediate


228: .seealso: PCSetDM(), KSPSetDM(), KSPGetDM()
229: @*/
230: PetscErrorCode  PCGetDM(PC pc,DM *dm)
231: {
234:   *dm = pc->dm;
235:   return(0);
236: }

240: /*@
241:    PCSetApplicationContext - Sets the optional user-defined context for the linear solver.

243:    Logically Collective on PC

245:    Input Parameters:
246: +  pc - the PC context
247: -  usrP - optional user context

249:    Level: intermediate

251: .keywords: PC, set, application, context

253: .seealso: PCGetApplicationContext()
254: @*/
255: PetscErrorCode  PCSetApplicationContext(PC pc,void *usrP)
256: {
259:   pc->user = usrP;
260:   return(0);
261: }

265: /*@
266:    PCGetApplicationContext - Gets the user-defined context for the linear solver.

268:    Not Collective

270:    Input Parameter:
271: .  pc - PC context

273:    Output Parameter:
274: .  usrP - user context

276:    Level: intermediate

278: .keywords: PC, get, application, context

280: .seealso: PCSetApplicationContext()
281: @*/
282: PetscErrorCode  PCGetApplicationContext(PC pc,void *usrP)
283: {
286:   *(void**)usrP = pc->user;
287:   return(0);
288: }