xref: /petsc/src/sys/objects/options.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
1 
2 /* Define Feature test macros to make sure atoll is available (SVr4, POSIX.1-2001, 4.3BSD, C99), not in (C89 and POSIX.1-1996) */
3 #define PETSC_DESIRE_FEATURE_TEST_MACROS
4 
5 /*
6    These routines simplify the use of command line, file options, etc., and are used to manipulate the options database.
7    This provides the low-level interface, the high level interface is in aoptions.c
8 
9    Some routines use regular malloc and free because it cannot know  what malloc is requested with the
10    options database until it has already processed the input.
11 */
12 
13 #include <petscsys.h>        /*I  "petscsys.h"   I*/
14 #include <ctype.h>
15 #if defined(PETSC_HAVE_STDLIB_H)
16 #include <stdlib.h>
17 #endif
18 #if defined(PETSC_HAVE_MALLOC_H)
19 #include <malloc.h>
20 #endif
21 #if defined(PETSC_HAVE_SYS_PARAM_H)
22 #include <sys/param.h>
23 #endif
24 #if defined(PETSC_HAVE_YAML)
25 #include <yaml.h>
26 #endif
27 
28 /*
29     This table holds all the options set by the user. For simplicity, we use a static size database
30 */
31 #define MAXOPTIONS 512
32 #define MAXALIASES 25
33 #define MAXOPTIONSMONITORS 5
34 #define MAXPREFIXES 25
35 
36 typedef struct {
37   int            N,argc,Naliases;
38   char           **args,*names[MAXOPTIONS],*values[MAXOPTIONS];
39   char           *aliases1[MAXALIASES],*aliases2[MAXALIASES];
40   PetscBool      used[MAXOPTIONS];
41   PetscBool      namegiven;
42   char           programname[PETSC_MAX_PATH_LEN]; /* HP includes entire path in name */
43 
44   /* --------User (or default) routines (most return -1 on error) --------*/
45   PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], void*); /* returns control to user after */
46   PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void**);         /* */
47   void           *monitorcontext[MAXOPTIONSMONITORS];                  /* to pass arbitrary user data into monitor */
48   PetscInt       numbermonitors;                                       /* to, for instance, detect options being set */
49 
50   /* Prefixes */
51   PetscInt prefixind,prefixstack[MAXPREFIXES];
52   char prefix[2048];
53 } PetscOptionsTable;
54 
55 
56 static PetscOptionsTable      *options = 0;
57 extern PetscOptionsObjectType PetscOptionsObject;
58 
59 /*
60     Options events monitor
61 */
62 #define PetscOptionsMonitor(name,value)                                     \
63         { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \
64           for (_i=0; _i<_im; _i++) {\
65             _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \
66 	  } \
67 	}
68 
69 #undef __FUNCT__
70 #define __FUNCT__ "PetscOptionsStringToInt"
71 /*
72    PetscOptionsStringToInt - Converts a string to an integer value. Handles special cases such as "default" and "decide"
73 */
74 PetscErrorCode  PetscOptionsStringToInt(const char name[],PetscInt *a)
75 {
76   PetscErrorCode ierr;
77   size_t         i,len;
78   PetscBool      decide,tdefault,mouse;
79 
80   PetscFunctionBegin;
81   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
82   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
83 
84   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
85   if (!tdefault) {
86     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
87   }
88   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
89   if (!decide) {
90     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
91   }
92   ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr);
93 
94   if (tdefault) {
95     *a = PETSC_DEFAULT;
96   } else if (decide) {
97     *a = PETSC_DECIDE;
98   } else if (mouse) {
99     *a = -1;
100   } else {
101     if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') {
102       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
103     }
104     for (i=1; i<len; i++) {
105       if (name[i] < '0' || name[i] > '9') {
106         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
107       }
108     }
109 
110 #if defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE_ATOLL)
111     *a = atoll(name);
112 #elif defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE___INT64)
113     *a = _atoi64(name);
114 #else
115     *a = (PetscInt)atoi(name);
116 #endif
117   }
118   PetscFunctionReturn(0);
119 }
120 
121 #undef __FUNCT__
122 #define __FUNCT__ "PetscOptionsStringToReal"
123 /*
124    Converts a string to PetscReal value. Handles special cases like "default" and "decide"
125 */
126 PetscErrorCode  PetscOptionsStringToReal(const char name[],PetscReal *a)
127 {
128   PetscErrorCode ierr;
129   size_t         len;
130   PetscBool      decide,tdefault;
131 
132   PetscFunctionBegin;
133   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
134   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
135 
136   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
137   if (!tdefault) {
138     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
139   }
140   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
141   if (!decide) {
142     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
143   }
144 
145   if (tdefault) {
146     *a = PETSC_DEFAULT;
147   } else if (decide) {
148     *a = PETSC_DECIDE;
149   } else {
150     if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') {
151       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
152     }
153     *a  = atof(name);
154   }
155   PetscFunctionReturn(0);
156 }
157 
158 #undef __FUNCT__
159 #define __FUNCT__ "PetscOptionsStringToBool"
160 /*
161    PetscOptionsStringToBool - Converts string to PetscBool , handles cases like "yes", "no", "true", "false", "0", "1"
162 */
163 PetscErrorCode  PetscOptionsStringToBool(const char value[], PetscBool  *a)
164 {
165   PetscBool      istrue, isfalse;
166   size_t         len;
167   PetscErrorCode ierr;
168 
169   PetscFunctionBegin;
170   ierr = PetscStrlen(value, &len);CHKERRQ(ierr);
171   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Character string of length zero has no logical value");
172   ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr);
173   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
174   ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr);
175   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
176   ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr);
177   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
178   ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr);
179   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
180   ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
181   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
182   ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
183   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
184   ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
185   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
186   ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr);
187   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
188   SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Unknown logical value: %s", value);
189   PetscFunctionReturn(0);
190 }
191 
192 #undef __FUNCT__
193 #define __FUNCT__ "PetscGetProgramName"
194 /*@C
195     PetscGetProgramName - Gets the name of the running program.
196 
197     Not Collective
198 
199     Input Parameter:
200 .   len - length of the string name
201 
202     Output Parameter:
203 .   name - the name of the running program
204 
205    Level: advanced
206 
207     Notes:
208     The name of the program is copied into the user-provided character
209     array of length len.  On some machines the program name includes
210     its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN.
211 @*/
212 PetscErrorCode  PetscGetProgramName(char name[],size_t len)
213 {
214   PetscErrorCode ierr;
215 
216   PetscFunctionBegin;
217   if (!options) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscInitialize() first");
218   if (!options->namegiven) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to determine program name");
219   ierr = PetscStrncpy(name,options->programname,len);CHKERRQ(ierr);
220   PetscFunctionReturn(0);
221 }
222 
223 #undef __FUNCT__
224 #define __FUNCT__ "PetscSetProgramName"
225 PetscErrorCode  PetscSetProgramName(const char name[])
226 {
227   PetscErrorCode ierr;
228 
229   PetscFunctionBegin;
230   options->namegiven = PETSC_TRUE;
231   ierr  = PetscStrncpy(options->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
232   PetscFunctionReturn(0);
233 }
234 
235 #undef __FUNCT__
236 #define __FUNCT__ "PetscOptionsValidKey"
237 /*@
238     PetscOptionsValidKey - PETSc Options database keys must begin with one or two dashes (-) followed by a letter.
239 
240    Input Parameter:
241 .    in_str - string to check if valid
242 
243    Output Parameter:
244 .    key - PETSC_TRUE if a valid key
245 
246   Level: intermediate
247 
248 @*/
249 PetscErrorCode  PetscOptionsValidKey(const char in_str[],PetscBool  *key)
250 {
251   PetscFunctionBegin;
252   *key = PETSC_FALSE;
253   if (!in_str) PetscFunctionReturn(0);
254   if (in_str[0] != '-') PetscFunctionReturn(0);
255   if (in_str[1] == '-') in_str++;
256   if (!isalpha(in_str[1])) PetscFunctionReturn(0);
257   if ((!strncmp(in_str+1,"inf",3) || !strncmp(in_str+1,"INF",3)) && !(in_str[4] == '_' || isalnum(in_str[4]))) PetscFunctionReturn(0);
258   *key = PETSC_TRUE;
259   PetscFunctionReturn(0);
260 }
261 
262 #undef __FUNCT__
263 #define __FUNCT__ "PetscOptionsInsertString"
264 /*@C
265      PetscOptionsInsertString - Inserts options into the database from a string
266 
267      Not collective: but only processes that call this routine will set the options
268                      included in the string
269 
270   Input Parameter:
271 .   in_str - string that contains options separated by blanks
272 
273 
274   Level: intermediate
275 
276   Contributed by Boyana Norris
277 
278 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
279           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
280           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
281           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
282           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
283           PetscOptionsList(), PetscOptionsEList(), PetscOptionsInsertFile()
284 
285 @*/
286 PetscErrorCode  PetscOptionsInsertString(const char in_str[])
287 {
288   char           *first,*second;
289   PetscErrorCode ierr;
290   PetscToken     token;
291   PetscBool      key,ispush,ispop;
292 
293   PetscFunctionBegin;
294   ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr);
295   ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
296   while (first) {
297     ierr = PetscStrcasecmp(first,"-prefix_push",&ispush);CHKERRQ(ierr);
298     ierr = PetscStrcasecmp(first,"-prefix_pop",&ispop);CHKERRQ(ierr);
299     ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr);
300     if (ispush) {
301       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
302       ierr = PetscOptionsPrefixPush(second);CHKERRQ(ierr);
303       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
304     } else if (ispop) {
305       ierr = PetscOptionsPrefixPop();CHKERRQ(ierr);
306       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
307     } else if (key) {
308       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
309       ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr);
310       if (!key) {
311         ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr);
312         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
313       } else {
314         ierr  = PetscOptionsSetValue(first,PETSC_NULL);CHKERRQ(ierr);
315         first = second;
316       }
317     } else {
318       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
319     }
320   }
321   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
322   PetscFunctionReturn(0);
323 }
324 
325 /*
326     Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free()
327 */
328 static char *Petscgetline(FILE * f)
329 {
330   size_t size = 0;
331   size_t len  = 0;
332   size_t last = 0;
333   char * buf  = PETSC_NULL;
334 
335   if (feof(f)) return 0;
336   do {
337     size += 1024; /* BUFSIZ is defined as "the optimal read size for this platform" */
338     buf = (char*)realloc((void *)buf,size); /* realloc(NULL,n) is the same as malloc(n) */
339     /* Actually do the read. Note that fgets puts a terminal '\0' on the
340     end of the string, so we make sure we overwrite this */
341     if (!fgets(buf+len,size,f)) buf[len]=0;
342     PetscStrlen(buf,&len);
343     last = len - 1;
344   } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r');
345   if (len) return buf;
346   free(buf);
347   return 0;
348 }
349 
350 
351 #undef __FUNCT__
352 #define __FUNCT__ "PetscOptionsInsertFile"
353 /*@C
354      PetscOptionsInsertFile - Inserts options into the database from a file.
355 
356      Collective on MPI_Comm
357 
358   Input Parameter:
359 +   comm - the processes that will share the options (usually PETSC_COMM_WORLD)
360 .   file - name of file
361 -   require - if PETSC_TRUE will generate an error if the file does not exist
362 
363 
364   Level: developer
365 
366 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
367           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
368           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
369           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
370           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
371           PetscOptionsList(), PetscOptionsEList()
372 
373 @*/
374 PetscErrorCode  PetscOptionsInsertFile(MPI_Comm comm,const char file[],PetscBool require)
375 {
376   char           *string,fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*vstring = 0,*astring = 0;
377   PetscErrorCode ierr;
378   size_t         i,len;
379   FILE           *fd;
380   PetscToken     token;
381   int            err;
382   char           cmt[3]={'#','!','%'},*cmatch;
383   PetscMPIInt    rank,cnt=0,acnt=0;
384 
385   PetscFunctionBegin;
386   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
387   if (!rank) {
388     /* Warning: assume a maximum size for all options in a string */
389     ierr = PetscMalloc(128000*sizeof(char),&vstring);CHKERRQ(ierr);
390     vstring[0] = 0;
391     ierr = PetscMalloc(64000*sizeof(char),&astring);CHKERRQ(ierr);
392     astring[0] = 0;
393     cnt     = 0;
394     acnt    = 0;
395 
396     ierr = PetscFixFilename(file,fname);CHKERRQ(ierr);
397     fd   = fopen(fname,"r");
398     if (fd) {
399       /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */
400       ierr = PetscInfo1(0,"Opened options file %s\n",file);CHKERRQ(ierr);
401       while ((string = Petscgetline(fd))) {
402         /* eliminate comments from each line */
403         for (i=0; i<3; i++){
404           ierr = PetscStrchr(string,cmt[i],&cmatch);
405           if (cmatch) *cmatch = 0;
406         }
407         ierr = PetscStrlen(string,&len);CHKERRQ(ierr);
408         /* replace tabs, ^M, \n with " " */
409         for (i=0; i<len; i++) {
410           if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') {
411             string[i] = ' ';
412           }
413         }
414         ierr = PetscTokenCreate(string,' ',&token);CHKERRQ(ierr);
415         free(string);
416         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
417         if (!first) {
418           goto destroy;
419         } else if (!first[0]) { /* if first token is empty spaces, redo first token */
420           ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
421         }
422         ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
423         if (!first) {
424           goto destroy;
425         } else if (first[0] == '-') {
426           /* warning: should be making sure we do not overfill vstring */
427           ierr = PetscStrcat(vstring,first);CHKERRQ(ierr);
428           ierr = PetscStrcat(vstring," ");CHKERRQ(ierr);
429           if (second) {
430             /* protect second with quotes in case it contains strings */
431             ierr = PetscStrcat(vstring,"\"");CHKERRQ(ierr);
432             ierr = PetscStrcat(vstring,second);CHKERRQ(ierr);
433             ierr = PetscStrcat(vstring,"\"");CHKERRQ(ierr);
434           }
435           ierr = PetscStrcat(vstring," ");CHKERRQ(ierr);
436         } else {
437           PetscBool  match;
438 
439           ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr);
440           if (match) {
441             ierr = PetscTokenFind(token,&third);CHKERRQ(ierr);
442             if (!third) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second);
443             ierr = PetscStrcat(astring,second);CHKERRQ(ierr);
444             ierr = PetscStrcat(astring," ");CHKERRQ(ierr);
445             ierr = PetscStrcat(astring,third);CHKERRQ(ierr);
446             ierr = PetscStrcat(astring," ");CHKERRQ(ierr);
447           } else {
448             SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown statement in options file: (%s)",string);
449           }
450         }
451         destroy:
452         ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
453       }
454       err = fclose(fd);
455       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
456       ierr = PetscStrlen(astring,&len);CHKERRQ(ierr);
457       acnt = PetscMPIIntCast(len);CHKERRQ(ierr);
458       ierr = PetscStrlen(vstring,&len);CHKERRQ(ierr);
459       cnt  = PetscMPIIntCast(len);CHKERRQ(ierr);
460     } else if (require) {
461       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Unable to open Options File %s",fname);
462     }
463   }
464 
465   ierr = MPI_Bcast(&acnt,1,MPI_INT,0,comm);CHKERRQ(ierr);
466   if (acnt) {
467     PetscToken token;
468     char       *first,*second;
469 
470     if (rank) {
471       ierr = PetscMalloc((acnt+1)*sizeof(char),&astring);CHKERRQ(ierr);
472     }
473     ierr = MPI_Bcast(astring,acnt,MPI_CHAR,0,comm);CHKERRQ(ierr);
474     astring[acnt] = 0;
475     ierr = PetscTokenCreate(astring,' ',&token);CHKERRQ(ierr);
476     ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
477     while (first) {
478       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
479       ierr = PetscOptionsSetAlias(first,second);CHKERRQ(ierr);
480       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
481     }
482     ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
483   }
484 
485   ierr = MPI_Bcast(&cnt,1,MPI_INT,0,comm);CHKERRQ(ierr);
486   if (cnt) {
487     if (rank) {
488       ierr = PetscMalloc((cnt+1)*sizeof(char),&vstring);CHKERRQ(ierr);
489     }
490     ierr = MPI_Bcast(vstring,cnt,MPI_CHAR,0,comm);CHKERRQ(ierr);
491     vstring[cnt] = 0;
492     ierr = PetscOptionsInsertString(vstring);CHKERRQ(ierr);
493   }
494   ierr = PetscFree(astring);CHKERRQ(ierr);
495   ierr = PetscFree(vstring);CHKERRQ(ierr);
496   PetscFunctionReturn(0);
497 }
498 
499 #undef __FUNCT__
500 #define __FUNCT__ "PetscOptionsInsertArgs_Private"
501 static PetscErrorCode PetscOptionsInsertArgs_Private(int argc,char *args[])
502 {
503   PetscErrorCode ierr;
504   int            left    = argc - 1;
505   char           **eargs = args + 1;
506 
507   PetscFunctionBegin;
508   while (left) {
509     PetscBool  isoptions_file,isprefixpush,isprefixpop,isp4,tisp4,isp4yourname,isp4rmrank,key;
510     ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr);
511     ierr = PetscStrcasecmp(eargs[0],"-prefix_push",&isprefixpush);CHKERRQ(ierr);
512     ierr = PetscStrcasecmp(eargs[0],"-prefix_pop",&isprefixpop);CHKERRQ(ierr);
513     ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr);
514     ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr);
515     ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr);
516     ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr);
517     isp4 = (PetscBool) (isp4 || tisp4);
518     ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr);
519     isp4 = (PetscBool) (isp4 || tisp4);
520     ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr);
521     ierr = PetscOptionsValidKey(eargs[0],&key);CHKERRQ(ierr);
522 
523     if (!key) {
524       eargs++; left--;
525     } else if (isoptions_file) {
526       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
527       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
528       ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,eargs[1],PETSC_TRUE);CHKERRQ(ierr);
529       eargs += 2; left -= 2;
530     } else if (isprefixpush) {
531       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option");
532       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option (prefixes cannot start with '-')");
533       ierr = PetscOptionsPrefixPush(eargs[1]);CHKERRQ(ierr);
534       eargs += 2; left -= 2;
535     } else if (isprefixpop) {
536       ierr = PetscOptionsPrefixPop();CHKERRQ(ierr);
537       eargs++; left--;
538 
539       /*
540        These are "bad" options that MPICH, etc put on the command line
541        we strip them out here.
542        */
543     } else if (tisp4 || isp4rmrank) {
544       eargs += 1; left -= 1;
545     } else if (isp4 || isp4yourname) {
546       eargs += 2; left -= 2;
547     } else {
548       PetscBool nextiskey = PETSC_FALSE;
549       if (left >= 2) {ierr = PetscOptionsValidKey(eargs[1],&nextiskey);CHKERRQ(ierr);}
550       if (left < 2 || nextiskey) {
551         ierr = PetscOptionsSetValue(eargs[0],PETSC_NULL);CHKERRQ(ierr);
552         eargs++; left--;
553       } else {
554         ierr = PetscOptionsSetValue(eargs[0],eargs[1]);CHKERRQ(ierr);
555         eargs += 2; left -= 2;
556       }
557     }
558   }
559   PetscFunctionReturn(0);
560 }
561 
562 
563 #undef __FUNCT__
564 #define __FUNCT__ "PetscOptionsInsert"
565 /*@C
566    PetscOptionsInsert - Inserts into the options database from the command line,
567                    the environmental variable and a file.
568 
569    Input Parameters:
570 +  argc - count of number of command line arguments
571 .  args - the command line arguments
572 -  file - optional filename, defaults to ~username/.petscrc
573 
574    Note:
575    Since PetscOptionsInsert() is automatically called by PetscInitialize(),
576    the user does not typically need to call this routine. PetscOptionsInsert()
577    can be called several times, adding additional entries into the database.
578 
579    Options Database Keys:
580 +   -options_monitor <optional filename> - print options names and values as they are set
581 .   -options_file <filename> - read options from a file
582 
583    Level: advanced
584 
585    Concepts: options database^adding
586 
587 .seealso: PetscOptionsDestroy_Private(), PetscOptionsView(), PetscOptionsInsertString(), PetscOptionsInsertFile(),
588           PetscInitialize()
589 @*/
590 PetscErrorCode  PetscOptionsInsert(int *argc,char ***args,const char file[])
591 {
592   PetscErrorCode ierr;
593   PetscMPIInt    rank;
594   char           pfile[PETSC_MAX_PATH_LEN];
595   PetscBool      flag = PETSC_FALSE;
596 
597   PetscFunctionBegin;
598   if (!options) {
599     fprintf(stderr, "Options have not been enabled.\nYou might have forgotten to call PetscInitialize().\n");
600     MPI_Abort(MPI_COMM_WORLD, PETSC_ERR_SUP);
601   }
602   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
603 
604   options->argc     = (argc) ? *argc : 0;
605   options->args     = (args) ? *args : PETSC_NULL;
606 
607   if (file && file[0]) {
608     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,file,PETSC_TRUE);CHKERRQ(ierr);
609   }
610   /*
611      We want to be able to give -skip_petscrc on the command line, but need to parse it first.  Since the command line
612      should take precedence, we insert it twice.  It would be sufficient to just scan for -skip_petscrc.
613   */
614   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(*argc,*args);CHKERRQ(ierr);}
615   ierr = PetscOptionsGetBool(PETSC_NULL,"-skip_petscrc",&flag,PETSC_NULL);CHKERRQ(ierr);
616   if (!flag) {
617     ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
618     /* warning: assumes all processes have a home directory or none, but nothing in between */
619     if (pfile[0]) {
620       ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr);
621       ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,pfile,PETSC_FALSE);CHKERRQ(ierr);
622     }
623     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,".petscrc",PETSC_FALSE);CHKERRQ(ierr);
624     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,"petscrc",PETSC_FALSE);CHKERRQ(ierr);
625   }
626 
627   /* insert environmental options */
628   {
629     char   *eoptions = 0;
630     size_t len = 0;
631     if (!rank) {
632       eoptions = (char*)getenv("PETSC_OPTIONS");
633       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
634       ierr     = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
635     } else {
636       ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
637       if (len) {
638         ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr);
639       }
640     }
641     if (len) {
642       ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
643       if (rank) eoptions[len] = 0;
644       ierr = PetscOptionsInsertString(eoptions);CHKERRQ(ierr);
645       if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);}
646     }
647   }
648 
649 #if defined(PETSC_HAVE_YAML)
650   char yaml_file[PETSC_MAX_PATH_LEN];
651   PetscBool yaml_flg = PETSC_FALSE;
652   ierr = PetscOptionsGetString(PETSC_NULL,"-options_file_yaml",yaml_file,PETSC_MAX_PATH_LEN,&yaml_flg);CHKERRQ(ierr);
653   if (yaml_flg) ierr = PetscOptionsInsertFile_YAML(PETSC_COMM_WORLD,yaml_file,PETSC_TRUE);CHKERRQ(ierr);
654 #endif
655 
656   /* insert command line options again because they take precedence over arguments in petscrc/environment */
657   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(*argc,*args);CHKERRQ(ierr);}
658 
659   PetscFunctionReturn(0);
660 }
661 
662 #undef __FUNCT__
663 #define __FUNCT__ "PetscOptionsView"
664 /*@C
665    PetscOptionsView - Prints the options that have been loaded. This is
666    useful for debugging purposes.
667 
668    Logically Collective on PetscViewer
669 
670    Input Parameter:
671 .  viewer - must be an PETSCVIEWERASCII viewer
672 
673    Options Database Key:
674 .  -optionstable - Activates PetscOptionsView() within PetscFinalize()
675 
676    Level: advanced
677 
678    Concepts: options database^printing
679 
680 .seealso: PetscOptionsAllUsed()
681 @*/
682 PetscErrorCode  PetscOptionsView(PetscViewer viewer)
683 {
684   PetscErrorCode ierr;
685   PetscInt       i;
686   PetscBool      isascii;
687 
688   PetscFunctionBegin;
689   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
690   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
691   if (!isascii) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_SUP,"Only supports ASCII viewer");
692 
693   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
694   if (options->N) {
695     ierr = PetscViewerASCIIPrintf(viewer,"#PETSc Option Table entries:\n");CHKERRQ(ierr);
696   } else {
697     ierr = PetscViewerASCIIPrintf(viewer,"#No PETSc Option Table entries\n");CHKERRQ(ierr);
698   }
699   for (i=0; i<options->N; i++) {
700     if (options->values[i]) {
701       ierr = PetscViewerASCIIPrintf(viewer,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
702     } else {
703       ierr = PetscViewerASCIIPrintf(viewer,"-%s\n",options->names[i]);CHKERRQ(ierr);
704     }
705   }
706   if (options->N) {
707     ierr = PetscViewerASCIIPrintf(viewer,"#End of PETSc Option Table entries\n");CHKERRQ(ierr);
708   }
709   PetscFunctionReturn(0);
710 }
711 
712 #undef __FUNCT__
713 #define __FUNCT__ "PetscOptionsGetAll"
714 /*@C
715    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
716 
717    Not Collective
718 
719    Output Parameter:
720 .  copts - pointer where string pointer is stored
721 
722    Notes: the array and each entry in the array should be freed with PetscFree()
723 
724    Level: advanced
725 
726    Concepts: options database^listing
727 
728 .seealso: PetscOptionsAllUsed(), PetscOptionsView()
729 @*/
730 PetscErrorCode  PetscOptionsGetAll(char *copts[])
731 {
732   PetscErrorCode ierr;
733   PetscInt       i;
734   size_t         len = 1,lent = 0;
735   char           *coptions = PETSC_NULL;
736 
737   PetscFunctionBegin;
738   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
739 
740   /* count the length of the required string */
741   for (i=0; i<options->N; i++) {
742     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
743     len += 2 + lent;
744     if (options->values[i]) {
745       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
746       len += 1 + lent;
747     }
748   }
749   ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr);
750   coptions[0] = 0;
751   for (i=0; i<options->N; i++) {
752     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
753     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
754     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
755     if (options->values[i]) {
756       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
757       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
758     }
759   }
760   *copts = coptions;
761   PetscFunctionReturn(0);
762 }
763 
764 #undef __FUNCT__
765 #define __FUNCT__ "PetscOptionsPrefixPush"
766 /*@
767    PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow.
768 
769    Not Collective, but prefix will only be applied on calling ranks
770 
771    Input Parameter:
772 .  prefix - The string to append to the existing prefix
773 
774    Options Database Keys:
775  +   -prefix_push <some_prefix_> - push the given prefix
776  -   -prefix_pop - pop the last prefix
777 
778    Notes:
779    It is common to use this in conjunction with -options_file as in
780 
781  $ -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop
782 
783    where the files no longer require all options to be prefixed with -system2_.
784 
785 Level: advanced
786 
787 .seealso: PetscOptionsPrefixPop()
788 @*/
789 PetscErrorCode  PetscOptionsPrefixPush(const char prefix[])
790 {
791   PetscErrorCode ierr;
792   size_t n;
793   PetscInt start;
794   char buf[2048];
795   PetscBool  key;
796 
797   PetscFunctionBegin;
798   PetscValidCharPointer(prefix,1);
799   /* Want to check validity of the key using PetscOptionsValidKey(), which requires that the first character is a '-' */
800   buf[0] = '-';
801   ierr = PetscStrncpy(buf+1,prefix,sizeof buf - 1);
802   buf[sizeof buf - 1] = 0;
803   ierr = PetscOptionsValidKey(buf,&key);CHKERRQ(ierr);
804   if (!key) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Given prefix \"%s\" not valid (the first character must be a letter, do not include leading '-')",prefix);
805 
806   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
807   if (options->prefixind >= MAXPREFIXES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum depth of prefix stack %d exceeded, recompile \n src/sys/objects/options.c with larger value for MAXPREFIXES",MAXPREFIXES);
808   start = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
809   ierr = PetscStrlen(prefix,&n);CHKERRQ(ierr);
810   if (n+1 > sizeof(options->prefix)-start) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum prefix length %d exceeded",sizeof(options->prefix));
811   ierr = PetscMemcpy(options->prefix+start,prefix,n+1);CHKERRQ(ierr);
812   options->prefixstack[options->prefixind++] = start+n;
813   PetscFunctionReturn(0);
814 }
815 
816 #undef __FUNCT__
817 #define __FUNCT__ "PetscOptionsPrefixPop"
818 /*@
819    PetscOptionsPrefixPop - Remove the latest options prefix, see PetscOptionsPrefixPush() for details
820 
821    Not  Collective, but prefix will only be popped on calling ranks
822 
823    Level: advanced
824 
825 .seealso: PetscOptionsPrefixPush()
826 @*/
827 PetscErrorCode  PetscOptionsPrefixPop(void)
828 {
829   PetscInt offset;
830 
831   PetscFunctionBegin;
832   if (options->prefixind < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More prefixes popped than pushed");
833   options->prefixind--;
834   offset = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
835   options->prefix[offset] = 0;
836   PetscFunctionReturn(0);
837 }
838 
839 #undef __FUNCT__
840 #define __FUNCT__ "PetscOptionsClear"
841 /*@C
842     PetscOptionsClear - Removes all options form the database leaving it empty.
843 
844    Level: developer
845 
846 .seealso: PetscOptionsInsert()
847 @*/
848 PetscErrorCode  PetscOptionsClear(void)
849 {
850   PetscInt i;
851 
852   PetscFunctionBegin;
853   if (!options) PetscFunctionReturn(0);
854   for (i=0; i<options->N; i++) {
855     if (options->names[i])  free(options->names[i]);
856     if (options->values[i]) free(options->values[i]);
857   }
858   for (i=0; i<options->Naliases; i++) {
859     free(options->aliases1[i]);
860     free(options->aliases2[i]);
861   }
862   options->prefix[0] = 0;
863   options->prefixind = 0;
864   options->N        = 0;
865   options->Naliases = 0;
866   PetscFunctionReturn(0);
867 }
868 
869 #undef __FUNCT__
870 #define __FUNCT__ "PetscOptionsDestroy"
871 /*@C
872     PetscOptionsDestroy - Destroys the option database.
873 
874     Note:
875     Since PetscOptionsDestroy() is called by PetscFinalize(), the user
876     typically does not need to call this routine.
877 
878    Level: developer
879 
880 .seealso: PetscOptionsInsert()
881 @*/
882 PetscErrorCode  PetscOptionsDestroy(void)
883 {
884   PetscErrorCode ierr;
885 
886   PetscFunctionBegin;
887   if (!options) PetscFunctionReturn(0);
888   ierr = PetscOptionsClear();CHKERRQ(ierr);
889   free(options);
890   options = 0;
891   PetscFunctionReturn(0);
892 }
893 
894 #undef __FUNCT__
895 #define __FUNCT__ "PetscOptionsSetValue"
896 /*@C
897    PetscOptionsSetValue - Sets an option name-value pair in the options
898    database, overriding whatever is already present.
899 
900    Not collective, but setting values on certain processors could cause problems
901    for parallel objects looking for options.
902 
903    Input Parameters:
904 +  name - name of option, this SHOULD have the - prepended
905 -  value - the option value (not used for all options)
906 
907    Level: intermediate
908 
909    Note:
910    Only some options have values associated with them, such as
911    -ksp_rtol tol.  Other options stand alone, such as -ksp_monitor.
912 
913   Concepts: options database^adding option
914 
915 .seealso: PetscOptionsInsert()
916 @*/
917 PetscErrorCode  PetscOptionsSetValue(const char iname[],const char value[])
918 {
919   size_t         len;
920   PetscErrorCode ierr;
921   PetscInt       N,n,i;
922   char           **names;
923   char           fullname[2048];
924   const char     *name = iname;
925   PetscBool      gt,match;
926 
927   PetscFunctionBegin;
928   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
929 
930   /* this is so that -h and -hel\p are equivalent (p4 does not like -help)*/
931   ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr);
932   if (match) name = "-help";
933 
934   name++; /* skip starting hyphen */
935   if (options->prefixind > 0) {
936     ierr = PetscStrncpy(fullname,options->prefix,sizeof fullname);CHKERRQ(ierr);
937     ierr = PetscStrncat(fullname,name,sizeof fullname);CHKERRQ(ierr);
938     name = fullname;
939   }
940 
941   /* check against aliases */
942   N = options->Naliases;
943   for (i=0; i<N; i++) {
944     ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr);
945     if (match) {
946       name = options->aliases2[i];
947       break;
948     }
949   }
950 
951   N     = options->N;
952   n     = N;
953   names = options->names;
954 
955   for (i=0; i<N; i++) {
956     ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
957     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
958     if (match) {
959       if (options->values[i]) free(options->values[i]);
960       ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
961       if (len) {
962         options->values[i] = (char*)malloc((len+1)*sizeof(char));
963         ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr);
964       } else { options->values[i] = 0;}
965       PetscOptionsMonitor(name,value);
966       PetscFunctionReturn(0);
967     } else if (gt) {
968       n = i;
969       break;
970     }
971   }
972   if (N >= MAXOPTIONS) {
973     SETERRQ1(PETSC_COMM_SELF,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);
974   }
975   /* shift remaining values down 1 */
976   for (i=N; i>n; i--) {
977     options->names[i]  = options->names[i-1];
978     options->values[i] = options->values[i-1];
979     options->used[i]   = options->used[i-1];
980   }
981   /* insert new name and value */
982   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
983   options->names[n] = (char*)malloc((len+1)*sizeof(char));
984   ierr = PetscStrcpy(options->names[n],name);CHKERRQ(ierr);
985   ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
986   if (len) {
987     options->values[n] = (char*)malloc((len+1)*sizeof(char));
988     ierr = PetscStrcpy(options->values[n],value);CHKERRQ(ierr);
989   } else {options->values[n] = 0;}
990   options->used[n] = PETSC_FALSE;
991   options->N++;
992   PetscOptionsMonitor(name,value);
993   PetscFunctionReturn(0);
994 }
995 
996 #undef __FUNCT__
997 #define __FUNCT__ "PetscOptionsClearValue"
998 /*@C
999    PetscOptionsClearValue - Clears an option name-value pair in the options
1000    database, overriding whatever is already present.
1001 
1002    Not Collective, but setting values on certain processors could cause problems
1003    for parallel objects looking for options.
1004 
1005    Input Parameter:
1006 .  name - name of option, this SHOULD have the - prepended
1007 
1008    Level: intermediate
1009 
1010    Concepts: options database^removing option
1011 .seealso: PetscOptionsInsert()
1012 @*/
1013 PetscErrorCode  PetscOptionsClearValue(const char iname[])
1014 {
1015   PetscErrorCode ierr;
1016   PetscInt       N,n,i;
1017   char           **names,*name=(char*)iname;
1018   PetscBool      gt,match;
1019 
1020   PetscFunctionBegin;
1021   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1022   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
1023 
1024   name++;
1025 
1026   N     = options->N; n = 0;
1027   names = options->names;
1028 
1029   for (i=0; i<N; i++) {
1030     ierr  = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
1031     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
1032     if (match) {
1033       if (options->names[i])  free(options->names[i]);
1034       if (options->values[i]) free(options->values[i]);
1035       PetscOptionsMonitor(name,"");
1036       break;
1037     } else if (gt) {
1038       PetscFunctionReturn(0); /* it was not listed */
1039     }
1040     n++;
1041   }
1042   if (n == N) PetscFunctionReturn(0); /* it was not listed */
1043 
1044   /* shift remaining values down 1 */
1045   for (i=n; i<N-1; i++) {
1046     options->names[i]  = options->names[i+1];
1047     options->values[i] = options->values[i+1];
1048     options->used[i]   = options->used[i+1];
1049   }
1050   options->N--;
1051   PetscFunctionReturn(0);
1052 }
1053 
1054 #undef __FUNCT__
1055 #define __FUNCT__ "PetscOptionsSetAlias"
1056 /*@C
1057    PetscOptionsSetAlias - Makes a key and alias for another key
1058 
1059    Not Collective, but setting values on certain processors could cause problems
1060    for parallel objects looking for options.
1061 
1062    Input Parameters:
1063 +  inewname - the alias
1064 -  ioldname - the name that alias will refer to
1065 
1066    Level: advanced
1067 
1068 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1069            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1070           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1071           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1072           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1073           PetscOptionsList(), PetscOptionsEList()
1074 @*/
1075 PetscErrorCode  PetscOptionsSetAlias(const char inewname[],const char ioldname[])
1076 {
1077   PetscErrorCode ierr;
1078   PetscInt       n = options->Naliases;
1079   size_t         len;
1080   char           *newname = (char *)inewname,*oldname = (char*)ioldname;
1081 
1082   PetscFunctionBegin;
1083   if (newname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname);
1084   if (oldname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname);
1085   if (n >= MAXALIASES) {
1086     SETERRQ1(PETSC_COMM_SELF,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);
1087   }
1088 
1089   newname++; oldname++;
1090   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
1091   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
1092   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
1093   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
1094   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
1095   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
1096   options->Naliases++;
1097   PetscFunctionReturn(0);
1098 }
1099 
1100 #undef __FUNCT__
1101 #define __FUNCT__ "PetscOptionsFindPair_Private"
1102 static PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscBool  *flg)
1103 {
1104   PetscErrorCode ierr;
1105   PetscInt       i,N;
1106   size_t         len;
1107   char           **names,tmp[256];
1108   PetscBool      match;
1109 
1110   PetscFunctionBegin;
1111   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
1112   N = options->N;
1113   names = options->names;
1114 
1115   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1116 
1117   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1118   if (pre) {
1119     char *ptr = tmp;
1120     const char *namep = name;
1121     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
1122     if (name[1] == '-') {
1123       *ptr++ = '-';
1124       namep++;
1125     }
1126     ierr = PetscStrncpy(ptr,pre,tmp+sizeof tmp-ptr);CHKERRQ(ierr);
1127     tmp[sizeof tmp-1] = 0;
1128     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1129     ierr = PetscStrncat(tmp,namep+1,sizeof tmp-len-1);CHKERRQ(ierr);
1130   } else {
1131     ierr = PetscStrncpy(tmp,name+1,sizeof tmp);CHKERRQ(ierr);
1132     tmp[sizeof tmp-1] = 0;
1133   }
1134 #if defined(PETSC_USE_DEBUG)
1135   {
1136     PetscBool valid;
1137     char key[sizeof tmp+1] = "-";
1138     ierr = PetscMemcpy(key+1,tmp,sizeof tmp);CHKERRQ(ierr);
1139     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1140     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1141   }
1142 #endif
1143 
1144   /* slow search */
1145   *flg = PETSC_FALSE;
1146   for (i=0; i<N; i++) {
1147     ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr);
1148     if (match) {
1149        *value           = options->values[i];
1150        options->used[i] = PETSC_TRUE;
1151        *flg             = PETSC_TRUE;
1152        break;
1153      }
1154   }
1155   if (!*flg) {
1156     PetscInt j,cnt = 0,locs[16],loce[16];
1157     size_t   n;
1158     ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr);
1159     /* determine the location and number of all _%d_ in the key */
1160     for (i=0; i< (PetscInt)n; i++) {
1161       if (tmp[i] == '_') {
1162         for (j=i+1; j< (PetscInt)n; j++) {
1163           if (tmp[j] >= '0' && tmp[j] <= '9') continue;
1164           if (tmp[j] == '_' && j > i+1) { /* found a number */
1165             locs[cnt]   = i+1;
1166             loce[cnt++] = j+1;
1167           }
1168           break;
1169         }
1170       }
1171     }
1172     if (cnt) {
1173       char tmp2[256];
1174       for (i=0; i<cnt; i++) {
1175         ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr);
1176         ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr);
1177         ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr);
1178         ierr = PetscOptionsFindPair_Private(PETSC_NULL,tmp2,value,flg);CHKERRQ(ierr);
1179         if (*flg) break;
1180       }
1181     }
1182   }
1183   PetscFunctionReturn(0);
1184 }
1185 
1186 #undef __FUNCT__
1187 #define __FUNCT__ "PetscOptionsReject"
1188 /*@C
1189    PetscOptionsReject - Generates an error if a certain option is given.
1190 
1191    Not Collective, but setting values on certain processors could cause problems
1192    for parallel objects looking for options.
1193 
1194    Input Parameters:
1195 +  name - the option one is seeking
1196 -  mess - error message (may be PETSC_NULL)
1197 
1198    Level: advanced
1199 
1200    Concepts: options database^rejecting option
1201 
1202 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1203            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1204           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1205           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1206           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1207           PetscOptionsList(), PetscOptionsEList()
1208 @*/
1209 PetscErrorCode  PetscOptionsReject(const char name[],const char mess[])
1210 {
1211   PetscErrorCode ierr;
1212   PetscBool      flag = PETSC_FALSE;
1213 
1214   PetscFunctionBegin;
1215   ierr = PetscOptionsHasName(PETSC_NULL,name,&flag);CHKERRQ(ierr);
1216   if (flag) {
1217     if (mess) {
1218       SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess);
1219     } else {
1220       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name);
1221     }
1222   }
1223   PetscFunctionReturn(0);
1224 }
1225 
1226 #undef __FUNCT__
1227 #define __FUNCT__ "PetscOptionsHasName"
1228 /*@C
1229    PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even
1230                       its value is set to false.
1231 
1232    Not Collective
1233 
1234    Input Parameters:
1235 +  name - the option one is seeking
1236 -  pre - string to prepend to the name or PETSC_NULL
1237 
1238    Output Parameters:
1239 .  set - PETSC_TRUE if found else PETSC_FALSE.
1240 
1241    Level: beginner
1242 
1243    Concepts: options database^has option name
1244 
1245    Notes: Name cannot be simply -h
1246 
1247           In many cases you probably want to use PetscOptionsGetBool() instead of calling this, to allowing toggling values.
1248 
1249 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1250            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1251           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1252           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1253           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1254           PetscOptionsList(), PetscOptionsEList()
1255 @*/
1256 PetscErrorCode  PetscOptionsHasName(const char pre[],const char name[],PetscBool  *set)
1257 {
1258   char           *value;
1259   PetscErrorCode ierr;
1260   PetscBool      flag;
1261 
1262   PetscFunctionBegin;
1263   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1264   if (set) *set = flag;
1265   PetscFunctionReturn(0);
1266 }
1267 
1268 #undef __FUNCT__
1269 #define __FUNCT__ "PetscOptionsGetInt"
1270 /*@C
1271    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
1272 
1273    Not Collective
1274 
1275    Input Parameters:
1276 +  pre - the string to prepend to the name or PETSC_NULL
1277 -  name - the option one is seeking
1278 
1279    Output Parameter:
1280 +  ivalue - the integer value to return
1281 -  set - PETSC_TRUE if found, else PETSC_FALSE
1282 
1283    Level: beginner
1284 
1285    Concepts: options database^has int
1286 
1287 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1288           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1289           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1290           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1291           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1292           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1293           PetscOptionsList(), PetscOptionsEList()
1294 @*/
1295 PetscErrorCode  PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscBool  *set)
1296 {
1297   char           *value;
1298   PetscErrorCode ierr;
1299   PetscBool      flag;
1300 
1301   PetscFunctionBegin;
1302   PetscValidCharPointer(name,2);
1303   PetscValidIntPointer(ivalue,3);
1304   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1305   if (flag) {
1306     if (!value) {if (set) *set = PETSC_FALSE;}
1307     else {
1308       if (set) *set = PETSC_TRUE;
1309       ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr);
1310     }
1311   } else {
1312     if (set) *set = PETSC_FALSE;
1313   }
1314   PetscFunctionReturn(0);
1315 }
1316 
1317 #undef __FUNCT__
1318 #define __FUNCT__ "PetscOptionsGetEList"
1319 /*@C
1320      PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
1321 
1322    Not Collective
1323 
1324    Input Parameters:
1325 +  pre - the string to prepend to the name or PETSC_NULL
1326 .  opt - option name
1327 .  list - the possible choices
1328 .  ntext - number of choices
1329 
1330    Output Parameter:
1331 +  value - the index of the value to return (defaults to zero if the option name is given but choice is listed)
1332 -  set - PETSC_TRUE if found, else PETSC_FALSE
1333 
1334    Level: intermediate
1335 
1336    See PetscOptionsList() for when the choices are given in a PetscFList()
1337 
1338    Concepts: options database^list
1339 
1340 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1341            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1342           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1343           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1344           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1345           PetscOptionsList(), PetscOptionsEList()
1346 @*/
1347 PetscErrorCode  PetscOptionsGetEList(const char pre[],const char opt[],const char *const*list,PetscInt ntext,PetscInt *value,PetscBool  *set)
1348 {
1349   PetscErrorCode ierr;
1350   size_t         alen,len = 0;
1351   char           *svalue;
1352   PetscBool      aset,flg = PETSC_FALSE;
1353   PetscInt       i;
1354 
1355   PetscFunctionBegin;
1356   for ( i=0; i<ntext; i++) {
1357     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
1358     if (alen > len) len = alen;
1359   }
1360   len += 5; /* a little extra space for user mistypes */
1361   ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr);
1362   ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr);
1363   if (aset) {
1364     if (set) *set = PETSC_TRUE;
1365     for (i=0; i<ntext; i++) {
1366       ierr = PetscStrcasecmp(svalue,list[i],&flg);CHKERRQ(ierr);
1367       if (flg || !svalue[0]) {
1368         flg    = PETSC_TRUE;
1369         *value = i;
1370         break;
1371       }
1372     }
1373     if (!flg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre?pre:"",opt+1);
1374   } else if (set) {
1375     *set = PETSC_FALSE;
1376   }
1377   ierr = PetscFree(svalue);CHKERRQ(ierr);
1378   PetscFunctionReturn(0);
1379 }
1380 
1381 #undef __FUNCT__
1382 #define __FUNCT__ "PetscOptionsGetEnum"
1383 /*@C
1384    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
1385 
1386    Not Collective
1387 
1388    Input Parameters:
1389 +  pre - option prefix or PETSC_NULL
1390 .  opt - option name
1391 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
1392 -  defaultv - the default (current) value
1393 
1394    Output Parameter:
1395 +  value - the  value to return
1396 -  set - PETSC_TRUE if found, else PETSC_FALSE
1397 
1398    Level: beginner
1399 
1400    Concepts: options database
1401 
1402    Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd()
1403 
1404           list is usually something like PCASMTypes or some other predefined list of enum names
1405 
1406 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
1407           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1408           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1409           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1410           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1411           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1412           PetscOptionsList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
1413 @*/
1414 PetscErrorCode  PetscOptionsGetEnum(const char pre[],const char opt[],const char *const*list,PetscEnum *value,PetscBool  *set)
1415 {
1416   PetscErrorCode ierr;
1417   PetscInt       ntext = 0,tval;
1418   PetscBool      fset;
1419 
1420   PetscFunctionBegin;
1421   while (list[ntext++]) {
1422     if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
1423   }
1424   if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
1425   ntext -= 3;
1426   ierr = PetscOptionsGetEList(pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr);
1427   /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */
1428   if (fset) *value = (PetscEnum)tval;
1429   if (set) *set = fset;
1430   PetscFunctionReturn(0);
1431 }
1432 
1433 #undef __FUNCT__
1434 #define __FUNCT__ "PetscOptionsGetBool"
1435 /*@C
1436    PetscOptionsGetBool - Gets the Logical (true or false) value for a particular
1437             option in the database.
1438 
1439    Not Collective
1440 
1441    Input Parameters:
1442 +  pre - the string to prepend to the name or PETSC_NULL
1443 -  name - the option one is seeking
1444 
1445    Output Parameter:
1446 +  ivalue - the logical value to return
1447 -  set - PETSC_TRUE  if found, else PETSC_FALSE
1448 
1449    Level: beginner
1450 
1451    Notes:
1452        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1453        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1454 
1455        If the user does not supply the option (as either true or false) ivalue is NOT changed. Thus
1456      you NEED TO ALWAYS initialize the ivalue.
1457 
1458    Concepts: options database^has logical
1459 
1460 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1461           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsBool(),
1462           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1463           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1464           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1465           PetscOptionsList(), PetscOptionsEList()
1466 @*/
1467 PetscErrorCode  PetscOptionsGetBool(const char pre[],const char name[],PetscBool  *ivalue,PetscBool  *set)
1468 {
1469   char           *value;
1470   PetscBool      flag;
1471   PetscErrorCode ierr;
1472 
1473   PetscFunctionBegin;
1474   PetscValidCharPointer(name,2);
1475   PetscValidIntPointer(ivalue,3);
1476   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1477   if (flag) {
1478     if (set) *set = PETSC_TRUE;
1479     if (!value) {
1480       *ivalue = PETSC_TRUE;
1481     } else {
1482       ierr = PetscOptionsStringToBool(value, ivalue);CHKERRQ(ierr);
1483     }
1484   } else {
1485     if (set) *set = PETSC_FALSE;
1486   }
1487   PetscFunctionReturn(0);
1488 }
1489 
1490 #undef __FUNCT__
1491 #define __FUNCT__ "PetscOptionsGetBoolArray"
1492 /*@C
1493    PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular
1494    option in the database.  The values must be separated with commas with
1495    no intervening spaces.
1496 
1497    Not Collective
1498 
1499    Input Parameters:
1500 +  pre - string to prepend to each name or PETSC_NULL
1501 .  name - the option one is seeking
1502 -  nmax - maximum number of values to retrieve
1503 
1504    Output Parameter:
1505 +  dvalue - the integer values to return
1506 .  nmax - actual number of values retreived
1507 -  set - PETSC_TRUE if found, else PETSC_FALSE
1508 
1509    Level: beginner
1510 
1511    Concepts: options database^array of ints
1512 
1513    Notes:
1514        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1515        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1516 
1517 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1518            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1519           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1520           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1521           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1522           PetscOptionsList(), PetscOptionsEList()
1523 @*/
1524 PetscErrorCode  PetscOptionsGetBoolArray(const char pre[],const char name[],PetscBool  dvalue[],PetscInt *nmax,PetscBool  *set)
1525 {
1526   char           *value;
1527   PetscErrorCode ierr;
1528   PetscInt       n = 0;
1529   PetscBool      flag;
1530   PetscToken     token;
1531 
1532   PetscFunctionBegin;
1533   PetscValidCharPointer(name,2);
1534   PetscValidIntPointer(dvalue,3);
1535   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1536   if (!flag)  {if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1537   if (!value) {if (set) *set = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1538 
1539   if (set) *set = PETSC_TRUE;
1540 
1541   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1542   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1543   while (n < *nmax) {
1544     if (!value) break;
1545     ierr = PetscOptionsStringToBool(value,dvalue);CHKERRQ(ierr);
1546     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1547     dvalue++;
1548     n++;
1549   }
1550   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
1551   *nmax = n;
1552   PetscFunctionReturn(0);
1553 }
1554 
1555 #undef __FUNCT__
1556 #define __FUNCT__ "PetscOptionsGetReal"
1557 /*@C
1558    PetscOptionsGetReal - Gets the double precision value for a particular
1559    option in the database.
1560 
1561    Not Collective
1562 
1563    Input Parameters:
1564 +  pre - string to prepend to each name or PETSC_NULL
1565 -  name - the option one is seeking
1566 
1567    Output Parameter:
1568 +  dvalue - the double value to return
1569 -  set - PETSC_TRUE if found, PETSC_FALSE if not found
1570 
1571    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1572 
1573    Level: beginner
1574 
1575    Concepts: options database^has double
1576 
1577 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1578            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1579           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1580           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1581           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1582           PetscOptionsList(), PetscOptionsEList()
1583 @*/
1584 PetscErrorCode  PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscBool  *set)
1585 {
1586   char           *value;
1587   PetscErrorCode ierr;
1588   PetscBool      flag;
1589 
1590   PetscFunctionBegin;
1591   PetscValidCharPointer(name,2);
1592   PetscValidDoublePointer(dvalue,3);
1593   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1594   if (flag) {
1595     if (!value) {if (set) *set = PETSC_FALSE;}
1596     else        {if (set) *set = PETSC_TRUE; ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);}
1597   } else {
1598     if (set) *set = PETSC_FALSE;
1599   }
1600   PetscFunctionReturn(0);
1601 }
1602 
1603 #undef __FUNCT__
1604 #define __FUNCT__ "PetscOptionsGetScalar"
1605 /*@C
1606    PetscOptionsGetScalar - Gets the scalar value for a particular
1607    option in the database.
1608 
1609    Not Collective
1610 
1611    Input Parameters:
1612 +  pre - string to prepend to each name or PETSC_NULL
1613 -  name - the option one is seeking
1614 
1615    Output Parameter:
1616 +  dvalue - the double value to return
1617 -  set - PETSC_TRUE if found, else PETSC_FALSE
1618 
1619    Level: beginner
1620 
1621    Usage:
1622    A complex number 2+3i can be specified as 2,3 at the command line.
1623    or a number 2.0e-10 - 3.3e-20 i  can be specified as 2.0e-10,-3.3e-20
1624 
1625    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1626 
1627    Concepts: options database^has scalar
1628 
1629 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1630            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1631           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1632           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1633           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1634           PetscOptionsList(), PetscOptionsEList()
1635 @*/
1636 PetscErrorCode  PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscBool  *set)
1637 {
1638   char           *value;
1639   PetscBool      flag;
1640   PetscErrorCode ierr;
1641 
1642   PetscFunctionBegin;
1643   PetscValidCharPointer(name,2);
1644   PetscValidScalarPointer(dvalue,3);
1645   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1646   if (flag) {
1647     if (!value) {
1648       if (set) *set = PETSC_FALSE;
1649     } else {
1650 #if !defined(PETSC_USE_COMPLEX)
1651       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
1652 #else
1653       PetscReal  re=0.0,im=0.0;
1654       PetscToken token;
1655       char       *tvalue = 0;
1656 
1657       ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1658       ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1659       if (!tvalue) { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"unknown string specified\n"); }
1660       ierr    = PetscOptionsStringToReal(tvalue,&re);CHKERRQ(ierr);
1661       ierr    = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1662       if (!tvalue) { /* Unknown separator used. using only real value */
1663         *dvalue = re;
1664       } else {
1665         ierr    = PetscOptionsStringToReal(tvalue,&im);CHKERRQ(ierr);
1666         *dvalue = re + PETSC_i*im;
1667       }
1668       ierr    = PetscTokenDestroy(&token);CHKERRQ(ierr);
1669 #endif
1670       if (set) *set    = PETSC_TRUE;
1671     }
1672   } else { /* flag */
1673     if (set) *set = PETSC_FALSE;
1674   }
1675   PetscFunctionReturn(0);
1676 }
1677 
1678 #undef __FUNCT__
1679 #define __FUNCT__ "PetscOptionsGetRealArray"
1680 /*@C
1681    PetscOptionsGetRealArray - Gets an array of double precision values for a
1682    particular option in the database.  The values must be separated with
1683    commas with no intervening spaces.
1684 
1685    Not Collective
1686 
1687    Input Parameters:
1688 +  pre - string to prepend to each name or PETSC_NULL
1689 .  name - the option one is seeking
1690 -  nmax - maximum number of values to retrieve
1691 
1692    Output Parameters:
1693 +  dvalue - the double value to return
1694 .  nmax - actual number of values retreived
1695 -  set - PETSC_TRUE if found, else PETSC_FALSE
1696 
1697    Level: beginner
1698 
1699    Concepts: options database^array of doubles
1700 
1701 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1702            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
1703           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1704           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1705           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1706           PetscOptionsList(), PetscOptionsEList()
1707 @*/
1708 PetscErrorCode  PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscBool  *set)
1709 {
1710   char           *value;
1711   PetscErrorCode ierr;
1712   PetscInt       n = 0;
1713   PetscBool      flag;
1714   PetscToken     token;
1715 
1716   PetscFunctionBegin;
1717   PetscValidCharPointer(name,2);
1718   PetscValidDoublePointer(dvalue,3);
1719   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1720   if (!flag)  {if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1721   if (!value) {if (set) *set = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1722 
1723   if (set) *set = PETSC_TRUE;
1724 
1725   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1726   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1727   while (n < *nmax) {
1728     if (!value) break;
1729     ierr = PetscOptionsStringToReal(value,dvalue++);CHKERRQ(ierr);
1730     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1731     n++;
1732   }
1733   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
1734   *nmax = n;
1735   PetscFunctionReturn(0);
1736 }
1737 
1738 #undef __FUNCT__
1739 #define __FUNCT__ "PetscOptionsGetIntArray"
1740 /*@C
1741    PetscOptionsGetIntArray - Gets an array of integer values for a particular
1742    option in the database.  The values must be separated with commas with
1743    no intervening spaces.
1744 
1745    Not Collective
1746 
1747    Input Parameters:
1748 +  pre - string to prepend to each name or PETSC_NULL
1749 .  name - the option one is seeking
1750 -  nmax - maximum number of values to retrieve, can include d-D to indicate d,d+1,..,D-1
1751 
1752    Output Parameter:
1753 +  dvalue - the integer values to return
1754 .  nmax - actual number of values retreived
1755 -  set - PETSC_TRUE if found, else PETSC_FALSE
1756 
1757    Level: beginner
1758 
1759    Concepts: options database^array of ints
1760 
1761 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1762            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1763           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1764           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1765           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1766           PetscOptionsList(), PetscOptionsEList()
1767 @*/
1768 PetscErrorCode  PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscBool  *set)
1769 {
1770   char           *value;
1771   PetscErrorCode ierr;
1772   PetscInt       n = 0,i,start,end;
1773   size_t         len;
1774   PetscBool      flag,foundrange;
1775   PetscToken     token;
1776 
1777   PetscFunctionBegin;
1778   PetscValidCharPointer(name,2);
1779   PetscValidIntPointer(dvalue,3);
1780   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1781   if (!flag)  {if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1782   if (!value) {if (set) *set = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1783 
1784   if (set) *set = PETSC_TRUE;
1785 
1786   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1787   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1788   while (n < *nmax) {
1789     if (!value) break;
1790 
1791     /* look for form  d-D where d and D are integers */
1792     foundrange = PETSC_FALSE;
1793     ierr      = PetscStrlen(value,&len);CHKERRQ(ierr);
1794     if (value[0] == '-') i=2;
1795     else i=1;
1796     for (;i<(int)len; i++) {
1797       if (value[i] == '-') {
1798         if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
1799         value[i] = 0;
1800         ierr     = PetscOptionsStringToInt(value,&start);CHKERRQ(ierr);
1801         ierr     = PetscOptionsStringToInt(value+i+1,&end);CHKERRQ(ierr);
1802         if (end <= start) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1);
1803         if (n + end - start - 1 >= *nmax) SETERRQ4(PETSC_COMM_SELF,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);
1804         for (;start<end; start++) {
1805           *dvalue = start; dvalue++;n++;
1806         }
1807         foundrange = PETSC_TRUE;
1808         break;
1809       }
1810     }
1811     if (!foundrange) {
1812       ierr      = PetscOptionsStringToInt(value,dvalue);CHKERRQ(ierr);
1813       dvalue++;
1814       n++;
1815     }
1816     ierr      = PetscTokenFind(token,&value);CHKERRQ(ierr);
1817   }
1818   ierr      = PetscTokenDestroy(&token);CHKERRQ(ierr);
1819   *nmax = n;
1820   PetscFunctionReturn(0);
1821 }
1822 
1823 #undef __FUNCT__
1824 #define __FUNCT__ "PetscOptionsGetString"
1825 /*@C
1826    PetscOptionsGetString - Gets the string value for a particular option in
1827    the database.
1828 
1829    Not Collective
1830 
1831    Input Parameters:
1832 +  pre - string to prepend to name or PETSC_NULL
1833 .  name - the option one is seeking
1834 -  len - maximum length of the string including null termination
1835 
1836    Output Parameters:
1837 +  string - location to copy string
1838 -  set - PETSC_TRUE if found, else PETSC_FALSE
1839 
1840    Level: beginner
1841 
1842    Fortran Note:
1843    The Fortran interface is slightly different from the C/C++
1844    interface (len is not used).  Sample usage in Fortran follows
1845 .vb
1846       character *20 string
1847       integer   flg, ierr
1848       call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-s',string,flg,ierr)
1849 .ve
1850 
1851    Notes: if the option is given but no string is provided then an empty string is returned and set is given the value of PETSC_TRUE
1852 
1853    Concepts: options database^string
1854 
1855     Note:
1856       Even if the user provided no string (for example -optionname -someotheroption) the flag is set to PETSC_TRUE (and the string is fulled with nulls).
1857 
1858 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1859            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1860           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1861           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1862           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1863           PetscOptionsList(), PetscOptionsEList()
1864 @*/
1865 PetscErrorCode  PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscBool  *set)
1866 {
1867   char           *value;
1868   PetscErrorCode ierr;
1869   PetscBool      flag;
1870 
1871   PetscFunctionBegin;
1872   PetscValidCharPointer(name,2);
1873   PetscValidCharPointer(string,3);
1874   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1875   if (!flag) {
1876     if (set) *set = PETSC_FALSE;
1877   } else {
1878     if (set) *set = PETSC_TRUE;
1879     if (value) {
1880       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
1881       string[len-1] = 0;        /* Ensure that the string is NULL terminated */
1882     } else {
1883       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
1884     }
1885   }
1886   PetscFunctionReturn(0);
1887 }
1888 
1889 #undef __FUNCT__
1890 #define __FUNCT__ "PetscOptionsGetStringMatlab"
1891 char* PetscOptionsGetStringMatlab(const char pre[],const char name[])
1892 {
1893   char           *value;
1894   PetscErrorCode ierr;
1895   PetscBool      flag;
1896 
1897   PetscFunctionBegin;
1898   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);if (ierr) PetscFunctionReturn(0);
1899   if (flag) PetscFunctionReturn(value);
1900   else PetscFunctionReturn(0);
1901 }
1902 
1903 
1904 #undef __FUNCT__
1905 #define __FUNCT__ "PetscOptionsGetStringArray"
1906 /*@C
1907    PetscOptionsGetStringArray - Gets an array of string values for a particular
1908    option in the database. The values must be separated with commas with
1909    no intervening spaces.
1910 
1911    Not Collective
1912 
1913    Input Parameters:
1914 +  pre - string to prepend to name or PETSC_NULL
1915 .  name - the option one is seeking
1916 -  nmax - maximum number of strings
1917 
1918    Output Parameter:
1919 +  strings - location to copy strings
1920 -  set - PETSC_TRUE if found, else PETSC_FALSE
1921 
1922    Level: beginner
1923 
1924    Notes:
1925    The user should pass in an array of pointers to char, to hold all the
1926    strings returned by this function.
1927 
1928    The user is responsible for deallocating the strings that are
1929    returned. The Fortran interface for this routine is not supported.
1930 
1931    Contributed by Matthew Knepley.
1932 
1933    Concepts: options database^array of strings
1934 
1935 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1936            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1937           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1938           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1939           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1940           PetscOptionsList(), PetscOptionsEList()
1941 @*/
1942 PetscErrorCode  PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscBool  *set)
1943 {
1944   char           *value;
1945   PetscErrorCode ierr;
1946   PetscInt       n;
1947   PetscBool      flag;
1948   PetscToken     token;
1949 
1950   PetscFunctionBegin;
1951   PetscValidCharPointer(name,2);
1952   PetscValidPointer(strings,3);
1953   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1954   if (!flag)  {*nmax = 0; if (set) *set = PETSC_FALSE; PetscFunctionReturn(0);}
1955   if (!value) {*nmax = 0; if (set) *set = PETSC_FALSE;PetscFunctionReturn(0);}
1956   if (!*nmax) {if (set) *set = PETSC_FALSE;PetscFunctionReturn(0);}
1957   if (set) *set = PETSC_TRUE;
1958 
1959   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1960   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1961   n = 0;
1962   while (n < *nmax) {
1963     if (!value) break;
1964     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
1965     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1966     n++;
1967   }
1968   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
1969   *nmax = n;
1970   PetscFunctionReturn(0);
1971 }
1972 
1973 #undef __FUNCT__
1974 #define __FUNCT__ "PetscOptionsUsed"
1975 /*@C
1976    PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database
1977 
1978    Not Collective
1979 
1980    Input Parameter:
1981 .    option - string name of option
1982 
1983    Output Parameter:
1984 .   used - PETSC_TRUE if the option was used, otherwise false, including if option was not found in options database
1985 
1986    Level: advanced
1987 
1988 .seealso: PetscOptionsView(), PetscOptionsLeft(), PetscOptionsAllUsed()
1989 @*/
1990 PetscErrorCode  PetscOptionsUsed(const char *option,PetscBool *used)
1991 {
1992   PetscInt       i;
1993   PetscErrorCode ierr;
1994 
1995   PetscFunctionBegin;
1996   *used = PETSC_FALSE;
1997   for (i=0; i<options->N; i++) {
1998     ierr = PetscStrcmp(options->names[i],option,used);CHKERRQ(ierr);
1999     if (*used) {
2000       *used = options->used[i];
2001       break;
2002     }
2003   }
2004   PetscFunctionReturn(0);
2005 }
2006 
2007 #undef __FUNCT__
2008 #define __FUNCT__ "PetscOptionsAllUsed"
2009 /*@C
2010    PetscOptionsAllUsed - Returns a count of the number of options in the
2011    database that have never been selected.
2012 
2013    Not Collective
2014 
2015    Output Parameter:
2016 .   N - count of options not used
2017 
2018    Level: advanced
2019 
2020 .seealso: PetscOptionsView()
2021 @*/
2022 PetscErrorCode  PetscOptionsAllUsed(PetscInt *N)
2023 {
2024   PetscInt i,n = 0;
2025 
2026   PetscFunctionBegin;
2027   for (i=0; i<options->N; i++) {
2028     if (!options->used[i]) { n++; }
2029   }
2030   *N = n;
2031   PetscFunctionReturn(0);
2032 }
2033 
2034 #undef __FUNCT__
2035 #define __FUNCT__ "PetscOptionsLeft"
2036 /*@
2037     PetscOptionsLeft - Prints to screen any options that were set and never used.
2038 
2039   Not collective
2040 
2041    Options Database Key:
2042 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
2043 
2044   Level: advanced
2045 
2046 .seealso: PetscOptionsAllUsed()
2047 @*/
2048 PetscErrorCode  PetscOptionsLeft(void)
2049 {
2050   PetscErrorCode ierr;
2051   PetscInt       i;
2052 
2053   PetscFunctionBegin;
2054   for (i=0; i<options->N; i++) {
2055     if (!options->used[i]) {
2056       if (options->values[i]) {
2057         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
2058       } else {
2059         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s no value \n",options->names[i]);CHKERRQ(ierr);
2060       }
2061     }
2062   }
2063   PetscFunctionReturn(0);
2064 }
2065 
2066 
2067 #undef __FUNCT__
2068 #define __FUNCT__ "PetscOptionsCreate"
2069 /*
2070     PetscOptionsCreate - Creates the empty options database.
2071 
2072 */
2073 PetscErrorCode  PetscOptionsCreate(void)
2074 {
2075   PetscErrorCode ierr;
2076 
2077   PetscFunctionBegin;
2078   options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable));
2079   ierr    = PetscMemzero(options,sizeof(PetscOptionsTable));CHKERRQ(ierr);
2080   options->namegiven 		= PETSC_FALSE;
2081   options->N         		= 0;
2082   options->Naliases  		= 0;
2083   options->numbermonitors 	= 0;
2084 
2085   PetscOptionsObject.prefix = PETSC_NULL;
2086   PetscOptionsObject.title  = PETSC_NULL;
2087 
2088   PetscFunctionReturn(0);
2089 }
2090 
2091 #undef __FUNCT__
2092 #define __FUNCT__ "PetscOptionsSetFromOptions"
2093 /*@
2094    PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options.
2095 
2096    Collective on PETSC_COMM_WORLD
2097 
2098    Options Database Keys:
2099 +  -options_monitor <optional filename> - prints the names and values of all runtime options as they are set. The monitor functionality is not
2100                 available for options set through a file, environment variable, or on
2101                 the command line. Only options set after PetscInitialize completes will
2102                 be monitored.
2103 .  -options_monitor_cancel - cancel all options database monitors
2104 
2105    Notes:
2106    To see all options, run your program with the -help option or consult
2107    the <A href="../../docs/manual.pdf">users manual</A>..
2108 
2109    Level: intermediate
2110 
2111 .keywords: set, options, database
2112 @*/
2113 PetscErrorCode  PetscOptionsSetFromOptions(void)
2114 {
2115   PetscBool           flgc,flgm;
2116   PetscErrorCode      ierr;
2117   char                monfilename[PETSC_MAX_PATH_LEN];
2118   PetscViewer         monviewer;
2119 
2120   PetscFunctionBegin;
2121   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr);
2122     ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flgm);CHKERRQ(ierr);
2123     ierr = PetscOptionsBool("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",PETSC_FALSE,&flgc,PETSC_NULL);CHKERRQ(ierr);
2124   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2125   if (flgm) {
2126     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
2127     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
2128   }
2129   if (flgc) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
2130   PetscFunctionReturn(0);
2131 }
2132 
2133 
2134 #undef __FUNCT__
2135 #define __FUNCT__ "PetscOptionsMonitorDefault"
2136 /*@C
2137    PetscOptionsMonitorDefault - Print all options set value events.
2138 
2139    Logically Collective on PETSC_COMM_WORLD
2140 
2141    Input Parameters:
2142 +  name  - option name string
2143 .  value - option value string
2144 -  dummy - unused monitor context
2145 
2146    Level: intermediate
2147 
2148 .keywords: PetscOptions, default, monitor
2149 
2150 .seealso: PetscOptionsMonitorSet()
2151 @*/
2152 PetscErrorCode  PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
2153 {
2154   PetscErrorCode ierr;
2155   PetscViewer    viewer = (PetscViewer) dummy;
2156 
2157   PetscFunctionBegin;
2158   if (!viewer) {
2159     ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
2160   }
2161   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
2162   PetscFunctionReturn(0);
2163 }
2164 
2165 #undef __FUNCT__
2166 #define __FUNCT__ "PetscOptionsMonitorSet"
2167 /*@C
2168    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
2169    modified the PETSc options database.
2170 
2171    Not collective
2172 
2173    Input Parameters:
2174 +  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring
2175 .  mctx    - [optional] context for private data for the
2176              monitor routine (use PETSC_NULL if no context is desired)
2177 -  monitordestroy - [optional] routine that frees monitor context
2178           (may be PETSC_NULL)
2179 
2180    Calling Sequence of monitor:
2181 $     monitor (const char name[], const char value[], void *mctx)
2182 
2183 +  name - option name string
2184 .  value - option value string
2185 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
2186 
2187    Options Database Keys:
2188 +    -options_monitor    - sets PetscOptionsMonitorDefault()
2189 -    -options_monitor_cancel - cancels all monitors that have
2190                           been hardwired into a code by
2191                           calls to PetscOptionsMonitorSet(), but
2192                           does not cancel those set via
2193                           the options database.
2194 
2195    Notes:
2196    The default is to do nothing.  To print the name and value of options
2197    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
2198    with a null monitoring context.
2199 
2200    Several different monitoring routines may be set by calling
2201    PetscOptionsMonitorSet() multiple times; all will be called in the
2202    order in which they were set.
2203 
2204    Level: beginner
2205 
2206 .keywords: PetscOptions, set, monitor
2207 
2208 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
2209 @*/
2210 PetscErrorCode  PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
2211 {
2212   PetscFunctionBegin;
2213   if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
2214   options->monitor[options->numbermonitors]           = monitor;
2215   options->monitordestroy[options->numbermonitors]    = monitordestroy;
2216   options->monitorcontext[options->numbermonitors++]  = (void*)mctx;
2217   PetscFunctionReturn(0);
2218 }
2219 
2220 #undef __FUNCT__
2221 #define __FUNCT__ "PetscOptionsMonitorCancel"
2222 /*@
2223    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
2224 
2225    Not collective
2226 
2227    Options Database Key:
2228 .  -options_monitor_cancel - Cancels all monitors that have
2229     been hardwired into a code by calls to PetscOptionsMonitorSet(),
2230     but does not cancel those set via the options database.
2231 
2232    Level: intermediate
2233 
2234 .keywords: PetscOptions, set, monitor
2235 
2236 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
2237 @*/
2238 PetscErrorCode  PetscOptionsMonitorCancel(void)
2239 {
2240   PetscErrorCode ierr;
2241   PetscInt       i;
2242 
2243   PetscFunctionBegin;
2244   for (i=0; i<options->numbermonitors; i++) {
2245     if (options->monitordestroy[i]) {
2246       ierr = (*options->monitordestroy[i])(&options->monitorcontext[i]);CHKERRQ(ierr);
2247     }
2248   }
2249   options->numbermonitors = 0;
2250   PetscFunctionReturn(0);
2251 }
2252