xref: /petsc/src/sys/objects/prefix.c (revision 42db9e714e1bef4aaf908856d87005ffefb44116)
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 
65d83a8b1SBarry Smith /*@
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 @*/
PetscObjectGetOptions(PetscObject obj,PetscOptions * options)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 
365d83a8b1SBarry Smith /*@
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 @*/
PetscObjectSetOptions(PetscObject obj,PetscOptions options)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 
64ffeef943SBarry Smith /*@
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 @*/
PetscObjectSetOptionsPrefix(PetscObject obj,const char prefix[])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) {
92*084fd5feSPierre Jolivet       size_t len;
93*084fd5feSPierre Jolivet 
949566063dSJacob Faibussowitsch       PetscCall(PetscFree(obj->prefix));
95*084fd5feSPierre Jolivet       PetscCall(PetscStrlen(prefix, &len));
96*084fd5feSPierre Jolivet       if (len) PetscCall(PetscStrallocpy(prefix, &obj->prefix));
97e5c89e4eSSatish Balay     }
989566063dSJacob Faibussowitsch   } else PetscCall(PetscFree(obj->prefix));
993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
100e5c89e4eSSatish Balay }
101e5c89e4eSSatish Balay 
102ffeef943SBarry Smith /*@
103811af0c4SBarry Smith   PetscObjectAppendOptionsPrefix - Appends to the prefix used for searching for options for the given object in the database.
104e5c89e4eSSatish Balay 
105e5c89e4eSSatish Balay   Input Parameters:
106811af0c4SBarry Smith + obj    - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
107a2b725a8SWilliam Gropp - prefix - the prefix string to prepend to option requests of the object.
108e5c89e4eSSatish Balay 
10921532e8aSBarry Smith   Level: advanced
11021532e8aSBarry Smith 
111811af0c4SBarry Smith   Note:
112e5c89e4eSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
113e5c89e4eSSatish Balay   The first character of all runtime options is AUTOMATICALLY the
114e5c89e4eSSatish Balay   hyphen.
115e5c89e4eSSatish Balay 
116db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectPrependOptionsPrefix()`,
117db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`, `TSAppendOptionsPrefix()`, `SNESAppendOptionsPrefix()`, `KSPAppendOptionsPrefix()`
1185cec412bSBarry Smith @*/
PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])119d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject obj, const char prefix[])
120d71ae5a4SJacob Faibussowitsch {
121c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
122e5c89e4eSSatish Balay 
123e5c89e4eSSatish Balay   PetscFunctionBegin;
1243cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1253ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
126c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1283ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
129e5c89e4eSSatish Balay   }
130cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
131e5c89e4eSSatish Balay 
132c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len1));
133c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(prefix, &len2));
134c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
135f4f49eeaSPierre Jolivet   PetscCall(PetscRealloc(new_len * sizeof(*obj->prefix), &obj->prefix));
136c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, prefix, len2 + 1));
1373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
138e5c89e4eSSatish Balay }
139e5c89e4eSSatish Balay 
140cc4c1da9SBarry Smith /*@
141811af0c4SBarry Smith   PetscObjectGetOptionsPrefix - Gets the prefix of the `PetscObject` used for searching in the options database
142e5c89e4eSSatish Balay 
1432fe279fdSBarry Smith   Input Parameter:
144811af0c4SBarry Smith . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
145e5c89e4eSSatish Balay 
1462fe279fdSBarry Smith   Output Parameter:
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 @*/
PetscObjectGetOptionsPrefix(PetscObject obj,const char * prefix[])154d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject obj, const char *prefix[])
155d71ae5a4SJacob Faibussowitsch {
156e5c89e4eSSatish Balay   PetscFunctionBegin;
1573cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1584f572ea9SToby Isaac   PetscAssertPointer(prefix, 2);
159e5c89e4eSSatish Balay   *prefix = obj->prefix;
1603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
161e5c89e4eSSatish Balay }
162e5c89e4eSSatish Balay 
1635d83a8b1SBarry Smith /*@
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 
17021532e8aSBarry Smith   Level: advanced
17121532e8aSBarry Smith 
172811af0c4SBarry Smith   Note:
173e5c89e4eSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
174e5c89e4eSSatish Balay   The first character of all runtime options is AUTOMATICALLY the
175e5c89e4eSSatish Balay   hyphen.
176e5c89e4eSSatish Balay 
177db781477SPatrick Sanan .seealso: `PetscOptionsCreate()`, `PetscOptionsDestroy()`, `PetscObjectSetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`,
178db781477SPatrick Sanan           `PetscObjectGetOptionsPrefix()`
1795cec412bSBarry Smith @*/
PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])180d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject obj, const char prefix[])
181d71ae5a4SJacob Faibussowitsch {
1825c7534e4SLisandro Dalcin   char  *buf;
183c6a7a370SJeremy L Thompson   size_t len1, len2, new_len;
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay   PetscFunctionBegin;
1863cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1873ba16761SJacob Faibussowitsch   if (!prefix) PetscFunctionReturn(PETSC_SUCCESS);
188c6a7a370SJeremy L Thompson   if (!obj->prefix) {
1899566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix(obj, prefix));
1903ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
191e5c89e4eSSatish Balay   }
192cc73adaaSBarry Smith   PetscCheck(prefix[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
193e5c89e4eSSatish Balay 
1949566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(prefix, &len1));
195c6a7a370SJeremy L Thompson   PetscCall(PetscStrlen(obj->prefix, &len2));
196c6a7a370SJeremy L Thompson   buf     = obj->prefix;
197c6a7a370SJeremy L Thompson   new_len = len1 + len2 + 1;
198c6a7a370SJeremy L Thompson   PetscCall(PetscMalloc1(new_len, &obj->prefix));
199c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix, prefix, len1 + 1));
200c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(obj->prefix + len1, buf, len2 + 1));
2019566063dSJacob Faibussowitsch   PetscCall(PetscFree(buf));
2023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
203e5c89e4eSSatish Balay }
204