Skip to content

Commit 52bf5f0

Browse files
committed
Add region labels to BoxInternalBoundary via centroid classification
BoxInternalBoundary uses a different label import path (Face Sets + _dm_unstack_bcs) that conflicts with useRegions=True. Instead of changing the import mechanism, classify cells by centroid position after mesh construction: cells below zintCoord = Inner, above = Outer. Works for both 2D and 3D, simplex and structured meshes. extract_region("Inner") verified on 2D box. All 21 boundary integral tests pass. Underworld development team with AI support from Claude Code
1 parent 5492ed5 commit 52bf5f0

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/underworld3/meshing/cartesian.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,12 +906,49 @@ def box_return_coords_to_bounds(coords):
906906
)
907907
uw.adaptivity._dm_unstack_bcs(new_mesh.dm, new_mesh.boundaries, "Face Sets")
908908

909-
# Assign regions
909+
# Create region labels by classifying cells based on centroid position
910+
# relative to the internal boundary coordinate
910911
if dim == 2:
911912
new_mesh.regions = regions_2D
912913
else:
913914
new_mesh.regions = regions_3D
914915

916+
dm = new_mesh.dm
917+
depth_label = dm.getLabel("depth")
918+
cell_is = depth_label.getStratumIS(dim)
919+
920+
if cell_is:
921+
cells = cell_is.getIndices()
922+
coord_sec = dm.getCoordinateSection()
923+
coord_vec = dm.getCoordinatesLocal()
924+
coord_arr = coord_vec.array
925+
926+
for region in new_mesh.regions:
927+
dm.createLabel(region.name)
928+
929+
inner_label = dm.getLabel(new_mesh.regions.Inner.name)
930+
outer_label = dm.getLabel(new_mesh.regions.Outer.name)
931+
932+
# z-coordinate index: 1 for 2D (y), 2 for 3D (z)
933+
z_idx = dim - 1
934+
935+
for cell in cells:
936+
# Compute centroid from cell vertex coordinates
937+
closure = dm.getTransitiveClosure(cell)[0]
938+
vert_coords = []
939+
for pt in closure:
940+
ndof = coord_sec.getDof(pt)
941+
if ndof > 0:
942+
off = coord_sec.getOffset(pt)
943+
vert_coords.append(coord_arr[off + z_idx])
944+
945+
if vert_coords:
946+
centroid_z = sum(vert_coords) / len(vert_coords)
947+
if centroid_z < zintCoord:
948+
inner_label.setValue(cell, new_mesh.regions.Inner.value)
949+
else:
950+
outer_label.setValue(cell, new_mesh.regions.Outer.value)
951+
915952
return new_mesh
916953

917954

0 commit comments

Comments
 (0)