xref: /petsc/src/ksp/pc/interface/pcset.c (revision 3ba1676111f5c958fe6c2729b46ca4d523958bb3)
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 
18c3339decSBarry Smith    Collective
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 @*/
54d71ae5a4SJacob Faibussowitsch PetscErrorCode PCSetType(PC pc, PCType type)
55d71ae5a4SJacob Faibussowitsch {
56ace3abfcSBarry Smith   PetscBool match;
575f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PC);
584b9ad928SBarry Smith 
594b9ad928SBarry Smith   PetscFunctionBegin;
600700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
614482741eSBarry Smith   PetscValidCharPointer(type, 2);
624b9ad928SBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, type, &match));
64*3ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
654b9ad928SBarry Smith 
669566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PCList, type, &r));
675f80ce2aSJacob Faibussowitsch   PetscCheck(r, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PC type %s", type);
68a7d21a52SLisandro Dalcin   /* Destroy the previous private PC context */
69dbbe0bcdSBarry Smith   PetscTryTypeMethod(pc, destroy);
700298fd71SBarry Smith   pc->ops->destroy = NULL;
710a545947SLisandro Dalcin   pc->data         = NULL;
72dbbe0bcdSBarry Smith 
739566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&((PetscObject)pc)->qlist));
74a7d21a52SLisandro Dalcin   /* Reinitialize function pointers in PCOps structure */
759566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(pc->ops, sizeof(struct _PCOps)));
76a7d21a52SLisandro Dalcin   /* XXX Is this OK?? */
770a545947SLisandro Dalcin   pc->modifysubmatrices  = NULL;
780a545947SLisandro Dalcin   pc->modifysubmatricesP = NULL;
79a7d21a52SLisandro Dalcin   /* Call the PCCreate_XXX routine for this particular preconditioner */
80a7d21a52SLisandro Dalcin   pc->setupcalled = 0;
812fa5cd67SKarl Rupp 
829566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, type));
839566063dSJacob Faibussowitsch   PetscCall((*r)(pc));
84*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
854b9ad928SBarry Smith }
864b9ad928SBarry Smith 
874b9ad928SBarry Smith /*@C
884b9ad928SBarry Smith    PCGetType - Gets the PC method type and name (as a string) from the PC
894b9ad928SBarry Smith    context.
904b9ad928SBarry Smith 
914b9ad928SBarry Smith    Not Collective
924b9ad928SBarry Smith 
934b9ad928SBarry Smith    Input Parameter:
944b9ad928SBarry Smith .  pc - the preconditioner context
954b9ad928SBarry Smith 
964b9ad928SBarry Smith    Output Parameter:
97c4e43342SLisandro Dalcin .  type - name of preconditioner method
984b9ad928SBarry Smith 
994b9ad928SBarry Smith    Level: intermediate
1004b9ad928SBarry Smith 
101db781477SPatrick Sanan .seealso: `PCSetType()`
1024b9ad928SBarry Smith 
1034b9ad928SBarry Smith @*/
104d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGetType(PC pc, PCType *type)
105d71ae5a4SJacob Faibussowitsch {
1064b9ad928SBarry Smith   PetscFunctionBegin;
1070700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
108c4e43342SLisandro Dalcin   PetscValidPointer(type, 2);
109c4e43342SLisandro Dalcin   *type = ((PetscObject)pc)->type_name;
110*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1114b9ad928SBarry Smith }
1124b9ad928SBarry Smith 
11309573ac7SBarry Smith extern PetscErrorCode PCGetDefaultType_Private(PC, const char *[]);
114566e8bf2SBarry Smith 
1154b9ad928SBarry Smith /*@
1164b9ad928SBarry Smith    PCSetFromOptions - Sets PC options from the options database.
1174b9ad928SBarry Smith    This routine must be called before PCSetUp() if the user is to be
1184b9ad928SBarry Smith    allowed to set the preconditioner method.
1194b9ad928SBarry Smith 
120c3339decSBarry Smith    Collective
1214b9ad928SBarry Smith 
1224b9ad928SBarry Smith    Input Parameter:
1234b9ad928SBarry Smith .  pc - the preconditioner context
1244b9ad928SBarry Smith 
125f1580f4eSBarry Smith    Options Database Key:
126147403d9SBarry Smith .   -pc_use_amat true,false - see PCSetUseAmat()
127b9ee023eSBarry Smith 
1284b9ad928SBarry Smith    Level: developer
1294b9ad928SBarry Smith 
130db781477SPatrick Sanan .seealso: `PCSetUseAmat()`
1314b9ad928SBarry Smith 
1324b9ad928SBarry Smith @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode PCSetFromOptions(PC pc)
134d71ae5a4SJacob Faibussowitsch {
1352fc52814SBarry Smith   char        type[256];
1362fc52814SBarry Smith   const char *def;
137ace3abfcSBarry Smith   PetscBool   flg;
1384b9ad928SBarry Smith 
1394b9ad928SBarry Smith   PetscFunctionBegin;
1400700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1414b9ad928SBarry Smith 
1429566063dSJacob Faibussowitsch   PetscCall(PCRegisterAll());
143d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)pc);
1447adad957SLisandro Dalcin   if (!((PetscObject)pc)->type_name) {
1459566063dSJacob Faibussowitsch     PetscCall(PCGetDefaultType_Private(pc, &def));
1464b9ad928SBarry Smith   } else {
1477adad957SLisandro Dalcin     def = ((PetscObject)pc)->type_name;
1484b9ad928SBarry Smith   }
1494b9ad928SBarry Smith 
1509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pc_type", "Preconditioner", "PCSetType", PCList, def, type, 256, &flg));
1514b9ad928SBarry Smith   if (flg) {
1529566063dSJacob Faibussowitsch     PetscCall(PCSetType(pc, type));
1537adad957SLisandro Dalcin   } else if (!((PetscObject)pc)->type_name) {
1549566063dSJacob Faibussowitsch     PetscCall(PCSetType(pc, def));
1554b9ad928SBarry Smith   }
1564b9ad928SBarry Smith 
1579566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &flg));
15825c41539SBarry Smith   if (flg) goto skipoptions;
15925c41539SBarry Smith 
1609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_use_amat", "use Amat (instead of Pmat) to define preconditioner in nested inner solves", "PCSetUseAmat", pc->useAmat, &pc->useAmat, NULL));
161b4813acdSShri Abhyankar 
162dbbe0bcdSBarry Smith   PetscTryTypeMethod(pc, setfromoptions, PetscOptionsObject);
1635d973c19SBarry Smith 
16425c41539SBarry Smith skipoptions:
1655d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
166dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pc, PetscOptionsObject));
167d0609cedSBarry Smith   PetscOptionsEnd();
1680c24e6a1SHong Zhang   pc->setfromoptionscalled++;
169*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1704b9ad928SBarry Smith }
1716c699258SBarry Smith 
1726c699258SBarry Smith /*@
1736c699258SBarry Smith    PCSetDM - Sets the DM that may be used by some preconditioners
1746c699258SBarry Smith 
175c3339decSBarry Smith    Logically Collective
1766c699258SBarry Smith 
1776c699258SBarry Smith    Input Parameters:
1786c699258SBarry Smith +  pc - the preconditioner context
1792a808120SBarry Smith -  dm - the dm, can be NULL
1806c699258SBarry Smith 
1816c699258SBarry Smith    Level: intermediate
1826c699258SBarry Smith 
183f1580f4eSBarry Smith    Developer Note:
18495452b02SPatrick Sanan     The routines KSP/SNES/TSSetDM() require the dm to be non-NULL, but this one can be NULL since all it does is
1852a808120SBarry Smith     replace the current DM
1866c699258SBarry Smith 
187db781477SPatrick Sanan .seealso: `PCGetDM()`, `KSPSetDM()`, `KSPGetDM()`
1886c699258SBarry Smith @*/
189d71ae5a4SJacob Faibussowitsch PetscErrorCode PCSetDM(PC pc, DM dm)
190d71ae5a4SJacob Faibussowitsch {
1916c699258SBarry Smith   PetscFunctionBegin;
1920700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1939566063dSJacob Faibussowitsch   if (dm) PetscCall(PetscObjectReference((PetscObject)dm));
1949566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&pc->dm));
1956c699258SBarry Smith   pc->dm = dm;
196*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1976c699258SBarry Smith }
1986c699258SBarry Smith 
1996c699258SBarry Smith /*@
2006c699258SBarry Smith    PCGetDM - Gets the DM that may be used by some preconditioners
2016c699258SBarry Smith 
2023f9fe445SBarry Smith    Not Collective
2036c699258SBarry Smith 
2046c699258SBarry Smith    Input Parameter:
2056c699258SBarry Smith . pc - the preconditioner context
2066c699258SBarry Smith 
2076c699258SBarry Smith    Output Parameter:
2086c699258SBarry Smith .  dm - the dm
2096c699258SBarry Smith 
2106c699258SBarry Smith    Level: intermediate
2116c699258SBarry Smith 
212db781477SPatrick Sanan .seealso: `PCSetDM()`, `KSPSetDM()`, `KSPGetDM()`
2136c699258SBarry Smith @*/
214d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGetDM(PC pc, DM *dm)
215d71ae5a4SJacob Faibussowitsch {
2166c699258SBarry Smith   PetscFunctionBegin;
2170700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2186c699258SBarry Smith   *dm = pc->dm;
219*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2206c699258SBarry Smith }
2216c699258SBarry Smith 
222b07ff414SBarry Smith /*@
2231b2093e4SBarry Smith    PCSetApplicationContext - Sets the optional user-defined context for the linear solver.
2241b2093e4SBarry Smith 
225c3339decSBarry Smith    Logically Collective
2261b2093e4SBarry Smith 
2271b2093e4SBarry Smith    Input Parameters:
2281b2093e4SBarry Smith +  pc - the PC context
2291b2093e4SBarry Smith -  usrP - optional user context
2301b2093e4SBarry Smith 
2311b2093e4SBarry Smith    Level: intermediate
2321b2093e4SBarry Smith 
233db781477SPatrick Sanan .seealso: `PCGetApplicationContext()`
2341b2093e4SBarry Smith @*/
235d71ae5a4SJacob Faibussowitsch PetscErrorCode PCSetApplicationContext(PC pc, void *usrP)
236d71ae5a4SJacob Faibussowitsch {
2371b2093e4SBarry Smith   PetscFunctionBegin;
2381b2093e4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2391b2093e4SBarry Smith   pc->user = usrP;
240*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2411b2093e4SBarry Smith }
2421b2093e4SBarry Smith 
243b07ff414SBarry Smith /*@
2441b2093e4SBarry Smith    PCGetApplicationContext - Gets the user-defined context for the linear solver.
2451b2093e4SBarry Smith 
2461b2093e4SBarry Smith    Not Collective
2471b2093e4SBarry Smith 
2481b2093e4SBarry Smith    Input Parameter:
2491b2093e4SBarry Smith .  pc - PC context
2501b2093e4SBarry Smith 
2511b2093e4SBarry Smith    Output Parameter:
2521b2093e4SBarry Smith .  usrP - user context
2531b2093e4SBarry Smith 
2541b2093e4SBarry Smith    Level: intermediate
2551b2093e4SBarry Smith 
256db781477SPatrick Sanan .seealso: `PCSetApplicationContext()`
2571b2093e4SBarry Smith @*/
258d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGetApplicationContext(PC pc, void *usrP)
259d71ae5a4SJacob Faibussowitsch {
2601b2093e4SBarry Smith   PetscFunctionBegin;
2611b2093e4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2621b2093e4SBarry Smith   *(void **)usrP = pc->user;
263*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2641b2093e4SBarry Smith }
265