xref: /petsc/src/sys/objects/options.c (revision b9147fbb7375951b83d4ce6f264c883d157e3a5b)
1 #define PETSC_DLL
2 /*
3    These routines simplify the use of command line, file options, etc.,
4    and are used to manipulate the options database.
5 
6   This file uses regular malloc and free because it cannot know
7   what malloc is being used until it has already processed the input.
8 */
9 
10 #include "petsc.h"        /*I  "petsc.h"   I*/
11 #include "petscsys.h"
12 #if defined(PETSC_HAVE_STDLIB_H)
13 #include <stdlib.h>
14 #endif
15 #if defined(PETSC_HAVE_MALLOC_H)
16 #include <malloc.h>
17 #endif
18 #if defined(PETSC_HAVE_SYS_PARAM_H)
19 #include "sys/param.h"
20 #endif
21 #include "petscfix.h"
22 
23 /*
24     For simplicity, we use a static size database
25 */
26 #define MAXOPTIONS 512
27 #define MAXALIASES 25
28 #define MAXOPTIONSMONITORS 5
29 
30 typedef struct {
31   int        N,argc,Naliases;
32   char       **args,*names[MAXOPTIONS],*values[MAXOPTIONS];
33   char       *aliases1[MAXALIASES],*aliases2[MAXALIASES];
34   PetscTruth used[MAXOPTIONS];
35   PetscTruth namegiven;
36   char       programname[PETSC_MAX_PATH_LEN]; /* HP includes entire path in name */
37 
38   /* --------User (or default) routines (most return -1 on error) --------*/
39   PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], void*); /* returns control to user after */
40   PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void*);         /* */
41   void           *monitorcontext[MAXOPTIONSMONITORS];                  /* to pass arbitrary user data into monitor */
42   PetscInt       numbermonitors;                                       /* to, for instance, detect options being set */
43 
44 } PetscOptionsTable;
45 
46 
47 static PetscOptionsTable *options = 0;
48 
49 /*
50     Options events monitor
51 */
52 #define PetscOptionsMonitor(name,value)                                     \
53         { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \
54           for (_i=0; _i<_im; _i++) {\
55             _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \
56 	  } \
57 	}
58 
59 #undef __FUNCT__
60 #define __FUNCT__ "PetscOptionsAtoi"
61 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtoi(const char name[],PetscInt *a)
62 {
63   PetscErrorCode ierr;
64   size_t         i,len;
65   PetscTruth     decide,tdefault,mouse;
66 
67   PetscFunctionBegin;
68   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
69   if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
70 
71   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
72   if (!tdefault) {
73     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
74   }
75   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
76   if (!decide) {
77     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
78   }
79   ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr);
80 
81   if (tdefault) {
82     *a = PETSC_DEFAULT;
83   } else if (decide) {
84     *a = PETSC_DECIDE;
85   } else if (mouse) {
86     *a = -1;
87   } else {
88     if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') {
89       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
90     }
91     for (i=1; i<len; i++) {
92       if (name[i] < '0' || name[i] > '9') {
93         SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
94       }
95     }
96     *a  = atoi(name);
97   }
98   PetscFunctionReturn(0);
99 }
100 
101 #undef __FUNCT__
102 #define __FUNCT__ "PetscOptionsAtod"
103 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtod(const char name[],PetscReal *a)
104 {
105   PetscErrorCode ierr;
106   size_t         len;
107   PetscTruth     decide,tdefault;
108 
109   PetscFunctionBegin;
110   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
111   if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
112 
113   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
114   if (!tdefault) {
115     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
116   }
117   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
118   if (!decide) {
119     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
120   }
121 
122   if (tdefault) {
123     *a = PETSC_DEFAULT;
124   } else if (decide) {
125     *a = PETSC_DECIDE;
126   } else {
127     if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') {
128       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
129     }
130     *a  = atof(name);
131   }
132   PetscFunctionReturn(0);
133 }
134 
135 #undef __FUNCT__
136 #define __FUNCT__ "PetscGetProgramName"
137 /*@C
138     PetscGetProgramName - Gets the name of the running program.
139 
140     Not Collective
141 
142     Input Parameter:
143 .   len - length of the string name
144 
145     Output Parameter:
146 .   name - the name of the running program
147 
148    Level: advanced
149 
150     Notes:
151     The name of the program is copied into the user-provided character
152     array of length len.  On some machines the program name includes
153     its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN.
154 @*/
155 PetscErrorCode PETSC_DLLEXPORT PetscGetProgramName(char name[],size_t len)
156 {
157   PetscErrorCode ierr;
158 
159   PetscFunctionBegin;
160   if (!options) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call PetscInitialize() first");
161   if (!options->namegiven) SETERRQ(PETSC_ERR_PLIB,"Unable to determine program name");
162   ierr = PetscStrncpy(name,options->programname,len);CHKERRQ(ierr);
163   PetscFunctionReturn(0);
164 }
165 
166 #undef __FUNCT__
167 #define __FUNCT__ "PetscSetProgramName"
168 PetscErrorCode PETSC_DLLEXPORT PetscSetProgramName(const char name[])
169 {
170   PetscErrorCode ierr;
171 
172   PetscFunctionBegin;
173   options->namegiven = PETSC_TRUE;
174   ierr  = PetscStrncpy(options->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
175   PetscFunctionReturn(0);
176 }
177 
178 #undef __FUNCT__
179 #define __FUNCT__ "PetscOptionsValidKey"
180 PetscErrorCode PETSC_DLLEXPORT PetscOptionsValidKey(const char in_str[],PetscTruth *key)
181 {
182   PetscFunctionBegin;
183   *key = PETSC_FALSE;
184   if (!in_str) PetscFunctionReturn(0);
185   if (in_str[0] != '-') PetscFunctionReturn(0);
186   if ((in_str[1] < 'A') || (in_str[1] > 'z')) PetscFunctionReturn(0);
187   *key = PETSC_TRUE;
188   PetscFunctionReturn(0);
189 }
190 
191 #undef __FUNCT__
192 #define __FUNCT__ "PetscOptionsInsertString"
193 /*@C
194      PetscOptionsInsertString - Inserts options into the database from a string
195 
196      Not collective: but only processes that call this routine will set the options
197                      included in the file
198 
199   Input Parameter:
200 .   in_str - string that contains options separated by blanks
201 
202 
203   Level: intermediate
204 
205   Contributed by Boyana Norris
206 
207 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(),
208           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
209           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
210           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
211           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
212           PetscOptionsList(), PetscOptionsEList(), PetscOptionsInsertFile()
213 
214 @*/
215 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertString(const char in_str[])
216 {
217   char           *first,*second;
218   PetscErrorCode ierr;
219   PetscToken     *token;
220   PetscTruth     key;
221 
222   PetscFunctionBegin;
223   ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr);
224   ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
225   while (first) {
226     ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr);
227     if (key) {
228       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
229       ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr);
230       if (!key) {
231         ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr);
232         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
233       } else {
234         ierr  = PetscOptionsSetValue(first,PETSC_NULL);CHKERRQ(ierr);
235         first = second;
236       }
237     } else {
238       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
239     }
240   }
241   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
242   PetscFunctionReturn(0);
243 }
244 
245 #undef __FUNCT__
246 #define __FUNCT__ "PetscOptionsInsertFile"
247 /*@C
248      PetscOptionsInsertFile - Inserts options into the database from a file.
249 
250      Not collective: but only processes that call this routine will set the options
251                      included in the file
252 
253   Input Parameter:
254 .   file - name of file
255 
256 
257   Level: intermediate
258 
259 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(),
260           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
261           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
262           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
263           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
264           PetscOptionsList(), PetscOptionsEList()
265 
266 @*/
267 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertFile(const char file[])
268 {
269   char           string[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*final;
270   PetscErrorCode ierr;
271   size_t         i,len,startIndex;
272   FILE           *fd;
273   PetscToken     *token;
274 
275   PetscFunctionBegin;
276   ierr = PetscFixFilename(file,fname);CHKERRQ(ierr);
277   fd   = fopen(fname,"r");
278   if (fd) {
279     while (fgets(string,128,fd)) {
280       /* Comments are indicated by #, ! or % in the first column */
281       if (string[0] == '#') continue;
282       if (string[0] == '!') continue;
283       if (string[0] == '%') continue;
284 
285       ierr = PetscStrlen(string,&len);CHKERRQ(ierr);
286 
287       /* replace tabs, ^M with " " */
288       for (i=0; i<len; i++) {
289         if (string[i] == '\t' || string[i] == '\r') {
290           string[i] = ' ';
291         }
292       }
293       for(startIndex = 0; startIndex < len-1; startIndex++) {
294         if (string[startIndex] != ' ') break;
295       }
296       ierr = PetscTokenCreate(&string[startIndex],' ',&token);CHKERRQ(ierr);
297       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
298       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
299       if (first && first[0] == '-') {
300         if (second) {final = second;} else {final = first;}
301         ierr = PetscStrlen(final,&len);CHKERRQ(ierr);
302         while (len > 0 && (final[len-1] == ' ' || final[len-1] == '\n')) {
303           len--; final[len] = 0;
304         }
305         ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr);
306       } else if (first) {
307         PetscTruth match;
308 
309         ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr);
310         if (match) {
311           ierr = PetscTokenFind(token,&third);CHKERRQ(ierr);
312           if (!third) SETERRQ1(PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second);
313           ierr = PetscStrlen(third,&len);CHKERRQ(ierr);
314           if (third[len-1] == '\n') third[len-1] = 0;
315           ierr = PetscOptionsSetAlias(second,third);CHKERRQ(ierr);
316         }
317       }
318       ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
319     }
320     fclose(fd);
321   } else {
322     SETERRQ1(PETSC_ERR_USER,"Unable to open Options File %s",fname);
323   }
324   PetscFunctionReturn(0);
325 }
326 
327 #undef __FUNCT__
328 #define __FUNCT__ "PetscOptionsInsert"
329 /*@C
330    PetscOptionsInsert - Inserts into the options database from the command line,
331                    the environmental variable and a file.
332 
333    Input Parameters:
334 +  argc - count of number of command line arguments
335 .  args - the command line arguments
336 -  file - optional filename, defaults to ~username/.petscrc
337 
338    Note:
339    Since PetscOptionsInsert() is automatically called by PetscInitialize(),
340    the user does not typically need to call this routine. PetscOptionsInsert()
341    can be called several times, adding additional entries into the database.
342 
343    Options Database Keys:
344 +   -options_monitor <optional filename> - print options names and values as they are set
345 
346    Level: advanced
347 
348    Concepts: options database^adding
349 
350 .seealso: PetscOptionsDestroy_Private(), PetscOptionsPrint(), PetscOptionsInsertString(), PetscOptionsInsertFile(),
351           PetscInitialize()
352 @*/
353 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsert(int *argc,char ***args,const char file[])
354 {
355   PetscErrorCode ierr;
356   PetscMPIInt    rank;
357   char           pfile[PETSC_MAX_PATH_LEN];
358 
359   PetscFunctionBegin;
360   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
361 
362   options->argc     = (argc) ? *argc : 0;
363   options->args     = (args) ? *args : 0;
364 
365   if (file) {
366     ierr = PetscOptionsInsertFile(file);CHKERRQ(ierr);
367   } else {
368     ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
369     if (pfile[0]) {
370       PetscTruth flag;
371       ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr);
372       ierr = PetscTestFile(pfile,'r',&flag);CHKERRQ(ierr);
373       if (flag) {
374 	ierr = PetscOptionsInsertFile(pfile);CHKERRQ(ierr);
375       }
376     } else {
377       ierr = PetscInfo(0,"Unable to determine home directory; skipping loading ~/.petscrc\n");CHKERRQ(ierr);
378     }
379 
380   }
381 
382   /* insert environmental options */
383   {
384     char   *eoptions = 0;
385     size_t len = 0;
386     if (!rank) {
387       eoptions = (char*)getenv("PETSC_OPTIONS");
388       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
389       ierr     = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
390     } else {
391       ierr = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
392       if (len) {
393         ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr);
394       }
395     }
396     if (len) {
397       ierr          = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
398       if (rank) eoptions[len] = 0;
399       ierr = PetscOptionsInsertString(eoptions);CHKERRQ(ierr);
400       if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);}
401     }
402   }
403 
404   /* insert command line options */
405   if (argc && args && *argc) {
406     int        left    = *argc - 1;
407     char       **eargs = *args + 1;
408     PetscTruth isoptions_file,isp4,tisp4,isp4yourname,isp4rmrank;
409 
410     while (left) {
411       ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr);
412       ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr);
413       ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr);
414       ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr);
415       ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr);
416       isp4 = (PetscTruth) (isp4 || tisp4);
417       ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr);
418       isp4 = (PetscTruth) (isp4 || tisp4);
419       ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr);
420 
421       if (eargs[0][0] != '-') {
422         eargs++; left--;
423       } else if (isoptions_file) {
424         if (left <= 1) SETERRQ(PETSC_ERR_USER,"Missing filename for -options_file filename option");
425         if (eargs[1][0] == '-') SETERRQ(PETSC_ERR_USER,"Missing filename for -options_file filename option");
426         ierr = PetscOptionsInsertFile(eargs[1]);CHKERRQ(ierr);
427         eargs += 2; left -= 2;
428 
429       /*
430          These are "bad" options that MPICH, etc put on the command line
431          we strip them out here.
432       */
433       } else if (tisp4 || isp4rmrank) {
434         eargs += 1; left -= 1;
435       } else if (isp4 || isp4yourname) {
436         eargs += 2; left -= 2;
437       } else if ((left < 2) || ((eargs[1][0] == '-') &&
438                ((eargs[1][1] > '9') || (eargs[1][1] < '0')))) {
439         ierr = PetscOptionsSetValue(eargs[0],PETSC_NULL);CHKERRQ(ierr);
440         eargs++; left--;
441       } else {
442         ierr = PetscOptionsSetValue(eargs[0],eargs[1]);CHKERRQ(ierr);
443         eargs += 2; left -= 2;
444       }
445     }
446   }
447   PetscFunctionReturn(0);
448 }
449 
450 #undef __FUNCT__
451 #define __FUNCT__ "PetscOptionsPrint"
452 /*@C
453    PetscOptionsPrint - Prints the options that have been loaded. This is
454    useful for debugging purposes.
455 
456    Collective on PETSC_COMM_WORLD
457 
458    Input Parameter:
459 .  FILE fd - location to print options (usually stdout or stderr)
460 
461    Options Database Key:
462 .  -optionstable - Activates PetscOptionsPrint() within PetscFinalize()
463 
464    Level: advanced
465 
466    Concepts: options database^printing
467 
468 .seealso: PetscOptionsAllUsed()
469 @*/
470 PetscErrorCode PETSC_DLLEXPORT PetscOptionsPrint(FILE *fd)
471 {
472   PetscErrorCode ierr;
473   PetscInt       i;
474 
475   PetscFunctionBegin;
476   if (!fd) fd = stdout;
477   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
478   for (i=0; i<options->N; i++) {
479     if (options->values[i]) {
480       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
481     } else {
482       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s\n",options->names[i]);CHKERRQ(ierr);
483     }
484   }
485   PetscFunctionReturn(0);
486 }
487 
488 #undef __FUNCT__
489 #define __FUNCT__ "PetscOptionsGetAll"
490 /*@C
491    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
492 
493    Not Collective
494 
495    Output Parameter:
496 .  copts - pointer where string pointer is stored
497 
498    Level: advanced
499 
500    Concepts: options database^listing
501 
502 .seealso: PetscOptionsAllUsed(), PetscOptionsPrint()
503 @*/
504 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetAll(char *copts[])
505 {
506   PetscErrorCode ierr;
507   PetscInt       i;
508   size_t         len = 1,lent;
509   char           *coptions;
510 
511   PetscFunctionBegin;
512   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
513 
514   /* count the length of the required string */
515   for (i=0; i<options->N; i++) {
516     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
517     len += 2 + lent;
518     if (options->values[i]) {
519       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
520       len += 1 + lent;
521     }
522   }
523   ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr);
524   coptions[0] = 0;
525   for (i=0; i<options->N; i++) {
526     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
527     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
528     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
529     if (options->values[i]) {
530       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
531       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
532     }
533   }
534   *copts = coptions;
535   PetscFunctionReturn(0);
536 }
537 
538 #undef __FUNCT__
539 #define __FUNCT__ "PetscOptionsDestroy"
540 /*@C
541     PetscOptionsDestroy - Destroys the option database.
542 
543     Note:
544     Since PetscOptionsDestroy() is called by PetscFinalize(), the user
545     typically does not need to call this routine.
546 
547    Level: developer
548 
549 .seealso: PetscOptionsInsert()
550 @*/
551 PetscErrorCode PETSC_DLLEXPORT PetscOptionsDestroy(void)
552 {
553   PetscInt i;
554 
555   PetscFunctionBegin;
556   if (!options) PetscFunctionReturn(0);
557   for (i=0; i<options->N; i++) {
558     if (options->names[i]) free(options->names[i]);
559     if (options->values[i]) free(options->values[i]);
560   }
561   for (i=0; i<options->Naliases; i++) {
562     free(options->aliases1[i]);
563     free(options->aliases2[i]);
564   }
565   free(options);
566   options = 0;
567   PetscFunctionReturn(0);
568 }
569 
570 #undef __FUNCT__
571 #define __FUNCT__ "PetscOptionsSetValue"
572 /*@C
573    PetscOptionsSetValue - Sets an option name-value pair in the options
574    database, overriding whatever is already present.
575 
576    Not collective, but setting values on certain processors could cause problems
577    for parallel objects looking for options.
578 
579    Input Parameters:
580 +  name - name of option, this SHOULD have the - prepended
581 -  value - the option value (not used for all options)
582 
583    Level: intermediate
584 
585    Note:
586    Only some options have values associated with them, such as
587    -ksp_rtol tol.  Other options stand alone, such as -ksp_monitor.
588 
589   Concepts: options database^adding option
590 
591 .seealso: PetscOptionsInsert()
592 @*/
593 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetValue(const char iname[],const char value[])
594 {
595   size_t         len;
596   PetscErrorCode ierr;
597   PetscInt       N,n,i;
598   char           **names;
599   const char     *name = (char*)iname;
600   PetscTruth     gt,match;
601 
602   PetscFunctionBegin;
603   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
604 
605   /* this is so that -h and -help are equivalent (p4 does not like -help)*/
606   ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr);
607   if (match) name = "-help";
608 
609   name++;
610   /* first check against aliases */
611   N = options->Naliases;
612   for (i=0; i<N; i++) {
613     ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr);
614     if (match) {
615       name = options->aliases2[i];
616       break;
617     }
618   }
619 
620   N     = options->N;
621   n     = N;
622   names = options->names;
623 
624   for (i=0; i<N; i++) {
625     ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
626     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
627     if (match) {
628       if (options->values[i]) free(options->values[i]);
629       ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
630       if (len) {
631         options->values[i] = (char*)malloc((len+1)*sizeof(char));
632         ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr);
633       } else { options->values[i] = 0;}
634       PetscOptionsMonitor(name,value);
635       PetscFunctionReturn(0);
636     } else if (gt) {
637       n = i;
638       break;
639     }
640   }
641   if (N >= MAXOPTIONS) {
642     SETERRQ1(PETSC_ERR_PLIB,"No more room in option table, limit %d recompile \n src/sys/objects/options.c with larger value for MAXOPTIONS\n",MAXOPTIONS);
643   }
644   /* shift remaining values down 1 */
645   for (i=N; i>n; i--) {
646     names[i]           = names[i-1];
647     options->values[i] = options->values[i-1];
648     options->used[i]   = options->used[i-1];
649   }
650   /* insert new name and value */
651   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
652   names[n] = (char*)malloc((len+1)*sizeof(char));
653   ierr = PetscStrcpy(names[n],name);CHKERRQ(ierr);
654   if (value) {
655     ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
656     options->values[n] = (char*)malloc((len+1)*sizeof(char));
657     ierr = PetscStrcpy(options->values[n],value);CHKERRQ(ierr);
658   } else {options->values[n] = 0;}
659   options->used[n] = PETSC_FALSE;
660   options->N++;
661   PetscOptionsMonitor(name,value);
662   PetscFunctionReturn(0);
663 }
664 
665 #undef __FUNCT__
666 #define __FUNCT__ "PetscOptionsClearValue"
667 /*@C
668    PetscOptionsClearValue - Clears an option name-value pair in the options
669    database, overriding whatever is already present.
670 
671    Not Collective, but setting values on certain processors could cause problems
672    for parallel objects looking for options.
673 
674    Input Parameter:
675 .  name - name of option, this SHOULD have the - prepended
676 
677    Level: intermediate
678 
679    Concepts: options database^removing option
680 .seealso: PetscOptionsInsert()
681 @*/
682 PetscErrorCode PETSC_DLLEXPORT PetscOptionsClearValue(const char iname[])
683 {
684   PetscErrorCode ierr;
685   PetscInt       N,n,i;
686   char           **names,*name=(char*)iname;
687   PetscTruth     gt,match;
688 
689   PetscFunctionBegin;
690   if (name[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
691   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
692 
693   name++;
694 
695   N     = options->N; n = 0;
696   names = options->names;
697 
698   for (i=0; i<N; i++) {
699     ierr  = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
700     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
701     if (match) {
702       if (options->values[i]) free(options->values[i]);
703       PetscOptionsMonitor(name,"");
704       break;
705     } else if (gt) {
706       PetscFunctionReturn(0); /* it was not listed */
707     }
708     n++;
709   }
710   if (n == N) PetscFunctionReturn(0); /* it was not listed */
711 
712   /* shift remaining values down 1 */
713   for (i=n; i<N-1; i++) {
714     names[i]           = names[i+1];
715     options->values[i] = options->values[i+1];
716     options->used[i]   = options->used[i+1];
717   }
718   options->N--;
719   PetscFunctionReturn(0);
720 }
721 
722 #undef __FUNCT__
723 #define __FUNCT__ "PetscOptionsSetAlias"
724 /*@C
725    PetscOptionsReject - Generates an error if a certain option is given.
726 
727    Not Collective, but setting values on certain processors could cause problems
728    for parallel objects looking for options.
729 
730    Input Parameters:
731 +  name - the option one is seeking
732 -  mess - error message (may be PETSC_NULL)
733 
734    Level: advanced
735 
736    Concepts: options database^rejecting option
737 
738 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
739            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(),
740           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
741           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
742           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
743           PetscOptionsList(), PetscOptionsEList()
744 @*/
745 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetAlias(const char inewname[],const char ioldname[])
746 {
747   PetscErrorCode ierr;
748   PetscInt       n = options->Naliases;
749   size_t         len;
750   char           *newname = (char *)inewname,*oldname = (char*)ioldname;
751 
752   PetscFunctionBegin;
753   if (newname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname);
754   if (oldname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname);
755   if (n >= MAXALIASES) {
756     SETERRQ1(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);
757   }
758 
759   newname++; oldname++;
760   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
761   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
762   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
763   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
764   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
765   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
766   options->Naliases++;
767   PetscFunctionReturn(0);
768 }
769 
770 #undef __FUNCT__
771 #define __FUNCT__ "PetscOptionsFindPair_Private"
772 static PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscTruth *flg)
773 {
774   PetscErrorCode ierr;
775   PetscInt       i,N;
776   size_t         len;
777   char           **names,tmp[256];
778   PetscTruth     match;
779 
780   PetscFunctionBegin;
781   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
782   N = options->N;
783   names = options->names;
784 
785   if (name[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
786 
787   /* append prefix to name */
788   if (pre) {
789     if (pre[0] == '-') SETERRQ(PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
790     ierr = PetscStrncpy(tmp,pre,256);CHKERRQ(ierr);
791     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
792     ierr = PetscStrncat(tmp,name+1,256-len-1);CHKERRQ(ierr);
793   } else {
794     ierr = PetscStrncpy(tmp,name+1,256);CHKERRQ(ierr);
795   }
796 
797   /* slow search */
798   *flg = PETSC_FALSE;
799   for (i=0; i<N; i++) {
800     ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr);
801     if (match) {
802        *value           = options->values[i];
803        options->used[i] = PETSC_TRUE;
804        *flg             = PETSC_TRUE;
805        break;
806      }
807   }
808   if (!*flg) {
809     PetscInt j,cnt = 0,locs[16],loce[16];
810     size_t   n;
811     ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr);
812     /* determine the location and number of all _%d_ in the key */
813     for (i=0; i< (PetscInt)n; i++) {
814       if (tmp[i] == '_') {
815         for (j=i+1; j< (PetscInt)n; j++) {
816           if (tmp[j] >= '0' && tmp[j] <= '9') continue;
817           if (tmp[j] == '_' && j > i+1) { /* found a number */
818             locs[cnt]   = i+1;
819             loce[cnt++] = j+1;
820           }
821           break;
822         }
823       }
824     }
825     if (cnt) {
826       char tmp2[256];
827       for (i=0; i<cnt; i++) {
828         ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr);
829         ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr);
830         ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr);
831         ierr = PetscOptionsFindPair_Private(PETSC_NULL,tmp2,value,flg);CHKERRQ(ierr);
832         if (*flg) break;
833       }
834     }
835   }
836   PetscFunctionReturn(0);
837 }
838 
839 #undef __FUNCT__
840 #define __FUNCT__ "PetscOptionsReject"
841 /*@C
842    PetscOptionsReject - Generates an error if a certain option is given.
843 
844    Not Collective, but setting values on certain processors could cause problems
845    for parallel objects looking for options.
846 
847    Input Parameters:
848 +  name - the option one is seeking
849 -  mess - error message (may be PETSC_NULL)
850 
851    Level: advanced
852 
853    Concepts: options database^rejecting option
854 
855 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
856            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
857           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
858           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
859           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
860           PetscOptionsList(), PetscOptionsEList()
861 @*/
862 PetscErrorCode PETSC_DLLEXPORT PetscOptionsReject(const char name[],const char mess[])
863 {
864   PetscErrorCode ierr;
865   PetscTruth     flag;
866 
867   PetscFunctionBegin;
868   ierr = PetscOptionsHasName(PETSC_NULL,name,&flag);CHKERRQ(ierr);
869   if (flag) {
870     if (mess) {
871       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess);
872     } else {
873       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name);
874     }
875   }
876   PetscFunctionReturn(0);
877 }
878 
879 #undef __FUNCT__
880 #define __FUNCT__ "PetscOptionsHasName"
881 /*@C
882    PetscOptionsHasName - Determines whether a certain option is given in the database.
883 
884    Not Collective
885 
886    Input Parameters:
887 +  name - the option one is seeking
888 -  pre - string to prepend to the name or PETSC_NULL
889 
890    Output Parameters:
891 .  flg - PETSC_TRUE if found else PETSC_FALSE.
892 
893    Level: beginner
894 
895    Concepts: options database^has option name
896 
897    Notes: Name cannot be simply -h
898 
899 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
900            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
901           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
902           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
903           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
904           PetscOptionsList(), PetscOptionsEList()
905 @*/
906 PetscErrorCode PETSC_DLLEXPORT PetscOptionsHasName(const char pre[],const char name[],PetscTruth *flg)
907 {
908   char           *value;
909   PetscErrorCode ierr;
910   PetscTruth     isfalse,flag;
911 
912   PetscFunctionBegin;
913   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
914 
915   /* remove if turned off */
916   if (flag) {
917     ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
918     if (isfalse) flag = PETSC_FALSE;
919     ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
920     if (isfalse) flag = PETSC_FALSE;
921     ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
922     if (isfalse) flag = PETSC_FALSE;
923   }
924   if (flg) *flg = flag;
925   PetscFunctionReturn(0);
926 }
927 
928 #undef __FUNCT__
929 #define __FUNCT__ "PetscOptionsGetInt"
930 /*@C
931    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
932 
933    Not Collective
934 
935    Input Parameters:
936 +  pre - the string to prepend to the name or PETSC_NULL
937 -  name - the option one is seeking
938 
939    Output Parameter:
940 +  ivalue - the integer value to return
941 -  flg - PETSC_TRUE if found, else PETSC_FALSE
942 
943    Level: beginner
944 
945    Concepts: options database^has int
946 
947 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
948           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth()
949           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(),
950           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
951           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
952           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
953           PetscOptionsList(), PetscOptionsEList()
954 @*/
955 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscTruth *flg)
956 {
957   char           *value;
958   PetscErrorCode ierr;
959   PetscTruth     flag;
960 
961   PetscFunctionBegin;
962   PetscValidCharPointer(name,2);
963   PetscValidIntPointer(ivalue,3);
964   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
965   if (flag) {
966     if (!value) {if (flg) *flg = PETSC_FALSE;}
967     else {
968       if (flg) *flg = PETSC_TRUE;
969       ierr = PetscOptionsAtoi(value,ivalue);CHKERRQ(ierr);
970     }
971   } else {
972     if (flg) *flg = PETSC_FALSE;
973   }
974   PetscFunctionReturn(0);
975 }
976 
977 #undef __FUNCT__
978 #define __FUNCT__ "PetscOptionsGetEList"
979 /*@C
980      PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
981 
982    Not Collective
983 
984    Input Parameters:
985 +  pre - the string to prepend to the name or PETSC_NULL
986 .  opt - option name
987 .  list - the possible choices
988 .  ntext - number of choices
989 
990    Output Parameter:
991 +  value - the index of the value to return
992 -  set - PETSC_TRUE if found, else PETSC_FALSE
993 
994    Level: intermediate
995 
996    See PetscOptionsList() for when the choices are given in a PetscFList()
997 
998    Concepts: options database^list
999 
1000 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1001            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1002           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1003           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1004           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1005           PetscOptionsList(), PetscOptionsEList()
1006 @*/
1007 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEList(const char pre[],const char opt[],const char **list,PetscInt ntext,PetscInt *value,PetscTruth *set)
1008 {
1009   PetscErrorCode ierr;
1010   size_t         alen,len = 0;
1011   char           *svalue;
1012   PetscTruth     aset,flg = PETSC_FALSE;
1013   PetscInt       i;
1014 
1015   PetscFunctionBegin;
1016   for ( i=0; i<ntext; i++) {
1017     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
1018     if (alen > len) len = alen;
1019   }
1020   len += 5; /* a little extra space for user mistypes */
1021   ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr);
1022   ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr);
1023   if (aset) {
1024     if (set) *set = PETSC_TRUE;
1025     for (i=0; i<ntext; i++) {
1026       ierr = PetscStrcasecmp(svalue,list[i],&flg);CHKERRQ(ierr);
1027       if (flg) {
1028         *value = i;
1029         break;
1030       }
1031     }
1032     if (!flg) SETERRQ3(PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre?pre:"",opt+1);
1033   } else if (set) {
1034     *set = PETSC_FALSE;
1035   }
1036   ierr = PetscFree(svalue);CHKERRQ(ierr);
1037   PetscFunctionReturn(0);
1038 }
1039 
1040 #undef __FUNCT__
1041 #define __FUNCT__ "PetscOptionsEnum"
1042 /*@C
1043    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
1044 
1045    Not Collective
1046 
1047    Input Parameters:
1048 +  pre - option prefix or PETSC_NULL
1049 .  opt - option name
1050 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
1051 -  defaultv - the default (current) value
1052 
1053    Output Parameter:
1054 +  value - the  value to return
1055 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1056 
1057    Level: beginner
1058 
1059    Concepts: options database
1060 
1061    Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd()
1062 
1063           list is usually something like PCASMTypes or some other predefined list of enum names
1064 
1065 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
1066           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth()
1067           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(),
1068           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1069           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1070           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1071           PetscOptionsList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
1072 @*/
1073 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEnum(const char pre[],const char opt[],const char **list,PetscEnum *value,PetscTruth *set)
1074 {
1075   PetscErrorCode ierr;
1076   PetscInt       ntext = 0;
1077 
1078   PetscFunctionBegin;
1079   while (list[ntext++]) {
1080     if (ntext > 50) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
1081   }
1082   if (ntext < 3) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
1083   ntext -= 3;
1084   ierr = PetscOptionsGetEList(pre,opt,list,ntext,(PetscInt*)value,set);CHKERRQ(ierr);
1085   PetscFunctionReturn(0);
1086 }
1087 
1088 #undef __FUNCT__
1089 #define __FUNCT__ "PetscOptionsGetTruth"
1090 /*@C
1091    PetscOptionsGetTruth - Gets the Logical (true or false) value for a particular
1092             option in the database.
1093 
1094    Not Collective
1095 
1096    Input Parameters:
1097 +  pre - the string to prepend to the name or PETSC_NULL
1098 -  name - the option one is seeking
1099 
1100    Output Parameter:
1101 +  ivalue - the logical value to return
1102 -  flg - PETSC_TRUE  if found, else PETSC_FALSE
1103 
1104    Level: beginner
1105 
1106    Notes:
1107        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1108        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1109 
1110    Concepts: options database^has logical
1111 
1112 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1113           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsTruth(),
1114           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1115           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1116           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1117           PetscOptionsList(), PetscOptionsEList()
1118 @*/
1119 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruth(const char pre[],const char name[],PetscTruth *ivalue,PetscTruth *flg)
1120 {
1121   char           *value;
1122   PetscTruth     flag,istrue,isfalse;
1123   PetscErrorCode ierr;
1124 
1125   PetscFunctionBegin;
1126   PetscValidCharPointer(name,2);
1127   PetscValidIntPointer(ivalue,3);
1128   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1129   if (flag) {
1130     if (flg) *flg = PETSC_TRUE;
1131     if (!value) {
1132       *ivalue = PETSC_TRUE;
1133     } else {
1134       *ivalue = PETSC_TRUE;
1135       ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr);
1136       if (istrue) PetscFunctionReturn(0);
1137       ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr);
1138       if (istrue) PetscFunctionReturn(0);
1139       ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr);
1140       if (istrue) PetscFunctionReturn(0);
1141       ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr);
1142       if (istrue) PetscFunctionReturn(0);
1143 
1144       *ivalue = PETSC_FALSE;
1145       ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
1146       if (isfalse) PetscFunctionReturn(0);
1147       ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
1148       if (isfalse) PetscFunctionReturn(0);
1149       ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
1150       if (isfalse) PetscFunctionReturn(0);
1151       ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr);
1152       if (isfalse) PetscFunctionReturn(0);
1153 
1154       SETERRQ1(PETSC_ERR_ARG_WRONG,"Unknown logical value: %s",value);
1155     }
1156   } else {
1157     if (flg) *flg = PETSC_FALSE;
1158   }
1159   PetscFunctionReturn(0);
1160 }
1161 
1162 #undef __FUNCT__
1163 #define __FUNCT__ "PetscOptionsGetReal"
1164 /*@C
1165    PetscOptionsGetReal - Gets the double precision value for a particular
1166    option in the database.
1167 
1168    Not Collective
1169 
1170    Input Parameters:
1171 +  pre - string to prepend to each name or PETSC_NULL
1172 -  name - the option one is seeking
1173 
1174    Output Parameter:
1175 +  dvalue - the double value to return
1176 -  flg - PETSC_TRUE if found, PETSC_FALSE if not found
1177 
1178    Level: beginner
1179 
1180    Concepts: options database^has double
1181 
1182 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1183            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(),
1184           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1185           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1186           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1187           PetscOptionsList(), PetscOptionsEList()
1188 @*/
1189 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscTruth *flg)
1190 {
1191   char           *value;
1192   PetscErrorCode ierr;
1193   PetscTruth     flag;
1194 
1195   PetscFunctionBegin;
1196   PetscValidCharPointer(name,2);
1197   PetscValidDoublePointer(dvalue,3);
1198   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1199   if (flag) {
1200     if (!value) {if (flg) *flg = PETSC_FALSE;}
1201     else        {if (flg) *flg = PETSC_TRUE; ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);}
1202   } else {
1203     if (flg) *flg = PETSC_FALSE;
1204   }
1205   PetscFunctionReturn(0);
1206 }
1207 
1208 #undef __FUNCT__
1209 #define __FUNCT__ "PetscOptionsGetScalar"
1210 /*@C
1211    PetscOptionsGetScalar - Gets the scalar value for a particular
1212    option in the database.
1213 
1214    Not Collective
1215 
1216    Input Parameters:
1217 +  pre - string to prepend to each name or PETSC_NULL
1218 -  name - the option one is seeking
1219 
1220    Output Parameter:
1221 +  dvalue - the double value to return
1222 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1223 
1224    Level: beginner
1225 
1226    Usage:
1227    A complex number 2+3i can be specified as 2,3 at the command line.
1228    or a number 2.0e-10 - 3.3e-20 i  can be specified as 2.0e-10,3.3e-20
1229 
1230    Concepts: options database^has scalar
1231 
1232 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1233            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1234           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1235           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1236           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1237           PetscOptionsList(), PetscOptionsEList()
1238 @*/
1239 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscTruth *flg)
1240 {
1241   char           *value;
1242   PetscTruth     flag;
1243   PetscErrorCode ierr;
1244 
1245   PetscFunctionBegin;
1246   PetscValidCharPointer(name,2);
1247   PetscValidScalarPointer(dvalue,3);
1248   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1249   if (flag) {
1250     if (!value) {
1251       if (flg) *flg = PETSC_FALSE;
1252     } else {
1253 #if !defined(PETSC_USE_COMPLEX)
1254       ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);
1255 #else
1256       PetscReal  re=0.0,im=0.0;
1257       PetscToken *token;
1258       char       *tvalue = 0;
1259 
1260       ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1261       ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1262       if (!tvalue) { SETERRQ(PETSC_ERR_ARG_WRONG,"unknown string specified\n"); }
1263       ierr    = PetscOptionsAtod(tvalue,&re);CHKERRQ(ierr);
1264       ierr    = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1265       if (!tvalue) { /* Unknown separator used. using only real value */
1266         *dvalue = re;
1267       } else {
1268         ierr    = PetscOptionsAtod(tvalue,&im);CHKERRQ(ierr);
1269         *dvalue = re + PETSC_i*im;
1270       }
1271       ierr    = PetscTokenDestroy(token);CHKERRQ(ierr);
1272 #endif
1273       if (flg) *flg    = PETSC_TRUE;
1274     }
1275   } else { /* flag */
1276     if (flg) *flg = PETSC_FALSE;
1277   }
1278   PetscFunctionReturn(0);
1279 }
1280 
1281 #undef __FUNCT__
1282 #define __FUNCT__ "PetscOptionsGetRealArray"
1283 /*@C
1284    PetscOptionsGetRealArray - Gets an array of double precision values for a
1285    particular option in the database.  The values must be separated with
1286    commas with no intervening spaces.
1287 
1288    Not Collective
1289 
1290    Input Parameters:
1291 +  pre - string to prepend to each name or PETSC_NULL
1292 .  name - the option one is seeking
1293 -  nmax - maximum number of values to retrieve
1294 
1295    Output Parameters:
1296 +  dvalue - the double value to return
1297 .  nmax - actual number of values retreived
1298 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1299 
1300    Level: beginner
1301 
1302    Concepts: options database^array of doubles
1303 
1304 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1305            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
1306           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1307           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1308           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1309           PetscOptionsList(), PetscOptionsEList()
1310 @*/
1311 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscTruth *flg)
1312 {
1313   char           *value;
1314   PetscErrorCode ierr;
1315   PetscInt       n = 0;
1316   PetscTruth     flag;
1317   PetscToken     *token;
1318 
1319   PetscFunctionBegin;
1320   PetscValidCharPointer(name,2);
1321   PetscValidDoublePointer(dvalue,3);
1322   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1323   if (!flag)  {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1324   if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1325 
1326   if (flg) *flg = PETSC_TRUE;
1327 
1328   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1329   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1330   while (n < *nmax) {
1331     if (!value) break;
1332     ierr = PetscOptionsAtod(value,dvalue++);CHKERRQ(ierr);
1333     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1334     n++;
1335   }
1336   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
1337   *nmax = n;
1338   PetscFunctionReturn(0);
1339 }
1340 
1341 #undef __FUNCT__
1342 #define __FUNCT__ "PetscOptionsGetIntArray"
1343 /*@C
1344    PetscOptionsGetIntArray - Gets an array of integer values for a particular
1345    option in the database.  The values must be separated with commas with
1346    no intervening spaces.
1347 
1348    Not Collective
1349 
1350    Input Parameters:
1351 +  pre - string to prepend to each name or PETSC_NULL
1352 .  name - the option one is seeking
1353 -  nmax - maximum number of values to retrieve
1354 
1355    Output Parameter:
1356 +  dvalue - the integer values to return
1357 .  nmax - actual number of values retreived
1358 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1359 
1360    Level: beginner
1361 
1362    Concepts: options database^array of ints
1363 
1364 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1365            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1366           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1367           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1368           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1369           PetscOptionsList(), PetscOptionsEList()
1370 @*/
1371 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscTruth *flg)
1372 {
1373   char           *value;
1374   PetscErrorCode ierr;
1375   PetscInt       n = 0,i,start,end;
1376   size_t         len;
1377   PetscTruth     flag,foundrange;
1378   PetscToken     *token;
1379 
1380   PetscFunctionBegin;
1381   PetscValidCharPointer(name,2);
1382   PetscValidIntPointer(dvalue,3);
1383   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1384   if (!flag)  {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1385   if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1386 
1387   if (flg) *flg = PETSC_TRUE;
1388 
1389   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1390   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1391   while (n < *nmax) {
1392     if (!value) break;
1393 
1394     /* look for form  d-D where d and D are integers */
1395     foundrange = PETSC_FALSE;
1396     ierr      = PetscStrlen(value,&len);CHKERRQ(ierr);
1397     if (value[0] == '-') i=2;
1398     else i=1;
1399     for (;i<(int)len; i++) {
1400       if (value[i] == '-') {
1401         if (i == (int)len-1) SETERRQ2(PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
1402         value[i] = 0;
1403         ierr     = PetscOptionsAtoi(value,&start);CHKERRQ(ierr);
1404         ierr     = PetscOptionsAtoi(value+i+1,&end);CHKERRQ(ierr);
1405         if (end <= start) SETERRQ3(PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1);
1406         if (n + end - start - 1 >= *nmax) SETERRQ4(PETSC_ERR_USER,"Error in %D-th array entry, not enough space in left in array (%D) to contain entire range from %D to %D",n,*nmax-n,start,end);
1407         for (;start<end; start++) {
1408           *dvalue = start; dvalue++;n++;
1409         }
1410         foundrange = PETSC_TRUE;
1411         break;
1412       }
1413     }
1414     if (!foundrange) {
1415       ierr      = PetscOptionsAtoi(value,dvalue);CHKERRQ(ierr);
1416       dvalue++;
1417       n++;
1418     }
1419     ierr      = PetscTokenFind(token,&value);CHKERRQ(ierr);
1420   }
1421   ierr      = PetscTokenDestroy(token);CHKERRQ(ierr);
1422   *nmax = n;
1423   PetscFunctionReturn(0);
1424 }
1425 
1426 #undef __FUNCT__
1427 #define __FUNCT__ "PetscOptionsGetString"
1428 /*@C
1429    PetscOptionsGetString - Gets the string value for a particular option in
1430    the database.
1431 
1432    Not Collective
1433 
1434    Input Parameters:
1435 +  pre - string to prepend to name or PETSC_NULL
1436 .  name - the option one is seeking
1437 -  len - maximum string length
1438 
1439    Output Parameters:
1440 +  string - location to copy string
1441 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1442 
1443    Level: beginner
1444 
1445    Fortran Note:
1446    The Fortran interface is slightly different from the C/C++
1447    interface (len is not used).  Sample usage in Fortran follows
1448 .vb
1449       character *20 string
1450       integer   flg, ierr
1451       call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-s',string,flg,ierr)
1452 .ve
1453 
1454    Concepts: options database^string
1455 
1456 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1457            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1458           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1459           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1460           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1461           PetscOptionsList(), PetscOptionsEList()
1462 @*/
1463 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscTruth *flg)
1464 {
1465   char           *value;
1466   PetscErrorCode ierr;
1467   PetscTruth     flag;
1468 
1469   PetscFunctionBegin;
1470   PetscValidCharPointer(name,2);
1471   PetscValidCharPointer(string,3);
1472   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1473   if (!flag) {
1474     if (flg) *flg = PETSC_FALSE;
1475   } else {
1476     if (flg) *flg = PETSC_TRUE;
1477     if (value) {
1478       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
1479     } else {
1480       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
1481     }
1482   }
1483   PetscFunctionReturn(0);
1484 }
1485 
1486 #undef __FUNCT__
1487 #define __FUNCT__ "PetscOptionsGetStringArray"
1488 /*@C
1489    PetscOptionsGetStringArray - Gets an array of string values for a particular
1490    option in the database. The values must be separated with commas with
1491    no intervening spaces.
1492 
1493    Not Collective
1494 
1495    Input Parameters:
1496 +  pre - string to prepend to name or PETSC_NULL
1497 .  name - the option one is seeking
1498 -  nmax - maximum number of strings
1499 
1500    Output Parameter:
1501 +  strings - location to copy strings
1502 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1503 
1504    Level: beginner
1505 
1506    Notes:
1507    The user should pass in an array of pointers to char, to hold all the
1508    strings returned by this function.
1509 
1510    The user is responsible for deallocating the strings that are
1511    returned. The Fortran interface for this routine is not supported.
1512 
1513    Contributed by Matthew Knepley.
1514 
1515    Concepts: options database^array of strings
1516 
1517 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1518            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1519           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1520           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1521           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1522           PetscOptionsList(), PetscOptionsEList()
1523 @*/
1524 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscTruth *flg)
1525 {
1526   char           *value;
1527   PetscErrorCode ierr;
1528   PetscInt       n;
1529   PetscTruth     flag;
1530   PetscToken     *token;
1531 
1532   PetscFunctionBegin;
1533   PetscValidCharPointer(name,2);
1534   PetscValidPointer(strings,3);
1535   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1536   if (!flag)  {*nmax = 0; if (flg) *flg = PETSC_FALSE; PetscFunctionReturn(0);}
1537   if (!value) {*nmax = 0; if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);}
1538   if (!*nmax) {if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);}
1539   if (flg) *flg = PETSC_TRUE;
1540 
1541   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1542   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1543   n = 0;
1544   while (n < *nmax) {
1545     if (!value) break;
1546     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
1547     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1548     n++;
1549   }
1550   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
1551   *nmax = n;
1552   PetscFunctionReturn(0);
1553 }
1554 
1555 #undef __FUNCT__
1556 #define __FUNCT__ "PetscOptionsAllUsed"
1557 /*@C
1558    PetscOptionsAllUsed - Returns a count of the number of options in the
1559    database that have never been selected.
1560 
1561    Not Collective
1562 
1563    Output Parameter:
1564 .   N - count of options not used
1565 
1566    Level: advanced
1567 
1568 .seealso: PetscOptionsPrint()
1569 @*/
1570 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAllUsed(int *N)
1571 {
1572   PetscInt i,n = 0;
1573 
1574   PetscFunctionBegin;
1575   for (i=0; i<options->N; i++) {
1576     if (!options->used[i]) { n++; }
1577   }
1578   *N = n;
1579   PetscFunctionReturn(0);
1580 }
1581 
1582 #undef __FUNCT__
1583 #define __FUNCT__ "PetscOptionsLeft"
1584 /*@
1585     PetscOptionsLeft - Prints to screen any options that were set and never used.
1586 
1587   Not collective
1588 
1589    Options Database Key:
1590 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
1591 
1592   Level: advanced
1593 
1594 .seealso: PetscOptionsAllUsed()
1595 @*/
1596 PetscErrorCode PETSC_DLLEXPORT PetscOptionsLeft(void)
1597 {
1598   PetscErrorCode ierr;
1599   PetscInt       i;
1600 
1601   PetscFunctionBegin;
1602   for (i=0; i<options->N; i++) {
1603     if (!options->used[i]) {
1604       if (options->values[i]) {
1605         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
1606       } else {
1607         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s no value \n",options->names[i]);CHKERRQ(ierr);
1608       }
1609     }
1610   }
1611   PetscFunctionReturn(0);
1612 }
1613 
1614 
1615 /*
1616     PetscOptionsCreate - Creates the empty options database.
1617 
1618 */
1619 #undef __FUNCT__
1620 #define __FUNCT__ "PetscOptionsCreate"
1621 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCreate(void)
1622 {
1623   PetscErrorCode ierr;
1624 
1625   PetscFunctionBegin;
1626   options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable));
1627   ierr    = PetscMemzero(options->used,MAXOPTIONS*sizeof(PetscTruth));CHKERRQ(ierr);
1628   options->namegiven 		= PETSC_FALSE;
1629   options->N         		= 0;
1630   options->Naliases  		= 0;
1631   options->numbermonitors 	= 0;
1632 
1633   PetscFunctionReturn(0);
1634 }
1635 
1636 #undef __FUNCT__
1637 #define __FUNCT__ "PetscOptionsSetFromOptions"
1638 /*@
1639    PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options.
1640 
1641    Collective on PETSC_COMM_WORLD
1642 
1643    Options Database Keys:
1644 +  -options_monitor <optional filename> - prints the names and values of all
1645  				runtime options as they are set. The monitor functionality is not
1646                 available for options set through a file, environment variable, or on
1647                 the command line. Only options set after PetscInitialize completes will
1648                 be monitored.
1649 .  -options_monitor_cancel - cancel all options database monitors
1650 
1651    Notes:
1652    To see all options, run your program with the -help option or consult
1653    the users manual.
1654 
1655    Level: intermediate
1656 
1657 .keywords: set, options, database
1658 @*/
1659 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetFromOptions()
1660 {
1661   PetscTruth          flg;
1662   PetscErrorCode      ierr;
1663   char                monfilename[PETSC_MAX_PATH_LEN];
1664   PetscViewer         monviewer;
1665 
1666   PetscFunctionBegin;
1667 
1668   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr);
1669   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1670   if (flg && (!options->numbermonitors)) {
1671     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
1672     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
1673   }
1674 
1675   ierr = PetscOptionsName("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",&flg);CHKERRQ(ierr);
1676   if (flg) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
1677 
1678   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1679 
1680   PetscFunctionReturn(0);
1681 }
1682 
1683 
1684 #undef __FUNCT__
1685 #define __FUNCT__ "PetscOptionsMonitorDefault"
1686 /*@C
1687    PetscOptionsMonitorDefault - Print all options set value events.
1688 
1689    Collective on PETSC_COMM_WORLD
1690 
1691    Input Parameters:
1692 +  name  - option name string
1693 .  value - option value string
1694 -  dummy - unused monitor context
1695 
1696    Level: intermediate
1697 
1698 .keywords: PetscOptions, default, monitor
1699 
1700 .seealso: PetscOptionsMonitorSet()
1701 @*/
1702 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
1703 {
1704   PetscErrorCode ierr;
1705   PetscViewer    viewer = (PetscViewer) dummy;
1706 
1707   PetscFunctionBegin;
1708   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD);
1709   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
1710   PetscFunctionReturn(0);
1711 }
1712 
1713 #undef __FUNCT__
1714 #define __FUNCT__ "PetscOptionsMonitorSet"
1715 /*@C
1716    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
1717    modified the PETSc options database.
1718 
1719    Not collective
1720 
1721    Input Parameters:
1722 +  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring
1723 .  mctx    - [optional] context for private data for the
1724              monitor routine (use PETSC_NULL if no context is desired)
1725 -  monitordestroy - [optional] routine that frees monitor context
1726           (may be PETSC_NULL)
1727 
1728    Calling Sequence of monitor:
1729 $     monitor (const char name[], const char value[], void *mctx)
1730 
1731 +  name - option name string
1732 .  value - option value string
1733 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
1734 
1735    Options Database Keys:
1736 +    -options_monitor    - sets PetscOptionsMonitorDefault()
1737 -    -options_monitor_cancel - cancels all monitors that have
1738                           been hardwired into a code by
1739                           calls to PetscOptionsMonitorSet(), but
1740                           does not cancel those set via
1741                           the options database.
1742 
1743    Notes:
1744    The default is to do nothing.  To print the name and value of options
1745    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
1746    with a null monitoring context.
1747 
1748    Several different monitoring routines may be set by calling
1749    PetscOptionsMonitorSet() multiple times; all will be called in the
1750    order in which they were set.
1751 
1752    Level: beginner
1753 
1754 .keywords: PetscOptions, set, monitor
1755 
1756 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
1757 @*/
1758 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
1759 {
1760   PetscFunctionBegin;
1761   if (options->numbermonitors >= MAXOPTIONSMONITORS) {
1762     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
1763   }
1764   options->monitor[options->numbermonitors]           = monitor;
1765   options->monitordestroy[options->numbermonitors]    = monitordestroy;
1766   options->monitorcontext[options->numbermonitors++]  = (void*)mctx;
1767   PetscFunctionReturn(0);
1768 }
1769 
1770 #undef __FUNCT__
1771 #define __FUNCT__ "PetscOptionsMonitorCancel"
1772 /*@
1773    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
1774 
1775    Not collective
1776 
1777    Options Database Key:
1778 .  -options_monitor_cancel - Cancels all monitors that have
1779     been hardwired into a code by calls to PetscOptionsMonitorSet(),
1780     but does not cancel those set via the options database.
1781 
1782    Level: intermediate
1783 
1784 .keywords: PetscOptions, set, monitor
1785 
1786 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
1787 @*/
1788 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorCancel(void)
1789 {
1790   PetscErrorCode ierr;
1791   PetscInt       i;
1792 
1793   PetscFunctionBegin;
1794   for (i=0; i<options->numbermonitors; i++) {
1795     if (options->monitordestroy[i]) {
1796       ierr = (*options->monitordestroy[i])(options->monitorcontext[i]);CHKERRQ(ierr);
1797     }
1798   }
1799   options->numbermonitors = 0;
1800   PetscFunctionReturn(0);
1801 }
1802