-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytical_solution.edp
171 lines (145 loc) · 5.96 KB
/
analytical_solution.edp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
include "getARGV.idp"
include "solvers.edp"
int config = getARGV("--config", 1); // 1: Navier-Stokes, 2: Natural convection
real nu = getARGV("--nu", 1.0); // the constant kinematic viscosity of the fluid (Navier-Stokes only)
real Pr = getARGV("--Pr", 1.0); // the Prandtl number (Natural convection only)
real Ra = 1.0; // the Rayleigh number (Natural convection only)
real tf = getARGV("--tf", 1.0/320); // the final time
string filename = getARGV("--filename", "convergence");
if(config == 2)
nu = Pr;
string outputFolder = getARGV("--resu", "results\analytical_solution");
cout << "Results and figures will be saved in " << outputFolder << endl;
system("mkdir "+outputFolder);
ofstream re(outputFolder + "/" + filename +".txt");
if(config == 1)
{
re << "nu\ttf\tn\th\tdt\tu_error_2\tu_rate_2\tu_error_1\tu_rate_1\tu_error_0\tu_rate_0" << endl;
}
else if(config == 2)
{
re << "Pr^-1\ttf\t\tn\th\tdt\t 2_T_error\t2_T_rate\t2_u_error\t2_u_rate\t 1_T_error\t1_T_rate\t1_u_error\t1_u_rate\t 0_T_error\t0_T_rate\t0_u_error\t0_u_rate" << endl;
}
int n;
real real errT2 = 0, errTp2 = 0, erru2 = 0, errup2 = 0, errT1 = 0, errTp1 = 0, erru1 = 0, errup1 = 0, errT0 = 0, errTp0 = 0, erru0 = 0, errup0 = 0;
real[int] iter(9);
iter = [10, 20, 50, 100, 200, 500, 1000, 2000, 5000]; // number of time intervals
for(int j = 0; j <= 8; j++)
{
for(int k = 2; k <= 7;k ++)
{
// Generate square mesh
n = 2^k;
mesh Th = square(n, n);
// Difine finite element spaces
fespace Xh(Th, P1b);
fespace Mh(Th, P1);
// Declare initial solutions (at t = 0) of thermal and velocity
if(config == 1)
{
Xh ux = -cos(pi * x) * sin(pi * y);
Xh uy = sin(pi * x) * cos(pi * y);
// Exact solutions of velocity at final time
func uxe = -cos(pi * x) * sin(pi * y) * exp(-2 * pi^2 * tf * nu);
func uye = sin(pi * x) * cos(pi * y) * exp(-2 * pi^2 * tf * nu);
}
else if(config == 2)
{
Xh T = 0.5 * pi * sin(2 * pi * y);
Xh ux = -cos(pi * x) * sin(pi * y);
Xh uy = sin(pi * x) * cos(pi * y);
// Exact solutions of thermal and velocity at final time
func Te = 0.5 * pi * sin(2 * pi * y) * exp(-4 * pi^2 * tf * Pr);
func uxe = -cos(pi * x) * sin(pi * y) * exp(-2 * pi^2 * tf * Pr);
func uxe = sin(pi * x) * cos(pi * y) * exp(-2 * pi^2 * tf * Pr);
}
// Declare variables
Xh dcx1, dcx2, dcx3, dcx4, dcy1, dcy2, dcy3, dcy4, tbc1, tbc2, tbc3, tbc4;
Xh upx, upy, vx, vy, Tp, Tau;
Xh fx = 0, fy = 0, Q = 0;
Mh p, q;
real t;
real epsr = 1e-8;
real dt = 1. * tf / iter[j];
real alpha = 1./dt;
int i = 0;
for (i = 0;i <= iter[j]; i++)
{
t = i * dt;
cout << "Iterations:" << i << "/ " << iter[j] << " with number of mesh points: " << k << endl;
if(config == 1)
{
dcx1 = 0.0;
dcx2 = sin(pi * y) * exp(-2 * pi^2 * t * nu);
dcx3 = 0.0;
dcx4 = -sin(pi * y) * exp(-2 * pi^2 * t * nu);
dcy1 = sin(pi * x) * exp(-2 * pi^2 * t * nu);
dcy2 = 0.0;
dcy3 = -sin(pi * x) * exp(-2 * pi^2 * t * nu);
dcy4 = 0.0;
upx = ux;
upy = uy;
NavierStokes;
}
else if(config == 2)
{
Q = (1. - 1. * Pr) * 2 * pi^3 * sin(2 * pi * y) * exp((-4 * pi^2 * t) * Pr) + pi^2 * sin(pi * x) * cos(pi * y) * cos(2 * pi * y) * exp((-6 * pi * pi * t) * Pr);
tbc1 = 0.0;
tbc2 = 0.5 * pi * sin(2 * pi * y) * exp(-4 * pi^2 * t * Pr);
tbc3 = 0.0;
tbc4 = 0.5 * pi * sin(2 * pi * y) * exp(-4 * pi^2 * t * Pr);
dcx1 = 0.0;
dcx2 = sin(pi * y) * exp(-2 * pi^2 * t * Pr);
dcx3 = 0.0;
dcx4 = -sin(pi * y) * exp(-2 * pi^2 * t * Pr);
dcy1 = sin(pi * x) * exp(-2 * pi^2 * t * Pr);
dcy2 = 0.0;
dcy3 = -sin(pi * x) * exp(-2 * pi^2 * t * Pr);
dcy4 = 0.0;
Tp = T;
upx = ux;
upy = uy;
NaturalConvection;
}
}
// Calculate errors and corresponding convergence rates
Xh dux = ux - uxe, duy = uy - uye;
errup2 = erru2;
errup1 = erru1;
errup0 = erru0;
erru2 = sqrt(int2d(Th)(dux * dux + duy * duy + nu * dt * (dx(dux) * dx(dux) + dy(dux) * dy(dux) + dx(duy) * dx(duy) + dy(duy) * dy(duy))));
erru1 = sqrt(int2d(Th)(dux * dux + duy * duy + (dx(dux) * dx(dux) + dy(dux) * dy(dux) + dx(duy) * dx(duy) + dy(duy) * dy(duy))));
erru0 = sqrt(int2d(Th)(dux * dux + duy * duy));
if(config == 1)
{
// Print to output file
re << nu << "\t" << tf << "\t" << k << "\t" << Th.hmax << "\t" << dt
<< "\t" << erru2 << "\t" << log(errup2/erru2)/log(2.)
<< "\t" << erru1 << "\t" << log(errup1/erru1)/log(2.)
<< "\t" << erru0 << "\t" << log(errup0/erru0)/log(2.) << endl;
}
if(config == 2)
{
Xh dT = T - Te;
errTp2 = errT2;
errTp1 = errT1;
errTp0 = errT0;
errT2 = sqrt(int2d(Th)(dT * dT + dt * (dx(dT) * dx(dT) + dy(dT) * dy(dT))));
errT1 = sqrt(int2d(Th)(dT * dT + (dx(dT) * dx(dT) + dy(dT) * dy(dT))));
errT0 = sqrt(int2d(Th)(dT * dT));
// Print to output file
re << 1/Pr << "\t" << tf << "\t" << k << "\t" << Th.hmax << "\t" << dt
<< "\t" << errT2 << "\t" << log(errTp2/errT2)/log(2.) << "\t" << erru2 << "\t" << log(errup2/erru2)/log(2.)
<< "\t" << errT1 << "\t" << log(errTp1/errT1)/log(2.) << "\t" << erru1 << "\t" << log(errup1/erru1)/log(2.)
<< "\t" << errT0 << "\t" << log(errTp0/errT0)/log(2.) << "\t" << erru0 << "\t" << log(errup0/erru0)/log(2.) << endl;
}
}
erru2 = 0;
erru1 = 0;
erru0 = 0;
errT2 = 0;
errT1 = 0;
errT0 = 0;
re << endl;
}
re.flush;