Skip to content

Commit a932639

Browse files
committed
removes body_force and defines acceleration g as a function of g(i,x,t), hence implementing the space-varying featured previously in charge of body_force. Tests have been mdofied accordingly, an all are locally passing for CPU and GPU. Need to update examples repo.
1 parent 24aa6b7 commit a932639

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

src/Flow.jl

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,15 @@ using EllipsisNotation
6363
"""
6464
accelerate!(r,t,g,U)
6565
66-
Add a uniform acceleration `gᵢ+dUᵢ/dt` at time `t=sum(dt)` to field `r`.
66+
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`.
6767
"""
68-
accelerate!(r,t,g::Nothing,U::Function) = body_force!(r,(i,x,t)->ForwardDiff.derivative->U(i,x,τ),t),t)
69-
accelerate!(r,t,g::Function,U::Function) = body_force!(r,(i,x,t)->g(i,t)+ForwardDiff.derivative->U(i,x,τ),t),t)
70-
accelerate!(r,t,g::Function,::Tuple) = body_force!(r,(i,x,t)->g(i,t),t)
71-
accelerate!(r,t,::Nothing,::Tuple) = nothing
72-
"""
73-
body_force!(r,force,t)
74-
75-
Adds a body force to the RHS
76-
77-
- `force` is either a vector field or a function `f(i,x,t)` that returns the component
78-
`i` of the vector field at time `t` and position `x`.
79-
"""
80-
body_force!(_,::Nothing,_) = nothing
81-
body_force!(r,force::AbstractArray,_) = r .+= force
82-
body_force!(r,force::Function,t) = for i 1:last(size(r))
83-
@loop r[I,i] += force(i,loc(i,I,eltype(r)),t) over I CartesianIndices(Base.front(size(r)))
68+
accelerate!(_,_,::Nothing,::Union{Nothing,Tuple}) = nothing
69+
accelerate!(r,t,f) = for i 1:last(size(r))
70+
@loop r[I,i] += f(i,loc(i,I,eltype(r)),t) over I CartesianIndices(Base.front(size(r)))
8471
end
72+
accelerate!(r,t,g::Function,::Union{Nothing,Tuple}) = accelerate!(r,t,g)
73+
accelerate!(r,t,::Nothing,U::Function) = accelerate!(r,t,(i,x,t)->ForwardDiff.derivative->U(i,x,τ),t))
74+
accelerate!(r,t,g::Function,U::Function) = accelerate!(r,t,(i,x,t)->g(i,x,t)+ForwardDiff.derivative->U(i,x,τ),t))
8575
"""
8676
Flow{D::Int, T::Float, Sf<:AbstractArray{T,D}, Vf<:AbstractArray{T,D+1}, Tf<:AbstractArray{T,D+2}}
8777
@@ -153,20 +143,20 @@ end
153143
Integrate the `Flow` one time step using the [Boundary Data Immersion Method](https://eprints.soton.ac.uk/369635/)
154144
and the `AbstractPoisson` pressure solver to project the velocity onto an incompressible flow.
155145
"""
156-
@fastmath function mom_step!(a::Flow{N},b::AbstractPoisson;body_force=nothing,udf=nothing,kwargs...) where N
146+
@fastmath function mom_step!(a::Flow{N},b::AbstractPoisson;udf=nothing,kwargs...) where N
157147
a.u⁰ .= a.u; scale_u!(a,0); t₁ = sum(a.Δt); t₀ = t₁-a.Δt[end]
158148
# predictor u → u'
159149
@log "p"
160150
conv_diff!(a.f,a.u⁰,a.σ,ν=a.ν,perdir=a.perdir)
161-
body_force!(a.f,body_force,t₀); udf!(a,udf,t₀; kwargs...)
151+
udf!(a,udf,t₀; kwargs...)
162152
accelerate!(a.f,t₀,a.g,a.U)
163153
BDIM!(a); BC!(a.u,a.U,a.exitBC,a.perdir,t₁) # BC MUST be at t₁
164154
a.exitBC && exitBC!(a.u,a.u⁰,a.Δt[end]) # convective exit
165155
project!(a,b); BC!(a.u,a.U,a.exitBC,a.perdir,t₁)
166156
# corrector u → u¹
167157
@log "c"
168158
conv_diff!(a.f,a.u,a.σ,ν=a.ν,perdir=a.perdir)
169-
body_force!(a.f,body_force,t₁); udf!(a,udf,t₁; kwargs...)
159+
udf!(a,udf,t₁; kwargs...)
170160
accelerate!(a.f,t₁,a.g,a.U)
171161
BDIM!(a); scale_u!(a,0.5); BC!(a.u,a.U,a.exitBC,a.perdir,t₁)
172162
project!(a,b,0.5); BC!(a.u,a.U,a.exitBC,a.perdir,t₁)

