Skip to content

Commit

Permalink
Solve compatibility issues with Sleopnir 0.3.0 in PDE and conservatio…
Browse files Browse the repository at this point in the history
…n of mass test
  • Loading branch information
facusapienza21 committed Aug 5, 2023
1 parent 5d7f8f2 commit ecf4f0a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
36 changes: 21 additions & 15 deletions test/PDE_UDE_solve.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@


function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::Bool=true) where {F <: AbstractFloat}
function pde_solve_test(; rtol::F, atol::F, save_refs::Bool=false, MB::Bool=false, fast::Bool=true) where {F <: AbstractFloat}

println("PDE solving with MB = $MB")
working_dir = joinpath(homedir(), "OGGM/Huginn_tests")

Expand All @@ -9,9 +10,10 @@ function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::
workers=2,
ice_thickness_source = "Farinotti19"),
simulation = SimulationParameters(use_MB=MB,
velocities=false,
tspan=(2010.0, 2015.0),
working_dir = Huginn.root_dir)
velocities=false,
tspan=(2010.0, 2015.0),
working_dir = Huginn.root_dir),
solver = SolverParameters(reltol=1e-12)
)

## Retrieving gdirs and climate for the following glaciers
Expand All @@ -24,13 +26,6 @@ function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::
"RGI60-07.00274", "RGI60-07.01323", "RGI60-01.17316"]
end

# Load reference values for the simulation
if MB
PDE_refs = load(joinpath(Huginn.root_dir, "test/data/PDE/PDE_refs_MB.jld2"))["results"]
else
PDE_refs = load(joinpath(Huginn.root_dir, "test/data/PDE/PDE_refs_noMB.jld2"))["results"]
end

model = Model(iceflow = SIA2Dmodel(params))

# We retrieve some glaciers for the simulation
Expand All @@ -51,6 +46,13 @@ function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::
end
end

# Load reference values for the simulation
if MB
PDE_refs = load(joinpath(Huginn.root_dir, "test/data/PDE/PDE_refs_MB.jld2"))["results"]
else
PDE_refs = load(joinpath(Huginn.root_dir, "test/data/PDE/PDE_refs_noMB.jld2"))["results"]
end

# # Run one epoch of the UDE training
# θ = zeros(10) # dummy data for the NN
# UA_f = zeros(10)
Expand All @@ -59,6 +61,7 @@ function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::
# H_V_preds = @time predict_iceflow(θ, UA_f, gdirs, context_batches, UDE_settings, mb_model) # Array{(H_pred, V̄x_pred, V̄y_pred, rgi_id)}

let results=prediction.results

for result in results
let result=result, test_ref=nothing, UDE_pred=nothing
for PDE_ref in PDE_refs
Expand All @@ -79,19 +82,22 @@ function pde_solve_test(atol::F, save_refs::Bool = false; MB::Bool=false, fast::
mkdir(test_plot_path)
end
MB ? vtol = 30.0*atol : vtol = 12.0*atol # a little extra tolerance for UDE surface velocities

### PDE ###
plot_test_error(result, test_ref, "H", result.rgi_id, atol, MB)
plot_test_error(result, test_ref, "H", result.rgi_id, atol, MB)
plot_test_error(result, test_ref, "Vx", result.rgi_id, vtol, MB)
plot_test_error(result, test_ref, "Vy", result.rgi_id, vtol, MB)

### UDE ###
# Huginn.plot_test_error(UDE_pred, test_ref, "H", rgi_id, atol, MB)
# Huginn.plot_test_error(UDE_pred, test_ref, "Vx", rgi_id, vtol, MB)
# Huginn.plot_test_error(UDE_pred, test_ref, "Vy", rgi_id, vtol, MB)

# Test that the PDE simulations are correct
@test all(isapprox.(result.H[end], test_ref.H[end], atol=atol))
@test all(isapprox.(result.Vx, test_ref.Vx, atol=vtol))
@test all(isapprox.(result.Vy, test_ref.Vy, atol=vtol))
@test all(isapprox.(result.H[end], test_ref.H[end], rtol=rtol, atol=atol))
@test all(isapprox.(result.Vx, test_ref.Vx, rtol=rtol, atol=vtol))
@test all(isapprox.(result.Vy, test_ref.Vy, rtol=rtol, atol=vtol))

# Test that the UDE simulations are correct
# @test all(isapprox.(UDE_pred[1], test_ref["H"], atol=atol))
# @test all(isapprox.(UDE_pred[2], test_ref["Vx"], atol=vtol))
Expand Down
Binary file modified test/data/PDE/PDE_refs_noMB.jld2
Binary file not shown.
10 changes: 5 additions & 5 deletions test/mass_conservation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ Arguments
- `rtol`: Relative tolerance
- `save_plot`: Optional bool to save plot during simulation
"""
function unit_mass_test(; H₀, B, A, t_sim, Δx, Δy, rtol=0.02, save_plot=false)
function unit_mass_test(; H₀, B, A, n, t_sim, Δx, Δy, rtol=0.02, save_plot=false)

# Get parameters for a simulation
parameters = Parameters(simulation=SimulationParameters(tspan=(0.0, t_sim),
use_MB=false,
use_iceflow=true,
multiprocessing=true,
workers=1),
physical=PhysicalParameters(A=A),
physical=PhysicalParameters(),
solver=SolverParameters(reltol=1e-12))

model = Model(iceflow = SIA2Dmodel(parameters))
Expand All @@ -39,8 +39,8 @@ function unit_mass_test(; H₀, B, A, t_sim, Δx, Δy, rtol=0.02, save_plot=fals
nx, ny = size(H₀)

# Define glacier object
glacier = Glacier(rgi_id = "toy", H₀ = H₀, S = S, B = B,
Δx=Δx, Δy=Δy, nx=nx, ny=ny)
glacier = Glacier2D(rgi_id = "toy", H₀ = H₀, S = S, B = B, A = A, n = n,
Δx=Δx, Δy=Δy, nx=nx, ny=ny)
glaciers = Vector{Sleipnir.AbstractGlacier}([glacier])

prediction = Prediction(model, glaciers, parameters)
Expand Down Expand Up @@ -99,7 +99,7 @@ function unit_mass_flatbed_test(; rtol)
H₀ = zeros((nx,ny))
@views H₀[floor(Int,nx/3):floor(Int,2nx/3), floor(Int,ny/3):floor(Int,2ny/3)] .= 400
end
unit_mass_test(; H₀=H₀, B=B, A=A, t_sim=10.0, Δx=50.0, Δy=50.0, rtol=rtol, save_plot=false)
unit_mass_test(; H₀=H₀, B=B, A=A, n=3.0, t_sim=10.0, Δx=50.0, Δy=50.0, rtol=rtol, save_plot=false)
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ ENV["GKSwstype"]="nul"

@testset "Solver parameters construction with default variables" params_constructor_default()

@testset "Halfar Solution" halfar_test(rtol=0.02, atol=1.0)
@testset "Halfar Solution" halfar_test(; rtol=0.02, atol=1.0)

atol = 0.01
@testset "PDE solving integration tests" pde_solve_test(atol; MB=false, fast=true)
@testset "PDE solving integration tests" pde_solve_test(; rtol=0.01, atol=0.01, save_refs=false, MB=false, fast=true)

@testset "Conservation of Mass - Flat Bed" unit_mass_flatbed_test(rtol=1.0e-7)
@testset "Conservation of Mass - Flat Bed" unit_mass_flatbed_test(; rtol=1.0e-7)

0 comments on commit ecf4f0a

Please sign in to comment.