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