Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advection-diffusion of passive tracer using a turbulent flow #53

Merged
merged 78 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
10b3f36
Begin addition of advection of passive tracer with turbulent flow
jbisits May 30, 2022
85aa62b
Finish module additions
jbisits May 30, 2022
1c5b555
Update turbulent_advection-diffusion.jl
jbisits May 30, 2022
1ab0d8a
Add explanation to the example
jbisits May 31, 2022
fcbf864
Add tests
jbisits May 31, 2022
768c232
Update test_traceradvectiondiffusion.jl
jbisits May 31, 2022
9e65a3a
add docs/src/literated
navidcy Jun 1, 2022
1366aa4
drop Plots; add latest FourierFlows version compat entries
navidcy Jun 1, 2022
90b4ad4
literate turbulent_advection-diffusion example
navidcy Jun 1, 2022
092bc6a
workaround for any number of layers
navidcy Jun 1, 2022
06ed8b7
simplification
navidcy Jun 1, 2022
15138eb
Merge branch 'main' into turb_advection-diffusion
navidcy Jun 1, 2022
3b05ce0
use GeophysicalFlows v0.14
navidcy Jun 1, 2022
f8008ed
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
dfae270
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
87cb5d8
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
b16c0df
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
029ec7e
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
afdca1c
update `get_concnetration`
jbisits Jun 1, 2022
97ca38e
add `LienarAlgebra`
jbisits Jun 1, 2022
33d90f8
Update turbulent_advection-diffusion.jl
jbisits Jun 1, 2022
8941881
add CUDA import
navidcy Jun 1, 2022
779cd6d
better name for test w MultilayerQG
navidcy Jun 1, 2022
95959ab
bump patch release
navidcy Jun 1, 2022
05d28cb
update readme
navidcy Jun 1, 2022
0e4fb5d
tracer_release -> tracer_release_time
navidcy Jun 1, 2022
bf226fc
minor fixes
navidcy Jun 1, 2022
8df467d
tracer_release -> tracer_release_time
navidcy Jun 2, 2022
625902b
Use `step_until!` to flow the MQG flow forward
jbisits Jun 2, 2022
cc1e2d4
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 2, 2022
c7cbc1c
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 2, 2022
364cf14
Update examples/turbulent_advection-diffusion.jl
jbisits Jun 2, 2022
759d2e1
Update src/traceradvectiondiffusion.jl
jbisits Jun 2, 2022
d5b5cc8
Update src/traceradvectiondiffusion.jl
jbisits Jun 2, 2022
58c76bc
Update traceradvectiondiffusion.jl
jbisits Jun 2, 2022
51a6f31
`tracer_release` -> `tracer_release_time`
jbisits Jun 2, 2022
314397d
Update src/traceradvectiondiffusion.jl
jbisits Jun 2, 2022
ee7155d
Update src/traceradvectiondiffusion.jl
jbisits Jun 6, 2022
109a8d1
Update src/traceradvectiondiffusion.jl
jbisits Jun 6, 2022
d3abc85
Update src/traceradvectiondiffusion.jl
jbisits Jun 6, 2022
5ceae51
better Vars
navidcy Jun 6, 2022
9c59951
attempt to declutter code
navidcy Jun 6, 2022
2c90b55
beautifications
navidcy Jun 6, 2022
935ad92
few more fixes
navidcy Jun 6, 2022
b9cc4d2
better colorrange
navidcy Jun 6, 2022
0f32daf
colorrange -> clims
navidcy Jun 6, 2022
bb6f467
Updates
jbisits Jun 7, 2022
dddaca5
change animation name
navidcy Jun 7, 2022
f406acc
add streamlines in animation
navidcy Jun 7, 2022
657d731
simpler and more informative
navidcy Jun 7, 2022
42aa8d8
Merge branch 'turb_advection-diffusion' of github.com:jbisits/Passive…
navidcy Jun 7, 2022
2c29d4d
some fixes
navidcy Jun 7, 2022
e6f6309
simplify layer selection
navidcy Jun 7, 2022
e0a7736
Update docs/Project.toml
navidcy Jun 7, 2022
9494a43
relocate where `MultiLayerQG` flow is stepped forward
jbisits Jun 7, 2022
83b71f1
Update turbulent_advection-diffusion.jl
jbisits Jun 7, 2022
97756e2
Another attempt
jbisits Jun 7, 2022
66225d4
fix MQG timestepping
navidcy Jun 7, 2022
5998e69
code alignment
navidcy Jun 7, 2022
dcebbf3
Remove background flow `U[1]`
jbisits Jun 7, 2022
ac91d73
fix coupled time-stepping: update MQG vars
navidcy Jun 7, 2022
caffd56
add dealias! in calcN!
navidcy Jun 7, 2022
5b86f7c
Merge branch 'turb_advection-diffusion' of github.com:jbisits/Passive…
navidcy Jun 7, 2022
97cca02
fix tests
navidcy Jun 7, 2022
57b9136
fix tests
navidcy Jun 7, 2022
fa5df1e
fix tests
navidcy Jun 7, 2022
dce3cf5
another test fix
navidcy Jun 7, 2022
91ebb7b
drop soft fail
navidcy Jun 7, 2022
0b39feb
dont enforce dealias!
navidcy Jun 7, 2022
c5ba007
fix tests
navidcy Jun 7, 2022
93b0cce
proper array type for L
navidcy Jun 7, 2022
68c1cd3
add dev in Equation
navidcy Jun 7, 2022
b3be4d0
add dev in Equation
navidcy Jun 7, 2022
b7b835b
fix for nlayers
navidcy Jun 7, 2022
7fb0f54
dev arg in Equation
navidcy Jun 7, 2022
e04bdd6
dev arg in Equation
navidcy Jun 7, 2022
a5e3737
@CUDA.allowscalar for repeat
navidcy Jun 7, 2022
c04777e
@CUDA.allowscalar for repeat
navidcy Jun 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
**/*.cov
**/Manifest.toml

