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