-
Notifications
You must be signed in to change notification settings - Fork 0
/
1d_heat_btcs.c
240 lines (178 loc) · 5.53 KB
/
1d_heat_btcs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
////////////////////////////////////////////////////////////////////////////////
//
// 1d_heat_btcs.c
//
// Description :
// C program to solve 1D heat conduction equation.
// Using the backward time-central space (Euler Implicit) method
// Tridag subroutine used to solve matrix system referred from the book 'Numerical Recipes in C'
// TDMA subroutine referred from the book 'Numerical Heat Transfer and Fluid Flow' by Dr. Ghoshdatidar
//
// Metrics:
// Date/Time : 12-09-2020 14:28:36
// Hostname : colossus.it.mtu.edu
// OS : RHEL Workstation release 7.7 (Maipo)
// Kernel : 3.10.0-1062.el7.x86_64
// RAM : 502 GB
// CPU model : Intel(R) Xeon(R) Gold 6130 CPU @ 2.10GHz
// CPU/Core count : 64
// Author information : Akshay Dongre ([email protected])
// Source code license : GPL v3 (https://www.gnu.org/licenses/gpl-3.0.txt)
// Software/Language : Dev-C++/ Linux C Compiler
// Version : 5.11
// Pre-req/Dependency :
// Compilation command : gcc -Wall -g -lm 1d_heat_btcs.c -o 1d_heat_btcs.x
// Compilation time : A few seconds
// Execution command : ./1d_heat_btcs.x
//
// Note :
// Execution time : Apprx. 0.22 seconds
//
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float tridag(float a[], float b[], float c[], float r[], float u[], unsigned long n);
float TDMA(int Ii, int Nn, float Aa[], float Bb[], float Cc[], float Rr[], float Uu[]);
#define pi 3.141592653589793
#define alpha 0.02 // Thermal diffusivity (m^2/hr)
#define c0 100.0 // Initial temperature (Celcius)
#define L 1.0 // Length of the rod (m)
int main()
{
float dx, dt, t;
float D;
int nx, i, j, k;
int tl;
// User inputs
printf("Enter the spatial step increment dx (eg. 0.1): ");
scanf("%f", &dx);
printf("Enter time step dt (eg. 0.1): ");
scanf("%f", &dt);
printf("Enter final time in hrs. (eg. 10.0): ");
scanf("%f", &t);
// Problem formulation
D = alpha * (dt / (dx * dx));
nx = round(L / dx); // Total number of nodes
tl = round(t / dt); // Number of loops to run the code => (final time / time step)
printf("\n");
printf("D = %0.2f, nx = %d, tl = %d\n\n", D, nx, tl);
float T0[nx + 1], T[nx + 1];
float x_ax[nx + 1];
float T_ana[nx + 1];
float subdia[nx + 1], dia[nx + 1], supdiag[nx + 1], rgn[nx + 1], Temp[nx + 1];
for (i = 1; i <= (nx + 1); i++)
{
x_ax[i] = (i - 1) * dx;
// Solution initialization
T0[i] = c0 * sin(pi * x_ax[i] / L);
// Analytical solution
T_ana[i] = c0 * exp((-alpha * pi * pi * t) / (L * L)) * sin(pi * x_ax[i] / L);
}
// Backward time-central space (Euler Implicit) method
// Forming the sub-diagonal matrix
for (i = 2; i <= nx; i++)
{
subdia[i] = -D;
}
subdia[nx + 1] = 0.0;
// Forming the super-diagonal matrix
supdiag[1] = 0.0;
for (i = 2; i <= nx; i++)
{
supdiag[i] = -D;
}
// Forming the diagonal matrix
dia[1] = 1.0;
for (i = 2; i <= nx; i++)
{
dia[i] = 1 + (2 * D);
}
dia[nx + 1] = 1.0;
// Solving the tri-diagonal matrix system using the TDMA algorithm
for (j = 1; j <= tl; j++) // Time stepping
{
// tridag(subdia, dia, supdiag, T0, T, nx);
TDMA(1, nx + 1, subdia, dia, supdiag, T0, T);
for (i = 2; i <= nx; i++)
{
T0[i] = T[i];
}
}
for (i = 1; i <= nx + 1; i++)
{
printf("i = %d, j = %d, D = %0.2f, tl = %d, dx = %0.2f, x_ax = %0.2f, T_ana = %0.2f, T = %0.2f\n", i, j, D, tl, dx, x_ax[i], T_ana[i], T[i]);
}
// Writing output data file
FILE *fp;
char fname[80];
sprintf(fname, "phw1_btcs_nodes_%d.dat", (nx + 1));
fp = fopen(fname, "w");
for (i = 1; i <= nx + 1; i++)
{
fprintf(fp, "%f,%f,%f\n", x_ax[i], T_ana[i], T[i]);
}
fclose(fp);
return 0;
}
// tridag(a, b, c, r, u, n) => Tridiagonal matrix system solver algorithm that takes following inputs;
// a -> Sub-diagonal element
// b -> Diagonal element
// c -> Super-diagonal element
// r -> Right-hand side element array
// u -> Solution vector
// n -> Total number of equations
// Reference => Numerical Recipes in C
float tridag(float a[], float b[], float c[], float r[], float u[], unsigned long n)
{
unsigned long j;
float bet, gam[n];
if (b[1] == 0.0)
printf("Error 1 in tridag\n");
bet = b[1];
u[1] = r[1] / b[1];
// Decomposition and forward substitution
for (j = 2; j <= n; j++)
{
gam[j] = c[j - 1] / bet;
bet = b[j] - a[j] * gam[j];
if (bet == 0.0)
printf("Error 2 in tridag\n");
u[j] = (r[j] - a[j] * u[j - 1]) / bet;
}
// Back substitution
for (j = (n - 1); j >= 1; j--)
u[j] -= gam[j + 1] * u[j + 1];
}
// TDMA(Ii, Nn, Aa, Bb, Cc, Rr, Uu) => Tridiagonal matrix system solver algorithm that takes following inputs;
// Ii -> Initial number of equation
// Nn -> Total number of nodes
// Aa -> Sub-diagonal element
// Bb -> Diagonal element
// Cc -> Super-diagonal element
// Rr -> Right-hand side element array
// Uu -> Solution vector
// Reference => Numerical Heat Transfer and Fluid Flow by Dr. Ghoshdatidar
float TDMA(int Ii, int Nn, float Aa[], float Bb[], float Cc[], float Rr[], float Uu[])
{
unsigned long j, k;
float beta[Nn], gama[Nn];
// Compute intermediate arrays beta and gama
beta[1] = Bb[1];
gama[1] = Rr[1];
int i1 = Ii + 1;
for (j = i1; j <= Nn; j++)
{
beta[j] = Bb[j] - ((Aa[j] * Cc[j - 1]) / beta[j - 1]);
gama[j] = (Rr[j] - (Aa[j] * gama[j - 1])) / beta[j];
}
// Compute the solution vector Uu
Uu[Nn] = gama[Nn];
int n1 = Nn - 1;
for (k = 1; k <= n1; k++)
{
j = Nn - k;
Uu[j] = gama[j] - Cc[j] * Uu[j + 1] / beta[j];
}
}