Commit 5a84959c authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass4 taddeus: Source code cleanup.

parent b4842ecf
...@@ -3,47 +3,35 @@ ...@@ -3,47 +3,35 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
double **create_y(int steps) { double **y = NULL, dx;
double **y = (double **)malloc(3 * sizeof(double *)); int x_steps, calc_steps, print_resolution;
int i;
if( y == NULL ) {
puts("Error: could not allocate memory.");
exit(EXIT_FAILURE);
}
for( i = 0; i < 3; i++ )
y[i] = (double *)malloc(steps * sizeof(double));
return y;
}
/* /*
* *
*/ */
void init_sinus(double **y, int steps, double l, double dx, double n) { void init_sinus(double l, double n) {
for( int i = 0; i < steps; i++ ) for( int i = 0; i < x_steps; i++ )
y[0][i] = sin(n * M_PI * i * dx / l); y[0][i] = sin(n * M_PI * i * dx / l);
// The string is attached at both ends // The string is attached at both ends
y[2][0] = y[1][0] = y[0][0]; y[2][0] = y[1][0] = y[0][0];
y[2][steps - 1] = y[1][steps - 1] = y[0][steps - 1]; y[2][x_steps - 1] = y[1][x_steps - 1] = y[0][x_steps - 1];
} }
/* /*
* *
*/ */
void init_plucked(double **y, int steps, double l, double dx, double xp) { void init_plucked(double l, double xp) {
double x; double x;
for( int i = 0; i < steps; i++ ) { for( int i = 0; i < x_steps; i++ ) {
x = i * dx; x = i * dx;
y[0][i] = x < xp ? x / xp : (l - x) / (l - xp); y[0][i] = x < xp ? x / xp : (l - x) / (l - xp);
} }
// The string is attached at both ends // The string is attached at both ends
y[2][0] = y[1][0] = y[0][0]; y[2][0] = y[1][0] = y[0][0];
y[2][steps - 1] = y[1][steps - 1] = y[0][steps - 1]; y[2][x_steps - 1] = y[1][x_steps - 1] = y[0][x_steps - 1];
} }
#define VERBOSE #define VERBOSE
...@@ -51,12 +39,12 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) { ...@@ -51,12 +39,12 @@ void init_plucked(double **y, int steps, double l, double dx, double xp) {
#ifdef VERBOSE #ifdef VERBOSE
#define PRINT_FORMAT "%e" #define PRINT_FORMAT "%e"
void print(double *y, int string_steps) { void print(int index) {
for( int i = 0; i < string_steps; i++ ) { for( int i = 0; i < x_steps; i++ ) {
if( i ) if( i )
printf(" "); printf(" ");
printf(PRINT_FORMAT, y[i]); printf(PRINT_FORMAT, y[index][i]);
} }
puts(""); puts("");
...@@ -66,14 +54,13 @@ void print(double *y, int string_steps) { ...@@ -66,14 +54,13 @@ void print(double *y, int string_steps) {
/* /*
* *
*/ */
void iterate(double **y, int string_steps, double dx, int calc_steps, void calculate_steps(int calc_steps, int print_resolution, double tau) {
int print_resolution, double tau) {
double x; double x;
int i, t, 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 = 1; i < string_steps - 1; i++ ) { for( i = 1; i < x_steps - 1; i++ ) {
x = i * dx; x = i * dx;
y[1][i] = y[0][i] + .5 * tau * tau * (y[0][i-1] - 2 * y[0][i] y[1][i] = y[0][i] + .5 * tau * tau * (y[0][i-1] - 2 * y[0][i]
+ y[0][i+1]); + y[0][i+1]);
...@@ -81,7 +68,7 @@ void iterate(double **y, int string_steps, double dx, int calc_steps, ...@@ -81,7 +68,7 @@ void iterate(double **y, int string_steps, double dx, int calc_steps,
#ifdef VERBOSE #ifdef VERBOSE
// Print x values (which are the same for every y-series) // Print x values (which are the same for every y-series)
for( i = 0; i < string_steps; i++ ) { for( i = 0; i < x_steps; i++ ) {
if( i ) if( i )
printf(" "); printf(" ");
...@@ -91,22 +78,22 @@ void iterate(double **y, int string_steps, double dx, int calc_steps, ...@@ -91,22 +78,22 @@ void iterate(double **y, int string_steps, double dx, int calc_steps,
puts(""); puts("");
// Print init states // Print init states
print(y[prev], string_steps); print(prev);
if( print_resolution == 1 ) if( print_resolution == 1 )
print(y[current], string_steps); print(current);
#endif #endif
// Iterate over the length of the string for each time interval step // Iterate over the length of the string for each time interval step
for( t = 2; t < calc_steps; t++ ) { for( t = 2; t < calc_steps; t++ ) {
for( i = 1; i < string_steps - 1; i++ ) { for( i = 1; i < x_steps - 1; i++ ) {
y[next][i] = 2 * y[current][i] - y[prev][i] + tau * tau y[next][i] = 2 * y[current][i] - y[prev][i] + tau * tau
* (y[current][i-1] - 2 * y[current][i] + y[current][i+1]); * (y[current][i-1] - 2 * y[current][i] + y[current][i+1]);
} }
#ifdef VERBOSE #ifdef VERBOSE
if( !(t % print_resolution) ) if( !(t % print_resolution) )
print(y[next], string_steps); print(next);
#endif #endif
prev = current; prev = current;
...@@ -116,10 +103,11 @@ void iterate(double **y, int string_steps, double dx, int calc_steps, ...@@ -116,10 +103,11 @@ void iterate(double **y, int string_steps, double dx, int calc_steps,
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int x_steps, calc_steps, i, print_resolution; int calc_steps, i, print_resolution;
void (*init_method)(double **, int, double, double, double); void (*init_method)(double, double);
double **y, l, dx, tau, constant; double l, tau, constant;
// Parse argument
if( argc < 8 ) { if( argc < 8 ) {
printf("Usage: %s INIT_METHOD CALCULATION_STEPS PRINT_RESOLUTION LENGTH" printf("Usage: %s INIT_METHOD CALCULATION_STEPS PRINT_RESOLUTION LENGTH"
" DX TAU N|XP\n", argv[0]); " DX TAU N|XP\n", argv[0]);
...@@ -142,10 +130,23 @@ int main(int argc, const char **argv) { ...@@ -142,10 +130,23 @@ int main(int argc, const char **argv) {
tau = atof(argv[6]); tau = atof(argv[6]);
constant = atof(argv[7]); constant = atof(argv[7]);
// Allocate a 3xN two-dimensional area that functions as a cyclic buffer
// for three states of the string (previous, current, next)
x_steps = (int)ceil(l / dx); x_steps = (int)ceil(l / dx);
y = create_y(x_steps);
(*init_method)(y, x_steps, l, dx, constant); if( (y = (double **)malloc(3 * sizeof(double *))) == NULL ) {
iterate(y, x_steps, dx, calc_steps, print_resolution, tau); puts("Error: could not allocate memory.");
exit(EXIT_FAILURE);
}
for( i = 0; i < 3; i++ )
y[i] = (double *)malloc(x_steps * sizeof(double));
// Initialize the string values (sinus/plucked)
(*init_method)(l, constant);
// Calculate the specified number of steps
calculate_steps(calc_steps, print_resolution, tau);
// Free allocated memory // Free allocated memory
for( i = 0; i < 3; i++ ) for( i = 0; i < 3; i++ )
......
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