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