xref: /petsc/src/sys/objects/prefix.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay      Provides utility routines for manulating any type of PETSc object.
4e5c89e4eSSatish Balay */
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I   "petscsys.h"    I*/
6e5c89e4eSSatish Balay 
7c5929fdfSBarry Smith /*@C
8*811af0c4SBarry Smith    PetscObjectGetOptions - Gets the options database used by the object that has been set with `PetscObjectSetOptions()`
9ffc43655SBarry Smith 
10*811af0c4SBarry Smith    Collective on obj
11ffc43655SBarry Smith 
12ffc43655SBarry Smith    Input Parameter:
13*811af0c4SBarry Smith .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
14ffc43655SBarry Smith 
15ffc43655SBarry Smith    Output Parameter:
16ffc43655SBarry Smith .  options - the options database
17ffc43655SBarry Smith 
18*811af0c4SBarry Smith    Note:
19*811af0c4SBarry Smith    If this is not called the object will use the default options database
20*811af0c4SBarry Smith 
21*811af0c4SBarry Smith    Developer Note:
22*811af0c4SBarry Smith    This functionality is not used in PETSc and should, perhaps, be removed
23ffc43655SBarry Smith 
24ffc43655SBarry Smith   Level: advanced
25ffc43655SBarry Smith 
26db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
27db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `PetscObjectSetOptions()`
28ffc43655SBarry Smith @*/
299371c9d4SSatish Balay PetscErrorCode PetscObjectGetOptions(PetscObject obj, PetscOptions *options) {
30ffc43655SBarry Smith   PetscFunctionBegin;
31ffc43655SBarry Smith   PetscValidHeader(obj, 1);
32ffc43655SBarry Smith   *options = obj->options;
33ffc43655SBarry Smith   PetscFunctionReturn(0);
34ffc43655SBarry Smith }
35ffc43655SBarry Smith 
36ffc43655SBarry Smith /*@C
3716413a6aSBarry Smith    PetscObjectSetOptions - Sets the options database used by the object. Call immediately after creating the object.
38c5929fdfSBarry Smith 
39*811af0c4SBarry Smith    Collective on obj
4096a0c994SBarry Smith 
41c5929fdfSBarry Smith    Input Parameters:
42*811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
43c5929fdfSBarry Smith -  options - the options database, use NULL for default
44c5929fdfSBarry Smith 
45*811af0c4SBarry Smith    Note:
46*811af0c4SBarry Smith    If this is not called the object will use the default options database
47*811af0c4SBarry Smith 
48*811af0c4SBarry Smith    Developer Note:
49*811af0c4SBarry Smith    This functionality is not used in PETSc and should, perhaps, be removed
50c5929fdfSBarry Smith 
5196a0c994SBarry Smith   Level: advanced
5296a0c994SBarry Smith 
53db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
54db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `PetscObjectGetOptions()`
5596a0c994SBarry Smith @*/
569371c9d4SSatish Balay PetscErrorCode PetscObjectSetOptions(PetscObject obj, PetscOptions options) {
57c5929fdfSBarry Smith   PetscFunctionBegin;
58c5929fdfSBarry Smith   PetscValidHeader(obj, 1);
59c5929fdfSBarry Smith   obj->options = options;
60c5929fdfSBarry Smith   PetscFunctionReturn(0);
61c5929fdfSBarry Smith }
62c5929fdfSBarry Smith 
635cec412bSBarry Smith /*@C
64e5c89e4eSSatish Balay    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
65*811af0c4SBarry Smith    options for the given object in the database.
66e5c89e4eSSatish Balay 
67*811af0c4SBarry Smith    Collective on obj
6896a0c994SBarry Smith 
69e5c89e4eSSatish Balay    Input Parameters:
70*811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
71a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
72e5c89e4eSSatish Balay 
73*811af0c4SBarry Smith    Note:
74e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
75e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
76e5c89e4eSSatish Balay    hyphen.
77e5c89e4eSSatish Balay 
78edc382c3SSatish Balay   Level: advanced
79edc382c3SSatish Balay 
80db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
81db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `SNESSetOptionsPrefix()`, `KSPSetOptionsPrefix()`
825cec412bSBarry Smith @*/
839371c9d4SSatish Balay PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject obj, const char prefix[]) {
84e5c89e4eSSatish Balay   PetscFunctionBegin;
853cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
865f80ce2aSJacob Faibussowitsch   if (prefix) {
875f80ce2aSJacob Faibussowitsch     PetscValidCharPointer(prefix, 2);
885f80ce2aSJacob Faibussowitsch     PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
894ed52fbaSBarry Smith     if (prefix != obj->prefix) {
909566063dSJacob Faibussowitsch       PetscCall(PetscFree(obj->prefix));
919566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(prefix, &obj->prefix));
92e5c89e4eSSatish Balay     }
939566063dSJacob Faibussowitsch   } else PetscCall(PetscFree(obj->prefix));
94e5c89e4eSSatish Balay   PetscFunctionReturn(0);
95e5c89e4eSSatish Balay }
96e5c89e4eSSatish Balay 
975cec412bSBarry Smith /*@C
98*811af0c4SBarry Smith    PetscObjectAppendOptionsPrefix - Appends to the prefix used for searching for options for the given object in the database.
99e5c89e4eSSatish Balay 
100e5c89e4eSSatish Balay    Input Parameters:
101*811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
102a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
103e5c89e4eSSatish Balay 
104*811af0c4SBarry Smith    Note:
105e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
106e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
107e5c89e4eSSatish Balay    hyphen.
108e5c89e4eSSatish Balay 
109edc382c3SSatish Balay   Level: advanced
110edc382c3SSatish Balay 
111db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
112db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSAppendOptionsPrefix()`, `SNESAppendOptionsPrefix()`, `KSPAppendOptionsPrefix()`
1135cec412bSBarry Smith @*/
1149371c9d4SSatish Balay PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject obj, const char prefix[]) {
115e5c89e4eSSatish Balay   char  *buf = obj->prefix;
116e5c89e4eSSatish Balay   size_t len1, len2;
117e5c89e4eSSatish Balay 
118e5c89e4eSSatish Balay   PetscFunctionBegin;
1193cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
120a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
121e5c89e4eSSatish Balay   if (!buf) {
1229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
123e5c89e4eSSatish Balay     PetscFunctionReturn(0);
124e5c89e4eSSatish Balay   }
125cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
126e5c89e4eSSatish Balay 
1279566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
1289566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(buf, &len2));
1299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1 + len1 + len2, &obj->prefix));
1309566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(obj->prefix, buf));
1319566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(obj->prefix, prefix));
1329566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
133e5c89e4eSSatish Balay   PetscFunctionReturn(0);
134e5c89e4eSSatish Balay }
135e5c89e4eSSatish Balay 
1365cec412bSBarry Smith /*@C
137*811af0c4SBarry Smith    PetscObjectGetOptionsPrefix - Gets the prefix of the `PetscObject` used for searching in the options database
138e5c89e4eSSatish Balay 
139e5c89e4eSSatish Balay    Input Parameters:
140*811af0c4SBarry Smith .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
141e5c89e4eSSatish Balay 
142e5c89e4eSSatish Balay    Output Parameters:
143e5c89e4eSSatish Balay .  prefix - pointer to the prefix string used is returned
144e5c89e4eSSatish Balay 
145edc382c3SSatish Balay   Level: advanced
146edc382c3SSatish Balay 
147db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
148db781477SPatrick Sanan           `TSGetOptionsPrefix()`, `SNESGetOptionsPrefix()`, `KSPGetOptionsPrefix()`
1495cec412bSBarry Smith @*/
1509371c9d4SSatish Balay PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject obj, const char *prefix[]) {
151e5c89e4eSSatish Balay   PetscFunctionBegin;
1523cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1533cfa8680SLisandro Dalcin   PetscValidPointer(prefix, 2);
154e5c89e4eSSatish Balay   *prefix = obj->prefix;
155e5c89e4eSSatish Balay   PetscFunctionReturn(0);
156e5c89e4eSSatish Balay }
157e5c89e4eSSatish Balay 
1585cec412bSBarry Smith /*@C
159*811af0c4SBarry Smith    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for options of for this object in the database.
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay    Input Parameters:
162*811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
163a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
164e5c89e4eSSatish Balay 
165*811af0c4SBarry Smith    Note:
166e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
167e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
168e5c89e4eSSatish Balay    hyphen.
169e5c89e4eSSatish Balay 
170edc382c3SSatish Balay   Level: advanced
171edc382c3SSatish Balay 
172db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`,
173db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`
1745cec412bSBarry Smith @*/
1759371c9d4SSatish Balay PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject obj, const char prefix[]) {
1765c7534e4SLisandro Dalcin   char  *buf;
177e5c89e4eSSatish Balay   size_t len1, len2;
178e5c89e4eSSatish Balay 
179e5c89e4eSSatish Balay   PetscFunctionBegin;
1803cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1815c7534e4SLisandro Dalcin   buf = obj->prefix;
182a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
183e5c89e4eSSatish Balay   if (!buf) {
1849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
185e5c89e4eSSatish Balay     PetscFunctionReturn(0);
186e5c89e4eSSatish Balay   }
187cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
188e5c89e4eSSatish Balay 
1899566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
1909566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(buf, &len2));
1919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1 + len1 + len2, &obj->prefix));
1929566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(obj->prefix, prefix));
1939566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(obj->prefix, buf));
1949566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
195e5c89e4eSSatish Balay   PetscFunctionReturn(0);
196e5c89e4eSSatish Balay }
197