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

Incorporating DimensionalData #73

Merged
merged 14 commits into from
Dec 16, 2024
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Hongyang Zhou <hyzhou@umich.edu>"]
version = "0.7.3"

[deps]
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
FortranFiles = "c58ffaec-ab22-586d-bfc5-781a99fd0b10"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Expand All @@ -28,6 +29,7 @@ Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
BatsrusMakieExt = "Makie"

[compat]
DimensionalData = "0.29"
FortranFiles = "0.6"
Glob = "1.3"
HDF5 = "0.17"
Expand Down
6 changes: 3 additions & 3 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ SUITE["read"]["ASCII"] = @benchmarkable load($file)

file = joinpath(directory, files[2])
bd = load(file)
SUITE["read"]["Binary structured"] = @benchmarkable load($file)
SUITE["read"]["Load binary structured"] = @benchmarkable load($file)
SUITE["read"]["Extract density"] = @benchmarkable Batsrus.getvar($bd, "Rho")
SUITE["read"]["Extract B"] = @benchmarkable Batsrus.getvar($bd, "B")
SUITE["read"]["Extract Bmag"] = @benchmarkable get_magnitude($bd, :B)
SUITE["read"]["Interp2d"] = @benchmarkable Batsrus.interp2d($bd, "rho")

file = joinpath(directory, files[3])
bd = load(file)
SUITE["read"]["Anisotropy"] = @benchmarkable bd["Anisotropy1"]
SUITE["read"]["Anisotropy"] = @benchmarkable get_anisotropy($bd, 1)
4 changes: 2 additions & 2 deletions docs/examples/visualization/demo_animate_2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ function get_vector(bd, var, plotrange, plotinterval)
X, Y = eachslice(x, dims=3)
X, Y = vec(X), vec(Y)
varstream = split(var, ";")
var1_ = findfirst(x->lowercase(x)==lowercase(varstream[1]), bd.head.wnames)
var2_ = findfirst(x->lowercase(x)==lowercase(varstream[2]), bd.head.wnames)
var1_ = findfirst(x->lowercase(x)==lowercase(varstream[1]), bd.head.wname)
var2_ = findfirst(x->lowercase(x)==lowercase(varstream[2]), bd.head.wname)
# 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
28 changes: 13 additions & 15 deletions docs/src/man/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,24 @@ w = interp1d(bd, "rho", point1, point2)

- Derived variables

We provide utility methods `get_magnitude`, `get_magnitude2`, and `fill_vector_from_scalars` for vector processing:

```julia
Bmag = bd["Bmag"]
Bmag = get_magnitude(bd, :B)
B2 = get_magnitude2(bd, :B)
Bvec = Batsrus.fill_vector_from_scalars(bd, :B)
paniso0 = get_anisotropy(bd, 0)
```

Here is a full list of predefined derived quantities:
These are built upon `get_vectors`. Here is a full list of predefined derived quantities in `get_vectors`:

| Derived variable name | Meaning | Required variable |
|-----------------------|----------------------------------|-------------------|
| B2 | Magnetic field magnitude squared | Bx, By, Bz |
| E2 | Electric field magnitude squared | Ex, Ey, Ez |
| U2 | Velocity magnitude squared | Ux, Uy, Uz |
| 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[0-1] | Pressure/Temperature anisotropy | B, P tensor |
| :B | Magnetic field vector | Bx, By, Bz |
| :E | Electric field vector | Ex, Ey, Ez |
| :U | Velocity vector | Ux, Uy, Uz |
| :U0 | Electron velocity vector | UxS0, UyS0, UzS0 |
| :U1 | Proton velocity vector | UxS1, UyS1, UzS1 |

## Output format conversion

