-
Notifications
You must be signed in to change notification settings - Fork 1
/
laplace2D.c
141 lines (118 loc) · 3.29 KB
/
laplace2D.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double** solve_lap_sor(int N, double **bound, double **rho, double w, double tau){
/*
Function for sove laplace equations with SOR method
N : int, size of grid
bound : matrix, boundary values
rho : matrix, source of field
w : float, overrelaxation parameter
tau : float, required convergence
*/
// usefull quantities
double conv, mean_U0, mean_U, force;
int iter;
// matrix of the potential
double **phi;
phi = (double **)malloc((N+1)*sizeof(double));
for(int i=0; i<N+1; i++){
phi[i] = (double*)calloc((N+1), sizeof(double) );
}
//boundary condition
for(int i=0; i<=N; i++){
// attention: the corners could be overwritten
phi[i][0] = bound[0][i]; // ovest
phi[i][N] = bound[1][i]; // est
phi[0][i] = bound[2][i]; // sud
phi[N][i] = bound[3][i]; // nord
}
conv = 10.0;
mean_U0 = 0;
iter = 0;
for(int i=0; i<N+1; i++){
for(int j=0; j<N+1; j++){
mean_U0 = mean_U0 + phi[i][j];
}
}
mean_U0 = mean_U0/((N+1)*(N+1)*1.0);
while(conv > tau){
for(int i=1; i<N; i++){
for(int j=1; j<N; j++){
force = phi[i][j+1] + phi[i][j-1] + phi[i+1][j] + phi[i-1][j];
force = force + rho[i][j];
phi[i][j] = w*0.25*force + (1 - w)*phi[i][j];
}
}
mean_U = 0;
for(int i=0; i<N+1; i++){
for(int j=0; j<N+1; j++){
mean_U = mean_U + phi[i][j];
}
}
mean_U = mean_U/((N+1)*(N+1)*1.0);
conv = fabs(mean_U - mean_U0);
mean_U0 = mean_U;
iter = iter + 1;
}
return phi;
}
int main(void){
int N = 300;
double w, tau;
double **phi, **bound, **rho;
// potential
phi = (double **)malloc((N+1)*sizeof(double));
for(int i=0; i<N+1; i++){
phi[i] = (double*)calloc((N+1), sizeof(double) );
}
// boundary condition
bound = (double **)malloc((4)*sizeof(double));
for(int i=0; i<4; i++){
bound[i] = (double*)calloc((N+1), sizeof(double) );
}
for(int i=0; i<=N; i++){
// attention: the corners could be overwritten
bound[0][i] = 0; // ovest
bound[1][i] = 0; // est
bound[2][i] = -2; // sud
bound[3][i] = 2; // nord
/*
if(i <N/4){
bound[3][i] = 1; // sud
}
else if(i > 3*N/4){
bound[3][i] = 1;
}
else{
bound[3][i] = 0;
}*/
}
// Source
rho = (double **)malloc((N)*sizeof(double));
for(int i=0; i<N; i++){
rho[i] = (double*)calloc((N), sizeof(double) );
}
for(int i=-1; i<=1; i++){
for(int j=-1; j<=1; j++){
rho[7*N/11+i][N/2+j] = -1;
rho[4*N/11+i][N/2+j] = 1;
}
}
w = 1.99;
tau = 1e-8;
phi = solve_lap_sor(N, bound, rho, w, tau);
FILE *fd;
fd = fopen("lap_c.txt", "w");
if( fd==NULL ) {
perror("Erron in opening");
}
fprintf(fd, "%d \n", N);
for(int i=0; i<N+1; i++){
for(int j=0; j<N+1; j++){
fprintf(fd, "%.20f \n", phi[i][j]);
}
}
fclose(fd);
return 0;
}