Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Makie and Plots support #31

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 10 in ext/BatsrusMakieExt/typerecipe.jl

View check run for this annotation

Codecov / codecov/patch

ext/BatsrusMakieExt/typerecipe.jl#L7-L10

Added lines #L7 - L10 were not covered by tests
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

Check warning on line 31 in ext/BatsrusMakieExt/typerecipe.jl

View check run for this annotation

Codecov / codecov/patch

ext/BatsrusMakieExt/typerecipe.jl#L29-L31

Added lines #L29 - L31 were not covered by tests
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 @@
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")

Check warning on line 698 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L698

Added line #L698 was not covered by tests
elseif data.list.bytes ≥ 1e6
println(io, "filesize = $(data.list.bytes/1e6) MB")
println(io, "filesize: $(data.list.bytes/1e6) MB")

Check warning on line 700 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L700

Added line #L700 was not covered by tests
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")

Check warning on line 704 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L704

Added line #L704 was not covered by tests
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;

Check warning on line 6 in src/plot/plots.jl

View check run for this annotation

Codecov / codecov/patch

src/plot/plots.jl#L6

Added line #L6 was not covered by tests
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

Check warning on line 19 in src/plot/plots.jl

View check run for this annotation

Codecov / codecov/patch

src/plot/plots.jl#L16-L19

Added lines #L16 - L19 were not covered by tests
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 @@

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