xref: /petsc/src/sys/objects/prefix.c (revision c3339decea92175325d9368fa13196bcd0e0e58b)
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
8811af0c4SBarry Smith    PetscObjectGetOptions - Gets the options database used by the object that has been set with `PetscObjectSetOptions()`
9ffc43655SBarry Smith 
10*c3339decSBarry Smith    Collective
11ffc43655SBarry Smith 
12ffc43655SBarry Smith    Input Parameter:
13811af0c4SBarry 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 
18811af0c4SBarry Smith    Note:
19811af0c4SBarry Smith    If this is not called the object will use the default options database
20811af0c4SBarry Smith 
21811af0c4SBarry Smith    Developer Note:
22811af0c4SBarry 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 @*/
29d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptions(PetscObject obj, PetscOptions *options)
30d71ae5a4SJacob Faibussowitsch {
31ffc43655SBarry Smith   PetscFunctionBegin;
32ffc43655SBarry Smith   PetscValidHeader(obj, 1);
33ffc43655SBarry Smith   *options = obj->options;
34ffc43655SBarry Smith   PetscFunctionReturn(0);
35ffc43655SBarry Smith }
36ffc43655SBarry Smith 
37ffc43655SBarry Smith /*@C
3816413a6aSBarry Smith    PetscObjectSetOptions - Sets the options database used by the object. Call immediately after creating the object.
39c5929fdfSBarry Smith 
40*c3339decSBarry Smith    Collective
4196a0c994SBarry Smith 
42c5929fdfSBarry Smith    Input Parameters:
43811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
44c5929fdfSBarry Smith -  options - the options database, use NULL for default
45c5929fdfSBarry Smith 
46811af0c4SBarry Smith    Note:
47811af0c4SBarry Smith    If this is not called the object will use the default options database
48811af0c4SBarry Smith 
49811af0c4SBarry Smith    Developer Note:
50811af0c4SBarry Smith    This functionality is not used in PETSc and should, perhaps, be removed
51c5929fdfSBarry Smith 
5296a0c994SBarry Smith   Level: advanced
5396a0c994SBarry Smith 
54db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
55db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `PetscObjectGetOptions()`
5696a0c994SBarry Smith @*/
57d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetOptions(PetscObject obj, PetscOptions options)
58d71ae5a4SJacob Faibussowitsch {
59c5929fdfSBarry Smith   PetscFunctionBegin;
60c5929fdfSBarry Smith   PetscValidHeader(obj, 1);
61c5929fdfSBarry Smith   obj->options = options;
62c5929fdfSBarry Smith   PetscFunctionReturn(0);
63c5929fdfSBarry Smith }
64c5929fdfSBarry Smith 
655cec412bSBarry Smith /*@C
66e5c89e4eSSatish Balay    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
67811af0c4SBarry Smith    options for the given object in the database.
68e5c89e4eSSatish Balay 
69*c3339decSBarry Smith    Collective
7096a0c994SBarry Smith 
71e5c89e4eSSatish Balay    Input Parameters:
72811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
73a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
74e5c89e4eSSatish Balay 
75811af0c4SBarry Smith    Note:
76e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
77e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
78e5c89e4eSSatish Balay    hyphen.
79e5c89e4eSSatish Balay 
80edc382c3SSatish Balay   Level: advanced
81edc382c3SSatish Balay 
82db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
83db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `SNESSetOptionsPrefix()`, `KSPSetOptionsPrefix()`
845cec412bSBarry Smith @*/
85d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject obj, const char prefix[])
86d71ae5a4SJacob Faibussowitsch {
87e5c89e4eSSatish Balay   PetscFunctionBegin;
883cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
895f80ce2aSJacob Faibussowitsch   if (prefix) {
905f80ce2aSJacob Faibussowitsch     PetscValidCharPointer(prefix, 2);
915f80ce2aSJacob Faibussowitsch     PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
924ed52fbaSBarry Smith     if (prefix != obj->prefix) {
939566063dSJacob Faibussowitsch       PetscCall(PetscFree(obj->prefix));
949566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(prefix, &obj->prefix));
95e5c89e4eSSatish Balay     }
969566063dSJacob Faibussowitsch   } else PetscCall(PetscFree(obj->prefix));
97e5c89e4eSSatish Balay   PetscFunctionReturn(0);
98e5c89e4eSSatish Balay }
99e5c89e4eSSatish Balay 
1005cec412bSBarry Smith /*@C
101811af0c4SBarry Smith    PetscObjectAppendOptionsPrefix - Appends to the prefix used for searching for options for the given object in the database.
102e5c89e4eSSatish Balay 
103e5c89e4eSSatish Balay    Input Parameters:
104811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
105a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
106e5c89e4eSSatish Balay 
107811af0c4SBarry Smith    Note:
108e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
109e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
110e5c89e4eSSatish Balay    hyphen.
111e5c89e4eSSatish Balay 
112edc382c3SSatish Balay   Level: advanced
113edc382c3SSatish Balay 
114db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
115db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSAppendOptionsPrefix()`, `SNESAppendOptionsPrefix()`, `KSPAppendOptionsPrefix()`
1165cec412bSBarry Smith @*/
117d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject obj, const char prefix[])
118d71ae5a4SJacob Faibussowitsch {
119e5c89e4eSSatish Balay   char  *buf = obj->prefix;
120e5c89e4eSSatish Balay   size_t len1, len2;
121e5c89e4eSSatish Balay 
122e5c89e4eSSatish Balay   PetscFunctionBegin;
1233cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
124a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
125e5c89e4eSSatish Balay   if (!buf) {
1269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
127e5c89e4eSSatish Balay     PetscFunctionReturn(0);
128e5c89e4eSSatish Balay   }
129cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
130e5c89e4eSSatish Balay 
1319566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
1329566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(buf, &len2));
1339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1 + len1 + len2, &obj->prefix));
1349566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(obj->prefix, buf));
1359566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(obj->prefix, prefix));
1369566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
137e5c89e4eSSatish Balay   PetscFunctionReturn(0);
138e5c89e4eSSatish Balay }
139e5c89e4eSSatish Balay 
1405cec412bSBarry Smith /*@C
141811af0c4SBarry Smith    PetscObjectGetOptionsPrefix - Gets the prefix of the `PetscObject` used for searching in the options database
142e5c89e4eSSatish Balay 
143e5c89e4eSSatish Balay    Input Parameters:
144811af0c4SBarry Smith .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay    Output Parameters:
147e5c89e4eSSatish Balay .  prefix - pointer to the prefix string used is returned
148e5c89e4eSSatish Balay 
149edc382c3SSatish Balay   Level: advanced
150edc382c3SSatish Balay 
151db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
152db781477SPatrick Sanan           `TSGetOptionsPrefix()`, `SNESGetOptionsPrefix()`, `KSPGetOptionsPrefix()`
1535cec412bSBarry Smith @*/
154d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject obj, const char *prefix[])
155d71ae5a4SJacob Faibussowitsch {
156e5c89e4eSSatish Balay   PetscFunctionBegin;
1573cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1583cfa8680SLisandro Dalcin   PetscValidPointer(prefix, 2);
159e5c89e4eSSatish Balay   *prefix = obj->prefix;
160e5c89e4eSSatish Balay   PetscFunctionReturn(0);
161e5c89e4eSSatish Balay }
162e5c89e4eSSatish Balay 
1635cec412bSBarry Smith /*@C
164811af0c4SBarry Smith    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for options of for this object in the database.
165e5c89e4eSSatish Balay 
166e5c89e4eSSatish Balay    Input Parameters:
167811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
168a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
169e5c89e4eSSatish Balay 
170811af0c4SBarry Smith    Note:
171e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
172e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
173e5c89e4eSSatish Balay    hyphen.
174e5c89e4eSSatish Balay 
175edc382c3SSatish Balay   Level: advanced
176edc382c3SSatish Balay 
177db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`,
178db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`
1795cec412bSBarry Smith @*/
180d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject obj, const char prefix[])
181d71ae5a4SJacob Faibussowitsch {
1825c7534e4SLisandro Dalcin   char  *buf;
183e5c89e4eSSatish Balay   size_t len1, len2;
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay   PetscFunctionBegin;
1863cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1875c7534e4SLisandro Dalcin   buf = obj->prefix;
188a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
189e5c89e4eSSatish Balay   if (!buf) {
1909566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
191e5c89e4eSSatish Balay     PetscFunctionReturn(0);
192e5c89e4eSSatish Balay   }
193cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
194e5c89e4eSSatish Balay 
1959566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
1969566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(buf, &len2));
1979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1 + len1 + len2, &obj->prefix));
1989566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(obj->prefix, prefix));
1999566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(obj->prefix, buf));
2009566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
201e5c89e4eSSatish Balay   PetscFunctionReturn(0);
202e5c89e4eSSatish Balay }
203