xref: /petsc/src/ksp/pc/interface/pcset.c (revision a7d21a526a65b4eaf18e8d7da4fb7df37817b1bf)
1dba47a55SKris Buschelman #define PETSCKSP_DLL
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith     Routines to set PC methods and options.
44b9ad928SBarry Smith */
54b9ad928SBarry Smith 
66356e834SBarry Smith #include "private/pcimpl.h"      /*I "petscpc.h" I*/
74b9ad928SBarry Smith #include "petscsys.h"
84b9ad928SBarry Smith 
94b9ad928SBarry Smith PetscTruth PCRegisterAllCalled = PETSC_FALSE;
104b9ad928SBarry Smith /*
114b9ad928SBarry Smith    Contains the list of registered KSP routines
124b9ad928SBarry Smith */
134b9ad928SBarry Smith PetscFList PCList = 0;
144b9ad928SBarry Smith 
154b9ad928SBarry Smith #undef __FUNCT__
164b9ad928SBarry Smith #define __FUNCT__ "PCSetType"
174b9ad928SBarry Smith /*@C
184b9ad928SBarry Smith    PCSetType - Builds PC for a particular preconditioner.
194b9ad928SBarry Smith 
204b9ad928SBarry Smith    Collective on PC
214b9ad928SBarry Smith 
224b9ad928SBarry Smith    Input Parameter:
234b9ad928SBarry Smith +  pc - the preconditioner context.
244b9ad928SBarry Smith -  type - a known method
254b9ad928SBarry Smith 
264b9ad928SBarry Smith    Options Database Key:
274b9ad928SBarry Smith .  -pc_type <type> - Sets PC type
284b9ad928SBarry Smith 
294b9ad928SBarry Smith    Use -help for a list of available methods (for instance,
304b9ad928SBarry Smith    jacobi or bjacobi)
314b9ad928SBarry Smith 
324b9ad928SBarry Smith   Notes:
334b9ad928SBarry Smith   See "petsc/include/petscpc.h" for available methods (for instance,
344b9ad928SBarry Smith   PCJACOBI, PCILU, or PCBJACOBI).
354b9ad928SBarry Smith 
364b9ad928SBarry Smith   Normally, it is best to use the KSPSetFromOptions() command and
374b9ad928SBarry Smith   then set the PC type from the options database rather than by using
384b9ad928SBarry Smith   this routine.  Using the options database provides the user with
394b9ad928SBarry Smith   maximum flexibility in evaluating the many different preconditioners.
404b9ad928SBarry Smith   The PCSetType() routine is provided for those situations where it
414b9ad928SBarry Smith   is necessary to set the preconditioner independently of the command
424b9ad928SBarry Smith   line or options database.  This might be the case, for example, when
434b9ad928SBarry Smith   the choice of preconditioner changes during the execution of the
444b9ad928SBarry Smith   program, and the user's application is taking responsibility for
454b9ad928SBarry Smith   choosing the appropriate preconditioner.  In other words, this
464b9ad928SBarry Smith   routine is not for beginners.
474b9ad928SBarry Smith 
484b9ad928SBarry Smith   Level: intermediate
494b9ad928SBarry Smith 
504b9ad928SBarry Smith .keywords: PC, set, method, type
514b9ad928SBarry Smith 
524b9ad928SBarry Smith .seealso: KSPSetType(), PCType
534b9ad928SBarry Smith 
544b9ad928SBarry Smith @*/
55f69a0ea3SMatthew Knepley PetscErrorCode PETSCKSP_DLLEXPORT PCSetType(PC pc, PCType type)
564b9ad928SBarry Smith {
57dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(PC);
584b9ad928SBarry Smith   PetscTruth     match;
594b9ad928SBarry Smith 
604b9ad928SBarry Smith   PetscFunctionBegin;
614482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
624482741eSBarry Smith   PetscValidCharPointer(type,2);
634b9ad928SBarry Smith 
644b9ad928SBarry Smith   ierr = PetscTypeCompare((PetscObject)pc,type,&match);CHKERRQ(ierr);
654b9ad928SBarry Smith   if (match) PetscFunctionReturn(0);
664b9ad928SBarry Smith 
67*a7d21a52SLisandro Dalcin   /* Get the function pointer for the method requested */
684b9ad928SBarry Smith   if (!PCRegisterAllCalled) {ierr = PCRegisterAll(0);CHKERRQ(ierr);}
694b9ad928SBarry Smith   ierr =  PetscFListFind(pc->comm,PCList,type,(void (**)(void)) &r);CHKERRQ(ierr);
70958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PC type %s",type);
71*a7d21a52SLisandro Dalcin   /* Destroy the previous private PC context */
72*a7d21a52SLisandro Dalcin   if (pc->ops->destroy) { ierr =  (*pc->ops->destroy)(pc);CHKERRQ(ierr); }
73*a7d21a52SLisandro Dalcin   ierr = PetscFListDestroy(&pc->qlist);CHKERRQ(ierr);
74*a7d21a52SLisandro Dalcin   /* Reinitialize function pointers in PCOps structure */
75*a7d21a52SLisandro Dalcin   ierr = PetscMemzero(pc->ops,sizeof(struct _PCOps));CHKERRQ(ierr);
76*a7d21a52SLisandro Dalcin   /* XXX Is this OK?? */
77*a7d21a52SLisandro Dalcin   pc->modifysubmatrices        = 0;
78*a7d21a52SLisandro Dalcin   pc->modifysubmatricesP       = 0;
79*a7d21a52SLisandro Dalcin   /* Call the PCCreate_XXX routine for this particular preconditioner */
80*a7d21a52SLisandro Dalcin   pc->setupcalled = 0;
814b9ad928SBarry Smith   ierr = (*r)(pc);CHKERRQ(ierr);
824b9ad928SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pc,type);CHKERRQ(ierr);
834b9ad928SBarry Smith   PetscFunctionReturn(0);
844b9ad928SBarry Smith }
854b9ad928SBarry Smith 
864b9ad928SBarry Smith #undef __FUNCT__
874b9ad928SBarry Smith #define __FUNCT__ "PCRegisterDestroy"
88f39d8e23SSatish Balay /*@
894b9ad928SBarry Smith    PCRegisterDestroy - Frees the list of preconditioners that were
904b9ad928SBarry Smith    registered by PCRegisterDynamic().
914b9ad928SBarry Smith 
924b9ad928SBarry Smith    Not Collective
934b9ad928SBarry Smith 
944b9ad928SBarry Smith    Level: advanced
954b9ad928SBarry Smith 
964b9ad928SBarry Smith .keywords: PC, register, destroy
974b9ad928SBarry Smith 
984b9ad928SBarry Smith .seealso: PCRegisterAll(), PCRegisterAll()
994b9ad928SBarry Smith 
1004b9ad928SBarry Smith @*/
101dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCRegisterDestroy(void)
1024b9ad928SBarry Smith {
103dfbe8321SBarry Smith   PetscErrorCode ierr;
1044b9ad928SBarry Smith 
1054b9ad928SBarry Smith   PetscFunctionBegin;
1064b9ad928SBarry Smith   if (PCList) {
1074b9ad928SBarry Smith     ierr = PetscFListDestroy(&PCList);CHKERRQ(ierr);
1084b9ad928SBarry Smith     PCList = 0;
1094b9ad928SBarry Smith   }
1104b9ad928SBarry Smith   PCRegisterAllCalled = PETSC_FALSE;
1114b9ad928SBarry Smith   PetscFunctionReturn(0);
1124b9ad928SBarry Smith }
1134b9ad928SBarry Smith 
1144b9ad928SBarry Smith #undef __FUNCT__
1154b9ad928SBarry Smith #define __FUNCT__ "PCGetType"
1164b9ad928SBarry Smith /*@C
1174b9ad928SBarry Smith    PCGetType - Gets the PC method type and name (as a string) from the PC
1184b9ad928SBarry Smith    context.
1194b9ad928SBarry Smith 
1204b9ad928SBarry Smith    Not Collective
1214b9ad928SBarry Smith 
1224b9ad928SBarry Smith    Input Parameter:
1234b9ad928SBarry Smith .  pc - the preconditioner context
1244b9ad928SBarry Smith 
1254b9ad928SBarry Smith    Output Parameter:
1264b9ad928SBarry Smith .  name - name of preconditioner
1274b9ad928SBarry Smith 
1284b9ad928SBarry Smith    Level: intermediate
1294b9ad928SBarry Smith 
1304b9ad928SBarry Smith .keywords: PC, get, method, name, type
1314b9ad928SBarry Smith 
1324b9ad928SBarry Smith .seealso: PCSetType()
1334b9ad928SBarry Smith 
1344b9ad928SBarry Smith @*/
135dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCGetType(PC pc,PCType *meth)
1364b9ad928SBarry Smith {
1374b9ad928SBarry Smith   PetscFunctionBegin;
1383cfa8680SLisandro Dalcin   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
1393cfa8680SLisandro Dalcin   PetscValidPointer(meth,2);
1404b9ad928SBarry Smith   *meth = (PCType) pc->type_name;
1414b9ad928SBarry Smith   PetscFunctionReturn(0);
1424b9ad928SBarry Smith }
1434b9ad928SBarry Smith 
144dfbe8321SBarry Smith EXTERN PetscErrorCode PCGetDefaultType_Private(PC,const char*[]);
145566e8bf2SBarry Smith 
1464b9ad928SBarry Smith #undef __FUNCT__
1474b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions"
1484b9ad928SBarry Smith /*@
1494b9ad928SBarry Smith    PCSetFromOptions - Sets PC options from the options database.
1504b9ad928SBarry Smith    This routine must be called before PCSetUp() if the user is to be
1514b9ad928SBarry Smith    allowed to set the preconditioner method.
1524b9ad928SBarry Smith 
1534b9ad928SBarry Smith    Collective on PC
1544b9ad928SBarry Smith 
1554b9ad928SBarry Smith    Input Parameter:
1564b9ad928SBarry Smith .  pc - the preconditioner context
1574b9ad928SBarry Smith 
1584b9ad928SBarry Smith    Level: developer
1594b9ad928SBarry Smith 
1604b9ad928SBarry Smith .keywords: PC, set, from, options, database
1614b9ad928SBarry Smith 
1624b9ad928SBarry Smith .seealso:
1634b9ad928SBarry Smith 
1644b9ad928SBarry Smith @*/
165dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCSetFromOptions(PC pc)
1664b9ad928SBarry Smith {
167dfbe8321SBarry Smith   PetscErrorCode ierr;
1682fc52814SBarry Smith   char           type[256];
1692fc52814SBarry Smith   const char     *def;
1704b9ad928SBarry Smith   PetscTruth     flg;
1714b9ad928SBarry Smith 
1724b9ad928SBarry Smith   PetscFunctionBegin;
1734482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
1744b9ad928SBarry Smith 
1754b9ad928SBarry Smith   if (!PCRegisterAllCalled) {ierr = PCRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1764b9ad928SBarry Smith   ierr = PetscOptionsBegin(pc->comm,pc->prefix,"Preconditioner (PC) Options","PC");CHKERRQ(ierr);
1774b9ad928SBarry Smith     if (!pc->type_name) {
178566e8bf2SBarry Smith       ierr = PCGetDefaultType_Private(pc,&def);CHKERRQ(ierr);
1794b9ad928SBarry Smith     } else {
1804b9ad928SBarry Smith       def = pc->type_name;
1814b9ad928SBarry Smith     }
1824b9ad928SBarry Smith 
1834b9ad928SBarry Smith     ierr = PetscOptionsList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);CHKERRQ(ierr);
1844b9ad928SBarry Smith     if (flg) {
1854b9ad928SBarry Smith       ierr = PCSetType(pc,type);CHKERRQ(ierr);
186566e8bf2SBarry Smith     } else if (!pc->type_name){
187566e8bf2SBarry Smith       ierr = PCSetType(pc,def);CHKERRQ(ierr);
1884b9ad928SBarry Smith     }
1894b9ad928SBarry Smith 
1904b9ad928SBarry Smith     if (pc->ops->setfromoptions) {
1914b9ad928SBarry Smith       ierr = (*pc->ops->setfromoptions)(pc);CHKERRQ(ierr);
1924b9ad928SBarry Smith     }
1934b9ad928SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1940c24e6a1SHong Zhang   pc->setfromoptionscalled++;
1954b9ad928SBarry Smith   PetscFunctionReturn(0);
1964b9ad928SBarry Smith }
197