ModSim ass4 part 1 and 2 are not finished.

parent dd69ac78
#!/bin/sh
NUM_PROCESSES=4
HOSTS=ow130,ow131
HOSTS=deleuze,localhost
PROGRAM_EXEC=main
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 \
-lnsl -lutil -lm -ldl
......
......@@ -2,84 +2,120 @@
#include <stdlib.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;
void init_sinus(char **argv) {
double amp;
int h_phases;
/*
* Macros used to manipulate the allocated memory for Y.
*/
#define GET_Y(i, t) \
Y[i + t * STRING_PARTS]
sscanf(argv[2], "%lf", &amp);
sscanf(argv[3], "%i", &h_phases);
for( int i = 0; i < GRID_LENGTH; i++ ) {
Y[i][t] = amp * sin(M_PI * (i(double)/(double)GRID_LENGTH) * h_phases);
}
}
#define SET_Y(i, t, val) \
Y[i + t * STRING_PARTS] = val
void init_pluck(char **argv) {
double amp, position;
int ipos, i;
/*
* Fill the allocated memory for Y with a sinus shape.
*/
static void init_sinus(int phases) {
double c = (double) (STRING_PARTS * phases) * M_PI ;
sscanf(argv[2], "%lf", &amp);
sscanf(argv[3], "%i", &position);
for( int i = 0; i < STRING_PARTS; i++ ) {
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++ ) {
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) {
for( int i = 1; i < GRID_LENGTH; i++ ) {
Y[i][t_next] = C * (Y[i-1][t_cur] - 2*Y[i][t_cur] + Y[i+1][t_cur])
- Y[i][t_prev]
+ 2 * Y[i][t_cur];
static void connect_string_ends() {
const int end = STRING_PARTS - 1;
Y[STRING_PARTS * 2] = Y[STRING_PARTS * 1] = Y[STRING_PARTS];
Y[STRING_PARTS * 2 + end] = Y[STRING_PARTS * 1 + end] = Y[STRING_PARTS + end];
}
/*
* 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) \
a ^= b; \
b ^= a; \
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");
}
int main(int argc, char**argv) {
int main(int argc, char **argv) {
if(argc < 4) {
fprintf(stderr, "Usage: %s <p amplitude position |s amplitude phases", argv[0]);
if(argc < 7) {
fprintf(stderr, "Usage: %s <p|s> ITERATIONS LENGTH STEP_SIZE"\
" TAU <PLUCK|PHASES>\n",
argv[0]);
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]) {
case 'p': init_pluck(argv); break;
case 's': init_sinus(argv); break;
case 'p': init_pluck(atof(argv[6]), length, dx); break;
case 's': init_sinus(atoi(argv[6])); break;
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;
}
double c = .04, dt = .1, dx = .02,
C = c * c * (dt/dx) * (dt/dx);
// TODO: implement iterations
int t_prev = 0, t_cur = 1, t_next = 2,
T = 10;
// The string is connected at both ends.
connect_string_ends();
for( int i = 0; i < T; i++ ) {
iterate(C, t_prev, t_cur, t_next);
for( int i = 0; i < iterations; i++ ) {
iterate(tau, t_prev, t_cur, t_next);
SWAP(t_cur, t_next);
SWAP(t_prev, t_next);
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