-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheat2D_LOD.c
156 lines (114 loc) · 2.56 KB
/
heat2D_LOD.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* 2D Heat equation using the LOD method
*
* Dale Roberts <dale.o.roberts@gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "config.h"
extern float U[N+1][N+1];
extern float V[N+1][N+1];
extern float D[N+1][N+1];
float minX = -5.0;
float maxX = 5.0;
float minY = -5.0;
float maxY = 5.0;
float b[N+1];
float du[N];
float dc[N];
float dl[N];
float dx, dy, dt, mux, muy;
extern int showVectorField;
void print_vec(float *vec, int n)
{
for (int i = 0; i < n; i++) {
if (i%8 == 0 && i != 0)
putchar('\n');
printf("%10.2Ef", vec[i]);
}
putchar('\n');
}
void lapack_sgtsv(long dim, long nRHS, float *dl, float *d, float *du, float *b, long ldb)
{
long info;
sgtsv_(&dim, &nRHS, dl, d, du, b, &ldb, &info);
assert( info == 0 );
}
inline float max(float a, float b) {
return a<b? a: b;
}
int inBound(int coord) {
if (coord < 0)
return 0;
else if (coord > N)
return N;
else
return coord;
}
void initialDensity(void) {
float x, y;
for (int s = 0; s < N+1; s++) {
x = minX + s * dx;
for (int r = 0; r < N+1; r++) {
y = minY + r * dy;
D[s][r] = exp(-x*x-y*y);
}
}
}
void initialVectorField(void) {
float x, y;
for (int s = 0; s < N+1; s++) {
x = minX + s * dx;
for (int r = 0; r < N+1; r++) {
y = minY + r * dy;
U[s][r] = sin(x*y);
V[s][r] = cos(x*y);
}
}
}
void initDiagonals(void) {
dc[0] = 1;
du[0] = 0;
for (int s = 1; s < N; s++) {
dc[s] = (1+mux);
du[s] = -0.5 * mux;
dl[s-1] = -0.5 * mux;
}
dc[N] = 1;
dl[N-1] = 0;
}
void stepSolver(void) {
// Generate U^{n+*}
for (int r = 0; r < N+1; r++) {
b[0] = D[0][r];
for (int s = 1; s < N; s++)
b[s] = 0.5 * mux * D[s-1][r] + (1 - mux) * D[s][r] + 0.5 * mux * D[s+1][r];
b[N] = D[N][r];
lapack_sgtsv(N+1, 1, dl, dc, du, b, N+1);
for (int s = 0; s < N+1; s++)
D[s][r] = b[s];
}
// Generate U^n
for (int s = 0; s < N+1; s++) {
b[0] = D[s][0];
for (int r = 1; r < N; r++)
b[r] = 0.5 * mux * D[s][r-1] + (1 - mux) * D[s][r] + 0.5 * mux * D[s][r+1];
b[N] = D[s][N];
lapack_sgtsv(N+1, 1, dl, dc, du, b, N+1);
for (int r = 0; r < N+1; r++)
D[s][r] = b[r];
}
}
void initSolver(void) {
dx = 10.0 / (float)N;
dy = 10.0 / (float)N;
dt = 0.001;
mux = dt / (dx*dx);
muy = dt / (dy*dy);
//printf("dx=%.3f dy=%.3f dt=%.3f mu_x=%.3f mu_y=%.3f\n", dx, dy, dt, mux, muy);
initDiagonals();
initialVectorField();
initialDensity();
showVectorField = 0;
}