From: W. Trevor King Date: Mon, 11 Feb 2013 17:02:40 +0000 (-0500) Subject: one_gaussian_bump.c: Incorperate safe-strto-example's parsing X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=356bfaf58dd1defa8d56c2661dce7d3afc2ed906;p=assignment-template.git one_gaussian_bump.c: Incorperate safe-strto-example's parsing --- diff --git a/config.h b/config.h index 6501584..bff742c 100644 --- a/config.h +++ b/config.h @@ -20,6 +20,6 @@ along with this program. If not, see . #ifndef _CONFIG_H_ #define _CONFIG_H_ -#define HAVE_COMPLEX /* enable safe_strtoc() */ +//#define HAVE_COMPLEX /* enable safe_strtoc() */ #endif /* _CONFIG_H_ */ diff --git a/one_gaussian_bump.c b/one_gaussian_bump.c index 92b7b7b..4adb80a 100644 --- a/one_gaussian_bump.c +++ b/one_gaussian_bump.c @@ -17,11 +17,29 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include /* for malloc(), free */ -#include /* for printf() */ -#include /* for assert() */ -#include /* for exp() */ - +#include /* for exit(), EXIT_*, malloc(), free() */ +#include /* for stderr, *printf() */ +#include /* for getopt_long() */ +#include /* for assert() */ +#include /* for exp(), sqrt() */ +#include "config.h" /* enabling safe_strtoc() */ +#include "safe-strto.h" /* for safe_strtol() and safe_strtoc() */ + +static struct option long_options[] = { + {"max-time", required_argument, 0, 't' }, + {"time-step", required_argument, 0, 'd' }, + {"x0", required_argument, 0, 'x' }, + {"y0", required_argument, 0, 'y' }, + {"vx0", required_argument, 0, 'X' }, + {"vx0-energy", required_argument, 0, 'E' }, + {"vy0", required_argument, 0, 'Y' }, + {"mass", required_argument, 0, 'm' }, + {"bump-magnitude", required_argument, 0, 'k' }, + {"bump-width", required_argument, 0, 'b' }, + {} +}; + +static char *short_options = "t:d:x:y:X:E:Y:m:k:b:"; /* dynamic system parameters */ typedef struct state_struct { @@ -106,11 +124,61 @@ void print_state(state_t *state) printf("\n"); } -int main() { - double t_max = 1; - double dt = 0.1; +void parse_args(int argc, char **argv, double *dt, double *t_max, + state_t *state, system_t *system) +{ + int ch; + gaussian_state_t *gstate = (gaussian_state_t *)system->dx_dt_state; + + while (1) { + ch = getopt_long(argc, argv, short_options, long_options, NULL); + if (ch == -1) + break; + switch(ch) { + case 't': + *t_max = safe_strtod(optarg, "max-time"); + break; + case 'd': + *dt = safe_strtod(optarg, "time-step"); + break; + case 'x': + state->x[0] = safe_strtod(optarg, "x0"); + break; + case 'y': + state->x[1] = safe_strtod(optarg, "y0"); + break; + case 'X': + state->x[2] = safe_strtod(optarg, "vx0"); + break; + case 'E': + state->x[2] = -sqrt(2*safe_strtod(optarg, "vx0-energy")/system->m); + break; + case 'Y': + state->x[3] = safe_strtod(optarg, "vy0"); + break; + case 'm': + system->m = safe_strtod(optarg, "mass"); + break; + case 'k': + gstate->k = safe_strtod(optarg, "bump-magnitude"); + break; + case 'b': + gstate->b = safe_strtod(optarg, "bump-width"); + break; + case '?': + exit(EXIT_FAILURE); + default: + printf("?? getopt returned character code 0%o ??\n", ch); + } + } + return; +} + +int main(int argc, char **argv) { + double t_max = 7; + double dt = 0.001; state_t state = {}; - double x[4] = {}; + double x[4] = {3.5, 0, 0, 0}; system_t system = {}; gaussian_state_t gaussian = {}; @@ -119,8 +187,11 @@ int main() { system.m = 1; system.dx_dt_fn = &single_gaussian_dx_dt; system.dx_dt_state = (void *)&gaussian; - gaussian.k = 1; + gaussian.k = -5; gaussian.b = 1; + state.x[2] = -sqrt(2*0.5/system.m); /* v = -sqrt(2E/m), with E = 0.5 */ + + parse_args(argc, argv, &dt, &t_max, &state, &system); print_state(&state); while (state.t < t_max) { diff --git a/safe-strto-example.c b/safe-strto-example.c deleted file mode 100644 index 0c1d4e9..0000000 --- a/safe-strto-example.c +++ /dev/null @@ -1,93 +0,0 @@ -/* -Demonstrate argument parsing with safe-strto.h - -Copyright (C) 2013 W. Trevor King - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include /* for exit(), EXIT_* */ -#include /* for stderr, *printf() */ -#include /* for getopt_long() */ -#include /* for the complex type qualifier */ -#include "config.h" /* enabling safe_strtoc() */ -#include "safe-strto.h" /* for safe_strtol() and safe_strtoc() */ - -static struct option long_options[] = { - {"my-long", required_argument, 0, 'l' }, - {"my-double", required_argument, 0, 'd' }, -#ifdef HAVE_COMPLEX - {"my-complex", required_argument, 0, 'c' }, -#endif /* HAVE_COMPLEX */ - {} -}; - -#ifdef HAVE_COMPLEX -static char *short_options = "l:d:c:"; -#else -static char *short_options = "l:d:"; -#endif /* HAVE_COMPLEX */ - -#ifdef HAVE_COMPLEX -void parse_args(int argc, char **argv, long *my_long, double *my_double, - double complex *my_complex) -#else -void parse_args(int argc, char **argv, long *my_long, double *my_double) -#endif /* HAVE_COMPLEX */ -{ - int ch; - - while (1) { - ch = getopt_long(argc, argv, short_options, long_options, NULL); - if (ch == -1) - break; - switch(ch) { - case 'l': - *my_long = safe_strtol(optarg, 10, "my-int"); - break; - case 'd': - *my_double = safe_strtod(optarg, "my-double"); - break; -#ifdef HAVE_COMPLEX - case 'c': - *my_complex = safe_strtoc(optarg, "my-complex"); - break; -#endif /* HAVE_COMPLEX */ - case '?': - exit(EXIT_FAILURE); - default: - printf("?? getopt returned character code 0%o ??\n", ch); - } - } - return; -} - -int main(int argc, char **argv) { - long int my_long = 0; - double my_double = 0; -#ifdef HAVE_COMPLEX - double complex my_complex = 0; - - parse_args(argc, argv, &my_long, &my_double, &my_complex); -#else - parse_args(argc, argv, &my_long, &my_double); -#endif /* HAVE_COMPLEX */ - printf("my_long: %ld\n", my_long); - printf("my_double: %g\n", my_double); -#ifdef HAVE_COMPLEX - printf("my_complex: %g + %gi\n", creal(my_complex), cimag(my_complex)); -#endif /* HAVE_COMPLEX */ - - return EXIT_SUCCESS; -}