From 560ec97c5f1223903c4f4d4fa967b0242f8be53b Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 22 Jul 2026 14:10:34 +0000 Subject: [PATCH 1/3] Only call adjust_wall for photons that were successfully placed in a cell since the cell indices of killed photons would index the wall arrays out of bounds --- src/grid/grid_geometry_cartesian_3d.f90 | 6 ++++-- src/grid/grid_geometry_cylindrical_3d.f90 | 6 ++++-- src/grid/grid_geometry_spherical_3d.f90 | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/grid/grid_geometry_cartesian_3d.f90 b/src/grid/grid_geometry_cartesian_3d.f90 index 6c0ea0ef..2d1ccd6b 100644 --- a/src/grid/grid_geometry_cartesian_3d.f90 +++ b/src/grid/grid_geometry_cartesian_3d.f90 @@ -250,10 +250,12 @@ subroutine place_in_cell(p) p%killed = .true. else p%in_cell = .true. + ! Only adjust the wall position for photons that were successfully + ! placed in a cell - for killed photons the cell indices are invalid + ! and adjust_wall would index the wall arrays out of bounds. + call adjust_wall(p) end if - call adjust_wall(p) - end subroutine place_in_cell logical function escaped_photon(p) diff --git a/src/grid/grid_geometry_cylindrical_3d.f90 b/src/grid/grid_geometry_cylindrical_3d.f90 index 0dd5ae33..1811c84a 100644 --- a/src/grid/grid_geometry_cylindrical_3d.f90 +++ b/src/grid/grid_geometry_cylindrical_3d.f90 @@ -358,10 +358,12 @@ subroutine place_in_cell(p) p%killed = .true. else p%in_cell = .true. + ! Only adjust the wall position for photons that were successfully + ! placed in a cell - for killed photons the cell indices are invalid + ! and adjust_wall would index the wall arrays out of bounds. + call adjust_wall(p) end if - call adjust_wall(p) - end subroutine place_in_cell logical function escaped_photon(p) diff --git a/src/grid/grid_geometry_spherical_3d.f90 b/src/grid/grid_geometry_spherical_3d.f90 index ec99307d..1ad35c06 100644 --- a/src/grid/grid_geometry_spherical_3d.f90 +++ b/src/grid/grid_geometry_spherical_3d.f90 @@ -476,10 +476,12 @@ subroutine place_in_cell(p) p%killed = .true. else p%in_cell = .true. + ! Only adjust the wall position for photons that were successfully + ! placed in a cell - for killed photons the cell indices are invalid + ! and adjust_wall would index the wall arrays out of bounds. + call adjust_wall(p) end if - call adjust_wall(p) - end subroutine place_in_cell logical function escaped_photon(p) From ce5be1db22c11da2cdda34e61efb338ee8f797e6 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 22 Jul 2026 14:10:34 +0000 Subject: [PATCH 2/3] Fix an off by one error in the neighbor slice when reading Voronoi grids which made the right hand side one element longer than the neighbor array --- src/grid/grid_geometry_voronoi.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grid/grid_geometry_voronoi.f90 b/src/grid/grid_geometry_voronoi.f90 index 6af303fc..03dd3406 100644 --- a/src/grid/grid_geometry_voronoi.f90 +++ b/src/grid/grid_geometry_voronoi.f90 @@ -144,7 +144,7 @@ subroutine setup_grid_geometry(group) ! Specifically: ! - sparse_idx(is) + 1 because sparse_idx is an array of C-style indices, ! - sparse_neighs(...) + 1 because the neighbour indices are C-style. - geo%cells(ic)%neighbors(1:n_neighbors) = sparse_neighs(sparse_idx(is) + 1:sparse_idx(is + 1) + 1) + 1 + geo%cells(ic)%neighbors(1:n_neighbors) = sparse_neighs(sparse_idx(is) + 1:sparse_idx(is + 1)) + 1 is = is + 1 end do From 07b4d4dc3db7ad8ad70b29d016d71bd44c6d040c Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 22 Jul 2026 14:10:34 +0000 Subject: [PATCH 3/3] Return immediately from the grid propagation routines for photons that start outside the grid, which happens for photons emitted exactly on the outer edge of the grid moving outwards --- src/grid/grid_propagate_3d.f90 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/grid/grid_propagate_3d.f90 b/src/grid/grid_propagate_3d.f90 index 41549b4b..12794d5d 100644 --- a/src/grid/grid_propagate_3d.f90 +++ b/src/grid/grid_propagate_3d.f90 @@ -71,6 +71,11 @@ subroutine grid_integrate(p,tau_required,tau_achieved) if(.not.p%in_cell) call error("grid_integrate", "photon has not been placed in a cell") + ! A photon emitted exactly on the outer edge of the grid moving outwards + ! is placed by adjust_wall in the cell just outside the grid, so it + ! escapes immediately without crossing any walls. + if(escaped(p)) return + if(allocated(n_photons)) then if(last_photon_id(p%icell%ic).ne.p%id) then n_photons(p%icell%ic) = n_photons(p%icell%ic) + 1 @@ -259,6 +264,11 @@ subroutine grid_integrate_noenergy(p,tau_required,tau_achieved) if(.not.p%in_cell) call error("grid_integrate_noenergy", "photon has not been placed in a cell") + ! A photon emitted exactly on the outer edge of the grid moving outwards + ! is placed by adjust_wall in the cell just outside the grid, so it + ! escapes immediately without crossing any walls. + if(escaped(p)) return + if(tau_required==0._dp) return ! Check what the distance to the nearest source is @@ -388,6 +398,14 @@ subroutine grid_escape_tau(p_orig,tmax,tau,killed) if(.not.p%in_cell) call error("grid_escape_tau", "photon has not been placed in a cell") + ! A photon emitted exactly on the outer edge of the grid moving outwards + ! is placed by adjust_wall in the cell just outside the grid, so it + ! escapes immediately without crossing any walls. + if(escaped(p)) then + tau = 0._dp + return + end if + ! Check what the distance to the nearest source is call find_nearest_source(p%r, p%v, t_source, source_id) @@ -485,6 +503,14 @@ subroutine grid_escape_column_density(p_orig,tmax,column_density,killed) if(.not.p%in_cell) call error("grid_escape_column_density", "photon has not been placed in a cell") + ! A photon emitted exactly on the outer edge of the grid moving outwards + ! is placed by adjust_wall in the cell just outside the grid, so it + ! escapes immediately without crossing any walls. + if(escaped(p)) then + column_density = 0._dp + return + end if + ! Check what the distance to the nearest source is call find_nearest_source(p%r, p%v, t_source, source_id)