#include #include #include #include #include #include #define neqs 4 eqf_t eqfs[] = {f1, f2, f3, f4}; void help(const char *program, int exit_code) { printf("Usage:\n%s --method euler|rk4 [--equations ] [--files ] [--dt ] [--interval ] [--init ]\n", program); puts( "\t--method, -m:\twhich method to use, euler's method or 4th order Runge-Kutta method. Default: euler\n" "\t--equations, -e:\tAmount of equations. Optional, default 4\n" "\t--files, -f:\tSpecifies output files for approximations. Default: f1.data, f2.data, ...\n" "\t--dt, -t:\tApproximation step. default 0.1\n" "\t--range, -r:\tInterval of an approximation. defaults to '0 40'\n" "\t--init, -i:\tInitial conditions for problem at the start of an interval. Default: '0.1 -0.1 0.1 -0.1' ...\n" "\t--help, -h:\tThis message\n\n" "Note: -e MUST come before -f and -i. If not done so, amount of files will be determined incorrectly" ); if (exit_code < 0) return; else exit(exit_code); } int main(int argc, char *argv[]) { i32 i, j, numeq = 4; f64 dt = 0.1, start = 0, end = 40; f64 * init = NULL; const char **files = NULL; void (*solver)(eqf_t*, f64*, const char**, usz, f64, f64, f64) = difeq_solve_euler; for (i = 1; i < argc; ++i) { if (!strcmp("-h", argv[i]) || !strcmp("--help", argv[i])) help(*argv, 0); if (!strcmp("-e", argv[i]) || !strcmp("--equations", argv[i])) numeq = atoi(argv[++i]); if (!strcmp("-t", argv[i]) || !strcmp("--dt", argv[i])) dt = atof(argv[++i]); if (!strcmp("-r", argv[i]) || !strcmp("--range", argv[i])) { start = atof(argv[++i]); end = atof(argv[++i]); } if (!strcmp("-i", argv[i]) || !strcmp("--init", argv[i])) { init = calloc(numeq, sizeof(f64)); for (j = 0; j < numeq; ++j) init[j] = atof(argv[++i]); } if (!strcmp("-f", argv[i]) || !strcmp("--files", argv[i])) { files = calloc(numeq, sizeof(char*)); for (j = 0; j < numeq; ++j) files[j] = argv[++i]; } if (!strcmp("-m", argv[i]) || !strcmp("--method", argv[i])) { ++i; if (!strcmp(argv[i], "euler")) { solver = difeq_solve_euler; continue; } if (!strcmp(argv[i], "rk4")) { solver = difeq_solve_RK; continue; } printf("\x1b[91mError:\x1b[0mNo such method \"%s\". Available methods: \"euler\"\"rk4\". see --help for details.\n", argv[i]); return EXIT_FAILURE; } } if (numeq != neqs) { printf("\x1b[91mError:\x1b[0mCan not solve more than 4 equations without changing source code of main."); return EXIT_FAILURE; } if (files == NULL) { files = calloc(4, sizeof(char*)); files[0] = "f1.data", files[1] = "f2.data", files[2] = "f3.data", files[3] = "f4.data"; } if (init == NULL) { init = calloc(4, sizeof (double)); init[0] = init[2] = 0.1, init[1] = init[3] = -0.1; } solver(eqfs, init, files, neqs, start, end, dt); puts("Done"); free(init); free(files); return EXIT_SUCCESS; }