AlanZee's picture
Upload folder using huggingface_hub
ed552fd verified
Raw
History Blame Contribute Delete
2.25 kB
import re, numpy as np
def read_foam_scalar(path):
with open(path) as f:
content = f.read()
m = re.search(r'List<scalar>\s+(\d+)', content)
if not m: return None, 0
n = int(m.group(1))
start = content.find('(', m.end()) + 1
end = content.find(')', start)
vals = np.array([float(x) for x in content[start:end].split()])
return vals, n
cases = [
("rising_bubble", "t00", 128, 256, 1.0, 2.0),
("droplet_impact", "t00", 256, 256, 0.025, 0.025),
("dam_break", "t00", 128, 128, 0.584, 0.584),
]
for case, t, nx, ny, Lx, Ly in cases:
path = f"/home/alanz/openfoam-workspace/output/data/{case}/{t}/0/alpha.water"
vals, n = read_foam_scalar(path)
if vals is None:
print(f"{case}: FAILED to read")
continue
# OpenFOAM x-fastest: idx = i + j*nx, so reshape to (ny, nx)
# np.where returns (row_idx, col_idx) = (y_idx, x_idx)
arr = vals.reshape(ny, nx)
if case == "rising_bubble":
region = arr < 0.5 # bubble is alpha=0
label = "bubble"
elif case == "droplet_impact":
region = arr > 0.5 # droplet is alpha=1
label = "droplet"
else:
region = arr > 0.5 # water column is alpha=1
label = "water"
if region.sum() > 0:
y_idx, x_idx = np.where(region)
r_eq = np.sqrt(region.sum() / np.pi)
dx = Lx / nx
dy = Ly / ny
# Cell center coordinates (0.5 offset for center of cell)
cx_phys = (x_idx.mean() + 0.5) * dx
cy_phys = (y_idx.mean() + 0.5) * dy
r_phys = r_eq * dx # isotropic: dx == dy for these cases
print(f"{case}: {label}")
print(f" cells={region.sum()}, r_eq={r_eq:.1f} cells ({r_phys*1000:.2f}mm)")
print(f" center=({cx_phys*1000:.2f}mm, {cy_phys*1000:.2f}mm)")
print(f" domain={Lx*1000:.1f}x{Ly*1000:.1f}mm, dx=dy={dx*1000:.4f}mm")
print(f" x_cells=[{x_idx.min()},{x_idx.max()}], y_cells=[{y_idx.min()},{y_idx.max()}]")
# Shape aspect ratio
span_x = (x_idx.max() - x_idx.min() + 1)
span_y = (y_idx.max() - y_idx.min() + 1)
print(f" span=({span_x},{span_y}) cells, aspect={span_x/span_y:.2f}")
else:
print(f"{case}: NO {label} region found")