Commit 3b74dfeb authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass4 taddeus: Inserted print statements and cleaned up code.

parent adcfb495
...@@ -3,20 +3,17 @@ ...@@ -3,20 +3,17 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#define DEBUG double **create_y(int steps) {
double **y = (double **)malloc(steps * sizeof(double *));
/*
*
*/
double **init_sinus(double l, double dx, double n, int *steps) {
double **y;
int i; int i;
*steps = (int)ceil(l / dx); if( y == NULL ) {
y = (double**)malloc(3 * *steps * sizeof(double)); puts("Error: could not allocate sufficient memory.");
exit(EXIT_FAILURE);
}
for( i = 0; i < *steps; i++ ) for( i = 0; i < steps; i++ )
y[i][0] = sin(n * M_PI * i * dx / l); y[i] = (double *)malloc(3 * sizeof(double));
return y; return y;
} }
...@@ -24,69 +21,138 @@ double **init_sinus(double l, double dx, double n, int *steps) { ...@@ -24,69 +21,138 @@ double **init_sinus(double l, double dx, double n, int *steps) {
/* /*
* *
*/ */
double **init_plucked(double l, double dx, double xp, int *steps) { void init_sinus(double **y, int steps, double l, double dx, double n) {
double **y, x; for( int i = 0; i < steps; i++ )
int i; y[i][0] = sin(n * M_PI * i * dx / l);
*steps = (int)ceil(l / dx); // The string is attached at both ends
y = (double**)malloc(3 * *steps * sizeof(double)); y[0][2] = y[0][1] = y[0][0];
y[steps - 1][2] = y[steps - 1][1] = y[steps - 1][0];
}
/*
*
*/
void init_plucked(double **y, int steps, double l, double dx, double xp) {
double x;
for( i = 0; i < *steps; i++ ) { for( int i = 0; i < steps; i++ ) {
x = i * dx; x = i * dx;
y[i][0] = x < xp ? x / xp : (l - x) / (l - xp); y[i][0] = x < xp ? x / xp : (l - x) / (l - xp);
} }
// The string is attached at both ends // The string is attached at both ends
y[0][2] = y[0][1] = y[0][0]; y[0][2] = y[0][1] = y[0][0];
y[*steps - 1][2] = y[*steps - 1][1] = y[*steps - 1][0]; y[steps - 1][2] = y[steps - 1][1] = y[steps - 1][0];
return y;
} }
void iterate(double **y, int steps, double dx, double T) { #define DEBUG
#ifdef DEBUG
#define PRINT_FORMAT "%10f" // More readable print format (outlined)
#else
#define PRINT_FORMAT "%f" // Raw data, aplittable by space character
#endif
void iterate(double **y, int string_steps, double dx, int time_steps,
double dt, double theta, int verbose_level) {
double x; double x;
int i, prev = 0, current = 1, next = 2; int i, t, prev = 0, current = 1, next = 2;
// Calculate the position over the entire string at time dt using the // Calculate the position over the entire string at time dt using the
// position at t = 0 and the information that y(x, -dt) == y(x, dt) // position at t = 0 and the information that y(x, -dt) == y(x, dt)
for( i = 2; i < steps; i++ ) { for( i = 1; i < string_steps - 1; i++ ) {
x = i * dx; x = i * dx;
y[i][1] = y[i][0] + .5 * T * T * (y[i-1][0] - 2 * y[i][0] + y[i-1][0]); y[i][1] = y[i][0] + .5 * theta * theta * (y[i-1][0] - 2 * y[i][0]
+ y[i+1][0]);
}
// Print init states
if( verbose_level ) {
for( t = 0; t < 2; t++ ) {
printf(PRINT_FORMAT, t * dt);
for( i = 0; i < string_steps; i++ )
printf(" " PRINT_FORMAT, y[i][t]);
puts("");
}
} }
// Iterate over the length of the string // Iterate over the length of the string
for( i = 1; i < steps - 1; i++ ) { for( t = 0; t < time_steps; t++ ) {
y[i][next] = 2 * y[i][current] - y[i][prev] if( verbose_level )
+ T * T * (y[i-1][current] - 2 * y[i][current] + y[i+1][current]); printf(PRINT_FORMAT " " PRINT_FORMAT, t * dt, y[0][next]);
for( i = 1; i < string_steps - 1; i++ ) {
y[i][next] = 2 * y[i][current] - y[i][prev] + theta * theta
* (y[i-1][current] - 2 * y[i][current] + y[i+1][current]);
if( verbose_level )
printf(" " PRINT_FORMAT, y[i][next]);
}
if( verbose_level )
printf(" " PRINT_FORMAT "\n", y[string_steps - 1][next]);
prev = current; prev = current;
current = next; current = next;
next = (next + 1) % 3; next = (next + 1) % 3;
} }
// Free allocated memory
for( i = 0; i < string_steps; i++ )
free(y[i]);
free(y); free(y);
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int steps; int steps, verbose_level = 1;
double **(*method)(double, double, double, int*); void (*init_method)(double **, int, double, double, double);
double **y, t, dt, l, dx, theta, constant;
if( argc < 5 ) {
printf("Usage: %s INIT_METHOD L DX T N|XP\n", argv[0]); /*
* Argv:
* 0: ./seq
* 1: sinus/plucked
* 2: t
* 3: dt
* 4: l
* 5: dx
* 6: theta
* 7: n/xp (init constant)
* 8: verbose level
*/
if( argc < 8 ) {
printf("Usage: %s METHOD TIME DT LENGTH DX THETA N|XP [ VERBOSE_LEVEL ]\n",
argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if( !strcmp(argv[1], "sinus") ) { if( !strcmp(argv[1], "sinus") ) {
method = init_sinus; init_method = init_sinus;
} else if( !strcmp(argv[1], "plucked") ) { } else if( !strcmp(argv[1], "plucked") ) {
method = init_plucked; init_method = init_plucked;
} else { } else {
printf("Unknown method '%s'.\n", argv[1]); printf("Unknown initialization method '%s'.\n", argv[1]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
iterate((*method)(atof(argv[2]), atof(argv[3]), atof(argv[5]), &steps), t = atof(argv[2]);
steps, atof(argv[3]), atof(argv[4])); dt = atof(argv[3]);
l = atof(argv[4]);
dx = atof(argv[5]);
theta = atof(argv[6]);
constant = atof(argv[7]);
if( argc == 9 )
verbose_level = atoi(argv[8]);
y = create_y(steps = (int)ceil(l / dx));
(*init_method)(y, steps, l, dx, constant);
iterate(y, steps, dx, (int)ceil(t / dt), dt, theta, verbose_level);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment