-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_test.cpp
More file actions
94 lines (85 loc) · 1.9 KB
/
cpu_test.cpp
File metadata and controls
94 lines (85 loc) · 1.9 KB
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
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
const int MAXITER = 1000000;
const int N = 32 * 32; // Êîëè÷åñòâî âíóòðåííèõ óçëîâ ñåòêè
const int SIZE = N + 2; // Îáùåå êîëè÷åñòâî óçëîâ â ñåòêå
float LENGTH = 10; // Äëèíà èíòåðâàëà ðàñ÷åòà
const float h = LENGTH / (SIZE - 1); // Âåëè÷èíà øàãà ñåòêè
const float a = 1.0; // Ïàðàìåòð äèôô. óðàâíåíèÿ
float F[SIZE][SIZE]; // Ìàòðèöà çíà÷åíèé ôóíêöèè íà çàäàííîé ñåòêå
float Fprev[SIZE][SIZE];
const float h_sq = h * h;
const float c = 4.0 / h_sq + a;
float r(float x, float y)
{
return -(x + y);
}
float solution(float x, float y)
{
return x + y;
}
void Init()
{
int i, j, k;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if ((i != 0) && (j != 0) && (i != SIZE - 1) && (j != SIZE - 1))
{
F[i][j] = 0;
}
else
{
F[i][j] = solution(i * h, j * h);
Fprev[i][j] = F[i][j];
}
}
}
}
int main(int argc, char * argv[])
{
clock_t start;
double duration;
bool complete = false;
Init();
start = clock();
int iteration;
for (iteration = 1; complete == false && iteration < MAXITER; iteration++)
{
complete = true;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
float Fi = (Fprev[i - 1][j] + Fprev[i + 1][j]) / h_sq;
float Fj = (Fprev[i][j - 1] + Fprev[i][j + 1]) / h_sq;
F[i][j] = (Fi + Fj - r(i * h, j * h)) / c;
if (fabs(F[i][j] - Fprev[i][j]) > 1e-5)
{
complete = false;
}
}
}
swap(Fprev, F);
}
float maxError = 0;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (fabs(F[i][j] - solution(i * h, j * h)) > maxError)
{
maxError = fabs(F[i][j] - solution(i * h, j * h));
}
}
}
duration = (clock() - start) / (double)CLOCKS_PER_SEC;
std::cout << "printf: " << duration << '\n';
printf("\nError = %f, Iteration = %d", maxError, iteration);
_getch();
return 0;
}