@@ -605,7 +605,9 @@ def _yee_h(self, axis: Axis):
605605
606606 return Coords (** yee_coords )
607607
608- def discretize_inds (self , box : Box , extend : bool = False ) -> list [tuple [int , int ]]:
608+ def discretize_inds (
609+ self , box : Box , extend : bool = False , relax_precision : bool = False
610+ ) -> list [tuple [int , int ]]:
609611 """Start and stopping indexes for the cells that intersect with a :class:`Box`.
610612
611613 Parameters
@@ -616,6 +618,9 @@ def discretize_inds(self, box: Box, extend: bool = False) -> list[tuple[int, int
616618 If ``True``, ensure that the returned indexes extend sufficiently in every direction to
617619 be able to interpolate any field component at any point within the ``box``, for field
618620 components sampled on the Yee grid.
621+ relax_precision : bool = False
622+ If ``True``, relax the precision of the discretization to allow for small numerical
623+ differences between the box boundaries and the cell boundaries.
619624
620625 Returns
621626 -------
@@ -646,17 +651,36 @@ def discretize_inds(self, box: Box, extend: bool = False) -> list[tuple[int, int
646651 # handle extensions
647652 if ind_max > ind_min and extend :
648653 # Left side
649- if box . bounds [ 0 ] [axis ] < self .centers .to_list [axis ][ind_min ]:
654+ if pts_min [axis ] < self .centers .to_list [axis ][ind_min ]:
650655 # Box bounds on the left side are to the left of the closest grid center
651656 ind_min -= 1
652657
653658 # We always need an extra pixel on the right for the tangential components
654659 ind_max += 1
655660
656661 # store indexes
657- inds_list .append ((ind_min , ind_max ))
658-
659- return inds_list
662+ inds_list .append ([ind_min , ind_max ])
663+
664+ if relax_precision :
665+ for dim in range (3 ):
666+ # Fix some corner cases when the box boundary is very close to
667+ # cell boundaries but due to finite precision is slightly smaller or larger
668+ cell_bounds = np .array (boundaries .to_list [dim ])
669+ num_cells = len (cell_bounds ) - 1
670+ min_ind = inds_list [dim ][0 ]
671+ max_ind = inds_list [dim ][1 ]
672+ box_min = pts_min [dim ]
673+ box_max = pts_max [dim ]
674+ # Check if the cell boundary after current min is close to the box min,
675+ # if it is close enough then choose that to be the new minimum cell index
676+ if min_ind + 1 < num_cells and np .isclose (box_min , cell_bounds [min_ind + 1 ]):
677+ inds_list [dim ][0 ] += 1
678+ # Same but for the max cell boundary. If the current cell boundary is close to the box bounds,
679+ # then it is considered equal and the stop index should be incremented
680+ if max_ind < num_cells and np .isclose (box_max , cell_bounds [max_ind ]):
681+ inds_list [dim ][1 ] += 1
682+
683+ return [(ind_min , ind_max ) for ind_min , ind_max in inds_list ]
660684
661685 def extended_subspace (
662686 self ,
0 commit comments