From 2985440332d7732cd8aa1b9ee13b4c83d67d727e Mon Sep 17 00:00:00 2001 From: Vivek <146078322+vivekag7@users.noreply.github.com> Date: Tue, 12 Dec 2023 18:01:37 +0100 Subject: [PATCH] Added plotting function to check influence of flow parameters A and n (#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 --- Project.toml | 1 + src/Huginn.jl | 5 +- src/plotting/plotting_utils.jl | 138 +++++++++++++++++++++++++++++++ test/data/PDE/PDE_refs_MB.jld2 | Bin 3990674 -> 3990674 bytes test/data/PDE/PDE_refs_noMB.jld2 | Bin 3990674 -> 3990674 bytes test/plotting.jl | 37 +++++++++ test/runtests.jl | 4 +- 7 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/plotting/plotting_utils.jl create mode 100644 test/plotting.jl diff --git a/Project.toml b/Project.toml index 5b0c90a..db64639 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/Huginn.jl b/src/Huginn.jl index cbf3c55..812d8f6 100644 --- a/src/Huginn.jl +++ b/src/Huginn.jl @@ -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 @@ -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 \ No newline at end of file diff --git a/src/plotting/plotting_utils.jl b/src/plotting/plotting_utils.jl new file mode 100644 index 0000000..5dd1408 --- /dev/null +++ b/src/plotting/plotting_utils.jl @@ -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 + + diff --git a/test/data/PDE/PDE_refs_MB.jld2 b/test/data/PDE/PDE_refs_MB.jld2 index 0addf377ba25b9552c5ae34046711879b3e5987e..03bcb484361cac3611d9670295090f178b5f71b4 100644 GIT binary patch delta 196 zcmWO0$#p>i06@|61ql+w7-CM$V~F9Q5#?CCy`vXJ=tBn@U~%{Ne7y(fr_&}_9Fagm z@*-i0NK{_sO=1$4guKg#Bqb$j`IL-gB`0|)NKw9|BxU)Qid3a0b!kXbTJj^m(w2^N kr6+wE$WTTymWfPdCVw)Qh5XA>Ri06@|61ql+wn20$sk1;EHE(=-8O4hQGXLT#P3;Vi06@|61ql+w7-CM$V~F9Q5#?CCy`vXJ=tBn@U~%{Ne7y(fr_&}_9Fagm z@*-i0NK{_sO=1$4guKg#Bqb$j`IL-gB`0|)NKw9|BxU)Qid3a0b!kXbTJj^m(w2^N kr6+wE$WTTymWfPdCVw)Qh5XA>Ri06@|61ql+wn20$sk1;EHE(=-8O4hQGXLT#P3;V