Added sources files for part 2 of modsim assignment 4.

parent c19b79d2
CFLAGS +=-std=c99 -Wall -Wextra -g -O2 -pthread
LDFLAGS+=-pthread -lmpi -lopen-rte -lopen-pal -ldl -Wl,--export-dynamic \
-lnsl -lutil -lm -ldl
MAIN = main
OFILES = $($(wildcard *.c):.c=.o)
$(MAIN): $(OFILES)
clean:
rm -vf $(MAIN) $(OFILES)
check-syntax:
$(CXX) $(CXXFLAGS) -fsyntax-only ${CHK_SOURCES}
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define GRID_LENGTH 64
double Y[GRID_LENGTH][3];
int t_next, t_prev, t_cur;
void init_sinus(char **argv) {
double amp;
int h_phases;
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);
}
}
void init_pluck(char **argv) {
double amp, position;
int ipos, i;
sscanf(argv[2], "%lf", &amp);
sscanf(argv[3], "%i", &position);
ipos = position / dx * GRID_LENGTH;
for( i = 0; i < ipos; i++ ) {
Y[i][t] = i * dx / position * amp;
}
for(;i < GRID_LENGTH; i++ ) {
Y[i][t] = ((GRID_LENGTH - i) * dx) / (GRID_LENGTH - ipos) * amp;
}
}
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];
}
}
#define SWAP(a, b) \
a ^= b; \
b ^= a; \
a ^= b;
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) {
if(argc < 4) {
fprintf(stderr, "Usage: %s <p amplitude position |s amplitude phases", argv[0]);
return 1;
}
switch(*argv[1]) {
case 'p': init_pluck(argv); break;
case 's': init_sinus(argv); break;
default:
fprintf(stderr, "Does this look like a 'p' or 's' to you?\n");
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;
for( int i = 0; i < T; i++ ) {
iterate(C, t_prev, t_cur, t_next);
SWAP(t_cur, t_next);
SWAP(t_prev, t_next);
plot(t_cur);
}
return 0;
}
from pyplot import show, fig
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