Actual source code: prefix.c


  2: /*
  3:      Provides utility routines for manulating any type of PETSc object.
  4: */
  5: #include <petsc/private/petscimpl.h>

  7: /*@C
  8:    PetscObjectGetOptions - Gets the options database used by the object. Call immediately after creating the object.

 10:    Collective on PetscObject

 12:    Input Parameter:
 13: .  obj - any PETSc object, for example a Vec, Mat or KSP.

 15:    Output Parameter:
 16: .  options - the options database

 18:    Notes:
 19:     if this is not called the object will use the default options database

 21:   Level: advanced

 23: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
 24:           PetscObjectGetOptionsPrefix(), PetscObjectSetOptions()

 26: @*/
 27: PetscErrorCode  PetscObjectGetOptions(PetscObject obj,PetscOptions *options)
 28: {
 30:   *options = obj->options;
 31:   return 0;
 32: }

 34: /*@C
 35:    PetscObjectSetOptions - Sets the options database used by the object. Call immediately after creating the object.

 37:    Collective on PetscObject

 39:    Input Parameters:
 40: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 41: -  options - the options database, use NULL for default

 43:    Notes:
 44:     if this is not called the object will use the default options database

 46:   Level: advanced

 48: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
 49:           PetscObjectGetOptionsPrefix(), PetscObjectGetOptions()

 51: @*/
 52: PetscErrorCode  PetscObjectSetOptions(PetscObject obj,PetscOptions options)
 53: {
 55:   obj->options = options;
 56:   return 0;
 57: }

 59: /*@C
 60:    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
 61:    options of PetscObjectType in the database.

 63:    Collective on Object

 65:    Input Parameters:
 66: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 67: -  prefix - the prefix string to prepend to option requests of the object.

 69:    Notes:
 70:    A hyphen (-) must NOT be given at the beginning of the prefix name.
 71:    The first character of all runtime options is AUTOMATICALLY the
 72:    hyphen.

 74:   Level: advanced

 76: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
 77:           PetscObjectGetOptionsPrefix(), TSSetOptionsPrefix(), SNESSetOptionsPrefix(), KSPSetOptionsPrefix()

 79: @*/
 80: PetscErrorCode  PetscObjectSetOptionsPrefix(PetscObject obj,const char prefix[])
 81: {
 83:   if (prefix) {
 86:     if (prefix != obj->prefix) {
 87:       PetscFree(obj->prefix);
 88:       PetscStrallocpy(prefix,&obj->prefix);
 89:     }
 90:   } else PetscFree(obj->prefix);
 91:   return 0;
 92: }

 94: /*@C
 95:    PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all
 96:    options of PetscObjectType in the database.

 98:    Input Parameters:
 99: +  obj - any PETSc object, for example a Vec, Mat or KSP.
100: -  prefix - the prefix string to prepend to option requests of the object.

102:    Notes:
103:    A hyphen (-) must NOT be given at the beginning of the prefix name.
104:    The first character of all runtime options is AUTOMATICALLY the
105:    hyphen.

107:   Level: advanced

109: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
110:           PetscObjectGetOptionsPrefix(), TSAppendOptionsPrefix(), SNESAppendOptionsPrefix(), KSPAppendOptionsPrefix()

112: @*/
113: PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])
114: {
115:   char           *buf = obj->prefix;
116:   size_t         len1,len2;

119:   if (!prefix) return 0;
120:   if (!buf) {
121:     PetscObjectSetOptionsPrefix(obj,prefix);
122:     return 0;
123:   }

126:   PetscStrlen(prefix,&len1);
127:   PetscStrlen(buf,&len2);
128:   PetscMalloc1(1+len1+len2,&obj->prefix);
129:   PetscStrcpy(obj->prefix,buf);
130:   PetscStrcat(obj->prefix,prefix);
131:   PetscFree(buf);
132:   return 0;
133: }

135: /*@C
136:    PetscObjectGetOptionsPrefix - Gets the prefix of the PetscObject.

138:    Input Parameters:
139: .  obj - any PETSc object, for example a Vec, Mat or KSP.

141:    Output Parameters:
142: .  prefix - pointer to the prefix string used is returned

144:   Level: advanced

146: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(), PetscObjectPrependOptionsPrefix(),
147:           TSGetOptionsPrefix(), SNESGetOptionsPrefix(), KSPGetOptionsPrefix()

149: @*/
150: PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject obj,const char *prefix[])
151: {
154:   *prefix = obj->prefix;
155:   return 0;
156: }

158: /*@C
159:    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for all
160:    options of PetscObjectType in the database.

162:    Input Parameters:
163: +  obj - any PETSc object, for example a Vec, Mat or KSP.
164: -  prefix - the prefix string to prepend to option requests of the object.

166:    Notes:
167:    A hyphen (-) must NOT be given at the beginning of the prefix name.
168:    The first character of all runtime options is AUTOMATICALLY the
169:    hyphen.

171:   Level: advanced

173: .seealso: PetscOptionsCreate(), PetscOptionsDestroy(), PetscObjectSetOptionsPrefix(), PetscObjectAppendOptionsPrefix(),
174:           PetscObjectGetOptionsPrefix()

176: @*/
177: PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])
178: {
179:   char           *buf;
180:   size_t         len1,len2;

183:   buf = obj->prefix;
184:   if (!prefix) return 0;
185:   if (!buf) {
186:     PetscObjectSetOptionsPrefix(obj,prefix);
187:     return 0;
188:   }

191:   PetscStrlen(prefix,&len1);
192:   PetscStrlen(buf,&len2);
193:   PetscMalloc1(1+len1+len2,&obj->prefix);
194:   PetscStrcpy(obj->prefix,prefix);
195:   PetscStrcat(obj->prefix,buf);
196:   PetscFree(buf);
197:   return 0;
198: }