Expand Down Expand Up @@ -280,7 +278,7 @@ For 3D outputs, we may use `cutplot` for visualizing on a sliced plane, or `stre
To get the index of a certain quantity, e.g. electron number density

```julia
ρe_= findfirst(x->x=="rhoS0", bd.head.wnames)
ρe_= findfirst(x->x=="rhoS0", bd.head.wname)
```

#### Get variable range
Expand Down
2 changes: 1 addition & 1 deletion examples/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PBcut = hypot.(Bx, By, Bz)
c = ax.contourf(cut1, cut2, Pcut./PBcut)
fig.colorbar(c; ax)
ax.axis("scaled")
title(data.head[:wnames][p_])
title(data.head.wname[p_])

xlabel("x"); ylabel("z")

Expand Down
36 changes: 18 additions & 18 deletions examples/vdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ function dist_select(fnameParticle, xC=-1.90, yC=0.0, zC=-0.1, xL=0.005, yL=0.2,

x, y, z = eachslice(data.x, dims=4)

ux_ = findfirst(x->x=="ux", data.head.wnames)
uy_ = findfirst(x->x=="uy", data.head.wnames)
uz_ = findfirst(x->x=="uz", data.head.wnames)
ux_ = findfirst(x->x=="ux", data.head.wname)
uy_ = findfirst(x->x=="uy", data.head.wname)
uz_ = findfirst(x->x=="uz", data.head.wname)

ux = @view data.w[:,:,:,ux_]
uy = @view data.w[:,:,:,uy_]
uz = @view data.w[:,:,:,uz_]

for ip in eachindex(x)
for iR = 1:nBox
for iR in 1:nBox
if region[1,iR] < x[ip] < region[2,iR] &&
region[3,iR] < y[ip] < region[4,iR] &&
region[5,iR] < z[ip] < region[6,iR]
Expand Down Expand Up @@ -182,9 +182,9 @@ function GetMeanField(filefield, limits; dir=".")
y = data.x[:,:,:,2]
z = data.x[:,:,:,3]

bx_ = findfirst(x->x=="Bx", data.head.wnames)
by_ = findfirst(x->x=="By", data.head.wnames)
bz_ = findfirst(x->x=="Bz", data.head.wnames)
bx_ = findfirst(x->x=="Bx", data.head.wname)
by_ = findfirst(x->x=="By", data.head.wname)
bz_ = findfirst(x->x=="Bz", data.head.wname)

Bx = @view data.w[:,:,:,bx_]
By = @view data.w[:,:,:,by_]
Expand All @@ -211,8 +211,8 @@ function plotExCut(filefield::String, region, xC, yC, zC, xL, yL, zL;
# Sample region plot over contour
data = load(joinpath(dir, filefield))

bx_ = findfirst(x->x=="Bx", data.head.wnames)
bz_ = findfirst(x->x=="Bz", data.head.wnames)
bx_ = findfirst(x->x=="Bx", data.head.wname)
bz_ = findfirst(x->x=="Bz", data.head.wname)

Bx = @view data.w[:,:,:,bx_]
Bz = @view data.w[:,:,:,bz_]
Expand Down Expand Up @@ -280,9 +280,9 @@ function dist_plot(pType='e')
y = @view data.x[:,:,:,2]
z = @view data.x[:,:,:,3]

ux_ = findfirst(x->x=="ux", data.head.wnames)
uy_ = findfirst(x->x=="uy", data.head.wnames)
uz_ = findfirst(x->x=="uz", data.head.wnames)
ux_ = findfirst(x->x=="ux", data.head.wname)
uy_ = findfirst(x->x=="uy", data.head.wname)
uz_ = findfirst(x->x=="uz", data.head.wname)

ux = @view data.w[:,:,:,ux_]
uy = @view data.w[:,:,:,uy_]
Expand Down Expand Up @@ -319,9 +319,9 @@ function dist_plot(pType='e')
y = @view data.x[:,:,:,2]
z = @view data.x[:,:,:,3]

ux_ = findfirst(x->x=="ux", data.head.wnames)
uy_ = findfirst(x->x=="uy", data.head.wnames)
uz_ = findfirst(x->x=="uz", data.head.wnames)
ux_ = findfirst(x->x=="ux", data.head.wname)
uy_ = findfirst(x->x=="uy", data.head.wname)
uz_ = findfirst(x->x=="uz", data.head.wname)

ux = @view data.w[:,:,:,ux_]
uy = @view data.w[:,:,:,uy_]
Expand Down Expand Up @@ -587,9 +587,9 @@ function HF_velocity()
y = data.x[:,:,:,2]
z = data.x[:,:,:,3]

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_ = findfirst(x->lowercase(x)=="uxs1", data.head.wname)
uy_ = findfirst(x->lowercase(x)=="uys1", data.head.wname)
uz_ = findfirst(x->lowercase(x)=="uzs1", data.head.wname)

Ux = @view data.w[:,:,:,ux_]
Uy = @view data.w[:,:,:,uy_]
Expand Down
6 changes: 3 additions & 3 deletions ext/BatsrusMakieExt/typerecipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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])
unitx = getunit(bd, bd.head.wname[1])

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

View check run for this annotation

Codecov / codecov/patch

ext/BatsrusMakieExt/typerecipe.jl#L7

Added line #L7 was not covered by tests
unitw = getunit(bd, var)
x = bd.x .* unitx
y = bd.w[:,var_] .* unitw
Expand All @@ -21,8 +21,8 @@
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1)
x, y, w = interp2d(bd, var, plotrange, plotinterval)

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

if unitx isa UnitfulBatsrus.Unitlike
Expand Down
3 changes: 2 additions & 1 deletion src/Batsrus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ using Parsers
using Interpolations: cubic_spline_interpolation, BSpline, Linear, scale, interpolate
import NaturalNeighbours as NN
using StaticArrays: SVector, @SMatrix, SA
using DimensionalData

export BATS,
load, readlogdata, readtecdata, showhead, # io
getvar, cutdata, subvolume, subsurface, get_convection_E, get_hall_E,
getvar, getderivedvar, cutdata, subvolume, subsurface, get_convection_E, get_hall_E,
get_anisotropy, get_vectors, get_magnitude, get_magnitude2,
fill_vector_from_scalars, # select
Batl, convertTECtoVTU, convertIDLtoVTK, readhead, readtree, getConnectivity, # vtk
Expand Down
53 changes: 27 additions & 26 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function readlogdata(file::AbstractString)
nLine = countlines(f) - 2
seekstart(f)
headline = readline(f)
variables = split(readline(f))
variable = split(readline(f))
ndim = 1
it = 0
t = 0.0
gencoord = false
nx = 1
nw = length(variables)
nw = length(variable)

data = zeros(nw, nLine)
@inbounds for i in 1:nLine
Expand All @@ -64,7 +64,7 @@ function readlogdata(file::AbstractString)
close(f)

head = (ndim=ndim, headline=headline, it=it, time=t, gencoord=gencoord,
nw=nw, nx=nx, variables=variables)
nw=nw, nx=nx, variable=variable)

head, data
end
Expand Down Expand Up @@ -97,7 +97,7 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
ln = readline(f) |> strip
if startswith(ln, "VARIABLES")
# Read until another keyword appears
varline = split(ln,'=')[2]
varline = split(ln, '=')[2]

ln = readline(f) |> strip
while !startswith(ln, "ZONE")
Expand All @@ -122,12 +122,12 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
name = uppercase(name)
if name == "T" # ZONE title
T = value
elseif name in ("NODES","N")
elseif name in ("NODES", "N")
nNode = Parsers.parse(Int32, value)
elseif name in ("ELEMENTS","E")
elseif name in ("ELEMENTS", "E")
nCell = Parsers.parse(Int32, value)
elseif name in ("ET","ZONETYPE")
if uppercase(value) in ("BRICK","FEBRICK")
elseif name in ("ET", "ZONETYPE")
if uppercase(value) in ("BRICK", "FEBRICK")
nDim = 3
elseif uppercase(value) in ("QUADRILATERAL", "FEQUADRILATERAL")
nDim = 2
Expand All @@ -146,7 +146,7 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
name, value = split(ln,'"', keepempty=false)
name = string(name[9:end-1])
str = string(strip(value))
if name in ("ITER","NPROC")
if name in ("ITER", "NPROC")
str = Parsers.parse(Int32, value)
elseif name == "TIMESIM"
sec = split(str, "=")
Expand Down Expand Up @@ -199,7 +199,7 @@ function readtecdata(file::AbstractString; verbose::Bool=false)

close(f)

head = (variables=VARS, nNode=nNode, nCell=nCell, nDim=nDim, ET=ET,
head = (variable=VARS, nNode=nNode, nCell=nCell, nDim=nDim, ET=ET,
title=title, auxdataname=auxdataname, auxdata=auxdata)

head, data, connectivity
Expand Down Expand Up @@ -297,13 +297,14 @@ function getfilehead(fileID::IOStream, filelist::FileList)
skip(fileID, TAG)
end

# Obtain output array
variables = split(varname)
# Obtain variable names
wnames = @view variables[ndim+1:ndim+nw]
# Obtain output variable names
variable = lowercase.(split(varname))
coord = @view variable[1:ndim]
wname = @view variable[ndim+1:ndim+nw]
param = @view variable[ndim+nw+1:end]

head = BatsHead(ndim, headline, it, t, gencoord,
neqpar, nw, nx, eqpar, variables, wnames)
head = BatsHead(ndim, headline, it, t, gencoord, neqpar, nw, nx, eqpar,
coord, wname, param)
end

function skipline(s::IO)
Expand Down Expand Up @@ -481,12 +482,12 @@ Also calculate convenient constants ti0, cs0 ... for typical formulas.
This function is currently not used anywhere!
"""
function setunits(head, type; distance=1.0, mp=1.0, me=1.0)
ndim = head.ndim
headline = head.headline
neqpar = head.neqpar
nw = head.nw
eqpar = head.eqpar
variables = head.variables
ndim = head.ndim
headline = head.headline
neqpar = head.neqpar
nw = head.nw
eqpar = head.eqpar
param = head.param

mu0SI = 4π*1e-7 # H/m
cSI = 2.9978e8 # speed of light, [m/s]
Expand Down Expand Up @@ -588,7 +589,7 @@ function setunits(head, type; distance=1.0, mp=1.0, me=1.0)

# Overwrite values if given by eqpar
for ieqpar in 1:neqpar
var = variables[ndim+nw+ieqpar]
var = param[ieqpar]
if var == "xSI"
xSI = eqpar[ieqpar]
elseif var == "tSI"
Expand Down Expand Up @@ -707,9 +708,9 @@ function showhead(file::FileList, head, io=stdout)

if head.neqpar > 0
println(io, "parameters : $(head.eqpar)")
println(io, "coord names: $(head.variables[1:head.ndim])")
println(io, "var names: $(head.variables[head.ndim+1:head.ndim+head.nw])")
println(io, "param names: $(head.variables[head.ndim+head.nw+1:end])")
println(io, "coordinate names: $(head.coord)")
println(io, "variable names: $(head.wname)")
println(io, "parameter names: $(head.param)")
end

return
Expand Down
12 changes: 6 additions & 6 deletions src/plot/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using RecipesBase

# Build a recipe which acts on a custom type.
@recipe function f(bd::BATS{1, 2, T}, var::AbstractString) where {T}
@recipe function f(bd::BATS{1, TV, TX, TW}, var::AbstractString) where {TV, TX, TW}

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
hasunits = hasunit(bd)

if hasunits
unitx = getunit(bd, bd.head.variables[1])
unitx = getunit(bd, bd.head.wname[1])

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

View check run for this annotation

Codecov / codecov/patch

src/plot/plots.jl#L10

Added line #L10 was not covered by tests
unitw = getunit(bd, var)
x = bd.x .* unitx
y = getview(bd, var) .* unitw
Expand All @@ -22,14 +22,14 @@
end
end

@recipe function f(bd::BATS{2, 3, T}, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1) where {T}
@recipe function f(bd::BATS{2, TV, TX, TW}, var::AbstractString;

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

View check run for this annotation

Codecov / codecov/patch

src/plot/plots.jl#L25

Added line #L25 was not covered by tests
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1) where {TV, TX, TW}
hasunits = hasunit(bd)

x, y, w = interp2d(bd, var, plotrange, plotinterval)
if hasunits
unitx = getunit(bd, bd.head.variables[1])
unity = getunit(bd, bd.head.variables[2])
unitx = getunit(bd, bd.head.wname[1])
unity = getunit(bd, bd.head.wname[2])

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

View check run for this annotation

Codecov / codecov/patch

src/plot/plots.jl#L31-L32

Added lines #L31 - L32 were not covered by tests
unitw = getunit(bd, var)

if unitx isa UnitfulBatsrus.Unitlike
Expand Down
Loading
Loading