-
Notifications
You must be signed in to change notification settings - Fork 1
/
Poisson_Solver.c
136 lines (112 loc) · 3.73 KB
/
Poisson_Solver.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "def.h"
void initialization(double **p);
void write_u(char *dir_nm,char *file_nm, double **p,double dx, double dy);
//-----------------------------------
// Poisson Solvers - Jacobi, SOR, CG
//-----------------------------------
void Jacobi(double **p,double dx, double dy, double tol,
double *tot_time, int *iter,int BC);
void SOR(double **p,double dx, double dy, double tol, double omega,
double *tot_time,int *iter,int BC);
void Conjugate_Gradient(double **p,double dx, double dy, double tol,
double *tot_time,int *iter,int BC);
//-----------------------------------
// Mathematical tools
//-----------------------------------
double func(int i, int j, double dx, double dy);
void func_anal(double **p, int row_num, int col_num, double dx, double dy);
void error_rms(double **p, double **p_anal, double *err);
void poisson_solver(double **u, double **u_anal, double tol, double omega,
int BC, int method, char *dir_name){
char *file_name ;
int iter = 0;
double Lx = 1.0, Ly = 1.0;
double dx, dy, err = 0, tot_time = 0;
dx = Lx/(ROW-1);
dy = Ly/(COL-1);
//-----------------------------
// Analytic Solutions
//-----------------------------
file_name = "Analytic_solution.plt";
func_anal(u_anal,ROW,COL,dx,dy);
write_u(dir_name,file_name,u_anal,dx,dy);
switch (method) {
case 1 :
//-----------------------------
// Jacobi Method
//-----------------------------
initialization(u);
Jacobi(u,dx,dy,tol,&tot_time,&iter,BC);
error_rms(u,u_anal,&err);
printf("Jacobi Method - Error : %e, Iteration : %d, Time : %f s \n",err,iter,tot_time);
file_name = "Jacobi_result.plt";
write_u(dir_name,file_name,u,dx,dy);
break;
case 2 :
//-----------------------------
// SOR Method
//-----------------------------
initialization(u);
SOR(u,dx,dy,tol,omega,&tot_time,&iter,BC);
error_rms(u,u_anal,&err);
printf("SOR Method - Error : %e, Iteration : %d, Time : %f s \n",err,iter,tot_time);
file_name = "SOR_result.plt";
write_u(dir_name,file_name,u,dx,dy);
break;
case 3 :
//-----------------------------
// Conjugate Gradient Method
//-----------------------------
initialization(u);
Conjugate_Gradient(u,dx,dy,tol,&tot_time,&iter,BC);
error_rms(u,u_anal,&err);
printf("CG method - Error : %e, Iteration : %d, Time : %f s \n",err,iter,tot_time);
file_name = "CG_result.plt";
write_u(dir_name,file_name,u,dx,dy);
break;
}
}
double func(int i,int j,double dx,double dy)
{
return sin(pi*i*dx)*cos(pi*j*dy);
}
void initialization(double **p)
{
int i,j;
for (i=0;i<ROW;i++){
for (j=0;j<COL;j++){
p[i][j] = 0; }}
}
void error_rms(double **p, double **p_anal, double *err)
{
int i,j;
for (i=0;i<ROW;i++){
for (j=0;j<COL;j++){
*err = *err + pow(p[i][j] -p_anal[i][j],2);
}
}
*err = sqrt(*err)/(ROW*COL);
}
void func_anal(double **p, int row_num, int col_num, double dx, double dy)
{
int i,j;
for (i=0;i<row_num;i++){
for (j=0;j<col_num;j++){
p[i][j] = -1/(2*pow(pi,2))*sin(pi*i*dx)*cos(pi*j*dy); }}
}
void write_u(char *dir_nm,char *file_nm, double **p,double dx, double dy)
{
FILE* stream;
int i,j;
char file_path[50];
sprintf(file_path,"%s%s",dir_nm,file_nm);
stream=fopen(file_path,"w");
fprintf(stream,"ZONE I=%d J=%d \n",ROW,COL);
for (i=0;i<ROW;i++){
for(j=0;j<COL;j++){
fprintf(stream,"%f %f %f \n",i*dx,j*dy,p[i][j]); }}
fclose(stream);
}