Skip to content

Commit

Permalink
Merge pull request #293 from FourierFlows/ncc/views
Browse files Browse the repository at this point in the history
Bit of cleanup and using `views` in `MultiLayerQG` module
  • Loading branch information
navidcy authored Jun 11, 2023
2 parents c0c42ce + 90fc710 commit d7e2526
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 230 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Documenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.8.0
uses: styfle/cancel-workflow-action@0.11.0
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@latest
with:
version: '1.6'
version: '1.8'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
Expand Down
4 changes: 2 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
CUDA = "^1, ^2.4.2, 3.0.0 - 3.6.4, ^3.7.1"
CairoMakie = "< 0.10.5"
CUDA = "1, 2.4.2, 3.0.0 - 3.6.4, 3.7.1, 4"
Literate = "≥2.9.0"
50 changes: 32 additions & 18 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using
Documenter,
Literate,
CairoMakie, # to not capture precompilation output
GeophysicalFlows,
Glob
using Documenter, Literate

using CairoMakie
# CairoMakie.activate!(type = "svg")

using GeophysicalFlows

#####
##### Generate literated examples
Expand All @@ -25,23 +25,22 @@ examples = [
"surfaceqg_decaying.jl",
]


for example in examples
withenv("GITHUB_REPOSITORY" => "FourierFlows/GeophysicalFlowsDocumentation") do
example_filepath = joinpath(EXAMPLES_DIR, example)
Literate.markdown(example_filepath, OUTPUT_DIR; flavor = Literate.DocumenterFlavor())
Literate.notebook(example_filepath, OUTPUT_DIR)
Literate.script(example_filepath, OUTPUT_DIR)
withenv("JULIA_DEBUG" => "Literate") do
Literate.markdown(example_filepath, OUTPUT_DIR;
flavor = Literate.DocumenterFlavor(), execute = true)
end
end
end


#####
##### Build and deploy docs
#####

# Set up a timer to print a space ' ' every 240 seconds. This is to avoid CI machines
# timing out when building demanding Literate.jl examples.
Timer(t -> println(" "), 0, interval=240)

