@@ -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
8280end
8381
8482@views function _grid2vtr (fid, out_file:: String )
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
100119end
0 commit comments