-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_slides.c
89 lines (74 loc) · 2.21 KB
/
code_slides.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
double getGauge(double *p, int N0, int N1){
const int i_gauge = (N0 -1)/2, j_gauge = (N1 -1)/4; /* at (0.5, 0.25) */
return p[i_gauge + N0 * j_gauge];
}
void setInitialPressure(double *p, int N0, int s0, int N1){
/* TODO had to make edit here to get j0 variable was: */
/* const int i0 = (N0-1)/2, i1 = (N1-1)/2; */
const int i0 = (N0-1)/2, j0 = (N1-1)/2; /* (0.5, 0.5) */
printf("i0,j0: %d, %d\n", i0, j0);
int i, j;
for (j = 0; j < N1; j++) {
for (i = 0; i < N0; i++){
if (i == i0 && j == j0) {
/* TODO why is there s0 instead of N0?? */
p[i + s0 * j] = 1; /* p = 1 at the centre of domain */
} else {
p[i + s0 * j] = 0; /* p = 0 elsewhere */
}
}
}
}
void copyPressure(double *p_dst, const double *p_src, int N0, int N1){
int i, j;
for (j=0; j < N1; j++) {
for (i=0; i < N0; i++) {
p_dst[i + N0 * j] = p_src[i + N0 * j];
}
}
}
void updatePressure(double *p_new, const double *p, int N0, int N1,
double a, double b) {
int i, j;
for (j = 1; j < N1 - 1; j++) {
for (i = 1; i < N0 - 1; i++) {
p_new[i + N0 * j] =
a * p[i + N0 * j] + /* centre */
b * (p[i - 1 + N0 * j] + /* west */
p[i + 1 + N0 * j] + /* east */
p[i + N0 * (j - 1)] + /* south */
p[i + N0 * (j + 1)]); /* north */
}
}
}
void printGrid(double *p, int N0, int N1, FILE *fp){
int i, j;
for (j = 1; j < N1 - 1; j++) {
for (i = 1; i < N0 - 1; i++) {
fprintf(fp, "%.2lf ", p[i + N0 *j]);
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
int main(int argc, char **argv){
const int N0 = 51, N1 = N0; /* grid size, must be odd */
/* Watch out for Courant condition (x2 resolution, x4 more time steps */
const double dx = 1./(N0-1), dt = 1e-5, t_end = 0.5,
a = 1-4 * dt/ (dx * dx), b = dt/(dx * dx);
/* TODO probably want to malloc this?? */
double t = 0., p_gauge, p[N0 * N1], p_new[N0 * N1], tmp[N0 * N1];
int n = 0;
setInitialPressure(p, N0, N0, N1);
while (t < t_end) {
updatePressure(p_new, p, N0, N1, a, b);
p_gauge = getGauge(p_new, N0, N1);
n++; t += dt;
if (t < 0.1){
printf("time step: %d, t=%e, p at gauge = %e\n", n, t, p_gauge);
}
/* TODO can just swap pointers here instead of copy */
copyPressure(p, p_new, N0, N1);
}
}