Skip to content

Commit

Permalink
Merge pull request #138 from Team-RADDISH/tk/initheight
Browse files Browse the repository at this point in the history
Refactor llw2d.initheight
  • Loading branch information
tkoskela committed Jan 11, 2021
2 parents 470de27 + da5c9b9 commit 2c60f1a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
49 changes: 27 additions & 22 deletions test/model/llw2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,41 +205,46 @@ function setup(nx::Int,
land_filter_m, land_filter_n, land_filter_e)
end

# Initializes height with a cosine wave with a peak height of 1.0
# located at 1/4 of x and y axis.
# Calculates the initial surface height at point x,y
function get_initial_height(x::T, y::T, peak_x::T, peak_y::T, peak_height::T, cutoff::T) where T

dx = peak_x - x
dy = peak_y - y
distance_to_peak = sqrt(dx^2 + dy^2)

if distance_to_peak <= cutoff
return peak_height / 4.0 * ((1 + cospi(dx / cutoff)) * (1 + cospi(dy / cutoff)))
else
return 0.0
end

end

# Initializes the surface height on all points of the grid
function initheight!(height::AbstractMatrix{T},
ocean_depth::AbstractMatrix{T},
dx::Real,dy::Real,cutoff_distance::Real) where T
dx::Real,dy::Real,cutoff_distance::Real,
peak_height::Real, peak_position::AbstractVector{T}) where T

@assert size(height) == size(ocean_depth)

nx, ny = size(height)

peak_position = [floor(Int, nx / 4) * dx, floor(Int, ny / 4) * dy]
distance_to_peak = zeros(2)

for iy in 1:ny
for ix in 1:nx
vector_to_peak = peak_position - [ix * dx, iy * dy]
distance_to_peak = sqrt(sum((vector_to_peak).^2))

if distance_to_peak <= cutoff_distance && ocean_depth[ix, iy] >= eps(T)
height[ix, iy] = 0.25 * ((1 + cospi(vector_to_peak[1] / cutoff_distance))
* (1 + cospi(vector_to_peak[2] / cutoff_distance)))
else
height[ix, iy] = 0.0
end
end
end
x = (1:nx) .* dx
y = (1:ny) .* dy

return height
height .= get_initial_height.(x', y, peak_position[1], peak_position[2], peak_height, cutoff_distance)
height[ocean_depth .< eps(T)] .= 0.0

end

# Initializes the surface height on all points of the grid
function initheight!(height::AbstractMatrix{T},
matrices::Matrices{T},
dx::Real,dy::Real,cutoff_distance::Real) where T
dx::Real,dy::Real,cutoff_distance::Real,
peak_height::Real, peak_position::AbstractVector{T}) where T
# Unpack the relevant field of `matrices`
return initheight!(height, matrices.ocean_depth, dx, dy, cutoff_distance)
initheight!(height, matrices.ocean_depth, dx, dy, cutoff_distance, peak_height, peak_position)
end

end # module
5 changes: 4 additions & 1 deletion test/model/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Base.@kwdef struct ModelParameters{T<:AbstractFloat}

source_size::T = 3.0e4
bathymetry_setup::T = 3.0e4
peak_height = 1.0
peak_position = [floor(Int, nx / 4) * dx, floor(Int, ny / 4) * dy]

lambda::T = 1.0e4
nu::T = 2.5
Expand Down Expand Up @@ -346,7 +348,8 @@ function set_initial_state!(states::StateVectors, model_matrices::LLW2d.Matrices
params::ModelParameters) where T

# Set true initial state
LLW2d.initheight!(@view(states.truth[:, :, 1]), model_matrices, params.dx, params.dy, params.source_size)
LLW2d.initheight!(@view(states.truth[:, :, 1]), model_matrices, params.dx, params.dy,
params.source_size, params.peak_height, params.peak_position)

# Create generator for the initial random field
x,y = get_axes(params)
Expand Down
16 changes: 10 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ using .Model: ModelParameters
@test jst == [75, 85, 95, 75, 85, 95, 75, 85, 95]

### initheight!
eta = ones(2, 2)
ocean_depth = ones(2, 2)
LLW2d.initheight!(eta, ocean_depth, dx, dy, 3e4)
nx = 2
ny = 2
eta = ones(nx, ny)
ocean_depth = ones(nx, ny)
peak_position = [floor(Int, nx/4) * dx, floor(Int, ny/4) * dy]
LLW2d.initheight!(eta, ocean_depth, dx, dy, 3e4, 1.0, peak_position)
@test eta [0.978266982572228 0.9463188389826958;
0.9463188389826958 0.9154140546161575]
eta = ones(2, 2)
ocean_depth = zeros(2, 2)
LLW2d.initheight!(eta, ocean_depth, dx, dy, 3e4)
LLW2d.initheight!(eta, ocean_depth, dx, dy, 3e4, 1.0, peak_position)
@test eta zeros(2, 2)

# timestep. TODO: add real tests. So far we're just making sure code won't
Expand Down Expand Up @@ -138,8 +141,9 @@ end

# Initialise and update a tsunami on a small grid
s = 4e3
eta = reshape(@view(x[1:nx*ny]), nx, ny)
LLW2d.initheight!(eta, model_matrices, dx, dy, s)
peak_position = [floor(Int, nx/4) * dx, floor(Int, ny/4) * dy]
eta = @view(x[:,:,1])
LLW2d.initheight!(eta, model_matrices, dx, dy, s, 1.0, peak_position)
@test eta[2,2] 1.0
@test sum(eta) 4.0
Model.tsunami_update!(dxeta, dyeta, x, nt, dx, dy, dt, model_matrices)
Expand Down

0 comments on commit 2c60f1a

Please sign in to comment.