Actual source code: options.c

  1: /* Define Feature test macros to make sure atoll is available (SVr4, POSIX.1-2001, 4.3BSD, C99), not in (C89 and POSIX.1-1996) */
  2: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for atoll() */

  4: /*
  5:    These routines simplify the use of command line, file options, etc., and are used to manipulate the options database.
  6:    This provides the low-level interface, the high level interface is in aoptions.c

  8:    Some routines use regular malloc and free because it cannot know  what malloc is requested with the
  9:    options database until it has already processed the input.
 10: */

 12: #include <petsc/private/petscimpl.h>
 13: #include <petscviewer.h>
 14: #include <ctype.h>
 15: #if defined(PETSC_HAVE_MALLOC_H)
 16:   #include <malloc.h>
 17: #endif
 18: #if defined(PETSC_HAVE_STRINGS_H)
 19:   #include <strings.h> /* strcasecmp */
 20: #endif

 22: #if defined(PETSC_HAVE_STRCASECMP)
 23:   #define PetscOptNameCmp(a, b) strcasecmp(a, b)
 24: #elif defined(PETSC_HAVE_STRICMP)
 25:   #define PetscOptNameCmp(a, b) stricmp(a, b)
 26: #else
 27:   #define PetscOptNameCmp(a, b) Error_strcasecmp_not_found
 28: #endif

 30: #include <petsc/private/hashtable.h>

 32: /* This assumes ASCII encoding and ignores locale settings */
 33: /* Using tolower() is about 2X slower in microbenchmarks   */
 34: static inline int PetscToLower(int c)
 35: {
 36:   return ((c >= 'A') & (c <= 'Z')) ? c + 'a' - 'A' : c;
 37: }

 39: /* Bob Jenkins's one at a time hash function (case-insensitive) */
 40: static inline unsigned int PetscOptHash(const char key[])
 41: {
 42:   unsigned int hash = 0;
 43:   while (*key) {
 44:     hash += PetscToLower(*key++);
 45:     hash += hash << 10;
 46:     hash ^= hash >> 6;
 47:   }
 48:   hash += hash << 3;
 49:   hash ^= hash >> 11;
 50:   hash += hash << 15;
 51:   return hash;
 52: }

 54: static inline int PetscOptEqual(const char a[], const char b[])
 55: {
 56:   return !PetscOptNameCmp(a, b);
 57: }

 59: KHASH_INIT(HO, kh_cstr_t, int, 1, PetscOptHash, PetscOptEqual)

 61: #define MAXPREFIXES        25
 62: #define MAXOPTIONSMONITORS 5

 64: const char *PetscOptionSources[] = {"code", "command line", "file", "environment"};

 66: // This table holds all the options set by the user
 67: struct _n_PetscOptions {
 68:   PetscOptions previous;

 70:   int                N;      /* number of options */
 71:   int                Nalloc; /* number of allocated options */
 72:   char             **names;  /* option names */
 73:   char             **values; /* option values */
 74:   PetscBool         *used;   /* flag option use */
 75:   PetscOptionSource *source; /* source for option value */
 76:   PetscBool          precedentProcessed;

 78:   /* Hash table */
 79:   khash_t(HO) *ht;

 81:   /* Prefixes */
 82:   int  prefixind;
 83:   int  prefixstack[MAXPREFIXES];
 84:   char prefix[PETSC_MAX_OPTION_NAME];

 86:   /* Aliases */
 87:   int    Na;       /* number or aliases */
 88:   int    Naalloc;  /* number of allocated aliases */
 89:   char **aliases1; /* aliased */
 90:   char **aliases2; /* aliasee */

 92:   /* Help */
 93:   PetscBool help;       /* flag whether "-help" is in the database */
 94:   PetscBool help_intro; /* flag whether "-help intro" is in the database */

 96:   /* Monitors */
 97:   PetscBool monitorFromOptions, monitorCancel;
 98:   PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], PetscOptionSource, void *); /* returns control to user after */
 99:   PetscCtxDestroyFn *monitordestroy[MAXOPTIONSMONITORS];                                                /* callback for monitor destruction */
100:   void              *monitorcontext[MAXOPTIONSMONITORS];                                                /* to pass arbitrary user data into monitor */
101:   PetscInt           numbermonitors;                                                                    /* to, for instance, detect options being set */
102: };

104: static PetscOptions defaultoptions = NULL; /* the options database routines query this object for options */

106: /* list of options which precede others, i.e., are processed in PetscOptionsProcessPrecedentFlags() */
107: /* these options can only take boolean values, the code will crash if given a non-boolean value */
108: static const char *precedentOptions[] = {"-petsc_ci", "-options_monitor", "-options_monitor_cancel", "-help", "-skip_petscrc"};
109: enum PetscPrecedentOption {
110:   PO_CI_ENABLE,
111:   PO_OPTIONS_MONITOR,
112:   PO_OPTIONS_MONITOR_CANCEL,
113:   PO_HELP,
114:   PO_SKIP_PETSCRC,
115:   PO_NUM
116: };

118: PETSC_INTERN PetscErrorCode PetscOptionsSetValue_Private(PetscOptions, const char[], const char[], int *, PetscOptionSource);
119: PETSC_INTERN PetscErrorCode PetscOptionsInsertStringYAML_Private(PetscOptions, const char[], PetscOptionSource);

121: /*
122:     Options events monitor
123: */
124: static PetscErrorCode PetscOptionsMonitor(PetscOptions options, const char name[], const char value[], PetscOptionSource source)
125: {
126:   PetscFunctionBegin;
127:   if (options->monitorFromOptions) PetscCall(PetscOptionsMonitorDefault(name, value, source, NULL));
128:   for (PetscInt i = 0; i < options->numbermonitors; i++) PetscCall((*options->monitor[i])(name, value, source, options->monitorcontext[i]));
129:   PetscFunctionReturn(PETSC_SUCCESS);
130: }

132: /*@
133:   PetscOptionsCreate - Creates an empty options database.

135:   Logically Collective

137:   Output Parameter:
138: . options - Options database object

140:   Level: advanced

142:   Note:
143:   Though PETSc has a concept of multiple options database the current code uses a single default `PetscOptions` object

145:   Developer Notes:
146:   We may want eventually to pass a `MPI_Comm` to determine the ownership of the object

148:   This object never got developed after being introduced, it is not clear that supporting multiple `PetscOptions` objects is useful

150: .seealso: `PetscOptionsDestroy()`, `PetscOptionsPush()`, `PetscOptionsPop()`, `PetscOptionsInsert()`, `PetscOptionsSetValue()`
151: @*/
152: PetscErrorCode PetscOptionsCreate(PetscOptions *options)
153: {
154:   PetscFunctionBegin;
155:   PetscAssertPointer(options, 1);
156:   *options = (PetscOptions)calloc(1, sizeof(**options));
157:   PetscCheck(*options, PETSC_COMM_SELF, PETSC_ERR_MEM, "Failed to allocate the options database");
158:   PetscFunctionReturn(PETSC_SUCCESS);
159: }

161: /*@
162:   PetscOptionsDestroy - Destroys an option database.

164:   Logically Collective on whatever communicator was associated with the call to `PetscOptionsCreate()`

166:   Input Parameter:
167: . options - the `PetscOptions` object

169:   Level: advanced

171: .seealso: `PetscOptionsInsert()`, `PetscOptionsPush()`, `PetscOptionsPop()`, `PetscOptionsSetValue()`
172: @*/
173: PetscErrorCode PetscOptionsDestroy(PetscOptions *options)
174: {
175:   PetscFunctionBegin;
176:   PetscAssertPointer(options, 1);
177:   if (!*options) PetscFunctionReturn(PETSC_SUCCESS);
178:   PetscCheck(!(*options)->previous, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "You are destroying an option that has been used with PetscOptionsPush() but does not have a corresponding PetscOptionsPop()");
179:   PetscCall(PetscOptionsClear(*options));
180:   /* XXX what about monitors ? */
181:   free(*options);
182:   *options = NULL;
183:   PetscFunctionReturn(PETSC_SUCCESS);
184: }

186: /*
187:     PetscOptionsCreateDefault - Creates the default global options database
188: */
189: PetscErrorCode PetscOptionsCreateDefault(void)
190: {
191:   PetscFunctionBegin;
192:   if (PetscUnlikely(!defaultoptions)) PetscCall(PetscOptionsCreate(&defaultoptions));
193:   PetscFunctionReturn(PETSC_SUCCESS);
194: }

196: /*@
197:   PetscOptionsPush - Push a new `PetscOptions` object as the default provider of options
198:   Allows using different parts of a code to use different options databases

200:   Logically Collective

202:   Input Parameter:
203: . opt - the options obtained with `PetscOptionsCreate()`

205:   Level: advanced

207:   Notes:
208:   Use `PetscOptionsPop()` to return to the previous default options database

210:   The collectivity of this routine is complex; only the MPI ranks that call this routine will
211:   have the affect of these options. If some processes that create objects call this routine and others do
212:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
213:   on different ranks.

215:   Developer Notes:
216:   Though this functionality has been provided it has never been used in PETSc and might be removed.

218: .seealso: `PetscOptionsPop()`, `PetscOptionsCreate()`, `PetscOptionsInsert()`, `PetscOptionsSetValue()`, `PetscOptionsLeft()`
219: @*/
220: PetscErrorCode PetscOptionsPush(PetscOptions opt)
221: {
222:   PetscFunctionBegin;
223:   PetscCall(PetscOptionsCreateDefault());
224:   opt->previous  = defaultoptions;
225:   defaultoptions = opt;
226:   PetscFunctionReturn(PETSC_SUCCESS);
227: }

