one_gaussian_bump.c: Incorperate safe-strto-example's parsing
authorW. Trevor King <wking@tremily.us>
Mon, 11 Feb 2013 17:02:40 +0000 (12:02 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 12 Feb 2013 18:17:11 +0000 (13:17 -0500)
config.h
one_gaussian_bump.c
safe-strto-example.c [deleted file]

index 6501584bb15e224b1ce3d56ffb35a9104778e2ab..bff742c3ef04feb708e92e4465a6df9ad5c4c9a9 100644 (file)
--- a/config.h
+++ b/config.h
@@ -20,6 +20,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef _CONFIG_H_
 #define _CONFIG_H_
 
-#define HAVE_COMPLEX  /* enable safe_strtoc() */
+//#define HAVE_COMPLEX  /* enable safe_strtoc() */
 
 #endif  /* _CONFIG_H_ */
index 92b7b7b0dcf8d17bf322d2c1609e7fba10fed9b9..4adb80afc60f1776672686f668120509766ddf9a 100644 (file)
@@ -17,11 +17,29 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <stdlib.h>  /* for malloc(), free */
-#include <stdio.h>   /* for printf() */
-#include <assert.h>  /* for assert() */
-#include <math.h>    /* for exp() */
-
+#include <stdlib.h>       /* for exit(), EXIT_*, malloc(), free() */
+#include <stdio.h>        /* for stderr, *printf() */
+#include <getopt.h>       /* for getopt_long() */
+#include <assert.h>       /* for assert() */
+#include <math.h>         /* 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 (file)
index 0c1d4e9..0000000
+++ /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 <http://www.gnu.org/licenses/>.
-*/
-
-#include <stdlib.h>       /* for exit(), EXIT_* */
-#include <stdio.h>        /* for stderr, *printf() */
-#include <getopt.h>       /* for getopt_long() */
-#include <complex.h>      /* 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;
-}