Skip to content
This repository was archived by the owner on Aug 29, 2022. It is now read-only.

Commit 1256fdc

Browse files
committed
FFTGS simulation on view of grid
1 parent 0441371 commit 1256fdc

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/fft.jl

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ FFT Gaussian simulation.
2323
Fourier transform method](https://link.springer.com/article/10.1007/BF02769641)
2424
"""
2525
@simsolver FFTGS begin
26-
@param variogram = GaussianVariogram()
27-
@param mean = 0.0
26+
@param variogram = GaussianVariogram()
27+
@param mean = 0.0
2828
@global threads = cpucores()
2929
@global rng = Random.GLOBAL_RNG
3030
end
@@ -33,11 +33,12 @@ function preprocess(problem::SimulationProblem, solver::FFTGS)
3333
hasdata(problem) && @error "conditional simulation is not implemented"
3434

3535
# retrieve problem info
36-
pdomain = domain(problem)
37-
dims = size(pdomain)
38-
nelms = nelements(pdomain)
39-
center = CartesianIndex(dims 2)
40-
cindex = LinearIndices(dims)[center]
36+
pdomain = domain(problem)
37+
pgrid, _ = unview(pdomain)
38+
dims = size(pgrid)
39+
nelms = nelements(pgrid)
40+
center = CartesianIndex(dims 2)
41+
cindex = LinearIndices(dims)[center]
4142

4243
# number of threads in FFTW
4344
FFTW.set_num_threads(solver.threads)
@@ -63,8 +64,8 @@ function preprocess(problem::SimulationProblem, solver::FFTGS)
6364
@assert isstationary(γ) "variogram model must be stationary"
6465

6566
# compute covariances between centroid and all points
66-
𝒟c = [centroid(pdomain, cindex)]
67-
𝒟p = [centroid(pdomain, eindex) for eindex in 1:nelms]
67+
𝒟c = [centroid(pgrid, cindex)]
68+
𝒟p = [centroid(pgrid, eindex) for eindex in 1:nelms]
6869
covs = sill(γ) .- pairwise(γ, 𝒟c, 𝒟p)
6970
C = reshape(covs, dims)
7071

@@ -85,8 +86,9 @@ function solvesingle(problem::SimulationProblem, covars::NamedTuple, solver::FFT
8586
rng = solver.rng
8687

8788
# retrieve problem info
88-
pdomain = domain(problem)
89-
dims = size(pdomain)
89+
pdomain = domain(problem)
90+
pgrid, inds = unview(pdomain)
91+
dims = size(pgrid)
9092

9193
mactypeof = Dict(name(v) => mactype(v) for v in variables(problem))
9294

@@ -108,7 +110,7 @@ function solvesingle(problem::SimulationProblem, covars::NamedTuple, solver::FFT
108110
Z .= (sill(γ) / σ²) .* Z .+ μ
109111

110112
# flatten result
111-
var => vec(Z)
113+
var => Z[inds]
112114
end
113115

114116
Dict(varreal)

test/fft.jl

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
@testset "FFTGS" begin
2-
𝒟 = CartesianGrid(100,100)
3-
problem = SimulationProblem(𝒟, :z=>Float64, 3)
4-
2+
# isotropic simulation
53
Random.seed!(2019)
6-
solver = FFTGS(:z => (variogram=GaussianVariogram(range=10.),))
7-
sol = solve(problem, solver)
4+
problem = SimulationProblem(CartesianGrid(100,100), :z=>Float64, 3)
5+
solver = FFTGS(:z => (variogram=GaussianVariogram(range=10.),))
6+
sol = solve(problem, solver)
87

98
if visualtests
109
@test_reference "data/FFT-iso.png" plot(sol,size=(900,300))
1110
end
1211

12+
# anisotropic simulation
1313
Random.seed!(2019)
14-
ball = MetricBall((20.,5.))
15-
solver = FFTGS(:z => (variogram=GaussianVariogram(ball),))
14+
problem = SimulationProblem(CartesianGrid(100,100), :z=>Float64, 3)
15+
solver = FFTGS(:z => (variogram=GaussianVariogram(MetricBall((20.,5.))),))
1616
sol = solve(problem, solver)
1717

1818
if visualtests
1919
@test_reference "data/FFT-aniso.png" plot(sol,size=(900,300))
2020
end
21+
22+
# simulation on view of grid
23+
Random.seed!(2022)
24+
grid = CartesianGrid(100,100)
25+
vgrid = view(grid, 1:5000)
26+
problem = SimulationProblem(vgrid, :z=>Float64, 3)
27+
solver = FFTGS(:z => (variogram=GaussianVariogram(range=10.),))
28+
sol = solve(problem, solver)
29+
@test domain(sol) == vgrid
30+
@test length(sol[1].z) == 5000
2131
end

0 commit comments

Comments
 (0)