Commit 2ea1c77f authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass4 taddeus: Worked on sequential program.

parent 36d87e81
...@@ -4,19 +4,19 @@ from pylab import figure, plot, subplot, show, savefig, legend ...@@ -4,19 +4,19 @@ from pylab import figure, plot, subplot, show, savefig, legend
from time import sleep from time import sleep
# Collect data # Collect data
data = {} t = []
y = []
for line in stdin.readlines(): for line in stdin.readlines():
s = line.split(' ') s = line.split(' ')
data[float(s[0])] = [map(float, s[1::2]), map(float, s[2::2])] t.append(float(s[0]))
y.append((map(float, s[1::2]), map(float, s[2::2])))
# Plot and optionally save data # Plot and optionally save data
figure(len(data)) figure(len(t))
i = 1 for i in range(len(t)):
for t in data: subplot(2, 5, i + 1)
subplot(2, 5, i) plot(y[i][0], y[i][1], 'x-', label='t = %.3f' % t[i])
plot(data[t][0], data[t][1], 'x-', label='t = %.3f' % t)
#legend() #legend()
i += 1
if len(argv) == 2: if len(argv) == 2:
savefig(argv[1]) savefig(argv[1])
show() show()
...@@ -8,7 +8,7 @@ double **create_y(int steps) { ...@@ -8,7 +8,7 @@ double **create_y(int steps) {
int i; int i;
if( y == NULL ) { if( y == NULL ) {
puts("Error: could not allocate sufficient memory."); puts("Error: could not allocate memory.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -49,6 +49,9 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) { ...@@ -49,6 +49,9 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) {
#define VERBOSE #define VERBOSE
#define PRINT_FORMAT "%e" #define PRINT_FORMAT "%e"
/*
*
*/
void iterate(double **y, int string_steps, double dx, int time_steps, double dt, void iterate(double **y, int string_steps, double dx, int time_steps, double dt,
double theta) { double theta) {
double x; double x;
...@@ -74,7 +77,7 @@ void iterate(double **y, int string_steps, double dx, int time_steps, double dt, ...@@ -74,7 +77,7 @@ void iterate(double **y, int string_steps, double dx, int time_steps, double dt,
} }
#endif #endif
// Iterate over the length of the string // Iterate over the length of the string for each time interval step
for( t = 2; t < time_steps; t++ ) { for( t = 2; t < time_steps; t++ ) {
#ifdef VERBOSE #ifdef VERBOSE
printf(PRINT_FORMAT " " PRINT_FORMAT " " PRINT_FORMAT, t * dt, .0, printf(PRINT_FORMAT " " PRINT_FORMAT " " PRINT_FORMAT, t * dt, .0,
...@@ -99,21 +102,15 @@ void iterate(double **y, int string_steps, double dx, int time_steps, double dt, ...@@ -99,21 +102,15 @@ void iterate(double **y, int string_steps, double dx, int time_steps, double dt,
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);
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int steps; int steps, i;
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;
if( argc < 8 ) { if( argc < 8 ) {
printf("Usage: %s METHOD TIME DT LENGTH DX THETA N|XP\n", argv[0]); printf("Usage: %s INIT_METHOD TIME DT LENGTH DX THETA N|XP\n", argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -134,9 +131,14 @@ int main(int argc, const char **argv) { ...@@ -134,9 +131,14 @@ int main(int argc, const char **argv) {
constant = atof(argv[7]); constant = atof(argv[7]);
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); iterate(y, steps, dx, (int)ceil(t / dt), dt, theta);
// Free allocated memory
for( i = 0; i < steps; i++ )
free(y[i]);
free(y);
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