flow
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
verify_segmentation.py
Go to the documentation of this file.
1"""Verify the SDF segmentation / geometry import."""
2import sys
3import os
4import struct
5import numpy as np
6import argparse
7
8sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../build')))
9sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../cfd_utils')))
10from peclet.flow import pnm
11from vti import save_vti
12
13def verify_segmentation(input_file, output_file, edge_file):
14 print(f"Reading {input_file}...")
15 # New Binding returns: (numpy_array_3d, origin_zyx, spacing_zyx)
16 sdf_3d, origin, spacing = pnm.SDFReader.read_vti(input_file)
17
18 # Resolution/Shape is now inherent in the array
19 shape = sdf_3d.shape
20 print(f"Grid Shape (Nz, Ny, Nx): {shape}")
21
22 print("Running Segmentation...")
23 # Updated binding accepts the 3D array and ZYX spacing
24 segmentation_flat = pnm.segment_volume(sdf_3d, spacing)
25
26 # Reshape the flat result to our 3D convention
27 seg_3d = np.array(segmentation_flat, dtype=np.int32).reshape(shape)
28
29 # Stats logic
30 unique_labels = np.unique(seg_3d)
31 pores = unique_labels[unique_labels > 0]
32 solids = unique_labels[unique_labels < 0]
33
34 print(f"Total Labels: {len(unique_labels)}")
35 print(f"Pore IDs: {len(pores)}")
36 print(f"Solid IDs: {len(solids)}")
37
38 # Save output using the general saver
39 print(f"Saving segmented volume to {output_file}...")
40 save_vti(output_file,
41 fields={"Labels": seg_3d, "SDF": sdf_3d},
42 spacing=spacing,
43 origin=origin)
44
45 print("Extracting Topology...")
46 # Note: If extract_topology_gpu still needs the {nx, ny, nz} list for its
47 # internal logic, we reverse our ZYX shape back to XYZ: shape[::-1]
48 connections = pnm.extract_topology_gpu(segmentation_flat, shape[::-1])
49 print(f"Found {len(connections)} connections.")
50
51 with open(edge_file, "w") as f:
52 for u, v in connections:
53 f.write(f"{u} {v}\n")
54 print(f"Saved {edge_file}")
55
56if __name__ == "__main__":
57 parser = argparse.ArgumentParser(description="PNM Segmentation Verification")
58 parser.add_argument("input", help="Input SDF VTI file")
59 parser.add_argument("-o", "--output", default="segmentation.vti", help="Output Labels VTI")
60 parser.add_argument("-e", "--edges", default="network.edges", help="Output edge list")
61
62 args = parser.parse_args()
63
64 if os.path.exists(args.input):
65 verify_segmentation(args.input, args.output, args.edges)
66 else:
67 print(f"Error: {args.input} not found.")