-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfluxcalc.f90
243 lines (208 loc) · 7.13 KB
/
fluxcalc.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
!=====================================================
!
! Copyright 2012-2016 Arno Mayrhofer
! This program is licensed under the GNU General Public License.
!
! This file is part of Gees.
!
! Gees is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! Gees is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with Gees. If not, see <http://www.gnu.org/licenses/>.
!
!=====================================================
module mod_fluxcalc
use mod_helper
! physical parameters
real(dp), parameter :: rho0 = 1d3
real(dp), parameter :: xi = 7d0
! public variables
public :: dp
! public functions
public :: fluxcalc, bcs
! private functions
private :: phi, ev, p_eos
contains
subroutine fluxcalc(u, f, nx, c0)
! Task:
! Compute flux at midpoints
! Input:
! u - state vector at center
! nx - number of gridpoints
! c0 - speed of sound
! Output:
! f - fluxes at midpoints
implicit none
integer, intent(in) :: nx
real(dp), dimension(-1:nx+1,2), intent(inout) :: u
real(dp), dimension(nx,2), intent(inout) :: f
real(dp), intent(in) :: c0
integer :: i, j
real(dp), dimension(2) :: ur, ul
real(dp) :: a, pl, pr, nom, denom, ri, rim1
real(dp), dimension(4) :: lam
! calculate flux at midpoints, f(i) = f_{i-1/2}
do i=1,nx
do j=1,2
! calculate r_{i} = \frac{u_i-u_{i-1}}{u_{i+1}-u_i}
nom = (u(i ,j)-u(i-1,j))
denom = (u(i+1,j)-u(i ,j))
! make sure division by 0 does not happen
if (abs(nom) < 1d-14) then ! nom = 0
nom = 0d0
denom = 1d0
elseif (nom > 1d-14 .and. abs(denom) < 1d-14) then ! nom > 0 => r = \inf
nom = 1d14
denom = 1d0
elseif (nom < -1d-14 .and. abs(denom) < 1d-14) then ! nom < 0 => r = 0
nom = -1d14
denom = 1d0
end if
ri = nom/denom
! calculate r_{i-1} = \frac{u_{i-1}-u_{i-2}}{u_i-u_{i-1}}
nom = (u(i-1,j)-u(i-2,j))
denom = (u(i ,j)-u(i-1,j))
! make sure division by 0 does not happen
if (abs(nom) < 1d-14) then
nom = 0d0
denom = 1d0
elseif (nom > 1d-14 .and. abs(denom) < 1d-14) then
nom = 1d14
denom = 1d0
elseif (nom < -1d-14.and.abs(denom) < 1d-14) then
nom = -1d14
denom = 1d0
end if
rim1 = nom/denom
! u^l_{i-1/2} = u_{i-1} + 0.5*phi(r_{i-1})*(u_i-u_{i-1})
ul(j) = u(i-1,j)+0.5d0*phi(rim1)*(u(i ,j)-u(i-1,j))
! u^r_{i-1/2} = u_i + 0.5*phi(r_i)*(u_{i+1}-u_i)
ur(j) = u(i,j) -0.5d0*phi(ri )*(u(i+1,j)-u(i ,j))
end do
! calculate eigenvalues of \frac{\partial F}{\parial u} at u^l_{i-1/2}
lam(1) = ev(ul,c0, 1d0)
lam(2) = ev(ul,c0,-1d0)
! calculate eigenvalues of \frac{\partial F}{\parial u} at u^r_{i-1/2}
lam(3) = ev(ur,c0, 1d0)
lam(4) = ev(ur,c0,-1d0)
! local propagation speed =
! max spectral radius (= max eigenvalue of dF/du) of flux Jacobians at (i-1/2)
a = maxval(abs(lam),dim=1)
! calculate pressure via equation of state:
pr = p_eos(ur(1),c0)
pl = p_eos(ul(1),c0)
! calculate flux based on the Kurganov and Tadmor central scheme
! F_{i-1/2} = 0.5*(F(u^r_{i-1/2})+F(u^l_{i-1/2}) - a*(u^r_{i-1/2} - u^l_{i-1/2}))
! F_1 = rho * v = u_2
f(i,1) = 0.5d0*(ur(2)+ul(2)-a*(ur(1)-ul(1)))
! F_2 = p + rho * v**2 = p + \frac{u_2**2}{u_1}
f(i,2) = 0.5d0*(pr+ur(2)**2/ur(1)+pl+ul(2)**2/ul(1)-a*(ur(2)-ul(2)))
end do
end subroutine fluxcalc
real(dp) function phi(r)
! Task:
! Compute limiter function for fluxes using the Ospre limiter
! Input:
! r - ratio of successive gradients
! Output:
! phi - limiter
implicit none
real(dp), intent(in) :: r
phi = 0d0
! ospre flux limiter phi(r) = \frac{1.5*(r^2+r)}{r^2+r+1}
if (r > 0d0) then
phi = 1.5d0*(r**2+r)/(r**2+r+1d0)
end if
! van leer
! phi = (r+abs(r))/(1d0+abs(r))
end function phi
real(dp) function ev(u,c0,sgn)
! Task:
! Compute the eigenvalues of the flux jacobian
! Input:
! u - state vector at midpoint (left or right approximation)
! c0 - speed of sound
! sgn - sign of the root
! Output:
! ev - the eigenvalue
implicit none
real(dp), dimension(2), intent(inout) :: u
real(dp), intent(in) :: sgn, c0
! calculate root of characteristic equation
! \lambda = v \pm c_0 (\frac{\rho}{\rho_0})^{(xi-1)/2}
ev = u(2)/u(1) + sgn*c0*(u(1)/rho0)**((xi-1d0)/2d0)
return
end function ev
real(dp) function p_eos(rho, c0)
! Task:
! Compute the pressure via the equation of state from the density
! Formula: p = \frac{\rho_0 c_0^2}{\xi} [(\frac{\rho}{\rho_0})^\xi - 1],
! where \rho_0 (=1000) is the reference density, c_0 the speed of sound and \xi the
! polytropic index (=7 for water).
! Input:
! rho - density
! c0 - speed of sound
! Output:
! p - pressure
implicit none
real(dp), intent(in) :: rho, c0
p_eos = rho0*c0**2d0/xi*((rho/rho0)**xi-1d0)
return
end function p_eos
subroutine bcs(u,p,v,nx,c0)
! Task:
! Update the velocity and pressure globally from the state vector
! Compute the boundary conditions on the ghost cells
! Input:
! u - state vector
! nx - number of gridpoints
! c0 - speed of sound
! Output:
! p - pressure
! v - velocity
implicit none
integer, intent(in) :: nx
real(dp), intent(in) :: c0
real(dp), dimension(-1:nx+1), intent(inout) :: p,v
real(dp), dimension(-1:nx+1,2), intent(inout) :: u
integer :: i
! calculate velocity and pressure
do i=1,nx-1
! v = u_2 / u_1
v(i) = u(i,2)/u(i,1)
! p = \frac{rho_0 c0^2}{xi}*((\frac{\rho}{\rho_0})^xi-1), (xi=7, rho_0=1d3)
p(i) = p_eos(u(i,1),c0)
end do
! calculate boundary conditions
! note: for periodic boundary conditions set
! f(0) = f(nx-1), f(-1) = f(nx-2), f(nx) = f(1), f(nx+1) = f(2) \forall f
! for rho: d\rho/dn = 0 using second order extrapolation
u(0,1) = (4d0*u(1,1)-u(2,1))/3d0
u(-1,1) = u(1,1)
u(nx,1) = (4d0*u(nx-1,1)-u(nx-2,1))/3d0
u(nx+1,1) = u(nx-1,1)
! for p: dp/dn = 0 (thus can use EOS)
p(0) = p_eos(u(0,1),c0)
p(-1) = p_eos(u(-1,1),c0)
p(nx) = p_eos(u(nx,1),c0)
p(nx+1) = p_eos(u(nx+1,1),c0)
! for v: v = 0 using second order extrapolation
v(0) = 0d0
v(-1) = v(2)-3d0*v(1)
v(nx) = 0d0
v(nx+1) = v(nx-2)-3d0*v(nx-1)
! for rho*v
u(0,2) = u(0,1)*v(0)
u(-1,2) = u(-1,1)*v(-1)
u(nx,2) = u(nx,1)*v(nx)
u(nx+1,2) = u(nx+1,1)*v(nx+1)
end subroutine bcs
end module mod_fluxcalc