forked from neuraloperator/physics_informed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
229 lines (183 loc) · 6.48 KB
/
data_utils.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import scipy.io
import numpy as np
from scipy.interpolate import griddata
try:
from pyDOE import lhs
# Only needed for PINN's dataset
except ImportError:
lhs = None
import torch
from torch.utils.data import Dataset
class MatReader(object):
def __init__(self, file_path, to_torch=True, to_cuda=False, to_float=True):
super(MatReader, self).__init__()
self.to_torch = to_torch
self.to_cuda = to_cuda
self.to_float = to_float
self.file_path = file_path
self.data = None
self.old_mat = None
self._load_file()
def _load_file(self):
try:
self.data = scipy.io.loadmat(self.file_path)
self.old_mat = True
except:
self.data = h5py.File(self.file_path)
self.old_mat = False
def load_file(self, file_path):
self.file_path = file_path
self._load_file()
def read_field(self, field):
x = self.data[field]
if not self.old_mat:
x = x[()]
x = np.transpose(x, axes=range(len(x.shape) - 1, -1, -1))
if self.to_float:
x = x.astype(np.float32)
if self.to_torch:
x = torch.from_numpy(x)
if self.to_cuda:
x = x.cuda()
return x
def set_cuda(self, to_cuda):
self.to_cuda = to_cuda
def set_torch(self, to_torch):
self.to_torch = to_torch
def set_float(self, to_float):
self.to_float = to_float
class DataConstructor(object):
def __init__(self, datapath, nx=2**10, nt=100, sub=8, sub_t=1, new=False):
dataloader = MatReader(datapath)
self.sub = sub
self.sub_t = sub_t
self.s = nx // sub
self.T = nt // sub_t
self.new = new
self.x_data = dataloader.read_field('input')[:, ::sub]
self.y_data = dataloader.read_field('output')[:, 1::sub_t, ::sub]
def make_loader(self, n_sample, batch_size, train=True):
if train:
Xs = self.x_data[:n_sample]
ys = self.y_data[:n_sample]
else:
Xs = self.x_data[-n_sample:]
ys = self.y_data[-n_sample:]
if self.new:
gridx = torch.tensor(np.linspace(0, 1, self.s + 1)[:-1], dtype=torch.float)
gridt = torch.tensor(np.linspace(0, 1, self.T + 1)[1:], dtype=torch.float)
else:
gridx = torch.tensor(np.linspace(0, 1, self.s), dtype=torch.float)
gridt = torch.tensor(np.linspace(0, 1, self.T + 1)[1:], dtype=torch.float)
gridx = gridx.reshape(1, 1, self.s)
gridt = gridt.reshape(1, self.T, 1)
Xs = Xs.reshape(n_sample, 1, self.s).repeat([1, self.T, 1])
Xs = torch.stack([Xs, gridx.repeat([n_sample, self.T, 1]), gridt.repeat([n_sample, 1, self.s])], dim=3)
dataset = torch.utils.data.TensorDataset(Xs, ys)
if train:
loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
else:
loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False)
return loader
def load_data(datapath, N_f=10000):
'''
Parameters:
- datapath: datapath of (x,t), u(x,t) data
- N_f: number of (x, t)
Return:
- X_u: (N, 2) ndarray
- u: (N , 1) ndarray
- X_f: (N_f + N, 2) ndarray
'''
data = scipy.io.loadmat(datapath)
t = data['t'].flatten()[:, None] # (100,1)
x = data['x'].flatten()[:, None] # (256, 1)
Exact = np.real(data['usol']).T # (100, 256)
# print(x.shape)
# print(t.shape)
# print(Exact.shape)
X, T = np.meshgrid(x, t)
X_star = np.hstack((X.flatten()[:, None], T.flatten()[:, None]))
u_star = Exact.flatten()[:, None]
# Domain bounds
lb = X_star.min(0)
ub = X_star.max(0)
xx1 = np.hstack((X[0:1, :].T, T[0:1, :].T))
uu1 = Exact[0:1, :].T
xx2 = np.hstack((X[:, 0:1], T[:, 0:1]))
uu2 = Exact[:, 0:1]
xx3 = np.hstack((X[:, -1:], T[:, -1:]))
uu3 = Exact[:, -1:]
X_u = np.vstack([xx1, xx2, xx3])
X_f = lb + (ub-lb)*lhs(2, N_f)
X_f = np.vstack((X_f, X_u))
u = np.vstack([uu1, uu2, uu3])
return X_u, u, X_f, X_star, u_star
def sample(X_u, u, N=100):
'''
Randomly sample N pairs
'''
idx = np.random.choice(X_u.shape[0], N, replace=False)
X_u = X_u[idx, :]
u = u[idx, :]
return X_u, u
class BurgerData(Dataset):
'''
members:
- t, x, Exact: raw data
- X, T: meshgrid
- X_star, u_star: flattened (x, t), u array
- lb, ub: lower bound and upper bound vector
- X_u, u: boundary condition data (x, t), u
'''
def __init__(self, datapath):
data = scipy.io.loadmat(datapath)
# raw 2D data
self.t = data['t'].flatten()[:, None] # (100,1)
self.x = data['x'].flatten()[:, None] # (256, 1)
self.Exact = np.real(data['usol']).T # (100, 256)
# Flattened sequence
self.get_flatten_data()
self.get_boundary_data()
def __len__(self):
return self.Exact.shape[0]
def __getitem__(self, idx):
return self.X_star[idx], self.u_star[idx]
def get_flatten_data(self):
X, T = np.meshgrid(self.x, self.t)
self.X, self.T = X, T
self.X_star = np.hstack((X.flatten()[:, None], T.flatten()[:, None]))
self.u_star = self.Exact.flatten()[:, None]
# lower bound of (x, t): 2-dimensional vector
self.lb = self.X_star.min(0)
# upper bound of (x, t): 2-dimensional vector
self.ub = self.X_star.max(0)
def get_boundary_data(self):
xx1 = np.hstack((self.X[0:1, :].T, self.T[0:1, :].T))
uu1 = self.Exact[0:1, :].T
xx2 = np.hstack((self.X[:, 0:1], self.T[:, 0:1]))
uu2 = self.Exact[:, 0:1]
xx3 = np.hstack((self.X[:, -1:], self.T[:, -1:]))
uu3 = self.Exact[:, -1:]
self.X_u = np.vstack([xx1, xx2, xx3])
self.u = np.vstack([uu1, uu2, uu3])
def sample_xt(self, N=10000):
'''
Sample (x, t) pairs within the boundary
Return:
- X_f: (N, 2) array
'''
X_f = self.lb + (self.ub-self.lb)*lhs(2, N)
X_f = np.vstack((X_f, self.X_u))
return X_f
def sample_xu(self, N=100):
'''
Sample N points from boundary data
Return:
- X_u: (N, 2) array
- u: (N, 1) array
'''
idx = np.random.choice(self.X_u.shape[0], N, replace=False)
X_u = self.X_u[idx, :]
u = self.u[idx, :]
return X_u, u