Refinement meshgrids

Hi, all. I am trying to plot the curves of potential 1/r on mesh grid xg and yg (xg and yg are 2d mesh grids, r = @. sqrt(xg^2 + yg^2)). I want to solve the curves where the potential equals 1. I want to refine the grids near the curve. Is there any simple way to construct refined meshes?

1 Like

Hi,
I’m not quite sure what you mean by solve for curves, but if you want to construct the level set for value 1, you could just use something like

using GLMakie

xs = LinRange(-1.5, 1.5, 100)  # 100 equidistant points between -1.5 and 1.5 (inclusive)
ys = LinRange(-1.5, 1.5, 100)
f(x, y) = sqrt(x^2 + y^2)

contour(xs, ys, [f(x, y) for x in xs, y in ys], levels=[1])

Now if you would want to refine this to get a higher resolution, what does that mean for xs and ys (or your xg and yg)? Would you want to obtain something like xs = ys = LinRange(-1.5, 1.5, 200)? Do you want to abandon the grid structure in favour of something like a quadtree?

1 Like

it closes, I want to obtain the grid structure like xsub = ysus = LinRange(-.5, .5, 100)

Assuming you’re referring to the Makie window, and you’re running the code as a script, you could use wait(display(contour(...))).

I’m not sure how exactly you get to -.5, but you could use e.g. new_xs = LinRange(xs[cld(length(xs), 3)], xs[cld(2*length(xs), 3)], length(xs)).


In general, if your xs is not a LinRange you could refine it by interpolating:

using Interpolations

itp = linear_interpolation(eachindex(xs), xs)

new_xs = itp[begin:0.5:end]  
# 199-element Vector{Float64}
# xs[1]   (xs[1] + xs[2])/2   xs[2]  ... xs[end]

new_xs2 = itp[LinRange(searchsortedfirst(xs, -0.5), searchsortedlast(xs, 0.5), 100)]
# 100-element Vector{Float64}:
# -0.5  -0.48989898989898983 ...  0.48989898989899006  0.5