Skip to content

Commit

Permalink
Added plotting function to check influence of flow parameters A and n (
Browse files Browse the repository at this point in the history
…#39)

* Added function to check influence of A and n

* Fixed small fatal error

* Added test for glacier analysis

* Fixed naming and layout issues

* Fixed test for plotting utility

* fixed plotting test

* Updated Sleipnir V0.4.0 and Muninn V0.2.6

* Changed plotting utility to utilise params object

* small fixes

* removed PDE ref test files

* New PDE refs test files
  • Loading branch information
vivekag7 authored Dec 12, 2023
1 parent 171ad4f commit 2985440
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 2 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.3.1"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Expand Down
5 changes: 4 additions & 1 deletion src/Huginn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Tullio
using Infiltrator
using Plots, PlotThemes
Plots.theme(:wong2) # sets overall theme for Plots
# using CairoMakie, GeoMakie
using CairoMakie
import Pkg
using Distributed
using ProgressMeter
Expand Down Expand Up @@ -65,4 +65,7 @@ include("models/iceflow/IceflowModel.jl")
# Everything related to running forward simulations of ice flow
include("simulations/predictions/Prediction.jl")

# Everything related to plotting
include("plotting/plotting_utils.jl")

end # module
138 changes: 138 additions & 0 deletions src/plotting/plotting_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
export plot_analysis_flow_parameters

########################################################
######## Flow-parameter analysing functions ###########
########################################################

"""
plot_analysis_flow_parameters(tspan, A_values, n_values, rgi_ids)
Generate and plot the difference in ice thickness for a specified glacier over a time span `tspan`, for varying parameters `A_values` and `n_values`.
# Arguments
- `tspan::Tuple`: A tuple specifying the start and end years for the analysis.
- `A_values::Array`: An array of A values (rate factor in Glen's flow law) to be used in the simulation.
- `n_values::Array`: An array of n values (flow law exponent in Glen's flow law) to be used in the simulation.
- `rgi_ids::Array`: An array containing the RGI (Randolph Glacier Inventory) ID of the glacier to be analyzed.
# Returns
- `Figure`: A Makie.jl figure object containing the generated plots.
# Constraints
- Supports a maximum grid size of 5x5 (lengths of `A_values` and `n_values` each should not exceed 5).
- Only supports analysis for one glacier at a time (length of `rgi_ids` should be 1).
"""

function plot_analysis_flow_parameters(
params,
A_values,
n_values,
rgi_ids,
;iceflow_model=SIA2Dmodel,
mass_balance_model=TImodel1,

)
# Calculate the size of the grid
rows = length(n_values)
cols = length(A_values)

if rows > 5 || cols > 5
error("more than a 5x5 grid is not supported")
end

if length(rgi_ids) > 1
error("only one glacier at a time is supported")
end

result = [
generate_result(
params, A_values[j], n_values[i], rgi_ids,
iceflow_model, mass_balance_model
) for i in 1:rows, j in 1:cols
]
h_diff = [result[i,j].H[end]-result[i,j].H[1] for i in 1:rows, j in 1:cols]


Δx = hasproperty(result[1,1], :Δx) ? result[1,1].Δx : 0

#Extract longitude and latitude
lon = hasproperty(result[1,1], :lon) ? result[1,1].lon : "none"
lat = hasproperty(result[1,1], :lat) ? result[1,1].lat : "none"


ny, nx = size(h_diff[1,1])
h_diff = [reverse(h_diff[i,j]', dims=2) for i in 1:rows, j in 1:cols]

scale_width = 0.10*nx
scale_number = round(Δx * scale_width / 1000; digits=1)
textsize=1.2*scale_width/max(rows,cols)

max_abs_value = max(abs(minimum(reduce(vcat, [vec(matrix) for matrix in h_diff]))), abs(maximum(reduce(vcat, [vec(matrix) for matrix in h_diff]))))

# Initialize the figure
fig = Makie.Figure(size = (800, 600),layout=GridLayout(rows, cols))

# Iterate over each combination of A and n values
for i in 1:rows
for j in 1:cols
ax_diff = Makie.Axis(fig[i, j],aspect=DataAspect())
hm_diff = Makie.heatmap!(ax_diff, h_diff[i,j],colormap=:redsblues,colorrange=(-max_abs_value, max_abs_value))

ax_diff.xlabel = "A= $(A_values[j]), n= $(n_values[i])"
ax_diff.xticklabelsvisible=false
ax_diff.yticklabelsvisible=false


if max(rows,cols) == 5
ax_diff.xlabelsize = 12.0
end

Makie.poly!(ax_diff, Rect(nx -round(0.15*nx) , round(0.075*ny), scale_width, scale_width/10), color=:black)
Makie.text!(ax_diff, "$scale_number km",
position = (nx - round(0.15*nx)+scale_width/16, round(0.075*ny)+scale_width/10),
fontsize=textsize)

if i == rows && j == cols
Makie.Colorbar(fig[1:end,cols+1],hm_diff)
end
end
end
rgi_id=rgi_ids[1]
start_year, end_year = round.(Int, params.simulation.tspan)
fig[0, :] = Label(fig, "Ice Thickness difference ΔH for varying A and n from $start_year to $end_year")

fig[rows+1, :] = Label(fig, "$rgi_id - latitude = $lat ° - longitude = $lon ° - scale = $scale_number km ")
return fig
end

function generate_result(
params,
A,
n,
rgi_ids,
iceflow_model,
mass_balance_model,

)

# Initialize the model using the specified or default models
model = Model(
iceflow = iceflow_model(params, n=n, A=A),
mass_balance = mass_balance_model(params)
)

# Initialize glaciers and run prediction
glaciers = initialize_glaciers(rgi_ids, params)
prediction = Prediction(model, glaciers, params)
run!(prediction)


# Extract the first result
result = prediction.results[1]



return result
end


Binary file modified test/data/PDE/PDE_refs_MB.jld2
Binary file not shown.
Binary file modified test/data/PDE/PDE_refs_noMB.jld2
Binary file not shown.
37 changes: 37 additions & 0 deletions test/plotting.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function plot_analysis_flow_parameters_test()

working_dir = joinpath(dirname(Base.current_project()), "data")
if !ispath(working_dir)
mkdir("data")
end

params = Parameters(OGGM = OGGMparameters(working_dir=working_dir,
multiprocessing=true,
workers=2,
ice_thickness_source = "Farinotti19"),
simulation = SimulationParameters(use_MB=true,
use_iceflow= true,
tspan=(2000.0, 2015.0),
working_dir = working_dir,
multiprocessing=true,
workers=2),
solver = SolverParameters(reltol=1e-8)
)


# Test for valid input
A_values = [8.5e-20]
n_values = [3.0]
rgi_ids = ["RGI60-11.01450"]

try
plot_analysis_flow_parameters(params, A_values, n_values, rgi_ids)
@test true # Test passes if no error is thrown
catch e
println("Error occurred: ", e)
@test false # Test fails if any error is caught

end

end

4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ using JLD2
using Plots
using Infiltrator
using OrdinaryDiffEq

using CairoMakie
using Huginn

include("utils_test.jl")
include("params_construction.jl")
include("halfar.jl")
include("PDE_UDE_solve.jl")
include("mass_conservation.jl")
include("plotting.jl")

# Activate to avoid GKS backend Plot issues in the JupyterHub
ENV["GKSwstype"]="nul"
Expand All @@ -35,3 +36,4 @@ ENV["GKSwstype"]="nul"

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

@testset "Glacier Plotting" plot_analysis_flow_parameters_test()

2 comments on commit 2985440

@JordiBolibar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96962

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 2985440332d7732cd8aa1b9ee13b4c83d67d727e
git push origin v0.3.1

Please sign in to comment.