This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
ShallowWaterModel.jl
331 lines (273 loc) · 6.47 KB
/
ShallowWaterModel.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
module ShallowWater
export ShallowWaterModel
using StaticArrays
using ...MPIStateArrays: MPIStateArray
using LinearAlgebra: dot, Diagonal
using CLIMAParameters.Planet: grav
using ..Ocean
using ...VariableTemplates
using ...Mesh.Geometry
using ...DGMethods
using ...DGMethods.NumericalFluxes
using ...BalanceLaws
using ..Ocean: kinematic_stress, coriolis_parameter
import ...DGMethods.NumericalFluxes: update_penalty!
import ...BalanceLaws:
vars_state,
init_state_prognostic!,
init_state_auxiliary!,
compute_gradient_argument!,
compute_gradient_flux!,
flux_first_order!,
flux_second_order!,
source!,
wavespeed,
boundary_conditions,
boundary_state!
import ..Ocean:
ocean_init_state!,
ocean_init_aux!,
ocean_boundary_state!,
_ocean_boundary_state!
using ...Mesh.Geometry: LocalGeometry
×(a::SVector, b::SVector) = StaticArrays.cross(a, b)
⋅(a::SVector, b::SVector) = StaticArrays.dot(a, b)
⊗(a::SVector, b::SVector) = a * b'
abstract type TurbulenceClosure end
struct LinearDrag{L} <: TurbulenceClosure
λ::L
end
struct ConstantViscosity{L} <: TurbulenceClosure
ν::L
end
"""
ShallowWaterModel <: BalanceLaw
A `BalanceLaw` for shallow water modeling.
write out the equations here
# Usage
ShallowWaterModel(problem)
"""
struct ShallowWaterModel{C, PS, P, T, A, FT} <: BalanceLaw
param_set::PS
problem::P
coupling::C
turbulence::T
advection::A
c::FT
fₒ::FT
β::FT
function ShallowWaterModel{FT}(
param_set::PS,
problem::P,
turbulence::T,
advection::A;
coupling::C = Uncoupled(),
c = FT(0), # m/s
fₒ = FT(1e-4), # Hz
β = FT(1e-11), # Hz / m
) where {FT <: AbstractFloat, PS, P, T, A, C}
return new{C, PS, P, T, A, FT}(
param_set,
problem,
coupling,
turbulence,
advection,
c,
fₒ,
β,
)
end
end
SWModel = ShallowWaterModel
function vars_state(m::SWModel, ::Prognostic, T)
@vars begin
η::T
U::SVector{2, T}
end
end
function init_state_prognostic!(m::SWModel, state::Vars, aux::Vars, localgeo, t)
ocean_init_state!(m, m.problem, state, aux, localgeo, t)
end
function vars_state(m::SWModel, ::Auxiliary, T)
@vars begin
y::T
Gᵁ::SVector{2, T} # integral of baroclinic tendency
Δu::SVector{2, T} # reconciliation Δu = 1/H * (Ū - ∫u)
end
end
function init_state_auxiliary!(
m::SWModel,
state_auxiliary::MPIStateArray,
grid,
direction,
)
init_state_auxiliary!(
m,
(m, A, tmp, geom) -> ocean_init_aux!(m, m.problem, A, geom),
state_auxiliary,
grid,
direction,
)
end
function vars_state(m::SWModel, ::Gradient, T)
@vars begin
∇U::SVector{2, T}
end
end
function compute_gradient_argument!(
m::SWModel,
f::Vars,
q::Vars,
α::Vars,
t::Real,
)
compute_gradient_argument!(m.turbulence, f, q, α, t)
end
compute_gradient_argument!(::LinearDrag, _...) = nothing
@inline function compute_gradient_argument!(
T::ConstantViscosity,
f::Vars,
q::Vars,
α::Vars,
t::Real,
)
f.∇U = q.U
return nothing
end
function vars_state(m::SWModel, ::GradientFlux, T)
@vars begin
ν∇U::SMatrix{3, 2, T, 6}
end
end
function compute_gradient_flux!(
m::SWModel,
σ::Vars,
δ::Grad,
q::Vars,
α::Vars,
t::Real,
)
compute_gradient_flux!(m, m.turbulence, σ, δ, q, α, t)
end
compute_gradient_flux!(::SWModel, ::LinearDrag, _...) = nothing
@inline function compute_gradient_flux!(
::SWModel,
T::ConstantViscosity,
σ::Vars,
δ::Grad,
q::Vars,
α::Vars,
t::Real,
)
ν = Diagonal(@SVector [T.ν, T.ν, -0])
∇U = δ.∇U
σ.ν∇U = -ν * ∇U
return nothing
end
@inline function flux_first_order!(
m::SWModel,
F::Grad,
q::Vars,
α::Vars,
t::Real,
direction,
)
U = @SVector [q.U[1], q.U[2], -0]
η = q.η
H = m.problem.H
Iʰ = @SMatrix [
1 -0
-0 1
-0 -0
]
F.η += U
F.U += grav(parameter_set(m)) * H * η * Iʰ
advective_flux!(m, m.advection, F, q, α, t)
return nothing
end
advective_flux!(::SWModel, ::Nothing, _...) = nothing
@inline function advective_flux!(
m::SWModel,
::NonLinearAdvectionTerm,
F::Grad,
q::Vars,
α::Vars,
t::Real,
)
U = q.U
H = m.problem.H
V = @SVector [U[1], U[2], -0]
F.U += 1 / H * V ⊗ U
return nothing
end
function flux_second_order!(
m::SWModel,
G::Grad,
q::Vars,
σ::Vars,
::Vars,
α::Vars,
t::Real,
)
flux_second_order!(m, m.turbulence, G, q, σ, α, t)
end
flux_second_order!(::SWModel, ::LinearDrag, _...) = nothing
@inline function flux_second_order!(
::SWModel,
::ConstantViscosity,
G::Grad,
q::Vars,
σ::Vars,
α::Vars,
t::Real,
)
G.U += σ.ν∇U
return nothing
end
@inline wavespeed(m::SWModel, n⁻, q::Vars, α::Vars, t::Real, direction) = m.c
@inline function source!(
m::SWModel{P},
S::Vars,
q::Vars,
d::Vars,
α::Vars,
t::Real,
direction,
) where {P}
# f × u
U, V = q.U
f = coriolis_parameter(m, m.problem, α.y)
S.U -= @SVector [-f * V, f * U]
forcing_term!(m, m.coupling, S, q, α, t)
linear_drag!(m.turbulence, S, q, α, t)
return nothing
end
@inline function forcing_term!(m::SWModel, ::Uncoupled, S, Q, A, t)
S.U += kinematic_stress(m.problem, A.y)
return nothing
end
linear_drag!(::ConstantViscosity, _...) = nothing
@inline function linear_drag!(T::LinearDrag, S::Vars, q::Vars, α::Vars, t::Real)
λ = T.λ
U = q.U
S.U -= λ * U
return nothing
end
boundary_conditions(shallow::SWModel) = shallow.problem.boundary_conditions
"""
boundary_state!(nf, ::SWModel, args...)
applies boundary conditions for the hyperbolic fluxes
dispatches to a function in OceanBoundaryConditions.jl based on bytype defined by a problem such as SimpleBoxProblem.jl
"""
@inline function boundary_state!(nf, bc, shallow::SWModel, args...)
return _ocean_boundary_state!(nf, bc, shallow, args...)
end
"""
ocean_boundary_state!(nf, bc::OceanBC, ::SWModel)
splits boundary condition application into velocity
"""
@inline function ocean_boundary_state!(nf, bc::OceanBC, m::SWModel, args...)
return ocean_boundary_state!(nf, bc.velocity, m, m.turbulence, args...)
end
include("bc_velocity.jl")
end