123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #include "sys-defines.h"
- #include "ode.h"
- #include "extern.h"
- #include "getopt.h"
- #define ARG_NONE 0
- #define ARG_REQUIRED 1
- #define ARG_OPTIONAL 2
- struct option long_options[] =
- {
- {"input-file", ARG_REQUIRED, NULL, 'f'},
- {"precision", ARG_REQUIRED, NULL, 'p'},
-
- {"adams-moulton", ARG_OPTIONAL, NULL, 'A'},
- {"euler", ARG_OPTIONAL, NULL, 'E'},
- {"runge-kutta", ARG_OPTIONAL, NULL, 'R'},
-
- {"absolute-error-bound", ARG_REQUIRED, NULL, 'e'},
- {"step-size-bound", ARG_REQUIRED, NULL, 'h'},
- {"relative-error-bound", ARG_REQUIRED, NULL, 'r'},
- {"suppress-error-bound", ARG_NONE, NULL, 's'},
- {"title", ARG_NONE, NULL, 't'},
-
- {"version", ARG_NONE, NULL, 'V' << 8},
- {"help", ARG_NONE, NULL, 'h' << 8},
- {NULL, 0, 0, 0}
- };
- int hidden_options[] = { 0 };
- static void fatal (const char *s);
- static void
- fatal (const char *s)
- {
- fprintf (stderr, "%s: %s\n", progname, s);
- exit (EXIT_FAILURE);
- }
- int
- main (int argc, char *argv[])
- {
- int option;
- int opt_index;
- int errcnt = 0;
- bool show_version = false;
- bool show_usage = false;
- double local_tstep, local_hmax;
- FILE *infile = NULL;
- for ( ; ; )
- {
- option = getopt_long (argc, argv, "e:f:h:p:r:stA::E::R::V", long_options, &opt_index);
- if (option == 0)
- option = long_options[opt_index].val;
- switch (option)
- {
-
- case 's':
- sflag = true;
- break;
- case 't':
- tflag = true;
- if (!pflag)
- {
- prec = 6;
- fwd = 13;
- }
- break;
- case 'V' << 8:
- show_version = true;
- break;
- case 'h' << 8:
- show_usage = true;
- break;
-
- case 'f':
- filename = xstrdup (optarg);
- break;
- case 'p':
- pflag = true;
- if (sscanf (optarg, "%d", &prec) <= 0)
- fatal ("-p: bad argument");
- prec--;
- if (prec <= 0 || prec > 18)
- fatal ("-p: argument must be in the range 2..19");
- fwd = prec + 7;
- if (fwd < 9)
- fwd = 9;
- break;
-
- case 'A':
- algorithm = A_ADAMS_MOULTON;
- if (optind >= argc)
- break;
-
- if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
- break;
- tstep = local_tstep;
- optind++;
- conflag = true;
- break;
- case 'E':
- algorithm = A_EULER;
- conflag = true;
- tstep = 0.1;
- if (optind >= argc)
- break;
-
- if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
- break;
- tstep = local_tstep;
- optind++;
- break;
- case 'R':
- algorithm = A_RUNGE_KUTTA_FEHLBERG;
- if (optind >= argc)
- break;
-
- if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
- break;
- tstep = local_tstep;
- optind++;
- conflag = true;
- break;
-
- case 'h':
- if (sscanf (optarg, "%lf", &hmin) <= 0)
- fatal ("-h: bad argument");
- if (hmin < HMIN)
- fatal ("-h: value too small");
- if (optind >= argc)
- break;
-
- if (sscanf (argv [optind], "%lf", &local_hmax) <= 0)
- break;
- hmax = local_hmax;
- optind++;
- hflag = true;
- break;
- case 'r':
- rflag = true;
- if (sscanf (optarg, "%lf", &ssmax) <= 0)
- fatal ("-r: bad argument");
- if (ssmax < HMIN)
- fatal ("-r: max value too small");
- if (optind >= argc)
- break;
-
- if (sscanf (argv [optind], "%lf", &ssmin) <= 0)
- {
- ssmin = ssmax * SCALE;
- break;
- }
- optind++;
- break;
- case 'e':
- eflag = true;
- if (sscanf (optarg, "%lf", &abmax) <= 0)
- fatal ("-e: bad argument");
- if (abmax < HMIN)
- fatal ("-e: max value too small");
- if (optind >= argc)
-
- break;
- if (sscanf (argv [optind], "%lf", &abmin) <= 0)
- {
- abmin = abmax * SCALE;
- break;
- }
- optind++;
- break;
-
- default:
- errcnt++;
- break;
- }
- if ((option == EOF))
- {
- errcnt--;
- break;
- }
- }
- if (optind < argc)
- {
- fprintf (stderr, "%s: there are too many arguments\n", progname);
- errcnt++;
- }
-
- if (errcnt > 0)
- {
- fprintf (stderr, "Try `%s --help' for more information\n", progname);
- return EXIT_FAILURE;
- }
- if (show_version)
- {
- display_version (progname, written, copyright);
- return EXIT_SUCCESS;
- }
- if (show_usage)
- {
- display_usage (progname, hidden_options, NULL, 0);
- return EXIT_SUCCESS;
- }
-
- if (algorithm == A_EULER && (eflag || rflag))
- fatal ("-E [Euler] illegal with -e or -r");
-
- if (filename != NULL)
- {
- infile = fopen (filename, "r");
- if (infile == NULL)
- {
- fprintf (stderr, "%s: %s: %s\n", progname, filename, strerror(errno));
- return EXIT_FAILURE;
- }
- yyin = infile;
-
- }
- else
- {
- yyin = stdin;
- filename = "";
- }
-
- yyparse();
- return EXIT_SUCCESS;
- }
|