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