Skip to content

Commit 79a9f01

Browse files
committed
Merge branch 'add-nonuniform-BCs' into turbulence_modelling
2 parents 6aaca69 + 9a7753d commit 79a9f01

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

src/Flow.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ upperBoundary!(r,u,Φ,ν,i,j,N,::Val{true}) = @loop r[I-δ(j,I),i] -= Φ[CIj(j,I
6262
"""
6363
accelerate!(r,t,g,U)
6464
65-
Adds a space-time acceleration field g(i,x,t) and velocity field U(i,x,t) such that `rᵢ += gᵢ(i,x,t)+dUᵢ(i,x,t)/dt` at time `t=sum(dt)` to vector field `r`.
65+
Accounts for applied and reference-frame acceleration using `rᵢ += g(i,x,t)+dU(i,x,t)/dt`
6666
"""
6767
accelerate!(r,t,::Nothing,::Union{Nothing,Tuple}) = nothing
68-
accelerate!(r,t,f) = for i 1:last(size(r))
69-
@loop r[I,i] += f(i,loc(i,I,eltype(r)),t) over I CartesianIndices(Base.front(size(r)))
70-
end
68+
accelerate!(r,t,f::Function) = @loop r[Ii] += f(last(Ii),loc(Ii,eltype(r)),t) over Ii CartesianIndices(r)
7169
accelerate!(r,t,g::Function,::Union{Nothing,Tuple}) = accelerate!(r,t,g)
7270
accelerate!(r,t,::Nothing,U::Function) = accelerate!(r,t,(i,x,t)->ForwardDiff.derivative->U(i,x,τ),t))
7371
accelerate!(r,t,g::Function,U::Function) = accelerate!(r,t,(i,x,t)->g(i,x,t)+ForwardDiff.derivative->U(i,x,τ),t))

src/WaterLily.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mutable struct Simulation <: AbstractSimulation
6767
Δt=0.25, ν=0., g=nothing, U=nothing, ϵ=1, perdir=(),
6868
=nothing, exitBC=false, body::AbstractBody=NoBody(),
6969
T=Float32, mem=Array) where N
70-
@assert !(isa(u_BC,Function) && isa(uλ,Function)) "`u_BC` and `uλ` cannot be both specified as Function"
70+
@assert !(isa(u_BC,Function) && isa(uλ,Function)) "`u_BC` will be used to generate `uλ=u_BC(t=0)` do not provide both"
7171
@assert !(isnothing(U) && isa(u_BC,Function)) "`U` must be specified if `u_BC` is a Function"
7272
isa(g,Function) && @assert first(methods(g)).nargs==4 "g::Function needs to be defined as g(i,x,t)"
7373
isa(g,Function) && @assert all(typeof.(ntuple(i->g(i,zeros(SVector{N}),zero(T)),N)).==T) "`g` is not type stable"

src/util.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ function BC!(a,u_BC::Function,saveexit=false,perdir=(),t=0)
203203
end
204204
(!saveexit || i>1) && (@loop a[I,i] = u_BC(i,loc(i,I),t) over I slice(N,N[j],j)) # overwrite exit
205205
else # Tangential directions, Neumann
206-
@loop a[I,i] = a[I+δ(j,I),i] over I slice(N,1,j)
207-
@loop a[I,i] = a[I-δ(j,I),i] over I slice(N,N[j],j)
206+
@loop a[I,i] = u_BC(i,loc(i,I),t)+a[I+δ(j,I),i]-u_BC(i,loc(i,I+δ(j,I)),t) over I slice(N,1,j)
207+
@loop a[I,i] = u_BC(i,loc(i,I),t)+a[I-δ(j,I),i]-u_BC(i,loc(i,I-δ(j,I)),t) over I slice(N,N[j],j)
208208
end
209209
end
210210
end

