-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheat1D_Thomas.c
152 lines (111 loc) · 2.29 KB
/
heat1D_Thomas.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
/* Heat equation using the Thomas algorithm
*
* Dale Roberts <dale.o.roberts@gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define N 100
double U[N+1];
double b[N+1];
double minX = 0.0;
double maxX = 1.0;
double du[N];
double dc[N+1];
double dl[N];
float dx, dt, mu, theta;
void dumpSolution(int n) {
char fname[20];
int s;
double x;
FILE *fd;
snprintf(fname, sizeof(fname), "data.%i", n);
fd = fopen(fname, "w");
for (s = 0; s < N+1; s++) {
x = minX + s * dx;
fprintf(fd, "%10.5lf %10.5lf\n", x, U[s]);
}
fclose(fd);
}
void dumpRHS(int n) {
char fname[20];
int s;
double x;
FILE *fd;
snprintf(fname, sizeof(fname), "rhs.%i", n);
fd = fopen(fname, "w");
for (s = 0; s < N+1; s++) {
x = minX + s * dx;
fprintf(fd, "%10.5lf\n", b[s]);
}
fclose(fd);
}
void lapack_dgtsv(long dim, long nRHS, double *dl, double *d, double *du, double *b, long ldb) {
long info;
dgtsv_(&dim, &nRHS, dl, d, du, b, &ldb, &info);
if (info != 0)
printf("sgtsv = %li\n", info);
}
void initialDensity(void) {
int s;
double x;
for (s = 0; s < N+1; s++) {
x = minX + s * dx;
if (x <= 0.5)
U[s] = 2*x;
else
U[s] = 2-2*x;
}
}
void initDiagonals(void) {
int s;
for (s = 0; s < N; s++) {
dc[s] = (1+2*theta*mu);
du[s] = -0.5 * mu;
dl[s] = -0.5 * mu;
}
/* boundary conditions */
dc[0] = 1.0;
du[0] = 0.0;
dl[N-1] = 0.0;
dc[N] = 1.0;
}
void createRHS(void) {
int s;
for (s = 1; s < N; s++)
b[s] = U[s] + (1-theta)*mu*(U[s-1] - 2 * U[s] + U[s+1]);
/* boundary conditions */
b[0] = 0.0;
b[N] = 0.0;
}
void stepSolver(int t) {
int s;
createRHS();
dumpRHS(t);
lapack_dgtsv(N+1, 1, dl, dc, du, b, N+1);
for (s = 0; s < N+1; s++)
U[s] = b[s];
}
void initSolver(void) {
dx = (maxX - minX) / (double) N;
dt = 0.001;
theta = 0.6;
mu = dt / (dx*dx);
printf("\ndx=%.5lf dt=%.5lf mu=%.5lf theta=%.5lf\n", dx, dt, mu, theta);
if (0.0 <= theta && theta < 0.5 && mu > 0.5/(1-2*theta))
puts("unstable parameters");
else
puts("stable parameters");
initDiagonals();
initialDensity();
}
int main(void) {
int t;
initSolver();
dumpSolution(0);
for (t = 1; t < 20; t++) {
stepSolver(t);
dumpSolution(t);
}
return 0;
}