| 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 |
| |
| |
| arr = vals.reshape(ny, nx) |
|
|
| if case == "rising_bubble": |
| region = arr < 0.5 |
| label = "bubble" |
| elif case == "droplet_impact": |
| region = arr > 0.5 |
| label = "droplet" |
| else: |
| region = arr > 0.5 |
| 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 |
| |
| cx_phys = (x_idx.mean() + 0.5) * dx |
| cy_phys = (y_idx.mean() + 0.5) * dy |
| r_phys = r_eq * dx |
| 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()}]") |
| |
| 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") |
|
|