Commit 36d87e81 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass4 taddeus: Added plot file, source cleanup.

parent 3b74dfeb
#!/usr/bin/env python
from sys import argv, stdin
from pylab import figure, plot, subplot, show, savefig, legend
from time import sleep
# Collect data
data = {}
for line in stdin.readlines():
s = line.split(' ')
data[float(s[0])] = [map(float, s[1::2]), map(float, s[2::2])]
# Plot and optionally save data
figure(len(data))
i = 1
for t in data:
subplot(2, 5, i)
plot(data[t][0], data[t][1], 'x-', label='t = %.3f' % t)
#legend()
i += 1
if len(argv) == 2:
savefig(argv[1])
show()
...@@ -46,16 +46,11 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) { ...@@ -46,16 +46,11 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) {
y[steps - 1][2] = y[steps - 1][1] = y[steps - 1][0]; y[steps - 1][2] = y[steps - 1][1] = y[steps - 1][0];
} }
#define DEBUG #define VERBOSE
#define PRINT_FORMAT "%e"
#ifdef DEBUG void iterate(double **y, int string_steps, double dx, int time_steps, double dt,
#define PRINT_FORMAT "%10f" // More readable print format (outlined) double theta) {
#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, t, prev = 0, current = 1, next = 2; int i, t, prev = 0, current = 1, next = 2;
...@@ -68,32 +63,37 @@ void iterate(double **y, int string_steps, double dx, int time_steps, ...@@ -68,32 +63,37 @@ void iterate(double **y, int string_steps, double dx, int time_steps,
} }
// Print init states // Print init states
if( verbose_level ) { #ifdef VERBOSE
for( t = 0; t < 2; t++ ) { for( t = 0; t < 2; t++ ) {
printf(PRINT_FORMAT, t * dt); printf(PRINT_FORMAT, t * dt);
for( i = 0; i < string_steps; i++ ) for( i = 0; i < string_steps; i++ )
printf(" " PRINT_FORMAT, y[i][t]); printf(" " PRINT_FORMAT " " PRINT_FORMAT, i * dx, y[i][t]);
puts(""); puts("");
} }
} #endif
// Iterate over the length of the string // Iterate over the length of the string
for( t = 0; t < time_steps; t++ ) { for( t = 2; t < time_steps; t++ ) {
if( verbose_level ) #ifdef VERBOSE
printf(PRINT_FORMAT " " PRINT_FORMAT, t * dt, y[0][next]); printf(PRINT_FORMAT " " PRINT_FORMAT " " PRINT_FORMAT, t * dt, .0,
y[0][next]);
#endif
for( i = 1; i < string_steps - 1; i++ ) { for( i = 1; i < string_steps - 1; i++ ) {
y[i][next] = 2 * y[i][current] - y[i][prev] + theta * theta y[i][next] = 2 * y[i][current] - y[i][prev] + theta * theta
* (y[i-1][current] - 2 * y[i][current] + y[i+1][current]); * (y[i-1][current] - 2 * y[i][current] + y[i+1][current]);
if( verbose_level ) #ifdef VERBOSE
printf(" " PRINT_FORMAT, y[i][next]); printf(" " PRINT_FORMAT " " PRINT_FORMAT, i * dx, y[i][next]);
#endif
} }
if( verbose_level ) #ifdef VERBOSE
printf(" " PRINT_FORMAT "\n", y[string_steps - 1][next]); printf(" " PRINT_FORMAT " " PRINT_FORMAT "\n", (string_steps - 1) * dx,
y[string_steps - 1][next]);
#endif
prev = current; prev = current;
current = next; current = next;
...@@ -108,25 +108,12 @@ void iterate(double **y, int string_steps, double dx, int time_steps, ...@@ -108,25 +108,12 @@ void iterate(double **y, int string_steps, double dx, int time_steps,
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int steps, verbose_level = 1; int steps;
void (*init_method)(double **, int, double, double, double); void (*init_method)(double **, int, double, double, double);
double **y, t, dt, l, dx, theta, constant; double **y, t, dt, l, dx, theta, constant;
/*
* 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 ) { if( argc < 8 ) {
printf("Usage: %s METHOD TIME DT LENGTH DX THETA N|XP [ VERBOSE_LEVEL ]\n", printf("Usage: %s METHOD TIME DT LENGTH DX THETA N|XP\n", argv[0]);
argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -146,13 +133,10 @@ int main(int argc, const char **argv) { ...@@ -146,13 +133,10 @@ int main(int argc, const char **argv) {
theta = atof(argv[6]); theta = atof(argv[6]);
constant = atof(argv[7]); constant = atof(argv[7]);
if( argc == 9 )
verbose_level = atoi(argv[8]);
y = create_y(steps = (int)ceil(l / dx)); y = create_y(steps = (int)ceil(l / dx));
(*init_method)(y, steps, l, dx, constant); (*init_method)(y, steps, l, dx, constant);
iterate(y, steps, dx, (int)ceil(t / dt), dt, theta, verbose_level); iterate(y, steps, dx, (int)ceil(t / dt), dt, theta);
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