subroutine write_restart_to_path(filename, mesh, flow, params, fields, species, energy, transport, step, time, chemistry_accum_dt)
character(len=*), intent(in) :: filename
type(mesh_t), intent(in) :: mesh
type(flow_mpi_t), intent(inout) :: flow
type(case_params_t), intent(in) :: params
type(flow_fields_t), intent(in) :: fields
type(species_fields_t), intent(in) :: species
type(energy_fields_t), intent(in) :: energy
type(transport_properties_t), intent(in) :: transport
integer, intent(in) :: step
real(rk), intent(in) :: time
real(rk), intent(in) :: chemistry_accum_dt
integer :: unit_id, ios
integer :: has_species, has_energy, has_variable_density, rhs_old_valid_int
integer :: species_rhs_valid_int, energy_rhs_valid_int
real(rk), allocatable :: root_scalar(:)
real(rk), allocatable :: root_matrix(:,:)
real(rk), allocatable :: root_u(:,:)
real(rk), allocatable :: face_global(:)
real(rk) :: energy_diag(12)
allocate(root_scalar(mesh%ncells))
allocate(root_u(3, mesh%ncells))
allocate(face_global(mesh%nfaces))
if (params%nspecies > 0) allocate(root_matrix(params%nspecies, mesh%ncells))
has_species = merge(1, 0, params%enable_species .and. species%nspecies > 0 .and. allocated(species%Y))
has_energy = merge(1, 0, params%enable_energy .and. energy%initialized)
has_variable_density = merge(1, 0, params%enable_variable_density)
rhs_old_valid_int = merge(1, 0, fields%rhs_old_valid)
species_rhs_valid_int = merge(1, 0, species%rhs_old_valid)
energy_rhs_valid_int = merge(1, 0, energy%rhs_old_valid)
if (flow%rank == 0) then
open(newunit=unit_id, file=trim(filename), access='stream', form='unformatted', &
status='replace', action='write', iostat=ios)
if (ios /= 0) call fatal_error('restart', 'could not open restart output: '//trim(filename))
write(unit_id) restart_magic
write(unit_id) restart_version
write(unit_id) mesh%ncells, mesh%nfaces, params%nspecies, step, flow%nprocs
write(unit_id) time, params%dt, chemistry_accum_dt
write(unit_id) has_species, has_energy, has_variable_density, rhs_old_valid_int, &
species_rhs_valid_int, energy_rhs_valid_int
end if
call flow_gather_owned_matrix_root(flow, fields%u, root_u)
if (flow%rank == 0) write(unit_id) root_u
call flow_gather_owned_matrix_root(flow, fields%u_old, root_u)
if (flow%rank == 0) write(unit_id) root_u
call flow_gather_owned_scalar_root(flow, fields%p, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, fields%phi, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, fields%divergence_source, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, fields%projection_rho, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, fields%projection_divergence_source, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_matrix_root(flow, fields%rhs_old, root_u)
if (flow%rank == 0) write(unit_id) root_u
call gather_owned_face_scalar(flow, mesh%nfaces, fields%face_flux, face_global)
if (flow%rank == 0) write(unit_id) face_global
call gather_owned_face_scalar(flow, mesh%nfaces, fields%mass_flux, face_global)
if (flow%rank == 0) write(unit_id) face_global
call flow_gather_owned_scalar_root(flow, transport%rho, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, transport%rho_old, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, transport%mu, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, transport%nu, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, transport%lambda, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
if (params%nspecies > 0) then
if (allocated(transport%diffusivity)) then
call flow_gather_owned_matrix_root(flow, transport%diffusivity, root_matrix)
else
root_matrix = zero
end if
if (flow%rank == 0) write(unit_id) root_matrix
end if
if (has_species == 1) then
call flow_gather_owned_matrix_root(flow, species%Y, root_matrix)
if (flow%rank == 0) write(unit_id) root_matrix
call flow_gather_owned_matrix_root(flow, species%rhs_old, root_matrix)
if (flow%rank == 0) write(unit_id) root_matrix
end if
if (has_energy == 1) then
call flow_gather_owned_scalar_root(flow, energy%T, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%T_old, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%h, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%h_old, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%rhs_old, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%qrad, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%cp, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%lambda, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call flow_gather_owned_scalar_root(flow, energy%rho_thermo, root_scalar)
if (flow%rank == 0) write(unit_id) root_scalar
call pack_energy_diagnostics(energy, energy_diag)
if (flow%rank == 0) write(unit_id) energy_diag
end if
if (flow%rank == 0) close(unit_id)
if (allocated(root_scalar)) deallocate(root_scalar)
if (allocated(root_u)) deallocate(root_u)
if (allocated(root_matrix)) deallocate(root_matrix)
if (allocated(face_global)) deallocate(face_global)
end subroutine write_restart_to_path