Skip to content

Commit afa2658

Browse files
authored
Merge pull request #15 from ZenanH/main
🎉 Release new version v0.2.0
2 parents a12bae6 + 3d21e55 commit afa2658

7 files changed

Lines changed: 64 additions & 40 deletions

File tree

Project.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MaterialPointVisualizer"
22
uuid = "9ce2fbfb-c269-402f-8683-a675189e795c"
33
authors = ["ZenanH <zenan.huo@outlook.com>"]
4-
version = "0.1.11"
4+
version = "0.2.0"
55

66
[deps]
77
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
@@ -12,18 +12,20 @@ HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
1212
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1313
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1414
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
15+
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
1516
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1617
WGLMakie = "276b4fcb-3e11-5398-bf8b-a0c2d153d008"
1718
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"
1819

1920
[compat]
20-
ColorSchemes = "3.30"
21+
ColorSchemes = "3.31"
2122
DelimitedFiles = "1"
22-
FastPointQuery = "0.1"
23+
FastPointQuery = "0.2"
2324
HDF5 = "0.17"
2425
PrecompileTools = "1.2"
26+
ProgressMeter = "1.10"
2527
StatsBase = "0.34"
26-
WGLMakie = "0.11"
28+
WGLMakie = "0.13"
2729
WriteVTK = "1.21"
2830
julia = "1.11"
2931

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![CI](https://github.com/LandslideSIM/MaterialPointVisualizer.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/LandslideSIM/MaterialPointVisualizer.jl/actions/workflows/ci.yml)
44
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg?logo=quicklook)](https://LandslideSIM.github.io/MaterialPointVisualizer.jl/stable)
55
[![Dev](https://img.shields.io/badge/docs-dev-red.svg?logo=quicklook)](https://LandslideSIM.github.io/MaterialPointVisualizer.jl/dev)
6-
[![Version](https://img.shields.io/badge/version-v0.1.11-pink)]()
6+
[![Version](https://img.shields.io/badge/version-v0.2.0-pink)]()
77

88
With this package, we can convert the MPM simulation results (HDF5 files from ***[MaterialPointSolver.jl](https://github.com/LandslideSIM/MaterialPointSolver.jl)*** ) into `.vtp` files or create ParaView-compatible animations. Additionally, it includes some post-processing functionalities.
99

src/MaterialPointVisualizer.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
module MaterialPointVisualizer
1313

14-
using ColorSchemes, Dates, DelimitedFiles, HDF5, Logging, PrecompileTools, Printf, WGLMakie, WriteVTK
14+
using ColorSchemes, Dates, DelimitedFiles, HDF5, Logging, PrecompileTools, Printf,
15+
ProgressMeter, #=WGLMakie,=# WriteVTK
1516

1617
import StatsBase: sample
1718
import FastPointQuery: trimesh, splashsurf, np, meshio
@@ -26,10 +27,10 @@ import FastPointQuery: trimesh, splashsurf, np, meshio
2627
@sprintf("%02d days: %s", days, Dates.format(time, "HH:MM:SS"))
2728
end
2829

29-
include(joinpath(@__DIR__, "hdf2pvd.jl" ))
30-
include(joinpath(@__DIR__, "pts2vtp.jl" ))
31-
include(joinpath(@__DIR__, "pts2surf.jl" ))
32-
include(joinpath(@__DIR__, "plot/display.jl"))
30+
include(joinpath(@__DIR__, "hdf2pvd.jl"))
31+
include(joinpath(@__DIR__, "pts2vtp.jl"))
32+
include(joinpath(@__DIR__, "pts2surf.jl"))
33+
# include(joinpath(@__DIR__, "plot/display.jl"))
3334

3435
quiet(f) = redirect_stdout(devnull) do
3536
redirect_stderr(devnull) do
@@ -39,6 +40,6 @@ quiet(f) = redirect_stdout(devnull) do
3940
end
4041
end
4142

42-
include(joinpath(@__DIR__, "precompile.jl"))
43+
# include(joinpath(@__DIR__, "precompile.jl"))
4344

4445
end

src/hdf2pvd.jl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ If your HDF5 file is generated by other means, please refer to [WriteVTK.jl](htt
3939
h5_str = joinpath(conf.prjdst, "$(conf.prjname).h5")
4040
h5_file = isfile(h5_str) ? h5_str : throw(ArgumentError("Expected a h5 file path at $h5_str"))
4141
anim_path = joinpath(conf.prjdst, "animation"); mkpath(anim_path)
42-
pvd = paraview_collection(joinpath(conf.prjdst, "$(conf.prjname).pvd"))
42+
#pvd = paraview_collection(joinpath(conf.prjdst, "$(conf.prjname).pvd"))
4343
fid = h5open(h5_file, "r")
4444
required = ("time", "ξ")
4545
groups = Tuple{String,Float64}[]
@@ -51,34 +51,32 @@ If your HDF5 file is generated by other means, please refer to [WriteVTK.jl](htt
5151
end; sort!(groups, by = last)
5252
isempty(groups) && @warn "No valid time-step groups found in $h5_str"
5353

54-
p_total, p_iters = length(groups), 0
55-
t1, t_start = time(), time()
56-
@inbounds for (step, (gname, t)) in enumerate(groups)
57-
grp = fid[gname]
58-
coords = read(grp["ξ"])
59-
vtk_path = joinpath(anim_path, @sprintf("%08d", step))
54+
p = Progress(length(1:1:length(groups)) - 1; dt=3.0,
55+
desc = "\e[1;36m[ Info:\e[0m writing",
56+
barlen = 20,
57+
barglyphs = BarGlyphs(" ■■ ")
58+
)
59+
@inbounds Threads.@threads for igroup in eachindex(groups)
60+
grp = fid[groups[igroup][1]]
61+
coords = permutedims(read(grp["ξ"]))
62+
vtk_path = joinpath(anim_path, @sprintf("%08d", igroup))
6063
vtp_cls = [MeshCell(PolyData.Verts(), [i]) for i in 1:size(coords, 2)]
6164
vtk_grid(vtk_path, coords, vtp_cls; compress=true, append=false, ascii=false) do vtk
6265
for name in keys(grp)
6366
name in ("ξ", "time") && continue
64-
vtk[name] = read(grp[name])
67+
data = read(grp[name])
68+
data = ndims(data) == 2 ? permutedims(data) : data
69+
vtk[name] = data
6570
end
66-
pvd[t] = vtk # current simulation time
67-
end
68-
69-
t2 = time(); if t2 - t1 > 3.0
70-
percen = "animation (.pvd): " * @sprintf("%6.2f%%", (p_iters / p_total) * 100)
71-
eta = "eta: " * format_seconds((p_total - p_iters) / (p_iters / (t2 - t_start)))
72-
invo_str = " \e[1;32m⇌\e[0m "
73-
info_con = "\e[1;32m[ Info: \e[0m"
74-
print(stdout, "\r\e[2K"); print(stdout, info_con * percen * invo_str * eta)
75-
t1 = t2
7671
end
77-
p_iters += 1; p_iters + 1 p_total && println()
72+
next!(p)
7873
end
79-
close(pvd) # close Paraview collection
8074
_grid2vtr(fid, joinpath(conf.prjdst, "grid.vtr"))
8175
close(fid) # close HDF5 文件
76+
times = map(last, groups) # 取出排序后的真实时间
77+
pvd_path = joinpath(conf.prjdst, "$(conf.prjname).pvd")
78+
_write_pvd(pvd_path, times; basedir="animation", ext=".vtp", part=0)
79+
return nothing
8280
end
8381

8482
@views function _grid2vtr(fid, out_file::String)
@@ -97,4 +95,25 @@ end
9795
end
9896
vtr["h"] = h
9997
close(vtr)
98+
end
99+
100+
function _write_pvd(outpath::AbstractString, times::AbstractVector{<:Real};
101+
basedir::AbstractString = "animation",
102+
ext::AbstractString = ".vtp",
103+
part::Integer = 0)
104+
open(outpath, "w") do io
105+
println(io, """<?xml version="1.0" encoding="utf-8"?>""")
106+
println(io, """<VTKFile type="Collection" version="1.0" byte_order="LittleEndian" compressor="vtkZLibDataCompressor">""")
107+
println(io, " <Collection>")
108+
# 用 %.17g 写 timestep,既短又精确(避免 0.14749999999999996 这种串太长)
109+
for (i, t) in enumerate(times)
110+
step = @sprintf("%08d", i)
111+
tstr = @sprintf("%.17g", float(t))
112+
fileattr = string(basedir, "/", step, ext) # 强制使用正斜杠,跨平台稳妥
113+
println(io, """ <DataSet timestep="$tstr" part="$part" file="$fileattr"/>""")
114+
end
115+
println(io, " </Collection>")
116+
println(io, "</VTKFile>")
117+
end
118+
return outpath
100119
end

src/precompile.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@setup_workload begin
22
@compile_workload begin
33
quiet() do
4-
scatter(rand(10, 3))
5-
pts2vtp(rand(2, 10); vtp_file=joinpath(tempdir(), "output.vtp"), data=(x=rand(10), y=rand(10)))
4+
#scatter(rand(10, 3))
5+
# pts2vtp(rand(2, 10); vtp_file=joinpath(tempdir(), "output.vtp"), data=(x=rand(10), y=rand(10)))
66
end
77
end
88
end

src/pts2surf.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ function pts2surf(coords::AbstractArray, output_file::String;
6262
subdomain_num_cubes_per_dim::Int=64,
6363
output_mesh_smoothing_weights::Bool=true
6464
)
65-
@assert size(coords, 1) (2, 3) "coords should be a 2D or 3D array"
66-
particles = np.array(coords', dtype=np.float64)
65+
@assert size(coords, 2) (2, 3) "coords should be a 2D or 3D array"
66+
particles = np.array(coords, dtype=np.float64)
6767
@assert isfile(output_file) "output_file should be a valid file path"
6868
file_name = endswith(lowercase(output_file), ".obj") ? output_file : output_file * ".obj"
6969
mesh_with_data, reconstruction = splashsurf.reconstruction_pipeline(particles,
@@ -177,7 +177,7 @@ function hdf2surf(conf::NamedTuple;
177177
t1, t_start = time(), time()
178178
@inbounds for (step, (gname, t)) in enumerate(groups)
179179
grp = fid[gname]
180-
coords = read(grp["ξ"])' # 读取粒子坐标
180+
coords = read(grp["ξ"]) # 读取粒子坐标
181181
obj_filename = joinpath(obj_path, "$(@sprintf("%08d", step)).obj")
182182
pts2surf(coords, obj_filename;
183183
radius=radius,

src/pts2vtp.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ Description:
1919
Generates a `.vtp` file by passing custom fields.
2020
"""
2121
function pts2vtp(coords; vtp_file="output.vtp", data::NamedTuple=NamedTuple())
22-
pts_num = size(coords, 2)
23-
size(coords, 1) in [2, 3] || throw(ArgumentError("The input coordinates should be 2D or 3D"))
22+
pts_num = size(coords, 1)
23+
size(coords, 2) in [2, 3] || throw(ArgumentError("The input coordinates should be 2D or 3D"))
2424
vtp_cls = [MeshCell(PolyData.Verts(), [i]) for i in 1:pts_num]
25-
vtk_grid(vtp_file, coords, vtp_cls; compress=true, append=false, ascii=false) do vtk
25+
vtk_grid(vtp_file, coords', vtp_cls; compress=true, append=false, ascii=false) do vtk
2626
keys(data) () && for vtp_key in keys(data)
27-
vtk[string(vtp_key)] = getfield(data, vtp_key)
27+
vdata = getfield(data, vtp_key)
28+
vdata = ndims(vdata) == 2 ? permutedims(vdata) : vdata
29+
vtk[string(vtp_key)] = vdata
2830
end
2931
end
3032
end

0 commit comments

Comments
 (0)