# vs code
.vscode

# Documenter.jl and MkDocs
docs/build/
docs/site/
docs/src/generated
docs/src/literated
docs/src/examples
docs/Manifest.toml
docs/build/
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = "0.6.0"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
FourierFlows = "2aec4490-903f-5c70-9b11-9bed06a700e1"
GeophysicalFlows = "44ee3b1c-bc02-53fa-8355-8e347616e15e"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -19,7 +20,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
[compat]
CUDA = "^1.3, ^2, ^3"
DocStringExtensions = "^0.8"
FourierFlows = "^0.6.17, ^0.7"
FourierFlows = "^0.6.17, ^0.7, 0.8, 0.9"
JLD2 = "^0.1, ^0.2, ^0.3, ^0.4"
Reexport = "^0.2, ^1"
julia = "^1.6"
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const OUTPUT_DIR = joinpath(@__DIR__, "src/literated")

examples = [
"cellularflow.jl",
"turbulent_advection-diffusion.jl"
]

for example in examples
Expand Down Expand Up @@ -56,6 +57,7 @@ makedocs(
"Examples" => Any[
"TracerAdvectionDiffusion" => Any[
"literated/cellularflow.md",
"literated/turbulent_advection-diffusion.md",
]
],
"Modules" => Any[
Expand Down
169 changes: 169 additions & 0 deletions examples/turbulent_advection-diffusion.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# # Advection-diffusion of tracer by a turbulent flow
#
# This is an example demonstrating the advection-diffusion of a tracer using a
# turbulent flow generated by the `GeophysicalFlows.jl` package.
#
# ## Install dependencies
#
# First let's make sure we have all the required packages installed

# ```julia
# using Pkg
# pkg.add(["PassiveTracerFlows", "Printf", "Plots", "JLD2"])
#
# ## Let's begin
# First load `PassiveTracerFlows.jl` and the other packages needed to run this example.
using PassiveTracerFlows, Printf, Plots, JLD2
using Random: seed!

# ## Choosing a device: CPU or GPU
dev = CPU()
nothing # hide

# ## Setting up a `MultiLayerQG.Problem` to generate a turbulent flow
#
# The tubulent flow we will use to advect the passive tracer is generated using the
# [`MultiLayerQG`](https://fourierflows.github.io/GeophysicalFlowsDocumentation/stable/modules/multilayerqg/) module from the [`GeophysicalFlows.jl`](https://fourierflows.github.io/GeophysicalFlowsDocumentation/stable/) package. A more detailed setup
# of this two layer system can be found [here](https://fourierflows.github.io/GeophysicalFlowsDocumentation/stable/literated/multilayerqg_2layer/).
jbisits marked this conversation as resolved.
Show resolved Hide resolved
#
# ### Numerical and time stepping parameters for the flow

n = 128 # 2D resolution = n²
stepper = "FilteredRK4" # timestepper
dt = 2.5e-3 # timestep

# Physical parameters
L = 2π # domain size
μ = 5e-2 # bottom drag
β = 5 # the y-gradient of planetary PV

nlayers = 2 # number of layers
f₀, g = 1, 1 # Coriolis parameter and gravitational constant
H = [0.2, 0.8] # the rest depths of each layer
ρ = [4.0, 5.0] # the density of each layer

U = zeros(nlayers) # the imposed mean zonal flow in each layer
U[1] = 1.0
U[2] = 0.0

# ### `MultiLayerQG.Problem` setup, shortcuts and initial conditions
MQGprob = MultiLayerQG.Problem(nlayers, dev;
nx=n, Lx=L, f₀=f₀, g=g, H=H, ρ=ρ, U=U, μ=μ, β=β,
dt=dt, stepper=stepper, aliased_fraction=0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nx=n, Lx=L, f₀=f₀, g=g, H=H, ρ=ρ, U=U, μ=μ, β=β,
dt=dt, stepper=stepper, aliased_fraction=0)
nx=n, Lx=L, f₀, g, H, ρ, U, μ, β,
dt, stepper, aliased_fraction=0)

grid = MQGprob.grid
x, y = grid.x, grid.y

# Initial conditions
seed!(1234) # reset of the random number generator for reproducibility
q₀ = 1e-2 * ArrayType(dev)(randn((grid.nx, grid.ny, nlayers)))
q₀h = MQGprob.timestepper.filter .* rfft(q₀, (1, 2)) # apply rfft only in dims=1, 2
q₀ = irfft(q₀h, grid.nx, (1, 2)) # apply irfft only in dims=1, 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
q₀ = irfft(q₀h, grid.nx, (1, 2)) # apply irfft only in dims=1, 2
q₀ = irfft(q₀h, grid.nx, (1, 2)) # apply irfft only in dims=1, 2


MultiLayerQG.set_q!(MQGprob, q₀)
nothing

# ## Tracer advection-diffusion setup
# Now that we have a `MultiLayerQG.Problem` setup to generate our turbulent flow, we
# setup an advection-diffusion simulation. This is done by passing the `MultiLayerQG.Problem`
# as an argument to `TracerAdvectionDiffusion.Problem` which sets up an advection-diffusion problem
# with same parameters where applicable. We also need to pass a value for the constant diffusivity `κ`,
# the `stepper` used to step the problem forward and when we want the tracer released into the flow.
# We will let the flow run until it reaches a statistical equilibrium and then advect-diffuse the tracer.

κ = 0.002
nsteps = 4000 # total number of time-steps
nsubs = 1 # number of steps the simulation takes at each iteration
tracer_release = dt * 8000 # run flow for some time before releasing tracer

ADprob = TracerAdvectionDiffusion.Problem(dev, MQGprob; κ, stepper, tracer_release)
nothing

# ## Initial condition for concentration in both layers
# We have a two layer system so we will advect-diffuse the tracer in both layers.
# To do this we set the initial condition for tracer concetration as a Gaussian centred at the origin.
# Then we create some shortcuts for the `TracerAdvectionDiffusion.Problem`.
gaussian(x, y, σ) = exp(-(x^2 + y^2) / (2σ^2))

amplitude, spread = 10, 0.15
c₀ = [amplitude * gaussian(x[i], y[j], spread) for i=1:grid.nx, j=1:grid.ny]

TracerAdvectionDiffusion.set_c!(ADprob, c₀, nlayers)

# Shortcuts for advection-diffusion problem
sol, clock, vars, params, grid = ADprob.sol, ADprob.clock, ADprob.vars, ADprob.params, ADprob.grid
x, y = grid.x, grid.y

# ## Saving output
# The parent package `FourierFlows.jl` provides a way to save information from a simulation.
# To do this we write a function `GetConcentration` and pass this to the `Output` function along
# with the `TracerAdvectionDiffusion.Problem` and the name of the output file.

function GetConcentration(prob)
Concentration = @. prob.vars.c
return Concentration
end
jbisits marked this conversation as resolved.
Show resolved Hide resolved
output = Output(ADprob, "advection-diffusion.jld2", (:Concentration, GetConcentration))
jbisits marked this conversation as resolved.
Show resolved Hide resolved
# This saves information that we will use for plotting later on
saveproblem(output)

# ## Step the problem forward and save the output
# We specify that we would like to save the concentration every 50 timesteps using `save_frequency`
# then step the problem forward.
save_frequency = 50 # Freqeuncy at which output is saved

startwalltime = time()
while clock.step <= nsteps

if clock.step % save_frequency == 0

saveoutput(output)
log = @sprintf("Output saved, step: %04d, t: %d, walltime: %.2f min",
clock.step, clock.t, (time()-startwalltime)/60)

println(log)
end

stepforward!(ADprob)
TracerAdvectionDiffusion.MQGupdatevars!(ADprob)
end

# Append this information to our saved data for plotting later on
jldopen(output.path, "a+") do path
path["save_frequency"] = save_frequency
path["final_step"] = ADprob.clock.step - 1
end

# ## Visualising the output
# We now have output from our simulation saved in `advection-diffusion.jld2`.
# From this we can create a time series for the tracer that has been advected-diffused
# in the lower layer of our turbulent flow (the simulation from the upper layer can be obtained in a similar manner).

# Create time series for the concentration in the upper layer
conc_data = load("advection-diffusion.jld2")

saved_data = 0:conc_data["save_frequency"]:conc_data["final_step"]
t = [conc_data["snapshots/t/"*string(i)] for i ∈ saved_data]
# Concentration time series in the lower layer
Cₗ = [abs.(conc_data["snapshots/Concentration/"*string(i)][:, :, 2]) for i ∈ saved_data]
jbisits marked this conversation as resolved.
Show resolved Hide resolved

x, y, = conc_data["grid/x"], conc_data["grid/y"]
Lx, Ly = conc_data["grid/Lx"], conc_data["grid/Ly"]
plot_args = (xlabel = "x",
ylabel = "y",
aspectratio = 1,
framestyle = :box,
xlims = (-Lx/2, Lx/2),
ylims = (-Ly/2, Ly/2),
colorbar = true,
colorbar_title = " \nConcentration",
color = :deep)
jbisits marked this conversation as resolved.
Show resolved Hide resolved

p = heatmap(x, y, Cₗ[1]', title = "Concentration, t = $(t[1])"; plot_args...)
jbisits marked this conversation as resolved.
Show resolved Hide resolved
conc_anim = @animate for i ∈ 2:length(t)

heatmap!(p, x, y, Cₗ[i]', title = "Concentration, t = $(t[i])"; plot_args...)
jbisits marked this conversation as resolved.
Show resolved Hide resolved

end

# Create a movie of the tracer
mp4(conc_anim, "conc_adv-diff.mp4", fps = 12)
Loading