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