229: /*@
230:   PetscOptionsPop - Pop the most recent `PetscOptionsPush()` to return to the previous default options

232:   Logically Collective on whatever communicator was associated with the call to `PetscOptionsCreate()`

234:   Level: advanced

236: .seealso: `PetscOptionsCreate()`, `PetscOptionsInsert()`, `PetscOptionsSetValue()`, `PetscOptionsLeft()`
237: @*/
238: PetscErrorCode PetscOptionsPop(void)
239: {
240:   PetscOptions current = defaultoptions;

242:   PetscFunctionBegin;
243:   PetscCheck(defaultoptions, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing default options");
244:   PetscCheck(defaultoptions->previous, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscOptionsPop() called too many times");
245:   defaultoptions    = defaultoptions->previous;
246:   current->previous = NULL;
247:   PetscFunctionReturn(PETSC_SUCCESS);
248: }

250: /*
251:     PetscOptionsDestroyDefault - Destroys the default global options database
252: */
253: PetscErrorCode PetscOptionsDestroyDefault(void)
254: {
255:   PetscFunctionBegin;
256:   if (!defaultoptions) PetscFunctionReturn(PETSC_SUCCESS);
257:   /* Destroy any options that the user forgot to pop */
258:   while (defaultoptions->previous) {
259:     PetscOptions tmp = defaultoptions;

261:     PetscCall(PetscOptionsPop());
262:     PetscCall(PetscOptionsDestroy(&tmp));
263:   }
264:   PetscCall(PetscOptionsDestroy(&defaultoptions));
265:   PetscFunctionReturn(PETSC_SUCCESS);
266: }

268: /*@
269:   PetscOptionsValidKey - PETSc Options database keys must begin with one or two dashes (-) followed by a letter.

271:   Not Collective

273:   Input Parameter:
274: . key - string to check if valid

276:   Output Parameter:
277: . valid - `PETSC_TRUE` if a valid key

279:   Level: intermediate

281: .seealso: `PetscOptionsCreate()`, `PetscOptionsInsert()`
282: @*/
283: PetscErrorCode PetscOptionsValidKey(const char key[], PetscBool *valid)
284: {
285:   char               *ptr;
286:   PETSC_UNUSED double d;

288:   PetscFunctionBegin;
289:   if (key) PetscAssertPointer(key, 1);
290:   PetscAssertPointer(valid, 2);
291:   *valid = PETSC_FALSE;
292:   if (!key) PetscFunctionReturn(PETSC_SUCCESS);
293:   if (key[0] != '-') PetscFunctionReturn(PETSC_SUCCESS);
294:   if (key[1] == '-') key++;
295:   if (!isalpha((int)key[1])) PetscFunctionReturn(PETSC_SUCCESS);
296:   d = strtod(key, &ptr);
297:   if (ptr != key && !(*ptr == '_' || isalnum((int)*ptr))) PetscFunctionReturn(PETSC_SUCCESS);
298:   *valid = PETSC_TRUE;
299:   PetscFunctionReturn(PETSC_SUCCESS);
300: }

302: static PetscErrorCode PetscOptionsInsertString_Private(PetscOptions options, const char in_str[], PetscOptionSource source)
303: {
304:   const char *first, *second;
305:   PetscToken  token;

307:   PetscFunctionBegin;
308:   PetscCall(PetscTokenCreate(in_str, ' ', &token));
309:   PetscCall(PetscTokenFind(token, &first));
310:   while (first) {
311:     PetscBool isfile, isfileyaml, isstringyaml, ispush, ispop, key;

313:     PetscCall(PetscStrcasecmp(first, "-options_file", &isfile));
314:     PetscCall(PetscStrcasecmp(first, "-options_file_yaml", &isfileyaml));
315:     PetscCall(PetscStrcasecmp(first, "-options_string_yaml", &isstringyaml));
316:     PetscCall(PetscStrcasecmp(first, "-prefix_push", &ispush));
317:     PetscCall(PetscStrcasecmp(first, "-prefix_pop", &ispop));
318:     PetscCall(PetscOptionsValidKey(first, &key));
319:     if (!key) {
320:       PetscCall(PetscTokenFind(token, &first));
321:     } else if (isfile) {
322:       PetscCall(PetscTokenFind(token, &second));
323:       PetscCall(PetscOptionsInsertFile(PETSC_COMM_SELF, options, second, PETSC_TRUE));
324:       PetscCall(PetscTokenFind(token, &first));
325:     } else if (isfileyaml) {
326:       PetscCall(PetscTokenFind(token, &second));
327:       PetscCall(PetscOptionsInsertFileYAML(PETSC_COMM_SELF, options, second, PETSC_TRUE));
328:       PetscCall(PetscTokenFind(token, &first));
329:     } else if (isstringyaml) {
330:       PetscCall(PetscTokenFind(token, &second));
331:       PetscCall(PetscOptionsInsertStringYAML_Private(options, second, source));
332:       PetscCall(PetscTokenFind(token, &first));
333:     } else if (ispush) {
334:       PetscCall(PetscTokenFind(token, &second));
335:       PetscCall(PetscOptionsPrefixPush(options, second));
336:       PetscCall(PetscTokenFind(token, &first));
337:     } else if (ispop) {
338:       PetscCall(PetscOptionsPrefixPop(options));
339:       PetscCall(PetscTokenFind(token, &first));
340:     } else {
341:       PetscCall(PetscTokenFind(token, &second));
342:       PetscCall(PetscOptionsValidKey(second, &key));
343:       if (!key) {
344:         PetscCall(PetscOptionsSetValue_Private(options, first, second, NULL, source));
345:         PetscCall(PetscTokenFind(token, &first));
346:       } else {
347:         PetscCall(PetscOptionsSetValue_Private(options, first, NULL, NULL, source));
348:         first = second;
349:       }
350:     }
351:   }
352:   PetscCall(PetscTokenDestroy(&token));
353:   PetscFunctionReturn(PETSC_SUCCESS);
354: }

356: /*@
357:   PetscOptionsInsertString - Inserts options into the database from a string

359:   Logically Collective

361:   Input Parameters:
362: + options - options object
363: - in_str  - string that contains options separated by blanks

365:   Level: intermediate

367:   The collectivity of this routine is complex; only the MPI processes that call this routine will
368:   have the affect of these options. If some processes that create objects call this routine and others do
369:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
370:   on different ranks.

372:    Contributed by Boyana Norris

374: .seealso: `PetscOptionsSetValue()`, `PetscOptionsView()`, `PetscOptionsHasName()`, `PetscOptionsGetInt()`,
375:           `PetscOptionsGetReal()`, `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsBool()`,
376:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
377:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
378:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
379:           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsInsertFile()`
380: @*/
381: PetscErrorCode PetscOptionsInsertString(PetscOptions options, const char in_str[])
382: {
383:   PetscFunctionBegin;
384:   PetscCall(PetscOptionsInsertString_Private(options, in_str, PETSC_OPT_CODE));
385:   PetscFunctionReturn(PETSC_SUCCESS);
386: }

388: /*
389:     Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free()
390: */
391: static char *Petscgetline(FILE *f)
392: {
393:   size_t size = 0;
394:   size_t len  = 0;
395:   size_t last = 0;
396:   char  *buf  = NULL;

398:   if (feof(f)) return NULL;
399:   do {
400:     size += 1024;                             /* BUFSIZ is defined as "the optimal read size for this platform" */
401:     buf = (char *)realloc((void *)buf, size); /* realloc(NULL,n) is the same as malloc(n) */
402:     /* Actually do the read. Note that fgets puts a terminal '\0' on the
403:     end of the string, so we make sure we overwrite this */
404:     if (!fgets(buf + len, 1024, f)) buf[len] = 0;
405:     PetscCallAbort(PETSC_COMM_SELF, PetscStrlen(buf, &len));
406:     last = len - 1;
407:   } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r');
408:   if (len) return buf;
409:   free(buf);
410:   return NULL;
411: }

413: static PetscErrorCode PetscOptionsFilename(MPI_Comm comm, const char file[], char filename[PETSC_MAX_PATH_LEN], PetscBool *yaml)
414: {
415:   char fname[PETSC_MAX_PATH_LEN + 8], path[PETSC_MAX_PATH_LEN + 8], *tail;

417:   PetscFunctionBegin;
418:   *yaml = PETSC_FALSE;
419:   PetscCall(PetscStrreplace(comm, file, fname, sizeof(fname)));
420:   PetscCall(PetscFixFilename(fname, path));
421:   PetscCall(PetscStrendswith(path, ":yaml", yaml));
422:   if (*yaml) {
423:     PetscCall(PetscStrrchr(path, ':', &tail));
424:     tail[-1] = 0; /* remove ":yaml" suffix from path */
425:   }
426:   PetscCall(PetscStrncpy(filename, path, PETSC_MAX_PATH_LEN));
427:   /* check for standard YAML and JSON filename extensions */
428:   if (!*yaml) PetscCall(PetscStrendswith(filename, ".yaml", yaml));
429:   if (!*yaml) PetscCall(PetscStrendswith(filename, ".yml", yaml));
430:   if (!*yaml) PetscCall(PetscStrendswith(filename, ".json", yaml));
431:   if (!*yaml) { /* check file contents */
432:     PetscMPIInt rank;
433:     PetscCallMPI(MPI_Comm_rank(comm, &rank));
434:     if (rank == 0) {
435:       FILE *fh = fopen(filename, "r");
436:       if (fh) {
437:         char buf[6] = "";
438:         if (fread(buf, 1, 6, fh) > 0) {
439:           PetscCall(PetscStrncmp(buf, "%YAML ", 6, yaml));          /* check for '%YAML' tag */
440:           if (!*yaml) PetscCall(PetscStrncmp(buf, "---", 3, yaml)); /* check for document start */
441:         }
442:         (void)fclose(fh);
443:       }
444:     }
445:     PetscCallMPI(MPI_Bcast(yaml, 1, MPI_C_BOOL, 0, comm));
446:   }
447:   PetscFunctionReturn(PETSC_SUCCESS);
448: }

450: static PetscErrorCode PetscOptionsInsertFilePetsc(MPI_Comm comm, PetscOptions options, const char file[], PetscBool require)
451: {
452:   char       *string, *vstring = NULL, *astring = NULL, *packed = NULL;
453:   const char *tokens[4];
454:   size_t      len;
455:   PetscCount  bytes;
456:   FILE       *fd;
457:   PetscToken  token = NULL;
458:   int         err;
459:   char       *cmatch = NULL;
460:   const char  cmt    = '#';
461:   PetscInt    line   = 1;
462:   PetscMPIInt rank, cnt = 0, acnt = 0, counts[2];
463:   PetscBool   isdir, alias = PETSC_FALSE, valid;

465:   PetscFunctionBegin;
466:   PetscCall(PetscMemzero(tokens, sizeof(tokens)));
467:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
468:   if (rank == 0) {
469:     char fpath[PETSC_MAX_PATH_LEN];
470:     char fname[PETSC_MAX_PATH_LEN];

472:     PetscCall(PetscStrreplace(PETSC_COMM_SELF, file, fname, sizeof(fname)));
473:     PetscCall(PetscFixFilename(fname, fpath));
474:     PetscCall(PetscGetFullPath(fpath, fname, sizeof(fname)));

476:     fd = fopen(fname, "r");
477:     PetscCall(PetscTestDirectory(fname, 'r', &isdir));
478:     PetscCheck(!isdir || !require, PETSC_COMM_SELF, PETSC_ERR_USER, "Specified options file %s is a directory", fname);
479:     if (fd && !isdir) {
480:       PetscSegBuffer vseg, aseg;

482:       PetscCall(PetscSegBufferCreate(1, 4000, &vseg));
483:       PetscCall(PetscSegBufferCreate(1, 2000, &aseg));

485:       /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */
486:       PetscCall(PetscInfo(NULL, "Opened options file %s\n", file));

488:       while ((string = Petscgetline(fd))) {
489:         /* eliminate comments from each line */
490:         PetscCall(PetscStrchr(string, cmt, &cmatch));
491:         if (cmatch) *cmatch = 0;
492:         PetscCall(PetscStrlen(string, &len));
493:         /* replace tabs, ^M, \n with " " */
494:         for (size_t i = 0; i < len; i++) {
495:           if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') string[i] = ' ';
496:         }
497:         PetscCall(PetscTokenCreate(string, ' ', &token));
498:         PetscCall(PetscTokenFind(token, &tokens[0]));
499:         if (!tokens[0]) {
500:           goto destroy;
501:         } else if (!tokens[0][0]) { /* if token 0 is empty (string begins with spaces), redo */
502:           PetscCall(PetscTokenFind(token, &tokens[0]));
503:         }
504:         for (PetscInt i = 1; i < 4; i++) PetscCall(PetscTokenFind(token, &tokens[i]));
505:         if (!tokens[0]) {
506:           goto destroy;
507:         } else if (tokens[0][0] == '-') {
508:           PetscCall(PetscOptionsValidKey(tokens[0], &valid));
509:           PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": invalid option %s", fname, line, tokens[0]);
510:           PetscCall(PetscStrlen(tokens[0], &len));
511:           PetscCall(PetscSegBufferGet(vseg, len + 1, &vstring));
512:           PetscCall(PetscArraycpy(vstring, tokens[0], len));
513:           vstring[len] = ' ';
514:           if (tokens[1]) {
515:             PetscCall(PetscOptionsValidKey(tokens[1], &valid));
516:             PetscCheck(!valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": cannot specify two options per line (%s %s)", fname, line, tokens[0], tokens[1]);
517:             PetscCall(PetscStrlen(tokens[1], &len));
518:             PetscCall(PetscSegBufferGet(vseg, len + 3, &vstring));
519:             vstring[0] = '"';
520:             PetscCall(PetscArraycpy(vstring + 1, tokens[1], len));
521:             vstring[len + 1] = '"';
522:             vstring[len + 2] = ' ';
523:           }
524:         } else {
525:           PetscCall(PetscStrcasecmp(tokens[0], "alias", &alias));
526:           PetscCheck(alias, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown first token in options file %s line %" PetscInt_FMT ": %s", fname, line, tokens[0]);
527:           PetscCall(PetscOptionsValidKey(tokens[1], &valid));
528:           PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": invalid aliased option %s", fname, line, tokens[1]);
529:           PetscCheck(tokens[2], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": alias missing for %s", fname, line, tokens[1]);
530:           PetscCall(PetscOptionsValidKey(tokens[2], &valid));
531:           PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": invalid aliasee option %s", fname, line, tokens[2]);
532:           PetscCall(PetscStrlen(tokens[1], &len));
533:           PetscCall(PetscSegBufferGet(aseg, len + 1, &astring));
534:           PetscCall(PetscArraycpy(astring, tokens[1], len));
535:           astring[len] = ' ';

537:           PetscCall(PetscStrlen(tokens[2], &len));
538:           PetscCall(PetscSegBufferGet(aseg, len + 1, &astring));
539:           PetscCall(PetscArraycpy(astring, tokens[2], len));
540:           astring[len] = ' ';
541:         }
542:         {
543:           const char *extraToken = alias ? tokens[3] : tokens[2];
544:           PetscCheck(!extraToken, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Error in options file %s line %" PetscInt_FMT ": extra token %s", fname, line, extraToken);
545:         }
546:       destroy:
547:         free(string);
548:         PetscCall(PetscTokenDestroy(&token));
549:         alias = PETSC_FALSE;
550:         line++;
551:       }
552:       err = fclose(fd);
553:       PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file %s", fname);
554:       PetscCall(PetscSegBufferGetSize(aseg, &bytes)); /* size without null termination */
555:       PetscCall(PetscMPIIntCast(bytes, &acnt));
556:       PetscCall(PetscSegBufferGet(aseg, 1, &astring));
557:       astring[0] = 0;
558:       PetscCall(PetscSegBufferGetSize(vseg, &bytes)); /* size without null termination */
559:       PetscCall(PetscMPIIntCast(bytes, &cnt));
560:       PetscCall(PetscSegBufferGet(vseg, 1, &vstring));
561:       vstring[0] = 0;
562:       PetscCall(PetscMalloc1(2 + acnt + cnt, &packed));
563:       PetscCall(PetscSegBufferExtractTo(aseg, packed));
564:       PetscCall(PetscSegBufferExtractTo(vseg, packed + acnt + 1));
565:       PetscCall(PetscSegBufferDestroy(&aseg));
566:       PetscCall(PetscSegBufferDestroy(&vseg));
567:     } else PetscCheck(!require, PETSC_COMM_SELF, PETSC_ERR_USER, "Unable to open options file %s", fname);
568:   }

570:   counts[0] = acnt;
571:   counts[1] = cnt;
572:   err       = MPI_Bcast(counts, 2, MPI_INT, 0, comm);
573:   PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in first MPI collective call, could be caused by using an incorrect mpiexec or a network problem, it can be caused by having VPN running: see https://petsc.org/release/faq/");
574:   acnt = counts[0];
575:   cnt  = counts[1];
576:   if (rank) PetscCall(PetscMalloc1(2 + acnt + cnt, &packed));
577:   if (acnt || cnt) {
578:     PetscCallMPI(MPI_Bcast(packed, 2 + acnt + cnt, MPI_CHAR, 0, comm));
579:     astring = packed;
580:     vstring = packed + acnt + 1;
581:   }

583:   if (acnt) {
584:     PetscCall(PetscTokenCreate(astring, ' ', &token));
585:     PetscCall(PetscTokenFind(token, &tokens[0]));
586:     while (tokens[0]) {
587:       PetscCall(PetscTokenFind(token, &tokens[1]));
588:       PetscCall(PetscOptionsSetAlias(options, tokens[0], tokens[1]));
589:       PetscCall(PetscTokenFind(token, &tokens[0]));
590:     }
591:     PetscCall(PetscTokenDestroy(&token));
592:   }

594:   if (cnt) PetscCall(PetscOptionsInsertString_Private(options, vstring, PETSC_OPT_FILE));
595:   PetscCall(PetscFree(packed));
596:   PetscFunctionReturn(PETSC_SUCCESS);
597: }

599: /*@
600:   PetscOptionsInsertFile - Inserts options into the database from a file.

602:   Collective

604:   Input Parameters:
605: + comm    - the processes that will share the options (usually `PETSC_COMM_WORLD`)
606: . options - options database, use `NULL` for default global database
607: . file    - name of file,
608:            ".yml" and ".yaml" filename extensions are inserted as YAML options,
609:            append ":yaml" to filename to force YAML options.
610: - require - if `PETSC_TRUE` will generate an error if the file does not exist

612:   Level: developer

614:   Notes:
615:   Use  # for lines that are comments and which should be ignored.
616:   Usually, instead of using this command, one should list the file name in the call to `PetscInitialize()`, this insures that certain options
617:   such as `-log_view` or `-malloc_debug` are processed properly. This routine only sets options into the options database that will be processed by later
618:   calls to `XXXSetFromOptions()`, it should not be used for options listed under PetscInitialize().
619:   The collectivity of this routine is complex; only the MPI processes in comm will
620:   have the effect of these options. If some processes that create objects call this routine and others do
621:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
622:   on different ranks.

624: .seealso: `PetscOptionsSetValue()`, `PetscOptionsView()`, `PetscOptionsHasName()`, `PetscOptionsGetInt()`,
625:           `PetscOptionsGetReal()`, `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsBool()`,
626:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
627:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
628:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
629:           `PetscOptionsFList()`, `PetscOptionsEList()`
630: @*/
631: PetscErrorCode PetscOptionsInsertFile(MPI_Comm comm, PetscOptions options, const char file[], PetscBool require)
632: {
633:   char      filename[PETSC_MAX_PATH_LEN];
634:   PetscBool yaml;

636:   PetscFunctionBegin;
637:   PetscCall(PetscOptionsFilename(comm, file, filename, &yaml));
638:   if (yaml) {
639:     PetscCall(PetscOptionsInsertFileYAML(comm, options, filename, require));
640:   } else {
641:     PetscCall(PetscOptionsInsertFilePetsc(comm, options, filename, require));
642:   }
643:   PetscFunctionReturn(PETSC_SUCCESS);
644: }

646: /*@C
647:   PetscOptionsInsertArgs - Inserts options into the database from a array of strings

649:   Logically Collective

651:   Input Parameters:
652: + options - options object
653: . argc    - the array length
654: - args    - the string array

656:   Level: intermediate

658: .seealso: `PetscOptions`, `PetscOptionsInsertString()`, `PetscOptionsInsertFile()`
659: @*/
660: PetscErrorCode PetscOptionsInsertArgs(PetscOptions options, int argc, const char *const args[])
661: {
662:   int                left  = PetscMax(argc, 0);
663:   const char *const *eargs = args;

665:   PetscFunctionBegin;
666:   while (left) {
667:     PetscBool isfile, isfileyaml, isstringyaml, ispush, ispop, key;
668:     PetscCall(PetscStrcasecmp(eargs[0], "-options_file", &isfile));
669:     PetscCall(PetscStrcasecmp(eargs[0], "-options_file_yaml", &isfileyaml));
670:     PetscCall(PetscStrcasecmp(eargs[0], "-options_string_yaml", &isstringyaml));
671:     PetscCall(PetscStrcasecmp(eargs[0], "-prefix_push", &ispush));
672:     PetscCall(PetscStrcasecmp(eargs[0], "-prefix_pop", &ispop));
673:     PetscCall(PetscOptionsValidKey(eargs[0], &key));
674:     if (!key) {
675:       eargs++;
676:       left--;
677:     } else if (isfile) {
678:       PetscCheck(left > 1 && eargs[1][0] != '-', PETSC_COMM_SELF, PETSC_ERR_USER, "Missing filename for -options_file filename option");
679:       PetscCall(PetscOptionsInsertFile(PETSC_COMM_WORLD, options, eargs[1], PETSC_TRUE));
680:       eargs += 2;
681:       left -= 2;
682:     } else if (isfileyaml) {
683:       PetscCheck(left > 1 && eargs[1][0] != '-', PETSC_COMM_SELF, PETSC_ERR_USER, "Missing filename for -options_file_yaml filename option");
684:       PetscCall(PetscOptionsInsertFileYAML(PETSC_COMM_WORLD, options, eargs[1], PETSC_TRUE));
685:       eargs += 2;
686:       left -= 2;
687:     } else if (isstringyaml) {
688:       PetscCheck(left > 1 && eargs[1][0] != '-', PETSC_COMM_SELF, PETSC_ERR_USER, "Missing string for -options_string_yaml string option");
689:       PetscCall(PetscOptionsInsertStringYAML_Private(options, eargs[1], PETSC_OPT_CODE));
690:       eargs += 2;
691:       left -= 2;
692:     } else if (ispush) {
693:       PetscCheck(left > 1, PETSC_COMM_SELF, PETSC_ERR_USER, "Missing prefix for -prefix_push option");
694:       PetscCheck(eargs[1][0] != '-', PETSC_COMM_SELF, PETSC_ERR_USER, "Missing prefix for -prefix_push option (prefixes cannot start with '-')");
695:       PetscCall(PetscOptionsPrefixPush(options, eargs[1]));
696:       eargs += 2;
697:       left -= 2;
698:     } else if (ispop) {
699:       PetscCall(PetscOptionsPrefixPop(options));
700:       eargs++;
701:       left--;
702:     } else {
703:       PetscBool nextiskey = PETSC_FALSE;
704:       if (left >= 2) PetscCall(PetscOptionsValidKey(eargs[1], &nextiskey));
705:       if (left < 2 || nextiskey) {
706:         PetscCall(PetscOptionsSetValue_Private(options, eargs[0], NULL, NULL, PETSC_OPT_COMMAND_LINE));
707:         eargs++;
708:         left--;
709:       } else {
710:         PetscCall(PetscOptionsSetValue_Private(options, eargs[0], eargs[1], NULL, PETSC_OPT_COMMAND_LINE));
711:         eargs += 2;
712:         left -= 2;
713:       }
714:     }
715:   }
716:   PetscFunctionReturn(PETSC_SUCCESS);
717: }

719: static inline PetscErrorCode PetscOptionsStringToBoolIfSet_Private(enum PetscPrecedentOption opt, const char *val[], const PetscBool set[], PetscBool *flg)
720: {
721:   PetscFunctionBegin;
722:   if (set[opt]) PetscCall(PetscOptionsStringToBool(val[opt], flg));
723:   else *flg = PETSC_FALSE;
724:   PetscFunctionReturn(PETSC_SUCCESS);
725: }

727: /* Process options with absolute precedence, these are only processed from the command line, not the environment or files */
728: static PetscErrorCode PetscOptionsProcessPrecedentFlags(PetscOptions options, int argc, char *args[], PetscBool *skip_petscrc, PetscBool *skip_petscrc_set)
729: {
730:   const char *const *opt = precedentOptions;
731:   const size_t       n   = PO_NUM;
732:   size_t             o;
733:   int                a;
734:   const char       **val;
735:   char             **cval;
736:   PetscBool         *set, unneeded;

738:   PetscFunctionBegin;
739:   PetscCall(PetscCalloc2(n, &cval, n, &set));
740:   val = (const char **)cval;

742:   /* Look for options possibly set using PetscOptionsSetValue beforehand */
743:   for (o = 0; o < n; o++) PetscCall(PetscOptionsFindPair(options, NULL, opt[o], &val[o], &set[o]));

745:   /* Loop through all args to collect last occurring value of each option */
746:   for (a = 1; a < argc; a++) {
747:     PetscBool valid, eq;

749:     PetscCall(PetscOptionsValidKey(args[a], &valid));
750:     if (!valid) continue;
751:     for (o = 0; o < n; o++) {
752:       PetscCall(PetscStrcasecmp(args[a], opt[o], &eq));
753:       if (eq) {
754:         set[o] = PETSC_TRUE;
755:         if (a == argc - 1 || !args[a + 1] || !args[a + 1][0] || args[a + 1][0] == '-') val[o] = NULL;
756:         else val[o] = args[a + 1];
757:         break;
758:       }
759:     }
760:   }

762:   /* Process flags */
763:   PetscCall(PetscStrcasecmp(val[PO_HELP], "intro", &options->help_intro));
764:   if (options->help_intro) options->help = PETSC_TRUE;
765:   else PetscCall(PetscOptionsStringToBoolIfSet_Private(PO_HELP, val, set, &options->help));
766:   PetscCall(PetscOptionsStringToBoolIfSet_Private(PO_CI_ENABLE, val, set, &unneeded));
767:   /* need to manage PO_CI_ENABLE option before the PetscOptionsMonitor is turned on, so its setting is not monitored */
768:   if (set[PO_CI_ENABLE]) PetscCall(PetscOptionsSetValue_Private(options, opt[PO_CI_ENABLE], val[PO_CI_ENABLE], &a, PETSC_OPT_COMMAND_LINE));
769:   PetscCall(PetscOptionsStringToBoolIfSet_Private(PO_OPTIONS_MONITOR_CANCEL, val, set, &options->monitorCancel));
770:   PetscCall(PetscOptionsStringToBoolIfSet_Private(PO_OPTIONS_MONITOR, val, set, &options->monitorFromOptions));
771:   PetscCall(PetscOptionsStringToBoolIfSet_Private(PO_SKIP_PETSCRC, val, set, skip_petscrc));
772:   *skip_petscrc_set = set[PO_SKIP_PETSCRC];

774:   /* Store precedent options in database and mark them as used */
775:   for (o = 1; o < n; o++) {
776:     if (set[o]) {
777:       PetscCall(PetscOptionsSetValue_Private(options, opt[o], val[o], &a, PETSC_OPT_COMMAND_LINE));
778:       options->used[a] = PETSC_TRUE;
779:     }
780:   }
781:   PetscCall(PetscFree2(cval, set));
782:   options->precedentProcessed = PETSC_TRUE;
783:   PetscFunctionReturn(PETSC_SUCCESS);
784: }

786: static inline PetscErrorCode PetscOptionsSkipPrecedent(PetscOptions options, const char name[], PetscBool *flg)
787: {
788:   PetscFunctionBegin;
789:   PetscAssertPointer(flg, 3);
790:   *flg = PETSC_FALSE;
791:   if (options->precedentProcessed) {
792:     for (int i = 0; i < PO_NUM; ++i) {
793:       if (!PetscOptNameCmp(precedentOptions[i], name)) {
794:         /* check if precedent option has been set already */
795:         PetscCall(PetscOptionsFindPair(options, NULL, name, NULL, flg));
796:         if (*flg) break;
797:       }
798:     }
799:   }
800:   PetscFunctionReturn(PETSC_SUCCESS);
801: }

803: /*@C
804:   PetscOptionsInsert - Inserts into the options database from the command line,
805:   the environmental variable and a file.

807:   Collective on `PETSC_COMM_WORLD`

809:   Input Parameters:
810: + options - options database or `NULL` for the default global database
811: . argc    - count of number of command line arguments
812: . args    - the command line arguments
813: - file    - [optional] PETSc database file, append ":yaml" to filename to specify YAML options format.
814:             Use `NULL` or empty string to not check for code specific file.
815:             Also checks ~/.petscrc, .petscrc and petscrc.
816:             Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files.

818:   Options Database Keys:
819: + -options_file filename      - read options from a file
820: - -options_file_yaml filename - read options from a YAML file

822:   Level: advanced

824:   Notes:
825:   Since `PetscOptionsInsert()` is automatically called by `PetscInitialize()`,
826:   the user does not typically need to call this routine. `PetscOptionsInsert()`
827:   can be called several times, adding additional entries into the database.

829:   See `PetscInitialize()` for options related to option database monitoring.

831: .seealso: `PetscOptionsDestroy()`, `PetscOptionsView()`, `PetscOptionsInsertString()`, `PetscOptionsInsertFile()`,
832:           `PetscInitialize()`
833: @*/
834: PetscErrorCode PetscOptionsInsert(PetscOptions options, int *argc, char ***args, const char file[]) PeNS
835: {
836:   PetscMPIInt rank;
837:   PetscBool   hasArgs     = (argc && *argc) ? PETSC_TRUE : PETSC_FALSE;
838:   PetscBool   skipPetscrc = PETSC_FALSE, skipPetscrcSet = PETSC_FALSE;
839:   char       *eoptions = NULL;
840:   size_t      len      = 0;

842:   PetscFunctionBegin;
843:   PetscCheck(!hasArgs || (args && *args), PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL, "*argc > 1 but *args not given");
844:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

846:   if (!options) {
847:     PetscCall(PetscOptionsCreateDefault());
848:     options = defaultoptions;
849:   }
850:   if (hasArgs) {
851:     /* process options with absolute precedence */
852:     PetscCall(PetscOptionsProcessPrecedentFlags(options, *argc, *args, &skipPetscrc, &skipPetscrcSet));
853:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_ci", &PetscCIEnabled, NULL));
854:   }
855:   if (file && file[0]) {
856:     PetscCall(PetscOptionsInsertFile(PETSC_COMM_WORLD, options, file, PETSC_TRUE));
857:     /* if -skip_petscrc has not been set from command line, check whether it has been set in the file */
858:     if (!skipPetscrcSet) PetscCall(PetscOptionsGetBool(options, NULL, "-skip_petscrc", &skipPetscrc, NULL));
859:   }
860:   if (!skipPetscrc) {
861:     char filename[PETSC_MAX_PATH_LEN];

863:     PetscCall(PetscGetHomeDirectory(filename, sizeof(filename)));
864:     PetscCallMPI(MPI_Bcast(filename, (int)sizeof(filename), MPI_CHAR, 0, PETSC_COMM_WORLD));
865:     if (filename[0]) PetscCall(PetscStrlcat(filename, "/.petscrc", sizeof(filename)));
866:     PetscCall(PetscOptionsInsertFile(PETSC_COMM_WORLD, options, filename, PETSC_FALSE));
867:     PetscCall(PetscOptionsInsertFile(PETSC_COMM_WORLD, options, ".petscrc", PETSC_FALSE));
868:     PetscCall(PetscOptionsInsertFile(PETSC_COMM_WORLD, options, "petscrc", PETSC_FALSE));
869:   }

871:   /* insert environment options */
872:   if (rank == 0) {
873:     eoptions = getenv("PETSC_OPTIONS");
874:     PetscCall(PetscStrlen(eoptions, &len));
875:   }
876:   PetscCallMPI(MPI_Bcast(&len, 1, MPIU_SIZE_T, 0, PETSC_COMM_WORLD));
877:   if (len) {
878:     if (rank) PetscCall(PetscMalloc1(len + 1, &eoptions));
879:     PetscCallMPI(MPI_Bcast(eoptions, (PetscMPIInt)len, MPI_CHAR, 0, PETSC_COMM_WORLD));
880:     if (rank) eoptions[len] = 0;
881:     PetscCall(PetscOptionsInsertString_Private(options, eoptions, PETSC_OPT_ENVIRONMENT));
882:     if (rank) PetscCall(PetscFree(eoptions));
883:   }

885:   /* insert YAML environment options */
886:   if (rank == 0) {
887:     eoptions = getenv("PETSC_OPTIONS_YAML");
888:     PetscCall(PetscStrlen(eoptions, &len));
889:   }
890:   PetscCallMPI(MPI_Bcast(&len, 1, MPIU_SIZE_T, 0, PETSC_COMM_WORLD));
891:   if (len) {
892:     if (rank) PetscCall(PetscMalloc1(len + 1, &eoptions));
893:     PetscCallMPI(MPI_Bcast(eoptions, (PetscMPIInt)len, MPI_CHAR, 0, PETSC_COMM_WORLD));
894:     if (rank) eoptions[len] = 0;
895:     PetscCall(PetscOptionsInsertStringYAML_Private(options, eoptions, PETSC_OPT_ENVIRONMENT));
896:     if (rank) PetscCall(PetscFree(eoptions));
897:   }

899:   /* insert command line options here because they take precedence over arguments in petscrc/environment */
900:   if (hasArgs) PetscCall(PetscOptionsInsertArgs(options, *argc - 1, (const char *const *)*args + 1));
901:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-petsc_ci_portable_error_output", &PetscCIEnabledPortableErrorOutput, NULL));
902:   PetscFunctionReturn(PETSC_SUCCESS);
903: }

905: /* These options are not printed with PetscOptionsView() or PetscOptionsMonitor() when PetscCIEnabled is on */
906: /* TODO: get the list from the test harness, do not have it hardwired here. Maybe from gmakegentest.py */
907: static const char *PetscCIOptions[] = {"malloc_debug", "malloc_dump", "malloc_test", "malloc", "nox", "nox_warning", "display", "saws_port_auto_select", "saws_port_auto_select_silent", "vecscatter_mpi1", "check_pointer_intensity", "cuda_initialize", "error_output_stdout", "use_gpu_aware_mpi", "checkfunctionlist", "fp_trap", "petsc_ci", "petsc_ci_portable_error_output", "options_left"};

909: static PetscBool PetscCIOption(const char *name)
910: {
911:   PetscInt  idx;
912:   PetscBool found;

914:   if (!PetscCIEnabled) return PETSC_FALSE;
915:   PetscCallAbort(PETSC_COMM_SELF, PetscEListFind(PETSC_STATIC_ARRAY_LENGTH(PetscCIOptions), PetscCIOptions, name, &idx, &found));
916:   return found;
917: }

919: /*@
920:   PetscOptionsView - Prints the options that have been loaded. This is
921:   useful for debugging purposes.

923:   Logically Collective, No Fortran Support

925:   Input Parameters:
926: + options - options database, use `NULL` for default global database
927: - viewer  - must be an `PETSCVIEWERASCII` viewer

929:   Options Database Key:
930: . -options_view - Activates `PetscOptionsView()` within `PetscFinalize()`

932:   Level: advanced

934:   Note:
935:   Only the MPI rank 0 of the `MPI_Comm` used to create view prints the option values. Other processes
936:   may have different values but they are not printed.

938: .seealso: `PetscOptionsAllUsed()`
939: @*/
940: PetscErrorCode PetscOptionsView(PetscOptions options, PetscViewer viewer)
941: {
942:   PetscInt  i, N = 0;
943:   PetscBool isascii;

945:   PetscFunctionBegin;
947:   options = options ? options : defaultoptions;
948:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
949:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
950:   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Only supports ASCII viewer");

952:   for (i = 0; i < options->N; i++) {
953:     if (PetscCIOption(options->names[i])) continue;
954:     N++;
955:   }

957:   if (!N) {
958:     PetscCall(PetscViewerASCIIPrintf(viewer, "#No PETSc Option Table entries\n"));
959:     PetscFunctionReturn(PETSC_SUCCESS);
960:   }

962:   PetscCall(PetscViewerASCIIPrintf(viewer, "#PETSc Option Table entries:\n"));
963:   for (i = 0; i < options->N; i++) {
964:     if (PetscCIOption(options->names[i])) continue;
965:     if (options->values[i]) {
966:       PetscCall(PetscViewerASCIIPrintf(viewer, "-%s %s", options->names[i], options->values[i]));
967:     } else {
968:       PetscCall(PetscViewerASCIIPrintf(viewer, "-%s", options->names[i]));
969:     }
970:     PetscCall(PetscViewerASCIIPrintf(viewer, " # (source: %s)\n", PetscOptionSources[options->source[i]]));
971:   }
972:   PetscCall(PetscViewerASCIIPrintf(viewer, "#End of PETSc Option Table entries\n"));
973:   PetscFunctionReturn(PETSC_SUCCESS);
974: }

976: /*
977:    Called by error handlers to print options used in run
978: */
979: PetscErrorCode PetscOptionsLeftError(void)
980: {
981:   PetscInt i, nopt = 0;

983:   for (i = 0; i < defaultoptions->N; i++) {
984:     if (!defaultoptions->used[i]) {
985:       if (PetscCIOption(defaultoptions->names[i])) continue;
986:       nopt++;
987:     }
988:   }
989:   if (nopt) {
990:     PetscCall((*PetscErrorPrintf)("WARNING! There are unused option(s) set! Could be the program crashed before usage or a spelling mistake, etc!\n"));
991:     for (i = 0; i < defaultoptions->N; i++) {
992:       if (!defaultoptions->used[i]) {
993:         if (PetscCIOption(defaultoptions->names[i])) continue;
994:         if (defaultoptions->values[i]) PetscCall((*PetscErrorPrintf)("  Option left: name:-%s value: %s source: %s\n", defaultoptions->names[i], defaultoptions->values[i], PetscOptionSources[defaultoptions->source[i]]));
995:         else PetscCall((*PetscErrorPrintf)("  Option left: name:-%s (no value) source: %s\n", defaultoptions->names[i], PetscOptionSources[defaultoptions->source[i]]));
996:       }
997:     }
998:   }
999:   return PETSC_SUCCESS;
1000: }

1002: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void)
1003: {
1004:   PetscInt     i, N = 0;
1005:   PetscOptions options = defaultoptions;

1007:   for (i = 0; i < options->N; i++) {
1008:     if (PetscCIOption(options->names[i])) continue;
1009:     N++;
1010:   }

1012:   if (N) {
1013:     PetscCall((*PetscErrorPrintf)("PETSc Option Table entries:\n"));
1014:   } else {
1015:     PetscCall((*PetscErrorPrintf)("No PETSc Option Table entries\n"));
1016:   }
1017:   for (i = 0; i < options->N; i++) {
1018:     if (PetscCIOption(options->names[i])) continue;
1019:     if (options->values[i]) {
1020:       PetscCall((*PetscErrorPrintf)("-%s %s (source: %s)\n", options->names[i], options->values[i], PetscOptionSources[options->source[i]]));
1021:     } else {
1022:       PetscCall((*PetscErrorPrintf)("-%s (source: %s)\n", options->names[i], PetscOptionSources[options->source[i]]));
1023:     }
1024:   }
1025:   return PETSC_SUCCESS;
1026: }

