-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnaive.c
131 lines (123 loc) · 2.78 KB
/
naive.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define M 100
#define analytical_tol 5.0e-3
/*
*
* Code by ARC, @arunningcroc
*
*/
void swap(double (*a)[M], double (*b)[M], int n)
{
double swp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
swp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = swp;
}
}
}
double mmax(double (*phi)[M], double (*phip)[M], int n)
{
double max = 0.0;
double diff = 0.0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
diff = fabs(phi[i][j]-phip[i][j]);
if (diff > max)
max = diff;
}
}
return max;
}
void normalize(double (*phi)[M]) {
double max = -50000;
for(int i=0; i<M; i++) {
for(int j=0; j<M; j++) {
if(fabs(phi[i][j]) > max) max = fabs(phi[i][j]);
}
}
for(int i=0; i<M; i++) {
for(int j=0; j<M; j++) {
phi[i][j] = phi[i][j]/max;
}
}
}
double rho(double x, double y)
{
return -(6.0*x*y*(1.0-y)-2.0*pow(x,3.0));
}
double boundary(double y)
{
return y*(1.0-y);
}
double analytical_sol(double x,double y)
{
return y*(1.0-y)*pow(x,3.0);
}
double get_average_error(double (*sol)[M], double (*analytical)[M]) {
double sum = 0.0;
normalize(sol);
normalize(analytical);
for(int i=0; i<M; i++) {
for(int j=0; j<M; j++) {
sum += fabs(sol[i][j]-analytical[i][j]);
}
}
return sum/(M*M);
}
int run(double toler, double a)
{
double a2;
double (*phi)[M];
double (*phip)[M];
double (*rhoa)[M];
double (*analytical)[M];
phi = malloc(sizeof(double[M][M]));
phip = malloc(sizeof(double[M][M]));
rhoa = malloc(sizeof(double[M][M]));
analytical = malloc(sizeof(double[M][M]));
for(int i=0; i<M; i++) {
for (int j=0; j<M; j++) {
analytical[i][j] = analytical_sol(i*a,j*a);
}
}
int iter = 0;
memset(phip, 0, sizeof(phip[0][0])*M*M);
memset(phi, 0, sizeof(phi[0][0])*M*M);
for (int j=0; j < M; j++) {
phip[M-1][j] = j*a*(1-j*a);
phi[M-1][j] = j*a*(1-j*a);
}
double delta = 1.0;
a2 = pow(a,2.0);
while (delta > toler) {
iter += 1;
for (int i=1; i < M-1; i++) {
for (int j=1; j < M-1; j++) {
phip[i][j] = (phi[i+1][j] + phi[i-1][j] +
phi[i][j+1] + phi[i][j-1])/4.0 +
a2*rho(i*a,j*a)/4.0;
}
}
delta = mmax(phi, phip, M);
swap(phi, phip, M);
}
if(get_average_error(phi, analytical) > analytical_tol) {
printf("Failed to reach analytical solution, error %f \n", get_average_error(phi, analytical));
}
return iter;
}
int main(int argc, char *argv[])
{
int iter;
clock_t start = clock();
iter = run(1e-8, 1.0/(M-1));
clock_t end = clock();
double total = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Execution time %f, iters: %d\n",total,iter);
}