|
| 1 | +# Update `v0_ode` and `u0_ode` with the data from the restart files |
| 2 | +function set_initial_conditions!(v0_ode, u0_ode, semi, restart_with::Tuple{Vararg{String}}) |
| 3 | + # Check number of systems |
| 4 | + if length(semi.systems) != length(restart_with) |
| 5 | + throw(ArgumentError("number of systems in `semi` does not match number of restart files provided " * |
| 6 | + "in `restart_with`")) |
| 7 | + end |
| 8 | + |
| 9 | + # Check that systems match |
| 10 | + expected_system_names = system_names(semi.systems) |
| 11 | + foreach_system(semi) do system |
| 12 | + system_index = system_indices(system, semi) |
| 13 | + filename = restart_with[system_index] |
| 14 | + expected_system_name = expected_system_names[system_index] |
| 15 | + if !occursin(expected_system_name, basename(splitext(filename)[1])) |
| 16 | + throw(ArgumentError("Filename '$filename' for system $system_index does not contain expected name '$expected_system_name'. " * |
| 17 | + "Expected a VTK file for system of type $(nameof(typeof(system))).")) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + # Set initial conditions |
| 22 | + foreach_noalloc(semi.systems, restart_with) do (system, restart_file) |
| 23 | + v0_system = wrap_v(v0_ode, system, semi) |
| 24 | + u0_system = wrap_u(u0_ode, system, semi) |
| 25 | + |
| 26 | + restart_data = vtk2trixi(restart_file; create_initial_condition=false) |
| 27 | + v_restart = restart_v(system, restart_data) |
| 28 | + u_restart = restart_u(system, restart_data) |
| 29 | + |
| 30 | + v0_system .= Adapt.adapt(semi.parallelization_backend, v_restart) |
| 31 | + u0_system .= Adapt.adapt(semi.parallelization_backend, u_restart) |
| 32 | + |
| 33 | + restore_previous_state!(system, restart_file) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +# Compute a new `tspan` based on the restart files |
| 38 | +function time_span(tspan, restart_with::Tuple{Vararg{String}}) |
| 39 | + # Read restart times from all files |
| 40 | + restart_times = [vtk2trixi(file).time for file in restart_with] |
| 41 | + t_restart = convert(eltype(tspan), first(restart_times)) |
| 42 | + |
| 43 | + # Check if all restart files have the same time |
| 44 | + if !all(isapprox(t, t_restart) for t in restart_times) |
| 45 | + throw(ArgumentError("all restart files must start from the same time")) |
| 46 | + end |
| 47 | + |
| 48 | + if !isapprox(tspan[1], t_restart) |
| 49 | + @info "Adjusting initial time from $(tspan[1]) to restart time $t_restart" |
| 50 | + end |
| 51 | + |
| 52 | + return (t_restart, tspan[2]) |
| 53 | +end |
| 54 | + |
| 55 | +function write_density_and_pressure!(v_restart, system, density_calculator, |
| 56 | + pressure, density) |
| 57 | + return v_restart |
| 58 | +end |
| 59 | + |
| 60 | +function write_density_and_pressure!(v_restart, system, |
| 61 | + density_calculator::ContinuityDensity, |
| 62 | + pressure, density) |
| 63 | + v_restart[size(v_restart, 1), :] = density |
| 64 | + |
| 65 | + return v_restart |
| 66 | +end |
| 67 | + |
| 68 | +function write_density_and_pressure!(v_restart, system::EntropicallyDampedSPHSystem, |
| 69 | + density_calculator::ContinuityDensity, |
| 70 | + pressure, density) |
| 71 | + v_restart[size(v_restart, 1), :] = density |
| 72 | + v_restart[size(v_restart, 1) - 1, :] = pressure |
| 73 | + |
| 74 | + return v_restart |
| 75 | +end |
| 76 | + |
| 77 | +function write_density_and_pressure!(v_restart, system::EntropicallyDampedSPHSystem, |
| 78 | + density_calculator::SummationDensity, |
| 79 | + pressure, density) |
| 80 | + v_restart[size(v_restart, 1) - 1, :] = pressure |
| 81 | + |
| 82 | + return v_restart |
| 83 | +end |
| 84 | + |
| 85 | +restore_previous_state!(system, restart_file) = system |
| 86 | + |
| 87 | +function initialize_neighborhood_searches!(semi, u0_ode, |
| 88 | + restart_with::Tuple{Vararg{String}}) |
| 89 | + foreach_system(semi) do system |
| 90 | + foreach_system(semi) do neighbor |
| 91 | + initialize_neighborhood_search!(semi, system, neighbor, u0_ode) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + return semi |
| 96 | +end |
| 97 | + |
| 98 | +function initialize_neighborhood_search!(semi, system, neighbor, u0_ode) |
| 99 | + # TODO Initialize after adapting to the GPU. |
| 100 | + # Currently, this cannot use `semi.parallelization_backend` |
| 101 | + # because data is still on the CPU. |
| 102 | + PointNeighbors.initialize!(get_neighborhood_search(system, neighbor, semi), |
| 103 | + initial_restart_coordinates(system, u0_ode, semi), |
| 104 | + initial_restart_coordinates(neighbor, u0_ode, semi), |
| 105 | + eachindex_y=each_active_particle(neighbor), |
| 106 | + parallelization_backend=PolyesterBackend()) |
| 107 | + return semi |
| 108 | +end |
| 109 | + |
| 110 | +function initialize_neighborhood_search!(semi, system::TotalLagrangianSPHSystem, |
| 111 | + neighbor::TotalLagrangianSPHSystem, u0_ode) |
| 112 | + # For TLSPH, the self-interaction NHS is already initialized in the system constructor |
| 113 | + return semi |
| 114 | +end |
| 115 | + |
| 116 | +function initial_restart_coordinates(system, u0_ode, semi) |
| 117 | + # Transfer to CPU if data is on the GPU. Do nothing if already on CPU. |
| 118 | + return transfer2cpu(wrap_u(u0_ode, system, semi)) |
| 119 | +end |
| 120 | + |
| 121 | +function initial_restart_coordinates(system::Union{WallBoundarySystem, |
| 122 | + AbstractStructureSystem}, u0_ode, semi) |
| 123 | + return initial_coordinates(system) |
| 124 | +end |
| 125 | + |
| 126 | +function initialize!(semi::Semidiscretization, restart_with::Tuple{Vararg{String}}) |
| 127 | + foreach_system(semi) do system |
| 128 | + # Initialize this system |
| 129 | + initialize_restart!(system, semi) |
| 130 | + end |
| 131 | + |
| 132 | + return semi |
| 133 | +end |
| 134 | + |
| 135 | +initialize_restart!(system, semi) = initialize!(system, semi) |
0 commit comments