commit ec7dfa9f0ee3daeb2dfef45f4703ca6e1399a978
parent bbd24afac544c4afc9772f126eec9c3ba2c602e4
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Wed, 10 Apr 2019 17:01:39 +0200
Allow modification of all simulation parameters through command line arguments
Diffstat:
M | Makefile | | | 18 | ++++++------------ |
M | main.c | | | 134 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
2 files changed, 136 insertions(+), 16 deletions(-)
diff --git a/Makefile b/Makefile
@@ -10,18 +10,12 @@ default: 1d_fd_simple_shear.png
$(CC) $(LDFLAGS) $(OBJ) -o $@
1d_fd_simple_shear.png: 1d_fd_simple_shear 1d_fd_simple_shear.gp
- sed -i 's/P_wall = .*;/P_wall = 10e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P10kPa.txt
- sed -i 's/P_wall = .*;/P_wall = 20e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P20kPa.txt
- sed -i 's/P_wall = .*;/P_wall = 40e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P40kPa.txt
- sed -i 's/P_wall = .*;/P_wall = 60e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P60kPa.txt
- sed -i 's/P_wall = .*;/P_wall = 80e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P80kPa.txt
- sed -i 's/P_wall = .*;/P_wall = 120e3;/' 1d_fd_simple_shear_damsgaard2013.h
- rm main.o && make $< && ./$< > $<_P120kPa.txt
+ ./$< -P 10e3 -N > $<_P10kPa.txt
+ ./$< -P 20e3 -N > $<_P20kPa.txt
+ ./$< -P 40e3 -N > $<_P40kPa.txt
+ ./$< -P 60e3 -N > $<_P60kPa.txt
+ ./$< -P 80e3 -N > $<_P80kPa.txt
+ ./$< -P 120e3 -N > $<_P120kPa.txt
gnuplot $<.gp > $@
.PHONY: watch
diff --git a/main.c b/main.c
@@ -1,17 +1,140 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
+#include <getopt.h>
#include "simulation.h"
-/* set up parameter values for this simulation */
+#define VERSION "0.1"
+#define PROGNAME "1d_fd_simple_shear"
+
+/* set default simulation parameter values */
#include "1d_fd_simple_shear_damsgaard2013.h"
-/* #include "1d_fd_simple_shear_henann_kamrin2016.h" */
+static void usage(void)
+{
+ printf("%s: %s [OPTIONS]\n"
+ "optional arguments:\n"
+ " -N, --normalize normalize output velocity\n"
+ " -G, --gravity VAL gravity magnitude [m/s^2]\n"
+ " -P, --pressure VAL normal stress [Pa]\n"
+ " -m, --stress-ratio VAL applied stress ratio [-]\n"
+ " -V, --velocity-bottom VAL base velocity at bottom [m/s]\n"
+ " -A, --nonlocal-amplitude VAL amplitude of nonlocality [-]\n"
+ " -b, --rate-dependence VAL rate dependence beyond yield [-]\n"
+ " -f, --friction-coefficient VAL grain friction coefficient [-]\n"
+ " -p, --porosity VAL porosity fraction [-]\n"
+ " -d, --grain-size VAL representative grain size [m]\n"
+ " -r, --density VAL grain material density [kg/m^3]\n"
+ " -n, --resolution VAL number of cells in domain [-]\n"
+ " -o, --origo VAL coordinate system origo [m]\n"
+ " -L, --length VAL domain length [m]\n"
+ " -v, --version show version information\n"
+ " -h, --help show this message\n"
+ , __func__, PROGNAME);
+}
-int main(int argc, char** argv) {
+static void version(void)
+{
+ printf("%s v%s\n"
+ "Licensed under the GNU Public License, v3+\n"
+ "written by Anders Damsgaard, anders@adamsgaard.dk\n"
+ "https://gitlab.com/admesg/1d_fd_simple_shear\n"
+ , PROGNAME, VERSION);
+}
+int main(int argc, char* argv[])
+{
+
+ /* load with default values */
struct simulation sim = init_sim();
+
+ int normalize = 0;
+
+ int opt;
+ const char* optstring = "hvNG:P:m:V:A:b:f:p:d:r:n:o:L:";
+ const struct option longopts[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'v'},
+ {"gravity", required_argument, NULL, 'G'},
+ {"pressure", required_argument, NULL, 'P'},
+ {"stress-ratio", required_argument, NULL, 'm'},
+ {"velocity-bottom", required_argument, NULL, 'V'},
+ {"nonlocal-amplitude", required_argument, NULL, 'A'},
+ {"rate-dependence", required_argument, NULL, 'b'},
+ {"friction-coefficient", required_argument, NULL, 'f'},
+ {"porosity", required_argument, NULL, 'p'},
+ {"grain-size", required_argument, NULL, 'd'},
+ {"density", required_argument, NULL, 'r'},
+ {"resolution", required_argument, NULL, 'n'},
+ {"origo", required_argument, NULL, 'o'},
+ {"length", required_argument, NULL, 'L'},
+ {NULL, 0, NULL, 0}
+ };
+
+ while ((opt = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
+ switch (opt) {
+ case -1: /* no more arguments */
+ case 0: /* long options toggles */
+ break;
+
+ case 'h':
+ usage();
+ return 0;
+ case 'v':
+ version();
+ return 0;
+ case 'N':
+ normalize = 1;
+ break;
+ case 'G':
+ sim.G = atof(optarg);
+ break;
+ case 'P':
+ sim.P_wall = atof(optarg);
+ break;
+ case 'm':
+ sim.mu_wall = atof(optarg);
+ break;
+ case 'V':
+ sim.v_x_bot = atof(optarg);
+ break;
+ case 'A':
+ sim.A = atof(optarg);
+ break;
+ case 'b':
+ sim.b = atof(optarg);
+ break;
+ case 'f':
+ sim.mu_s = atof(optarg);
+ break;
+ case 'p':
+ sim.phi = atof(optarg);
+ break;
+ case 'd':
+ sim.d = atof(optarg);
+ break;
+ case 'r':
+ sim.rho_s = atof(optarg);
+ break;
+ case 'n':
+ sim.nz = atoi(optarg);
+ break;
+ case 'o':
+ sim.origo_z = atof(optarg);
+ break;
+ case 'L':
+ sim.L_z = atof(optarg);
+ break;
+
+ default:
+ fprintf(stderr, "%s: invalid option -- %c\n", argv[0], opt);
+ fprintf(stderr, "Try `%s --help` for more information\n",
+ argv[0]);
+ return -2;
+ }
+ }
+
prepare_arrays(&sim);
init_pressure(&sim);
@@ -30,7 +153,10 @@ int main(int argc, char** argv) {
compute_shear_strain_rate_plastic(&sim);
compute_shear_velocity(&sim);
- print_arrays_2nd_normalized(sim.z, sim.v_x, sim.nz);
+ if (normalize)
+ print_arrays_2nd_normalized(sim.z, sim.v_x, sim.nz);
+ else
+ print_arrays(sim.z, sim.v_x, sim.nz);
free_arrays(&sim);
return 0;