-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_rusanov.f90
283 lines (239 loc) · 7.22 KB
/
euler_rusanov.f90
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
!=======================================================================
! This program solves the Euler equations with the Godunov Method
! using the Rusanov Fluxes
!=======================================================================
!=======================================================================
! This module contains global variables
module globals
implicit none
!
! This is the number of points used to discretize X
integer, parameter :: nx=100
! Here we set the extent of X and calculate $\Delta x$
real, parameter :: xmax=1, dx=xmax/float(nx)
real, parameter :: gamma=1.4
! This is a vector that contains u(x)
real :: u(3,0:nx+1), prim(3,0:nx+1)
!
end module globals
!=======================================================================
! main program
program euler_lax
use globals
implicit none
! declaration of some variables needed by the main program
real :: time, dt ! t, $\Delta t$
real, parameter :: tmax= .2 ! maximumn integration time
real, parameter :: dtprint=0.01 ! interval between outputs
real :: tprint ! time of next output
integer :: itprint ! number of current output
! This subroutine generates the initial conditions
call initconds(time, tprint, itprint)
! main loop, iterate until maximum time is reached
do while (time.lt.tmax)
! updates the primitives
call u2prim(nx,gamma,u,prim)
! output at tprint intervals
if(time.ge.tprint) then
write(*,*) time,tmax,dt
call output(itprint)
tprint=tprint+dtprint
itprint=itprint+1
end if
! Obtain the $\Delta t$ allowed by the CFL criterium
call timestep(dt)
!
! Integrate u fom t to t+dt
call tstep(dt,time)
! time counter increases
time=time+dt
end do
stop
end program euler_lax
!=======================================================================
! generates initial condition
subroutine initconds(time, tprint, itprint)
use globals
implicit none
real, intent(out) :: time, tprint
integer, intent (out) :: itprint
! The initial condition imposed here is the Sod tube test
integer :: i
real :: x
! fill the vector u
do i=0,nx+1
x=float(i)*dx ! obtain the position $x_i$
if (x < 0.5 ) then
u(1,i)=1.0
u(2,i)=0.0
u(3,i)=1.0/(gamma-1.)
else
u(1,i)=.125
u(2,i)=0.0
u(3,i)=0.1/(gamma-1.)
end if
!
if( (x-0.5*dx <= 0.5).and.(x+0.5*dx >= 0.5) ) then
u(1,i)=1.125/2.
u(2,i)=0.0
u(3,i)=1.1/2./(gamma-1.)
end if
end do
! reset the counters and time to 0
time=0.
tprint=0.
itprint=0
return
end subroutine initconds
!=======================================================================
! computes the primitives as a function of the Us
subroutine u2prim(nx,gamma,u,prim)
implicit none
integer, intent(in) :: nx
real, intent(in) :: gamma
real , intent(in) :: u(3,0:nx+1)
real , intent(out) :: prim(3,0:nx+1)
integer :: i
real :: rho, vx
do i=0,nx+1
prim(1,i) = u(1,i)
prim(2,i) = u(2,i)/u(1,i)
prim(3,i) = (u(3,i)-0.5*prim(1,i)*prim(2,i)**2)*(gamma-1.)
end do
return
end subroutine u2prim
!=======================================================================
! output to file
subroutine output(itprint)
use globals
implicit none
integer, intent(in) :: itprint
character (len=20) file1
integer :: i
real :: rho,vx,P
! open output file
write(file1,'(a,i2.2,a)') 'euler_god-',itprint,'.dat'
open(unit=10,file=file1,status='unknown')
! writes x and rho, u(=vx) and P
do i=1,nx
write(10,*) float(i)*dx,prim(:,i)
end do
! closes output file
close(10)
return
end subroutine output
!=======================================================================
! computes the timestep allowed by the CFL criterium
subroutine timestep(dt)
use globals
implicit none
real, intent(out) ::dt
! Courant number =0.9
real, parameter :: Co=0.9
real :: del, cs
integer :: i
del=1E30
do i=0,nx+1
cs=sqrt(gamma*prim(3,i)/prim(1,i))
del=min( del,dx/(abs(prim(2,i))+cs) )
end do
dt=Co*del
return
end subroutine timestep
!=======================================================================
! integration from t to t+dt with the method of Lax
subroutine tstep(dt,time)
use globals
implicit none
real, intent(in) :: dt, time
real :: up(3,0:nx+1), f(3,0:nx+1)
real :: dtx
integer :: i
! obtain the fluxes
call rusanov_fluxes(nx,gamma,prim,f)
! Here is the upwind Godunov method
dtx=dt/dx
do i=1,nx
up(:,i)=u(:,i)-dtx*(f(:,i)-f(:,i-1))
end do
! Boundary conditions to the U^n+1
call boundaries(nx,up)
! copy the up to the u
u(:,:)=up(:,:)
return
end subroutine tstep
!=======================================================================
! computes the Rusanov fluxes on the entire domain
subroutine rusanov_fluxes(nx,gamma,prim,f)
implicit none
integer, intent(in) :: nx
real, intent(in) :: gamma
real, intent(in) :: prim(3,0:nx+1)
real, intent(out):: f(3,0:nx+1)
integer :: i
real :: priml(3), primr(3), ff(3)
do i=0,nx
! these are the Left and Right states that enter
! the Riemann problem
primL(:)= prim(:,i )
primR(:)= prim(:,i+1)
call prim2rus(gamma, primL, primR, ff)
f(:,i)=ff(:)
end do
end subroutine rusanov_fluxes
!=======================================================================
! Obtain the Rusanov fluxes
subroutine prim2rus(gamma,primL,primR,ff)
implicit none
real, intent(in) :: gamma, primL(3), primR(3)
real, intent(out):: ff(3)
real :: fL(3), fR(3),ur(3), ul(3)
real :: lambda, vmax
lambda=max(abs(primL(2)) + sqrt(gamma*primL(3)/primL(1)) &
, abs(primR(2)) + sqrt(gamma*primR(3)/primR(1)) )
call eulerfluxes(gamma,primL,fL)
call eulerfluxes(gamma,primR,fR)
call prim2u(gamma, primL, uL)
call prim2u(gamma, primR, uR)
ff(:)=0.5*( fl(:)+fr(:) -lambda*( ur(:)-ul(:) ) )
return
end subroutine prim2rus
!=======================================================================
! computes the euler fluxes, one cell
subroutine eulerfluxes(gamma,pp,ff)
implicit none
real, intent(in) :: gamma, pp(3)
real, intent(out):: ff(3)
ff(1)=pp(1)*pp(2)
ff(2)=pp(1)*pp(2)**2+pp(3)
ff(3)=pp(2)*(0.5*pp(1)*pp(2)**2+gamma*pp(3)/(gamma-1.) )
return
end subroutine eulerfluxes
!=======================================================================
! computes the primitives as a function of the Us, only in one cell
subroutine prim2u(gamma,pp,uu)
implicit none
real, intent(in) :: gamma
real , intent(in) :: pp(3)
real , intent(out) :: uu(3)
uu(1) = pp(1)
uu(2) = pp(1)*pp(2)
uu(3) = 0.5*pp(1)*pp(2)**2 +pp(3)/(gamma-1.)
return
end subroutine prim2u
!=======================================================================
! Set boundary conditions
subroutine boundaries(nx,u)
implicit none
integer, intent(in) :: nx
real, intent(out):: u(3,0:nx+1)
integer :: i
! Periodic boundary conditions
!u(:,0 )=u(:,nx)
!u(:,nx+1)=u(:,1)
! open boundary conditions
u(:,0)=u(:,1)
u(:,nx+1)=u(:,nx)
return
end subroutine boundaries
!=======================================================================