Actual source code: pcset.c

  1: #define PETSCKSP_DLL
  2: /*
  3:     Routines to set PC methods and options.
  4: */

 6:  #include private/pcimpl.h

  8: PetscTruth PCRegisterAllCalled = PETSC_FALSE;
  9: /*
 10:    Contains the list of registered KSP routines
 11: */
 12: PetscFList PCList = 0;

 16: /*@C
 17:    PCSetType - Builds PC for a particular preconditioner.

 19:    Collective on PC

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

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

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

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

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

 47:   Level: intermediate

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

 51: .seealso: KSPSetType(), PCType

 53: @*/
 54: PetscErrorCode  PCSetType(PC pc,const PCType type)
 55: {
 56:   PetscErrorCode ierr,(*r)(PC);
 57:   PetscTruth     match;


 63:   PetscTypeCompare((PetscObject)pc,type,&match);
 64:   if (match) return(0);

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

 85: /*@
 86:    PCRegisterDestroy - Frees the list of preconditioners that were
 87:    registered by PCRegisterDynamic().

 89:    Not Collective

 91:    Level: advanced

 93: .keywords: PC, register, destroy

 95: .seealso: PCRegisterAll(), PCRegisterAll()

 97: @*/
 98: PetscErrorCode  PCRegisterDestroy(void)
 99: {

103:   PetscFListDestroy(&PCList);
104:   PCRegisterAllCalled = PETSC_FALSE;
105:   return(0);
106: }

110: /*@C
111:    PCGetType - Gets the PC method type and name (as a string) from the PC
112:    context.

114:    Not Collective

116:    Input Parameter:
117: .  pc - the preconditioner context

119:    Output Parameter:
120: .  type - name of preconditioner method

122:    Level: intermediate

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

126: .seealso: PCSetType()

128: @*/
129: PetscErrorCode  PCGetType(PC pc,const PCType *type)
130: {
134:   *type = ((PetscObject)pc)->type_name;
135:   return(0);
136: }

138: EXTERN PetscErrorCode PCGetDefaultType_Private(PC,const char*[]);

142: /*@
143:    PCSetFromOptions - Sets PC options from the options database.
144:    This routine must be called before PCSetUp() if the user is to be
145:    allowed to set the preconditioner method. 

147:    Collective on PC

149:    Input Parameter:
150: .  pc - the preconditioner context

152:    Level: developer

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

156: .seealso: 

158: @*/
159: PetscErrorCode  PCSetFromOptions(PC pc)
160: {
162:   char           type[256];
163:   const char     *def;
164:   PetscTruth     flg;


169:   if (!PCRegisterAllCalled) {PCRegisterAll(PETSC_NULL);}
170:   PetscOptionsBegin(((PetscObject)pc)->comm,((PetscObject)pc)->prefix,"Preconditioner (PC) Options","PC");
171:     if (!((PetscObject)pc)->type_name) {
172:       PCGetDefaultType_Private(pc,&def);
173:     } else {
174:       def = ((PetscObject)pc)->type_name;
175:     }

177:     PetscOptionsList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);
178:     if (flg) {
179:       PCSetType(pc,type);
180:     } else if (!((PetscObject)pc)->type_name){
181:       PCSetType(pc,def);
182:     }

184:     if (pc->ops->setfromoptions) {
185:       (*pc->ops->setfromoptions)(pc);
186:     }
187:   PetscOptionsEnd();
188:   pc->setfromoptionscalled++;
189:   return(0);
190: }