Skip to content

Commit

Permalink
Primitive struct optimization (#66)
Browse files Browse the repository at this point in the history
* Use Enum for FileTypes; dispatch on FileType values

* Work around derive type construction for BATS, reordering

* Reduce allocations for lowercase check

* Add type annotations

* Int32 -> Int64 parameters

* Add anisotropy benchmark

* Bump minor version
  • Loading branch information
henry2004y authored Nov 11, 2024
1 parent b9a56ad commit 80e96e1
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 247 deletions.
2 changes: 1 addition & 1 deletion 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.6.9"
version = "0.7.0"

[deps]
FortranFiles = "c58ffaec-ab22-586d-bfc5-781a99fd0b10"
Expand Down
5 changes: 2 additions & 3 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ println()

directory = artifact"testdata"
files = ("1d__raw_2_t25.60000_n00000258.out", "z=0_raw_1_t25.60000_n00000258.out",
"bx0_mhd_6_t00000100_n00000352.out")

"z=0_fluid_region0_0_t00001640_n00010142.out")

const SUITE = BenchmarkGroup()

Expand All @@ -25,4 +24,4 @@ SUITE["read"]["Extract B"] = @benchmarkable Batsrus.getvar($bd, "B")
SUITE["read"]["Binary structured"] = @benchmarkable load($file)

file = joinpath(directory, files[3])
SUITE["read"]["Binary unstructured"] = @benchmarkable load($file)
SUITE["read"]["Anisotropy"] = @benchmarkable bd["Anisotropy1"]
5 changes: 2 additions & 3 deletions docs/examples/visualization/demo_animate_2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,8 @@ function get_vector(bd, var, plotrange, plotinterval)
X, Y = eachslice(x, dims=3)
X, Y = vec(X), vec(Y)
varstream = split(var, ";")
wnames = lowercase.(bd.head.wnames)
var1_ = findfirst(x->x==lowercase(varstream[1]), wnames)
var2_ = findfirst(x->x==lowercase(varstream[2]), wnames)
var1_ = findfirst(x->lowercase(x)==lowercase(varstream[1]), bd.head.wnames)
var2_ = findfirst(x->lowercase(x)==lowercase(varstream[2]), bd.head.wnames)
# Create grid values first.
xi = range(Float64(plotrange[1]), stop=Float64(plotrange[2]), step=plotinterval)
yi = range(Float64(plotrange[3]), stop=Float64(plotrange[4]), step=plotinterval)
Expand Down
4 changes: 3 additions & 1 deletion docs/src/man/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ Here is a full list of predefined derived quantities:
| Bmag | Magnetic field magnitude | Bx, By, Bz |
| Emag | Electric field magnitude | Ex, Ey, Ez |
| Umag | Velocity magnitude | Ux, Uy, Uz |
| Uemag | Electron Velocity magnitude | UxS0, UyS0, UzS0 |
| Uimag | Ion Velocity magnitude | UxS1, UyS1, UzS1 |
| B | Magnetic field vector | Bx, By, Bz |
| E | Electric field vector | Ex, Ey, Ez |
| U | Velocity vector | Ux, Uy, Uz |
| Anisotropy | Pressure/Temperature anisotropy | B, P tensor |
| Anisotropy[0-1] | Pressure/Temperature anisotropy | B, P tensor |

## Output format conversion

Expand Down
6 changes: 3 additions & 3 deletions examples/vdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ function HF_velocity()
y = data.x[:,:,:,2]
z = data.x[:,:,:,3]

ux_ = findfirst(x->x=="uxs1", lowercase.(data.head.wnames))
uy_ = findfirst(x->x=="uys1", lowercase.(data.head.wnames))
uz_ = findfirst(x->x=="uzs1", lowercase.(data.head.wnames))
ux_ = findfirst(x->lowercase(x)=="uxs1", data.head.wnames)
uy_ = findfirst(x->lowercase(x)=="uys1", data.head.wnames)
uz_ = findfirst(x->lowercase(x)=="uzs1", data.head.wnames)

Ux = @view data.w[:,:,:,ux_]
Uy = @view data.w[:,:,:,uy_]
Expand Down
4 changes: 2 additions & 2 deletions ext/BatsrusMakieExt/typerecipe.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Type conversion from Batsrus to Makie

"Conversion for 1D plots"
function Makie.convert_arguments(P::Makie.PointBased, bd::BATLData, var::String)
function Makie.convert_arguments(P::Makie.PointBased, bd::BATS, var::String)
var_ = findindex(bd, var)
if hasunit(bd)
unitx = getunit(bd, bd.head.variables[1])
Expand All @@ -17,7 +17,7 @@ function Makie.convert_arguments(P::Makie.PointBased, bd::BATLData, var::String)
end

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

Expand Down
45 changes: 2 additions & 43 deletions src/Batsrus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,13 @@ using Interpolations: cubic_spline_interpolation, BSpline, Linear, scale, interp
import NaturalNeighbours as NN
using StaticArrays: SVector, @SMatrix, SA

export BATLData,
export BATS,
load, readlogdata, readtecdata, showhead, # io
getvar, cutdata, subvolume, subsurface, # select
Batl, convertTECtoVTU, convertIDLtoVTK, readhead, readtree, getConnectivity, # vtk
interp1d, interp2d, slice1d, get_var_range, squeeze, get_range # plot/utility

"Type for the file information."
struct FileList
"filename"
name::String
"file type"
type::Symbol
"directory"
dir::String
"file size"
bytes::Int
"number of snapshots"
npictinfiles::Int
"length of meta data"
lenhead::Int
end

struct BATLHead
ndim::Int32
headline::SubString{String}
it::Int32
time::Float32
gencoord::Bool
neqpar::Int32
nw::Int32
nx::Vector{Int32}
eqpar::Vector{Float32}
variables::Vector{SubString{String}}
wnames::Vector{SubString{String}}
end

"Primary Batsrus data storage type."
struct BATLData{dim, T<:AbstractFloat, U}
"header information"
head::BATLHead
"grid"
x::U
"variables"
w::U
"file information"
list::FileList
end

include("type.jl")
include("unit/UnitfulBatsrus.jl")
using .UnitfulBatsrus

Expand Down
Loading

2 comments on commit 80e96e1

@henry2004y
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Refactor and optimize the primitive struct

Add anisotropy calculation

Various bug fixes and doc improvements

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119131

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.0 -m "<description of version>" 80e96e14c63a52253d70d8c749dec4aceb4d3036
git push origin v0.7.0

Please sign in to comment.