xref: /petsc/src/sys/objects/prefix.c (revision 285fb4e2b69b3de46a0633bd0adc6a7f684caa1e)
1 
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include <petsc/private/petscimpl.h>  /*I   "petscsys.h"    I*/
6 
7 /*@C
8    PetscObjectSetOptions - Sets the options database used by the object
9 
10    Collective on PetscObject
11 
12    Input Parameters:
13 +  obj - any PETSc object, for example a Vec, Mat or KSP.
14 -  options - the options database, use NULL for default
15 
16    Notes:
17     if this is not called the object will use the default options database
18 
19   Level: advanced
20 
21 .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
22           PetscObjectGetOptionsPrefix()
23 
24 @*/
25 PetscErrorCode  PetscObjectSetOptions(PetscObject obj,PetscOptions options)
26 {
27   PetscFunctionBegin;
28   PetscValidHeader(obj,1);
29   obj->options = options;
30   PetscFunctionReturn(0);
31 }
32 
33 /*@C
34    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
35    options of PetscObjectType in the database.
36 
37    Collective on Object
38 
39    Input Parameters:
40 .  obj - any PETSc object, for example a Vec, Mat or KSP.
41 .  prefix - the prefix string to prepend to option requests of the object.
42 
43    Notes:
44    A hyphen (-) must NOT be given at the beginning of the prefix name.
45    The first character of all runtime options is AUTOMATICALLY the
46    hyphen.
47 
48    Concepts: prefix^setting
49 
50   Level: advanced
51 
52 .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
53           PetscObjectGetOptionsPrefix(), TSSetOptionsPrefix(), SNESSetOptionsPrefix(), KSPSetOptionsPrefix()
54 
55 @*/
56 PetscErrorCode  PetscObjectSetOptionsPrefix(PetscObject obj,const char prefix[])
57 {
58   PetscErrorCode ierr;
59 
60   PetscFunctionBegin;
61   PetscValidHeader(obj,1);
62   if (!prefix) {
63     ierr = PetscFree(obj->prefix);CHKERRQ(ierr);
64   } else {
65     if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
66     if (prefix != obj->prefix) {
67       ierr = PetscFree(obj->prefix);CHKERRQ(ierr);
68       ierr = PetscStrallocpy(prefix,&obj->prefix);CHKERRQ(ierr);
69     }
70   }
71   PetscFunctionReturn(0);
72 }
73 
74 /*@C
75    PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all
76    options of PetscObjectType in the database.
77 
78    Input Parameters:
79 .  obj - any PETSc object, for example a Vec, Mat or KSP.
80 .  prefix - the prefix string to prepend to option requests of the object.
81 
82    Notes:
83    A hyphen (-) must NOT be given at the beginning of the prefix name.
84    The first character of all runtime options is AUTOMATICALLY the
85    hyphen.
86 
87    Concepts: prefix^setting
88 
89   Level: advanced
90 
91 .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
92           PetscObjectGetOptionsPrefix(), TSAppendOptionsPrefix(), SNESAppendOptionsPrefix(), KSPAppendOptionsPrefix()
93 
94 @*/
95 PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])
96 {
97   char           *buf = obj->prefix;
98   PetscErrorCode ierr;
99   size_t         len1,len2;
100 
101   PetscFunctionBegin;
102   PetscValidHeader(obj,1);
103   if (!prefix) PetscFunctionReturn(0);
104   if (!buf) {
105     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
106     PetscFunctionReturn(0);
107   }
108   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
109 
110   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
111   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
112   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
113   ierr = PetscStrcpy(obj->prefix,buf);CHKERRQ(ierr);
114   ierr = PetscStrcat(obj->prefix,prefix);CHKERRQ(ierr);
115   ierr = PetscFree(buf);CHKERRQ(ierr);
116   PetscFunctionReturn(0);
117 }
118 
119 /*@C
120    PetscObjectGetOptionsPrefix - Gets the prefix of the PetscObject.
121 
122    Input Parameters:
123 .  obj - any PETSc object, for example a Vec, Mat or KSP.
124 
125    Output Parameters:
126 .  prefix - pointer to the prefix string used is returned
127 
128    Concepts: prefix^getting
129 
130   Level: advanced
131 
132 .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
133           TSGetOptionsPrefix(), SNESGetOptionsPrefix(), KSPGetOptionsPrefix()
134 
135 @*/
136 PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject obj,const char *prefix[])
137 {
138   PetscFunctionBegin;
139   PetscValidHeader(obj,1);
140   PetscValidPointer(prefix,2);
141   *prefix = obj->prefix;
142   PetscFunctionReturn(0);
143 }
144 
145 /*@C
146    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for all
147    options of PetscObjectType in the database.
148 
149    Input Parameters:
150 .  obj - any PETSc object, for example a Vec, Mat or KSP.
151 .  prefix - the prefix string to prepend to option requests of the object.
152 
153    Notes:
154    A hyphen (-) must NOT be given at the beginning of the prefix name.
155    The first character of all runtime options is AUTOMATICALLY the
156    hyphen.
157 
158    Concepts: prefix^setting
159 
160   Level: advanced
161 
162 .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(),
163           PetscObjectGetOptionsPrefix(), TSPrependOptionsPrefix(), SNESPrependOptionsPrefix(), KSPPrependOptionsPrefix()
164 
165 @*/
166 PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])
167 {
168   char           *buf;
169   size_t         len1,len2;
170   PetscErrorCode ierr;
171 
172   PetscFunctionBegin;
173   PetscValidHeader(obj,1);
174   buf = obj->prefix;
175   if (!prefix) PetscFunctionReturn(0);
176   if (!buf) {
177     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
178     PetscFunctionReturn(0);
179   }
180   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
181 
182   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
183   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
184   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
185   ierr = PetscStrcpy(obj->prefix,prefix);CHKERRQ(ierr);
186   ierr = PetscStrcat(obj->prefix,buf);CHKERRQ(ierr);
187   ierr = PetscFree(buf);CHKERRQ(ierr);
188   PetscFunctionReturn(0);
189 }
190 
191