1028: /*@
1029:   PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow.

1031:   Logically Collective

1033:   Input Parameters:
1034: + options - options database, or `NULL` for the default global database
1035: - prefix  - The string to append to the existing prefix

1037:   Options Database Keys:
1038: + -prefix_push some_prefix_ - push the given prefix
1039: - -prefix_pop               - pop the last prefix

1041:   Level: advanced

1043:   Notes:
1044:   It is common to use this in conjunction with `-options_file` as in
1045: .vb
1046:  -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop
1047: .ve
1048:   where the files no longer require all options to be prefixed with `-system2_`.

1050:   The collectivity of this routine is complex; only the MPI processes that call this routine will
1051:   have the affect of these options. If some processes that create objects call this routine and others do
1052:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
1053:   on different ranks.

1055: .seealso: `PetscOptionsPrefixPop()`, `PetscOptionsPush()`, `PetscOptionsPop()`, `PetscOptionsCreate()`, `PetscOptionsSetValue()`
1056: @*/
1057: PetscErrorCode PetscOptionsPrefixPush(PetscOptions options, const char prefix[])
1058: {
1059:   size_t    n;
1060:   PetscInt  start;
1061:   char      key[PETSC_MAX_OPTION_NAME + 1];
1062:   PetscBool valid;

1064:   PetscFunctionBegin;
1065:   PetscAssertPointer(prefix, 2);
1066:   options = options ? options : defaultoptions;
1067:   PetscCheck(options->prefixind < MAXPREFIXES, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Maximum depth of prefix stack %d exceeded, recompile src/sys/objects/options.c with larger value for MAXPREFIXES", MAXPREFIXES);
1068:   key[0] = '-'; /* keys must start with '-' */
1069:   PetscCall(PetscStrncpy(key + 1, prefix, sizeof(key) - 1));
1070:   PetscCall(PetscOptionsValidKey(key, &valid));
1071:   if (!valid && options->prefixind > 0 && isdigit((int)prefix[0])) valid = PETSC_TRUE; /* If the prefix stack is not empty, make numbers a valid prefix */
1072:   PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_USER, "Given prefix \"%s\" not valid (the first character must be a letter%s, do not include leading '-')", prefix, options->prefixind ? " or digit" : "");
1073:   start = options->prefixind ? options->prefixstack[options->prefixind - 1] : 0;
1074:   PetscCall(PetscStrlen(prefix, &n));
1075:   PetscCheck(n + 1 <= sizeof(options->prefix) - start, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Maximum prefix length %zu exceeded", sizeof(options->prefix));
1076:   PetscCall(PetscArraycpy(options->prefix + start, prefix, n + 1));
1077:   options->prefixstack[options->prefixind++] = (int)(start + n);
1078:   PetscFunctionReturn(PETSC_SUCCESS);
1079: }

1081: /*@
1082:   PetscOptionsPrefixPop - Remove the latest options prefix, see `PetscOptionsPrefixPush()` for details

1084:   Logically Collective on the `MPI_Comm` used when called `PetscOptionsPrefixPush()`

1086:   Input Parameter:
1087: . options - options database, or `NULL` for the default global database

1089:   Level: advanced

1091: .seealso: `PetscOptionsPrefixPush()`, `PetscOptionsPush()`, `PetscOptionsPop()`, `PetscOptionsCreate()`, `PetscOptionsSetValue()`
1092: @*/
1093: PetscErrorCode PetscOptionsPrefixPop(PetscOptions options)
1094: {
1095:   PetscInt offset;

1097:   PetscFunctionBegin;
1098:   options = options ? options : defaultoptions;
1099:   PetscCheck(options->prefixind >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "More prefixes popped than pushed");
1100:   options->prefixind--;
1101:   offset                  = options->prefixind ? options->prefixstack[options->prefixind - 1] : 0;
1102:   options->prefix[offset] = 0;
1103:   PetscFunctionReturn(PETSC_SUCCESS);
1104: }

1106: /*@
1107:   PetscOptionsClear - Removes all options form the database leaving it empty.

1109:   Logically Collective

1111:   Input Parameter:
1112: . options - options database, use `NULL` for the default global database

1114:   Level: developer

1116:   Note:
1117:   The collectivity of this routine is complex; only the MPI processes that call this routine will
1118:   have the affect of these options. If some processes that create objects call this routine and others do
1119:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
1120:   on different ranks.

1122:   Developer Note:
1123:   Uses `free()` directly because the current option values were set with `malloc()`

1125: .seealso: `PetscOptionsInsert()`
1126: @*/
1127: PetscErrorCode PetscOptionsClear(PetscOptions options)
1128: {
1129:   PetscInt i;

1131:   PetscFunctionBegin;
1132:   options = options ? options : defaultoptions;
1133:   if (!options) PetscFunctionReturn(PETSC_SUCCESS);

1135:   for (i = 0; i < options->N; i++) {
1136:     if (options->names[i]) free(options->names[i]);
1137:     if (options->values[i]) free(options->values[i]);
1138:   }
1139:   options->N = 0;
1140:   free(options->names);
1141:   free(options->values);
1142:   free(options->used);
1143:   free(options->source);
1144:   options->names  = NULL;
1145:   options->values = NULL;
1146:   options->used   = NULL;
1147:   options->source = NULL;
1148:   options->Nalloc = 0;

1150:   for (i = 0; i < options->Na; i++) {
1151:     free(options->aliases1[i]);
1152:     free(options->aliases2[i]);
1153:   }
1154:   options->Na = 0;
1155:   free(options->aliases1);
1156:   free(options->aliases2);
1157:   options->aliases1 = options->aliases2 = NULL;
1158:   options->Naalloc                      = 0;

1160:   /* destroy hash table */
1161:   kh_destroy(HO, options->ht);
1162:   options->ht = NULL;

1164:   options->prefixind  = 0;
1165:   options->prefix[0]  = 0;
1166:   options->help       = PETSC_FALSE;
1167:   options->help_intro = PETSC_FALSE;
1168:   PetscFunctionReturn(PETSC_SUCCESS);
1169: }

1171: /*@
1172:   PetscOptionsSetAlias - Makes a key and alias for another key

1174:   Logically Collective

1176:   Input Parameters:
1177: + options - options database, or `NULL` for default global database
1178: . newname - the alias
1179: - oldname - the name that alias will refer to

1181:   Level: advanced

1183:   Note:
1184:   The collectivity of this routine is complex; only the MPI processes that call this routine will
1185:   have the affect of these options. If some processes that create objects call this routine and others do
1186:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
1187:   on different ranks.

1189:   Developer Note:
1190:   Uses `malloc()` directly because PETSc may not be initialized yet.

1192: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`,
1193:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
1194:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
1195:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
1196:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
1197:           `PetscOptionsFList()`, `PetscOptionsEList()`
1198: @*/
1199: PetscErrorCode PetscOptionsSetAlias(PetscOptions options, const char newname[], const char oldname[])
1200: {
1201:   size_t    len;
1202:   PetscBool valid;

1204:   PetscFunctionBegin;
1205:   PetscAssertPointer(newname, 2);
1206:   PetscAssertPointer(oldname, 3);
1207:   options = options ? options : defaultoptions;
1208:   PetscCall(PetscOptionsValidKey(newname, &valid));
1209:   PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid aliased option %s", newname);
1210:   PetscCall(PetscOptionsValidKey(oldname, &valid));
1211:   PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid aliasee option %s", oldname);

1213:   if (options->Na == options->Naalloc) {
1214:     char **tmpA1, **tmpA2;

1216:     options->Naalloc = PetscMax(4, options->Naalloc * 2);
1217:     tmpA1            = (char **)malloc(options->Naalloc * sizeof(char *));
1218:     tmpA2            = (char **)malloc(options->Naalloc * sizeof(char *));
1219:     for (int i = 0; i < options->Na; ++i) {
1220:       tmpA1[i] = options->aliases1[i];
1221:       tmpA2[i] = options->aliases2[i];
1222:     }
1223:     free(options->aliases1);
1224:     free(options->aliases2);
1225:     options->aliases1 = tmpA1;
1226:     options->aliases2 = tmpA2;
1227:   }
1228:   newname++;
1229:   oldname++;
1230:   PetscCall(PetscStrlen(newname, &len));
1231:   options->aliases1[options->Na] = (char *)malloc((len + 1) * sizeof(char));
1232:   PetscCall(PetscStrncpy(options->aliases1[options->Na], newname, len + 1));
1233:   PetscCall(PetscStrlen(oldname, &len));
1234:   options->aliases2[options->Na] = (char *)malloc((len + 1) * sizeof(char));
1235:   PetscCall(PetscStrncpy(options->aliases2[options->Na], oldname, len + 1));
1236:   ++options->Na;
1237:   PetscFunctionReturn(PETSC_SUCCESS);
1238: }

1240: /*@
1241:   PetscOptionsSetValue - Sets an option name-value pair in the options
1242:   database, overriding whatever is already present.

1244:   Logically Collective

1246:   Input Parameters:
1247: + options - options database, use `NULL` for the default global database
1248: . name    - name of option, this SHOULD have the - prepended
1249: - value   - the option value (not used for all options, so can be `NULL`)

1251:   Level: intermediate

1253:   Note:
1254:   This function can be called BEFORE `PetscInitialize()`

1256:   The collectivity of this routine is complex; only the MPI processes that call this routine will
1257:   have the affect of these options. If some processes that create objects call this routine and others do
1258:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
1259:   on different ranks.

1261:   Developer Note:
1262:   Uses `malloc()` directly because PETSc may not be initialized yet.

1264: .seealso: `PetscOptionsInsert()`, `PetscOptionsClearValue()`
1265: @*/
1266: PetscErrorCode PetscOptionsSetValue(PetscOptions options, const char name[], const char value[])
1267: {
1268:   PetscFunctionBegin;
1269:   PetscCall(PetscOptionsSetValue_Private(options, name, value, NULL, PETSC_OPT_CODE));
1270:   PetscFunctionReturn(PETSC_SUCCESS);
1271: }

1273: PetscErrorCode PetscOptionsSetValue_Private(PetscOptions options, const char name[], const char value[], int *pos, PetscOptionSource source)
1274: {
1275:   size_t    len;
1276:   int       n, i;
1277:   char    **names;
1278:   char      fullname[PETSC_MAX_OPTION_NAME] = "";
1279:   PetscBool flg;

1281:   PetscFunctionBegin;
1282:   if (!options) {
1283:     PetscCall(PetscOptionsCreateDefault());
1284:     options = defaultoptions;
1285:   }
1286:   PetscCheck(name[0] == '-', PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "name %s must start with '-'", name);

1288:   PetscCall(PetscOptionsSkipPrecedent(options, name, &flg));
1289:   if (flg) PetscFunctionReturn(PETSC_SUCCESS);

1291:   name++; /* skip starting dash */

1293:   if (options->prefixind > 0) {
1294:     strncpy(fullname, options->prefix, sizeof(fullname));
1295:     fullname[sizeof(fullname) - 1] = 0;
1296:     strncat(fullname, name, sizeof(fullname) - strlen(fullname) - 1);
1297:     fullname[sizeof(fullname) - 1] = 0;
1298:     name                           = fullname;
1299:   }

1301:   /* check against aliases */
1302:   for (i = 0; i < options->Na; i++) {
1303:     int result = PetscOptNameCmp(options->aliases1[i], name);
1304:     if (!result) {
1305:       name = options->aliases2[i];
1306:       break;
1307:     }
1308:   }

1310:   /* slow search */
1311:   n     = options->N;
1312:   names = options->names;
1313:   for (i = 0; i < options->N; i++) {
1314:     int result = PetscOptNameCmp(names[i], name);
1315:     if (!result) {
1316:       n = i;
1317:       goto setvalue;
1318:     } else if (result > 0) {
1319:       n = i;
1320:       break;
1321:     }
1322:   }
1323:   if (options->N == options->Nalloc) {
1324:     char             **names, **values;
1325:     PetscBool         *used;
1326:     PetscOptionSource *source;

1328:     options->Nalloc = PetscMax(10, options->Nalloc * 2);
1329:     names           = (char **)malloc(options->Nalloc * sizeof(char *));
1330:     values          = (char **)malloc(options->Nalloc * sizeof(char *));
1331:     used            = (PetscBool *)malloc(options->Nalloc * sizeof(PetscBool));
1332:     source          = (PetscOptionSource *)malloc(options->Nalloc * sizeof(PetscOptionSource));
1333:     for (int i = 0; i < options->N; ++i) {
1334:       names[i]  = options->names[i];
1335:       values[i] = options->values[i];
1336:       used[i]   = options->used[i];
1337:       source[i] = options->source[i];
1338:     }
1339:     free(options->names);
1340:     free(options->values);
1341:     free(options->used);
1342:     free(options->source);
1343:     options->names  = names;
1344:     options->values = values;
1345:     options->used   = used;
1346:     options->source = source;
1347:   }

1349:   /* shift remaining values up 1 */
1350:   for (i = options->N; i > n; i--) {
1351:     options->names[i]  = options->names[i - 1];
1352:     options->values[i] = options->values[i - 1];
1353:     options->used[i]   = options->used[i - 1];
1354:     options->source[i] = options->source[i - 1];
1355:   }
1356:   options->names[n]  = NULL;
1357:   options->values[n] = NULL;
1358:   options->used[n]   = PETSC_FALSE;
1359:   options->source[n] = PETSC_OPT_CODE;
1360:   options->N++;

1362:   /* destroy hash table */
1363:   kh_destroy(HO, options->ht);
1364:   options->ht = NULL;

1366:   /* set new name */
1367:   len               = strlen(name);
1368:   options->names[n] = (char *)malloc((len + 1) * sizeof(char));
1369:   PetscCheck(options->names[n], PETSC_COMM_SELF, PETSC_ERR_MEM, "Failed to allocate option name");
1370:   strcpy(options->names[n], name);

1372: setvalue:
1373:   /* set new value */
1374:   if (options->values[n]) free(options->values[n]);
1375:   len = value ? strlen(value) : 0;
1376:   if (len) {
1377:     options->values[n] = (char *)malloc((len + 1) * sizeof(char));
1378:     if (!options->values[n]) return PETSC_ERR_MEM;
1379:     strcpy(options->values[n], value);
1380:     options->values[n][len] = '\0';
1381:   } else {
1382:     options->values[n] = NULL;
1383:   }
1384:   options->source[n] = source;

1386:   /* handle -help so that it can be set from anywhere */
1387:   if (!PetscOptNameCmp(name, "help")) {
1388:     options->help       = PETSC_TRUE;
1389:     options->help_intro = (value && !PetscOptNameCmp(value, "intro")) ? PETSC_TRUE : PETSC_FALSE;
1390:     options->used[n]    = PETSC_TRUE;
1391:   }

1393:   PetscCall(PetscOptionsMonitor(options, name, value ? value : "", source));
1394:   if (pos) *pos = n;
1395:   PetscFunctionReturn(PETSC_SUCCESS);
1396: }

1398: /*@
1399:   PetscOptionsClearValue - Clears an option name-value pair in the options
1400:   database, overriding whatever is already present.

1402:   Logically Collective

1404:   Input Parameters:
1405: + options - options database, use `NULL` for the default global database
1406: - name    - name of option, this SHOULD have the - prepended

1408:   Level: intermediate

1410:   Note:
1411:   The collectivity of this routine is complex; only the MPI processes that call this routine will
1412:   have the affect of these options. If some processes that create objects call this routine and others do
1413:   not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options
1414:   on different ranks.

1416:   Developer Note:
1417:   Uses `free()` directly because the options have been set with `malloc()`

1419: .seealso: `PetscOptionsInsert()`
1420: @*/
1421: PetscErrorCode PetscOptionsClearValue(PetscOptions options, const char name[])
1422: {
1423:   int    N, n, i;
1424:   char **names;

1426:   PetscFunctionBegin;
1427:   options = options ? options : defaultoptions;
1428:   PetscCheck(name[0] == '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Name must begin with '-': Instead %s", name);
1429:   if (!PetscOptNameCmp(name, "-help")) options->help = options->help_intro = PETSC_FALSE;

1431:   name++; /* skip starting dash */

1433:   /* slow search */
1434:   N = n = options->N;
1435:   names = options->names;
1436:   for (i = 0; i < N; i++) {
1437:     int result = PetscOptNameCmp(names[i], name);
1438:     if (!result) {
1439:       n = i;
1440:       break;
1441:     } else if (result > 0) {
1442:       n = N;
1443:       break;
1444:     }
1445:   }
1446:   if (n == N) PetscFunctionReturn(PETSC_SUCCESS); /* it was not present */

1448:   /* remove name and value */
1449:   if (options->names[n]) free(options->names[n]);
1450:   if (options->values[n]) free(options->values[n]);
1451:   /* shift remaining values down 1 */
1452:   for (i = n; i < N - 1; i++) {
1453:     options->names[i]  = options->names[i + 1];
1454:     options->values[i] = options->values[i + 1];
1455:     options->used[i]   = options->used[i + 1];
1456:     options->source[i] = options->source[i + 1];
1457:   }
1458:   options->N--;

1460:   /* destroy hash table */
1461:   kh_destroy(HO, options->ht);
1462:   options->ht = NULL;

1464:   PetscCall(PetscOptionsMonitor(options, name, NULL, PETSC_OPT_CODE));
1465:   PetscFunctionReturn(PETSC_SUCCESS);
1466: }

1468: /*@C
1469:   PetscOptionsFindPair - Gets an option name-value pair from the options database.

1471:   Not Collective

1473:   Input Parameters:
1474: + options - options database, use `NULL` for the default global database
1475: . pre     - the string to prepend to the name or `NULL`, this SHOULD NOT have the "-" prepended
1476: - name    - name of option, this SHOULD have the "-" prepended

1478:   Output Parameters:
1479: + value - the option value (optional, not used for all options)
1480: - set   - whether the option is set (optional)

1482:   Level: developer

1484:   Note:
1485:   Each process may find different values or no value depending on how options were inserted into the database

1487: .seealso: `PetscOptionsSetValue()`, `PetscOptionsClearValue()`
1488: @*/
1489: PetscErrorCode PetscOptionsFindPair(PetscOptions options, const char pre[], const char name[], const char *value[], PetscBool *set)
1490: {
1491:   char      buf[PETSC_MAX_OPTION_NAME];
1492:   PetscBool matchnumbers = PETSC_TRUE;

1494:   PetscFunctionBegin;
1495:   if (!options) {
1496:     PetscCall(PetscOptionsCreateDefault());
1497:     options = defaultoptions;
1498:   }
1499:   PetscCheck(!pre || !PetscUnlikely(pre[0] == '-'), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Prefix cannot begin with '-': Instead %s", pre);
1500:   PetscCheck(name[0] == '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Name must begin with '-': Instead %s", name);

1502:   name++; /* skip starting dash */

1504:   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1505:   if (pre && pre[0]) {
1506:     char *ptr = buf;
1507:     if (name[0] == '-') {
1508:       *ptr++ = '-';
1509:       name++;
1510:     }
1511:     PetscCall(PetscStrncpy(ptr, pre, buf + sizeof(buf) - ptr));
1512:     PetscCall(PetscStrlcat(buf, name, sizeof(buf)));
1513:     name = buf;
1514:   }

1516:   if (PetscDefined(USE_DEBUG)) {
1517:     PetscBool valid;
1518:     char      key[PETSC_MAX_OPTION_NAME + 1] = "-";
1519:     PetscCall(PetscStrncpy(key + 1, name, sizeof(key) - 1));
1520:     PetscCall(PetscOptionsValidKey(key, &valid));
1521:     PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid option '%s' obtained from pre='%s' and name='%s'", key, pre ? pre : "", name);
1522:   }

1524:   if (!options->ht) {
1525:     int          i, ret;
1526:     khiter_t     it;
1527:     khash_t(HO) *ht;
1528:     ht = kh_init(HO);
1529:     PetscCheck(ht, PETSC_COMM_SELF, PETSC_ERR_MEM, "Hash table allocation failed");
1530:     ret = kh_resize(HO, ht, options->N * 2); /* twice the required size to reduce risk of collisions */
1531:     PetscCheck(!ret, PETSC_COMM_SELF, PETSC_ERR_MEM, "Hash table allocation failed");
1532:     for (i = 0; i < options->N; i++) {
1533:       it = kh_put(HO, ht, options->names[i], &ret);
1534:       PetscCheck(ret == 1, PETSC_COMM_SELF, PETSC_ERR_MEM, "Hash table allocation failed");
1535:       kh_val(ht, it) = i;
1536:     }
1537:     options->ht = ht;
1538:   }

1540:   khash_t(HO) *ht = options->ht;
1541:   khiter_t     it = kh_get(HO, ht, name);
1542:   if (it != kh_end(ht)) {
1543:     int i            = kh_val(ht, it);
1544:     options->used[i] = PETSC_TRUE;
1545:     if (value) *value = options->values[i];
1546:     if (set) *set = PETSC_TRUE;
1547:     PetscFunctionReturn(PETSC_SUCCESS);
1548:   }

1550:   /*
1551:    The following block slows down all lookups in the most frequent path (most lookups are unsuccessful).
1552:    Maybe this special lookup mode should be enabled on request with a push/pop API.
1553:    The feature of matching _%d_ used sparingly in the codebase.
1554:    */
1555:   if (matchnumbers) {
1556:     int i, j, cnt = 0, locs[16], loce[16];
1557:     /* determine the location and number of all _%d_ in the key */
1558:     for (i = 0; name[i]; i++) {
1559:       if (name[i] == '_') {
1560:         for (j = i + 1; name[j]; j++) {
1561:           if (name[j] >= '0' && name[j] <= '9') continue;
1562:           if (name[j] == '_' && j > i + 1) { /* found a number */
1563:             locs[cnt]   = i + 1;
1564:             loce[cnt++] = j + 1;
1565:           }
1566:           i = j - 1;
1567:           break;
1568:         }
1569:       }
1570:     }
1571:     for (i = 0; i < cnt; i++) {
1572:       PetscBool found;
1573:       char      opt[PETSC_MAX_OPTION_NAME + 1] = "-", tmp[PETSC_MAX_OPTION_NAME];
1574:       PetscCall(PetscStrncpy(tmp, name, PetscMin((size_t)(locs[i] + 1), sizeof(tmp))));
1575:       PetscCall(PetscStrlcat(opt, tmp, sizeof(opt)));
1576:       PetscCall(PetscStrlcat(opt, name + loce[i], sizeof(opt)));
1577:       PetscCall(PetscOptionsFindPair(options, NULL, opt, value, &found));
1578:       if (found) {
1579:         if (set) *set = PETSC_TRUE;
1580:         PetscFunctionReturn(PETSC_SUCCESS);
1581:       }
1582:     }
1583:   }

1585:   if (set) *set = PETSC_FALSE;
1586:   PetscFunctionReturn(PETSC_SUCCESS);
1587: }

1589: /* Check whether any option begins with pre+name */
1590: PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions options, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *set)
1591: {
1592:   char buf[PETSC_MAX_OPTION_NAME];
1593:   int  numCnt = 0, locs[16], loce[16];

1595:   PetscFunctionBegin;
1596:   options = options ? options : defaultoptions;
1597:   PetscCheck(!pre || pre[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Prefix cannot begin with '-': Instead %s", pre);
1598:   PetscCheck(name[0] == '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Name must begin with '-': Instead %s", name);

1600:   name++; /* skip starting dash */

1602:   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1603:   if (pre && pre[0]) {
1604:     char *ptr = buf;
1605:     if (name[0] == '-') {
1606:       *ptr++ = '-';
1607:       name++;
1608:     }
1609:     PetscCall(PetscStrncpy(ptr, pre, sizeof(buf) - ((ptr == buf) ? 0 : 1)));
1610:     PetscCall(PetscStrlcat(buf, name, sizeof(buf)));
1611:     name = buf;
1612:   }

1614:   if (PetscDefined(USE_DEBUG)) {
1615:     PetscBool valid;
1616:     char      key[PETSC_MAX_OPTION_NAME + 1] = "-";
1617:     PetscCall(PetscStrncpy(key + 1, name, sizeof(key) - 1));
1618:     PetscCall(PetscOptionsValidKey(key, &valid));
1619:     PetscCheck(valid, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid option '%s' obtained from pre='%s' and name='%s'", key, pre ? pre : "", name);
1620:   }

1622:   /* determine the location and number of all _%d_ in the key */
1623:   {
1624:     int i, j;
1625:     for (i = 0; name[i]; i++) {
1626:       if (name[i] == '_') {
1627:         for (j = i + 1; name[j]; j++) {
1628:           if (name[j] >= '0' && name[j] <= '9') continue;
1629:           if (name[j] == '_' && j > i + 1) { /* found a number */
1630:             locs[numCnt]   = i + 1;
1631:             loce[numCnt++] = j + 1;
1632:           }
1633:           i = j - 1;
1634:           break;
1635:         }
1636:       }
1637:     }
1638:   }

1640:   /* slow search */
1641:   for (int c = -1; c < numCnt; ++c) {
1642:     char   opt[PETSC_MAX_OPTION_NAME + 2] = "";
1643:     size_t len;

1645:     if (c < 0) {
1646:       PetscCall(PetscStrncpy(opt, name, sizeof(opt)));
1647:     } else {
1648:       PetscCall(PetscStrncpy(opt, name, PetscMin((size_t)(locs[c] + 1), sizeof(opt))));
1649:       PetscCall(PetscStrlcat(opt, name + loce[c], sizeof(opt) - 1));
1650:     }
1651:     PetscCall(PetscStrlen(opt, &len));
1652:     for (int i = 0; i < options->N; i++) {
1653:       PetscBool match;

1655:       PetscCall(PetscStrncmp(options->names[i], opt, len, &match));
1656:       if (match) {
1657:         options->used[i] = PETSC_TRUE;
1658:         if (option) *option = options->names[i];
1659:         if (value) *value = options->values[i];
1660:         if (set) *set = PETSC_TRUE;
1661:         PetscFunctionReturn(PETSC_SUCCESS);
1662:       }
1663:     }
1664:   }

1666:   if (set) *set = PETSC_FALSE;
1667:   PetscFunctionReturn(PETSC_SUCCESS);
1668: }

1670: /*@
1671:   PetscOptionsReject - Generates an error if a certain option is given.

1673:   Not Collective

1675:   Input Parameters:
1676: + options - options database, use `NULL` for default global database
1677: . pre     - the option prefix (may be `NULL`)
1678: . name    - the option name one is seeking
1679: - mess    - error message (may be `NULL`)

1681:   Level: advanced

1683: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`,
1684:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
1685:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
1686:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
1687:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
1688:           `PetscOptionsFList()`, `PetscOptionsEList()`
1689: @*/
1690: PetscErrorCode PetscOptionsReject(PetscOptions options, const char pre[], const char name[], const char mess[])
1691: {
1692:   PetscBool flag = PETSC_FALSE;

1694:   PetscFunctionBegin;
1695:   PetscCall(PetscOptionsHasName(options, pre, name, &flag));
1696:   if (flag) {
1697:     PetscCheck(!mess || !mess[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Program has disabled option: -%s%s with %s", pre ? pre : "", name + 1, mess);
1698:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Program has disabled option: -%s%s", pre ? pre : "", name + 1);
1699:   }
1700:   PetscFunctionReturn(PETSC_SUCCESS);
1701: }

1703: /*@
1704:   PetscOptionsHasHelp - Determines whether the "-help" option is in the database.

1706:   Not Collective

1708:   Input Parameter:
1709: . options - options database, use `NULL` for default global database

1711:   Output Parameter:
1712: . set - `PETSC_TRUE` if found else `PETSC_FALSE`.

1714:   Level: advanced

1716: .seealso: `PetscOptionsHasName()`
1717: @*/
1718: PetscErrorCode PetscOptionsHasHelp(PetscOptions options, PetscBool *set)
1719: {
1720:   PetscFunctionBegin;
1721:   PetscAssertPointer(set, 2);
1722:   options = options ? options : defaultoptions;
1723:   *set    = options->help;
1724:   PetscFunctionReturn(PETSC_SUCCESS);
1725: }

1727: PetscErrorCode PetscOptionsHasHelpIntro_Internal(PetscOptions options, PetscBool *set)
1728: {
1729:   PetscFunctionBegin;
1730:   PetscAssertPointer(set, 2);
1731:   options = options ? options : defaultoptions;
1732:   *set    = options->help_intro;
1733:   PetscFunctionReturn(PETSC_SUCCESS);
1734: }

1736: /*@
1737:   PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or Boolean, even
1738:   if its value is set to false.

1740:   Not Collective

1742:   Input Parameters:
1743: + options - options database, use `NULL` for default global database
1744: . pre     - string to prepend to the name or `NULL`
1745: - name    - the option one is seeking

1747:   Output Parameter:
1748: . set - `PETSC_TRUE` if found else `PETSC_FALSE`.

1750:   Level: beginner

1752:   Note:
1753:   In many cases you probably want to use `PetscOptionsGetBool()` instead of calling this, to allowing toggling values.

1755: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`,
1756:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
1757:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
1758:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
1759:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
1760:           `PetscOptionsFList()`, `PetscOptionsEList()`
1761: @*/
1762: PetscErrorCode PetscOptionsHasName(PetscOptions options, const char pre[], const char name[], PetscBool *set)
1763: {
1764:   const char *value;
1765:   PetscBool   flag;

1767:   PetscFunctionBegin;
1768:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
1769:   if (set) *set = flag;
1770:   PetscFunctionReturn(PETSC_SUCCESS);
1771: }

1773: /*@C
1774:   PetscOptionsGetAll - Lists all the options the program was run with in a single string.

1776:   Not Collective

1778:   Input Parameter:
1779: . options - the options database, use `NULL` for the default global database

1781:   Output Parameter:
1782: . copts - pointer where string pointer is stored

1784:   Level: advanced

1786:   Notes:
1787:   The array and each entry in the array should be freed with `PetscFree()`

1789:   Each process may have different values depending on how the options were inserted into the database

1791: .seealso: `PetscOptionsAllUsed()`, `PetscOptionsView()`, `PetscOptionsPush()`, `PetscOptionsPop()`
1792: @*/
1793: PetscErrorCode PetscOptionsGetAll(PetscOptions options, char *copts[]) PeNS
1794: {
1795:   PetscInt i;
1796:   size_t   len = 1, lent = 0;
1797:   char    *coptions = NULL;

1799:   PetscFunctionBegin;
1800:   PetscAssertPointer(copts, 2);
1801:   options = options ? options : defaultoptions;
1802:   /* count the length of the required string */
1803:   for (i = 0; i < options->N; i++) {
1804:     PetscCall(PetscStrlen(options->names[i], &lent));
1805:     len += 2 + lent;
1806:     if (options->values[i]) {
1807:       PetscCall(PetscStrlen(options->values[i], &lent));
1808:       len += 1 + lent;
1809:     }
1810:   }
1811:   PetscCall(PetscMalloc1(len, &coptions));
1812:   coptions[0] = 0;
1813:   for (i = 0; i < options->N; i++) {
1814:     PetscCall(PetscStrlcat(coptions, "-", len));
1815:     PetscCall(PetscStrlcat(coptions, options->names[i], len));
1816:     PetscCall(PetscStrlcat(coptions, " ", len));
1817:     if (options->values[i]) {
1818:       PetscCall(PetscStrlcat(coptions, options->values[i], len));
1819:       PetscCall(PetscStrlcat(coptions, " ", len));
1820:     }
1821:   }
1822:   *copts = coptions;
1823:   PetscFunctionReturn(PETSC_SUCCESS);
1824: }

1826: /*@
1827:   PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database

1829:   Not Collective

1831:   Input Parameters:
1832: + options - options database, use `NULL` for default global database
1833: - name    - string name of option

1835:   Output Parameter:
1836: . used - `PETSC_TRUE` if the option was used, otherwise false, including if option was not found in options database

1838:   Level: advanced

1840:   Note:
1841:   The value returned may be different on each process and depends on which options have been processed
1842:   on the given process

1844: .seealso: `PetscOptionsView()`, `PetscOptionsLeft()`, `PetscOptionsAllUsed()`
1845: @*/
1846: PetscErrorCode PetscOptionsUsed(PetscOptions options, const char *name, PetscBool *used)
1847: {
1848:   PetscInt i;

1850:   PetscFunctionBegin;
1851:   PetscAssertPointer(name, 2);
1852:   PetscAssertPointer(used, 3);
1853:   options = options ? options : defaultoptions;
1854:   *used   = PETSC_FALSE;
1855:   for (i = 0; i < options->N; i++) {
1856:     PetscCall(PetscStrcasecmp(options->names[i], name, used));
1857:     if (*used) {
1858:       *used = options->used[i];
1859:       break;
1860:     }
1861:   }
1862:   PetscFunctionReturn(PETSC_SUCCESS);
1863: }

1865: /*@
1866:   PetscOptionsAllUsed - Returns a count of the number of options in the
1867:   database that have never been selected.

1869:   Not Collective

1871:   Input Parameter:
1872: . options - options database, use `NULL` for default global database

1874:   Output Parameter:
1875: . N - count of options not used

1877:   Level: advanced

1879:   Note:
1880:   The value returned may be different on each process and depends on which options have been processed
1881:   on the given process

1883: .seealso: `PetscOptionsView()`
1884: @*/
1885: PetscErrorCode PetscOptionsAllUsed(PetscOptions options, PetscInt *N)
1886: {
1887:   PetscInt i, n = 0;

1889:   PetscFunctionBegin;
1890:   PetscAssertPointer(N, 2);
1891:   options = options ? options : defaultoptions;
1892:   for (i = 0; i < options->N; i++) {
1893:     if (!options->used[i]) n++;
1894:   }
1895:   *N = n;
1896:   PetscFunctionReturn(PETSC_SUCCESS);
1897: }

1899: /*@
1900:   PetscOptionsLeft - Prints to screen any options that were set and never used.

1902:   Not Collective

1904:   Input Parameter:
1905: . options - options database; use `NULL` for default global database

1907:   Options Database Key:
1908: . -options_left - activates `PetscOptionsAllUsed()` within `PetscFinalize()`

1910:   Level: advanced

1912:   Notes:
1913:   This is rarely used directly, it is called by `PetscFinalize()` by default (unless
1914:   `-options_left false` is specified) to help users determine possible mistakes in their usage of
1915:   options. This only prints values on process zero of `PETSC_COMM_WORLD`.

1917:   Other processes depending the objects
1918:   used may have different options that are left unused.

1920: .seealso: `PetscOptionsAllUsed()`
1921: @*/
1922: PetscErrorCode PetscOptionsLeft(PetscOptions options)
1923: {
1924:   PetscInt     cnt = 0;
1925:   PetscOptions toptions;

1927:   PetscFunctionBegin;
1928:   toptions = options ? options : defaultoptions;
1929:   for (PetscInt i = 0; i < toptions->N; i++) {
1930:     if (!toptions->used[i]) {
1931:       if (PetscCIOption(toptions->names[i])) continue;
1932:       if (toptions->values[i]) {
1933:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Option left: name:-%s value: %s source: %s\n", toptions->names[i], toptions->values[i], PetscOptionSources[toptions->source[i]]));
1934:       } else {
1935:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Option left: name:-%s (no value) source: %s\n", toptions->names[i], PetscOptionSources[toptions->source[i]]));
1936:       }
1937:     }
1938:   }
1939:   if (!options) {
1940:     toptions = defaultoptions;
1941:     while (toptions->previous) {
1942:       cnt++;
1943:       toptions = toptions->previous;
1944:     }
1945:     if (cnt) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Option left: You may have forgotten some calls to PetscOptionsPop(),\n             PetscOptionsPop() has been called %" PetscInt_FMT " less times than PetscOptionsPush()\n", cnt));
1946:   }
1947:   PetscFunctionReturn(PETSC_SUCCESS);
1948: }

1950: /*@C
1951:   PetscOptionsLeftGet - Returns all options that were set and never used.

1953:   Not Collective

1955:   Input Parameter:
1956: . options - options database, use `NULL` for default global database

1958:   Output Parameters:
1959: + N      - count of options not used
1960: . names  - names of options not used
1961: - values - values of options not used

1963:   Level: advanced

1965:   Notes:
1966:   Users should call `PetscOptionsLeftRestore()` to free the memory allocated in this routine

1968:   The value returned may be different on each process and depends on which options have been processed
1969:   on the given process

1971: .seealso: `PetscOptionsAllUsed()`, `PetscOptionsLeft()`
1972: @*/
1973: PetscErrorCode PetscOptionsLeftGet(PetscOptions options, PetscInt *N, char **names[], char **values[])
1974: {
1975:   PetscInt n;

1977:   PetscFunctionBegin;
1978:   if (N) PetscAssertPointer(N, 2);
1979:   if (names) PetscAssertPointer(names, 3);
1980:   if (values) PetscAssertPointer(values, 4);
1981:   options = options ? options : defaultoptions;

1983:   /* The number of unused PETSc options */
1984:   n = 0;
1985:   for (PetscInt i = 0; i < options->N; i++) {
1986:     if (PetscCIOption(options->names[i])) continue;
1987:     if (!options->used[i]) n++;
1988:   }
1989:   if (N) *N = n;
1990:   if (names) PetscCall(PetscMalloc1(n, names));
1991:   if (values) PetscCall(PetscMalloc1(n, values));

1993:   n = 0;
1994:   if (names || values) {
1995:     for (PetscInt i = 0; i < options->N; i++) {
1996:       if (!options->used[i]) {
1997:         if (PetscCIOption(options->names[i])) continue;
1998:         if (names) (*names)[n] = options->names[i];
1999:         if (values) (*values)[n] = options->values[i];
2000:         n++;
2001:       }
2002:     }
2003:   }
2004:   PetscFunctionReturn(PETSC_SUCCESS);
2005: }

2007: /*@C
2008:   PetscOptionsLeftRestore - Free memory for the unused PETSc options obtained using `PetscOptionsLeftGet()`.

2010:   Not Collective

2012:   Input Parameters:
2013: + options - options database, use `NULL` for default global database
2014: . N       - count of options not used
2015: . names   - names of options not used
2016: - values  - values of options not used

2018:   Level: advanced

2020:   Notes:
2021:   The user should pass the same pointer to `N` as they did when calling `PetscOptionsLeftGet()`

2023: .seealso: `PetscOptionsAllUsed()`, `PetscOptionsLeft()`, `PetscOptionsLeftGet()`
2024: @*/
2025: PetscErrorCode PetscOptionsLeftRestore(PetscOptions options, PetscInt *N, char **names[], char **values[])
2026: {
2027:   PetscFunctionBegin;
2028:   (void)options;
2029:   if (N) PetscAssertPointer(N, 2);
2030:   if (names) PetscAssertPointer(names, 3);
2031:   if (values) PetscAssertPointer(values, 4);
2032:   if (N) *N = 0;
2033:   if (names) PetscCall(PetscFree(*names));
2034:   if (values) PetscCall(PetscFree(*values));
2035:   PetscFunctionReturn(PETSC_SUCCESS);
2036: }

2038: /*@C
2039:   PetscOptionsMonitorDefault - Print all options set value events using the supplied `PetscViewer`.

2041:   Logically Collective

2043:   Input Parameters:
2044: + name   - option name string
2045: . value  - option value string
2046: . source - The source for the option
2047: - ctx    - a `PETSCVIEWERASCII` or `NULL`

2049:   Level: intermediate

2051:   Notes:
2052:   If ctx is `NULL`, `PetscPrintf()` is used.
2053:   The first MPI process in the `PetscViewer` viewer actually prints the values, other
2054:   processes may have different values set

2056:   If `PetscCIEnabled` then do not print the test harness options

2058: .seealso: `PetscOptionsMonitorSet()`
2059: @*/
2060: PetscErrorCode PetscOptionsMonitorDefault(const char name[], const char value[], PetscOptionSource source, PetscCtx ctx)
2061: {
2062:   PetscFunctionBegin;
2063:   if (PetscCIOption(name)) PetscFunctionReturn(PETSC_SUCCESS);

2065:   if (ctx) {
2066:     PetscViewer viewer = (PetscViewer)ctx;
2067:     if (!value) {
2068:       PetscCall(PetscViewerASCIIPrintf(viewer, "Removing option: %s\n", name));
2069:     } else if (!value[0]) {
2070:       PetscCall(PetscViewerASCIIPrintf(viewer, "Setting option: %s (no value) (source: %s)\n", name, PetscOptionSources[source]));
2071:     } else {
2072:       PetscCall(PetscViewerASCIIPrintf(viewer, "Setting option: %s = %s (source: %s)\n", name, value, PetscOptionSources[source]));
2073:     }
2074:   } else {
2075:     if (!value) {
2076:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Removing option: %s\n", name));
2077:     } else if (!value[0]) {
2078:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Setting option: %s (no value) (source: %s)\n", name, PetscOptionSources[source]));
2079:     } else {
2080:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Setting option: %s = %s (source: %s)\n", name, value, PetscOptionSources[source]));
2081:     }
2082:   }
2083:   PetscFunctionReturn(PETSC_SUCCESS);
2084: }

2086: /*@C
2087:   PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
2088:   modified the PETSc options database.

2090:   Not Collective

2092:   Input Parameters:
2093: + monitor        - pointer to function (if this is `NULL`, it turns off monitoring
2094: . mctx           - [optional] context for private data for the monitor routine (use `NULL` if
2095:                    no context is desired)
2096: - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for its calling sequence

2098:   Calling sequence of `monitor`:
2099: + name   - option name string
2100: . value  - option value string, a value of `NULL` indicates the option is being removed from the database. A value
2101:            of "" indicates the option is in the database but has no value.
2102: . source - option source
2103: - mctx   - optional monitoring context, as set by `PetscOptionsMonitorSet()`

2105:   Options Database Keys:
2106: + -options_monitor viewer - turn on default monitoring of changes to the options database
2107: - -options_monitor_cancel - turn off any option monitors except the default monitor obtained with `-options_monitor`

2109:   Level: intermediate

2111:   Notes:
2112:   See `PetscInitialize()` for options related to option database monitoring.

2114:   The default is to do no monitoring.  To print the name and value of options
2115:   being inserted into the database, use `PetscOptionsMonitorDefault()` as the monitoring routine,
2116:   with a `NULL` monitoring context. Or use the option `-options_monitor viewer`.

2118:   Several different monitoring routines may be set by calling
2119:   `PetscOptionsMonitorSet()` multiple times; all will be called in the
2120:   order in which they were set.

2122: .seealso: `PetscOptionsMonitorDefault()`, `PetscInitialize()`, `PetscCtxDestroyFn`
2123: @*/
2124: PetscErrorCode PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], PetscOptionSource source, PetscCtx mctx), PetscCtx mctx, PetscCtxDestroyFn *monitordestroy)
2125: {
2126:   PetscOptions options = defaultoptions;

2128:   PetscFunctionBegin;
2129:   if (options->monitorCancel) PetscFunctionReturn(PETSC_SUCCESS);
2130:   PetscCheck(options->numbermonitors < MAXOPTIONSMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptions monitors set");
2131:   options->monitor[options->numbermonitors]          = monitor;
2132:   options->monitordestroy[options->numbermonitors]   = monitordestroy;
2133:   options->monitorcontext[options->numbermonitors++] = mctx;
2134:   PetscFunctionReturn(PETSC_SUCCESS);
2135: }

2137: /*
2138:    PetscOptionsStringToBool - Converts string to PetscBool, handles cases like "yes", "no", "true", "false", "0", "1", "off", "on".
2139:      Empty string is considered as true.
2140: */
2141: PetscErrorCode PetscOptionsStringToBool(const char value[], PetscBool *a)
2142: {
2143:   PetscBool istrue, isfalse;
2144:   size_t    len;

2146:   PetscFunctionBegin;
2147:   /* PetscStrlen() returns 0 for NULL or "" */
2148:   PetscCall(PetscStrlen(value, &len));
2149:   if (!len) {
2150:     *a = PETSC_TRUE;
2151:     PetscFunctionReturn(PETSC_SUCCESS);
2152:   }
2153:   PetscCall(PetscStrcasecmp(value, "TRUE", &istrue));
2154:   if (istrue) {
2155:     *a = PETSC_TRUE;
2156:     PetscFunctionReturn(PETSC_SUCCESS);
2157:   }
2158:   PetscCall(PetscStrcasecmp(value, "YES", &istrue));
2159:   if (istrue) {
2160:     *a = PETSC_TRUE;
2161:     PetscFunctionReturn(PETSC_SUCCESS);
2162:   }
2163:   PetscCall(PetscStrcasecmp(value, "1", &istrue));
2164:   if (istrue) {
2165:     *a = PETSC_TRUE;
2166:     PetscFunctionReturn(PETSC_SUCCESS);
2167:   }
2168:   PetscCall(PetscStrcasecmp(value, "on", &istrue));
2169:   if (istrue) {
2170:     *a = PETSC_TRUE;
2171:     PetscFunctionReturn(PETSC_SUCCESS);
2172:   }
2173:   PetscCall(PetscStrcasecmp(value, "FALSE", &isfalse));
2174:   if (isfalse) {
2175:     *a = PETSC_FALSE;
2176:     PetscFunctionReturn(PETSC_SUCCESS);
2177:   }
2178:   PetscCall(PetscStrcasecmp(value, "NO", &isfalse));
2179:   if (isfalse) {
2180:     *a = PETSC_FALSE;
2181:     PetscFunctionReturn(PETSC_SUCCESS);
2182:   }
2183:   PetscCall(PetscStrcasecmp(value, "0", &isfalse));
2184:   if (isfalse) {
2185:     *a = PETSC_FALSE;
2186:     PetscFunctionReturn(PETSC_SUCCESS);
2187:   }
2188:   PetscCall(PetscStrcasecmp(value, "off", &isfalse));
2189:   if (isfalse) {
2190:     *a = PETSC_FALSE;
2191:     PetscFunctionReturn(PETSC_SUCCESS);
2192:   }
2193:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown logical value: %s", value);
2194: }

2196: /*
2197:    PetscOptionsStringToInt - Converts a string to an integer value. Handles special cases such as "default" and "decide"
2198: */
2199: PetscErrorCode PetscOptionsStringToInt(const char name[], PetscInt *a)
2200: {
2201:   size_t    len;
2202:   PetscBool decide, tdefault, mouse, unlimited;

2204:   PetscFunctionBegin;
2205:   PetscCall(PetscStrlen(name, &len));
2206:   PetscCheck(len, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "character string of length zero has no numerical value");

2208:   PetscCall(PetscStrcasecmp(name, "PETSC_DEFAULT", &tdefault));
2209:   if (!tdefault) PetscCall(PetscStrcasecmp(name, "DEFAULT", &tdefault));
2210:   PetscCall(PetscStrcasecmp(name, "PETSC_DECIDE", &decide));
2211:   if (!decide) PetscCall(PetscStrcasecmp(name, "DECIDE", &decide));
2212:   if (!decide) PetscCall(PetscStrcasecmp(name, "PETSC_DETERMINE", &decide));
2213:   if (!decide) PetscCall(PetscStrcasecmp(name, "DETERMINE", &decide));
2214:   PetscCall(PetscStrcasecmp(name, "PETSC_UNLIMITED", &unlimited));
2215:   if (!unlimited) PetscCall(PetscStrcasecmp(name, "UNLIMITED", &unlimited));
2216:   PetscCall(PetscStrcasecmp(name, "mouse", &mouse));

2218:   if (tdefault) *a = PETSC_DEFAULT;
2219:   else if (decide) *a = PETSC_DECIDE;
2220:   else if (unlimited) *a = PETSC_UNLIMITED;
2221:   else if (mouse) *a = -1;
2222:   else {
2223:     char *endptr;
2224:     long  strtolval;

2226:     strtolval = strtol(name, &endptr, 10);
2227:     PetscCheck((size_t)(endptr - name) == len, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Input string %s has no integer value (do not include . in it)", name);

2229: #if defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE_ATOLL)
2230:     (void)strtolval;
2231:     *a = atoll(name);
2232: #elif defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE___INT64)
2233:     (void)strtolval;
2234:     *a = _atoi64(name);
2235: #else
2236:     *a = (PetscInt)strtolval;
2237: #endif
2238:   }
2239:   PetscFunctionReturn(PETSC_SUCCESS);
2240: }

2242: #if defined(PETSC_USE_REAL___FLOAT128)
2243:   #include <quadmath.h>
2244: #endif

2246: static PetscErrorCode PetscStrtod(const char name[], PetscReal *a, char **endptr)
2247: {
2248:   PetscFunctionBegin;
2249: #if defined(PETSC_USE_REAL___FLOAT128)
2250:   *a = strtoflt128(name, endptr);
2251: #else
2252:   *a = (PetscReal)strtod(name, endptr);
2253: #endif
2254:   PetscFunctionReturn(PETSC_SUCCESS);
2255: }

2257: static PetscErrorCode PetscStrtoz(const char name[], PetscScalar *a, char **endptr, PetscBool *isImaginary)
2258: {
2259:   PetscBool hasi = PETSC_FALSE;
2260:   char     *ptr;
2261:   PetscReal strtoval;

2263:   PetscFunctionBegin;
2264:   PetscCall(PetscStrtod(name, &strtoval, &ptr));
2265:   if (ptr == name) {
2266:     strtoval = 1.;
2267:     hasi     = PETSC_TRUE;
2268:     if (name[0] == 'i') {
2269:       ptr++;
2270:     } else if (name[0] == '+' && name[1] == 'i') {
2271:       ptr += 2;
2272:     } else if (name[0] == '-' && name[1] == 'i') {
2273:       strtoval = -1.;
2274:       ptr += 2;
2275:     }
2276:   } else if (*ptr == 'i') {
2277:     hasi = PETSC_TRUE;
2278:     ptr++;
2279:   }
2280:   *endptr      = ptr;
2281:   *isImaginary = hasi;
2282:   if (hasi) {
2283: #if !defined(PETSC_USE_COMPLEX)
2284:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Input string %s contains imaginary but complex not supported ", name);
2285: #else
2286:     *a = PetscCMPLX(0., strtoval);
2287: #endif
2288:   } else {
2289:     *a = strtoval;
2290:   }
2291:   PetscFunctionReturn(PETSC_SUCCESS);
2292: }

2294: /*
2295:    Converts a string to PetscReal value. Handles special cases like "default" and "decide"
2296: */
2297: PetscErrorCode PetscOptionsStringToReal(const char name[], PetscReal *a)
2298: {
2299:   size_t    len;
2300:   PetscBool match;
2301:   char     *endptr;

2303:   PetscFunctionBegin;
2304:   PetscCall(PetscStrlen(name, &len));
2305:   PetscCheck(len, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "String of length zero has no numerical value");

2307:   PetscCall(PetscStrcasecmp(name, "PETSC_DEFAULT", &match));
2308:   if (!match) PetscCall(PetscStrcasecmp(name, "DEFAULT", &match));
2309:   if (match) {
2310:     *a = PETSC_DEFAULT;
2311:     PetscFunctionReturn(PETSC_SUCCESS);
2312:   }

2314:   PetscCall(PetscStrcasecmp(name, "PETSC_DECIDE", &match));
2315:   if (!match) PetscCall(PetscStrcasecmp(name, "DECIDE", &match));
2316:   if (match) {
2317:     *a = PETSC_DECIDE;
2318:     PetscFunctionReturn(PETSC_SUCCESS);
2319:   }

2321:   PetscCall(PetscStrcasecmp(name, "PETSC_DETERMINE", &match));
2322:   if (!match) PetscCall(PetscStrcasecmp(name, "DETERMINE", &match));
2323:   if (match) {
2324:     *a = PETSC_DETERMINE;
2325:     PetscFunctionReturn(PETSC_SUCCESS);
2326:   }

2328:   PetscCall(PetscStrcasecmp(name, "PETSC_UNLIMITED", &match));
2329:   if (!match) PetscCall(PetscStrcasecmp(name, "UNLIMITED", &match));
2330:   if (match) {
2331:     *a = PETSC_UNLIMITED;
2332:     PetscFunctionReturn(PETSC_SUCCESS);
2333:   }

2335:   PetscCall(PetscStrtod(name, a, &endptr));
2336:   PetscCheck((size_t)(endptr - name) == len, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Input string %s has no numeric value", name);
2337:   PetscFunctionReturn(PETSC_SUCCESS);
2338: }

2340: PetscErrorCode PetscOptionsStringToScalar(const char name[], PetscScalar *a)
2341: {
2342:   PetscBool   imag1;
2343:   size_t      len;
2344:   PetscScalar val = 0.;
2345:   char       *ptr = NULL;

2347:   PetscFunctionBegin;
2348:   PetscCall(PetscStrlen(name, &len));
2349:   PetscCheck(len, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "character string of length zero has no numerical value");
2350:   PetscCall(PetscStrtoz(name, &val, &ptr, &imag1));
2351: #if defined(PETSC_USE_COMPLEX)
2352:   if ((size_t)(ptr - name) < len) {
2353:     PetscBool   imag2;
2354:     PetscScalar val2;

2356:     PetscCall(PetscStrtoz(ptr, &val2, &ptr, &imag2));
2357:     if (imag1) PetscCheck(imag2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Input string %s: must specify imaginary component second", name);
2358:     val = PetscCMPLX(PetscRealPart(val), PetscImaginaryPart(val2));
2359:   }
2360: #endif
2361:   PetscCheck((size_t)(ptr - name) == len, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Input string %s has no numeric value ", name);
2362:   *a = val;
2363:   PetscFunctionReturn(PETSC_SUCCESS);
2364: }

2366: /*@C
2367:   PetscOptionsGetBool - Gets the Logical (true or false) value for a particular
2368:   option in the database.

2370:   Not Collective

2372:   Input Parameters:
2373: + options - options database, use `NULL` for default global database
2374: . pre     - the string to prepend to the name or `NULL`
2375: - name    - the option one is seeking

2377:   Output Parameters:
2378: + ivalue - the logical value to return
2379: - set    - `PETSC_TRUE`  if found, else `PETSC_FALSE`

2381:   Level: beginner

2383:   Notes:
2384:   TRUE, true, YES, yes, ON, on, nostring, and 1 all translate to `PETSC_TRUE`
2385:   FALSE, false, NO, no, OFF, off and 0 all translate to `PETSC_FALSE`

2387:   If the option is given, but no value is provided, then `ivalue` and `set` are both given the value `PETSC_TRUE`. That is `-requested_bool`
2388:   is equivalent to `-requested_bool true`

2390:   If the user does not supply the option at all `ivalue` is NOT changed. Thus
2391:   you should ALWAYS initialize `ivalue` if you access it without first checking that the `set` flag is true.

2393: .seealso: `PetscOptionsGetBool3()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
2394:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsGetInt()`, `PetscOptionsBool()`,
2395:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2396:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2397:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2398:           `PetscOptionsFList()`, `PetscOptionsEList()`
2399: @*/
2400: PetscErrorCode PetscOptionsGetBool(PetscOptions options, const char pre[], const char name[], PetscBool *ivalue, PetscBool *set)
2401: {
2402:   const char *value;
2403:   PetscBool   flag;

2405:   PetscFunctionBegin;
2406:   PetscAssertPointer(name, 3);
2407:   if (ivalue) PetscAssertPointer(ivalue, 4);
2408:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2409:   if (flag) {
2410:     if (set) *set = PETSC_TRUE;
2411:     PetscCall(PetscOptionsStringToBool(value, &flag));
2412:     if (ivalue) *ivalue = flag;
2413:   } else {
2414:     if (set) *set = PETSC_FALSE;
2415:   }
2416:   PetscFunctionReturn(PETSC_SUCCESS);
2417: }

2419: /*@C
2420:   PetscOptionsGetBool3 - Gets the ternary logical (true, false or unknown) value for a particular
2421:   option in the database.

2423:   Not Collective

2425:   Input Parameters:
2426: + options - options database, use `NULL` for default global database
2427: . pre     - the string to prepend to the name or `NULL`
2428: - name    - the option one is seeking

2430:   Output Parameters:
2431: + ivalue - the ternary logical value to return
2432: - set    - `PETSC_TRUE`  if found, else `PETSC_FALSE`

2434:   Level: beginner

2436:   Notes:
2437:   TRUE, true, YES, yes, ON, on, nostring and 1 all translate to `PETSC_BOOL3_TRUE`
2438:   FALSE, false, NO, no, OFF, off and 0 all translate to `PETSC_BOOL3_FALSE`
2439:   UNKNOWN, unknown, AUTO and auto all translate to `PETSC_BOOL3_UNKNOWN`

2441:   If the option is given, but no value is provided, then `ivalue` will be set to `PETSC_BOOL3_TRUE` and `set` will be set to `PETSC_TRUE`. That is `-requested_bool3`
2442:   is equivalent to `-requested_bool3 true`

2444:   If the user does not supply the option at all `ivalue` is NOT changed. Thus
2445:   you should ALWAYS initialize `ivalue` if you access it without first checking that the `set` flag is true.

2447: .seealso: `PetscOptionsGetBool()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
2448:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsGetInt()`, `PetscOptionsBool()`,
2449:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2450:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2451:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2452:           `PetscOptionsFList()`, `PetscOptionsEList()`
2453: @*/
2454: PetscErrorCode PetscOptionsGetBool3(PetscOptions options, const char pre[], const char name[], PetscBool3 *ivalue, PetscBool *set)
2455: {
2456:   const char *value;
2457:   PetscBool   flag;

2459:   PetscFunctionBegin;
2460:   PetscAssertPointer(name, 3);
2461:   if (ivalue) PetscAssertPointer(ivalue, 4);
2462:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2463:   if (flag) { // found the option
2464:     PetscBool isAUTO = PETSC_FALSE, isUNKNOWN = PETSC_FALSE;

2466:     if (set) *set = PETSC_TRUE;
2467:     PetscCall(PetscStrcasecmp("AUTO", value, &isAUTO));                    // auto or AUTO
2468:     if (!isAUTO) PetscCall(PetscStrcasecmp("UNKNOWN", value, &isUNKNOWN)); // unknown or UNKNOWN
2469:     if (isAUTO || isUNKNOWN) {
2470:       if (ivalue) *ivalue = PETSC_BOOL3_UNKNOWN;
2471:     } else { // handle boolean values (if no value is given, it returns true)
2472:       PetscCall(PetscOptionsStringToBool(value, &flag));
2473:       if (ivalue) *ivalue = PetscBoolToBool3(flag);
2474:     }
2475:   } else {
2476:     if (set) *set = PETSC_FALSE;
2477:   }
2478:   PetscFunctionReturn(PETSC_SUCCESS);
2479: }

2481: /*@C
2482:   PetscOptionsGetEList - Puts a list of option values that a single one may be selected from

2484:   Not Collective

2486:   Input Parameters:
2487: + options - options database, use `NULL` for default global database
2488: . pre     - the string to prepend to the name or `NULL`
2489: . opt     - option name
2490: . list    - the possible choices (one of these must be selected, anything else is invalid)
2491: - ntext   - number of choices

2493:   Output Parameters:
2494: + value - the index of the value to return (defaults to zero if the option name is given but no choice is listed)
2495: - set   - `PETSC_TRUE` if found, else `PETSC_FALSE`

2497:   Level: intermediate

2499:   Notes:
2500:   If the user does not supply the option `value` is NOT changed. Thus
2501:   you should ALWAYS initialize `value` if you access it without first checking that the `set` flag is true.

2503:   See `PetscOptionsFList()` for when the choices are given in a `PetscFunctionList`

2505: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`,
2506:           `PetscOptionsHasName()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2507:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2508:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2509:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2510:           `PetscOptionsFList()`, `PetscOptionsEList()`
2511: @*/
2512: PetscErrorCode PetscOptionsGetEList(PetscOptions options, const char pre[], const char opt[], const char *const list[], PetscInt ntext, PetscInt *value, PetscBool *set)
2513: {
2514:   size_t    alen, len = 0, tlen = 0;
2515:   char     *svalue;
2516:   PetscBool aset, flg = PETSC_FALSE;

2518:   PetscFunctionBegin;
2519:   PetscAssertPointer(opt, 3);
2520:   for (PetscInt i = 0; i < ntext; i++) {
2521:     PetscCall(PetscStrlen(list[i], &alen));
2522:     if (alen > len) len = alen;
2523:     tlen += len + 1;
2524:   }
2525:   len += 5; /* a little extra space for user mistypes */
2526:   PetscCall(PetscMalloc1(len, &svalue));
2527:   PetscCall(PetscOptionsGetString(options, pre, opt, svalue, len, &aset));
2528:   if (aset) {
2529:     PetscCall(PetscEListFind(ntext, list, svalue, value, &flg));
2530:     if (!flg) {
2531:       char *avail;

2533:       PetscCall(PetscMalloc1(tlen, &avail));
2534:       avail[0] = '\0';
2535:       for (PetscInt i = 0; i < ntext; i++) {
2536:         PetscCall(PetscStrlcat(avail, list[i], tlen));
2537:         PetscCall(PetscStrlcat(avail, " ", tlen));
2538:       }
2539:       PetscCall(PetscStrtolower(avail));
2540:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Unknown option \"%s\" for -%s%s. Available options: %s", svalue, pre ? pre : "", opt + 1, avail);
2541:     }
2542:     if (set) *set = PETSC_TRUE;
2543:   } else if (set) *set = PETSC_FALSE;
2544:   PetscCall(PetscFree(svalue));
2545:   PetscFunctionReturn(PETSC_SUCCESS);
2546: }

2548: /*@C
2549:   PetscOptionsGetEnum - Gets the enum value for a particular option in the database.

2551:   Not Collective

2553:   Input Parameters:
2554: + options - options database, use `NULL` for default global database
2555: . pre     - option prefix or `NULL`
2556: . opt     - option name
2557: - list    - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null

2559:   Output Parameters:
2560: + value - the value to return
2561: - set   - `PETSC_TRUE` if found, else `PETSC_FALSE`

2563:   Level: beginner

2565:   Notes:
2566:   If the user does not supply the option `value` is NOT changed. Thus
2567:   you should ALWAYS initialize `value` if you access it without first checking that the `set` flag is true.

2569:   `list` is usually something like `PCASMTypes` or some other predefined list of enum names

2571: .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`, `PetscOptionsGetInt()`,
2572:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2573:           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
2574:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2575:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2576:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2577:           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsGetEList()`, `PetscOptionsEnum()`
2578: @*/
2579: PetscErrorCode PetscOptionsGetEnum(PetscOptions options, const char pre[], const char opt[], const char *const list[], PetscEnum *value, PetscBool *set) PeNSS
2580: {
2581:   PetscInt  ntext = 0, tval;
2582:   PetscBool fset;

2584:   PetscFunctionBegin;
2585:   PetscAssertPointer(opt, 3);
2586:   while (list[ntext++]) PetscCheck(ntext <= 50, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "List argument appears to be wrong or have more than 50 entries");
2587:   PetscCheck(ntext >= 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "List argument must have at least two entries: typename and type prefix");
2588:   ntext -= 3;
2589:   PetscCall(PetscOptionsGetEList(options, pre, opt, list, ntext, &tval, &fset));
2590:   /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */
2591:   if (fset) *value = (PetscEnum)tval;
2592:   if (set) *set = fset;
2593:   PetscFunctionReturn(PETSC_SUCCESS);
2594: }

2596: /*@C
2597:   PetscOptionsGetInt - Gets the integer value for a particular option in the database.

2599:   Not Collective

2601:   Input Parameters:
2602: + options - options database, use `NULL` for default global database
2603: . pre     - the string to prepend to the name or `NULL`
2604: - name    - the option one is seeking

2606:   Output Parameters:
2607: + ivalue - the integer value to return
2608: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2610:   Level: beginner

2612:   Notes:
2613:   If the user does not supply the option `ivalue` is NOT changed. Thus
2614:   you should ALWAYS initialize the `ivalue` if you access it without first checking that the `set` flag is true.

2616:   Accepts the special values `determine`, `decide` and `unlimited`.

2618:   Accepts the deprecated value `default`.

2620: .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
2621:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2622:           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
2623:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2624:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2625:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2626:           `PetscOptionsFList()`, `PetscOptionsEList()`
2627: @*/
2628: PetscErrorCode PetscOptionsGetInt(PetscOptions options, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
2629: {
2630:   const char *value;
2631:   PetscBool   flag;

2633:   PetscFunctionBegin;
2634:   PetscAssertPointer(name, 3);
2635:   PetscAssertPointer(ivalue, 4);
2636:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2637:   if (flag) {
2638:     if (!value) {
2639:       if (set) *set = PETSC_FALSE;
2640:     } else {
2641:       if (set) *set = PETSC_TRUE;
2642:       PetscCall(PetscOptionsStringToInt(value, ivalue));
2643:     }
2644:   } else {
2645:     if (set) *set = PETSC_FALSE;
2646:   }
2647:   PetscFunctionReturn(PETSC_SUCCESS);
2648: }

2650: /*@C
2651:   PetscOptionsGetMPIInt - Gets the MPI integer value for a particular option in the database.

2653:   Not Collective

2655:   Input Parameters:
2656: + options - options database, use `NULL` for default global database
2657: . pre     - the string to prepend to the name or `NULL`
2658: - name    - the option one is seeking

2660:   Output Parameters:
2661: + ivalue - the MPI integer value to return
2662: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2664:   Level: beginner

2666:   Notes:
2667:   If the user does not supply the option `ivalue` is NOT changed. Thus
2668:   you should ALWAYS initialize the `ivalue` if you access it without first checking that the `set` flag is true.

2670:   Accepts the special values `determine`, `decide` and `unlimited`.

2672:   Accepts the deprecated value `default`.

2674: .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
2675:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2676:           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
2677:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2678:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2679:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2680:           `PetscOptionsFList()`, `PetscOptionsEList()`
2681: @*/
2682: PetscErrorCode PetscOptionsGetMPIInt(PetscOptions options, const char pre[], const char name[], PetscMPIInt *ivalue, PetscBool *set)
2683: {
2684:   PetscInt  value;
2685:   PetscBool flag;

2687:   PetscFunctionBegin;
2688:   PetscCall(PetscOptionsGetInt(options, pre, name, &value, &flag));
2689:   if (flag) PetscCall(PetscMPIIntCast(value, ivalue));
2690:   if (set) *set = flag;
2691:   PetscFunctionReturn(PETSC_SUCCESS);
2692: }

2694: /*@C
2695:   PetscOptionsGetReal - Gets the double precision value for a particular
2696:   option in the database.

2698:   Not Collective

2700:   Input Parameters:
2701: + options - options database, use `NULL` for default global database
2702: . pre     - string to prepend to each name or `NULL`
2703: - name    - the option one is seeking

2705:   Output Parameters:
2706: + dvalue - the double value to return
2707: - set    - `PETSC_TRUE` if found, `PETSC_FALSE` if not found

2709:   Level: beginner

2711:   Notes:
2712:   Accepts the special values `determine`, `decide` and `unlimited`.

2714:   Accepts the deprecated value `default`

2716:   If the user does not supply the option `dvalue` is NOT changed. Thus
2717:   you should ALWAYS initialize `dvalue` if you access it without first checking that the `set` flag is true.

2719: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
2720:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2721:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2722:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2723:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2724:           `PetscOptionsFList()`, `PetscOptionsEList()`
2725: @*/
2726: PetscErrorCode PetscOptionsGetReal(PetscOptions options, const char pre[], const char name[], PetscReal *dvalue, PetscBool *set)
2727: {
2728:   const char *value;
2729:   PetscBool   flag;

2731:   PetscFunctionBegin;
2732:   PetscAssertPointer(name, 3);
2733:   PetscAssertPointer(dvalue, 4);
2734:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2735:   if (flag) {
2736:     if (!value) {
2737:       if (set) *set = PETSC_FALSE;
2738:     } else {
2739:       if (set) *set = PETSC_TRUE;
2740:       PetscCall(PetscOptionsStringToReal(value, dvalue));
2741:     }
2742:   } else {
2743:     if (set) *set = PETSC_FALSE;
2744:   }
2745:   PetscFunctionReturn(PETSC_SUCCESS);
2746: }

2748: /*@C
2749:   PetscOptionsGetScalar - Gets the scalar value for a particular
2750:   option in the database.

2752:   Not Collective

2754:   Input Parameters:
2755: + options - options database, use `NULL` for default global database
2756: . pre     - string to prepend to each name or `NULL`
2757: - name    - the option one is seeking

2759:   Output Parameters:
2760: + dvalue - the scalar value to return
2761: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2763:   Level: beginner

2765:   Example Usage:
2766:   A complex number 2+3i must be specified with NO spaces

2768:   Note:
2769:   If the user does not supply the option `dvalue` is NOT changed. Thus
2770:   you should ALWAYS initialize `dvalue` if you access it without first checking if the `set` flag is true.

2772: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
2773:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2774:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2775:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2776:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2777:           `PetscOptionsFList()`, `PetscOptionsEList()`
2778: @*/
2779: PetscErrorCode PetscOptionsGetScalar(PetscOptions options, const char pre[], const char name[], PetscScalar *dvalue, PetscBool *set)
2780: {
2781:   const char *value;
2782:   PetscBool   flag;

2784:   PetscFunctionBegin;
2785:   PetscAssertPointer(name, 3);
2786:   PetscAssertPointer(dvalue, 4);
2787:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2788:   if (flag) {
2789:     if (!value) {
2790:       if (set) *set = PETSC_FALSE;
2791:     } else {
2792: #if !defined(PETSC_USE_COMPLEX)
2793:       PetscCall(PetscOptionsStringToReal(value, dvalue));
2794: #else
2795:       PetscCall(PetscOptionsStringToScalar(value, dvalue));
2796: #endif
2797:       if (set) *set = PETSC_TRUE;
2798:     }
2799:   } else { /* flag */
2800:     if (set) *set = PETSC_FALSE;
2801:   }
2802:   PetscFunctionReturn(PETSC_SUCCESS);
2803: }

2805: /*@C
2806:   PetscOptionsGetString - Gets the string value for a particular option in
2807:   the database.

2809:   Not Collective

2811:   Input Parameters:
2812: + options - options database, use `NULL` for default global database
2813: . pre     - string to prepend to name or `NULL`
2814: . name    - the option one is seeking
2815: - len     - maximum length of the string including null termination

2817:   Output Parameters:
2818: + string - location to copy string
2819: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2821:   Level: beginner

2823:   Note:
2824:   if the option is given but no string is provided then an empty string is returned and `set` is given the value of `PETSC_TRUE`

2826:   If the user does not use the option then `string` is not changed. Thus
2827:   you should ALWAYS initialize `string` if you access it without first checking that the `set` flag is true.

2829:   Fortran Notes:
2830:   The Fortran interface is slightly different from the C/C++
2831:   interface.  Sample usage in Fortran follows
2832: .vb
2833:       character *20    string
2834:       PetscErrorCode   ierr
2835:       PetscBool        set
2836:       call PetscOptionsGetString(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-s',string,set,ierr)
2837: .ve

2839: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`,
2840:           `PetscOptionsHasName()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2841:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2842:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2843:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2844:           `PetscOptionsFList()`, `PetscOptionsEList()`
2845: @*/
2846: PetscErrorCode PetscOptionsGetString(PetscOptions options, const char pre[], const char name[], char string[], size_t len, PetscBool *set) PeNS
2847: {
2848:   const char *value;
2849:   PetscBool   flag;

2851:   PetscFunctionBegin;
2852:   PetscAssertPointer(name, 3);
2853:   PetscAssertPointer(string, 4);
2854:   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
2855:   if (!flag) {
2856:     if (set) *set = PETSC_FALSE;
2857:   } else {
2858:     if (set) *set = PETSC_TRUE;
2859:     if (value) PetscCall(PetscStrncpy(string, value, len));
2860:     else PetscCall(PetscArrayzero(string, len));
2861:   }
2862:   PetscFunctionReturn(PETSC_SUCCESS);
2863: }

2865: /*@C
2866:   PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular
2867:   option in the database.  The values must be separated with commas with no intervening spaces.

2869:   Not Collective

2871:   Input Parameters:
2872: + options - options database, use `NULL` for default global database
2873: . pre     - string to prepend to each name or `NULL`
2874: - name    - the option one is seeking

2876:   Output Parameters:
2877: + dvalue - the Boolean values to return
2878: . nmax   - On input maximum number of values to retrieve, on output the actual number of values retrieved
2879: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2881:   Level: beginner

2883:   Note:
2884:   TRUE, true, YES, yes, nostring, and 1 all translate to `PETSC_TRUE`. FALSE, false, NO, no, and 0 all translate to `PETSC_FALSE`

2886: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
2887:           `PetscOptionsGetString()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2888:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
2889:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
2890:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2891:           `PetscOptionsFList()`, `PetscOptionsEList()`
2892: @*/
2893: PetscErrorCode PetscOptionsGetBoolArray(PetscOptions options, const char pre[], const char name[], PetscBool dvalue[], PetscInt *nmax, PetscBool *set)
2894: {
2895:   const char *svalue;
2896:   const char *value;
2897:   PetscInt    n = 0;
2898:   PetscBool   flag;
2899:   PetscToken  token;

2901:   PetscFunctionBegin;
2902:   PetscAssertPointer(name, 3);
2903:   PetscAssertPointer(nmax, 5);
2904:   if (*nmax) PetscAssertPointer(dvalue, 4);

2906:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
2907:   if (!flag || !svalue) {
2908:     if (set) *set = PETSC_FALSE;
2909:     *nmax = 0;
2910:     PetscFunctionReturn(PETSC_SUCCESS);
2911:   }
2912:   if (set) *set = PETSC_TRUE;
2913:   PetscCall(PetscTokenCreate(svalue, ',', &token));
2914:   PetscCall(PetscTokenFind(token, &value));
2915:   while (value && n < *nmax) {
2916:     PetscCall(PetscOptionsStringToBool(value, dvalue));
2917:     PetscCall(PetscTokenFind(token, &value));
2918:     dvalue++;
2919:     n++;
2920:   }
2921:   PetscCall(PetscTokenDestroy(&token));
2922:   *nmax = n;
2923:   PetscFunctionReturn(PETSC_SUCCESS);
2924: }

2926: /*@C
2927:   PetscOptionsGetEnumArray - Gets an array of enum values for a particular option in the database.

2929:   Not Collective

2931:   Input Parameters:
2932: + options - options database, use `NULL` for default global database
2933: . pre     - option prefix or `NULL`
2934: . name    - option name
2935: - list    - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null

2937:   Output Parameters:
2938: + ivalue - the  enum values to return
2939: . nmax   - On input maximum number of values to retrieve, on output the actual number of values retrieved
2940: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

2942:   Level: beginner

2944:   Notes:
2945:   The array must be passed as a comma separated list with no spaces between the items.

2947:   `list` is usually something like `PCASMTypes` or some other predefined list of enum names.

2949: .seealso: `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`, `PetscOptionsGetInt()`,
2950:           `PetscOptionsGetEnum()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
2951:           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsName()`,
2952:           `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`, `PetscOptionsStringArray()`, `PetscOptionsRealArray()`,
2953:           `PetscOptionsScalar()`, `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
2954:           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsGetEList()`, `PetscOptionsEnum()`
2955: @*/
2956: PetscErrorCode PetscOptionsGetEnumArray(PetscOptions options, const char pre[], const char name[], const char *const list[], PetscEnum ivalue[], PetscInt *nmax, PetscBool *set)
2957: {
2958:   const char *svalue;
2959:   const char *value;
2960:   PetscInt    n = 0;
2961:   PetscEnum   evalue;
2962:   PetscBool   flag;
2963:   PetscToken  token;

2965:   PetscFunctionBegin;
2966:   PetscAssertPointer(name, 3);
2967:   PetscAssertPointer(list, 4);
2968:   PetscAssertPointer(nmax, 6);
2969:   if (*nmax) PetscAssertPointer(ivalue, 5);

2971:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
2972:   if (!flag || !svalue) {
2973:     if (set) *set = PETSC_FALSE;
2974:     *nmax = 0;
2975:     PetscFunctionReturn(PETSC_SUCCESS);
2976:   }
2977:   if (set) *set = PETSC_TRUE;
2978:   PetscCall(PetscTokenCreate(svalue, ',', &token));
2979:   PetscCall(PetscTokenFind(token, &value));
2980:   while (value && n < *nmax) {
2981:     PetscCall(PetscEnumFind(list, value, &evalue, &flag));
2982:     PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_USER, "Unknown enum value '%s' for -%s%s", svalue, pre ? pre : "", name + 1);
2983:     ivalue[n++] = evalue;
2984:     PetscCall(PetscTokenFind(token, &value));
2985:   }
2986:   PetscCall(PetscTokenDestroy(&token));
2987:   *nmax = n;
2988:   PetscFunctionReturn(PETSC_SUCCESS);
2989: }

2991: /*@C
2992:   PetscOptionsGetIntArray - Gets an array of integer values for a particular option in the database.

2994:   Not Collective

2996:   Input Parameters:
2997: + options - options database, use `NULL` for default global database
2998: . pre     - string to prepend to each name or `NULL`
2999: - name    - the option one is seeking

3001:   Output Parameters:
3002: + ivalue - the integer values to return
3003: . nmax   - On input maximum number of values to retrieve, on output the actual number of values retrieved
3004: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

3006:   Level: beginner

3008:   Notes:
3009:   The array can be passed as
3010: +  a comma separated list -                                 0,1,2,3,4,5,6,7
3011: .  a range (start\-end+1) -                                 0-8
3012: .  a range with given increment (start\-end+1:inc) -        0-7:2
3013: -  a combination of values and ranges separated by commas - 0,1-8,8-15:2

3015:   There must be no intervening spaces between the values.

3017: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
3018:           `PetscOptionsGetString()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
3019:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
3020:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
3021:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
3022:           `PetscOptionsFList()`, `PetscOptionsEList()`
3023: @*/
3024: PetscErrorCode PetscOptionsGetIntArray(PetscOptions options, const char pre[], const char name[], PetscInt ivalue[], PetscInt *nmax, PetscBool *set)
3025: {
3026:   const char *svalue;
3027:   const char *value;
3028:   PetscInt    n = 0, i, j, start, end, inc, nvalues;
3029:   size_t      len;
3030:   PetscBool   flag, foundrange;
3031:   PetscToken  token;

3033:   PetscFunctionBegin;
3034:   PetscAssertPointer(name, 3);
3035:   PetscAssertPointer(nmax, 5);
3036:   if (*nmax) PetscAssertPointer(ivalue, 4);

3038:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
3039:   if (!flag || !svalue) {
3040:     if (set) *set = PETSC_FALSE;
3041:     *nmax = 0;
3042:     PetscFunctionReturn(PETSC_SUCCESS);
3043:   }
3044:   if (set) *set = PETSC_TRUE;
3045:   PetscCall(PetscTokenCreate(svalue, ',', &token));
3046:   PetscCall(PetscTokenFind(token, &value));
3047:   while (value && n < *nmax) {
3048:     char *iivalue;

3050:     /* look for form  d-D where d and D are integers */
3051:     PetscCall(PetscStrallocpy(value, &iivalue));
3052:     foundrange = PETSC_FALSE;
3053:     PetscCall(PetscStrlen(iivalue, &len));
3054:     if (iivalue[0] == '-') i = 2;
3055:     else i = 1;
3056:     for (; i < (int)len; i++) {
3057:       if (iivalue[i] == '-') {
3058:         PetscCheck(i != (int)len - 1, PETSC_COMM_SELF, PETSC_ERR_USER, "Error in %" PetscInt_FMT "-th array entry %s", n, iivalue);
3059:         iivalue[i] = 0;

3061:         PetscCall(PetscOptionsStringToInt(iivalue, &start));
3062:         inc = 1;
3063:         j   = i + 1;
3064:         for (; j < (int)len; j++) {
3065:           if (iivalue[j] == ':') {
3066:             iivalue[j] = 0;

3068:             PetscCall(PetscOptionsStringToInt(iivalue + j + 1, &inc));
3069:             PetscCheck(inc > 0, PETSC_COMM_SELF, PETSC_ERR_USER, "Error in %" PetscInt_FMT "-th array entry,%s cannot have negative increment", n, iivalue + j + 1);
3070:             break;
3071:           }
3072:         }
3073:         PetscCall(PetscOptionsStringToInt(iivalue + i + 1, &end));
3074:         PetscCheck(end > start, PETSC_COMM_SELF, PETSC_ERR_USER, "Error in %" PetscInt_FMT "-th array entry, %s-%s cannot have decreasing list", n, iivalue, iivalue + i + 1);
3075:         nvalues = (end - start) / inc + (end - start) % inc;
3076:         PetscCheck(n + nvalues <= *nmax, PETSC_COMM_SELF, PETSC_ERR_USER, "Error in %" PetscInt_FMT "-th array entry, not enough space left in array (%" PetscInt_FMT ") to contain entire range from %" PetscInt_FMT " to %" PetscInt_FMT, n, *nmax - n, start, end);
3077:         for (; start < end; start += inc) {
3078:           *ivalue = start;
3079:           ivalue++;
3080:           n++;
3081:         }
3082:         foundrange = PETSC_TRUE;
3083:         break;
3084:       }
3085:     }
3086:     if (!foundrange) {
3087:       PetscCall(PetscOptionsStringToInt(value, ivalue));
3088:       ivalue++;
3089:       n++;
3090:     }
3091:     PetscCall(PetscFree(iivalue));
3092:     PetscCall(PetscTokenFind(token, &value));
3093:   }
3094:   PetscCall(PetscTokenDestroy(&token));
3095:   *nmax = n;
3096:   PetscFunctionReturn(PETSC_SUCCESS);
3097: }

3099: /*@C
3100:   PetscOptionsGetRealArray - Gets an array of double precision values for a
3101:   particular option in the database.  The values must be separated with commas with no intervening spaces.

3103:   Not Collective

3105:   Input Parameters:
3106: + options - options database, use `NULL` for default global database
3107: . pre     - string to prepend to each name or `NULL`
3108: - name    - the option one is seeking

3110:   Output Parameters:
3111: + dvalue - the double values to return
3112: . nmax   - On input maximum number of values to retrieve, on output the actual number of values retrieved
3113: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

3115:   Level: beginner

3117: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
3118:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsBool()`,
3119:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
3120:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
3121:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
3122:           `PetscOptionsFList()`, `PetscOptionsEList()`
3123: @*/
3124: PetscErrorCode PetscOptionsGetRealArray(PetscOptions options, const char pre[], const char name[], PetscReal dvalue[], PetscInt *nmax, PetscBool *set)
3125: {
3126:   const char *svalue;
3127:   const char *value;
3128:   PetscInt    n = 0;
3129:   PetscBool   flag;
3130:   PetscToken  token;

3132:   PetscFunctionBegin;
3133:   PetscAssertPointer(name, 3);
3134:   PetscAssertPointer(nmax, 5);
3135:   if (*nmax) PetscAssertPointer(dvalue, 4);

3137:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
3138:   if (!flag || !svalue) {
3139:     if (set) *set = PETSC_FALSE;
3140:     *nmax = 0;
3141:     PetscFunctionReturn(PETSC_SUCCESS);
3142:   }
3143:   if (set) *set = PETSC_TRUE;
3144:   PetscCall(PetscTokenCreate(svalue, ',', &token));
3145:   PetscCall(PetscTokenFind(token, &value));
3146:   while (value && n < *nmax) {
3147:     PetscCall(PetscOptionsStringToReal(value, dvalue++));
3148:     PetscCall(PetscTokenFind(token, &value));
3149:     n++;
3150:   }
3151:   PetscCall(PetscTokenDestroy(&token));
3152:   *nmax = n;
3153:   PetscFunctionReturn(PETSC_SUCCESS);
3154: }

3156: /*@C
3157:   PetscOptionsGetScalarArray - Gets an array of scalars for a
3158:   particular option in the database.  The values must be separated with commas with no intervening spaces.

3160:   Not Collective

3162:   Input Parameters:
3163: + options - options database, use `NULL` for default global database
3164: . pre     - string to prepend to each name or `NULL`
3165: - name    - the option one is seeking

3167:   Output Parameters:
3168: + dvalue - the scalar values to return
3169: . nmax   - On input maximum number of values to retrieve, on output the actual number of values retrieved
3170: - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`

3172:   Level: beginner

3174: .seealso: `PetscOptionsGetInt()`, `PetscOptionsHasName()`,
3175:           `PetscOptionsGetString()`, `PetscOptionsGetIntArray()`, `PetscOptionsBool()`,
3176:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
3177:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
3178:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
3179:           `PetscOptionsFList()`, `PetscOptionsEList()`
3180: @*/
3181: PetscErrorCode PetscOptionsGetScalarArray(PetscOptions options, const char pre[], const char name[], PetscScalar dvalue[], PetscInt *nmax, PetscBool *set)
3182: {
3183:   const char *svalue;
3184:   const char *value;
3185:   PetscInt    n = 0;
3186:   PetscBool   flag;
3187:   PetscToken  token;

3189:   PetscFunctionBegin;
3190:   PetscAssertPointer(name, 3);
3191:   PetscAssertPointer(nmax, 5);
3192:   if (*nmax) PetscAssertPointer(dvalue, 4);

3194:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
3195:   if (!flag || !svalue) {
3196:     if (set) *set = PETSC_FALSE;
3197:     *nmax = 0;
3198:     PetscFunctionReturn(PETSC_SUCCESS);
3199:   }
3200:   if (set) *set = PETSC_TRUE;
3201:   PetscCall(PetscTokenCreate(svalue, ',', &token));
3202:   PetscCall(PetscTokenFind(token, &value));
3203:   while (value && n < *nmax) {
3204:     PetscCall(PetscOptionsStringToScalar(value, dvalue++));
3205:     PetscCall(PetscTokenFind(token, &value));
3206:     n++;
3207:   }
3208:   PetscCall(PetscTokenDestroy(&token));
3209:   *nmax = n;
3210:   PetscFunctionReturn(PETSC_SUCCESS);
3211: }

3213: /*@C
3214:   PetscOptionsGetStringArray - Gets an array of string values for a particular
3215:   option in the database. The values must be separated with commas with no intervening spaces.

3217:   Not Collective; No Fortran Support

3219:   Input Parameters:
3220: + options - options database, use `NULL` for default global database
3221: . pre     - string to prepend to name or `NULL`
3222: - name    - the option one is seeking

3224:   Output Parameters:
3225: + strings - location to copy strings
3226: . nmax    - On input maximum number of strings, on output the actual number of strings found
3227: - set     - `PETSC_TRUE` if found, else `PETSC_FALSE`

3229:   Level: beginner

3231:   Notes:
3232:   The `nmax` parameter is used for both input and output.

3234:   The user should pass in an array of pointers to `char`, to hold all the
3235:   strings returned by this function.

3237:   The user is responsible for deallocating the strings that are
3238:   returned.

3240: .seealso: `PetscOptionsGetInt()`, `PetscOptionsGetReal()`,
3241:           `PetscOptionsHasName()`, `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
3242:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
3243:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
3244:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
3245:           `PetscOptionsFList()`, `PetscOptionsEList()`
3246: @*/
3247: PetscErrorCode PetscOptionsGetStringArray(PetscOptions options, const char pre[], const char name[], char *strings[], PetscInt *nmax, PetscBool *set) PeNS
3248: {
3249:   const char *svalue;
3250:   const char *value;
3251:   PetscInt    n = 0;
3252:   PetscBool   flag;
3253:   PetscToken  token;

3255:   PetscFunctionBegin;
3256:   PetscAssertPointer(name, 3);
3257:   PetscAssertPointer(nmax, 5);
3258:   if (*nmax) PetscAssertPointer(strings, 4);

3260:   PetscCall(PetscOptionsFindPair(options, pre, name, &svalue, &flag));
3261:   if (!flag || !svalue) {
3262:     if (set) *set = PETSC_FALSE;
3263:     *nmax = 0;
3264:     PetscFunctionReturn(PETSC_SUCCESS);
3265:   }
3266:   if (set) *set = PETSC_TRUE;
3267:   PetscCall(PetscTokenCreate(svalue, ',', &token));
3268:   PetscCall(PetscTokenFind(token, &value));
3269:   while (value && n < *nmax) {
3270:     PetscCall(PetscStrallocpy(value, &strings[n]));
3271:     PetscCall(PetscTokenFind(token, &value));
3272:     n++;
3273:   }
3274:   PetscCall(PetscTokenDestroy(&token));
3275:   *nmax = n;
3276:   PetscFunctionReturn(PETSC_SUCCESS);
3277: }

3279: /*@C
3280:   PetscOptionsDeprecated_Private - mark an option as deprecated, optionally replacing it with `newname`

3282:   Prints a deprecation warning, unless an option is supplied to suppress.

3284:   Logically Collective

3286:   Input Parameters:
3287: + PetscOptionsObject - string to prepend to name or `NULL`
3288: . oldname            - the old, deprecated option
3289: . newname            - the new option, or `NULL` if option is purely removed
3290: . version            - a string describing the version of first deprecation, e.g. "3.9"
3291: - info               - additional information string, or `NULL`.

3293:   Options Database Key:
3294: . -options_suppress_deprecated_warnings - do not print deprecation warnings

3296:   Level: developer

3298:   Notes:
3299:   If `newname` is provided then the options database will automatically check the database for `oldname`.

3301:   The old call `PetscOptionsXXX`(`oldname`) should be removed from the source code when both (1) the call to `PetscOptionsDeprecated()` occurs before the
3302:   new call to `PetscOptionsXXX`(`newname`) and (2) the argument handling of the new call to `PetscOptionsXXX`(`newname`) is identical to the previous call.
3303:   See `PTScotch_PartGraph_Seq()` for an example of when (1) fails and `SNESTestJacobian()` where an example of (2) fails.

3305:   Must be called between `PetscOptionsBegin()` (or `PetscObjectOptionsBegin()`) and `PetscOptionsEnd()`.
3306:   Only the process of rank zero that owns the `PetscOptionsItems` are argument (managed by `PetscOptionsBegin()` or
3307:   `PetscObjectOptionsBegin()` prints the information
3308:   If newname is provided, the old option is replaced. Otherwise, it remains
3309:   in the options database.
3310:   If an option is not replaced, the info argument should be used to advise the user
3311:   on how to proceed.
3312:   There is a limit on the length of the warning printed, so very long strings
3313:   provided as info may be truncated.

3315: .seealso: `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsScalar()`, `PetscOptionsBool()`, `PetscOptionsString()`, `PetscOptionsSetValue()`
3316: @*/
3317: PetscErrorCode PetscOptionsDeprecated_Private(PetscOptionItems PetscOptionsObject, const char oldname[], const char newname[], const char version[], const char info[])
3318: {
3319:   PetscBool         found, quiet;
3320:   const char       *value;
3321:   const char *const quietopt = "-options_suppress_deprecated_warnings";
3322:   char              msg[4096];
3323:   char             *prefix  = NULL;
3324:   PetscOptions      options = NULL;
3325:   MPI_Comm          comm    = PETSC_COMM_SELF;

3327:   PetscFunctionBegin;
3328:   PetscAssertPointer(oldname, 2);
3329:   PetscAssertPointer(version, 4);
3330:   if (PetscOptionsObject) {
3331:     prefix  = PetscOptionsObject->prefix;
3332:     options = PetscOptionsObject->options;
3333:     comm    = PetscOptionsObject->comm;
3334:   }
3335:   PetscCall(PetscOptionsFindPair(options, prefix, oldname, &value, &found));
3336:   if (found) {
3337:     if (newname) {
3338:       PetscBool newfound;

3340:       /* do not overwrite if the new option has been provided */
3341:       PetscCall(PetscOptionsFindPair(options, prefix, newname, NULL, &newfound));
3342:       if (!newfound) {
3343:         if (prefix) PetscCall(PetscOptionsPrefixPush(options, prefix));
3344:         PetscCall(PetscOptionsSetValue(options, newname, value));
3345:         if (prefix) PetscCall(PetscOptionsPrefixPop(options));
3346:       }
3347:       PetscCall(PetscOptionsClearValue(options, oldname));
3348:     }
3349:     quiet = PETSC_FALSE;
3350:     PetscCall(PetscOptionsGetBool(options, NULL, quietopt, &quiet, NULL));
3351:     if (!quiet) {
3352:       PetscCall(PetscStrncpy(msg, "** PETSc DEPRECATION WARNING ** : the option -", sizeof(msg)));
3353:       PetscCall(PetscStrlcat(msg, prefix, sizeof(msg)));
3354:       PetscCall(PetscStrlcat(msg, oldname + 1, sizeof(msg)));
3355:       PetscCall(PetscStrlcat(msg, " is deprecated as of version ", sizeof(msg)));
3356:       PetscCall(PetscStrlcat(msg, version, sizeof(msg)));
3357:       PetscCall(PetscStrlcat(msg, " and will be removed in a future release.\n", sizeof(msg)));
3358:       if (newname) {
3359:         PetscCall(PetscStrlcat(msg, "   Use the option -", sizeof(msg)));
3360:         PetscCall(PetscStrlcat(msg, prefix, sizeof(msg)));
3361:         PetscCall(PetscStrlcat(msg, newname + 1, sizeof(msg)));
3362:         PetscCall(PetscStrlcat(msg, " instead.", sizeof(msg)));
3363:       }
3364:       if (info) {
3365:         PetscCall(PetscStrlcat(msg, " ", sizeof(msg)));
3366:         PetscCall(PetscStrlcat(msg, info, sizeof(msg)));
3367:       }
3368:       PetscCall(PetscStrlcat(msg, " (Silence this warning with ", sizeof(msg)));
3369:       PetscCall(PetscStrlcat(msg, quietopt, sizeof(msg)));
3370:       PetscCall(PetscStrlcat(msg, ")\n", sizeof(msg)));
3371:       PetscCall(PetscPrintf(comm, "%s", msg));
3372:     }
3373:   }
3374:   PetscFunctionReturn(PETSC_SUCCESS);
3375: }