Commit d5bfd07b authored by Taddeüs Kroes's avatar Taddeüs Kroes

Merge branch 'master' of ssh://vo20.nl/home/git/repos/uva

parents 2ed3ac6e 03a8c9e4
#!/bin/sh #!/bin/sh
NUM_PROCESSES=4 NUM_PROCESSES=4
HOSTS=ow130,ow131 HOSTS=deleuze,localhost
PROGRAM_EXEC=main PROGRAM_EXEC=main
mpirun -np $NUM_PROCESSES -H $HOSTS $PROGRAM_EXEC mpirun -np $NUM_PROCESSES -H $HOSTS $PROGRAM_EXEC
CFLAGS +=-std=c99 -Wall -Wextra -g -O2 -pthread CFLAGS +=-std=c99 -Wall -Wextra -pedantic -g -O2 -pthread -D_GNU_SOURCE
LDFLAGS+=-pthread -lmpi -lopen-rte -lopen-pal -ldl -Wl,--export-dynamic \ LDFLAGS+=-pthread -lmpi -lopen-rte -lopen-pal -ldl -Wl,--export-dynamic \
-lnsl -lutil -lm -ldl -lnsl -lutil -lm -ldl
......
...@@ -2,84 +2,120 @@ ...@@ -2,84 +2,120 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#define GRID_LENGTH 64 #define STRING_PARTS len
double Y[GRID_LENGTH][3]; int STRING_PARTS;
double *Y;
int t_next, t_prev, t_cur; int t_next, t_prev, t_cur;
void init_sinus(char **argv) { /*
double amp; * Macros used to manipulate the allocated memory for Y.
int h_phases; */
#define GET_Y(i, t) \
Y[i + t * STRING_PARTS]
sscanf(argv[2], "%lf", &amp); #define SET_Y(i, t, val) \
sscanf(argv[3], "%i", &h_phases); Y[i + t * STRING_PARTS] = val
for( int i = 0; i < GRID_LENGTH; i++ ) {
Y[i][t] = amp * sin(M_PI * (i(double)/(double)GRID_LENGTH) * h_phases);
}
}
void init_pluck(char **argv) { /*
double amp, position; * Fill the allocated memory for Y with a sinus shape.
int ipos, i; */
static void init_sinus(int phases) {
double c = (double) (STRING_PARTS * phases) * M_PI ;
sscanf(argv[2], "%lf", &amp); for( int i = 0; i < STRING_PARTS; i++ ) {
sscanf(argv[3], "%i", &position); SET_Y(i, 0, sin(((double) i) / c));
}
}
ipos = position / dx * GRID_LENGTH; /*
* Fill the allocated memory for Y with a pluck shape.
*/
static void init_pluck(double pluck, double length, double dx) {
int ipos = ((int) dx) + 1, i;
fprintf(stderr, "ipos: %d\n", ipos);
for( i = 0; i < ipos; i++ ) { for( i = 0; i < ipos; i++ ) {
Y[i][t] = i * dx / position * amp; SET_Y(i, 0, i * dx / pluck);
} }
for(;i < GRID_LENGTH; i++ ) {
Y[i][t] = ((GRID_LENGTH - i) * dx) / (GRID_LENGTH - ipos) * amp; for( ; i < STRING_PARTS; i++ ) {
SET_Y(i, 0, ((STRING_PARTS - i) * dx) / (STRING_PARTS - ipos));
} }
} }
void iterate(double C, int t_prev, int t_cur, int t_next) { static void connect_string_ends() {
for( int i = 1; i < GRID_LENGTH; i++ ) { const int end = STRING_PARTS - 1;
Y[i][t_next] = C * (Y[i-1][t_cur] - 2*Y[i][t_cur] + Y[i+1][t_cur]) Y[STRING_PARTS * 2] = Y[STRING_PARTS * 1] = Y[STRING_PARTS];
- Y[i][t_prev] Y[STRING_PARTS * 2 + end] = Y[STRING_PARTS * 1 + end] = Y[STRING_PARTS + end];
+ 2 * Y[i][t_cur]; }
/*
* Calculate position over the entire string at time 't_cur' (= dt).
*/
static void iterate(double C, int t_prev, int t_cur, int t_next) {
for( int i = 1, max = STRING_PARTS - 1; i < max; i++ ) {
SET_Y(i, t_next, \
C * (GET_Y(i-1, t_cur) - 2 * GET_Y(i, t_cur) + GET_Y(i+1, t_cur))
- GET_Y(i, t_prev)
+ 2 * GET_Y(i, t_cur));
} }
} }
/*
* Integer XOR swapping macro.
*/
#define SWAP(a, b) \ #define SWAP(a, b) \
a ^= b; \ a ^= b; \
b ^= a; \ b ^= a; \
a ^= b; a ^= b;
static void plot(int col) {
printf("%e", GET_Y(0, col));
for( int i = 1; i < STRING_PARTS; i++ )
printf(",%e", GET_Y(i, col));
void plot(int col) {
printf("%f", Y[0][col]);
for( int i = 1; i < GRID_LENGTH; i++ )
printf(",%f", Y[i][col]);
printf("\n"); printf("\n");
} }
int main(int argc, char**argv) { int main(int argc, char **argv) {
if(argc < 4) { if(argc < 7) {
fprintf(stderr, "Usage: %s <p amplitude position |s amplitude phases", argv[0]); fprintf(stderr, "Usage: %s <p|s> ITERATIONS LENGTH STEP_SIZE"\
" TAU <PLUCK|PHASES>\n",
argv[0]);
return 1; return 1;
} }
// Initialise time indices, iterations and string length.
int t_prev = 0, t_cur = 1, t_next = 2,
iterations = atoi(argv[2]),
length = atoi(argv[3]);
double dx = atof(argv[4]),
tau = atof(argv[5]);
STRING_PARTS = length / dx;
if( !(Y = (double *) malloc(3 * STRING_PARTS * sizeof(double))) )
return 2;
// Initialise the requested shape in the allocated memory.
switch(*argv[1]) { switch(*argv[1]) {
case 'p': init_pluck(argv); break; case 'p': init_pluck(atof(argv[6]), length, dx); break;
case 's': init_sinus(argv); break; case 's': init_sinus(atoi(argv[6])); break;
default: default:
fprintf(stderr, "Does this look like a 'p' or 's' to you?\n"); fprintf(stderr, "Argument 2 '%s' does not look like a 'p' or 's'.\n",
argv[1]);
return 1; return 1;
} }
double c = .04, dt = .1, dx = .02, // The string is connected at both ends.
C = c * c * (dt/dx) * (dt/dx); connect_string_ends();
// TODO: implement iterations
int t_prev = 0, t_cur = 1, t_next = 2,
T = 10;
for( int i = 0; i < T; i++ ) { for( int i = 0; i < iterations; i++ ) {
iterate(C, t_prev, t_cur, t_next); iterate(tau, t_prev, t_cur, t_next);
SWAP(t_cur, t_next); SWAP(t_cur, t_next);
SWAP(t_prev, t_next); SWAP(t_prev, t_next);
plot(t_cur); plot(t_cur);
......
from pyplot import show, fig #!/usr/bin/env python
from pylab import plot, figure, clf, show
from sys import stdin
lines = stdin.readlines()
Y = map(float, lines[0].split(','))
figure(1)
clf()
plot(Y, '-')
show()
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