henry2004y / Batsrus.jl

BATSRUS/SWMF Data Processor
https://henry2004y.github.io/Batsrus.jl/dev/
MIT License
5 stars 2 forks source link

meshgrid support #18

Closed henry2004y closed 3 years ago

henry2004y commented 3 years ago

I have made several rounds of mistakes related to array storage ordering, specifically meshgrid vs ndgrid. Even though some Matplotlib functions like contourf support 1D grid arrays, it is very easy to mess it up when other conditions are handling 2D ndarrays from the interpolation package Dierckx.

Therefore I decided to eliminate passing 1D arrays and always use 2D grid arrays. The list comprehension version of meshgrid is

X = [x for _ in y, x in x]
Y = [y for y in y, _ in x]

while the matrix multiplication version is

X = x' .* ones(length(y))
Y = ones(length(x))' .* y

Based on my test results, the list comprehension version is consistently faster and consumes less memory than the matrix multiplication version. I have now replaced all the occurrences with a function called meshgrid.

henry2004y commented 3 years ago

See de24e8173047c

henry2004y commented 3 years ago

There are some side effects. The contour function in gr() backend from Plots.jl only accepts 1D arrays, so most likely https://github.com/henry2004y/Batsrus.jl/blob/a5da0a5b211de4f33b18299790695239d673c9a3/src/plot/plots.jl#L37 won't work. I need to fix this!

henry2004y commented 3 years ago

I realized that the initial issue was not about 1D/2D grid arrays, but the consistency of ndgrid/meshgrid format for generating the variable w. Now I switched back to 1D grid arrays in getdata by default, and it only returns 2D grid array when griddim=2.

Tested for both PyPlot and Plots.