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) 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 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)