xref: /petsc/src/sys/objects/options.c (revision 966be33a19c9230d4aa438248a644248d45cc287)
1 
2 /* Define Feature test macros to make sure atoll is available (SVr4, POSIX.1-2001, 4.3BSD, C99), not in (C89 and POSIX.1-1996) */
3 #define PETSC_DESIRE_FEATURE_TEST_MACROS
4 
5 /*
6    These routines simplify the use of command line, file options, etc., and are used to manipulate the options database.
7    This provides the low-level interface, the high level interface is in aoptions.c
8 
9    Some routines use regular malloc and free because it cannot know  what malloc is requested with the
10    options database until it has already processed the input.
11 */
12 
13 #include <petsc/private/petscimpl.h>        /*I  "petscsys.h"   I*/
14 #include <petscviewer.h>
15 #include <ctype.h>
16 #if defined(PETSC_HAVE_MALLOC_H)
17 #include <malloc.h>
18 #endif
19 #if defined(PETSC_HAVE_STRING_H)
20 #include <string.h>             /* strstr */
21 #endif
22 #if defined(PETSC_HAVE_STRINGS_H)
23 #  include <strings.h>          /* strcasecmp */
24 #endif
25 #if defined(PETSC_HAVE_YAML)
26 #include <yaml.h>
27 #endif
28 
29 /*
30     This table holds all the options set by the user. For simplicity, we use a static size database
31 */
32 #define MAXOPTIONS 512
33 #define MAXALIASES 25
34 #define MAXOPTIONSMONITORS 5
35 #define MAXPREFIXES 25
36 
37 struct  _n_PetscOptions {
38   int            N,argc,Naliases;
39   char           **args,*names[MAXOPTIONS],*values[MAXOPTIONS];
40   char           *aliases1[MAXALIASES],*aliases2[MAXALIASES];
41   PetscBool      used[MAXOPTIONS];
42   PetscBool      namegiven;
43   char           programname[PETSC_MAX_PATH_LEN]; /* HP includes entire path in name */
44 
45   /* --------User (or default) routines (most return -1 on error) --------*/
46   PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], void*); /* returns control to user after */
47   PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void**);         /* */
48   void           *monitorcontext[MAXOPTIONSMONITORS];                  /* to pass arbitrary user data into monitor */
49   PetscInt       numbermonitors;                                       /* to, for instance, detect options being set */
50 
51   /* Prefixes */
52   PetscInt prefixind,prefixstack[MAXPREFIXES];
53   char     prefix[2048];
54 };
55 
56 
57 static PetscOptions      defaultoptions = NULL;
58 
59 /*
60     Options events monitor
61 */
62 #define PetscOptionsMonitor(name,value)                              \
63   { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \
64     for (_i=0; _i<_im; _i++) { \
65       _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \
66     } \
67   }
68 
69 #undef __FUNCT__
70 #define __FUNCT__ "PetscOptionsStringToInt"
71 /*
72    PetscOptionsStringToInt - Converts a string to an integer value. Handles special cases such as "default" and "decide"
73 */
74 PetscErrorCode  PetscOptionsStringToInt(const char name[],PetscInt *a)
75 {
76   PetscErrorCode ierr;
77   size_t         i,len;
78   PetscBool      decide,tdefault,mouse;
79 
80   PetscFunctionBegin;
81   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
82   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
83 
84   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
85   if (!tdefault) {
86     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
87   }
88   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
89   if (!decide) {
90     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
91   }
92   ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr);
93 
94   if (tdefault)    *a = PETSC_DEFAULT;
95   else if (decide) *a = PETSC_DECIDE;
96   else if (mouse)  *a = -1;
97   else {
98     if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
99 
100     for (i=1; i<len; i++) {
101       if (name[i] < '0' || name[i] > '9') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
102     }
103 
104 #if defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE_ATOLL)
105     *a = atoll(name);
106 #elif defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE___INT64)
107     *a = _atoi64(name);
108 #else
109     *a = (PetscInt)atoi(name);
110 #endif
111   }
112   PetscFunctionReturn(0);
113 }
114 
115 #if defined(PETSC_USE_REAL___FLOAT128)
116 #include <quadmath.h>
117 #endif
118 
119 #undef __FUNCT__
120 #define __FUNCT__ "PetscOptionsStringToReal"
121 /*
122    Converts a string to PetscReal value. Handles special cases like "default" and "decide"
123 */
124 PetscErrorCode  PetscOptionsStringToReal(const char name[],PetscReal *a)
125 {
126   PetscErrorCode ierr;
127   size_t         len;
128   PetscBool      decide,tdefault;
129 
130   PetscFunctionBegin;
131   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
132   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
133 
134   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
135   if (!tdefault) {
136     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
137   }
138   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
139   if (!decide) {
140     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
141   }
142 
143   if (tdefault)    *a = PETSC_DEFAULT;
144   else if (decide) *a = PETSC_DECIDE;
145   else {
146     if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
147 #if defined(PETSC_USE_REAL___FLOAT128)
148     *a = strtoflt128(name,NULL);
149 #else
150     *a = atof(name);
151 #endif
152   }
153   PetscFunctionReturn(0);
154 }
155 
156 #undef __FUNCT__
157 #define __FUNCT__ "PetscOptionsStringToScalar"
158 /*
159    Converts a string to PetscScalar value. Handles
160       [-][2].0
161       [-][2].0i
162       [-][2].0+/-2.0i
163 
164 */
165 PetscErrorCode  PetscOptionsStringToScalar(const char name[],PetscScalar *a)
166 {
167   PetscErrorCode ierr;
168   size_t         len;
169 
170   PetscFunctionBegin;
171   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
172   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
173 
174   if (name[0] == '+') name++;
175   if (name[0] == 'i') {
176 #if defined(PETSC_USE_COMPLEX)
177     *a = PETSC_i;
178 #else
179     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s is imaginary but complex not supported ",name);
180 #endif
181   } else {
182     PetscToken token;
183     char       *tvalue1,*tvalue2;
184     PetscBool  neg = PETSC_FALSE, negim = PETSC_FALSE;
185     PetscReal  re = 0.0,im = 0.0;
186 
187     if (name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
188     if (name[0] == '-') {
189       neg = PETSC_TRUE;
190       name++;
191     }
192     if (name[0] == 'i') {
193 #if defined(PETSC_USE_COMPLEX)
194       *a = -PETSC_i;
195 #else
196      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s is imaginary but complex not supported ",name);
197 #endif
198       PetscFunctionReturn(0);
199     }
200 
201     ierr = PetscTokenCreate(name,'+',&token);CHKERRQ(ierr);
202     ierr = PetscTokenFind(token,&tvalue1);CHKERRQ(ierr);
203     ierr = PetscTokenFind(token,&tvalue2);CHKERRQ(ierr);
204     if (!tvalue2) {
205       negim = PETSC_TRUE;
206       ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
207       ierr  = PetscTokenCreate(name,'-',&token);CHKERRQ(ierr);
208       ierr  = PetscTokenFind(token,&tvalue1);CHKERRQ(ierr);
209       ierr  = PetscTokenFind(token,&tvalue2);CHKERRQ(ierr);
210     }
211     if (!tvalue2) {
212       PetscBool isim;
213       ierr = PetscStrendswith(tvalue1,"i",&isim);CHKERRQ(ierr);
214       if (isim) {
215         tvalue2 = tvalue1;
216         tvalue1 = NULL;
217         negim   = neg;
218       }
219     } else {
220       PetscBool isim;
221       ierr = PetscStrendswith(tvalue2,"i",&isim);CHKERRQ(ierr);
222       if (!isim) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
223     }
224     if (tvalue1) {
225       ierr = PetscOptionsStringToReal(tvalue1,&re);CHKERRQ(ierr);
226       if (neg) re = -re;
227     }
228     if (tvalue2) {
229       ierr = PetscStrlen(tvalue2,&len);CHKERRQ(ierr);
230       tvalue2[len-1] = 0;
231       ierr = PetscOptionsStringToReal(tvalue2,&im);CHKERRQ(ierr);
232       if (negim) im = -im;
233     }
234     ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
235 #if defined(PETSC_USE_COMPLEX)
236     *a = re + im*PETSC_i;
237 #else
238     if (im != 0.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s is complex but complex not supported ",name);
239     *a = re;
240 #endif
241   }
242   PetscFunctionReturn(0);
243 }
244 
245 #undef __FUNCT__
246 #define __FUNCT__ "PetscOptionsStringToBool"
247 /*
248    PetscOptionsStringToBool - Converts string to PetscBool , handles cases like "yes", "no", "true", "false", "0", "1"
249 */
250 PetscErrorCode  PetscOptionsStringToBool(const char value[], PetscBool  *a)
251 {
252   PetscBool      istrue, isfalse;
253   size_t         len;
254   PetscErrorCode ierr;
255 
256   PetscFunctionBegin;
257   ierr = PetscStrlen(value, &len);CHKERRQ(ierr);
258   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Character string of length zero has no logical value");
259   ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr);
260   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
261   ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr);
262   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
263   ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr);
264   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
265   ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr);
266   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
267   ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
268   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
269   ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
270   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
271   ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
272   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
273   ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr);
274   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
275   SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Unknown logical value: %s", value);
276 }
277 
278 #undef __FUNCT__
279 #define __FUNCT__ "PetscGetProgramName"
280 /*@C
281     PetscGetProgramName - Gets the name of the running program.
282 
283     Not Collective
284 
285     Input Parameter:
286 .   len - length of the string name
287 
288     Output Parameter:
289 .   name - the name of the running program
290 
291    Level: advanced
292 
293     Notes:
294     The name of the program is copied into the user-provided character
295     array of length len.  On some machines the program name includes
296     its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN.
297 @*/
298 PetscErrorCode  PetscGetProgramName(char name[],size_t len)
299 {
300   PetscErrorCode ierr;
301 
302   PetscFunctionBegin;
303    ierr = PetscStrncpy(name,defaultoptions->programname,len);CHKERRQ(ierr);
304   PetscFunctionReturn(0);
305 }
306 
307 #undef __FUNCT__
308 #define __FUNCT__ "PetscSetProgramName"
309 PetscErrorCode  PetscSetProgramName(const char name[])
310 {
311   PetscErrorCode ierr;
312 
313   PetscFunctionBegin;
314   ierr  = PetscStrncpy(defaultoptions->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
315   PetscFunctionReturn(0);
316 }
317 
318 #undef __FUNCT__
319 #define __FUNCT__ "PetscOptionsValidKey"
320 /*@
321     PetscOptionsValidKey - PETSc Options database keys must begin with one or two dashes (-) followed by a letter.
322 
323    Input Parameter:
324 .    in_str - string to check if valid
325 
326    Output Parameter:
327 .    key - PETSC_TRUE if a valid key
328 
329   Level: intermediate
330 
331 @*/
332 PetscErrorCode  PetscOptionsValidKey(const char in_str[],PetscBool  *key)
333 {
334   PetscBool      inf,INF;
335   PetscErrorCode ierr;
336 
337   PetscFunctionBegin;
338   *key = PETSC_FALSE;
339   if (!in_str) PetscFunctionReturn(0);
340   if (in_str[0] != '-') PetscFunctionReturn(0);
341   if (in_str[1] == '-') in_str++;
342   if (!isalpha((int)(in_str[1]))) PetscFunctionReturn(0);
343   ierr = PetscStrncmp(in_str+1,"inf",3,&inf);CHKERRQ(ierr);
344   ierr = PetscStrncmp(in_str+1,"INF",3,&INF);CHKERRQ(ierr);
345   if ((inf || INF) && !(in_str[4] == '_' || isalnum((int)(in_str[4])))) PetscFunctionReturn(0);
346   *key = PETSC_TRUE;
347   PetscFunctionReturn(0);
348 }
349 
350 #undef __FUNCT__
351 #define __FUNCT__ "PetscOptionsInsertString"
352 /*@C
353      PetscOptionsInsertString - Inserts options into the database from a string
354 
355      Not collective: but only processes that call this routine will set the options
356                      included in the string
357 
358   Input Parameter:
359 .   in_str - string that contains options separated by blanks
360 
361 
362   Level: intermediate
363 
364   Contributed by Boyana Norris
365 
366 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
367           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
368           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
369           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
370           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
371           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsInsertFile()
372 
373 @*/
374 PetscErrorCode  PetscOptionsInsertString(PetscOptions options,const char in_str[])
375 {
376   char           *first,*second;
377   PetscErrorCode ierr;
378   PetscToken     token;
379   PetscBool      key,ispush,ispop;
380 
381   PetscFunctionBegin;
382   options = options ? options : defaultoptions;
383   ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr);
384   ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
385   while (first) {
386     ierr = PetscStrcasecmp(first,"-prefix_push",&ispush);CHKERRQ(ierr);
387     ierr = PetscStrcasecmp(first,"-prefix_pop",&ispop);CHKERRQ(ierr);
388     ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr);
389     if (ispush) {
390       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
391       ierr = PetscOptionsPrefixPush(options,second);CHKERRQ(ierr);
392       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
393     } else if (ispop) {
394       ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr);
395       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
396     } else if (key) {
397       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
398       ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr);
399       if (!key) {
400         ierr = PetscOptionsSetValue(options,first,second);CHKERRQ(ierr);
401         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
402       } else {
403         ierr  = PetscOptionsSetValue(options,first,NULL);CHKERRQ(ierr);
404         first = second;
405       }
406     } else {
407       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
408     }
409   }
410   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
411   PetscFunctionReturn(0);
412 }
413 
414 /*
415     Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free()
416 */
417 static char *Petscgetline(FILE * f)
418 {
419   size_t size  = 0;
420   size_t len   = 0;
421   size_t last  = 0;
422   char   *buf  = NULL;
423 
424   if (feof(f)) return 0;
425   do {
426     size += 1024; /* BUFSIZ is defined as "the optimal read size for this platform" */
427     buf   = (char*)realloc((void*)buf,size); /* realloc(NULL,n) is the same as malloc(n) */
428     /* Actually do the read. Note that fgets puts a terminal '\0' on the
429     end of the string, so we make sure we overwrite this */
430     if (!fgets(buf+len,size,f)) buf[len]=0;
431     PetscStrlen(buf,&len);
432     last = len - 1;
433   } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r');
434   if (len) return buf;
435   free(buf);
436   return 0;
437 }
438 
439 
440 #undef __FUNCT__
441 #define __FUNCT__ "PetscOptionsInsertFile"
442 /*@C
443      PetscOptionsInsertFile - Inserts options into the database from a file.
444 
445      Collective on MPI_Comm
446 
447   Input Parameter:
448 +   comm - the processes that will share the options (usually PETSC_COMM_WORLD)
449 .   options - options database, use NULL for default global database
450 .   file - name of file
451 -   require - if PETSC_TRUE will generate an error if the file does not exist
452 
453 
454   Notes: Use  # for lines that are comments and which should be ignored.
455 
456    Usually, instead of using this command, one should list the file name in the call to PetscInitialize(), this insures that certain options
457    such as -log_summary or -malloc_debug are processed properly. This routine only sets options into the options database that will be processed by later
458    calls to XXXSetFromOptions() it should not be used for options listed under PetscInitialize().
459 
460   Level: developer
461 
462 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
463           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
464           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
465           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
466           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
467           PetscOptionsFList(), PetscOptionsEList()
468 
469 @*/
470 PetscErrorCode  PetscOptionsInsertFile(MPI_Comm comm,PetscOptions options,const char file[],PetscBool require)
471 {
472   char           *string,fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*vstring = 0,*astring = 0,*packed = 0;
473   PetscErrorCode ierr;
474   size_t         i,len,bytes;
475   FILE           *fd;
476   PetscToken     token;
477   int            err;
478   char           cmt[1]={'#'},*cmatch;
479   PetscMPIInt    rank,cnt=0,acnt=0,counts[2];
480 
481   PetscFunctionBegin;
482   options = options ? options : defaultoptions;
483   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
484   if (!rank) {
485     cnt        = 0;
486     acnt       = 0;
487 
488     ierr = PetscFixFilename(file,fname);CHKERRQ(ierr);
489     fd   = fopen(fname,"r");
490     if (fd) {
491       PetscSegBuffer vseg,aseg;
492       ierr = PetscSegBufferCreate(1,4000,&vseg);CHKERRQ(ierr);
493       ierr = PetscSegBufferCreate(1,2000,&aseg);CHKERRQ(ierr);
494 
495       /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */
496       ierr = PetscInfo1(0,"Opened options file %s\n",file);CHKERRQ(ierr);
497 
498       while ((string = Petscgetline(fd))) {
499         /* eliminate comments from each line */
500         for (i=0; i<1; i++) {
501           ierr = PetscStrchr(string,cmt[i],&cmatch);CHKERRQ(ierr);
502           if (cmatch) *cmatch = 0;
503         }
504         ierr = PetscStrlen(string,&len);CHKERRQ(ierr);
505         /* replace tabs, ^M, \n with " " */
506         for (i=0; i<len; i++) {
507           if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') {
508             string[i] = ' ';
509           }
510         }
511         ierr = PetscTokenCreate(string,' ',&token);CHKERRQ(ierr);
512         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
513         if (!first) {
514           goto destroy;
515         } else if (!first[0]) { /* if first token is empty spaces, redo first token */
516           ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
517         }
518         ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
519         if (!first) {
520           goto destroy;
521         } else if (first[0] == '-') {
522           ierr = PetscStrlen(first,&len);CHKERRQ(ierr);
523           ierr = PetscSegBufferGet(vseg,len+1,&vstring);CHKERRQ(ierr);
524           ierr = PetscMemcpy(vstring,first,len);CHKERRQ(ierr);
525           vstring[len] = ' ';
526           if (second) {
527             ierr = PetscStrlen(second,&len);CHKERRQ(ierr);
528             ierr = PetscSegBufferGet(vseg,len+3,&vstring);CHKERRQ(ierr);
529             vstring[0] = '"';
530             ierr = PetscMemcpy(vstring+1,second,len);CHKERRQ(ierr);
531             vstring[len+1] = '"';
532             vstring[len+2] = ' ';
533           }
534         } else {
535           PetscBool match;
536 
537           ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr);
538           if (match) {
539             ierr = PetscTokenFind(token,&third);CHKERRQ(ierr);
540             if (!third) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second);
541             ierr = PetscStrlen(second,&len);CHKERRQ(ierr);
542             ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr);
543             ierr = PetscMemcpy(astring,second,len);CHKERRQ(ierr);
544             astring[len] = ' ';
545 
546             ierr = PetscStrlen(third,&len);CHKERRQ(ierr);
547             ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr);
548             ierr = PetscMemcpy(astring,third,len);CHKERRQ(ierr);
549             astring[len] = ' ';
550           } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown statement in options file: (%s)",string);
551         }
552 destroy:
553         free(string);
554         ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
555       }
556       err = fclose(fd);
557       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
558       ierr = PetscSegBufferGetSize(aseg,&bytes);CHKERRQ(ierr); /* size without null termination */
559       ierr = PetscMPIIntCast(bytes,&acnt);CHKERRQ(ierr);
560       ierr = PetscSegBufferGet(aseg,1,&astring);CHKERRQ(ierr);
561       astring[0] = 0;
562       ierr = PetscSegBufferGetSize(vseg,&bytes);CHKERRQ(ierr); /* size without null termination */
563       ierr = PetscMPIIntCast(bytes,&cnt);CHKERRQ(ierr);
564       ierr = PetscSegBufferGet(vseg,1,&vstring);CHKERRQ(ierr);
565       vstring[0] = 0;
566       ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr);
567       ierr = PetscSegBufferExtractTo(aseg,packed);CHKERRQ(ierr);
568       ierr = PetscSegBufferExtractTo(vseg,packed+acnt+1);CHKERRQ(ierr);
569       ierr = PetscSegBufferDestroy(&aseg);CHKERRQ(ierr);
570       ierr = PetscSegBufferDestroy(&vseg);CHKERRQ(ierr);
571     } else if (require) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Unable to open Options File %s",fname);
572   }
573 
574   counts[0] = acnt;
575   counts[1] = cnt;
576   ierr = MPI_Bcast(counts,2,MPI_INT,0,comm);CHKERRQ(ierr);
577   acnt = counts[0];
578   cnt = counts[1];
579   if (rank) {
580     ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr);
581   }
582   if (acnt || cnt) {
583     ierr = MPI_Bcast(packed,2+acnt+cnt,MPI_CHAR,0,comm);CHKERRQ(ierr);
584     astring = packed;
585     vstring = packed + acnt + 1;
586   }
587 
588   if (acnt) {
589     PetscToken token;
590     char       *first,*second;
591 
592     ierr = PetscTokenCreate(astring,' ',&token);CHKERRQ(ierr);
593     ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
594     while (first) {
595       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
596       ierr = PetscOptionsSetAlias(options,first,second);CHKERRQ(ierr);
597       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
598     }
599     ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
600   }
601 
602   if (cnt) {
603     ierr = PetscOptionsInsertString(options,vstring);CHKERRQ(ierr);
604   }
605   ierr = PetscFree(packed);CHKERRQ(ierr);
606   PetscFunctionReturn(0);
607 }
608 
609 #undef __FUNCT__
610 #define __FUNCT__ "PetscOptionsInsertArgs_Private"
611 static PetscErrorCode PetscOptionsInsertArgs_Private(PetscOptions options,int argc,char *args[])
612 {
613   PetscErrorCode ierr;
614   int            left    = argc - 1;
615   char           **eargs = args + 1;
616 
617   PetscFunctionBegin;
618   options = options ? options : defaultoptions;
619   while (left) {
620     PetscBool isoptions_file,isprefixpush,isprefixpop,isp4,tisp4,isp4yourname,isp4rmrank,key;
621     ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr);
622     ierr = PetscStrcasecmp(eargs[0],"-prefix_push",&isprefixpush);CHKERRQ(ierr);
623     ierr = PetscStrcasecmp(eargs[0],"-prefix_pop",&isprefixpop);CHKERRQ(ierr);
624     ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr);
625     ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr);
626     ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr);
627     ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr);
628     isp4 = (PetscBool) (isp4 || tisp4);
629     ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr);
630     isp4 = (PetscBool) (isp4 || tisp4);
631     ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr);
632     ierr = PetscOptionsValidKey(eargs[0],&key);CHKERRQ(ierr);
633 
634     if (!key) {
635       eargs++; left--;
636     } else if (isoptions_file) {
637       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
638       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
639       ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,eargs[1],PETSC_TRUE);CHKERRQ(ierr);
640       eargs += 2; left -= 2;
641     } else if (isprefixpush) {
642       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option");
643       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option (prefixes cannot start with '-')");
644       ierr = PetscOptionsPrefixPush(options,eargs[1]);CHKERRQ(ierr);
645       eargs += 2; left -= 2;
646     } else if (isprefixpop) {
647       ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr);
648       eargs++; left--;
649 
650       /*
651        These are "bad" options that MPICH, etc put on the command line
652        we strip them out here.
653        */
654     } else if (tisp4 || isp4rmrank) {
655       eargs += 1; left -= 1;
656     } else if (isp4 || isp4yourname) {
657       eargs += 2; left -= 2;
658     } else {
659       PetscBool nextiskey = PETSC_FALSE;
660       if (left >= 2) {ierr = PetscOptionsValidKey(eargs[1],&nextiskey);CHKERRQ(ierr);}
661       if (left < 2 || nextiskey) {
662         ierr = PetscOptionsSetValue(options,eargs[0],NULL);CHKERRQ(ierr);
663         eargs++; left--;
664       } else {
665         ierr = PetscOptionsSetValue(options,eargs[0],eargs[1]);CHKERRQ(ierr);
666         eargs += 2; left -= 2;
667       }
668     }
669   }
670   PetscFunctionReturn(0);
671 }
672 
673 
674 #undef __FUNCT__
675 #define __FUNCT__ "PetscOptionsInsert"
676 /*@C
677    PetscOptionsInsert - Inserts into the options database from the command line,
678                    the environmental variable and a file.
679 
680    Input Parameters:
681 +  options - options database or NULL for the default global database
682 .  argc - count of number of command line arguments
683 .  args - the command line arguments
684 -  file - optional filename, defaults to ~username/.petscrc
685 
686    Note:
687    Since PetscOptionsInsert() is automatically called by PetscInitialize(),
688    the user does not typically need to call this routine. PetscOptionsInsert()
689    can be called several times, adding additional entries into the database.
690 
691    Options Database Keys:
692 +   -options_monitor <optional filename> - print options names and values as they are set
693 .   -options_file <filename> - read options from a file
694 
695    Level: advanced
696 
697    Concepts: options database^adding
698 
699 .seealso: PetscOptionsDestroy_Private(), PetscOptionsView(), PetscOptionsInsertString(), PetscOptionsInsertFile(),
700           PetscInitialize()
701 @*/
702 PetscErrorCode  PetscOptionsInsert(PetscOptions options,int *argc,char ***args,const char file[])
703 {
704   PetscErrorCode ierr;
705   PetscMPIInt    rank;
706   char           pfile[PETSC_MAX_PATH_LEN];
707   PetscBool      flag = PETSC_FALSE;
708 
709 
710   PetscFunctionBegin;
711   if (!options) {
712     if (!defaultoptions) {
713       ierr = PetscOptionsCreateDefault();CHKERRQ(ierr);
714     }
715     options = defaultoptions;
716   }
717   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
718 
719   options->argc = (argc) ? *argc : 0;
720   options->args = (args) ? *args : NULL;
721 
722   if (file && file[0]) {
723     char fullpath[PETSC_MAX_PATH_LEN];
724 
725     ierr = PetscStrreplace(PETSC_COMM_WORLD,file,fullpath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
726     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,fullpath,PETSC_TRUE);CHKERRQ(ierr);
727   }
728   /*
729      We want to be able to give -skip_petscrc on the command line, but need to parse it first.  Since the command line
730      should take precedence, we insert it twice.  It would be sufficient to just scan for -skip_petscrc.
731   */
732   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(options,*argc,*args);CHKERRQ(ierr);}
733   ierr = PetscOptionsGetBool(NULL,NULL,"-skip_petscrc",&flag,NULL);CHKERRQ(ierr);
734   if (!flag) {
735     ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
736     /* PetscOptionsInsertFile() does a fopen() on rank0 only - so only rank0 HomeDir value is relavent */
737     if (pfile[0]) { ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr); }
738     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,pfile,PETSC_FALSE);CHKERRQ(ierr);
739     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,".petscrc",PETSC_FALSE);CHKERRQ(ierr);
740     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,"petscrc",PETSC_FALSE);CHKERRQ(ierr);
741   }
742 
743   /* insert environmental options */
744   {
745     char   *eoptions = 0;
746     size_t len       = 0;
747     if (!rank) {
748       eoptions = (char*)getenv("PETSC_OPTIONS");
749       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
750       ierr     = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
751     } else {
752       ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
753       if (len) {
754         ierr = PetscMalloc1(len+1,&eoptions);CHKERRQ(ierr);
755       }
756     }
757     if (len) {
758       ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
759       if (rank) eoptions[len] = 0;
760       ierr = PetscOptionsInsertString(options,eoptions);CHKERRQ(ierr);
761       if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);}
762     }
763   }
764 
765 #if defined(PETSC_HAVE_YAML)
766   char      yaml_file[PETSC_MAX_PATH_LEN];
767   PetscBool yaml_flg = PETSC_FALSE;
768   ierr = PetscOptionsGetString(NULL,NULL,"-options_file_yaml",yaml_file,PETSC_MAX_PATH_LEN,&yaml_flg);CHKERRQ(ierr);
769   if (yaml_flg) ierr = PetscOptionsInsertFileYAML(PETSC_COMM_WORLD,yaml_file,PETSC_TRUE);CHKERRQ(ierr);
770 #endif
771 
772   /* insert command line options again because they take precedence over arguments in petscrc/environment */
773   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(options,*argc,*args);CHKERRQ(ierr);}
774   PetscFunctionReturn(0);
775 }
776 
777 #undef __FUNCT__
778 #define __FUNCT__ "PetscOptionsView"
779 /*@C
780    PetscOptionsView - Prints the options that have been loaded. This is
781    useful for debugging purposes.
782 
783    Logically Collective on PetscViewer
784 
785    Input Parameter:
786 .  viewer - must be an PETSCVIEWERASCII viewer
787 
788    Options Database Key:
789 .  -options_table - Activates PetscOptionsView() within PetscFinalize()
790 
791    Level: advanced
792 
793    Concepts: options database^printing
794 
795 .seealso: PetscOptionsAllUsed()
796 @*/
797 PetscErrorCode  PetscOptionsView(PetscOptions options,PetscViewer viewer)
798 {
799   PetscErrorCode ierr;
800   PetscInt       i;
801   PetscBool      isascii;
802 
803   PetscFunctionBegin;
804   options = options ? options : defaultoptions;
805   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
806   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
807   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only supports ASCII viewer");
808 
809   if (options->N) {
810     ierr = PetscViewerASCIIPrintf(viewer,"#PETSc Option Table entries:\n");CHKERRQ(ierr);
811   } else {
812     ierr = PetscViewerASCIIPrintf(viewer,"#No PETSc Option Table entries\n");CHKERRQ(ierr);
813   }
814   for (i=0; i<options->N; i++) {
815     if (options->values[i]) {
816       ierr = PetscViewerASCIIPrintf(viewer,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
817     } else {
818       ierr = PetscViewerASCIIPrintf(viewer,"-%s\n",options->names[i]);CHKERRQ(ierr);
819     }
820   }
821   if (options->N) {
822     ierr = PetscViewerASCIIPrintf(viewer,"#End of PETSc Option Table entries\n");CHKERRQ(ierr);
823   }
824   PetscFunctionReturn(0);
825 }
826 
827 #undef __FUNCT__
828 #define __FUNCT__ "PetscOptionsViewError"
829 /*
830    Called by error handlers to print options used in run
831 */
832 PetscErrorCode  PetscOptionsViewError(void)
833 {
834   PetscInt       i;
835   PetscOptions   options = defaultoptions;
836 
837   PetscFunctionBegin;
838   if (options->N) {
839     (*PetscErrorPrintf)("PETSc Option Table entries:\n");
840   } else {
841     (*PetscErrorPrintf)("No PETSc Option Table entries\n");
842   }
843   for (i=0; i<options->N; i++) {
844     if (options->values[i]) {
845       (*PetscErrorPrintf)("-%s %s\n",options->names[i],options->values[i]);
846     } else {
847       (*PetscErrorPrintf)("-%s\n",options->names[i]);
848     }
849   }
850   PetscFunctionReturn(0);
851 }
852 
853 #undef __FUNCT__
854 #define __FUNCT__ "PetscOptionsGetAll"
855 /*@C
856    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
857 
858    Not Collective
859 
860    Input Paramter:
861 .  options - the options database, use NULL for the default global database
862 
863    Output Parameter:
864 .  copts - pointer where string pointer is stored
865 
866    Notes: the array and each entry in the array should be freed with PetscFree()
867 
868    Level: advanced
869 
870    Concepts: options database^listing
871 
872 .seealso: PetscOptionsAllUsed(), PetscOptionsView()
873 @*/
874 PetscErrorCode  PetscOptionsGetAll(PetscOptions options,char *copts[])
875 {
876   PetscErrorCode ierr;
877   PetscInt       i;
878   size_t         len       = 1,lent = 0;
879   char           *coptions = NULL;
880 
881   PetscFunctionBegin;
882   options = options ? options : defaultoptions;
883 
884   /* count the length of the required string */
885   for (i=0; i<options->N; i++) {
886     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
887     len += 2 + lent;
888     if (options->values[i]) {
889       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
890       len += 1 + lent;
891     }
892   }
893   ierr = PetscMalloc1(len,&coptions);CHKERRQ(ierr);
894   coptions[0] = 0;
895   for (i=0; i<options->N; i++) {
896     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
897     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
898     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
899     if (options->values[i]) {
900       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
901       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
902     }
903   }
904   *copts = coptions;
905   PetscFunctionReturn(0);
906 }
907 
908 #undef __FUNCT__
909 #define __FUNCT__ "PetscOptionsPrefixPush"
910 /*@C
911    PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow.
912 
913    Not Collective, but prefix will only be applied on calling ranks
914 
915    Input Parameter:
916 +  options - options database, or NULL for the default global database
917 -  prefix - The string to append to the existing prefix
918 
919    Options Database Keys:
920  +   -prefix_push <some_prefix_> - push the given prefix
921  -   -prefix_pop - pop the last prefix
922 
923    Notes:
924    It is common to use this in conjunction with -options_file as in
925 
926  $ -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop
927 
928    where the files no longer require all options to be prefixed with -system2_.
929 
930 Level: advanced
931 
932 .seealso: PetscOptionsPrefixPop()
933 @*/
934 PetscErrorCode  PetscOptionsPrefixPush(PetscOptions options,const char prefix[])
935 {
936   PetscErrorCode ierr;
937   size_t         n;
938   PetscInt       start;
939   char           buf[2048];
940   PetscBool      key;
941 
942   PetscFunctionBegin;
943   PetscValidCharPointer(prefix,1);
944   options = options ? options : defaultoptions;
945   /* Want to check validity of the key using PetscOptionsValidKey(), which requires that the first character is a '-' */
946   buf[0] = '-';
947   ierr = PetscStrncpy(buf+1,prefix,sizeof(buf) - 1);CHKERRQ(ierr);
948   buf[sizeof(buf) - 1] = 0;
949   ierr = PetscOptionsValidKey(buf,&key);CHKERRQ(ierr);
950   if (!key) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Given prefix \"%s\" not valid (the first character must be a letter, do not include leading '-')",prefix);
951 
952   if (!options) {
953     ierr = PetscOptionsInsert(NULL,0,0,0);CHKERRQ(ierr);
954     options = defaultoptions;
955   }
956   if (options->prefixind >= MAXPREFIXES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum depth of prefix stack %d exceeded, recompile \n src/sys/objects/options.c with larger value for MAXPREFIXES",MAXPREFIXES);
957   start = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
958   ierr = PetscStrlen(prefix,&n);CHKERRQ(ierr);
959   if (n+1 > sizeof(options->prefix)-start) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum prefix length %d exceeded",sizeof(options->prefix));
960   ierr = PetscMemcpy(options->prefix+start,prefix,n+1);CHKERRQ(ierr);
961   options->prefixstack[options->prefixind++] = start+n;
962   PetscFunctionReturn(0);
963 }
964 
965 #undef __FUNCT__
966 #define __FUNCT__ "PetscOptionsPrefixPop"
967 /*@C
968    PetscOptionsPrefixPop - Remove the latest options prefix, see PetscOptionsPrefixPush() for details
969 
970    Not  Collective, but prefix will only be popped on calling ranks
971 
972   Input Parameters:
973 .  options - options database, or NULL for the default global database
974 
975    Level: advanced
976 
977 .seealso: PetscOptionsPrefixPush()
978 @*/
979 PetscErrorCode  PetscOptionsPrefixPop(PetscOptions options)
980 {
981   PetscInt offset;
982 
983   PetscFunctionBegin;
984   options = options ? options : defaultoptions;
985   if (options->prefixind < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More prefixes popped than pushed");
986   options->prefixind--;
987   offset = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
988   options->prefix[offset] = 0;
989   PetscFunctionReturn(0);
990 }
991 
992 #undef __FUNCT__
993 #define __FUNCT__ "PetscOptionsClear"
994 /*@C
995     PetscOptionsClear - Removes all options form the database leaving it empty.
996 
997   Input Parameters:
998 .  options - options database, use NULL for the default global database
999 
1000    Level: developer
1001 
1002 .seealso: PetscOptionsInsert()
1003 @*/
1004 PetscErrorCode  PetscOptionsClear(PetscOptions options)
1005 {
1006   PetscInt     i;
1007 
1008   PetscFunctionBegin;
1009   options = options ? options : defaultoptions;
1010   for (i=0; i<options->N; i++) {
1011     if (options->names[i])  free(options->names[i]);
1012     if (options->values[i]) free(options->values[i]);
1013   }
1014   for (i=0; i<options->Naliases; i++) {
1015     free(options->aliases1[i]);
1016     free(options->aliases2[i]);
1017   }
1018   options->prefix[0] = 0;
1019   options->prefixind = 0;
1020   options->N         = 0;
1021   options->Naliases  = 0;
1022   PetscFunctionReturn(0);
1023 }
1024 
1025 #undef __FUNCT__
1026 #define __FUNCT__ "PetscObjectSetPrintedOptions"
1027 /*@
1028     PetscObjectSetPrintedOptions - indicate to an object that it should behave as if it has already printed the help for its options
1029 
1030   Input Parameters:
1031 .  obj  - the PetscObject
1032 
1033    Level: developer
1034 
1035    Developer Notes: This is used, for example to prevent sequential objects that are created from a parallel object; such as the KSP created by
1036     PCBJACOBI from all printing the same help messages to the screen
1037 
1038 .seealso: PetscOptionsInsert()
1039 @*/
1040 PetscErrorCode  PetscObjectSetPrintedOptions(PetscObject obj)
1041 {
1042   PetscFunctionBegin;
1043   obj->optionsprinted = PETSC_TRUE;
1044   PetscFunctionReturn(0);
1045 }
1046 
1047 #undef __FUNCT__
1048 #define __FUNCT__ "PetscObjectInheritPrintedOptions"
1049 /*@
1050     PetscObjectInheritPrintedOptions - If the child object is not on the rank 0 process of the parent object and the child is sequential then the child gets it set.
1051 
1052   Input Parameters:
1053 +  pobj - the parent object
1054 -  obj  - the PetscObject
1055 
1056    Level: developer
1057 
1058    Developer Notes: This is used, for example to prevent sequential objects that are created from a parallel object; such as the KSP created by
1059     PCBJACOBI from all printing the same help messages to the screen
1060 
1061     This will not handle more complicated situations like with GASM where children may live on any subset of the parent's processes and overlap
1062 
1063 .seealso: PetscOptionsInsert(), PetscObjectSetPrintedOptions()
1064 @*/
1065 PetscErrorCode  PetscObjectInheritPrintedOptions(PetscObject pobj,PetscObject obj)
1066 {
1067   PetscErrorCode ierr;
1068   PetscMPIInt    prank,size;
1069 
1070   PetscFunctionBegin;
1071   ierr = MPI_Comm_rank(pobj->comm,&prank);CHKERRQ(ierr);
1072   ierr = MPI_Comm_size(obj->comm,&size);CHKERRQ(ierr);
1073   if (size == 1 && prank > 0) obj->optionsprinted = PETSC_TRUE;
1074   PetscFunctionReturn(0);
1075 }
1076 
1077 #undef __FUNCT__
1078 #define __FUNCT__ "PetscOptionsDestroy"
1079 /*@
1080     PetscOptionsDestroy - Destroys an option database.
1081 
1082   Input Parameter:
1083 .  options - the PetscOptions object
1084 
1085    Level: developer
1086 
1087 .seealso: PetscOptionsInsert()
1088 @*/
1089 PetscErrorCode  PetscOptionsDestroy(PetscOptions *options)
1090 {
1091   PetscErrorCode ierr;
1092 
1093   PetscFunctionBegin;
1094   ierr = PetscOptionsClear(*options);CHKERRQ(ierr);
1095   free(*options);
1096   *options = NULL;
1097   PetscFunctionReturn(0);
1098 }
1099 
1100 #undef __FUNCT__
1101 #define __FUNCT__ "PetscOptionsDestroyDefault"
1102 PetscErrorCode  PetscOptionsDestroyDefault(void)
1103 {
1104   PetscErrorCode ierr;
1105 
1106   ierr = PetscOptionsDestroy(&defaultoptions);if (ierr) return ierr;
1107   return 0;
1108 }
1109 
1110 
1111 #undef __FUNCT__
1112 #define __FUNCT__ "PetscOptionsSetValue"
1113 /*@C
1114    PetscOptionsSetValue - Sets an option name-value pair in the options
1115    database, overriding whatever is already present.
1116 
1117    Not collective, but setting values on certain processors could cause problems
1118    for parallel objects looking for options.
1119 
1120    Input Parameters:
1121 +  options - options database, use NULL for the default global database
1122 .  name - name of option, this SHOULD have the - prepended
1123 -  value - the option value (not used for all options)
1124 
1125    Level: intermediate
1126 
1127    Note:
1128    This function can be called BEFORE PetscInitialize()
1129 
1130    Only some options have values associated with them, such as
1131    -ksp_rtol tol.  Other options stand alone, such as -ksp_monitor.
1132 
1133   Developers Note: Uses malloc() directly because PETSc may not yet have been fully initialized
1134 
1135   Concepts: options database^adding option
1136 
1137 .seealso: PetscOptionsInsert()
1138 @*/
1139 PetscErrorCode  PetscOptionsSetValue(PetscOptions options,const char iname[],const char value[])
1140 {
1141   size_t         len;
1142   PetscErrorCode ierr;
1143   PetscInt       N,n,i;
1144   char           **names;
1145   char           fullname[2048];
1146   const char     *name = iname;
1147   int            gt,match;
1148 
1149   if (!options) {
1150     if (!defaultoptions) {
1151       ierr = PetscOptionsCreateDefault();
1152       if (ierr) return ierr;
1153     }
1154     options = defaultoptions;
1155   }
1156 
1157   /* this is so that -h and -help are equivalent (p4 does not like -help)*/
1158   match = strcmp(name,"-h");
1159   if (!match) name = "-help";
1160 
1161   name++; /* skip starting hyphen */
1162   if (options->prefixind > 0) {
1163     strncpy(fullname,options->prefix,sizeof(fullname));
1164     strncat(fullname,name,sizeof(fullname)-strlen(fullname)-1);
1165     name = fullname;
1166   }
1167 
1168   /* check against aliases */
1169   N = options->Naliases;
1170   for (i=0; i<N; i++) {
1171 #if defined(PETSC_HAVE_STRCASECMP)
1172     match = strcasecmp(options->aliases1[i],name);
1173 #elif defined(PETSC_HAVE_STRICMP)
1174     match = stricmp(options->aliases1[i],name);
1175 #else
1176     Error
1177 #endif
1178     if (!match) {
1179       name = options->aliases2[i];
1180       break;
1181     }
1182   }
1183 
1184   N     = options->N;
1185   n     = N;
1186   names = options->names;
1187 
1188   for (i=0; i<N; i++) {
1189 #if defined(PETSC_HAVE_STRCASECMP)
1190     gt = strcasecmp(names[i],name);
1191 #elif defined(PETSC_HAVE_STRICMP)
1192     gt = stricmp(names[i],name);
1193 #else
1194     Error
1195 #endif
1196     if (!gt) {
1197       if (options->values[i]) free(options->values[i]);
1198       len = value ? strlen(value) : 0;
1199       if (len) {
1200         options->values[i] = (char*)malloc((len+1)*sizeof(char));
1201         if (!options->values[i]) return PETSC_ERR_MEM;
1202         strcpy(options->values[i],value);
1203       } else options->values[i] = 0;
1204       return 0;
1205     } else if (gt > 0) {
1206       n = i;
1207       break;
1208     }
1209   }
1210   if (N >= MAXOPTIONS) abort();
1211 
1212   /* shift remaining values down 1 */
1213   for (i=N; i>n; i--) {
1214     options->names[i]  = options->names[i-1];
1215     options->values[i] = options->values[i-1];
1216     options->used[i]   = options->used[i-1];
1217   }
1218   /* insert new name and value */
1219   len = strlen(name);
1220   options->names[n] = (char*)malloc((len+1)*sizeof(char));
1221   if (!options->names[n]) return PETSC_ERR_MEM;
1222   strcpy(options->names[n],name);
1223   len = value ? strlen(value) : 0;
1224   if (len) {
1225     options->values[n] = (char*)malloc((len+1)*sizeof(char));
1226     if (!options->values[n]) return PETSC_ERR_MEM;
1227     strcpy(options->values[n],value);
1228   } else options->values[n] = NULL;
1229   options->used[n] = PETSC_FALSE;
1230   options->N++;
1231   return 0;
1232 }
1233 
1234 #undef __FUNCT__
1235 #define __FUNCT__ "PetscOptionsClearValue"
1236 /*@C
1237    PetscOptionsClearValue - Clears an option name-value pair in the options
1238    database, overriding whatever is already present.
1239 
1240    Not Collective, but setting values on certain processors could cause problems
1241    for parallel objects looking for options.
1242 
1243    Input Parameter:
1244 +  options - options database, use NULL for the default global database
1245 .  name - name of option, this SHOULD have the - prepended
1246 
1247    Level: intermediate
1248 
1249    Concepts: options database^removing option
1250 .seealso: PetscOptionsInsert()
1251 @*/
1252 PetscErrorCode  PetscOptionsClearValue(PetscOptions options,const char iname[])
1253 {
1254   PetscErrorCode ierr;
1255   PetscInt       N,n,i;
1256   char           **names,*name=(char*)iname;
1257   PetscBool      gt,match;
1258 
1259   PetscFunctionBegin;
1260   options = options ? options : defaultoptions;
1261   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1262   name++;
1263 
1264   N     = options->N; n = 0;
1265   names = options->names;
1266 
1267   for (i=0; i<N; i++) {
1268     ierr  = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
1269     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
1270     if (match) {
1271       if (options->names[i])  free(options->names[i]);
1272       if (options->values[i]) free(options->values[i]);
1273       PetscOptionsMonitor(name,"");
1274       break;
1275     } else if (gt) PetscFunctionReturn(0); /* it was not listed */
1276 
1277     n++;
1278   }
1279   if (n == N) PetscFunctionReturn(0); /* it was not listed */
1280 
1281   /* shift remaining values down 1 */
1282   for (i=n; i<N-1; i++) {
1283     options->names[i]  = options->names[i+1];
1284     options->values[i] = options->values[i+1];
1285     options->used[i]   = options->used[i+1];
1286   }
1287   options->N--;
1288   PetscFunctionReturn(0);
1289 }
1290 
1291 #undef __FUNCT__
1292 #define __FUNCT__ "PetscOptionsSetAlias"
1293 /*@C
1294    PetscOptionsSetAlias - Makes a key and alias for another key
1295 
1296    Not Collective, but setting values on certain processors could cause problems
1297    for parallel objects looking for options.
1298 
1299    Input Parameters:
1300 +  options - options database or NULL for default global database
1301 .  inewname - the alias
1302 -  ioldname - the name that alias will refer to
1303 
1304    Level: advanced
1305 
1306 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1307            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1308           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1309           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1310           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1311           PetscOptionsFList(), PetscOptionsEList()
1312 @*/
1313 PetscErrorCode  PetscOptionsSetAlias(PetscOptions options,const char inewname[],const char ioldname[])
1314 {
1315   PetscErrorCode ierr;
1316   PetscInt       n = options->Naliases;
1317   size_t         len;
1318   char           *newname = (char*)inewname,*oldname = (char*)ioldname;
1319 
1320   PetscFunctionBegin;
1321   options = options ? options : defaultoptions;
1322   if (newname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname);
1323   if (oldname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname);
1324   if (n >= MAXALIASES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEM,"You have defined to many PETSc options aliases, limit %d recompile \n  src/sys/objects/options.c with larger value for MAXALIASES",MAXALIASES);
1325 
1326   newname++; oldname++;
1327   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
1328   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
1329   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
1330   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
1331   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
1332   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
1333   options->Naliases++;
1334   PetscFunctionReturn(0);
1335 }
1336 
1337 #undef __FUNCT__
1338 #define __FUNCT__ "PetscOptionsFindPair_Private"
1339 PetscErrorCode PetscOptionsFindPair_Private(PetscOptions options,const char pre[],const char name[],char *value[],PetscBool  *flg)
1340 {
1341   PetscErrorCode ierr;
1342   PetscInt       i,N;
1343   size_t         len;
1344   char           **names,tmp[256];
1345   PetscBool      match;
1346 
1347   PetscFunctionBegin;
1348   options = options ? options : defaultoptions;
1349   N     = options->N;
1350   names = options->names;
1351 
1352   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1353 
1354   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1355   if (pre) {
1356     char       *ptr   = tmp;
1357     const char *namep = name;
1358     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
1359     if (name[1] == '-') {
1360       *ptr++ = '-';
1361       namep++;
1362     }
1363     ierr = PetscStrncpy(ptr,pre,tmp+sizeof(tmp)-ptr);CHKERRQ(ierr);
1364     tmp[sizeof(tmp)-1] = 0;
1365     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1366     ierr = PetscStrncat(tmp,namep+1,sizeof(tmp)-len-1);CHKERRQ(ierr);
1367   } else {
1368     ierr = PetscStrncpy(tmp,name+1,sizeof(tmp));CHKERRQ(ierr);
1369     tmp[sizeof(tmp)-1] = 0;
1370   }
1371 #if defined(PETSC_USE_DEBUG)
1372   {
1373     PetscBool valid;
1374     char      key[sizeof(tmp)+1] = "-";
1375 
1376     ierr = PetscMemcpy(key+1,tmp,sizeof(tmp));CHKERRQ(ierr);
1377     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1378     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1379   }
1380 #endif
1381 
1382   /* slow search */
1383   *flg = PETSC_FALSE;
1384   for (i=0; i<N; i++) {
1385     ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr);
1386     if (match) {
1387       *value           = options->values[i];
1388       options->used[i] = PETSC_TRUE;
1389       *flg             = PETSC_TRUE;
1390       break;
1391     }
1392   }
1393   if (!*flg) {
1394     PetscInt j,cnt = 0,locs[16],loce[16];
1395     size_t   n;
1396     ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr);
1397     /* determine the location and number of all _%d_ in the key */
1398     for (i=0; i< (PetscInt)n; i++) {
1399       if (tmp[i] == '_') {
1400         for (j=i+1; j< (PetscInt)n; j++) {
1401           if (tmp[j] >= '0' && tmp[j] <= '9') continue;
1402           if (tmp[j] == '_' && j > i+1) { /* found a number */
1403             locs[cnt]   = i+1;
1404             loce[cnt++] = j+1;
1405           }
1406           break;
1407         }
1408       }
1409     }
1410     if (cnt) {
1411       char tmp2[256];
1412       for (i=0; i<cnt; i++) {
1413         ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr);
1414         ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr);
1415         ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr);
1416         ierr = PetscOptionsFindPair_Private(options,NULL,tmp2,value,flg);CHKERRQ(ierr);
1417         if (*flg) break;
1418       }
1419     }
1420   }
1421   PetscFunctionReturn(0);
1422 }
1423 
1424 #undef __FUNCT__
1425 #define __FUNCT__ "PetscOptionsFindPairPrefix_Private"
1426 PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions options,const char pre[], const char name[], char *value[], PetscBool *flg)
1427 {
1428   PetscErrorCode ierr;
1429   PetscInt       i,N;
1430   size_t         len;
1431   char           **names,tmp[256];
1432   PetscBool      match;
1433 
1434   PetscFunctionBegin;
1435   options = options ? options : defaultoptions;
1436   N     = options->N;
1437   names = options->names;
1438 
1439   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1440 
1441   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1442   if (pre) {
1443     char       *ptr   = tmp;
1444     const char *namep = name;
1445     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
1446     if (name[1] == '-') {
1447       *ptr++ = '-';
1448       namep++;
1449     }
1450     ierr = PetscStrncpy(ptr,pre,tmp+sizeof(tmp)-ptr);CHKERRQ(ierr);
1451     tmp[sizeof(tmp)-1] = 0;
1452     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1453     ierr = PetscStrncat(tmp,namep+1,sizeof(tmp)-len-1);CHKERRQ(ierr);
1454   } else {
1455     ierr = PetscStrncpy(tmp,name+1,sizeof(tmp));CHKERRQ(ierr);
1456     tmp[sizeof(tmp)-1] = 0;
1457   }
1458 #if defined(PETSC_USE_DEBUG)
1459   {
1460     PetscBool valid;
1461     char      key[sizeof(tmp)+1] = "-";
1462 
1463     ierr = PetscMemcpy(key+1,tmp,sizeof(tmp));CHKERRQ(ierr);
1464     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1465     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1466   }
1467 #endif
1468 
1469   /* slow search */
1470   *flg = PETSC_FALSE;
1471   ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1472   for (i = 0; i < N; ++i) {
1473     ierr = PetscStrncmp(names[i], tmp, len, &match);CHKERRQ(ierr);
1474     if (match) {
1475       if (value) *value = options->values[i];
1476       options->used[i]  = PETSC_TRUE;
1477       if (flg)   *flg   = PETSC_TRUE;
1478       break;
1479     }
1480   }
1481   PetscFunctionReturn(0);
1482 }
1483 
1484 #undef __FUNCT__
1485 #define __FUNCT__ "PetscOptionsReject"
1486 /*@C
1487    PetscOptionsReject - Generates an error if a certain option is given.
1488 
1489    Not Collective, but setting values on certain processors could cause problems
1490    for parallel objects looking for options.
1491 
1492    Input Parameters:
1493 +  options - options database use NULL for default global database
1494 .  name - the option one is seeking
1495 -  mess - error message (may be NULL)
1496 
1497    Level: advanced
1498 
1499    Concepts: options database^rejecting option
1500 
1501 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1502            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1503           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1504           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1505           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1506           PetscOptionsFList(), PetscOptionsEList()
1507 @*/
1508 PetscErrorCode  PetscOptionsReject(PetscOptions options,const char name[],const char mess[])
1509 {
1510   PetscErrorCode ierr;
1511   PetscBool      flag = PETSC_FALSE;
1512 
1513   PetscFunctionBegin;
1514   options = options ? options : defaultoptions;
1515   ierr = PetscOptionsHasName(options,NULL,name,&flag);CHKERRQ(ierr);
1516   if (flag) {
1517     if (mess) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess);
1518     else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name);
1519   }
1520   PetscFunctionReturn(0);
1521 }
1522 
1523 #undef __FUNCT__
1524 #define __FUNCT__ "PetscOptionsHasName"
1525 /*@C
1526    PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even
1527                       its value is set to false.
1528 
1529    Not Collective
1530 
1531    Input Parameters:
1532 +  options - options database use NULL for default global database
1533 .  name - the option one is seeking
1534 -  pre - string to prepend to the name or NULL
1535 
1536    Output Parameters:
1537 .  set - PETSC_TRUE if found else PETSC_FALSE.
1538 
1539    Level: beginner
1540 
1541    Concepts: options database^has option name
1542 
1543    Notes: Name cannot be simply -h
1544 
1545           In many cases you probably want to use PetscOptionsGetBool() instead of calling this, to allowing toggling values.
1546 
1547 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1548            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1549           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1550           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1551           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1552           PetscOptionsFList(), PetscOptionsEList()
1553 @*/
1554 PetscErrorCode  PetscOptionsHasName(PetscOptions options,const char pre[],const char name[],PetscBool  *set)
1555 {
1556   char           *value;
1557   PetscErrorCode ierr;
1558   PetscBool      flag;
1559 
1560   PetscFunctionBegin;
1561   options = options ? options : defaultoptions;
1562   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1563   if (set) *set = flag;
1564   PetscFunctionReturn(0);
1565 }
1566 
1567 #undef __FUNCT__
1568 #define __FUNCT__ "PetscOptionsGetInt"
1569 /*@C
1570    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
1571 
1572    Not Collective
1573 
1574    Input Parameters:
1575 +  options - options database use NULL for default global database
1576 .  pre - the string to prepend to the name or NULL
1577 -  name - the option one is seeking
1578 
1579    Output Parameter:
1580 +  ivalue - the integer value to return
1581 -  set - PETSC_TRUE if found, else PETSC_FALSE
1582 
1583    Level: beginner
1584 
1585    Concepts: options database^has int
1586 
1587 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1588           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1589           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1590           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1591           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1592           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1593           PetscOptionsFList(), PetscOptionsEList()
1594 @*/
1595 PetscErrorCode  PetscOptionsGetInt(PetscOptions options,const char pre[],const char name[],PetscInt *ivalue,PetscBool  *set)
1596 {
1597   char           *value;
1598   PetscErrorCode ierr;
1599   PetscBool      flag;
1600 
1601   PetscFunctionBegin;
1602   PetscValidCharPointer(name,2);
1603   PetscValidIntPointer(ivalue,3);
1604   options = options ? options : defaultoptions;
1605   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1606   if (flag) {
1607     if (!value) {
1608       if (set) *set = PETSC_FALSE;
1609     } else {
1610       if (set) *set = PETSC_TRUE;
1611       ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr);
1612     }
1613   } else {
1614     if (set) *set = PETSC_FALSE;
1615   }
1616   PetscFunctionReturn(0);
1617 }
1618 
1619 #undef __FUNCT__
1620 #define __FUNCT__ "PetscOptionsGetEList"
1621 /*@C
1622      PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
1623 
1624    Not Collective
1625 
1626    Input Parameters:
1627 +  options - options database use NULL for default global database
1628 .  pre - the string to prepend to the name or NULL
1629 .  opt - option name
1630 .  list - the possible choices (one of these must be selected, anything else is invalid)
1631 .  ntext - number of choices
1632 
1633    Output Parameter:
1634 +  value - the index of the value to return (defaults to zero if the option name is given but choice is listed)
1635 -  set - PETSC_TRUE if found, else PETSC_FALSE
1636 
1637    Level: intermediate
1638 
1639    See PetscOptionsFList() for when the choices are given in a PetscFunctionList()
1640 
1641    Concepts: options database^list
1642 
1643 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1644            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1645           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1646           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1647           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1648           PetscOptionsFList(), PetscOptionsEList()
1649 @*/
1650 PetscErrorCode  PetscOptionsGetEList(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscInt ntext,PetscInt *value,PetscBool  *set)
1651 {
1652   PetscErrorCode ierr;
1653   size_t         alen,len = 0;
1654   char           *svalue;
1655   PetscBool      aset,flg = PETSC_FALSE;
1656   PetscInt       i;
1657 
1658   PetscFunctionBegin;
1659   options = options ? options : defaultoptions;
1660   for (i=0; i<ntext; i++) {
1661     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
1662     if (alen > len) len = alen;
1663   }
1664   len += 5; /* a little extra space for user mistypes */
1665   ierr = PetscMalloc1(len,&svalue);CHKERRQ(ierr);
1666   ierr = PetscOptionsGetString(options,pre,opt,svalue,len,&aset);CHKERRQ(ierr);
1667   if (aset) {
1668     ierr = PetscEListFind(ntext,list,svalue,value,&flg);CHKERRQ(ierr);
1669     if (!flg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre ? pre : "",opt+1);
1670     if (set) *set = PETSC_TRUE;
1671   } else if (set) *set = PETSC_FALSE;
1672   ierr = PetscFree(svalue);CHKERRQ(ierr);
1673   PetscFunctionReturn(0);
1674 }
1675 
1676 #undef __FUNCT__
1677 #define __FUNCT__ "PetscOptionsGetEnum"
1678 /*@C
1679    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
1680 
1681    Not Collective
1682 
1683    Input Parameters:
1684 +  options - options database use NULL for default global database
1685 .  pre - option prefix or NULL
1686 .  opt - option name
1687 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
1688 -  defaultv - the default (current) value
1689 
1690    Output Parameter:
1691 +  value - the  value to return
1692 -  set - PETSC_TRUE if found, else PETSC_FALSE
1693 
1694    Level: beginner
1695 
1696    Concepts: options database
1697 
1698    Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd()
1699 
1700           list is usually something like PCASMTypes or some other predefined list of enum names
1701 
1702 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
1703           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1704           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1705           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1706           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1707           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1708           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
1709 @*/
1710 PetscErrorCode  PetscOptionsGetEnum(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscEnum *value,PetscBool  *set)
1711 {
1712   PetscErrorCode ierr;
1713   PetscInt       ntext = 0,tval;
1714   PetscBool      fset;
1715 
1716   PetscFunctionBegin;
1717   options = options ? options : defaultoptions;
1718   while (list[ntext++]) {
1719     if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
1720   }
1721   if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
1722   ntext -= 3;
1723   ierr = PetscOptionsGetEList(options,pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr);
1724   /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */
1725   if (fset) *value = (PetscEnum)tval;
1726   if (set) *set = fset;
1727   PetscFunctionReturn(0);
1728 }
1729 
1730 #undef __FUNCT__
1731 #define __FUNCT__ "PetscOptionsGetBool"
1732 /*@C
1733    PetscOptionsGetBool - Gets the Logical (true or false) value for a particular
1734             option in the database.
1735 
1736    Not Collective
1737 
1738    Input Parameters:
1739 +  options - options database use NULL for default global database
1740 .  pre - the string to prepend to the name or NULL
1741 -  name - the option one is seeking
1742 
1743    Output Parameter:
1744 +  ivalue - the logical value to return
1745 -  set - PETSC_TRUE  if found, else PETSC_FALSE
1746 
1747    Level: beginner
1748 
1749    Notes:
1750        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1751        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1752 
1753        If the user does not supply the option (as either true or false) ivalue is NOT changed. Thus
1754      you NEED TO ALWAYS initialize the ivalue.
1755 
1756    Concepts: options database^has logical
1757 
1758 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1759           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsBool(),
1760           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1761           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1762           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1763           PetscOptionsFList(), PetscOptionsEList()
1764 @*/
1765 PetscErrorCode  PetscOptionsGetBool(PetscOptions options,const char pre[],const char name[],PetscBool  *ivalue,PetscBool  *set)
1766 {
1767   char           *value;
1768   PetscBool      flag;
1769   PetscErrorCode ierr;
1770 
1771   PetscFunctionBegin;
1772   PetscValidCharPointer(name,2);
1773   if (ivalue) PetscValidIntPointer(ivalue,3);
1774   options = options ? options : defaultoptions;
1775   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1776   if (flag) {
1777     if (set) *set = PETSC_TRUE;
1778     if (!value) {
1779       if (ivalue) *ivalue = PETSC_TRUE;
1780     } else {
1781       ierr = PetscOptionsStringToBool(value, ivalue);CHKERRQ(ierr);
1782     }
1783   } else {
1784     if (set) *set = PETSC_FALSE;
1785   }
1786   PetscFunctionReturn(0);
1787 }
1788 
1789 #undef __FUNCT__
1790 #define __FUNCT__ "PetscOptionsGetBoolArray"
1791 /*@C
1792    PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular
1793    option in the database.  The values must be separated with commas with
1794    no intervening spaces.
1795 
1796    Not Collective
1797 
1798    Input Parameters:
1799 +  options - options database use NULL for default global database
1800 .  pre - string to prepend to each name or NULL
1801 .  name - the option one is seeking
1802 -  nmax - maximum number of values to retrieve
1803 
1804    Output Parameter:
1805 +  dvalue - the integer values to return
1806 .  nmax - actual number of values retreived
1807 -  set - PETSC_TRUE if found, else PETSC_FALSE
1808 
1809    Level: beginner
1810 
1811    Concepts: options database^array of ints
1812 
1813    Notes:
1814        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1815        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1816 
1817 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1818            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1819           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1820           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1821           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1822           PetscOptionsFList(), PetscOptionsEList()
1823 @*/
1824 PetscErrorCode  PetscOptionsGetBoolArray(PetscOptions options,const char pre[],const char name[],PetscBool dvalue[],PetscInt *nmax,PetscBool  *set)
1825 {
1826   char           *value;
1827   PetscErrorCode ierr;
1828   PetscInt       n = 0;
1829   PetscBool      flag;
1830   PetscToken     token;
1831 
1832   PetscFunctionBegin;
1833   PetscValidCharPointer(name,2);
1834   PetscValidIntPointer(dvalue,3);
1835   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1836   if (!flag)  {if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1837   if (!value) {if (set) *set = PETSC_TRUE;  *nmax = 0; PetscFunctionReturn(0);}
1838 
1839   if (set) *set = PETSC_TRUE;
1840 
1841   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1842   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1843   while (n < *nmax) {
1844     if (!value) break;
1845     ierr = PetscOptionsStringToBool(value,dvalue);CHKERRQ(ierr);
1846     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1847     dvalue++;
1848     n++;
1849   }
1850   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
1851   *nmax = n;
1852   PetscFunctionReturn(0);
1853 }
1854 
1855 #undef __FUNCT__
1856 #define __FUNCT__ "PetscOptionsGetReal"
1857 /*@C
1858    PetscOptionsGetReal - Gets the double precision value for a particular
1859    option in the database.
1860 
1861    Not Collective
1862 
1863    Input Parameters:
1864 +  options - options database use NULL for default global database
1865 .  pre - string to prepend to each name or NULL
1866 -  name - the option one is seeking
1867 
1868    Output Parameter:
1869 +  dvalue - the double value to return
1870 -  set - PETSC_TRUE if found, PETSC_FALSE if not found
1871 
1872    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1873 
1874    Level: beginner
1875 
1876    Concepts: options database^has double
1877 
1878 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1879            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1880           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1881           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1882           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1883           PetscOptionsFList(), PetscOptionsEList()
1884 @*/
1885 PetscErrorCode  PetscOptionsGetReal(PetscOptions options,const char pre[],const char name[],PetscReal *dvalue,PetscBool  *set)
1886 {
1887   char           *value;
1888   PetscErrorCode ierr;
1889   PetscBool      flag;
1890 
1891   PetscFunctionBegin;
1892   PetscValidCharPointer(name,2);
1893   PetscValidRealPointer(dvalue,3);
1894   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1895   if (flag) {
1896     if (!value) {
1897       if (set) *set = PETSC_FALSE;
1898     } else {
1899       if (set) *set = PETSC_TRUE;
1900       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
1901     }
1902   } else {
1903     if (set) *set = PETSC_FALSE;
1904   }
1905   PetscFunctionReturn(0);
1906 }
1907 
1908 #undef __FUNCT__
1909 #define __FUNCT__ "PetscOptionsGetScalar"
1910 /*@C
1911    PetscOptionsGetScalar - Gets the scalar value for a particular
1912    option in the database.
1913 
1914    Not Collective
1915 
1916    Input Parameters:
1917 +  options - options database use NULL for default global database
1918 .  pre - string to prepend to each name or NULL
1919 -  name - the option one is seeking
1920 
1921    Output Parameter:
1922 +  dvalue - the double value to return
1923 -  set - PETSC_TRUE if found, else PETSC_FALSE
1924 
1925    Level: beginner
1926 
1927    Usage:
1928    A complex number 2+3i must be specified with NO spaces
1929 
1930    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1931 
1932    Concepts: options database^has scalar
1933 
1934 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1935            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1936           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1937           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1938           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1939           PetscOptionsFList(), PetscOptionsEList()
1940 @*/
1941 PetscErrorCode  PetscOptionsGetScalar(PetscOptions options,const char pre[],const char name[],PetscScalar *dvalue,PetscBool  *set)
1942 {
1943   char           *value;
1944   PetscBool      flag;
1945   PetscErrorCode ierr;
1946 
1947   PetscFunctionBegin;
1948   PetscValidCharPointer(name,2);
1949   PetscValidScalarPointer(dvalue,3);
1950   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
1951   if (flag) {
1952     if (!value) {
1953       if (set) *set = PETSC_FALSE;
1954     } else {
1955 #if !defined(PETSC_USE_COMPLEX)
1956       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
1957 #else
1958       ierr = PetscOptionsStringToScalar(value,dvalue);CHKERRQ(ierr);
1959 #endif
1960       if (set) *set = PETSC_TRUE;
1961     }
1962   } else { /* flag */
1963     if (set) *set = PETSC_FALSE;
1964   }
1965   PetscFunctionReturn(0);
1966 }
1967 
1968 #undef __FUNCT__
1969 #define __FUNCT__ "PetscOptionsGetRealArray"
1970 /*@C
1971    PetscOptionsGetRealArray - Gets an array of double precision values for a
1972    particular option in the database.  The values must be separated with
1973    commas with no intervening spaces.
1974 
1975    Not Collective
1976 
1977    Input Parameters:
1978 +  options - options database use NULL for default global database
1979 .  pre - string to prepend to each name or NULL
1980 .  name - the option one is seeking
1981 -  nmax - maximum number of values to retrieve
1982 
1983    Output Parameters:
1984 +  dvalue - the double values to return
1985 .  nmax - actual number of values retreived
1986 -  set - PETSC_TRUE if found, else PETSC_FALSE
1987 
1988    Level: beginner
1989 
1990    Concepts: options database^array of doubles
1991 
1992 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1993            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
1994           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1995           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1996           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1997           PetscOptionsFList(), PetscOptionsEList()
1998 @*/
1999 PetscErrorCode  PetscOptionsGetRealArray(PetscOptions options,const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscBool  *set)
2000 {
2001   char           *value;
2002   PetscErrorCode ierr;
2003   PetscInt       n = 0;
2004   PetscBool      flag;
2005   PetscToken     token;
2006 
2007   PetscFunctionBegin;
2008   PetscValidCharPointer(name,2);
2009   PetscValidRealPointer(dvalue,3);
2010   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
2011   if (!flag) {
2012     if (set) *set = PETSC_FALSE;
2013     *nmax = 0;
2014     PetscFunctionReturn(0);
2015   }
2016   if (!value) {
2017     if (set) *set = PETSC_TRUE;
2018     *nmax = 0;
2019     PetscFunctionReturn(0);
2020   }
2021 
2022   if (set) *set = PETSC_TRUE;
2023 
2024   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
2025   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2026   while (n < *nmax) {
2027     if (!value) break;
2028     ierr = PetscOptionsStringToReal(value,dvalue++);CHKERRQ(ierr);
2029     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2030     n++;
2031   }
2032   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2033   *nmax = n;
2034   PetscFunctionReturn(0);
2035 }
2036 
2037 #undef __FUNCT__
2038 #define __FUNCT__ "PetscOptionsGetScalarArray"
2039 /*@C
2040    PetscOptionsGetScalarArray - Gets an array of scalars for a
2041    particular option in the database.  The values must be separated with
2042    commas with no intervening spaces.
2043 
2044    Not Collective
2045 
2046    Input Parameters:
2047 +  options - options database use NULL for default global database
2048 .  pre - string to prepend to each name or NULL
2049 .  name - the option one is seeking
2050 -  nmax - maximum number of values to retrieve
2051 
2052    Output Parameters:
2053 +  dvalue - the scalar values to return
2054 .  nmax - actual number of values retreived
2055 -  set - PETSC_TRUE if found, else PETSC_FALSE
2056 
2057    Level: beginner
2058 
2059    Concepts: options database^array of doubles
2060 
2061 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2062            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
2063           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2064           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2065           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2066           PetscOptionsFList(), PetscOptionsEList()
2067 @*/
2068 PetscErrorCode  PetscOptionsGetScalarArray(PetscOptions options,const char pre[],const char name[],PetscScalar dvalue[],PetscInt *nmax,PetscBool  *set)
2069 {
2070   char           *value;
2071   PetscErrorCode ierr;
2072   PetscInt       n = 0;
2073   PetscBool      flag;
2074   PetscToken     token;
2075 
2076   PetscFunctionBegin;
2077   PetscValidCharPointer(name,2);
2078   PetscValidRealPointer(dvalue,3);
2079   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
2080   if (!flag) {
2081     if (set) *set = PETSC_FALSE;
2082     *nmax = 0;
2083     PetscFunctionReturn(0);
2084   }
2085   if (!value) {
2086     if (set) *set = PETSC_TRUE;
2087     *nmax = 0;
2088     PetscFunctionReturn(0);
2089   }
2090 
2091   if (set) *set = PETSC_TRUE;
2092 
2093   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
2094   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2095   while (n < *nmax) {
2096     if (!value) break;
2097     ierr = PetscOptionsStringToScalar(value,dvalue++);CHKERRQ(ierr);
2098     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2099     n++;
2100   }
2101   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2102   *nmax = n;
2103   PetscFunctionReturn(0);
2104 }
2105 
2106 #undef __FUNCT__
2107 #define __FUNCT__ "PetscOptionsGetIntArray"
2108 /*@C
2109    PetscOptionsGetIntArray - Gets an array of integer values for a particular
2110    option in the database.
2111 
2112    Not Collective
2113 
2114    Input Parameters:
2115 +  options - options database use NULL for default global database
2116 .  pre - string to prepend to each name or NULL
2117 .  name - the option one is seeking
2118 -  nmax - maximum number of values to retrieve
2119 
2120    Output Parameter:
2121 +  dvalue - the integer values to return
2122 .  nmax - actual number of values retreived
2123 -  set - PETSC_TRUE if found, else PETSC_FALSE
2124 
2125    Level: beginner
2126 
2127    Notes:
2128    The array can be passed as
2129    a comma separated list:                                 0,1,2,3,4,5,6,7
2130    a range (start-end+1):                                  0-8
2131    a range with given increment (start-end+1:inc):         0-7:2
2132    a combination of values and ranges separated by commas: 0,1-8,8-15:2
2133 
2134    There must be no intervening spaces between the values.
2135 
2136    Concepts: options database^array of ints
2137 
2138 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2139            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2140           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2141           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2142           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2143           PetscOptionsFList(), PetscOptionsEList()
2144 @*/
2145 PetscErrorCode  PetscOptionsGetIntArray(PetscOptions options,const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscBool  *set)
2146 {
2147   char           *value;
2148   PetscErrorCode ierr;
2149   PetscInt       n = 0,i,j,start,end,inc,nvalues;
2150   size_t         len;
2151   PetscBool      flag,foundrange;
2152   PetscToken     token;
2153 
2154   PetscFunctionBegin;
2155   PetscValidCharPointer(name,2);
2156   PetscValidIntPointer(dvalue,3);
2157   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
2158   if (!flag) {
2159     if (set) *set = PETSC_FALSE;
2160     *nmax = 0;
2161     PetscFunctionReturn(0);
2162   }
2163   if (!value) {
2164     if (set) *set = PETSC_TRUE;
2165     *nmax = 0;
2166     PetscFunctionReturn(0);
2167   }
2168 
2169   if (set) *set = PETSC_TRUE;
2170 
2171   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
2172   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2173   while (n < *nmax) {
2174     if (!value) break;
2175 
2176     /* look for form  d-D where d and D are integers */
2177     foundrange = PETSC_FALSE;
2178     ierr       = PetscStrlen(value,&len);CHKERRQ(ierr);
2179     if (value[0] == '-') i=2;
2180     else i=1;
2181     for (;i<(int)len; i++) {
2182       if (value[i] == '-') {
2183         if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
2184         value[i] = 0;
2185 
2186         ierr = PetscOptionsStringToInt(value,&start);CHKERRQ(ierr);
2187         inc  = 1;
2188         j    = i+1;
2189         for (;j<(int)len; j++) {
2190           if (value[j] == ':') {
2191             value[j] = 0;
2192 
2193             ierr = PetscOptionsStringToInt(value+j+1,&inc);CHKERRQ(ierr);
2194             if (inc <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry,%s cannot have negative increment",n,value+j+1);CHKERRQ(ierr);
2195             break;
2196           }
2197         }
2198         ierr = PetscOptionsStringToInt(value+i+1,&end);CHKERRQ(ierr);
2199         if (end <= start) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1);
2200         nvalues = (end-start)/inc + (end-start)%inc;
2201         if (n + nvalues  > *nmax) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, not enough space left in array (%D) to contain entire range from %D to %D",n,*nmax-n,start,end);
2202         for (;start<end; start+=inc) {
2203           *dvalue = start; dvalue++;n++;
2204         }
2205         foundrange = PETSC_TRUE;
2206         break;
2207       }
2208     }
2209     if (!foundrange) {
2210       ierr = PetscOptionsStringToInt(value,dvalue);CHKERRQ(ierr);
2211       dvalue++;
2212       n++;
2213     }
2214     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2215   }
2216   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2217   *nmax = n;
2218   PetscFunctionReturn(0);
2219 }
2220 
2221 #undef __FUNCT__
2222 #define __FUNCT__ "PetscOptionsGetEnumArray"
2223 /*@C
2224    PetscOptionsGetEnumArray - Gets an array of enum values for a particular option in the database.
2225 
2226    Not Collective
2227 
2228    Input Parameters:
2229 +  options - options database use NULL for default global database
2230 .  pre - option prefix or NULL
2231 .  name - option name
2232 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
2233 -  nmax - maximum number of values to retrieve
2234 
2235    Output Parameters:
2236 +  dvalue - the  enum values to return
2237 .  nmax - actual number of values retreived
2238 -  set - PETSC_TRUE if found, else PETSC_FALSE
2239 
2240    Level: beginner
2241 
2242    Concepts: options database
2243 
2244    Notes:
2245    The array must be passed as a comma separated list.
2246 
2247    There must be no intervening spaces between the values.
2248 
2249    list is usually something like PCASMTypes or some other predefined list of enum names.
2250 
2251 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
2252           PetscOptionsGetEnum(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
2253           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), PetscOptionsName(),
2254           PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), PetscOptionsStringArray(),PetscOptionsRealArray(),
2255           PetscOptionsScalar(), PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2256           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
2257 @*/
2258 PetscErrorCode PetscOptionsGetEnumArray(PetscOptions options,const char pre[],const char name[],const char *const *list,PetscEnum dvalue[],PetscInt *nmax,PetscBool *set)
2259 {
2260   char           *svalue;
2261   PetscInt       n = 0;
2262   PetscEnum      evalue;
2263   PetscBool      flag;
2264   PetscToken     token;
2265   PetscErrorCode ierr;
2266 
2267   PetscFunctionBegin;
2268   PetscValidCharPointer(name,2);
2269   PetscValidPointer(list,3);
2270   PetscValidPointer(dvalue,4);
2271   PetscValidIntPointer(nmax,5);
2272 
2273   ierr = PetscOptionsFindPair_Private(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2274   if (!flag) {
2275     if (set) *set = PETSC_FALSE;
2276     *nmax = 0;
2277     PetscFunctionReturn(0);
2278   }
2279   if (!svalue) {
2280     if (set) *set = PETSC_TRUE;
2281     *nmax = 0;
2282     PetscFunctionReturn(0);
2283   }
2284   if (set) *set = PETSC_TRUE;
2285 
2286   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2287   ierr = PetscTokenFind(token,&svalue);CHKERRQ(ierr);
2288   while (svalue && n < *nmax) {
2289     ierr = PetscEnumFind(list,svalue,&evalue,&flag);CHKERRQ(ierr);
2290     if (!flag) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown enum value '%s' for -%s%s",svalue,pre ? pre : "",name+1);
2291     dvalue[n++] = evalue;
2292     ierr = PetscTokenFind(token,&svalue);CHKERRQ(ierr);
2293   }
2294   *nmax = n;
2295   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
2296   PetscFunctionReturn(0);
2297 }
2298 
2299 #undef __FUNCT__
2300 #define __FUNCT__ "PetscOptionsGetString"
2301 /*@C
2302    PetscOptionsGetString - Gets the string value for a particular option in
2303    the database.
2304 
2305    Not Collective
2306 
2307    Input Parameters:
2308 +  options - options database use NULL for default global database
2309 .  pre - string to prepend to name or NULL
2310 .  name - the option one is seeking
2311 -  len - maximum length of the string including null termination
2312 
2313    Output Parameters:
2314 +  string - location to copy string
2315 -  set - PETSC_TRUE if found, else PETSC_FALSE
2316 
2317    Level: beginner
2318 
2319    Fortran Note:
2320    The Fortran interface is slightly different from the C/C++
2321    interface (len is not used).  Sample usage in Fortran follows
2322 .vb
2323       character *20 string
2324       integer   flg, ierr
2325       call PetscOptionsGetString(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,'-s',string,flg,ierr)
2326 .ve
2327 
2328    Notes: 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
2329 
2330    Concepts: options database^string
2331 
2332     Note:
2333       Even if the user provided no string (for example -optionname -someotheroption) the flag is set to PETSC_TRUE (and the string is fulled with nulls).
2334 
2335 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2336            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2337           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2338           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2339           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2340           PetscOptionsFList(), PetscOptionsEList()
2341 @*/
2342 PetscErrorCode  PetscOptionsGetString(PetscOptions options,const char pre[],const char name[],char string[],size_t len,PetscBool  *set)
2343 {
2344   char           *value;
2345   PetscErrorCode ierr;
2346   PetscBool      flag;
2347 
2348   PetscFunctionBegin;
2349   PetscValidCharPointer(name,2);
2350   PetscValidCharPointer(string,3);
2351   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
2352   if (!flag) {
2353     if (set) *set = PETSC_FALSE;
2354   } else {
2355     if (set) *set = PETSC_TRUE;
2356     if (value) {
2357       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
2358       string[len-1] = 0;        /* Ensure that the string is NULL terminated */
2359     } else {
2360       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
2361     }
2362   }
2363   PetscFunctionReturn(0);
2364 }
2365 
2366 #undef __FUNCT__
2367 #define __FUNCT__ "PetscOptionsGetStringMatlab"
2368 char *PetscOptionsGetStringMatlab(PetscOptions options,const char pre[],const char name[])
2369 {
2370   char           *value;
2371   PetscErrorCode ierr;
2372   PetscBool      flag;
2373 
2374   PetscFunctionBegin;
2375   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);if (ierr) PetscFunctionReturn(0);
2376   if (flag) PetscFunctionReturn(value);
2377   else PetscFunctionReturn(0);
2378 }
2379 
2380 
2381 #undef __FUNCT__
2382 #define __FUNCT__ "PetscOptionsGetStringArray"
2383 /*@C
2384    PetscOptionsGetStringArray - Gets an array of string values for a particular
2385    option in the database. The values must be separated with commas with
2386    no intervening spaces.
2387 
2388    Not Collective
2389 
2390    Input Parameters:
2391 +  options - options database use NULL for default global database
2392 .  pre - string to prepend to name or NULL
2393 .  name - the option one is seeking
2394 -  nmax - maximum number of strings
2395 
2396    Output Parameter:
2397 +  strings - location to copy strings
2398 -  set - PETSC_TRUE if found, else PETSC_FALSE
2399 
2400    Level: beginner
2401 
2402    Notes:
2403    The user should pass in an array of pointers to char, to hold all the
2404    strings returned by this function.
2405 
2406    The user is responsible for deallocating the strings that are
2407    returned. The Fortran interface for this routine is not supported.
2408 
2409    Contributed by Matthew Knepley.
2410 
2411    Concepts: options database^array of strings
2412 
2413 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2414            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2415           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2416           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2417           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2418           PetscOptionsFList(), PetscOptionsEList()
2419 @*/
2420 PetscErrorCode  PetscOptionsGetStringArray(PetscOptions options,const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscBool  *set)
2421 {
2422   char           *value;
2423   PetscErrorCode ierr;
2424   PetscInt       n;
2425   PetscBool      flag;
2426   PetscToken     token;
2427 
2428   PetscFunctionBegin;
2429   PetscValidCharPointer(name,2);
2430   PetscValidPointer(strings,3);
2431   ierr = PetscOptionsFindPair_Private(options,pre,name,&value,&flag);CHKERRQ(ierr);
2432   if (!flag) {
2433     *nmax = 0;
2434     if (set) *set = PETSC_FALSE;
2435     PetscFunctionReturn(0);
2436   }
2437   if (!value) {
2438     *nmax = 0;
2439     if (set) *set = PETSC_FALSE;
2440     PetscFunctionReturn(0);
2441   }
2442   if (!*nmax) {
2443     if (set) *set = PETSC_FALSE;
2444     PetscFunctionReturn(0);
2445   }
2446   if (set) *set = PETSC_TRUE;
2447 
2448   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
2449   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2450   n    = 0;
2451   while (n < *nmax) {
2452     if (!value) break;
2453     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
2454     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2455     n++;
2456   }
2457   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2458   *nmax = n;
2459   PetscFunctionReturn(0);
2460 }
2461 
2462 #undef __FUNCT__
2463 #define __FUNCT__ "PetscOptionsUsed"
2464 /*@C
2465    PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database
2466 
2467    Not Collective
2468 
2469    Input Parameter:
2470 +   options - options database use NULL for default global database
2471 -   option - string name of option
2472 
2473    Output Parameter:
2474 .   used - PETSC_TRUE if the option was used, otherwise false, including if option was not found in options database
2475 
2476    Level: advanced
2477 
2478 .seealso: PetscOptionsView(), PetscOptionsLeft(), PetscOptionsAllUsed()
2479 @*/
2480 PetscErrorCode  PetscOptionsUsed(PetscOptions options,const char *option,PetscBool *used)
2481 {
2482   PetscInt       i;
2483   PetscErrorCode ierr;
2484 
2485   PetscFunctionBegin;
2486   options = options ? options : defaultoptions;
2487   *used = PETSC_FALSE;
2488   for (i=0; i<options->N; i++) {
2489     ierr = PetscStrcmp(options->names[i],option,used);CHKERRQ(ierr);
2490     if (*used) {
2491       *used = options->used[i];
2492       break;
2493     }
2494   }
2495   PetscFunctionReturn(0);
2496 }
2497 
2498 #undef __FUNCT__
2499 #define __FUNCT__ "PetscOptionsAllUsed"
2500 /*@C
2501    PetscOptionsAllUsed - Returns a count of the number of options in the
2502    database that have never been selected.
2503 
2504    Not Collective
2505 
2506    Input Parameter:
2507 .  options - options database use NULL for default global database
2508 
2509    Output Parameter:
2510 .   N - count of options not used
2511 
2512    Level: advanced
2513 
2514 .seealso: PetscOptionsView()
2515 @*/
2516 PetscErrorCode  PetscOptionsAllUsed(PetscOptions options,PetscInt *N)
2517 {
2518   PetscInt     i,n = 0;
2519 
2520   PetscFunctionBegin;
2521   options = options ? options : defaultoptions;
2522   for (i=0; i<options->N; i++) {
2523     if (!options->used[i]) n++;
2524   }
2525   *N = n;
2526   PetscFunctionReturn(0);
2527 }
2528 
2529 #undef __FUNCT__
2530 #define __FUNCT__ "PetscOptionsLeft"
2531 /*@C
2532     PetscOptionsLeft - Prints to screen any options that were set and never used.
2533 
2534   Not collective
2535 
2536    Input Parameter:
2537 .  options - options database use NULL for default global database
2538 
2539    Options Database Key:
2540 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
2541 
2542   Level: advanced
2543 
2544 .seealso: PetscOptionsAllUsed()
2545 @*/
2546 PetscErrorCode  PetscOptionsLeft(PetscOptions options)
2547 {
2548   PetscErrorCode ierr;
2549   PetscInt       i;
2550 
2551   PetscFunctionBegin;
2552   options = options ? options : defaultoptions;
2553   for (i=0; i<options->N; i++) {
2554     if (!options->used[i]) {
2555       if (options->values[i]) {
2556         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
2557       } else {
2558         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s (no value)\n",options->names[i]);CHKERRQ(ierr);
2559       }
2560     }
2561   }
2562   PetscFunctionReturn(0);
2563 }
2564 
2565 #undef __FUNCT__
2566 #define __FUNCT__ "PetscOptionsCreate"
2567 /*@
2568     PetscOptionsCreate - Creates the empty options database.
2569 
2570   Output Parameter:
2571 .   options - Options database object
2572 
2573   Level: advanced
2574 
2575 @*/
2576 PetscErrorCode  PetscOptionsCreate(PetscOptions *options)
2577 {
2578   *options = (PetscOptions)calloc(1,sizeof(struct _n_PetscOptions));
2579   if (!options) return PETSC_ERR_MEM;
2580   (*options)->namegiven      = PETSC_FALSE;
2581   (*options)->N              = 0;
2582   (*options)->Naliases       = 0;
2583   (*options)->numbermonitors = 0;
2584   return 0;
2585 }
2586 
2587 #undef __FUNCT__
2588 #define __FUNCT__ "PetscOptionsCreateDefault"
2589 /*
2590     PetscOptionsCreateDefault - Creates the default global options database
2591 
2592 */
2593 PetscErrorCode  PetscOptionsCreateDefault(void)
2594 {
2595   PetscErrorCode ierr;
2596 
2597   if (!defaultoptions) {
2598     ierr = PetscOptionsCreate(&defaultoptions);if (ierr) return ierr;
2599   }
2600   return 0;
2601 }
2602 
2603 #undef __FUNCT__
2604 #define __FUNCT__ "PetscOptionsSetFromOptions"
2605 /*@C
2606    PetscOptionsSetFromOptions - Sets options related to the handling of options in PETSc
2607 
2608    Collective on PETSC_COMM_WORLD
2609 
2610    Input Parameter:
2611 .  options - options database use NULL for default global database
2612 
2613    Options Database Keys:
2614 +  -options_monitor <optional filename> - prints the names and values of all runtime options as they are set. The monitor functionality is not
2615                 available for options set through a file, environment variable, or on
2616                 the command line. Only options set after PetscInitialize() completes will
2617                 be monitored.
2618 .  -options_monitor_cancel - cancel all options database monitors
2619 
2620    Notes:
2621    To see all options, run your program with the -help option or consult Users-Manual: sec_gettingstarted
2622 
2623    Level: intermediate
2624 
2625 .keywords: set, options, database
2626 @*/
2627 PetscErrorCode  PetscOptionsSetFromOptions(PetscOptions options)
2628 {
2629   PetscBool      flgc = PETSC_FALSE,flgm;
2630   PetscErrorCode ierr;
2631   char           monfilename[PETSC_MAX_PATH_LEN];
2632   PetscViewer    monviewer;
2633 
2634   PetscFunctionBegin;
2635   /*
2636      The options argument is currently ignored since we currently maintain only a single options database
2637 
2638      options = options ? options : defaultoptions;
2639   */
2640   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Options for handling options","PetscOptions");CHKERRQ(ierr);
2641   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flgm);CHKERRQ(ierr);
2642   ierr = PetscOptionsBool("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",flgc,&flgc,NULL);CHKERRQ(ierr);
2643   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2644   if (flgm) {
2645     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
2646     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
2647   }
2648   if (flgc) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
2649   PetscFunctionReturn(0);
2650 }
2651 
2652 
2653 #undef __FUNCT__
2654 #define __FUNCT__ "PetscOptionsMonitorDefault"
2655 /*@C
2656    PetscOptionsMonitorDefault - Print all options set value events.
2657 
2658    Logically Collective on PETSC_COMM_WORLD
2659 
2660    Input Parameters:
2661 +  name  - option name string
2662 .  value - option value string
2663 -  dummy - an ASCII viewer
2664 
2665    Level: intermediate
2666 
2667 .keywords: PetscOptions, default, monitor
2668 
2669 .seealso: PetscOptionsMonitorSet()
2670 @*/
2671 PetscErrorCode  PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
2672 {
2673   PetscErrorCode ierr;
2674   PetscViewer    viewer = (PetscViewer) dummy;
2675 
2676   PetscFunctionBegin;
2677   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
2678   PetscFunctionReturn(0);
2679 }
2680 
2681 #undef __FUNCT__
2682 #define __FUNCT__ "PetscOptionsMonitorSet"
2683 /*@C
2684    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
2685    modified the PETSc options database.
2686 
2687    Not collective
2688 
2689    Input Parameters:
2690 +  monitor - pointer to function (if this is NULL, it turns off monitoring
2691 .  mctx    - [optional] context for private data for the
2692              monitor routine (use NULL if no context is desired)
2693 -  monitordestroy - [optional] routine that frees monitor context
2694           (may be NULL)
2695 
2696    Calling Sequence of monitor:
2697 $     monitor (const char name[], const char value[], void *mctx)
2698 
2699 +  name - option name string
2700 .  value - option value string
2701 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
2702 
2703    Options Database Keys:
2704 +    -options_monitor    - sets PetscOptionsMonitorDefault()
2705 -    -options_monitor_cancel - cancels all monitors that have
2706                           been hardwired into a code by
2707                           calls to PetscOptionsMonitorSet(), but
2708                           does not cancel those set via
2709                           the options database.
2710 
2711    Notes:
2712    The default is to do nothing.  To print the name and value of options
2713    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
2714    with a null monitoring context.
2715 
2716    Several different monitoring routines may be set by calling
2717    PetscOptionsMonitorSet() multiple times; all will be called in the
2718    order in which they were set.
2719 
2720    Level: beginner
2721 
2722 .keywords: PetscOptions, set, monitor
2723 
2724 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
2725 @*/
2726 PetscErrorCode  PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
2727 {
2728   PetscOptions options = defaultoptions;
2729 
2730   PetscFunctionBegin;
2731   if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
2732   options->monitor[options->numbermonitors]          = monitor;
2733   options->monitordestroy[options->numbermonitors]   = monitordestroy;
2734   options->monitorcontext[options->numbermonitors++] = (void*)mctx;
2735   PetscFunctionReturn(0);
2736 }
2737 
2738 #undef __FUNCT__
2739 #define __FUNCT__ "PetscOptionsMonitorCancel"
2740 /*@
2741    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
2742 
2743    Not collective
2744 
2745    Options Database Key:
2746 .  -options_monitor_cancel - Cancels all monitors that have
2747     been hardwired into a code by calls to PetscOptionsMonitorSet(),
2748     but does not cancel those set via the options database.
2749 
2750    Level: intermediate
2751 
2752 .keywords: PetscOptions, set, monitor
2753 
2754 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
2755 @*/
2756 PetscErrorCode  PetscOptionsMonitorCancel(void)
2757 {
2758   PetscErrorCode ierr;
2759   PetscInt       i;
2760   PetscOptions   options = defaultoptions;
2761 
2762   PetscFunctionBegin;
2763   for (i=0; i<options->numbermonitors; i++) {
2764     if (options->monitordestroy[i]) {
2765       ierr = (*options->monitordestroy[i])(&options->monitorcontext[i]);CHKERRQ(ierr);
2766     }
2767   }
2768   options->numbermonitors = 0;
2769   PetscFunctionReturn(0);
2770 }
2771 
2772 #define CHKERRQI(incall,ierr) if (ierr) {incall = PETSC_FALSE; CHKERRQ(ierr);}
2773 
2774 #undef __FUNCT__
2775 #define __FUNCT__ "PetscObjectViewFromOptions"
2776 /*@C
2777   PetscObjectViewFromOptions - Processes command line options to determine if/how a PetscObject is to be viewed.
2778 
2779   Collective on PetscObject
2780 
2781   Input Parameters:
2782 + obj   - the object
2783 . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
2784 - optionname - option to activate viewing
2785 
2786   Level: intermediate
2787 
2788 @*/
2789 PetscErrorCode PetscObjectViewFromOptions(PetscObject obj,PetscObject bobj,const char optionname[])
2790 {
2791   PetscErrorCode    ierr;
2792   PetscViewer       viewer;
2793   PetscBool         flg;
2794   static PetscBool  incall = PETSC_FALSE;
2795   PetscViewerFormat format;
2796   char              *prefix;
2797 
2798   PetscFunctionBegin;
2799   if (incall) PetscFunctionReturn(0);
2800   incall = PETSC_TRUE;
2801   prefix = bobj ? bobj->prefix : obj->prefix;
2802   ierr   = PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),prefix,optionname,&viewer,&format,&flg);CHKERRQI(incall,ierr);
2803   if (flg) {
2804     ierr = PetscViewerPushFormat(viewer,format);CHKERRQI(incall,ierr);
2805     ierr = PetscObjectView(obj,viewer);CHKERRQI(incall,ierr);
2806     ierr = PetscViewerPopFormat(viewer);CHKERRQI(incall,ierr);
2807     ierr = PetscViewerDestroy(&viewer);CHKERRQI(incall,ierr);
2808   }
2809   incall = PETSC_FALSE;
2810   PetscFunctionReturn(0);
2811 }
2812