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