FSI-pde-dataset / fsi_reader.py
ashiq24's picture
code for dataloading and visualization
e75efae
raw
history blame
5.2 kB
import os
import h5py
import numpy as np
import torch
import h5py
class FsiDataReader():
def __init__(self,
location,
mu=None,
in_lets_x1=None,
in_lets_x2=None,):
self.location = location
self._x1 = ['-4.0', '-2.0', '0.0', '2.0', '4.0', '6.0']
self._x2 = ['-4.0', '-2.0', '0', '2.0', '4.0', '6.0']
self._mu = ['0.1', '0.01', '0.5', '5', '1.0', '10.0']
# keeping vx,xy, P, dx,dy
self.varable_idices = [0, 1, 3, 4, 5]
if mu is not None:
# check if mu is _mu else raise error
assert set(mu).issubset(set(self._mu))
self._mu = mu
if in_lets_x1 is not None:
# check if in_lets_x1 is _x1 else raise error
assert set(in_lets_x1).issubset(set(self._x1))
self._x1 = in_lets_x1
if in_lets_x2 is not None:
# check if in_lets_x2 is _x2 else raise error
assert set(in_lets_x2).issubset(set(self._x2))
self._x2 = in_lets_x2
mesh_h = h5py.File(os.path.join(location, 'mesh.h5'), 'r')
mesh = mesh_h['mesh/coordinates'][:]
self.input_mesh = torch.from_numpy(mesh).type(torch.float)
def _readh5(self, h5f, dtype=torch.float32):
a_dset_keys = list(h5f['VisualisationVector'].keys())
size = len(a_dset_keys)
readings = [None for i in range(size)]
for dset in a_dset_keys:
ds_data = (h5f['VisualisationVector'][dset])
if ds_data.dtype == 'float64':
csvfmt = '%.18e'
elif ds_data.dtype == 'int64':
csvfmt = '%.10d'
else:
csvfmt = '%s'
readings[int(dset)] = torch.tensor(np.array(ds_data), dtype=dtype)
readings_tensor = torch.stack(readings, dim=0)
print(f"Loaded tensor Size: {readings_tensor.shape}")
return readings_tensor
def get_data(self, mu, x1, x2):
if mu not in self._mu:
raise ValueError(f"Value of mu must be one of {self._mu}")
if x1 not in self._x1 or x2 not in self._x2:
raise ValueError(
f"Value of is must be one of {self._ivals3} and {self._ivals12} ")
path = os.path.join(
self.location,
'mu='+str(mu),
'x1='+str(x1),
'x2='+str(x2),
'Visualization')
filename = os.path.join(path, 'displacement.h5')
h5f = h5py.File(filename, 'r')
displacements_tensor = self._readh5(h5f)
filename = os.path.join(path, 'pressure.h5')
h5f = h5py.File(filename, 'r')
pressure_tensor = self._readh5(h5f)
filename = os.path.join(path, 'velocity.h5')
h5f = h5py.File(filename, 'r')
velocity_tensor = self._readh5(h5f)
combined = torch.cat([velocity_tensor, pressure_tensor, displacements_tensor], dim=-1)[..., self.varable_idices]
# return velocity_tensor, pressure_tensor, displacements_tensor
return combined
def get_data_txt(self, mu, x1, x2):
if mu not in self._mu:
raise ValueError(f"Value of mu must be one of {self._mu}")
if x1 not in self._x1 or x2 not in self._x2:
raise ValueError(
f"Value of is must be one of {self._ivals3} and {self._ivals12} ")
path = os.path.join(
self.params.super_res_data_location,
'mu='+str(mu),
'x1='+str(x1),
'x2='+str(x2),
'1')
#try:
dis_x = torch.tensor(np.loadtxt(os.path.join(path, 'dis_x.txt')))
dis_y = torch.tensor(np.loadtxt(os.path.join(path, 'dis_y.txt')))
pressure = torch.tensor(np.loadtxt(os.path.join(path, 'pres.txt')))
velocity_x = torch.tensor(np.loadtxt(os.path.join(path, 'vel_x.txt')))
velocity_y = torch.tensor(np.loadtxt(os.path.join(path, 'vel_y.txt')))
# reshape each tensor into 2d by keeping 876 entries in each row
dis_x = dis_x.view(-1, 876,1)
dis_y = dis_y.view(-1, 876,1)
pressure = pressure.view(-1, 876,1)
velocity_x = velocity_x.view(-1, 876,1)
velocity_y = velocity_y.view(-1, 876,1)
combined = torch.cat([velocity_x, velocity_y, pressure, dis_x, dis_y], dim=-1)[..., ]
return combined
def get_loader(self, batch_size, shuffle=True):
data = []
for mu in self._mu:
for x1 in self._x1:
for x2 in self._x2:
try:
if mu == 0.5:
data.append(self.get_data_txt(mu, x1, x2))
else:
data.append(self.get_data(mu, x1, x2))
except FileNotFoundError as e:
print(
f"file not found for mu={mu}, x1={x1}, x2={x2}")
continue
data = torch.cat(data, dim=0)
print(f"Data shape: {data.shape}")
data_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=shuffle)
return data_loader