xref: /petsc/src/sys/objects/prefix.c (revision f4f49eeac7efa77fffa46b7ff95a3ed169f659ed)
1e5c89e4eSSatish Balay /*
2e5c89e4eSSatish Balay      Provides utility routines for manulating any type of PETSc object.
3e5c89e4eSSatish Balay */
4af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I   "petscsys.h"    I*/
5e5c89e4eSSatish Balay 
6c5929fdfSBarry Smith /*@C
7811af0c4SBarry Smith   PetscObjectGetOptions - Gets the options database used by the object that has been set with `PetscObjectSetOptions()`
8ffc43655SBarry Smith 
9c3339decSBarry Smith   Collective
10ffc43655SBarry Smith 
11ffc43655SBarry Smith   Input Parameter:
12811af0c4SBarry Smith . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
13ffc43655SBarry Smith 
14ffc43655SBarry Smith   Output Parameter:
15ffc43655SBarry Smith . options - the options database
16ffc43655SBarry Smith 
172fe279fdSBarry Smith   Level: advanced
182fe279fdSBarry Smith 
19811af0c4SBarry Smith   Note:
20811af0c4SBarry Smith   If this is not called the object will use the default options database
21811af0c4SBarry Smith 
22aec76313SJacob Faibussowitsch   Developer Notes:
23811af0c4SBarry Smith   This functionality is not used in PETSc and should, perhaps, be removed
24ffc43655SBarry Smith 
25db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
26db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `PetscObjectSetOptions()`
27ffc43655SBarry Smith @*/
28d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptions(PetscObject obj, PetscOptions *options)
29d71ae5a4SJacob Faibussowitsch {
30ffc43655SBarry Smith   PetscFunctionBegin;
31ffc43655SBarry Smith   PetscValidHeader(obj, 1);
32ffc43655SBarry Smith   *options = obj->options;
333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
39c3339decSBarry Smith   Collective
4096a0c994SBarry Smith 
41c5929fdfSBarry Smith   Input Parameters:
42811af0c4SBarry Smith + obj     - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
43c5929fdfSBarry Smith - options - the options database, use NULL for default
44c5929fdfSBarry Smith 
452fe279fdSBarry Smith   Level: advanced
462fe279fdSBarry Smith 
47811af0c4SBarry Smith   Note:
48811af0c4SBarry Smith   If this is not called the object will use the default options database
49811af0c4SBarry Smith 
50aec76313SJacob Faibussowitsch   Developer Notes:
51811af0c4SBarry Smith   This functionality is not used in PETSc and should, perhaps, be removed
52c5929fdfSBarry Smith 
53db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
54db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `PetscObjectGetOptions()`
5596a0c994SBarry Smith @*/
56d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetOptions(PetscObject obj, PetscOptions options)
57d71ae5a4SJacob Faibussowitsch {
58c5929fdfSBarry Smith   PetscFunctionBegin;
59c5929fdfSBarry Smith   PetscValidHeader(obj, 1);
60c5929fdfSBarry Smith   obj->options = options;
613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62c5929fdfSBarry Smith }
63c5929fdfSBarry Smith 
645cec412bSBarry Smith /*@C
65e5c89e4eSSatish Balay   PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
66811af0c4SBarry Smith   options for the given object in the database.
67e5c89e4eSSatish Balay 
68c3339decSBarry Smith   Collective
6996a0c994SBarry Smith 
70e5c89e4eSSatish Balay   Input Parameters:
71811af0c4SBarry Smith + obj    - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
72a2b725a8SWilliam Gropp - prefix - the prefix string to prepend to option requests of the object.
73e5c89e4eSSatish Balay 
742fe279fdSBarry Smith   Level: advanced
752fe279fdSBarry Smith 
76811af0c4SBarry Smith   Note:
77e5c89e4eSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
78e5c89e4eSSatish Balay   The first character of all runtime options is AUTOMATICALLY the
79e5c89e4eSSatish Balay   hyphen.
80e5c89e4eSSatish Balay 
81db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
82db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `SNESSetOptionsPrefix()`, `KSPSetOptionsPrefix()`
835cec412bSBarry Smith @*/
84d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject obj, const char prefix[])
85d71ae5a4SJacob Faibussowitsch {
86e5c89e4eSSatish Balay   PetscFunctionBegin;
873cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
885f80ce2aSJacob Faibussowitsch   if (prefix) {
894f572ea9SToby Isaac     PetscAssertPointer(prefix, 2);
905f80ce2aSJacob Faibussowitsch     PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
914ed52fbaSBarry Smith     if (prefix != obj->prefix) {
929566063dSJacob Faibussowitsch       PetscCall(PetscFree(obj->prefix));
939566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(prefix, &obj->prefix));
94e5c89e4eSSatish Balay     }
959566063dSJacob Faibussowitsch   } else PetscCall(PetscFree(obj->prefix));
963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
97e5c89e4eSSatish Balay }
98e5c89e4eSSatish Balay 
995cec412bSBarry Smith /*@C
100811af0c4SBarry Smith   PetscObjectAppendOptionsPrefix - Appends to the prefix used for searching for options for the given object in the database.
101e5c89e4eSSatish Balay 
102e5c89e4eSSatish Balay   Input Parameters:
103811af0c4SBarry Smith + obj    - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
104a2b725a8SWilliam Gropp - prefix - the prefix string to prepend to option requests of the object.
105e5c89e4eSSatish Balay 
10621532e8aSBarry Smith   Level: advanced
10721532e8aSBarry Smith 
108811af0c4SBarry Smith   Note:
109e5c89e4eSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
110e5c89e4eSSatish Balay   The first character of all runtime options is AUTOMATICALLY the
111e5c89e4eSSatish Balay   hyphen.
112e5c89e4eSSatish Balay 
113db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
114db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSAppendOptionsPrefix()`, `SNESAppendOptionsPrefix()`, `KSPAppendOptionsPrefix()`
1155cec412bSBarry Smith @*/
116d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject obj, const char prefix[])
117d71ae5a4SJacob Faibussowitsch {
118c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
119e5c89e4eSSatish Balay 
120e5c89e4eSSatish Balay   PetscFunctionBegin;
1213cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1223ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
123c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1249566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1253ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
126e5c89e4eSSatish Balay   }
127cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
128e5c89e4eSSatish Balay 
129c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len1));
130c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(prefix, &len2));
131c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
132*f4f49eeaSPierre Jolivet   PetscCall(PetscRealloc(new_len * sizeof(*obj->prefix), &obj->prefix));
133c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, prefix, len2 + 1));
1343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
135e5c89e4eSSatish Balay }
136e5c89e4eSSatish Balay 
1375cec412bSBarry Smith /*@C
138811af0c4SBarry Smith   PetscObjectGetOptionsPrefix - Gets the prefix of the `PetscObject` used for searching in the options database
139e5c89e4eSSatish Balay 
1402fe279fdSBarry Smith   Input Parameter:
141811af0c4SBarry Smith . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
142e5c89e4eSSatish Balay 
1432fe279fdSBarry Smith   Output Parameter:
144e5c89e4eSSatish Balay . prefix - pointer to the prefix string used is returned
145e5c89e4eSSatish Balay 
146edc382c3SSatish Balay   Level: advanced
147edc382c3SSatish Balay 
148db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
149db781477SPatrick Sanan           `TSGetOptionsPrefix()`, `SNESGetOptionsPrefix()`, `KSPGetOptionsPrefix()`
1505cec412bSBarry Smith @*/
151d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject obj, const char *prefix[])
152d71ae5a4SJacob Faibussowitsch {
153e5c89e4eSSatish Balay   PetscFunctionBegin;
1543cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1554f572ea9SToby Isaac   PetscAssertPointer(prefix, 2);
156e5c89e4eSSatish Balay   *prefix = obj->prefix;
1573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
158e5c89e4eSSatish Balay }
159e5c89e4eSSatish Balay 
1605cec412bSBarry Smith /*@C
161811af0c4SBarry Smith   PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for options of for this object in the database.
162e5c89e4eSSatish Balay 
163e5c89e4eSSatish Balay   Input Parameters:
164811af0c4SBarry Smith + obj    - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
165a2b725a8SWilliam Gropp - prefix - the prefix string to prepend to option requests of the object.
166e5c89e4eSSatish Balay 
16721532e8aSBarry Smith   Level: advanced
16821532e8aSBarry Smith 
169811af0c4SBarry Smith   Note:
170e5c89e4eSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
171e5c89e4eSSatish Balay   The first character of all runtime options is AUTOMATICALLY the
172e5c89e4eSSatish Balay   hyphen.
173e5c89e4eSSatish Balay 
174db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`,
175db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`
1765cec412bSBarry Smith @*/
177d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject obj, const char prefix[])
178d71ae5a4SJacob Faibussowitsch {
1795c7534e4SLisandro Dalcin   char  *buf;
180c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
181e5c89e4eSSatish Balay 
182e5c89e4eSSatish Balay   PetscFunctionBegin;
1833cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1843ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
185c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1869566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1873ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
188e5c89e4eSSatish Balay   }
189cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
190e5c89e4eSSatish Balay 
1919566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
192c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len2));
193c6a7a370SJeremy L Thompson   buf     = obj->prefix;
194c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
195c6a7a370SJeremy L Thompson   PetscCall(PetscMalloc1(new_len, &obj->prefix));
196c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix, prefix, len1 + 1));
197c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, buf, len2 + 1));
1989566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
1993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
200e5c89e4eSSatish Balay }
201