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