format = Documenter.HTML(
collapselevel = 2,
prettyurls = get(ENV, "CI", nothing) == "true",
Expand All @@ -50,14 +49,14 @@ format = Documenter.HTML(

makedocs(
modules = [GeophysicalFlows],
doctest = false,
doctest = true,
clean = true,
checkdocs = :all,
format = format,
authors = "Navid C. Constantinou and Gregory L. Wagner",
authors = "Navid C. Constantinou, Gregory L. Wagner, and contributors",
sitename = "GeophysicalFlows.jl",
pages = Any[
"Home" => "index.md",
"Home" => "index.md",
"Installation instructions" => "installation_instructions.md",
"Aliasing" => "aliasing.md",
"GPU" => "gpu.md",
Expand Down Expand Up @@ -100,9 +99,24 @@ sitename = "GeophysicalFlows.jl",
]
)

@info "Cleaning up temporary .jld2 and .nc files created by doctests..."
@info "Clean up temporary .jld2 and .nc output created by doctests or literated examples..."

"""
recursive_find(directory, pattern)
Return list of filepaths within `directory` that contains the `pattern::Regex`.
"""
recursive_find(directory, pattern) =
mapreduce(vcat, walkdir(directory)) do (root, dirs, files)
joinpath.(root, filter(contains(pattern), files))
end

files = []
for pattern in [r"\.jld2", r"\.nc"]
global files = vcat(files, recursive_find(@__DIR__, pattern))
end

for file in vcat(glob("docs/*.jld2"), glob("docs/*.nc"))
for file in files
rm(file)
end

Expand Down
6 changes: 3 additions & 3 deletions docs/src/stochastic_forcing.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ hl3 = lines!(ax, μ * t, E_str[:, 1];
Legend(fig[1, 2], [hl1, hl2, hl3], ["½ xₜ²", "Eₜ (Ito)", "Eₜ (Stratonovich)"])
save("assets/energy_comparison.svg", fig); nothing # hide
save("assets/energy_comparison.svg", fig); nothing #hide
```

![energy_comparison](assets/energy_comparison.svg)
Expand Down Expand Up @@ -378,7 +378,7 @@ hl3 = lines!(ax2, μ * t[1:nsteps-1], dEdt_theory[1:nsteps-1];
Legend(fig[2, 2], [hl1, hl2, hl3],
["numerical 𝖽⟨E⟩/𝖽t", "⟨work - dissipation⟩", "theoretical 𝖽⟨E⟩/𝖽t"])
save("assets/energy_budgets_Ito.svg", fig); nothing # hide
save("assets/energy_budgets_Ito.svg", fig); nothing #hide
```

![energy_budgets_Ito](assets/energy_budgets_Ito.svg)
Expand Down Expand Up @@ -423,7 +423,7 @@ hl3 = lines!(ax2, μ * t[1:nsteps-1], dEdt_theory[1:nsteps-1];
Legend(fig[2, 2], [hl1, hl2, hl3],
["numerical 𝖽⟨E⟩/𝖽t", "⟨work - dissipation⟩", "theoretical 𝖽⟨E⟩/𝖽t"])
save("assets/energy_budgets_Stratonovich.svg", fig); nothing # hide
save("assets/energy_budgets_Stratonovich.svg", fig); nothing #hide
```

![energy_budgets_Stratonovich](assets/energy_budgets_Stratonovich.svg)
Expand Down
36 changes: 17 additions & 19 deletions examples/barotropicqgql_betaforced.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# # [Quasi-Linear forced-dissipative barotropic QG beta-plane turbulence](@id barotropicqgql_betaforced_example)
#
#md # This example can be viewed as a Jupyter notebook via [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/literated/barotropicqgql_betaforced.ipynb).
#
# A simulation of forced-dissipative barotropic quasi-geostrophic turbulence on
# a beta plane under the *quasi-linear approximation*. The dynamics include
# linear drag and stochastic excitation.
Expand All @@ -24,12 +22,12 @@ using Statistics: mean

parsevalsum = FourierFlows.parsevalsum
record = CairoMakie.record # disambiguate between CairoMakie.record and CUDA.record
nothing # hide
nothing #hide

# ## Choosing a device: CPU or GPU

dev = CPU() # Device (CPU/GPU)
nothing # hide
nothing #hide


# ## Numerical parameters and time-stepping parameters
Expand All @@ -39,15 +37,15 @@ stepper = "FilteredRK4" # timestepper
dt = 0.05 # timestep
nsteps = 8000 # total number of time-steps
nsubs = 10 # number of time-steps for intermediate logging/plotting (nsteps must be multiple of nsubs)
nothing # hide
nothing #hide


# ## Physical parameters

L = 2π # domain size
β = 10.0 # planetary PV gradient
μ = 0.01 # bottom drag
nothing # hide
nothing #hide


# ## Forcing
Expand All @@ -72,12 +70,12 @@ forcing_spectrum = @. exp(-(K - forcing_wavenumber)^2 / (2 * forcing_bandwidth^2

ε0 = parsevalsum(forcing_spectrum .* grid.invKrsq / 2, grid) / (grid.Lx * grid.Ly)
@. forcing_spectrum *= ε/ε0 # normalize forcing to inject energy at rate ε
nothing # hide
nothing #hide


# We reset of the random number generator for reproducibility
if dev==CPU(); Random.seed!(1234); else; CUDA.seed!(1234); end
nothing # hide
nothing #hide


# Next we construct function `calcF!` that computes a forcing realization every timestep.
Expand All @@ -91,7 +89,7 @@ function calcF!(Fh, sol, t, clock, vars, params, grid)

return nothing
end
nothing # hide
nothing #hide


# ## Problem setup
Expand All @@ -102,13 +100,13 @@ nothing # hide
# Thus, we choose not to do any dealiasing by providing `aliased_fraction=0`.
prob = BarotropicQGQL.Problem(dev; nx=n, Lx=L, β, μ, dt, stepper,
calcF=calcF!, stochastic=true, aliased_fraction=0)
nothing # hide
nothing #hide

# and define some shortcuts.
sol, clock, vars, params, grid = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
x, y = grid.x, grid.y
Lx, Ly = grid.Lx, grid.Ly
nothing # hide
nothing #hide


# First let's see how a forcing realization looks like. Note that when plotting, we decorate
Expand Down Expand Up @@ -136,24 +134,24 @@ fig

# Our initial condition is simply fluid at rest.
BarotropicQGQL.set_zeta!(prob, device_array(dev)(zeros(grid.nx, grid.ny)))
nothing # hide
nothing #hide

# ## Diagnostics

# Create Diagnostics -- `energy` and `enstrophy` are functions imported at the top.
E = Diagnostic(BarotropicQGQL.energy, prob; nsteps)
Z = Diagnostic(BarotropicQGQL.enstrophy, prob; nsteps)
nothing # hide
nothing #hide

# We can also define our custom diagnostics via functions.
zetaMean(prob) = prob.sol[1, :]

zMean = Diagnostic(zetaMean, prob; nsteps, freq=10) # the zonal-mean vorticity
nothing # hide
nothing #hide

# We combile all diags in a list.
diags = [E, Z, zMean] # A list of Diagnostics types passed to "stepforward!" will be updated every timestep.
nothing # hide
nothing #hide


# ## Output
Expand All @@ -163,12 +161,12 @@ filepath = "."
plotpath = "./plots_forcedbetaQLturb"
plotname = "snapshots"
filename = joinpath(filepath, "forcedbetaQLturb.jld2")
nothing # hide
nothing #hide

# Do some basic file management,
if isfile(filename); rm(filename); end
if !isdir(plotpath); mkdir(plotpath); end
nothing # hide
nothing #hide

# and then create Output.
get_sol(prob) = prob.sol # extracts the Fourier-transformed solution
Expand Down Expand Up @@ -255,7 +253,7 @@ lines!(axū, 0y, y; linewidth = 1, linestyle=:dash)
lines!(axE, μt, energy; linewidth = 3)
lines!(axZ, μt, enstrophy; linewidth = 3, color = :red)

nothing # hide
nothing #hide


# ## Time-stepping the `Problem` forward
Expand Down Expand Up @@ -291,7 +289,7 @@ record(fig, "barotropicqgql_betaforced.mp4", frames, framerate = 18) do j
stepforward!(prob, diags, nsubs)
BarotropicQGQL.updatevars!(prob)
end
nothing # hide
nothing #hide

# ![](barotropicqgql_betaforced.mp4)

Expand Down
24 changes: 11 additions & 13 deletions examples/multilayerqg_2layer.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# # [Phillips model of Baroclinic Instability](@id multilayerqg_2layer_example)
#
#md # This example can be viewed as a Jupyter notebook via [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/literated/multilayerqg_2layer.ipynb).
#
# A simulation of the growth of barolinic instability in the Phillips 2-layer model
# when we impose a vertical mean flow shear as a difference ``\Delta U`` in the
# imposed, domain-averaged, zonal flow at each layer.
Expand All @@ -26,7 +24,7 @@ using Random: seed!
# ## Choosing a device: CPU or GPU

dev = CPU() # Device (CPU/GPU)
nothing # hide
nothing #hide


# ## Numerical parameters and time-stepping parameters
Expand All @@ -36,7 +34,7 @@ stepper = "FilteredRK4" # timestepper
dt = 2.5e-3 # timestep
nsteps = 20000 # total number of time-steps
nsubs = 50 # number of time-steps for plotting (nsteps must be multiple of nsubs)
nothing # hide
nothing #hide


# ## Physical parameters
Expand All @@ -52,7 +50,7 @@ H = [0.2, 0.8] # the rest depths of each layer
U = zeros(nlayers) # the imposed mean zonal flow in each layer
U[1] = 1.0
U[2] = 0.0
nothing # hide
nothing #hide


# ## Problem setup
Expand All @@ -63,12 +61,12 @@ nothing # hide

prob = MultiLayerQG.Problem(nlayers, dev; nx=n, Lx=L, f₀, g, H, ρ, U, μ, β,
dt, stepper, aliased_fraction=0)
nothing # hide
nothing #hide

# and define some shortcuts.
sol, clock, params, vars, grid = prob.sol, prob.clock, prob.params, prob.vars, prob.grid
x, y = grid.x, grid.y
nothing # hide
nothing #hide


# ## Setting initial conditions
Expand All @@ -85,15 +83,15 @@ q₀h = prob.timestepper.filter .* rfft(q₀, (1, 2)) # apply rfft only in dims
q₀ = irfft(q₀h, grid.nx, (1, 2)) # apply irfft only in dims=1, 2

MultiLayerQG.set_q!(prob, q₀)
nothing # hide
nothing #hide


# ## Diagnostics

# Create Diagnostics -- `energies` function is imported at the top.
E = Diagnostic(MultiLayerQG.energies, prob; nsteps)
diags = [E] # A list of Diagnostics types passed to "stepforward!" will be updated every timestep.
nothing # hide
nothing #hide


# ## Output
Expand All @@ -103,12 +101,12 @@ filepath = "."
plotpath = "./plots_2layer"
plotname = "snapshots"
filename = joinpath(filepath, "2layer.jld2")
nothing # hide
nothing #hide

# Do some basic file management
if isfile(filename); rm(filename); end
if !isdir(plotpath); mkdir(plotpath); end
nothing # hide
nothing #hide

# And then create Output

Expand All @@ -126,7 +124,7 @@ function get_u(prob)
end

out = Output(prob, filename, (:sol, get_sol), (:u, get_u))
nothing # hide
nothing #hide


# ## Visualizing the simulation
Expand Down Expand Up @@ -258,7 +256,7 @@ record(fig, "multilayerqg_2layer.mp4", frames, framerate = 18) do j
stepforward!(prob, diags, nsubs)
MultiLayerQG.updatevars!(prob)
end
nothing # hide
nothing #hide

# ![](multilayerqg_2layer.mp4)

Expand Down
Loading

0 comments on commit d7e2526

Please sign in to comment.