From e11e3bb0f3e2819622ee13771eb72b9784a1363a Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 13:45:44 +0200 Subject: [PATCH 1/6] use randn! for stochastic forcing --- examples/barotropicqgql_betaforced.jl | 15 ++++++++++----- examples/singlelayerqg_betaforced.jl | 15 ++++++++++----- examples/twodnavierstokes_stochasticforcing.jl | 15 ++++++++++----- .../twodnavierstokes_stochasticforcing_budgets.jl | 15 ++++++++++----- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/examples/barotropicqgql_betaforced.jl b/examples/barotropicqgql_betaforced.jl index 42d9694a..680dda5d 100644 --- a/examples/barotropicqgql_betaforced.jl +++ b/examples/barotropicqgql_betaforced.jl @@ -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 +# During that `randn!` is called to produce 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_uniform = dev==CPU() ? rand : CUDA.rand +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 diff --git a/examples/singlelayerqg_betaforced.jl b/examples/singlelayerqg_betaforced.jl index 5239e3e9..3a80e319 100644 --- a/examples/singlelayerqg_betaforced.jl +++ b/examples/singlelayerqg_betaforced.jl @@ -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 +# During that `randn!` is called to produce 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_uniform = dev==CPU() ? rand : CUDA.rand +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 diff --git a/examples/twodnavierstokes_stochasticforcing.jl b/examples/twodnavierstokes_stochasticforcing.jl index 1c6fccf2..69f6ff5b 100644 --- a/examples/twodnavierstokes_stochasticforcing.jl +++ b/examples/twodnavierstokes_stochasticforcing.jl @@ -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 +# During that `randn!` is called to produce 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_uniform = dev==CPU() ? rand : CUDA.rand +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 diff --git a/examples/twodnavierstokes_stochasticforcing_budgets.jl b/examples/twodnavierstokes_stochasticforcing_budgets.jl index 9ff5b582..cee89cfe 100644 --- a/examples/twodnavierstokes_stochasticforcing_budgets.jl +++ b/examples/twodnavierstokes_stochasticforcing_budgets.jl @@ -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 +# During that `randn!` is called to produce 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_uniform = dev==CPU() ? rand : CUDA.rand +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 From f4ef39413b656e65c4176d5d52ce19c68ff73957 Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 13:47:50 +0200 Subject: [PATCH 2/6] better line break --- examples/barotropicqgql_betaforced.jl | 6 +++--- examples/singlelayerqg_betaforced.jl | 6 +++--- examples/twodnavierstokes_stochasticforcing.jl | 6 +++--- examples/twodnavierstokes_stochasticforcing_budgets.jl | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/barotropicqgql_betaforced.jl b/examples/barotropicqgql_betaforced.jl index 680dda5d..459d5c3e 100644 --- a/examples/barotropicqgql_betaforced.jl +++ b/examples/barotropicqgql_betaforced.jl @@ -82,9 +82,9 @@ nothing #hide # During that `randn!` is called to produce 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. +# 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()") diff --git a/examples/singlelayerqg_betaforced.jl b/examples/singlelayerqg_betaforced.jl index 3a80e319..80d26ffc 100644 --- a/examples/singlelayerqg_betaforced.jl +++ b/examples/singlelayerqg_betaforced.jl @@ -83,9 +83,9 @@ nothing #hide # During that `randn!` is called to produce 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. +# 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()") diff --git a/examples/twodnavierstokes_stochasticforcing.jl b/examples/twodnavierstokes_stochasticforcing.jl index 69f6ff5b..cc486247 100644 --- a/examples/twodnavierstokes_stochasticforcing.jl +++ b/examples/twodnavierstokes_stochasticforcing.jl @@ -75,9 +75,9 @@ nothing #hide # During that `randn!` is called to produce 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. +# 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()") diff --git a/examples/twodnavierstokes_stochasticforcing_budgets.jl b/examples/twodnavierstokes_stochasticforcing_budgets.jl index cee89cfe..14178d47 100644 --- a/examples/twodnavierstokes_stochasticforcing_budgets.jl +++ b/examples/twodnavierstokes_stochasticforcing_budgets.jl @@ -77,9 +77,9 @@ nothing #hide # During that `randn!` is called to produce 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. +# 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()") From feb78dc29612869cf1bec12944338325cbc39682 Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 17:07:34 +0200 Subject: [PATCH 3/6] better phrasing --- docs/Project.toml | 1 + examples/barotropicqgql_betaforced.jl | 2 +- examples/singlelayerqg_betaforced.jl | 2 +- examples/twodnavierstokes_stochasticforcing.jl | 2 +- examples/twodnavierstokes_stochasticforcing_budgets.jl | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index cf889bcc..dfbe88eb 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -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" diff --git a/examples/barotropicqgql_betaforced.jl b/examples/barotropicqgql_betaforced.jl index 459d5c3e..4b561df4 100644 --- a/examples/barotropicqgql_betaforced.jl +++ b/examples/barotropicqgql_betaforced.jl @@ -79,7 +79,7 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. -# During that `randn!` is called to produce complex numbers whose real and imaginary part +# 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 diff --git a/examples/singlelayerqg_betaforced.jl b/examples/singlelayerqg_betaforced.jl index 80d26ffc..26631f94 100644 --- a/examples/singlelayerqg_betaforced.jl +++ b/examples/singlelayerqg_betaforced.jl @@ -80,7 +80,7 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. -# During that `randn!` is called to produce complex numbers whose real and imaginary part +# 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 diff --git a/examples/twodnavierstokes_stochasticforcing.jl b/examples/twodnavierstokes_stochasticforcing.jl index cc486247..0be511ad 100644 --- a/examples/twodnavierstokes_stochasticforcing.jl +++ b/examples/twodnavierstokes_stochasticforcing.jl @@ -72,7 +72,7 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. -# During that `randn!` is called to produce complex numbers whose real and imaginary part +# 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 diff --git a/examples/twodnavierstokes_stochasticforcing_budgets.jl b/examples/twodnavierstokes_stochasticforcing_budgets.jl index 14178d47..f148cac7 100644 --- a/examples/twodnavierstokes_stochasticforcing_budgets.jl +++ b/examples/twodnavierstokes_stochasticforcing_budgets.jl @@ -74,7 +74,7 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. -# During that `randn!` is called to produce complex numbers whose real and imaginary part +# 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 From cb37bc5aad41891402e9bbe6b05ee8d6be7faa94 Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 20:22:27 +0200 Subject: [PATCH 4/6] no need to use CUDA.randn --- examples/barotropicqgql_betaforced.jl | 10 +--------- examples/singlelayerqg_betaforced.jl | 10 +--------- examples/twodnavierstokes_stochasticforcing.jl | 9 +-------- examples/twodnavierstokes_stochasticforcing_budgets.jl | 10 +--------- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/examples/barotropicqgql_betaforced.jl b/examples/barotropicqgql_betaforced.jl index 4b561df4..558dbdd3 100644 --- a/examples/barotropicqgql_betaforced.jl +++ b/examples/barotropicqgql_betaforced.jl @@ -81,19 +81,11 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. # 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) - random_normal!(Fh) + randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing -end nothing #hide diff --git a/examples/singlelayerqg_betaforced.jl b/examples/singlelayerqg_betaforced.jl index 26631f94..d92eb23d 100644 --- a/examples/singlelayerqg_betaforced.jl +++ b/examples/singlelayerqg_betaforced.jl @@ -82,19 +82,11 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. # 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) - random_normal!(Fh) + randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing -end nothing #hide diff --git a/examples/twodnavierstokes_stochasticforcing.jl b/examples/twodnavierstokes_stochasticforcing.jl index 0be511ad..4ccb5919 100644 --- a/examples/twodnavierstokes_stochasticforcing.jl +++ b/examples/twodnavierstokes_stochasticforcing.jl @@ -74,16 +74,9 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. # 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) - random_normal!(Fh) + randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing end diff --git a/examples/twodnavierstokes_stochasticforcing_budgets.jl b/examples/twodnavierstokes_stochasticforcing_budgets.jl index f148cac7..e25d795e 100644 --- a/examples/twodnavierstokes_stochasticforcing_budgets.jl +++ b/examples/twodnavierstokes_stochasticforcing_budgets.jl @@ -76,19 +76,11 @@ nothing #hide # Next we construct function `calcF!` that computes a forcing realization every timestep. # 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) - random_normal!(Fh) + randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing -end nothing #hide From 4a2789144036c969b03888a9b1c2a59be2fe837c Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 20:43:57 +0200 Subject: [PATCH 5/6] add missing end --- examples/barotropicqgql_betaforced.jl | 1 + examples/singlelayerqg_betaforced.jl | 1 + examples/twodnavierstokes_stochasticforcing_budgets.jl | 1 + 3 files changed, 3 insertions(+) diff --git a/examples/barotropicqgql_betaforced.jl b/examples/barotropicqgql_betaforced.jl index 558dbdd3..3040adc0 100644 --- a/examples/barotropicqgql_betaforced.jl +++ b/examples/barotropicqgql_betaforced.jl @@ -86,6 +86,7 @@ function calcF!(Fh, sol, t, clock, vars, params, grid) randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing +end nothing #hide diff --git a/examples/singlelayerqg_betaforced.jl b/examples/singlelayerqg_betaforced.jl index d92eb23d..234629d6 100644 --- a/examples/singlelayerqg_betaforced.jl +++ b/examples/singlelayerqg_betaforced.jl @@ -87,6 +87,7 @@ function calcF!(Fh, sol, t, clock, vars, params, grid) randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing +end nothing #hide diff --git a/examples/twodnavierstokes_stochasticforcing_budgets.jl b/examples/twodnavierstokes_stochasticforcing_budgets.jl index e25d795e..bdb2484d 100644 --- a/examples/twodnavierstokes_stochasticforcing_budgets.jl +++ b/examples/twodnavierstokes_stochasticforcing_budgets.jl @@ -81,6 +81,7 @@ function calcF!(Fh, sol, t, clock, vars, params, grid) randn!(Fh) @. Fh *= sqrt(forcing_spectrum) / sqrt(clock.dt) return nothing +end nothing #hide From 9ac215a50be83c948a8ea200ecdcde82e4246596 Mon Sep 17 00:00:00 2001 From: "Navid C. Constantinou" Date: Sun, 10 Mar 2024 20:51:35 +0200 Subject: [PATCH 6/6] remove GeophysicalFlows --- docs/Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/Project.toml b/docs/Project.toml index dfbe88eb..cf889bcc 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -3,7 +3,6 @@ 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"