xref: /petsc/src/sys/objects/prefix.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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
8ffc43655SBarry Smith    PetscObjectGetOptions - Gets the options database used by the object. Call immediately after creating the object.
9ffc43655SBarry Smith 
10ffc43655SBarry Smith    Collective on PetscObject
11ffc43655SBarry Smith 
12ffc43655SBarry Smith    Input Parameter:
13ffc43655SBarry 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 
18ffc43655SBarry Smith    Notes:
19ffc43655SBarry Smith     if this is not called the object will use the default options database
20ffc43655SBarry Smith 
21ffc43655SBarry Smith   Level: advanced
22ffc43655SBarry Smith 
23ffc43655SBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
24ffc43655SBarry Smith           PetscObjectGetOptionsPrefix(), PetscObjectSetOptions()
25ffc43655SBarry Smith 
26ffc43655SBarry Smith @*/
27ffc43655SBarry Smith PetscErrorCode  PetscObjectGetOptions(PetscObject obj,PetscOptions *options)
28ffc43655SBarry Smith {
29ffc43655SBarry Smith   PetscFunctionBegin;
30ffc43655SBarry Smith   PetscValidHeader(obj,1);
31ffc43655SBarry Smith   *options = obj->options;
32ffc43655SBarry Smith   PetscFunctionReturn(0);
33ffc43655SBarry Smith }
34ffc43655SBarry Smith 
35ffc43655SBarry Smith /*@C
3616413a6aSBarry Smith    PetscObjectSetOptions - Sets the options database used by the object. Call immediately after creating the object.
37c5929fdfSBarry Smith 
3896a0c994SBarry Smith    Collective on PetscObject
3996a0c994SBarry Smith 
40c5929fdfSBarry Smith    Input Parameters:
41c5929fdfSBarry Smith +  obj - any PETSc object, for example a Vec, Mat or KSP.
42c5929fdfSBarry Smith -  options - the options database, use NULL for default
43c5929fdfSBarry Smith 
4495452b02SPatrick Sanan    Notes:
4595452b02SPatrick Sanan     if this is not called the object will use the default options database
46c5929fdfSBarry Smith 
4796a0c994SBarry Smith   Level: advanced
4896a0c994SBarry Smith 
495cec412bSBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
50ffc43655SBarry Smith           PetscObjectGetOptionsPrefix(), PetscObjectGetOptions()
51c5929fdfSBarry Smith 
5296a0c994SBarry Smith @*/
53c5929fdfSBarry Smith PetscErrorCode  PetscObjectSetOptions(PetscObject obj,PetscOptions options)
54c5929fdfSBarry Smith {
55c5929fdfSBarry Smith   PetscFunctionBegin;
56c5929fdfSBarry Smith   PetscValidHeader(obj,1);
57c5929fdfSBarry Smith   obj->options = options;
58c5929fdfSBarry Smith   PetscFunctionReturn(0);
59c5929fdfSBarry Smith }
60c5929fdfSBarry Smith 
615cec412bSBarry Smith /*@C
62e5c89e4eSSatish Balay    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
63e5c89e4eSSatish Balay    options of PetscObjectType in the database.
64e5c89e4eSSatish Balay 
6596a0c994SBarry Smith    Collective on Object
6696a0c994SBarry Smith 
67e5c89e4eSSatish Balay    Input Parameters:
68a2b725a8SWilliam Gropp +  obj - any PETSc object, for example a Vec, Mat or KSP.
69a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
70e5c89e4eSSatish Balay 
71e5c89e4eSSatish Balay    Notes:
72e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
73e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
74e5c89e4eSSatish Balay    hyphen.
75e5c89e4eSSatish Balay 
76edc382c3SSatish Balay   Level: advanced
77edc382c3SSatish Balay 
785cec412bSBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
795cec412bSBarry Smith           PetscObjectGetOptionsPrefix(), TSSetOptionsPrefix(), SNESSetOptionsPrefix(), KSPSetOptionsPrefix()
805cec412bSBarry Smith 
815cec412bSBarry Smith @*/
827087cfbeSBarry Smith PetscErrorCode  PetscObjectSetOptionsPrefix(PetscObject obj,const char prefix[])
83e5c89e4eSSatish Balay {
84e5c89e4eSSatish Balay   PetscFunctionBegin;
853cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
86*5f80ce2aSJacob Faibussowitsch   if (prefix) {
87*5f80ce2aSJacob Faibussowitsch     PetscValidCharPointer(prefix,2);
88*5f80ce2aSJacob 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) {
90*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(obj->prefix));
91*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrallocpy(prefix,&obj->prefix));
92e5c89e4eSSatish Balay     }
93*5f80ce2aSJacob Faibussowitsch   } else CHKERRQ(PetscFree(obj->prefix));
94e5c89e4eSSatish Balay   PetscFunctionReturn(0);
95e5c89e4eSSatish Balay }
96e5c89e4eSSatish Balay 
975cec412bSBarry Smith /*@C
98e5c89e4eSSatish Balay    PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all
99e5c89e4eSSatish Balay    options of PetscObjectType in the database.
100e5c89e4eSSatish Balay 
101e5c89e4eSSatish Balay    Input Parameters:
102a2b725a8SWilliam Gropp +  obj - any PETSc object, for example a Vec, Mat or KSP.
103a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
104e5c89e4eSSatish Balay 
105e5c89e4eSSatish Balay    Notes:
106e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
107e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
108e5c89e4eSSatish Balay    hyphen.
109e5c89e4eSSatish Balay 
110edc382c3SSatish Balay   Level: advanced
111edc382c3SSatish Balay 
1125cec412bSBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
1135cec412bSBarry Smith           PetscObjectGetOptionsPrefix(), TSAppendOptionsPrefix(), SNESAppendOptionsPrefix(), KSPAppendOptionsPrefix()
1145cec412bSBarry Smith 
1155cec412bSBarry Smith @*/
1167087cfbeSBarry Smith PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])
117e5c89e4eSSatish Balay {
118e5c89e4eSSatish Balay   char           *buf = obj->prefix;
119e5c89e4eSSatish Balay   size_t         len1,len2;
120e5c89e4eSSatish Balay 
121e5c89e4eSSatish Balay   PetscFunctionBegin;
1223cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
123a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
124e5c89e4eSSatish Balay   if (!buf) {
125*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix(obj,prefix));
126e5c89e4eSSatish Balay     PetscFunctionReturn(0);
127e5c89e4eSSatish Balay   }
1282c71b3e2SJacob Faibussowitsch   PetscCheckFalse(prefix[0] == '-',PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hyphen");
129e5c89e4eSSatish Balay 
130*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrlen(prefix,&len1));
131*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrlen(buf,&len2));
132*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(1+len1+len2,&obj->prefix));
133*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcpy(obj->prefix,buf));
134*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcat(obj->prefix,prefix));
135*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(buf));
136e5c89e4eSSatish Balay   PetscFunctionReturn(0);
137e5c89e4eSSatish Balay }
138e5c89e4eSSatish Balay 
1395cec412bSBarry Smith /*@C
140e5c89e4eSSatish Balay    PetscObjectGetOptionsPrefix - Gets the prefix of the PetscObject.
141e5c89e4eSSatish Balay 
142e5c89e4eSSatish Balay    Input Parameters:
143e5c89e4eSSatish Balay .  obj - any PETSc object, for example a Vec, Mat or KSP.
144e5c89e4eSSatish Balay 
145e5c89e4eSSatish Balay    Output Parameters:
146e5c89e4eSSatish Balay .  prefix - pointer to the prefix string used is returned
147e5c89e4eSSatish Balay 
148edc382c3SSatish Balay   Level: advanced
149edc382c3SSatish Balay 
1505cec412bSBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
1515cec412bSBarry Smith           TSGetOptionsPrefix(), SNESGetOptionsPrefix(), KSPGetOptionsPrefix()
1525cec412bSBarry Smith 
1535cec412bSBarry Smith @*/
1547087cfbeSBarry Smith PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject obj,const char *prefix[])
155e5c89e4eSSatish Balay {
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
164e5c89e4eSSatish Balay    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for all
165e5c89e4eSSatish Balay    options of PetscObjectType in the database.
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay    Input Parameters:
168a2b725a8SWilliam Gropp +  obj - any PETSc object, for example a Vec, Mat or KSP.
169a2b725a8SWilliam Gropp -  prefix - the prefix string to prepend to option requests of the object.
170e5c89e4eSSatish Balay 
171e5c89e4eSSatish Balay    Notes:
172e5c89e4eSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
173e5c89e4eSSatish Balay    The first character of all runtime options is AUTOMATICALLY the
174e5c89e4eSSatish Balay    hyphen.
175e5c89e4eSSatish Balay 
176edc382c3SSatish Balay   Level: advanced
177edc382c3SSatish Balay 
1785cec412bSBarry Smith .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(),
17977591d71SPierre Jolivet           PetscObjectGetOptionsPrefix()
1805cec412bSBarry Smith 
1815cec412bSBarry Smith @*/
1827087cfbeSBarry Smith PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])
183e5c89e4eSSatish Balay {
1845c7534e4SLisandro Dalcin   char           *buf;
185e5c89e4eSSatish Balay   size_t         len1,len2;
186e5c89e4eSSatish Balay 
187e5c89e4eSSatish Balay   PetscFunctionBegin;
1883cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
1895c7534e4SLisandro Dalcin   buf = obj->prefix;
190a297a907SKarl Rupp   if (!prefix) PetscFunctionReturn(0);
191e5c89e4eSSatish Balay   if (!buf) {
192*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix(obj,prefix));
193e5c89e4eSSatish Balay     PetscFunctionReturn(0);
194e5c89e4eSSatish Balay   }
1952c71b3e2SJacob Faibussowitsch   PetscCheckFalse(prefix[0] == '-',PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hyphen");
196e5c89e4eSSatish Balay 
197*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrlen(prefix,&len1));
198*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrlen(buf,&len2));
199*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(1+len1+len2,&obj->prefix));
200*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcpy(obj->prefix,prefix));
201*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcat(obj->prefix,buf));
202*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(buf));
203e5c89e4eSSatish Balay   PetscFunctionReturn(0);
204e5c89e4eSSatish Balay }
205