Actual source code: pcset.c
1: /*
2: Routines to set PC methods and options.
3: */
5: #include <petsc/private/pcimpl.h>
6: #include <petscdm.h>
8: PetscBool PCRegisterAllCalled = PETSC_FALSE;
9: /*
10: Contains the list of registered PC routines
11: */
12: PetscFunctionList PCList = NULL;
14: /*@
15: PCSetType - Builds `PC` for a particular preconditioner type
17: Collective
19: Input Parameters:
20: + pc - the preconditioner context
21: - type - a known method, see `PCType` for possible values
23: Options Database Key:
24: . -pc_type type - Sets `PC` type
26: Notes:
27: Normally, it is best to use the `KSPSetFromOptions()` command and
28: then set the `PC` type from the options database rather than by using
29: this routine. Using the options database provides the user with
30: maximum flexibility in evaluating the many different preconditioners.
31: The `PCSetType()` routine is provided for those situations where it
32: is necessary to set the preconditioner independently of the command
33: line or options database. This might be the case, for example, when
34: the choice of preconditioner changes during the execution of the
35: program, and the user's application is taking responsibility for
36: choosing the appropriate preconditioner.
38: Level: intermediate
40: Developer Notes:
41: `PCRegister()` is used to add preconditioner types to `PCList` from which they
42: are accessed by `PCSetType()`.
44: .seealso: [](ch_ksp), `KSPSetType()`, `PCType`, `PCRegister()`, `PCCreate()`, `KSPGetPC()`
45: @*/
46: PetscErrorCode PCSetType(PC pc, PCType type)
47: {
48: PetscBool match;
49: PetscErrorCode (*r)(PC);
51: PetscFunctionBegin;
53: PetscAssertPointer(type, 2);
55: PetscCall(PetscObjectTypeCompare((PetscObject)pc, type, &match));
56: if (match) PetscFunctionReturn(PETSC_SUCCESS);
58: PetscCall(PetscFunctionListFind(PCList, type, &r));
59: PetscCheck(r, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PC type %s", type);
60: /* Destroy the previous private PC context */
61: PetscTryTypeMethod(pc, destroy);
62: PetscCall(PCMatWorkReset(pc));
63: /* Reinitialize default parameters */
64: PetscCall(PCParametersInitialize(pc));
66: PetscCall(PetscFunctionListDestroy(&((PetscObject)pc)->qlist));
67: /* Reinitialize function pointers in PCOps structure */
68: PetscCall(PetscMemzero(pc->ops, sizeof(struct _PCOps)));
69: /* XXX Is this OK?? */
70: pc->modifysubmatrices = NULL;
71: pc->modifysubmatricesP = NULL;
72: /* Call the PCCreate_XXX routine for this particular preconditioner */
73: pc->setupcalled = PETSC_FALSE;
75: PetscCall(PetscObjectChangeTypeName((PetscObject)pc, type));
76: PetscCall((*r)(pc));
77: PetscFunctionReturn(PETSC_SUCCESS);
78: }
80: /*@
81: PCGetType - Gets the `PCType` (as a string) from the `PC`
82: context.
84: Not Collective
86: Input Parameter:
87: . pc - the preconditioner context
89: Output Parameter:
90: . type - name of preconditioner method
92: Level: intermediate
94: Note:
95: `type` should not be retained for later use as it will be an invalid pointer if the `PCType` of `pc` is changed.
97: .seealso: [](ch_ksp), `PC`, `PCType`, `PCSetType()`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`
98: @*/
99: PetscErrorCode PCGetType(PC pc, PCType *type)
100: {
101: PetscFunctionBegin;
103: PetscAssertPointer(type, 2);
104: *type = ((PetscObject)pc)->type_name;
105: PetscFunctionReturn(PETSC_SUCCESS);
106: }
108: PETSC_INTERN PetscErrorCode PCGetDefaultType_Private(PC, const char *[]);
110: /*@
111: PCSetFromOptions - Sets `PC` options from the options database.
113: Collective
115: Input Parameter:
116: . pc - the preconditioner context
118: Options Database Key:
119: . -pc_type - name of type, for example `bjacobi`
121: Level: advanced
123: Notes:
124: This routine must be called before `PCSetUp()` if the user is to be
125: allowed to set the preconditioner method from the options database.
127: This is called from `KSPSetFromOptions()` so rarely needs to be called directly
129: .seealso: [](ch_ksp), `PC`, `PCSetType()`, `PCType`, `KSPSetFromOptions()`
130: @*/
131: PetscErrorCode PCSetFromOptions(PC pc)
132: {
133: char type[256];
134: const char *def;
135: PetscBool flg;
137: PetscFunctionBegin;
140: PetscCall(PCRegisterAll());
141: if (!((PetscObject)pc)->type_name) {
142: PetscCall(PCGetDefaultType_Private(pc, &def));
143: } else {
144: def = ((PetscObject)pc)->type_name;
145: }
146: PetscObjectOptionsBegin((PetscObject)pc);
148: PetscCall(PetscOptionsFList("-pc_type", "Preconditioner", "PCSetType", PCList, def, type, sizeof(type), &flg));
149: if (flg) PetscCall(PCSetType(pc, type));
150: else if (!((PetscObject)pc)->type_name && def) PetscCall(PCSetType(pc, def));
152: PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &flg));
153: if (flg) goto skipoptions;
155: PetscCall(PetscOptionsBool("-pc_use_amat", "use Amat (instead of Pmat) to define preconditioner in nested inner solves", "PCSetUseAmat", pc->useAmat, &pc->useAmat, NULL));
157: PetscTryTypeMethod(pc, setfromoptions, PetscOptionsObject);
159: skipoptions:
160: /* process any options handlers added with PetscObjectAddOptionsHandler() */
161: PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pc, PetscOptionsObject));
162: PetscOptionsEnd();
163: pc->setfromoptionscalled++;
164: PetscFunctionReturn(PETSC_SUCCESS);
165: }
167: /*@
168: PCSetDM - Sets the `DM` that may be used by some preconditioners
170: Logically Collective
172: Input Parameters:
173: + pc - the preconditioner context
174: - dm - the `DM`, can be `NULL` to remove any current `DM`
176: Level: intermediate
178: Note:
179: Users generally call `KSPSetDM()`, `SNESSetDM()`, or `TSSetDM()` so this is rarely called directly
181: Developer Notes:
182: The routines KSP/SNES/TSSetDM() require `dm` to be non-`NULL`, but this one can be `NULL` since all it does is
183: replace the current `DM`
185: .seealso: [](ch_ksp), `PC`, `DM`, `PCGetDM()`, `KSPSetDM()`, `KSPGetDM()`, `SNESSetDM()`, `TSSetDM()`
186: @*/
187: PetscErrorCode PCSetDM(PC pc, DM dm)
188: {
189: PetscFunctionBegin;
191: PetscCall(PetscObjectReference((PetscObject)dm));
192: PetscCall(DMDestroy(&pc->dm));
193: pc->dm = dm;
194: PetscFunctionReturn(PETSC_SUCCESS);
195: }
197: /*@
198: PCGetDM - Gets the `DM` that may be used by some preconditioners
200: Not Collective
202: Input Parameter:
203: . pc - the preconditioner context
205: Output Parameter:
206: . dm - the `DM`
208: Level: intermediate
210: .seealso: [](ch_ksp), `PC`, `DM`, `PCSetDM()`, `KSPSetDM()`, `KSPGetDM()`
211: @*/
212: PetscErrorCode PCGetDM(PC pc, DM *dm)
213: {
214: PetscFunctionBegin;
216: *dm = pc->dm;
217: PetscFunctionReturn(PETSC_SUCCESS);
218: }
220: /*@
221: PCSetApplicationContext - Sets the optional user-defined context for the preconditioner
223: Logically Collective
225: Input Parameters:
226: + pc - the `PC` context
227: - ctx - optional application context
229: Level: advanced
231: Fortran Note:
232: This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this
233: function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `PCGetApplicationContext()` for
234: an example.
236: .seealso: [](ch_ksp), `PC`, `PCGetApplicationContext()`, `KSPSetApplicationContext()`, `KSPGetApplicationContext()`, `PetscObjectCompose()`
237: @*/
238: PetscErrorCode PCSetApplicationContext(PC pc, PetscCtx ctx)
239: {
240: PetscFunctionBegin;
242: pc->ctx = ctx;
243: PetscFunctionReturn(PETSC_SUCCESS);
244: }
246: /*@
247: PCGetApplicationContext - Gets the user-defined context for the preconditioner set with `PCSetApplicationContext()`
249: Not Collective
251: Input Parameter:
252: . pc - `PC` context
254: Output Parameter:
255: . ctx - application context
257: Level: intermediate
259: Fortran Note:
260: This only works when the context is a Fortran derived type or a `PetscObject`. Define `ctx` with
261: .vb
262: type(tUsertype), pointer :: ctx
263: .ve
265: .seealso: [](ch_ksp), `PC`, `PCSetApplicationContext()`, `KSPSetApplicationContext()`, `KSPGetApplicationContext()`
266: @*/
267: PetscErrorCode PCGetApplicationContext(PC pc, PetscCtxRt ctx)
268: {
269: PetscFunctionBegin;
271: *(void **)ctx = pc->ctx;
272: PetscFunctionReturn(PETSC_SUCCESS);
273: }