Skip to content

Commit

Permalink
Refactoring (#46)
Browse files Browse the repository at this point in the history
* Minor refactoring

* Bump patch
  • Loading branch information
henry2004y authored Apr 26, 2024
1 parent 9629d87 commit 6e42bb4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 63 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.5.6"
version = "0.5.7"

[deps]
FortranFiles = "c58ffaec-ab22-586d-bfc5-781a99fd0b10"
Expand Down
4 changes: 1 addition & 3 deletions examples/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ data = load(file)
sequence = 65 # cut plane index from -
p_ = 18 # thermal pressure index

X = @view data.x[:,:,:,1]
Y = @view data.x[:,:,:,2]
Z = @view data.x[:,:,:,3]
X, Y, Z = eachslice(data.x, dims=4)

fig, ax = plt.subplots(1, 1)
fig.set_size_inches(3.3, 6)
Expand Down
4 changes: 1 addition & 3 deletions examples/vdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ function dist_select(fnameParticle, xC=-1.90, yC=0.0, zC=-0.1, xL=0.005, yL=0.2,

data = load(joinpath(dir, fnameParticle))

x = @view data.x[:,:,:,1]
y = @view data.x[:,:,:,2]
z = @view data.x[:,:,:,3]
x, y, z = eachslice(data.x, dims=4)

ux_ = findfirst(x->x=="ux", data.head.wnames)
uy_ = findfirst(x->x=="uy", data.head.wnames)
Expand Down
2 changes: 0 additions & 2 deletions src/plot/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ using RecipesBase
# Build a recipe which acts on a custom type.
@recipe function f(bd::BATLData, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], plotinterval=0.1)

ndim = bd.head.ndim

hasunits = hasunit(bd)

if ndim == 1
Expand Down
9 changes: 0 additions & 9 deletions src/plot/pyplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,4 @@ function set_colorbar(colorscale, vmin, vmax, data=[1.0])
end

cnorm, ticks
end

function adjust_plotrange!(plotrange, xlimit, ylimit)
plotrange[1] = ifelse(isinf(plotrange[1]), xlimit[1], plotrange[1])
plotrange[2] = ifelse(isinf(plotrange[2]), xlimit[2], plotrange[2])
plotrange[3] = ifelse(isinf(plotrange[3]), ylimit[1], plotrange[3])
plotrange[4] = ifelse(isinf(plotrange[4]), ylimit[2], plotrange[4])

return
end
30 changes: 15 additions & 15 deletions src/plot/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ function getdata2d(bd::BATLData, var::AbstractString,
varIndex_ = findindex(bd, var)

if bd.head.gencoord # Generalized coordinates
X = @view x[:,:,1]
Y = @view x[:,:,2]
X, Y = eachslice(x, dims=3)
W = @view w[:,:,varIndex_]

if any(abs.(plotrange) .== Inf)
if plotrange[1] == -Inf plotrange[1] = minimum(X) end
if plotrange[2] == Inf plotrange[2] = maximum(X) end
if plotrange[3] == -Inf plotrange[3] = minimum(Y) end
if plotrange[4] == Inf plotrange[4] = maximum(Y) end
end

adjust_plotrange!(plotrange, extrema(X), extrema(Y))
# Set a heuristic value if not set
if isinf(plotinterval)
# set a heuristic value
plotinterval = (plotrange[2] - plotrange[1]) / size(X,1)
plotinterval = (plotrange[2] - plotrange[1]) / size(X, 1)
end
xi = range(plotrange[1], stop=plotrange[2], step=plotinterval)
yi = range(plotrange[3], stop=plotrange[4], step=plotinterval)
Expand All @@ -45,10 +38,7 @@ function getdata2d(bd::BATLData, var::AbstractString,
xi, yi = xrange, yrange
Wi = w[:,:,varIndex_]' # Matplotlib does not accept view!
else
if plotrange[1] == -Inf plotrange[1] = xrange[1] end
if plotrange[2] == Inf plotrange[2] = xrange[end] end
if plotrange[3] == -Inf plotrange[3] = yrange[1] end
if plotrange[4] == Inf plotrange[4] = yrange[end] end
adjust_plotrange!(plotrange, (xrange[1], xrange[end]), (yrange[1], yrange[end]))

if isinf(plotinterval)
xi = range(plotrange[1], stop=plotrange[2], step=xrange[2] - xrange[1])
Expand Down Expand Up @@ -100,4 +90,14 @@ end
else
return true
end
end

"Adjust 2D plot ranges."
function adjust_plotrange!(plotrange, xlimit, ylimit)
plotrange[1] = ifelse(isinf(plotrange[1]), xlimit[1], plotrange[1])
plotrange[2] = ifelse(isinf(plotrange[2]), xlimit[2], plotrange[2])
plotrange[3] = ifelse(isinf(plotrange[3]), ylimit[1], plotrange[3])
plotrange[4] = ifelse(isinf(plotrange[4]), ylimit[2], plotrange[4])

return
end
21 changes: 10 additions & 11 deletions src/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ The returned 2D data lies in the `sequence` plane from - to + in `dir`.
"""
function cutdata(bd::BATLData, var::AbstractString;
plotrange=[-Inf,Inf,-Inf,Inf], dir::String="x", sequence::Int=1)

x, w = bd.x, bd.w
var_ = findfirst(x->x==lowercase(var), lowercase.(bd.head.wnames))
isempty(var_) && error("$(var) not found in header variables!")
Expand Down Expand Up @@ -57,10 +56,10 @@ end
hx = @view x[:,1]
hy = @view y[1,:]

if isinf(limits[1]) limits[1] = hx[1] end
if isinf(limits[3]) limits[3] = hy[1] end
if isinf(limits[2]) limits[2] = hx[end] end
if isinf(limits[4]) limits[4] = hy[end] end
limits[1] = ifelse(isinf(limits[1]), hx[1], limits[1])
limits[2] = ifelse(isinf(limits[2]), hx[end], limits[2])
limits[3] = ifelse(isinf(limits[3]), hy[1], limits[3])
limits[4] = ifelse(isinf(limits[4]), hy[end], limits[4])

xind = searchsortedfirst(hx, limits[1]):searchsortedlast(hx, limits[2])
yind = searchsortedfirst(hy, limits[3]):searchsortedlast(hy, limits[4])
Expand All @@ -73,12 +72,12 @@ end
hy = @view y[1,:,1]
hz = @view z[1,1,:]

if isinf(limits[1]) limits[1] = hx[1] end
if isinf(limits[3]) limits[3] = hy[1] end
if isinf(limits[5]) limits[5] = hz[1] end
if isinf(limits[2]) limits[2] = hx[end] end
if isinf(limits[4]) limits[4] = hy[end] end
if isinf(limits[6]) limits[6] = hz[end] end
limits[1] = ifelse(isinf(limits[1]), hx[1], limits[1])
limits[2] = ifelse(isinf(limits[2]), hx[end], limits[2])
limits[3] = ifelse(isinf(limits[3]), hy[1], limits[3])
limits[4] = ifelse(isinf(limits[4]), hy[end], limits[4])
limits[5] = ifelse(isinf(limits[5]), hz[1], limits[5])
limits[6] = ifelse(isinf(limits[6]), hz[end], limits[6])

xind = searchsortedfirst(hx, limits[1]):searchsortedlast(hx, limits[2])
yind = searchsortedfirst(hy, limits[3]):searchsortedlast(hy, limits[4])
Expand Down
34 changes: 15 additions & 19 deletions src/vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function convertTECtoVTU(head, data, connectivity, filename="out")

vtkfile = vtk_grid(filename, points, cells)

for ivar = head.nDim+1:nVar
for ivar in head.nDim+1:nVar
if endswith(head.variables[ivar],"_x") # vector
if head.nDim == 3
var1 = @view data[ivar,:]
Expand Down Expand Up @@ -238,7 +238,7 @@ function convertIDLtoVTK(filename::AbstractString; gridType::Int=1, verbose::Boo

vtkfile = vtk_grid(filename, points, cells)

for ivar = 1:nVar
for ivar in 1:nVar
if endswith(data.head.wnames[ivar], "x") # vector
if nDim == 3
var1 = @view data.w[:,1,1,ivar]
Expand Down Expand Up @@ -450,7 +450,7 @@ function find_tree_node(batl::Batl, Coord_D)
floor.(Int32, (Coord_D[iDimAmr_D] - iRoot_D[iDimAmr_D])*maxCoord_I[nLevelMax+1]))

# Go down the tree using bit information
for iLevel = nLevelMax-1:-1:0
for iLevel in nLevelMax-1:-1:0
# Get the binary bits based on the coordinates
iBit_D = ibits.(iCoord_D, iLevel, 1)

Expand Down Expand Up @@ -502,23 +502,19 @@ function order_tree(batl::Batl)

iNodeMorton_I = fill(Int32(unset_), nNodeUsed)

for kRoot = 1:nRoot_D[3]
for jRoot = 1:nRoot_D[2]
for iRoot = 1:nRoot_D[1]
# Root nodes are the first ones
iNode += Int32(1)
# All root nodes are handled as if they were first child
iMorton = order_children!(batl, iNode, iMorton, iNodeMorton_I)
end
end
for kRoot in 1:nRoot_D[3], jRoot in 1:nRoot_D[2], iRoot in 1:nRoot_D[1]
# Root nodes are the first ones
iNode += Int32(1)
# All root nodes are handled as if they were first child
iMorton = order_children!(batl, iNode, iMorton, iNodeMorton_I)
end

nNodeUsed = iMorton

# Set min and max refinement levels
nLevelMin = maxLevel
nLevelMax = 0
for iMorton = 1:nNodeUsed
for iMorton in 1:nNodeUsed
iNode = iNodeMorton_I[iMorton]
iLevel = iTree_IA[level_,iNode]
nLevelMin = min(iLevel, nLevelMin)
Expand All @@ -543,7 +539,7 @@ function order_children!(batl::Batl, iNode::Int32, iMorton::Int,
iMorton += 1
iNodeMorton_I[iMorton] = iNode
else
for iChild = child1_:size(iTree_IA,1)
for iChild in child1_:size(iTree_IA,1)
iMorton = order_children!(batl, iTree_IA[iChild, iNode], iMorton, iNodeMorton_I)
end
end
Expand Down Expand Up @@ -636,7 +632,7 @@ function find_neighbor_for_anynode(batl::Batl, iNode::Int)
y0 = y
for i in 0:3
# Exclude inner points
if 0<i<3 && 0<j<3 && 0<k<3 continue end
if 0 < i < 3 && 0 < j < 3 && 0 < k < 3 continue end

Di = round(Int8, (i - 1.5)/1.5)

Expand Down Expand Up @@ -728,21 +724,21 @@ function getConnectivity(batl::Batl)
nProc = maximum(iTree_IA[proc_,:]) + 1
nBlock_P = fill(Int32(0), nProc)
if nProc > 1
for iProc = 1:nProc-1
for iProc in 1:nProc-1
nBlock_P[iProc+1] = nBlock_P[iProc] + count(==(iProc-1), iTree_IA[proc_,:])
end
end

# Pre-allocate just to let Julia know this variable.
connectivity = Array{Int32,2}(undef, nConn, nElem)

for iRound = 1:2
for iRound in 1:2
if iRound == 2
connectivity = Array{Int32,2}(undef, nConn, nElem)
iElem = 0
nBlockBefore = 0 # Reset
end
for iProc = 0:nProc-1
for iProc in 0:nProc-1
localNodes_B = findall(x->x==iProc, iTree_IA[proc_,:])
# It needs to be sorted for filling the correct iCell_G indexes!
localBlocks_B = iTree_IA[block_,localNodes_B]
Expand All @@ -751,7 +747,7 @@ function getConnectivity(batl::Batl)

nBlock = length(localNodes_B) # number of blocks on this processor

for iBlock = 1:nBlock
for iBlock in 1:nBlock
# initial cell index
iCell = nI*nJ*nK*(nBlockBefore + iBlock - 1)

Expand Down

2 comments on commit 6e42bb4

@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
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/105675

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

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.5.7 -m "<description of version>" 6e42bb487579b941264bdeabd9a8f6260d23a816
git push origin v0.5.7

Please sign in to comment.