From 0182e0ad8924ea3e3ace630877dba74afb938137 Mon Sep 17 00:00:00 2001 From: Joshua Lampert <51029046+JoshuaLampert@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:42:33 +0100 Subject: [PATCH] Interpolate x value for plotting (#64) * interpolate x value for plotting * format * add compat for Interpolations --- Project.toml | 2 ++ src/DispersiveShallowWater.jl | 1 + src/visualization.jl | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 2ca57501..060118ea 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.1.2-pre" [deps] DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" +Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" PolynomialBases = "c74db56a-226d-5e98-8bb0-a6049094aeea" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -19,6 +20,7 @@ SummationByPartsOperators = "9f78cca6-572e-554e-b819-917d2f1cf240" [compat] DiffEqBase = "6.121" +Interpolations = "0.14" LinearAlgebra = "1" PolynomialBases = "0.4.15" Printf = "1" diff --git a/src/DispersiveShallowWater.jl b/src/DispersiveShallowWater.jl index d866f52f..77b98726 100644 --- a/src/DispersiveShallowWater.jl +++ b/src/DispersiveShallowWater.jl @@ -1,6 +1,7 @@ module DispersiveShallowWater using DiffEqBase +using Interpolations using LinearAlgebra: mul!, ldiv!, factorize, I, Diagonal using PolynomialBases using Printf: @printf, @sprintf diff --git a/src/visualization.jl b/src/visualization.jl index f80ade23..4d72c9a8 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -105,12 +105,14 @@ end nsubplots -= 1 end - index = argmin(abs.(grid(semi) .- x)) solution = zeros(nvariables(semi), length(sol.t)) data = zeros(nvars, length(sol.t)) for i in 1:nvariables(semi) for k in 1:length(sol.t) - solution[i, k] = wrap_array(sol.u[k], semi)[i, index] + # Allow that the spatial value `x` is not on the grid. Thus, interpolate the given values to the provided `x` + # with a linear spline. + solution[i, k] = linear_interpolation(grid(semi), + view(wrap_array(sol.u[k], semi), i, :))(x) end end @@ -120,7 +122,7 @@ end names = varnames(conversion, equations) plot_title --> - "$(get_name(semi.equations)) at x = $(round(grid(semi)[index], digits=5))" + "$(get_name(semi.equations)) at x = $(round(x, digits=5))" layout --> nsubplots for i in 1:nsubplots