-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhuongTrinhNhiet2D.c
97 lines (88 loc) · 2.43 KB
/
PhuongTrinhNhiet2D.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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <time.h>
#define m 20
#define n 20
#define T 2
#define dt 0.01
#define dx 0.1
#define D 0.1
//=========================
void DisplayMatrix(float *A, int row, int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) printf(" %.2f", *(A + i * col + j));
printf("\n");
}
}
//=========================
void Write2File(float *C) {
FILE *result = fopen("result2.txt", "a");
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
fprintf(result, "%lf\t", *(C + i * n + j));
}
fprintf(result, "\n");
}
fclose(result);
}
//=========================
void KhoiTao(float *C) {
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) {
if (i >= (m / 2 - 5) && i < (m / 2 + 5) && j >= (n / 2 - 5) && j < (n / 2 + 5))
*(C + i * n + j) = 80.0;
else
*(C + i * n + j) = 25.0;
}
}
//=========================
void FD(float *C, float *dC) {
int i, j;
float c, u, d, l, r;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) {
c = *(C + i * n + j);
u = (i == 0) ? *(C + i * n + j) : *(C + (i - 1) * n + j);
d = (i == m - 1) ? *(C + i * n + j) : *(C + (i + 1) * n + j);
l = (j == 0) ? *(C + i * n + j) : *(C + i * n + j - 1);
r = (j == n - 1) ? *(C + i * n + j) : *(C + i * n + j + 1);
*(dC + i * n + j) = (D / (dx * dx))*(u + d + l + r - 4 * c);
}
}
//=========================
int main() {
int i, j, count;
float t;
time_t t1, t2;
t = 0;
float *C, *dC;
C = (float *) malloc((m * n) * sizeof (float));
dC = (float *) malloc((m * n) * sizeof (float));
KhoiTao(C);
Write2File(C);
printf("Gia tri khoi tao:\n");
DisplayMatrix(C, m, n);
count = 0;
t1 = time(NULL);
while (t <= T) {
FD(C, dC);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
*(C + i * n + j) = *(C + i * n + j) + dt * (*(dC + i * n + j));
t = t + dt;
count = count + 1;
// if (count % 5 == 0) Write2File(C);
}
t2 = time(NULL);
//FD(C, dC);
printf("Gia tri cuoi cung:\n");
DisplayMatrix(C, m, n);
//
printf("\tThe Calculation time:%ld\n", (long) (t2 - t1));
return 0;
}