xref: /petsc/src/sys/objects/prefix.c (revision 2fe279fdf3e687a416e4eadb7d3c7a82d60442c6)
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 
10c3339decSBarry 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 
18*2fe279fdSBarry Smith   Level: advanced
19*2fe279fdSBarry Smith 
20811af0c4SBarry Smith    Note:
21811af0c4SBarry Smith    If this is not called the object will use the default options database
22811af0c4SBarry Smith 
23811af0c4SBarry Smith    Developer Note:
24811af0c4SBarry Smith    This functionality is not used in PETSc and should, perhaps, be removed
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;
343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
40c3339decSBarry 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 
46*2fe279fdSBarry Smith   Level: advanced
47*2fe279fdSBarry Smith 
48811af0c4SBarry Smith    Note:
49811af0c4SBarry Smith    If this is not called the object will use the default options database
50811af0c4SBarry Smith 
51811af0c4SBarry Smith    Developer Note:
52811af0c4SBarry Smith    This functionality is not used in PETSc and should, perhaps, be removed
53c5929fdfSBarry 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;
623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
69c3339decSBarry 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 
75*2fe279fdSBarry Smith   Level: advanced
76*2fe279fdSBarry Smith 
77811af0c4SBarry Smith    Note:
78e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
79e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
80e5c89e4eSSatish Balay    hyphen.
81e5c89e4eSSatish 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));
973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 {
119c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
120e5c89e4eSSatish Balay 
121e5c89e4eSSatish Balay   PetscFunctionBegin;
1223cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1233ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
124c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1263ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
127e5c89e4eSSatish Balay   }
128cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
129e5c89e4eSSatish Balay 
130c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len1));
131c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(prefix, &len2));
132c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
133c6a7a370SJeremy L Thompson   PetscCall(PetscRealloc(new_len * sizeof(*(obj->prefix)), &obj->prefix));
134c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, prefix, len2 + 1));
1353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136e5c89e4eSSatish Balay }
137e5c89e4eSSatish Balay 
1385cec412bSBarry Smith /*@C
139811af0c4SBarry Smith    PetscObjectGetOptionsPrefix - Gets the prefix of the `PetscObject` used for searching in the options database
140e5c89e4eSSatish Balay 
141*2fe279fdSBarry Smith    Input Parameter:
142811af0c4SBarry Smith .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
143e5c89e4eSSatish Balay 
144*2fe279fdSBarry Smith    Output Parameter:
145e5c89e4eSSatish Balay .  prefix - pointer to the prefix string used is returned
146e5c89e4eSSatish Balay 
147edc382c3SSatish Balay   Level: advanced
148edc382c3SSatish Balay 
149db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
150db781477SPatrick Sanan           `TSGetOptionsPrefix()`, `SNESGetOptionsPrefix()`, `KSPGetOptionsPrefix()`
1515cec412bSBarry Smith @*/
152d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject obj, const char *prefix[])
153d71ae5a4SJacob Faibussowitsch {
154e5c89e4eSSatish Balay   PetscFunctionBegin;
1553cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1563cfa8680SLisandro Dalcin   PetscValidPointer(prefix, 2);
157e5c89e4eSSatish Balay   *prefix = obj->prefix;
1583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
159e5c89e4eSSatish Balay }
160e5c89e4eSSatish Balay 
1615cec412bSBarry Smith /*@C
162811af0c4SBarry Smith    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for options of for this object in the database.
163e5c89e4eSSatish Balay 
164e5c89e4eSSatish Balay    Input Parameters:
165811af0c4SBarry Smith +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
166a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
167e5c89e4eSSatish Balay 
168811af0c4SBarry Smith    Note:
169e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
170e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
171e5c89e4eSSatish Balay    hyphen.
172e5c89e4eSSatish Balay 
173edc382c3SSatish Balay   Level: advanced
174edc382c3SSatish Balay 
175db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`,
176db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`
1775cec412bSBarry Smith @*/
178d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject obj, const char prefix[])
179d71ae5a4SJacob Faibussowitsch {
1805c7534e4SLisandro Dalcin   char  *buf;
181c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
182e5c89e4eSSatish Balay 
183e5c89e4eSSatish Balay   PetscFunctionBegin;
1843cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1853ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
186c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1879566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1883ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
189e5c89e4eSSatish Balay   }
190cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
191e5c89e4eSSatish Balay 
1929566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
193c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len2));
194c6a7a370SJeremy L Thompson   buf     = obj->prefix;
195c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
196c6a7a370SJeremy L Thompson   PetscCall(PetscMalloc1(new_len, &obj->prefix));
197c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix, prefix, len1 + 1));
198c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, buf, len2 + 1));
1999566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
2003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
201e5c89e4eSSatish Balay }
202