@@ -920,3 +920,76 @@ def test_filter_time_mesh(run_in_tmpdir):
920920 f"Collision vs tracklength tallies disagree: chi2={ chi2_stat :.2f} "
921921 f">= { crit = :.2f} ({ dof = } , { alpha = } )"
922922 )
923+
924+
925+ def test_regular_mesh_get_indices_at_coords ():
926+ """Test get_indices_at_coords method for RegularMesh"""
927+ # Create a 10x10x10 mesh from (0,0,0) to (1,1,1)
928+ # Each voxel is 0.1 x 0.1 x 0.1
929+ mesh = openmc .RegularMesh ()
930+ mesh .lower_left = (0 , 0 , 0 )
931+ mesh .upper_right = (1 , 1 , 1 )
932+ mesh .dimension = [10 , 10 , 10 ]
933+
934+ # Test lower-left corner maps to first voxel (0, 0, 0)
935+ assert mesh .get_indices_at_coords ([0.0 , 0.0 , 0.0 ]) == (0 , 0 , 0 )
936+
937+ # Test centroid of first voxel
938+ # Voxel 0 spans [0.0, 0.1], so centroid is at 0.05
939+ assert mesh .get_indices_at_coords ([0.05 , 0.05 , 0.05 ]) == (0 , 0 , 0 )
940+
941+ # Test centroid of last voxel maps correctly
942+ # Voxel 9 spans [0.9, 1.0], so centroid is at 0.95
943+ assert mesh .get_indices_at_coords ([0.95 , 0.95 , 0.95 ]) == (9 , 9 , 9 )
944+
945+ # Test a middle voxel
946+ # Voxel 4 spans [0.4, 0.5], so 0.45 should map to it
947+ assert mesh .get_indices_at_coords ([0.45 , 0.45 , 0.45 ]) == (4 , 4 , 4 )
948+
949+ # Test mixed indices
950+ assert mesh .get_indices_at_coords ([0.05 , 0.45 , 0.95 ]) == (0 , 4 , 9 )
951+ assert mesh .get_indices_at_coords ([0.95 , 0.05 , 0.45 ]) == (9 , 0 , 4 )
952+
953+ # Test coordinates outside mesh bounds raise ValueError
954+ with pytest .raises (ValueError ):
955+ mesh .get_indices_at_coords ([- 0.5 , 0.5 , 0.5 ])
956+ with pytest .raises (ValueError ):
957+ mesh .get_indices_at_coords ([1.5 , 0.5 , 0.5 ])
958+ with pytest .raises (ValueError ):
959+ mesh .get_indices_at_coords ([0.5 , - 0.5 , 0.5 ])
960+ with pytest .raises (ValueError ):
961+ mesh .get_indices_at_coords ([0.5 , 1.5 , 0.5 ])
962+ with pytest .raises (ValueError ):
963+ mesh .get_indices_at_coords ([0.5 , 0.5 , - 0.5 ])
964+ with pytest .raises (ValueError ):
965+ mesh .get_indices_at_coords ([0.5 , 0.5 , 1.5 ])
966+
967+ # Test that results match expected dimensionality (3D mesh returns 3-tuple)
968+ result = mesh .get_indices_at_coords ([0.5 , 0.5 , 0.5 ])
969+ assert isinstance (result , tuple )
970+ assert len (result ) == 3
971+
972+ # Test that indices can be used directly with centroids array
973+ idx = mesh .get_indices_at_coords ([0.95 , 0.95 , 0.95 ])
974+ centroid = mesh .centroids [idx ]
975+ np .testing .assert_array_almost_equal (centroid , [0.95 , 0.95 , 0.95 ])
976+
977+ # Test with a 2D mesh
978+ mesh_2d = openmc .RegularMesh ()
979+ mesh_2d .lower_left = (0 , 0 )
980+ mesh_2d .upper_right = (1 , 1 )
981+ mesh_2d .dimension = [10 , 10 ]
982+ result_2d = mesh_2d .get_indices_at_coords ([0.5 , 0.5 , 999.0 ])
983+ assert isinstance (result_2d , tuple )
984+ assert len (result_2d ) == 2
985+ assert result_2d == (5 , 5 )
986+
987+ # Test with a 1D mesh
988+ mesh_1d = openmc .RegularMesh ()
989+ mesh_1d .lower_left = [0 ]
990+ mesh_1d .upper_right = [1 ]
991+ mesh_1d .dimension = [10 ]
992+ result_1d = mesh_1d .get_indices_at_coords ([0.5 , 999.0 , 999.0 ])
993+ assert isinstance (result_1d , tuple )
994+ assert len (result_1d ) == 1
995+ assert result_1d == (5 ,)
0 commit comments