test/maintests.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ using ReadVTK, WriteVTK
4949
Ubc(i,x,t) = i==1 ? 1.0 : 0.5
5050
v = rand(Ng..., D) |> f # vector
5151
BC!(v,Ubc,false); BC!(u,U,false) # make sure we apply the same
52-
@test all(v[1, :, 1] . u[1, :, 1]) && all(v[2, :, 1] . u[2, :, 1]) && all(v[end, :, 1] . u[end, :, 1])
53-
@test all(v[:, 1, 2] . u[:, 1, 2]) && all(v[:, 2, 2] . u[:, 2, 2]) && all(v[:, end, 2] . u[:, end, 2])
52+
@test GPUArrays.@allowscalar all(v[1, :, 1] .== u[1, :, 1]) && all(v[2, :, 1] .== u[2, :, 1]) && all(v[end, :, 1] .== u[end, :, 1])
53+
@test GPUArrays.@allowscalar all(v[:, 1, 2] .== u[:, 1, 2]) && all(v[:, 2, 2] .== u[:, 2, 2]) && all(v[:, end, 2] .== u[:, end, 2])
5454
# test exit bc
5555
GPUArrays.@allowscalar v[end,:,1] .= 3
5656
BC!(v,Ubc,true) # save exit values
@@ -65,6 +65,20 @@ using ReadVTK, WriteVTK
6565
BC!(u,U,true,(1,)) #saveexit has no effect here as x-periodic
6666
@test GPUArrays.@allowscalar all(u[1:2, :, 1] .== u[end-1:end, :, 1]) && all(u[1:2, :, 2] .== u[end-1:end, :, 2]) &&
6767
all(u[:, 1, 2] .== U[2]) && all(u[:, 2, 2] .== U[2]) && all(u[:, end, 2] .== U[2])
68+
# test non-uniform BCs
69+
Ubc_1(i,x,t) = i==1 ? x[2] : x[1]
70+
v .= 0; BC!(v,Ubc_1)
71+
# the tangential BC change the value of the ghost cells on the other axis, so we cannot check it
72+
@test GPUArrays.@allowscalar all(v[1,2:end-1,1] .≈ v[end,2:end-1,1])
73+
@test GPUArrays.@allowscalar all(v[2:end-1,1,2] .≈ v[2:end-1,end,2])
74+
# more complex
75+
Ng, D = (8, 8, 8), 3
76+
u = zeros(Ng..., D) |> f # vector
77+
Ubc_2(i,x,t) = i==1 ? cos(2π*x[1]/8) : i==2 ? sin(2π*x[2]/8) : tan*x[3]/16)
78+
BC!(u,Ubc_2)
79+
@test GPUArrays.@allowscalar all(u[1,:,:,1] .≈ cos(-1π/4)) && all(u[2,:,:,1] .≈ cos(0)) && all(u[end,:,:,1] .≈ cos(6π/4))
80+
@test GPUArrays.@allowscalar all(u[:,1,:,2] .≈ sin(-1π/4)) && all(u[:,2,:,2] .≈ sin(0)) && all(u[:,end,:,2] .≈ sin(6π/4))
81+
@test GPUArrays.@allowscalar all(u[:,:,1,3] .≈ tan(-1π/16)) && all(u[:,:,2,3] .≈ tan(0)) && all(u[:,:,end,3].-tan(6π/16).<1e-6)
6882

6983
# test interpolation
7084
a = zeros(5,5,2) |> f; b = zeros(5,5) |> f
@@ -334,6 +348,35 @@ end
334348
)
335349
end
336350
end
351+
@testset "Boundary Layer Flow" begin
352+
for f arrays
353+
make_bl_flow(L=32;T=Float32) = Simulation((L,L),
354+
(i,x,t)-> i==1 ? convert(Float32,4.0*(((x[2]+0.5)/2L)-((x[2]+0.5)/2L)^2)) : 0.f0,
355+
L;ν=0.001,U=1,mem=f,T,exitBC=false) # fails with exitBC=true, but the profile is maintained
356+
sim = make_bl_flow(32)
357+
sim_step!(sim,10)
358+
@test GPUArrays.@allowscalar all(sim.flow.u[1,:,1] .≈ sim.flow.u[end,:,1])
359+
end
360+
end
361+
362+
@testset "Rotating reference frame" begin
363+
function rotating_reference(N,x₀::SVector{2,T}::T,mem=Array) where T
364+
function velocity(i,x,t)
365+
s,c = sincos*t); y = ω*(x-x₀)
366+
i==1 ? s*y[1]+c*y[2] : -c*y[1]+s*y[2]
367+
end
368+
coriolis(i,x,t) = i==1 ? 2ω*velocity(2,x,t) : -2ω*velocity(1,x,t)
369+
centrifugal(i,x,t) = ω^2*(x-x₀)[i]
370+
g(i,x,t) = coriolis(i,x,t)+centrifugal(i,x,t)
371+
udf(a::Flow,t) = WaterLily.@loop a.f[Ii] += g(last(Ii),loc(Ii,eltype(a.f)),t) over Ii in CartesianIndices(a.f)
372+
simg = Simulation((N,N),velocity,N; g, U=1, T, mem) # use g
373+
simg,Simulation((N,N),velocity,N; U=1, T, mem),udf
374+
end
375+
L = 4
376+
simg,sim,udf = rotating_reference(2L,SA_F64[L,L],1/L,Array)
377+
sim_step!(simg);sim_step!(sim;udf)
378+
@test L₂(simg.flow.p)==L₂(sim.flow.p)<3e-3 # should be zero
379+
end
337380

338381
@testset "Circle in accelerating flow" begin
339382
for f arrays

0 commit comments

Comments
 (0)