-
Notifications
You must be signed in to change notification settings - Fork 9
/
boundary_conditions.jl
530 lines (473 loc) · 15.6 KB
/
boundary_conditions.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
abstract type AbstractBC end
"""
PeriodicBC()
Periodic boundary conditions. Must be periodic on both sides.
"""
struct PeriodicBC <: AbstractBC end
"""
DirichletBC()
No slip boundary conditions, where all velocity components are zero.
DirichletBC(u, dudt)
Dirichlet boundary conditions for the velocity, where `u[1] = (x..., t) ->
u1_BC` up to `u[d] = (x..., t) -> ud_BC`, where `d` is the dimension.
To make the pressure the same order as velocity, also provide `dudt`.
"""
struct DirichletBC{F,G} <: AbstractBC
u::F
dudt::G
end
DirichletBC() = DirichletBC(nothing, nothing)
DirichletBC(u) = DirichletBC(u, nothing)
"""
SymmetricBC()
Symmetric boundary conditions.
The parallel velocity and pressure is the same at each side of the boundary.
The normal velocity is zero.
"""
struct SymmetricBC <: AbstractBC end
"""
PressureBC()
Pressure boundary conditions.
The pressure is prescribed on the boundary (usually an "outlet").
The velocity has zero Neumann conditions.
Note: Currently, the pressure is prescribed with the constant value of
zero on the entire boundary.
"""
struct PressureBC <: AbstractBC end
function ghost_a! end
function ghost_b! end
# Add opposite boundary ghost volume
# Do everything in first function call for periodic
function ghost_a!(::PeriodicBC, x)
Δx_a = x[2] - x[1]
Δx_b = x[end] - x[end-1]
pushfirst!(x, x[1] - Δx_b)
push!(x, x[end] + Δx_a)
end
ghost_b!(::PeriodicBC, x) = nothing
# Add infinitely thin boundary volume
ghost_a!(::DirichletBC, x) = pushfirst!(x, x[1])
ghost_b!(::DirichletBC, x) = push!(x, x[end])
# Duplicate boundary volume
ghost_a!(::SymmetricBC, x) = pushfirst!(x, x[1] - (x[2] - x[1]))
ghost_b!(::SymmetricBC, x) = push!(x, x[end] + (x[end] - x[end-1]))
# Add infinitely thin boundary volume
# On the left, we need to add two ghost volumes to have a normal component at
# the left of the first ghost volume
ghost_a!(::PressureBC, x) = pushfirst!(x, x[1], x[1])
ghost_b!(::PressureBC, x) = push!(x, x[end])
"""
offset_u(bc, isnormal, isright)
Number of non-DOF velocity components at boundary.
If `isnormal`, then the velocity is normal to the boundary, else parallel.
If `isright`, it is at the end/right/rear/top boundary, otherwise beginning.
"""
function offset_u end
"""
offset_p(bc)
Number of non-DOF pressure components at boundary.
"""
function offset_p end
offset_u(::PeriodicBC, isnormal, isright) = 1
offset_p(::PeriodicBC, isright) = 1
offset_u(::DirichletBC, isnormal, isright) = 1 + isnormal * isright
offset_p(::DirichletBC, isright) = 1
offset_u(::SymmetricBC, isnormal, isright) = 1 + isnormal * isright
offset_p(::SymmetricBC, isright) = 1
offset_u(::PressureBC, isnormal, isright) = 1 + !isnormal * !isright
offset_p(::PressureBC, isright) = 1 + !isright
function apply_bc_u! end
function apply_bc_p! end
function apply_bc_temp! end
apply_bc_u(u, t, setup; kwargs...) = apply_bc_u!(copy.(u), t, setup; kwargs...)
apply_bc_p(p, t, setup; kwargs...) = apply_bc_p!(copy(p), t, setup; kwargs...)
apply_bc_temp(temp, t, setup; kwargs...) = apply_bc_temp!(copy(temp), t, setup; kwargs...)
ChainRulesCore.rrule(::typeof(apply_bc_u), u, t, setup; kwargs...) = (
apply_bc_u(u, t, setup; kwargs...),
# With respect to (apply_bc_u, u, t, setup)
φbar -> (
NoTangent(),
# Important: identity operator should be part of `apply_bc_u_pullback`,
# but is actually implemented via the `copy` below instead.
apply_bc_u_pullback!(Tangent{typeof(u)}(copy.((φbar...,))...), t, setup; kwargs...),
NoTangent(),
NoTangent(),
),
)
ChainRulesCore.rrule(::typeof(apply_bc_p), p, t, setup) = (
apply_bc_p(p, t, setup),
# With respect to (apply_bc_p, p, t, setup)
φbar -> (
NoTangent(),
apply_bc_p_pullback!(
# Important: identity operator should be part of `apply_bc_p_pullback`,
# but is actually implemented via the `copy` below instead.
copy(unthunk(φbar)),
t,
setup,
),
NoTangent(),
NoTangent(),
),
)
ChainRulesCore.rrule(::typeof(apply_bc_temp), temp, t, setup) = (
apply_bc_temp(temp, t, setup),
# With respect to (apply_bc_temp, temp, t, setup)
φbar -> (
NoTangent(),
apply_bc_temp_pullback!(
# Important: identity operator should be part of `apply_bc_temp_pullback`,
# but is actually implemented via the `copy` below instead.
copy(unthunk(φbar)),
t,
setup,
),
NoTangent(),
NoTangent(),
),
)
function apply_bc_u!(u, t, setup; kwargs...)
(; boundary_conditions) = setup
D = length(u)
for β = 1:D
apply_bc_u!(boundary_conditions[β][1], u, β, t, setup; isright = false, kwargs...)
apply_bc_u!(boundary_conditions[β][2], u, β, t, setup; isright = true, kwargs...)
end
u
end
function apply_bc_u_pullback!(φbar, t, setup; kwargs...)
(; grid, boundary_conditions) = setup
(; dimension) = grid
D = dimension()
for β = 1:D
apply_bc_u_pullback!(
boundary_conditions[β][1],
φbar,
β,
t,
setup;
isright = false,
kwargs...,
)
apply_bc_u_pullback!(
boundary_conditions[β][2],
φbar,
β,
t,
setup;
isright = true,
kwargs...,
)
end
φbar
end
function apply_bc_p!(p, t, setup; kwargs...)
(; boundary_conditions, grid) = setup
(; dimension) = grid
D = dimension()
for β = 1:D
apply_bc_p!(boundary_conditions[β][1], p, β, t, setup; isright = false)
apply_bc_p!(boundary_conditions[β][2], p, β, t, setup; isright = true)
end
p
end
function apply_bc_p_pullback!(φbar, t, setup; kwargs...)
(; grid, boundary_conditions) = setup
(; dimension) = grid
D = dimension()
for β = 1:D
apply_bc_p_pullback!(
boundary_conditions[β][1],
φbar,
β,
t,
setup;
isright = false,
kwargs...,
)
apply_bc_p_pullback!(
boundary_conditions[β][2],
φbar,
β,
t,
setup;
isright = true,
kwargs...,
)
end
φbar
end
function apply_bc_temp!(temp, t, setup; kwargs...)
(; temperature, grid) = setup
(; boundary_conditions) = temperature
(; dimension) = grid
D = dimension()
for β = 1:D
apply_bc_temp!(boundary_conditions[β][1], temp, β, t, setup; isright = false)
apply_bc_temp!(boundary_conditions[β][2], temp, β, t, setup; isright = true)
end
temp
end
function apply_bc_temp_pullback!(φbar, t, setup; kwargs...)
(; temperature, grid) = setup
(; boundary_conditions) = temperature
(; dimension) = grid
D = dimension()
for β = 1:D
apply_bc_temp_pullback!(
boundary_conditions[β][1],
φbar,
β,
t,
setup;
isright = false,
kwargs...,
)
apply_bc_temp_pullback!(
boundary_conditions[β][2],
φbar,
β,
t,
setup;
isright = true,
kwargs...,
)
end
φbar
end
function apply_bc_u!(::PeriodicBC, u, β, t, setup; isright, kwargs...)
(; grid, workgroupsize) = setup
(; dimension, N) = grid
D = dimension()
e = Offset{D}()
@kernel function _bc_a!(u, ::Val{α}, ::Val{β}) where {α,β}
I = @index(Global, Cartesian)
u[α][I] = u[α][I+(N[β]-2)*e(β)]
end
@kernel function _bc_b!(u, ::Val{α}, ::Val{β}) where {α,β}
I = @index(Global, Cartesian)
u[α][I+(N[β]-1)*e(β)] = u[α][I+e(β)]
end
ndrange = ntuple(γ -> γ == β ? 1 : N[γ], D)
for α = 1:D
if isright
_bc_b!(get_backend(u[1]), workgroupsize)(u, Val(α), Val(β); ndrange)
else
_bc_a!(get_backend(u[1]), workgroupsize)(u, Val(α), Val(β); ndrange)
end
end
u
end
function apply_bc_u_pullback!(::PeriodicBC, φbar, β, t, setup; isright, kwargs...)
(; grid, workgroupsize) = setup
(; dimension, N) = grid
D = dimension()
e = Offset{D}()
@kernel function adj_a!(φ, ::Val{α}, ::Val{β}) where {α,β}
I = @index(Global, Cartesian)
φ[α][I+(N[β]-2)*e(β)] += φ[α][I]
φ[α][I] = 0
end
@kernel function adj_b!(φ, ::Val{α}, ::Val{β}) where {α,β}
I = @index(Global, Cartesian)
φ[α][I+e(β)] += φ[α][I+(N[β]-1)*e(β)]
φ[α][I+(N[β]-1)*e(β)] = 0
end
ndrange = ntuple(γ -> γ == β ? 1 : N[γ], D)
for α = 1:D
if isright
adj_b!(get_backend(φbar[1]), workgroupsize)(φbar, Val(α), Val(β); ndrange)
else
adj_a!(get_backend(φbar[1]), workgroupsize)(φbar, Val(α), Val(β); ndrange)
end
end
φbar
end
function apply_bc_p!(::PeriodicBC, p, β, t, setup; isright, kwargs...)
(; grid, workgroupsize) = setup
(; dimension, N) = grid
D = dimension()
e = Offset{D}()
@kernel function _bc_a(p, ::Val{β}) where {β}
I = @index(Global, Cartesian)
p[I] = p[I+(N[β]-2)*e(β)]
end
@kernel function _bc_b(p, ::Val{β}) where {β}
I = @index(Global, Cartesian)
p[I+(N[β]-1)*e(β)] = p[I+e(β)]
end
ndrange = ntuple(γ -> γ == β ? 1 : N[γ], D)
if isright
_bc_b(get_backend(p), workgroupsize)(p, Val(β); ndrange)
else
_bc_a(get_backend(p), workgroupsize)(p, Val(β); ndrange)
end
p
end
function apply_bc_p_pullback!(::PeriodicBC, φbar, β, t, setup; isright, kwargs...)
(; grid, workgroupsize) = setup
(; dimension, N) = grid
D = dimension()
e = Offset{D}()
@kernel function adj_a!(φ, ::Val{β}) where {β}
I = @index(Global, Cartesian)
φ[I+(N[β]-2)*e(β)] += φ[I]
φ[I] = 0
end
@kernel function adj_b!(φ, ::Val{β}) where {β}
I = @index(Global, Cartesian)
φ[I+e(β)] += φ[I+(N[β]-1)*e(β)]
φ[I+(N[β]-1)*e(β)] = 0
end
ndrange = ntuple(γ -> γ == β ? 1 : N[γ], D)
if isright
adj_b!(get_backend(φbar), workgroupsize)(φbar, Val(β); ndrange)
else
adj_a!(get_backend(φbar), workgroupsize)(φbar, Val(β); ndrange)
end
φbar
end
apply_bc_temp!(bc::PeriodicBC, temp, β, t, setup; isright, kwargs...) =
apply_bc_p!(bc, temp, β, t, setup; isright, kwargs...)
apply_bc_temp_pullback!(bc::PeriodicBC, φbar, β, t, setup; isright, kwargs...) =
apply_bc_p_pullback!(bc, φbar, β, t, setup; isright, kwargs...)
function apply_bc_u!(bc::DirichletBC, u, β, t, setup; isright, dudt = false, kwargs...)
(; dimension, x, xp, N) = setup.grid
D = dimension()
e = Offset{D}()
# isnothing(bc.u) && return
bcfunc = dudt ? bc.dudt : bc.u
for α = 1:D
I = if isright
CartesianIndices(
ntuple(γ -> γ == β ? α == β ? (N[γ]-1:N[γ]-1) : (N[γ]:N[γ]) : (1:N[γ]), D),
)
else
CartesianIndices(ntuple(γ -> γ == β ? (1:1) : (1:N[γ]), D))
end
xI = ntuple(
γ -> reshape(
γ == α ? x[γ][I.indices[α].+1] : xp[γ][I.indices[γ]],
ntuple(Returns(1), γ - 1)...,
:,
ntuple(Returns(1), D - γ)...,
),
D,
)
if isnothing(bc.u)
u[α][I] .= 0
else
u[α][I] .= bcfunc.((Dimension(α),), xI..., t)
end
end
u
end
# apply_bc_u_pullback!(::DirichletBC, φbar, β, t, setup; isright, kwargs...) =
# @not_implemented("DirichletBC pullback not yet implemented.")
function apply_bc_p!(::DirichletBC, p, β, t, setup; isright, kwargs...)
(; dimension, N) = setup.grid
D = dimension()
e = Offset{D}()
if isright
I = CartesianIndices(ntuple(γ -> γ == β ? (N[γ]:N[γ]) : (1:N[γ]), D))
p[I] .= p[I.-e(β)]
else
I = CartesianIndices(ntuple(γ -> γ == β ? (1:1) : (1:N[γ]), D))
p[I] .= p[I.+e(β)]
end
p
end
# apply_bc_p_pullback!(::DirichletBC, φbar, β, t, setup; isright, kwargs...) =
# @not_implemented("DirichletBC pullback not yet implemented.")
function apply_bc_temp!(bc::DirichletBC, temp, β, t, setup; isright, kwargs...)
(; dimension, N) = setup.grid
D = dimension()
e = Offset{D}()
I = if isright
CartesianIndices(ntuple(γ -> γ == β ? (N[γ]:N[γ]) : (1:N[γ]), D))
else
CartesianIndices(ntuple(γ -> γ == β ? (1:1) : (1:N[γ]), D))
end
temp[I] .= bc.u
temp
end
function apply_bc_u!(::SymmetricBC, u, β, t, setup; isright, kwargs...)
(; dimension, N) = setup.grid
D = dimension()
e = Offset{D}()
for α = 1:D
if α != β
if isright
I = CartesianIndices(ntuple(γ -> γ == β ? (N[γ]:N[γ]) : (1:N[γ]), D))
u[α][I] .= u[α][I.-e(β)]
else
I = CartesianIndices(ntuple(γ -> γ == β ? (1:1) : (1:N[γ]), D))
u[α][I] .= u[α][I.+e(β)]
end
end
end
u
end
# apply_bc_u_pullback!(::SymmetricBC, φbar, β, t, setup; isright, kwargs...) =
# @not_implemented("SymmetricBC pullback not yet implemented.")
function apply_bc_p!(::SymmetricBC, p, β, t, setup; isright, kwargs...)
(; dimension, N) = setup.grid
D = dimension()
e = Offset{D}()
if isright
I = CartesianIndices(ntuple(γ -> γ == β ? (N[γ]:N[γ]) : (1:N[γ]), D))
p[I] .= p[I.-e(β)]
else
I = CartesianIndices(ntuple(γ -> γ == β ? (1:1) : (1:N[γ]), D))
p[I] .= p[I.+e(β)]
end
p
end
# apply_bc_p_pullback!(::SymmetricBC, φbar, β, t, setup; isright, kwargs...) =
# @not_implemented("SymmetricBC pullback not yet implemented.")
apply_bc_temp!(bc::SymmetricBC, temp, β, t, setup; isright, kwargs...) =
apply_bc_p!(bc, temp, β, t, setup; isright, kwargs...)
function apply_bc_u!(bc::PressureBC, u, β, t, setup; isright, kwargs...)
(; grid, workgroupsize) = setup
(; dimension, N, Nu, Iu) = grid
D = dimension()
e = Offset{D}()
@kernel function _bc_a!(u, ::Val{α}, ::Val{β}, I0) where {α,β}
I = @index(Global, Cartesian)
I = I + I0
u[α][I] = u[α][I+e(β)]
end
@kernel function _bc_b!(u, ::Val{α}, ::Val{β}, I0) where {α,β}
I = @index(Global, Cartesian)
I = I + I0
u[α][I] = u[α][I-e(β)]
end
ndrange = (N[1:β-1]..., 1, N[β+1:end]...)
for α = 1:D
if isright
I0 = CartesianIndex(ntuple(γ -> γ == β ? N[β] : 1, D))
I0 -= oneunit(I0)
_bc_b!(get_backend(u[1]), workgroupsize)(u, Val(α), Val(β), I0; ndrange)
else
I0 = CartesianIndex(ntuple(γ -> γ == β && α != β ? 2 : 1, D))
I0 -= oneunit(I0)
_bc_a!(get_backend(u[1]), workgroupsize)(u, Val(α), Val(β), I0; ndrange)
end
end
u
end
# apply_bc_u_pullback!(::PressureBC, φbar, β, t, setup; isright, kwargs...) =
# @not_implemented("PressureBC pullback not yet implemented.")
function apply_bc_p!(bc::PressureBC, p, β, t, setup; isright, kwargs...)
(; dimension, N) = setup.grid
D = dimension()
I = if isright
CartesianIndices(ntuple(γ -> γ == β ? (N[γ]:N[γ]) : (1:N[γ]), D))
else
CartesianIndices(ntuple(γ -> γ == β ? (2:2) : (1:N[γ]), D))
end
p[I] .= 0
p
end
apply_bc_p_pullback!(::PressureBC, φbar, β, t, setup; isright, kwargs...) =
@not_implemented("PressureBC pullback not yet implemented.")