-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define a custom type for the random field
With this change we also take advantage of a new change introduced in GaussianRandomFields v2.1.1 that allows us to reduce memory allocations when sampling. For example, with current master: ```julia julia> using BenchmarkTools, TDAC julia> x = 1.:200.; julia> y = 1.:200.; julia> grf = TDAC.init_gaussian_random_field_generator(1.0,1.0,1.0,x,y,0); julia> f = zeros(40000); julia> rnn = rand(160000); julia> @Btime TDAC.sample_gaussian_random_field!($f, $grf, $rnn); 1.461 ms (10 allocations: 2.75 MiB) ``` With this change: ```julia julia> using BenchmarkTools, TDAC julia> x = 1.:200.; julia> y = 1.:200.; julia> grf = TDAC.init_gaussian_random_field_generator(1.0,1.0,1.0,x,y,0,true); julia> f = zeros(40000); julia> rnn = randn(size(grf.grf.data[1])); julia> @Btime TDAC.sample_gaussian_random_field!($f, $grf, $rnn); 1.366 ms (3 allocations: 128 bytes) ``` The speed-up is modest, the major benefit is from having much less memory allocations.
- Loading branch information
Showing
5 changed files
with
122 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using GaussianRandomFields | ||
|
||
struct RandomField{F<:GaussianRandomField,W<:AbstractArray,Z<:AbstractArray} | ||
grf::F | ||
w::W | ||
z::Z | ||
end | ||
|
||
function init_gaussian_random_field_generator(params::tdac_params) | ||
|
||
x, y = get_axes(params) | ||
return init_gaussian_random_field_generator(params.lambda,params.nu, params.sigma, x, y, params.padding, params.primes) | ||
|
||
end | ||
|
||
# Initialize a gaussian random field generating function using the Matern covariance kernel | ||
# and circulant embedding generation method | ||
# TODO: Could generalise this | ||
function init_gaussian_random_field_generator(lambda::T, | ||
nu::T, | ||
sigma::T, | ||
x::AbstractVector{T}, | ||
y::AbstractVector{T}, | ||
pad::Int, | ||
primes::Bool) where T | ||
|
||
# Let's limit ourselves to two-dimensional fields | ||
dim = 2 | ||
|
||
cov = CovarianceFunction(dim, Matern(lambda, nu, σ = sigma)) | ||
grf = GaussianRandomField(cov, CirculantEmbedding(), x, y, minpadding=pad, primes=primes) | ||
v = grf.data[1] | ||
w = Array{complex(float(eltype(v)))}(undef, size(v)) | ||
z = Array{eltype(grf.cov)}(undef, length.(grf.pts)) | ||
|
||
return RandomField(grf, w, z) | ||
end | ||
|
||
# Get a random sample from gaussian random field grf using random number generator rng | ||
function sample_gaussian_random_field!(field::AbstractVector{T}, | ||
grf::RandomField, | ||
rng::Random.AbstractRNG) where T | ||
|
||
field .= @view(GaussianRandomFields._sample!(grf.w, grf.z, grf.grf, randn(rng, size(grf.grf.data[1])))[:]) | ||
|
||
end | ||
|
||
# Get a random sample from gaussian random field grf using random_numbers | ||
function sample_gaussian_random_field!(field::AbstractVector{T}, | ||
grf::RandomField, | ||
random_numbers::AbstractArray{T}) where T | ||
|
||
field .= @view(GaussianRandomFields._sample!(grf.w, grf.z, grf.grf, random_numbers)[:]) | ||
|
||
end | ||
|
||
function add_random_field!(state::AbstractVector{T}, | ||
grf::RandomField, | ||
rng::Random.AbstractRNG, | ||
params::tdac_params) where T | ||
|
||
add_random_field!(state, grf, rng, params.n_state_var, params.dim_grid) | ||
|
||
end | ||
|
||
# Add a gaussian random field to each variable in the state vector of one particle | ||
function add_random_field!(state::AbstractVector{T}, | ||
grf::RandomField, | ||
rng::Random.AbstractRNG, | ||
nvar::Int, | ||
dim_grid::Int) where T | ||
|
||
random_field = Vector{Float64}(undef, dim_grid) | ||
|
||
for ivar in 1:nvar | ||
|
||
sample_gaussian_random_field!(random_field, grf, rng) | ||
@view(state[(nvar-1)*dim_grid+1:nvar*dim_grid]) .+= random_field | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters