FourierFlows / GeophysicalFlows.jl

Geophysical fluid dynamics pseudospectral solvers with Julia and FourierFlows.jl.
https://fourierflows.github.io/GeophysicalFlowsDocumentation/stable/
MIT License
153 stars 31 forks source link

Faster and with reduced memory allocations `calcF!` function in examples with stochastic noise #316

Closed navidcy closed 1 year ago

navidcy commented 1 year ago

This PR makes the calcF! functions for stochastic forcing faster and with fewer allocations.

julia> using BenchmarkTools

julia> function calcF!(Fh, sol, t, clock, vars, params, grid)
         Fh .= sqrt.(forcing_spectrum) .* exp.(2π * im * random_uniform(eltype(grid), size(sol))) ./ sqrt(clock.dt)

         return nothing
       end
calcF! (generic function with 1 method)

julia> function calcF_new!(Fh, sol, t, clock, vars, params, grid)
         @. Fh = sqrt(forcing_spectrum) * cis(2random_uniform()) / sqrt(clock.dt)

         return nothing
       end
calcF_new! (generic function with 1 method)

julia> @btime calcF!(Fh, sol, t, clock, vars, params, grid)
  152.208 μs (14 allocations: 195.33 KiB)

julia> @btime calcF_new!(Fh, sol, t, clock, vars, params, grid)
  111.416 μs (11 allocations: 240 bytes)

The PR also fixes some links to Makie package in the docs.