|
|
"""
|
|
|
Utilities for loading WAKESET volume extractions (unstructured CFD data)
|
|
|
and interpolating them onto regular 3D grids.
|
|
|
"""
|
|
|
|
|
|
import pandas as pd
|
|
|
import numpy as np
|
|
|
from pathlib import Path
|
|
|
|
|
|
def process_fluent_export_sparse(
|
|
|
filepath: str,
|
|
|
grid_dim: int = 128,
|
|
|
precision_round: int = 3,
|
|
|
fill_value: float = 0.0
|
|
|
):
|
|
|
"""
|
|
|
Parses CFD exports where solid geometry cells are missing.
|
|
|
Maps physical coordinates to a fixed tensor grid.
|
|
|
"""
|
|
|
filepath = Path(filepath)
|
|
|
print(f"Processing: {filepath.name}...")
|
|
|
|
|
|
|
|
|
try:
|
|
|
df = pd.read_csv(
|
|
|
filepath, sep=',', skipinitialspace=True,
|
|
|
engine='c', on_bad_lines='warn'
|
|
|
)
|
|
|
except Exception as e:
|
|
|
print(f"Read failed: {e}")
|
|
|
return None
|
|
|
|
|
|
|
|
|
df.columns = [c.strip().lower().replace('-', '_') for c in df.columns]
|
|
|
|
|
|
|
|
|
|
|
|
x_raw = df['x_coordinate'].values.round(precision_round)
|
|
|
y_raw = df['y_coordinate'].values.round(precision_round)
|
|
|
z_raw = df['z_coordinate'].values.round(precision_round)
|
|
|
|
|
|
|
|
|
|
|
|
x_unique = np.unique(x_raw)
|
|
|
y_unique = np.unique(y_raw)
|
|
|
z_unique = np.unique(z_raw)
|
|
|
|
|
|
|
|
|
|
|
|
if len(x_unique) > grid_dim or len(y_unique) > grid_dim or len(z_unique) > grid_dim:
|
|
|
print(f"Error: Found too many grid ticks (X:{len(x_unique)}, Y:{len(y_unique)}, Z:{len(z_unique)}).")
|
|
|
print("Try reducing 'precision_round' (e.g., to 3) if floating point jitter is high.")
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
idx_x = np.searchsorted(x_unique, x_raw)
|
|
|
idx_y = np.searchsorted(y_unique, y_raw)
|
|
|
idx_z = np.searchsorted(z_unique, z_raw)
|
|
|
|
|
|
|
|
|
|
|
|
volume_shape = (grid_dim, grid_dim, grid_dim)
|
|
|
|
|
|
channels = [
|
|
|
'velocity_magnitude', 'x_velocity', 'y_velocity', 'z_velocity',
|
|
|
'total_pressure', 'absolute_pressure'
|
|
|
]
|
|
|
|
|
|
volume_data = {}
|
|
|
|
|
|
for col in channels:
|
|
|
if col in df.columns:
|
|
|
|
|
|
grid = np.full(volume_shape, fill_value, dtype=np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grid[idx_x, idx_y, idx_z] = df[col].values
|
|
|
|
|
|
volume_data[col] = grid
|
|
|
|
|
|
|
|
|
filled_count = np.count_nonzero(volume_data['velocity_magnitude'] != fill_value)
|
|
|
print(f" -> Grid filled. Solid cells (missing data): {grid_dim**3 - filled_count}")
|
|
|
|
|
|
return volume_data
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
fpath = "C:/Users/zacco/Desktop/Files/WAKESET/Test Files/Forward_0100_ms_Angle_00_CUBE_128"
|
|
|
|
|
|
|
|
|
p = Path(fpath)
|
|
|
if not p.exists() and p.with_suffix(".csv").exists(): p = p.with_suffix(".csv")
|
|
|
|
|
|
if p.exists():
|
|
|
|
|
|
|
|
|
volumes = process_fluent_export_sparse(p, fill_value=0.0)
|
|
|
|
|
|
if volumes:
|
|
|
print("Success.")
|
|
|
|
|
|
print(f"Shape: {volumes['velocity_magnitude'].shape}")
|
|
|
|
|
|
|
|
|
np.savez_compressed(p.name + ".npz", **volumes) |