-
Notifications
You must be signed in to change notification settings - Fork 0
/
kfvs_euler.m
179 lines (164 loc) · 4.57 KB
/
kfvs_euler.m
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
172
173
174
175
176
177
178
179
% Solve 1D Euler equation
% nx - number of spatial grids
% cfl - CFL number
% t_F - final time
% rhol - initial density on the left
% rhor - initial density on the right
% rhoul - initial momentum on the left
% rhour - initial momentum on the right
% rhoEl - initial total energy on the left
% rhoEr - initial total energy on the right
% rk - 1, Kinetic flux 2, Roe flux
% fre - output frequency
% called by MATLAB command line: kfvs_euler(500, 0.4, 0.2, 1., 0.1, 0., 0., 1., 0.25, 1, 100)
% Author: Luo Li, [email protected]
function kfvs_euler(nx,cfl,t_F,rhol,rhor,rhoul,rhour,rhoEl,rhoEr,rk,fre)
gammer=1.4;
itmax=100000; % maximum of time steps
m=1; % number of ghost points in x
xmin=0.;
xmax=1.;
dx=(xmax-xmin)/nx; % space step length
x=zeros(nx);
u0=zeros(nx+2*m,3);
u1=zeros(nx+2*m,3);
rho_=zeros(nx);
u_=zeros(nx);
p_=zeros(nx);
[x_exact,rho_exact,u_exact,p_exact,e_exact]=textread('./Euler1D/e1rpex.out','%f%f%f%f%f','headerlines',1);
for i=1:nx
x(i)=xmin+0.5*dx+(i-1)*dx;
end
% initialize
for i=1:nx
if x(i)<=0.5*(xmax-xmin)
u0(m+i,1)=rhol;
u0(m+i,2)=rhoul;
u0(m+i,3)=rhoEl;
else
u0(m+i,1)=rhor;
u0(m+i,2)=rhour;
u0(m+i,3)=rhoEr;
end
end
% apply boundary condition
for i=1:m
% left outflow
u0(i,:)=u0(m+1,:);
% right outflow
u0(m+nx+i,:)=u0(m+nx,:);
end
% plot
for i=1:nx
rho_(i)=u0(m+i,1);
u_(i)=u0(m+i,2)/u0(m+i,1);
p_(i)=(gammer-1.)*(u0(m+i,3)-0.5*u0(m+i,2)*u0(m+i,2)/u0(m+i,1));
end
plot(x,rho_,'b--','LineWidth',1); hold on;
plot(x_exact,rho_exact,'b-','LineWidth',2); hold on;
xlabel('x'); ylabel('density');
axis([0,1,-0.05,1.2]);
figure;
plot(x,u_,'k--','LineWidth',1); hold on;
plot(x_exact,u_exact,'k-','LineWidth',2); hold on;
xlabel('x'); ylabel('velocity');
axis([0,1,-0.05,0.55]);
figure;
plot(x,p_,'r--','LineWidth',1); hold on;
plot(x_exact,p_exact,'r-','LineWidth',2); hold on;
xlabel('x'); ylabel('pressure');
axis([0,1,-0.05,0.45]);
pause;
% start time stepping
t=0.0;
for it=1:itmax
rhomaxx=0.;
for i=1:nx
% primitive variables
rho=u0(m+i,1);
rhou=u0(m+i,2);
rhoE=u0(m+i,3);
u=rhou/rho;
p=(gammer-1.)*(rhoE-0.5*rhou*rhou/rho);
% sound speed
c = sqrt(gammer*p/rho);
% evaluate the maximum of velocity
rhox = max(abs(u+c),abs(u-c));
rhomaxx = max(rhomaxx,rhox);
end
% compute time step length
dt=cfl*dx/rhomaxx;
% final time step length
if t<t_F && t+dt>t_F
dt=t_F-t;
end
t=t+dt
% if reach time limit, stop time stepping
if t+dt>t_F || it==itmax
% plot
for i=1:nx
rho_(i)=u0(m+i,1);
u_(i)=u0(m+i,2)/u0(m+i,1);
p_(i)=(gammer-1.)*(u0(m+i,3)-0.5*u0(m+i,2)*u0(m+i,2)/u0(m+i,1));
end
plot(x,rho_,'b--','LineWidth',1); hold on;
plot(x_exact,rho_exact,'b-','LineWidth',2); hold on;
xlabel('x'); ylabel('density');
axis([0,1,-0.05,1.2]);
figure;
plot(x,u_,'k--','LineWidth',1); hold on;
plot(x_exact,u_exact,'k-','LineWidth',2); hold on;
xlabel('x'); ylabel('velocity');
axis([0,1,-0.05,0.55]);
figure;
plot(x,p_,'r--','LineWidth',1); hold on;
plot(x_exact,p_exact,'r-','LineWidth',2); hold on;
xlabel('x'); ylabel('pressure');
axis([0,1,-0.05,0.45]);
break;
end
lambdax=dt/dx;
% update solution
if rk==1
for i=1:nx
u1(m+i,:)=u0(m+i,:)+...
lambdax*(k_f_Euler(u0(m+i-1,:),u0(m+i,:))-k_f_Euler(u0(m+i,:),u0(m+i+1,:)));
end
else
for i=1:nx
u1(m+i,:)=u0(m+i,:)+...
lambdax*(r_f_euler(u0(m+i-1,:),u0(m+i,:))-r_f_euler(u0(m+i,:),u0(m+i+1,:)));
end
end
% apply boundary condition
for i=1:m
% left outflow
u1(i,:)=u1(m+1,:);
% right outflow
u1(m+nx+i,:)=u1(m+nx,:);
end
% swap
u0(:,:,:)=u1(:,:,:);
if mod(it,fre)==0 % draw frequency
for i=1:nx
rho_(i)=u0(m+i,1);
u_(i)=u0(m+i,2)/u0(m+i,1);
p_(i)=(gammer-1.)*(u0(m+i,3)-0.5*u0(m+i,2)*u0(m+i,2)/u0(m+i,1));
end
plot(x,rho_,'b--','LineWidth',1); hold on;
plot(x_exact,rho_exact,'b-','LineWidth',2); hold on;
xlabel('x'); ylabel('density');
axis([0,1,-0.05,1.2]);
figure;
plot(x,u_,'k--','LineWidth',1); hold on;
plot(x_exact,u_exact,'k-','LineWidth',2); hold on;
xlabel('x'); ylabel('velocity');
axis([0,1,-0.05,0.55]);
figure;
plot(x,p_,'r--','LineWidth',1); hold on;
plot(x_exact,p_exact,'r-','LineWidth',2); hold on;
xlabel('x'); ylabel('pressure');
axis([0,1,-0.05,0.45]);
pause;
end
end