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

Use randn! for stochastic forcing implementations #351

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
GeophysicalFlows = "44ee3b1c-bc02-53fa-8355-8e347616e15e"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
17 changes: 11 additions & 6 deletions examples/barotropicqgql_betaforced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,19 @@ nothing #hide


# Next we construct function `calcF!` that computes a forcing realization every timestep.
# First we make sure that if `dev=GPU()`, then `CUDA.rand()` function is called for random
# numbers uniformly distributed between 0 and 1.
random_uniform = dev==CPU() ? rand : CUDA.rand
# For that, we call `randn!` to obtain complex numbers whose real and imaginary part
# are normally-distributed with zero mean and variance 1/2.
#
# We ensure that either `Random.randn!` or `CUDA.randn! is called according to the chosen
# `dev`, then `CUDA.rand()` function is called for random numbers uniformly distributed
# between 0 and 1.
random_normal! = dev==CPU() ? Random.randn! :
dev==GPU() ? CUDA.randn! :
error("dev must be CPU() or GPU()")

function calcF!(Fh, sol, t, clock, vars, params, grid)
T = eltype(grid)
@. Fh = sqrt(forcing_spectrum) * cis(2π * random_uniform(T)) / sqrt(clock.dt)

random_normal!(Fh)
@. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt)
return nothing
end
nothing #hide
Expand Down
17 changes: 11 additions & 6 deletions examples/singlelayerqg_betaforced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,19 @@ nothing #hide


# Next we construct function `calcF!` that computes a forcing realization every timestep.
# First we make sure that if `dev=GPU()`, then `CUDA.rand()` function is called for random
# numbers uniformly distributed between 0 and 1.
random_uniform = dev==CPU() ? rand : CUDA.rand
# For that, we call `randn!` to obtain complex numbers whose real and imaginary part
# are normally-distributed with zero mean and variance 1/2.
#
# We ensure that either `Random.randn!` or `CUDA.randn! is called according to the chosen
# `dev`, then `CUDA.rand()` function is called for random numbers uniformly distributed
# between 0 and 1.
random_normal! = dev==CPU() ? Random.randn! :
dev==GPU() ? CUDA.randn! :
error("dev must be CPU() or GPU()")

function calcF!(Fh, sol, t, clock, vars, params, grid)
T = eltype(grid)
@. Fh = sqrt(forcing_spectrum) * cis(2π * random_uniform(T)) / sqrt(clock.dt)

random_normal!(Fh)
@. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt)
return nothing
end
nothing #hide
Expand Down
17 changes: 11 additions & 6 deletions examples/twodnavierstokes_stochasticforcing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ nothing #hide


# Next we construct function `calcF!` that computes a forcing realization every timestep.
# First we make sure that if `dev=GPU()`, then `CUDA.rand()` function is called for random
# numbers uniformly distributed between 0 and 1.
random_uniform = dev==CPU() ? rand : CUDA.rand
# For that, we call `randn!` to obtain complex numbers whose real and imaginary part
# are normally-distributed with zero mean and variance 1/2.
#
# We ensure that either `Random.randn!` or `CUDA.randn! is called according to the chosen
# `dev`, then `CUDA.rand()` function is called for random numbers uniformly distributed
# between 0 and 1.
random_normal! = dev==CPU() ? Random.randn! :
dev==GPU() ? CUDA.randn! :
error("dev must be CPU() or GPU()")

function calcF!(Fh, sol, t, clock, vars, params, grid)
T = eltype(grid)
@. Fh = sqrt(forcing_spectrum) * cis(2π * random_uniform(T)) / sqrt(clock.dt)

random_normal!(Fh)
@. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt)
return nothing
end
nothing #hide
Expand Down
17 changes: 11 additions & 6 deletions examples/twodnavierstokes_stochasticforcing_budgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ nothing #hide


# Next we construct function `calcF!` that computes a forcing realization every timestep.
# First we make sure that if `dev=GPU()`, then `CUDA.rand()` function is called for random
# numbers uniformly distributed between 0 and 1.
random_uniform = dev==CPU() ? rand : CUDA.rand
# For that, we call `randn!` to obtain complex numbers whose real and imaginary part
# are normally-distributed with zero mean and variance 1/2.
#
# We ensure that either `Random.randn!` or `CUDA.randn! is called according to the chosen
# `dev`, then `CUDA.rand()` function is called for random numbers uniformly distributed
# between 0 and 1.
random_normal! = dev==CPU() ? Random.randn! :
dev==GPU() ? CUDA.randn! :
error("dev must be CPU() or GPU()")

function calcF!(Fh, sol, t, clock, vars, params, grid)
T = eltype(grid)
@. Fh = sqrt(forcing_spectrum) * cis(2π * random_uniform(T)) / sqrt(clock.dt)

random_normal!(Fh)
@. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt)
return nothing
end
nothing #hide
Expand Down
Loading