Skip to content

Commit

Permalink
Add Makie and Plots support (#31)
Browse files Browse the repository at this point in the history
* Add some @view

* Add Makie support as an extension

* Add tests for PyPlot, Plots, and Makie
  • Loading branch information
henry2004y authored Nov 20, 2023
1 parent 4efa0d5 commit cfcec4b
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 217 deletions.
18 changes: 8 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Batsrus"
uuid = "e74ebddf-6ac1-4047-a0e5-c32c99e57753"
authors = ["Hongyang Zhou <[email protected]>"]
version = "0.3.11"
version = "0.4.0"

[deps]
Dierckx = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
Expand All @@ -15,6 +15,12 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
BatsrusMakieExt = "Makie"

[compat]
Dierckx = "0.5"
FortranFiles = "0.6"
Expand All @@ -25,12 +31,4 @@ RecipesBase = "1.1"
Requires = "1.1"
Unitful = "1.7"
WriteVTK = "1.9"
julia = "1.6"

[extras]
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "LazyArtifacts", "SHA"]
julia = "1.6"
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Fast [BATSRUS](https://github.com/MSTEM-QUDA/BATSRUS)/[SWMF](https://github.com/

This package provides the following functionalities:

* simulation data reader
* Simulation data reader
* 2D/3D region cut from the whole domain
* interpolation from unstructured to structured data
* data format conversion to VTK
* simulation data visualization through multiple plotting libraries
* Interpolation from unstructured to structured data
* Data format conversion to VTK
* Data visualization through multiple plotting libraries

For more details, please check the [document][Batsrus-doc].

Expand Down
10 changes: 10 additions & 0 deletions ext/BatsrusMakieExt/BatsrusMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module BatsrusMakieExt

using Batsrus, Printf
import Batsrus: findindex, hasunit, getunit, getdata
import Batsrus.UnitfulBatsrus
import Makie

include("typerecipe.jl")

end
35 changes: 35 additions & 0 deletions ext/BatsrusMakieExt/typerecipe.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Type conversion from Batsrus to Makie

"Conversion for 1D plots"
function Makie.convert_arguments(P::Makie.PointBased, bd::BATLData, var::String)
var_ = findindex(bd, var)
if hasunit(bd)
unitx = getunit(bd, bd.head.variables[1])
unitw = getunit(bd, var)
x = bd.x .* unitx
y = bd.w[:,var_] .* unitw
else
x = bd.x
y = bd.w[:,var_]
end

([Makie.Point2f(i, j) for (i, j) in zip(x, y)],)
end

"Conversion for 2D plots."
function Makie.convert_arguments(P::Makie.SurfaceLike, bd::BATLData, var::String;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1)
x, y, w = getdata(bd, var, plotrange, plotinterval)

unitx = getunit(bd, bd.head.variables[1])
unity = getunit(bd, bd.head.variables[2])
unitw = getunit(bd, var)

if unitx isa UnitfulBatsrus.Unitlike
x *= unitx
y *= unity
w *= unitw
end

(x, y, w)
end
2 changes: 1 addition & 1 deletion src/Batsrus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct FileList
lenhead::Int
end

"Primary BATLData storage type"
"Primary Batsrus data storage type."
struct BATLData{T<:AbstractFloat}
"header information"
head::NamedTuple
Expand Down
10 changes: 5 additions & 5 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,15 @@ end
function Base.show(io::IO, data::BATLData)
showhead(io, data)
if data.list.bytes 1e9
println(io, "filesize = $(data.list.bytes/1e9) GB")
println(io, "filesize: $(data.list.bytes/1e9) GB")
elseif data.list.bytes 1e6
println(io, "filesize = $(data.list.bytes/1e6) MB")
println(io, "filesize: $(data.list.bytes/1e6) MB")
elseif data.list.bytes 1e3
println(io, "filesize = $(data.list.bytes/1e3) KB")
println(io, "filesize: $(data.list.bytes/1e3) KB")
else
println(io, "filesize = $(data.list.bytes) bytes")
println(io, "filesize: $(data.list.bytes) bytes")
end
println(io, "snapshots = $(data.list.npictinfiles)")
println(io, "snapshots: $(data.list.npictinfiles)")
end

"""
Expand Down
40 changes: 0 additions & 40 deletions src/plot/makie.jl

This file was deleted.

34 changes: 15 additions & 19 deletions src/plot/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@
using RecipesBase

# Build a recipe which acts on a custom type.
@recipe function f(data::BATLData, var::AbstractString;
@recipe function f(bd::BATLData, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1)

ndim = data.head.ndim
ndim = bd.head.ndim

if startswith(data.head.headline, "normalized")
hasunits = false
else
hasunits = true
unitw = getunit(data, var)
end
hasunits = hasunit(bd)

if ndim == 1
VarIndex_ = findindex(data, var)
VarIndex_ = findindex(bd, var)
if hasunits
unitx = getunit(data, data.head.variables[1])
x = data.x .* unitx
w = data.w
y = w[:,VarIndex_] .* unitw
unitx = getunit(bd, bd.head.variables[1])
unitw = getunit(bd, var)
x = bd.x .* unitx
y = bd.w[:,VarIndex_] .* unitw
else
x, w = data.x, data.w
y = w[:,VarIndex_]
x = bd.x
y = @view bd.w[:,VarIndex_]
end

@series begin
seriestype --> :path
x, y
end
elseif ndim == 2
x, y, w = getdata(data, var, plotrange, plotinterval)
x, y, w = getdata(bd, var, plotrange, plotinterval)

unitx = getunit(data, data.head.variables[1])
unity = getunit(data, data.head.variables[2])
unitx = getunit(bd, bd.head.variables[1])
unity = getunit(bd, bd.head.variables[2])
unitw = getunit(bd, var)

if unitx isa UnitfulBatsrus.Unitlike
x *= unitx
Expand All @@ -45,7 +41,7 @@ using RecipesBase

@series begin
seriestype --> :contourf # use := if you want to force it
x, y, w
x, y, w'
end
end
end
Loading

0 comments on commit cfcec4b

Please sign in to comment.