Skip to content

Commit

Permalink
Merge branch 'main' into trixibase-timers
Browse files Browse the repository at this point in the history
  • Loading branch information
efaulhaber authored Jun 17, 2024
2 parents bfe79f1 + c5dfd07 commit a3b6493
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 54 deletions.
15 changes: 10 additions & 5 deletions src/callbacks/post_process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/callbacks/solution_saving.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
31 changes: 0 additions & 31 deletions src/general/file_system.jl

This file was deleted.

4 changes: 3 additions & 1 deletion src/general/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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")
18 changes: 18 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 18 additions & 9 deletions src/visualization/write2vtk.jl
Original file line number Diff line number Diff line change
@@ -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...)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down

0 comments on commit a3b6493

Please sign in to comment.