Skip to content

Commit

Permalink
Using Parsers for faster parsing (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
henry2004y authored Jul 4, 2024
1 parent 3d28155 commit e239440
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
4 changes: 3 additions & 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.10"
version = "0.5.11"

[deps]
FortranFiles = "c58ffaec-ab22-586d-bfc5-781a99fd0b10"
Expand All @@ -10,6 +10,7 @@ HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
NaturalNeighbours = "f16ad982-4edb-46b1-8125-78e5a8b5a9e6"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand All @@ -32,6 +33,7 @@ Interpolations = "0.14, 0.15"
LightXML = "0.9"
Makie = "0.21"
NaturalNeighbours = "1.3"
Parsers = "2.4"
PyPlot = "2.9"
RecipesBase = "1.1"
Reexport = "1.2"
Expand Down
1 change: 1 addition & 0 deletions src/Batsrus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Batsrus
# Hongyang Zhou, [email protected]

using Printf, Reexport, Requires
using Parsers
using NaturalNeighbours: interpolate, Triangle

export BATLData,
Expand Down
44 changes: 22 additions & 22 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end

"Read information from log file."
function readlogdata(file::AbstractString)
f = open(file, "r")
f = open(file)
nLine = countlines(f) - 2
seekstart(f)
headline = readline(f)
Expand All @@ -65,7 +65,7 @@ function readlogdata(file::AbstractString)
data = zeros(nw, nLine)
@inbounds for i in 1:nLine
line = split(readline(f))
data[:,i] = parse.(Float64, line)
data[:,i] = Parsers.parse.(Float64, line)
end

close(f)
Expand Down Expand Up @@ -113,7 +113,7 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
ln = readline(f) |> strip
end
VARS = split(varline, '\"')
deleteat!(VARS,findall(x -> x in (" ",", ") || isempty(x), VARS))
deleteat!(VARS, findall(x -> x in (" ",", ") || isempty(x), VARS))
else
@warn "No variable names provided."
end
Expand All @@ -131,9 +131,9 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
if name == "T" # ZONE title
T = value
elseif name in ("NODES","N")
nNode = parse(Int32, value)
nNode = Parsers.parse(Int32, value)
elseif name in ("ELEMENTS","E")
nCell = parse(Int32, value)
nCell = Parsers.parse(Int32, value)
elseif name in ("ET","ZONETYPE")
if uppercase(value) in ("BRICK","FEBRICK")
nDim = 3
Expand All @@ -155,9 +155,9 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
name = string(name[9:end-1])
str = string(strip(value))
if name in ("ITER","NPROC")
str = parse(Int32, value)
str = Parsers.parse(Int32, value)
elseif name == "TIMESIM"
sec = split(str,"=")
sec = split(str, "=")
str = string(strip(sec[2]))
end
push!(auxdataname, name)
Expand All @@ -179,7 +179,7 @@ function readtecdata(file::AbstractString; verbose::Bool=false)

IsBinary = false
try
parse.(Float32, split(readline(f)))
Parsers.parse.(Float32, split(readline(f)))
catch
IsBinary = true
verbose && @info "reading binary file"
Expand All @@ -197,11 +197,11 @@ function readtecdata(file::AbstractString; verbose::Bool=false)
else
@inbounds for i in 1:nNode
x = readline(f)
data[:,i] .= parse.(Float32, split(x))
data[:,i] .= Parsers.parse.(Float32, split(x))
end
@inbounds for i in 1:nCell
x = readline(f)
connectivity[:,i] .= parse.(Int32, split(x))
connectivity[:,i] .= Parsers.parse.(Int32, split(x))
end
end

Expand Down Expand Up @@ -273,16 +273,16 @@ function getfilehead(fileID::IOStream, filelist::FileList)
headline = readline(fileID)
line = readline(fileID)
line = split(line)
it = parse(Int, line[1])
t = parse(Float64, line[2])
ndim = parse(Int8, line[3])
neqpar = parse(Int32, line[4])
nw = parse(Int8, line[5])
it = Parsers.parse(Int, line[1])
t = Parsers.parse(Float64, line[2])
ndim = Parsers.parse(Int8, line[3])
neqpar = Parsers.parse(Int32, line[4])
nw = Parsers.parse(Int8, line[5])
gencoord = ndim < 0
ndim = abs(ndim)
nx = parse.(Int64, split(readline(fileID)))
nx = Parsers.parse.(Int64, split(readline(fileID)))
if neqpar > 0
eqpar = parse.(Float64, split(readline(fileID)))
eqpar = Parsers.parse.(Float64, split(readline(fileID)))
end
varname = readline(fileID)
elseif type (:real4, :binary)
Expand Down Expand Up @@ -342,12 +342,12 @@ function getfilesize(fileID::IOStream, type::Symbol, lenstr::Int32)
skipline(fileID)
line = readline(fileID)
line = split(line)
ndim = parse(Int32, line[3])
neqpar = parse(Int32, line[4])
nw = parse(Int8, line[5])
ndim = Parsers.parse(Int32, line[3])
neqpar = Parsers.parse(Int32, line[4])
nw = Parsers.parse(Int8, line[5])
gencoord = ndim < 0
ndim = abs(ndim)
nx = parse.(Int64, split(readline(fileID)))
nx = Parsers.parse.(Int64, split(readline(fileID)))
neqpar > 0 && skipline(fileID)
skipline(fileID)
elseif type (:real4, :binary)
Expand Down Expand Up @@ -412,7 +412,7 @@ function getascii!(x, w, fileID::IOStream, filehead::NamedTuple)
ndim = filehead.ndim
Ids = CartesianIndices(size(x)[1:ndim])
@inbounds @views for ids in Ids
temp = parse.(Float64, split(readline(fileID)))
temp = Parsers.parse.(Float64, split(readline(fileID)))
x[ids,:] = temp[1:ndim]
w[ids,:] = temp[ndim+1:end]
end
Expand Down

2 comments on commit e239440

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

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.11 -m "<description of version>" e239440ed9dd7b84bc5aafa8384b9f8ebeda30d4
git push origin v0.5.11

Please sign in to comment.