diff --git a/src/callbacks/post_process.jl b/src/callbacks/post_process.jl index e2e98a2a0..d68168258 100644 --- a/src/callbacks/post_process.jl +++ b/src/callbacks/post_process.jl @@ -73,6 +73,7 @@ struct PostprocessCallback{I, F} append_timestamp :: Bool write_csv :: Bool write_json :: Bool + git_hash :: Ref{String} end function PostprocessCallback(; interval::Integer=0, dt=0.0, exclude_boundary=true, @@ -94,7 +95,8 @@ function PostprocessCallback(; interval::Integer=0, dt=0.0, exclude_boundary=tru post_callback = PostprocessCallback(interval, write_file_interval, Dict{String, Vector{Any}}(), Float64[], exclude_boundary, funcs, filename, output_directory, - append_timestamp, write_csv, write_json) + append_timestamp, write_csv, write_json, + Ref("UnknownVersion")) if dt > 0 # Add a `tstop` every `dt`, and save the final solution return PeriodicCallback(post_callback, dt, @@ -209,9 +211,12 @@ function initialize_postprocess_callback!(cb, u, t, integrator) end function initialize_postprocess_callback!(cb::PostprocessCallback, u, t, integrator) + cb.git_hash[] = compute_git_hash() + # Apply the callback cb(integrator) - return nothing + + return cb end # `condition` with interval @@ -281,7 +286,7 @@ function write_postprocess_callback(pp::PostprocessCallback) mkpath(pp.output_directory) data = Dict{String, Any}() - write_meta_data!(data) + write_meta_data!(data, pp.git_hash[]) prepare_series_data!(data, pp) time_stamp = "" @@ -331,9 +336,9 @@ function create_series_dict(values, times, system_name="") "time" => times) end -function write_meta_data!(data) +function write_meta_data!(data, git_hash) meta_data = Dict("solver_name" => "TrixiParticles.jl", - "solver_version" => get_git_hash(), + "solver_version" => git_hash, "julia_version" => string(VERSION)) data["meta"] = meta_data diff --git a/src/callbacks/solution_saving.jl b/src/callbacks/solution_saving.jl index 5163199d6..04552d57e 100644 --- a/src/callbacks/solution_saving.jl +++ b/src/callbacks/solution_saving.jl @@ -77,6 +77,7 @@ mutable struct SolutionSavingCallback{I, CQ} max_coordinates :: Float64 custom_quantities :: CQ latest_saved_iter :: Int + git_hash :: Ref{String} end function SolutionSavingCallback(; interval::Integer=0, dt=0.0, @@ -102,7 +103,7 @@ function SolutionSavingCallback(; interval::Integer=0, dt=0.0, save_initial_solution, save_final_solution, write_meta_data, verbose, output_directory, prefix, max_coordinates, custom_quantities, - -1) + -1, Ref("UnknownVersion")) if length(save_times) > 0 # See the large comment below for an explanation why we use `finalize` here @@ -131,6 +132,7 @@ end function initialize_save_cb!(solution_callback::SolutionSavingCallback, u, t, integrator) # Reset `latest_saved_iter` solution_callback.latest_saved_iter = -1 + solution_callback.git_hash[] = compute_git_hash() # Save initial solution if solution_callback.save_initial_solution @@ -156,7 +158,7 @@ end # `affect!` function (solution_callback::SolutionSavingCallback)(integrator) - (; interval, output_directory, custom_quantities, write_meta_data, + (; interval, output_directory, custom_quantities, write_meta_data, git_hash, verbose, prefix, latest_saved_iter, max_coordinates) = solution_callback vu_ode = integrator.u @@ -178,12 +180,10 @@ function (solution_callback::SolutionSavingCallback)(integrator) println("Writing solution to $output_directory at t = $(integrator.t)") end - @trixi_timeit timer() "save solution" trixi2vtk(vu_ode, semi, integrator.t; iter=iter, - output_directory=output_directory, - prefix=prefix, - write_meta_data=write_meta_data, - max_coordinates=max_coordinates, - custom_quantities...) + @trixi_timeit timer() "save solution" trixi2vtk(vu_ode, semi, integrator.t; + iter, output_directory, prefix, + write_meta_data, git_hash=git_hash[], + max_coordinates, custom_quantities...) # Tell OrdinaryDiffEq that `u` has not been modified u_modified!(integrator, false) diff --git a/src/general/file_system.jl b/src/general/file_system.jl deleted file mode 100644 index 0cc4cc56d..000000000 --- a/src/general/file_system.jl +++ /dev/null @@ -1,31 +0,0 @@ -vtkname(system::FluidSystem) = "fluid" -vtkname(system::SolidSystem) = "solid" -vtkname(system::BoundarySystem) = "boundary" - -function system_names(systems) - # Add `_i` to each system name, where `i` is the index of the corresponding - # system type. - # `["fluid", "boundary", "boundary"]` becomes `["fluid_1", "boundary_1", "boundary_2"]`. - cnames = systems .|> vtkname - filenames = [string(cnames[i], "_", count(==(cnames[i]), cnames[1:i])) - for i in eachindex(cnames)] - return filenames -end - -function get_git_hash() - pkg_directory = pkgdir(@__MODULE__) - git_directory = joinpath(pkg_directory, ".git") - - # Check if the .git directory exists - if !isdir(git_directory) - return "UnknownVersion" - end - - try - git_cmd = Cmd(`git describe --tags --always --first-parent --dirty`, - dir=pkg_directory) - return string(readchomp(git_cmd)) - catch e - return "UnknownVersion" - end -end diff --git a/src/general/general.jl b/src/general/general.jl index dfe52e06a..4a60cb52f 100644 --- a/src/general/general.jl +++ b/src/general/general.jl @@ -7,12 +7,15 @@ GPUSystem = System{NDIMS, Nothing} where {NDIMS} abstract type FluidSystem{NDIMS, IC} <: System{NDIMS, IC} end timer_name(::FluidSystem) = "fluid" +vtkname(system::FluidSystem) = "fluid" abstract type SolidSystem{NDIMS, IC} <: System{NDIMS, IC} end timer_name(::SolidSystem) = "solid" +vtkname(system::SolidSystem) = "solid" abstract type BoundarySystem{NDIMS, IC} <: System{NDIMS, IC} end timer_name(::BoundarySystem) = "boundary" +vtkname(system::BoundarySystem) = "boundary" @inline function set_zero!(du) du .= zero(eltype(du)) @@ -29,6 +32,5 @@ include("smoothing_kernels.jl") include("initial_condition.jl") include("system.jl") include("interpolation.jl") -include("file_system.jl") include("custom_quantities.jl") include("neighborhood_search.jl") diff --git a/src/util.jl b/src/util.jl index f04589947..338a94415 100644 --- a/src/util.jl +++ b/src/util.jl @@ -164,3 +164,21 @@ end function type2string(type) return string(nameof(typeof(type))) end + +function compute_git_hash() + pkg_directory = pkgdir(@__MODULE__) + git_directory = joinpath(pkg_directory, ".git") + + # Check if the .git directory exists + if !isdir(git_directory) + return "UnknownVersion" + end + + try + git_cmd = Cmd(`git describe --tags --always --first-parent --dirty`, + dir=pkg_directory) + return string(readchomp(git_cmd)) + catch e + return "UnknownVersion" + end +end diff --git a/src/visualization/write2vtk.jl b/src/visualization/write2vtk.jl index bd1658e32..a05e6727e 100644 --- a/src/visualization/write2vtk.jl +++ b/src/visualization/write2vtk.jl @@ -1,3 +1,13 @@ +function system_names(systems) + # Add `_i` to each system name, where `i` is the index of the corresponding + # system type. + # `["fluid", "boundary", "boundary"]` becomes `["fluid_1", "boundary_1", "boundary_2"]`. + cnames = vtkname.(systems) + filenames = [string(cnames[i], "_", count(==(cnames[i]), cnames[1:i])) + for i in eachindex(cnames)] + return filenames +end + """ trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix="", write_meta_data=true, max_coordinates=Inf, custom_quantities...) @@ -39,13 +49,14 @@ trixi2vtk(sol.u[end], semi, 0.0, iter=1, my_custom_quantity=kinetic_energy) ``` """ function trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix="", - write_meta_data=true, max_coordinates=Inf, custom_quantities...) + write_meta_data=true, git_hash=compute_git_hash(), + max_coordinates=Inf, custom_quantities...) (; systems) = semi v_ode, u_ode = vu_ode.x # Update quantities that are stored in the systems. These quantities (e.g. pressure) # still have the values from the last stage of the previous step if not updated here. - update_systems_and_nhs(v_ode, u_ode, semi, t) + @trixi_timeit timer() "update systems" update_systems_and_nhs(v_ode, u_ode, semi, t) filenames = system_names(systems) @@ -57,17 +68,15 @@ function trixi2vtk(vu_ode, semi, t; iter=nothing, output_directory="out", prefix periodic_box = get_neighborhood_search(system, semi).periodic_box trixi2vtk(v, u, t, system, periodic_box; - output_directory=output_directory, - system_name=filenames[system_index], iter=iter, prefix=prefix, - write_meta_data=write_meta_data, max_coordinates=max_coordinates, - custom_quantities...) + system_name=filenames[system_index], output_directory, iter, prefix, + write_meta_data, git_hash, max_coordinates, custom_quantities...) end end # Convert data for a single TrixiParticle system to VTK format function trixi2vtk(v, u, t, system, periodic_box; output_directory="out", prefix="", iter=nothing, system_name=vtkname(system), write_meta_data=true, - max_coordinates=Inf, + max_coordinates=Inf, git_hash=compute_git_hash(), custom_quantities...) mkpath(output_directory) @@ -98,7 +107,7 @@ function trixi2vtk(v, u, t, system, periodic_box; output_directory="out", prefix end end - vtk_grid(file, points, cells) do vtk + @trixi_timeit timer() "write to vtk" vtk_grid(file, points, cells) do vtk # dispatches based on the different system types e.g. FluidSystem, TotalLagrangianSPHSystem write2vtk!(vtk, v, u, t, system, write_meta_data=write_meta_data) @@ -107,7 +116,7 @@ function trixi2vtk(v, u, t, system, periodic_box; output_directory="out", prefix vtk["time"] = t if write_meta_data - vtk["solver_version"] = get_git_hash() + vtk["solver_version"] = git_hash vtk["julia_version"] = string(VERSION) end