src/WaterLily.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ Can be set to `false` for static geometries to speed up simulation.
9898
A user-defined function `udf` can be passed to arbitrarily modify the `::Flow` during the predictor and corrector steps.
9999
If the `udf` user keyword arguments, these needs to be included in the `sim_step!` call as well.
100100
"""
101-
function sim_step!(sim::AbstractSimulation,t_end;remeasure=true,max_steps=typemax(Int),body_force=nothing,udf=nothing,verbose=false,kwargs...)
101+
function sim_step!(sim::AbstractSimulation,t_end;remeasure=true,max_steps=typemax(Int),udf=nothing,verbose=false,kwargs...)
102102
steps₀ = length(sim.flow.Δt)
103103
while sim_time(sim) < t_end && length(sim.flow.Δt) - steps₀ < max_steps
104-
sim_step!(sim; remeasure, body_force, udf, kwargs...)
104+
sim_step!(sim; remeasure, udf, kwargs...)
105105
verbose && println("tU/L=",round(sim_time(sim),digits=4),
106106
", Δt=",round(sim.flow.Δt[end],digits=3))
107107
end
108108
end
109-
function sim_step!(sim::AbstractSimulation;remeasure=true,body_force=nothing,udf=nothing,kwargs...)
109+
function sim_step!(sim::AbstractSimulation;remeasure=true,udf=nothing,kwargs...)
110110
remeasure && measure!(sim)
111-
mom_step!(sim.flow, sim.pois; body_force, udf, kwargs...)
111+
mom_step!(sim.flow, sim.pois; udf, kwargs...)
112112
end
113113

114114
"""

test/maintests.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,21 @@ end
170170
N = 4; a = zeros(N,N,2) |> f
171171
WaterLily.accelerate!(a,1,nothing,())
172172
@test all(a .== 0)
173-
WaterLily.accelerate!(a,1,(i,t) -> i==1 ? t : 2*t,())
173+
WaterLily.accelerate!(a,1,(i,x,t)->i==1 ? t : 2*t,())
174174
@test all(a[:,:,1] .== 1) && all(a[:,:,2] .== 2)
175175
WaterLily.accelerate!(a,1,nothing,(i,x,t) -> i==1 ? -t : -2*t)
176176
@test all(a[:,:,1] .== 0) && all(a[:,:,2] .== 0)
177-
WaterLily.accelerate!(a,1,(i,t) -> i==1 ? t : 2*t,(i,x,t) -> i==1 ? -t : -2*t)
177+
WaterLily.accelerate!(a,1,(i,x,t) -> i==1 ? t : 2*t,(i,x,t) -> i==1 ? -t : -2*t)
178178
@test all(a[:,:,1] .== 0) && all(a[:,:,2] .== 0)
179-
# check applying body force
180-
b = zeros(N,N,2) |> f; bf = ones(N,N,2) |> f
181-
WaterLily.body_force!(b, nothing, 0)
182-
@test all(b .== 0)
183-
WaterLily.body_force!(b, bf, 0)
179+
# check applying body force (changes in x but not t)
180+
b = zeros(N,N,2) |> f
181+
WaterLily.accelerate!(b,0,(i,x,t)->1,nothing)
184182
@test all(b .== 1)
185-
apply!((i,x)->x[i], bf)
186-
WaterLily.body_force!(b, bf, 0)
187183
a .= 0 # reset and accelerate using a non-uniform velocity field
188-
WaterLily.accelerate!(a,1.,nothing,(i,x,t)->t*(x[i]+1.0))
184+
WaterLily.accelerate!(a,0.,nothing,(i,x,t)->t*(x[i]+1.0))
185+
WaterLily.accelerate!(b,0,(i,x,t)->x[i],nothing)
189186
@test all(b .== a)
190-
WaterLily.body_force!(b,(i,x,t)->x[i]+1.0,1.)
187+
WaterLily.accelerate!(b,1.,(i,x,t)->x[i]+1.0,nothing)
191188
WaterLily.accelerate!(a,1.,nothing,(i,x,t)->t*(x[i]+1.0))
192189
@test all(b .== a)
193190
end
@@ -304,7 +301,7 @@ function acceleratingFlow(N;use_g=false,T=Float64,perdir=(1,),jerk=4,mem=Array)
304301
# assuming gravitational scale is 1 and Fr is 1, U scale is Fr*√gL
305302
UScale = N # this is also initial U
306303
# constant jerk in x, zero acceleration in y
307-
g(i,t) = i==1 ? t*jerk : 0
304+
g(i,x,t) = i==1 ? t*jerk : 0
308305
!use_g && (g = nothing)
309306
return WaterLily.Simulation(
310307
(N,N), (UScale,0.), N; ν=0.001,g,Δt=0.001,perdir,T,mem

0 commit comments

Comments
 (0)