subroutine compute_step_health(mesh, flow, params, fields, transport, kinetic_energy, cfl_value, max_velocity)
type(mesh_t), intent(in) :: mesh
type(flow_mpi_t), intent(in) :: flow
type(case_params_t), intent(in) :: params
type(flow_fields_t), intent(in) :: fields
type(transport_properties_t), intent(in) :: transport
real(rk), intent(out) :: kinetic_energy
real(rk), intent(out) :: cfl_value
real(rk), intent(out) :: max_velocity
integer :: c, lf, f, ierr
real(rk) :: rho_cell, vmag, local_ke, global_ke
real(rk) :: local_umax, global_umax
real(rk) :: local_cfl_rate, global_cfl_rate, cell_outward_flux
local_ke = zero
local_umax = zero
local_cfl_rate = zero
do c = flow%first_cell, flow%last_cell
rho_cell = params%rho
if (params%enable_variable_density .and. allocated(transport%rho)) then
if (c <= size(transport%rho)) rho_cell = max(transport%rho(c), tiny_safe)
end if
vmag = sqrt(dot_product(fields%u(:, c), fields%u(:, c)))
local_umax = max(local_umax, vmag)
local_ke = local_ke + 0.5_rk * rho_cell * mesh%cells(c)%volume * vmag * vmag
if (allocated(fields%face_flux)) then
cell_outward_flux = zero
do lf = 1, mesh%ncell_faces(c)
f = mesh%cell_faces(lf, c)
cell_outward_flux = cell_outward_flux + abs(fields%face_flux(f))
end do
if (mesh%cells(c)%volume > tiny_safe) then
local_cfl_rate = max(local_cfl_rate, 0.5_rk * cell_outward_flux / mesh%cells(c)%volume)
end if
end if
end do
call MPI_Allreduce(local_ke, global_ke, 1, MPI_DOUBLE_PRECISION, MPI_SUM, flow%comm, ierr)
if (ierr /= MPI_SUCCESS) call fatal_error('main', 'MPI failure reducing step kinetic energy')
call MPI_Allreduce(local_umax, global_umax, 1, MPI_DOUBLE_PRECISION, MPI_MAX, flow%comm, ierr)
if (ierr /= MPI_SUCCESS) call fatal_error('main', 'MPI failure reducing step max velocity')
call MPI_Allreduce(local_cfl_rate, global_cfl_rate, 1, MPI_DOUBLE_PRECISION, MPI_MAX, flow%comm, ierr)
if (ierr /= MPI_SUCCESS) call fatal_error('main', 'MPI failure reducing step CFL rate')
kinetic_energy = global_ke
max_velocity = global_umax
cfl_value = global_cfl_rate * params%dt
end subroutine compute_step_health