timholy / Grid.jl

Interpolation and related operations on grids
MIT License
47 stars 26 forks source link

Extrapolation not working for some BCs #31

Closed tomasaschan closed 7 years ago

tomasaschan commented 10 years ago

There are still some boundary conditions for which InterpCubic (from #19) isn't working correctly. See e.g. this comment on #29.

tomasaschan commented 10 years ago

Acutally, it appears that it's not only for cubic interpolation we have problems with some BCs:

using Grid

f(x) = sin(2pi * (x-1) / 10)
yirefl = InterpGrid(f(1:11),BCreflect,InterpLinear)
yiperi = InterpGrid(f(1:11),BCperiodic,InterpLinear)

df = vcat(
DataFrame(x=-9:.1:40,y=[yirefl[x]+2 for x in -9:.1:40],t="BCreflect"),
DataFrame(x=-9:.1:40,y=[yiperi[x]-1 for x in -9:.1:40],t="BCperiodic"),
)
plot(df,x=:x,y=:y,color=:t,Geom.path,Guide.colorkey("Boundary condition"))

grid

There's a weird shift in BCreflect, stemming from reflecting around the half-point outside the data set rather than around the edge, and similarly in BCperiodic. Also, neither seem to work at all for indices x<1, i.e. to the left of the given data set.

(If the final datapoint is left out from the BCperiodic case, i.e. f(1:10) instead of f(1:11), it looks good for large indices. Still bad for x<1 though.)

timholy commented 10 years ago

This definition of reflection is consistent with a reflection of a vector x begin defined as [x, x[end:-1:1]]. It's also the definition used by matlab in their image transformations.

The thing below 1 is just weird, gotta look into that.

tomasaschan commented 10 years ago

@timholy Yeah, I guess it makes a lot of sense for imaging applications, but it really doesn't for physics applications. For example, interpolating the electric field along a ray path, when the signal hits a reflecting surface could be done using BCreflect if the reflection point was the last node, rather than at the half-point outside. Maybe Interpolations.jl should have two reflecting boundary conditions, which do this differently (one nice for images and one nice for physics)?

timholy commented 10 years ago

I'm not sure it's physics vs images. Who says the reflecting surface is positioned exactly at a node? Perhaps I might have set my grid points to be a half-node away from the edge.

But flexibility would be good. I'm not sure we want to support any choice in the interval [0,1], but certainly on-node and half-node seem reasonable.

tomasaschan commented 10 years ago

Yeah, I didn't like the naming either, but I had to distinguish between them somehow :P Supporting both seems like the target then. Hopefully, on-node reflection will be good enough for cases when the current behavior between the two near-edge nodes is unsatisfactory for some reason.

timholy commented 10 years ago

Sounds good to me.

cmundi commented 9 years ago

Joining very late. In most applications of field theories (EM, QM, solid state, etc.) it is essential to be able to place the boundary at node or mid-node. There are even some edge (pun!) cases where quarter-intervals are numerically convenient. Having node and mid-node will cover most cases and help users write more transparently correct code.

tomasaschan commented 9 years ago

@cmundi: Both node and mid-node are supported in Interpolations.jl in terms of the GridBehavior type argument - if the boundaries should be on the outermost datapoint, use OnGrid, while if you want the boundary a half-interval outside the otuermost datapoint, use OnCell.

cmundi commented 9 years ago

Perfect! Thanks for your reply. On Mar 17, 2015 9:48 AM, "Tomas Lycken" notifications@github.com wrote:

@cmundi https://github.com/cmundi: Both node and mid-node are supported in Interpolations.jl in terms of the GridBehavior type argument - if the boundaries should be on the outermost datapoint, use OnGrid, while if you want the boundary a half-interval outside the otuermost datapoint, use OnCell.

— Reply to this email directly or view it on GitHub https://github.com/timholy/Grid.jl/issues/31#issuecomment-82471560.