xref: /petsc/src/ksp/pc/interface/pcset.c (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
17d0a6c19SBarry Smith 
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith     Routines to set PC methods and options.
44b9ad928SBarry Smith */
54b9ad928SBarry Smith 
6af0996ceSBarry Smith #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
71e25c274SJed Brown #include <petscdm.h>
84b9ad928SBarry Smith 
9ace3abfcSBarry Smith PetscBool         PCRegisterAllCalled = PETSC_FALSE;
104b9ad928SBarry Smith /*
11f3b08a26SMatthew G. Knepley    Contains the list of registered PC routines
124b9ad928SBarry Smith */
130a545947SLisandro Dalcin PetscFunctionList PCList              = NULL;
144b9ad928SBarry Smith 
154b9ad928SBarry Smith /*@C
168f6c3df8SBarry Smith    PCSetType - Builds PC for a particular preconditioner type
174b9ad928SBarry Smith 
18*f1580f4eSBarry Smith    Collective on pc
194b9ad928SBarry Smith 
20d8d19677SJose E. Roman    Input Parameters:
214b9ad928SBarry Smith +  pc - the preconditioner context.
224b9ad928SBarry Smith -  type - a known method
234b9ad928SBarry Smith 
244b9ad928SBarry Smith    Options Database Key:
254b9ad928SBarry Smith .  -pc_type <type> - Sets PC type
264b9ad928SBarry Smith 
274b9ad928SBarry Smith    Use -help for a list of available methods (for instance,
284b9ad928SBarry Smith    jacobi or bjacobi)
294b9ad928SBarry Smith 
304b9ad928SBarry Smith   Notes:
314b9ad928SBarry Smith   See "petsc/include/petscpc.h" for available methods (for instance,
324b9ad928SBarry Smith   PCJACOBI, PCILU, or PCBJACOBI).
334b9ad928SBarry Smith 
344b9ad928SBarry Smith   Normally, it is best to use the KSPSetFromOptions() command and
354b9ad928SBarry Smith   then set the PC type from the options database rather than by using
364b9ad928SBarry Smith   this routine.  Using the options database provides the user with
374b9ad928SBarry Smith   maximum flexibility in evaluating the many different preconditioners.
384b9ad928SBarry Smith   The PCSetType() routine is provided for those situations where it
394b9ad928SBarry Smith   is necessary to set the preconditioner independently of the command
404b9ad928SBarry Smith   line or options database.  This might be the case, for example, when
414b9ad928SBarry Smith   the choice of preconditioner changes during the execution of the
424b9ad928SBarry Smith   program, and the user's application is taking responsibility for
434b9ad928SBarry Smith   choosing the appropriate preconditioner.  In other words, this
444b9ad928SBarry Smith   routine is not for beginners.
454b9ad928SBarry Smith 
464b9ad928SBarry Smith   Level: intermediate
474b9ad928SBarry Smith 
488f6c3df8SBarry Smith   Developer Note: PCRegister() is used to add preconditioner types to PCList from which they
498f6c3df8SBarry Smith   are accessed by PCSetType().
508f6c3df8SBarry Smith 
51db781477SPatrick Sanan .seealso: `KSPSetType()`, `PCType`, `PCRegister()`, `PCCreate()`, `KSPGetPC()`
524b9ad928SBarry Smith 
534b9ad928SBarry Smith @*/
549371c9d4SSatish Balay PetscErrorCode PCSetType(PC pc, PCType type) {
55ace3abfcSBarry Smith   PetscBool match;
565f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PC);
574b9ad928SBarry Smith 
584b9ad928SBarry Smith   PetscFunctionBegin;
590700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
604482741eSBarry Smith   PetscValidCharPointer(type, 2);
614b9ad928SBarry Smith 
629566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, type, &match));
634b9ad928SBarry Smith   if (match) PetscFunctionReturn(0);
644b9ad928SBarry Smith 
659566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PCList, type, &r));
665f80ce2aSJacob Faibussowitsch   PetscCheck(r, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PC type %s", type);
67a7d21a52SLisandro Dalcin   /* Destroy the previous private PC context */
68dbbe0bcdSBarry Smith   PetscTryTypeMethod(pc, destroy);
690298fd71SBarry Smith   pc->ops->destroy = NULL;
700a545947SLisandro Dalcin   pc->data         = NULL;
71dbbe0bcdSBarry Smith 
729566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&((PetscObject)pc)->qlist));
73a7d21a52SLisandro Dalcin   /* Reinitialize function pointers in PCOps structure */
749566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(pc->ops, sizeof(struct _PCOps)));
75a7d21a52SLisandro Dalcin   /* XXX Is this OK?? */
760a545947SLisandro Dalcin   pc->modifysubmatrices  = NULL;
770a545947SLisandro Dalcin   pc->modifysubmatricesP = NULL;
78a7d21a52SLisandro Dalcin   /* Call the PCCreate_XXX routine for this particular preconditioner */
79a7d21a52SLisandro Dalcin   pc->setupcalled        = 0;
802fa5cd67SKarl Rupp 
819566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, type));
829566063dSJacob Faibussowitsch   PetscCall((*r)(pc));
834b9ad928SBarry Smith   PetscFunctionReturn(0);
844b9ad928SBarry Smith }
854b9ad928SBarry Smith 
864b9ad928SBarry Smith /*@C
874b9ad928SBarry Smith    PCGetType - Gets the PC method type and name (as a string) from the PC
884b9ad928SBarry Smith    context.
894b9ad928SBarry Smith 
904b9ad928SBarry Smith    Not Collective
914b9ad928SBarry Smith 
924b9ad928SBarry Smith    Input Parameter:
934b9ad928SBarry Smith .  pc - the preconditioner context
944b9ad928SBarry Smith 
954b9ad928SBarry Smith    Output Parameter:
96c4e43342SLisandro Dalcin .  type - name of preconditioner method
974b9ad928SBarry Smith 
984b9ad928SBarry Smith    Level: intermediate
994b9ad928SBarry Smith 
100db781477SPatrick Sanan .seealso: `PCSetType()`
1014b9ad928SBarry Smith 
1024b9ad928SBarry Smith @*/
1039371c9d4SSatish Balay PetscErrorCode PCGetType(PC pc, PCType *type) {
1044b9ad928SBarry Smith   PetscFunctionBegin;
1050700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
106c4e43342SLisandro Dalcin   PetscValidPointer(type, 2);
107c4e43342SLisandro Dalcin   *type = ((PetscObject)pc)->type_name;
1084b9ad928SBarry Smith   PetscFunctionReturn(0);
1094b9ad928SBarry Smith }
1104b9ad928SBarry Smith 
11109573ac7SBarry Smith extern PetscErrorCode PCGetDefaultType_Private(PC, const char *[]);
112566e8bf2SBarry Smith 
1134b9ad928SBarry Smith /*@
1144b9ad928SBarry Smith    PCSetFromOptions - Sets PC options from the options database.
1154b9ad928SBarry Smith    This routine must be called before PCSetUp() if the user is to be
1164b9ad928SBarry Smith    allowed to set the preconditioner method.
1174b9ad928SBarry Smith 
118*f1580f4eSBarry Smith    Collective on pc
1194b9ad928SBarry Smith 
1204b9ad928SBarry Smith    Input Parameter:
1214b9ad928SBarry Smith .  pc - the preconditioner context
1224b9ad928SBarry Smith 
123*f1580f4eSBarry Smith    Options Database Key:
124147403d9SBarry Smith .   -pc_use_amat true,false - see PCSetUseAmat()
125b9ee023eSBarry Smith 
1264b9ad928SBarry Smith    Level: developer
1274b9ad928SBarry Smith 
128db781477SPatrick Sanan .seealso: `PCSetUseAmat()`
1294b9ad928SBarry Smith 
1304b9ad928SBarry Smith @*/
1319371c9d4SSatish Balay PetscErrorCode PCSetFromOptions(PC pc) {
1322fc52814SBarry Smith   char        type[256];
1332fc52814SBarry Smith   const char *def;
134ace3abfcSBarry Smith   PetscBool   flg;
1354b9ad928SBarry Smith 
1364b9ad928SBarry Smith   PetscFunctionBegin;
1370700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1384b9ad928SBarry Smith 
1399566063dSJacob Faibussowitsch   PetscCall(PCRegisterAll());
140d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)pc);
1417adad957SLisandro Dalcin   if (!((PetscObject)pc)->type_name) {
1429566063dSJacob Faibussowitsch     PetscCall(PCGetDefaultType_Private(pc, &def));
1434b9ad928SBarry Smith   } else {
1447adad957SLisandro Dalcin     def = ((PetscObject)pc)->type_name;
1454b9ad928SBarry Smith   }
1464b9ad928SBarry Smith 
1479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pc_type", "Preconditioner", "PCSetType", PCList, def, type, 256, &flg));
1484b9ad928SBarry Smith   if (flg) {
1499566063dSJacob Faibussowitsch     PetscCall(PCSetType(pc, type));
1507adad957SLisandro Dalcin   } else if (!((PetscObject)pc)->type_name) {
1519566063dSJacob Faibussowitsch     PetscCall(PCSetType(pc, def));
1524b9ad928SBarry Smith   }
1534b9ad928SBarry Smith 
1549566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &flg));
15525c41539SBarry Smith   if (flg) goto skipoptions;
15625c41539SBarry Smith 
1579566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_use_amat", "use Amat (instead of Pmat) to define preconditioner in nested inner solves", "PCSetUseAmat", pc->useAmat, &pc->useAmat, NULL));
158b4813acdSShri Abhyankar 
159dbbe0bcdSBarry Smith   PetscTryTypeMethod(pc, setfromoptions, PetscOptionsObject);
1605d973c19SBarry Smith 
16125c41539SBarry Smith skipoptions:
1625d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
163dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pc, PetscOptionsObject));
164d0609cedSBarry Smith   PetscOptionsEnd();
1650c24e6a1SHong Zhang   pc->setfromoptionscalled++;
1664b9ad928SBarry Smith   PetscFunctionReturn(0);
1674b9ad928SBarry Smith }
1686c699258SBarry Smith 
1696c699258SBarry Smith /*@
1706c699258SBarry Smith    PCSetDM - Sets the DM that may be used by some preconditioners
1716c699258SBarry Smith 
172*f1580f4eSBarry Smith    Logically Collective on pc
1736c699258SBarry Smith 
1746c699258SBarry Smith    Input Parameters:
1756c699258SBarry Smith +  pc - the preconditioner context
1762a808120SBarry Smith -  dm - the dm, can be NULL
1776c699258SBarry Smith 
1786c699258SBarry Smith    Level: intermediate
1796c699258SBarry Smith 
180*f1580f4eSBarry Smith    Developer Note:
18195452b02SPatrick Sanan     The routines KSP/SNES/TSSetDM() require the dm to be non-NULL, but this one can be NULL since all it does is
1822a808120SBarry Smith     replace the current DM
1836c699258SBarry Smith 
184db781477SPatrick Sanan .seealso: `PCGetDM()`, `KSPSetDM()`, `KSPGetDM()`
1856c699258SBarry Smith @*/
1869371c9d4SSatish Balay PetscErrorCode PCSetDM(PC pc, DM dm) {
1876c699258SBarry Smith   PetscFunctionBegin;
1880700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1899566063dSJacob Faibussowitsch   if (dm) PetscCall(PetscObjectReference((PetscObject)dm));
1909566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&pc->dm));
1916c699258SBarry Smith   pc->dm = dm;
1926c699258SBarry Smith   PetscFunctionReturn(0);
1936c699258SBarry Smith }
1946c699258SBarry Smith 
1956c699258SBarry Smith /*@
1966c699258SBarry Smith    PCGetDM - Gets the DM that may be used by some preconditioners
1976c699258SBarry Smith 
1983f9fe445SBarry Smith    Not Collective
1996c699258SBarry Smith 
2006c699258SBarry Smith    Input Parameter:
2016c699258SBarry Smith . pc - the preconditioner context
2026c699258SBarry Smith 
2036c699258SBarry Smith    Output Parameter:
2046c699258SBarry Smith .  dm - the dm
2056c699258SBarry Smith 
2066c699258SBarry Smith    Level: intermediate
2076c699258SBarry Smith 
208db781477SPatrick Sanan .seealso: `PCSetDM()`, `KSPSetDM()`, `KSPGetDM()`
2096c699258SBarry Smith @*/
2109371c9d4SSatish Balay PetscErrorCode PCGetDM(PC pc, DM *dm) {
2116c699258SBarry Smith   PetscFunctionBegin;
2120700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2136c699258SBarry Smith   *dm = pc->dm;
2146c699258SBarry Smith   PetscFunctionReturn(0);
2156c699258SBarry Smith }
2166c699258SBarry Smith 
217b07ff414SBarry Smith /*@
2181b2093e4SBarry Smith    PCSetApplicationContext - Sets the optional user-defined context for the linear solver.
2191b2093e4SBarry Smith 
220*f1580f4eSBarry Smith    Logically Collective on pc
2211b2093e4SBarry Smith 
2221b2093e4SBarry Smith    Input Parameters:
2231b2093e4SBarry Smith +  pc - the PC context
2241b2093e4SBarry Smith -  usrP - optional user context
2251b2093e4SBarry Smith 
2261b2093e4SBarry Smith    Level: intermediate
2271b2093e4SBarry Smith 
228db781477SPatrick Sanan .seealso: `PCGetApplicationContext()`
2291b2093e4SBarry Smith @*/
2309371c9d4SSatish Balay PetscErrorCode PCSetApplicationContext(PC pc, void *usrP) {
2311b2093e4SBarry Smith   PetscFunctionBegin;
2321b2093e4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2331b2093e4SBarry Smith   pc->user = usrP;
2341b2093e4SBarry Smith   PetscFunctionReturn(0);
2351b2093e4SBarry Smith }
2361b2093e4SBarry Smith 
237b07ff414SBarry Smith /*@
2381b2093e4SBarry Smith    PCGetApplicationContext - Gets the user-defined context for the linear solver.
2391b2093e4SBarry Smith 
2401b2093e4SBarry Smith    Not Collective
2411b2093e4SBarry Smith 
2421b2093e4SBarry Smith    Input Parameter:
2431b2093e4SBarry Smith .  pc - PC context
2441b2093e4SBarry Smith 
2451b2093e4SBarry Smith    Output Parameter:
2461b2093e4SBarry Smith .  usrP - user context
2471b2093e4SBarry Smith 
2481b2093e4SBarry Smith    Level: intermediate
2491b2093e4SBarry Smith 
250db781477SPatrick Sanan .seealso: `PCSetApplicationContext()`
2511b2093e4SBarry Smith @*/
2529371c9d4SSatish Balay PetscErrorCode PCGetApplicationContext(PC pc, void *usrP) {
2531b2093e4SBarry Smith   PetscFunctionBegin;
2541b2093e4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2551b2093e4SBarry Smith   *(void **)usrP = pc->user;
2561b2093e4SBarry Smith   PetscFunctionReturn(0);
2571b2093e4SBarry Smith }
258