-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllxf.jl
159 lines (125 loc) · 3.21 KB
/
llxf.jl
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
# Define precision
const Float = Float64
# Define state space datatype
mutable struct Q
h :: Float
hu :: Float
end
# Define spatial domain datatype
QS = Array{Q,1}
# Make Q a vector space
import Base.+, Base.-, Base.*
function +(lhs::Q, rhs::Q)
Q(lhs.h + rhs.h, lhs.hu + rhs.hu)
end
function -(lhs::Q, rhs::Q)
Q(lhs.h - rhs.h, lhs.hu - rhs.hu)
end
function *(a::Real, q::Q)
Q(a*q.h, a*q.hu)
end
"Shallow-water point-wise flux"
const G = 9.81f0
function f(q :: Q)
Q(q.hu, q.hu^2/q.h + 0.5*G*q.h^2)
end
"Shallow-water point-wise max wave speed"
function wavespeed(q :: Q)
u = q.hu / q.h
c = sqrt(G * q.h)
max(abs(u-c), abs(u+c))
end
"Finite volume skeleton"
function run(stoptime::Real, ncells::Int, choose_dt,
compute_fluxes!, apply_bcs!, apply_ics)
qs = apply_ics(ncells)
Fl = QS(ncells+2)
Fr = QS(ncells+2)
currenttime = 0
timesteps = 0
dx = Float(1000.0/ncells)
while currenttime < stoptime
apply_bcs!(qs)
dt = choose_dt(qs, dx)
compute_fluxes!(qs, Fl, Fr, ncells, dx, dt)
for x = 2:ncells+1
qs[x] -= dt/dx * (Fr[x] + Fl[x-1])
end
currenttime += dt
timesteps += 1
end
return (qs, currenttime, timesteps)
end
"Lax-Friedrichs kernel"
function lxf!(qs::QS, Fl::QS, Fr::QS,
ncells::Int, dx::Float, dt::Float)
a = dx/dt
for x = 2:ncells+2
ql,qr = qs[x-1], qs[x]
fl,fr = f(ql), f(qr)
Fr[x-1] = 0.5*((fr-fl) - a*(qr - ql))
Fl[x-1] = 0.5*((fr-fl) + a*(qr - ql))
end
end
"Local Lax-Friedrichs kernel"
function llxf!(qs::QS, Fl::QS, Fr::QS,
ncells::Int, dx::Float, dt::Float)
for x = 2:ncells+2
ql,qr = qs[x-1], qs[x]
fl,fr = f(ql), f(qr)
a = max(wavespeed(ql), wavespeed(qr))
Fr[x-1] = 0.5*((fr-fl) - a*(qr-ql))
Fl[x-1] = 0.5*((fr-fl) + a*(qr-ql))
end
end
# Timestepping strategies: (QS, dx) -> dt
function make_const_dt(dt::Float)
return (qs, dx) -> dt
end
function cfl_dt(qs::QS, dx::Real, mu::Real)
lambda = map(wavespeed,qs) |> maximum
return Float(dx / lambda * mu)
end
function make_cfl_dt(mu::Real)
return (qs, dx) -> cfl_dt(qs, dx, mu)
end
function make_lagging_dt(dt0::Real, mu::Real)
dt = dt0
function lagging_dt(qs, dx)
result = dt
dt = cfl_dt(qs, dx, mu)
return result
end
return lagging_dt
end
# Initial conditions: ncells -> QS
function gaussian_ics(ncells::Int)
qs = QS(ncells+2)
mid = ncells / 2.0 + 1
for x = 1:ncells+2
pos_rel = (mid-x) / (ncells/10.0)
qs[x] = Q(exp(-pos_rel^2) + 1, 0.0)
end
return qs
end
function breakingdam_ics(ncells::Int)
[x < ncells/2 ? Q(2,0) : Q(1,0) for x in 1:ncells+2]
end
# Boundary conditions: QS -> nothing
function outflow_bcs!(qs::QS)
qs[1] = qs[2]
qs[end] = qs[end-1]
end
function periodic_bcs!(qs::QS)
qs[1] = qs[end-1]
qs[end] = qs[2]
end
function reflecting_bcs!(qs::QS)
qs[1].h = qs[2].h
qs[1].hu = -qs[2].hu
qs[end].h = qs[end-1].h
qs[end].hu = -qs[end-1].hu
end
# Sample experiment run
qs,t,ts = run(50, 1000, make_cfl_dt(0.5),
lxf!, outflow_bcs!, breakingdam_ics)