Spaces:
Sleeping
Sleeping
File size: 8,014 Bytes
67fb03c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import os
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
import pyvista as pv
import json
import glob
class Data_loader(Dataset):
def __init__(self, cfg, split, epoch_seed=None, mode='train'):
"""
data_dir: parent directory
split: list of int, e.g. [0,1,2,3,4] for train, [5] for val, [6] for test
num_points: number of points to sample per geometry
epoch_seed: seed for random sampling (for training)
mode: 'train', 'val', or 'test'
"""
self.data_dir = cfg.data_dir
self.split = split
self.num_points = cfg.num_points
self.epoch_seed = epoch_seed
self.mode = mode
self.cfg = cfg
self.meshes = []
self.mesh_names = []
for idx in split:
# Find folder matching *_{idx}
folder = os.path.join(self.data_dir, f"{idx}")
if not os.path.exists(folder):
raise FileNotFoundError(f"No folder matching '{idx}' found in {self.data_dir}")
# Find file matching *_{idx}.vtp inside the folder
vtp_files = glob.glob(os.path.join(folder, f"{idx}.vtp"))
if not vtp_files:
raise FileNotFoundError(f"No file matching '{idx}.vtp' found in {folder}")
vtp_file = vtp_files[0]
mesh = pv.read(vtp_file)
self.meshes.append(mesh)
self.mesh_names.append(os.path.splitext(os.path.basename(vtp_file))[0])
# For validation chunking
self.val_indices = None
self.val_chunk_ptr = 0
with open(cfg.json_file, "r") as f:
self.json_data = json.load(f)
def set_epoch(self, epoch):
self.epoch_seed = epoch
self.val_indices = None
self.val_chunk_ptr = 0
def __len__(self):
if self.mode == 'train':
return len(self.meshes)
elif self.mode == 'val':
return len(self.meshes)
elif self.mode == 'test':
# Number of chunks = total points in all val meshes // num_points + remainder chunk
total = 0
for mesh in self.meshes:
return len(self.meshes)
else:
raise ValueError(f"Unknown mode: {self.mode}")
def __getitem__(self, idx):
if self.mode == 'train' or self.mode == 'val':
# Each item is a geometry, sample num_points randomly
mesh = self.meshes[idx]
n_pts = mesh.points.shape[0]
rng = np.random.default_rng(self.epoch_seed+idx)
indices = rng.choice(n_pts, self.num_points, replace=False)
pos = mesh.points
pos = torch.tensor(pos, dtype=torch.float32)
pressure = torch.tensor( mesh["pressure"][indices], dtype=torch.float32).unsqueeze(-1)
if self.cfg.normalization == "std_norm":
target = (pressure - self.json_data["scalars"]["pressure"]["mean"]) / self.json_data["scalars"]["pressure"]["std"]
if self.cfg.diff_input_velocity:
inlet_x_vel = torch.tensor( mesh["inlet_x_velocity"], dtype=torch.float32).unsqueeze(-1)
pos = torch.cat((pos,inlet_x_vel),dim = 1)
if self.cfg.input_normalization == "shift_axis":
coords = pos[:,:3].clone()
# Shift x: set minimum x (front bumper) to 0
coords[:, 0] = coords[:, 0] - coords[:, 0].min()
# Shift z: set minimum z (ground) to 0
coords[:, 2] = coords[:, 2] - coords[:, 2].min()
# Shift y: center about 0 (left/right symmetry)
y_center = (coords[:, 1].max() + coords[:, 1].min()) / 2.0
coords[:, 1] = coords[:, 1] - y_center
pos[:,:3] = coords
if self.cfg.pos_embed_sincos:
if self.cfg.diff_input_velocity:
raise Exception("pos_embed_sincos not supported with diff_input_velocity=True")
input_pos_mins = torch.tensor(self.json_data["mesh_stats"]["min"])
input_pos_maxs = torch.tensor(self.json_data["mesh_stats"]["max"])
pos = 1000*(pos - input_pos_mins) / (input_pos_maxs - input_pos_mins)
assert torch.all(pos >= 0)
assert torch.all(pos <= 1000)
pos = pos[indices]
return {"input_pos": pos, "output_feat": target ,"data_id": self.mesh_names[idx]}
elif self.mode == 'test':
# For each mesh in test, scramble all points and return the full mesh
mesh = self.meshes[idx]
n_pts = mesh.points.shape[0]
rng = np.random.default_rng(self.epoch_seed+idx)
indices = rng.permutation(n_pts)
pos = mesh.points
pos = torch.tensor(pos, dtype=torch.float32)
pressure = torch.tensor( mesh["pressure"][indices], dtype=torch.float32).unsqueeze(-1)
if self.cfg.normalization == "std_norm":
target = (pressure - self.json_data["scalars"]["pressure"]["mean"]) / self.json_data["scalars"]["pressure"]["std"]
if hasattr(self.cfg, "diff_input_velocity") and self.cfg.diff_input_velocity:
inlet_x_vel = torch.tensor( mesh["inlet_x_velocity"], dtype=torch.float32).unsqueeze(-1)
pos = torch.cat((pos,inlet_x_vel),dim = 1)
if self.cfg.input_normalization == "shift_axis":
coords = pos[:,:3].clone()
# Shift x: set minimum x (front bumper) to 0
coords[:, 0] = coords[:, 0] - coords[:, 0].min()
# Shift z: set minimum z (ground) to 0
coords[:, 2] = coords[:, 2] - coords[:, 2].min()
# Shift y: center about 0 (left/right symmetry)
y_center = (coords[:, 1].max() + coords[:, 1].min()) / 2.0
coords[:, 1] = coords[:, 1] - y_center
pos[:,:3] = coords
if self.cfg.pos_embed_sincos:
if hasattr(self.cfg, "diff_input_velocity") and self.cfg.diff_input_velocity:
raise Exception("pos_embed_sincos not supported with diff_input_velocity=True")
input_pos_mins = torch.tensor(self.json_data["mesh_stats"]["min"])
input_pos_maxs = torch.tensor(self.json_data["mesh_stats"]["max"])
pos = 1000*(pos - input_pos_mins) / (input_pos_maxs - input_pos_mins)
assert torch.all(pos >= 0)
assert torch.all(pos <= 1000)
pos = pos[indices]
return {"input_pos": pos, "output_feat": target ,"data_id": self.mesh_names[idx],"physical_coordinates":mesh.points[indices]}
else:
raise ValueError(f"Unknown mode: {self.mode}")
def get_dataloaders(cfg):
with open(os.path.join(cfg.splits_file, "train.txt")) as f:
train_split = [line.strip() for line in f if line.strip()]
with open(os.path.join(cfg.splits_file, "test.txt")) as f:
val_split = [line.strip() for line in f if line.strip()]
with open(os.path.join(cfg.splits_file, "test.txt")) as f:
test_split = [line.strip() for line in f if line.strip()]
print("Indices in test_split:", test_split[:5]) # Print first 5 indices for verification
train_dataset = Data_loader(cfg, train_split, mode='train')
val_dataset = Data_loader(cfg, val_split, mode='val')
test_dataset = Data_loader(cfg, test_split, mode='test')
train_loader = DataLoader(train_dataset, batch_size=1, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=1, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)
return train_loader, val_loader, test_loader
|