-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavierStokes_3D.py
More file actions
268 lines (211 loc) · 9.45 KB
/
NavierStokes_3D.py
File metadata and controls
268 lines (211 loc) · 9.45 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import torch.nn.functional as F
from utilities import *
from timeit import default_timer
torch.manual_seed(0)
np.random.seed(0)
class NeuralConv3d(nn.Module):
def __init__(self, in_channels, out_channels, dims, modes1, modes2, modes3):
super(NeuralConv3d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.dims = dims
self.modes1 = modes1
self.modes2 = modes2
self.modes3 = modes3
self.scale = (1 / (in_channels * out_channels))
self.weights1 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3))
self.weights2 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3))
self.weights3 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3))
self.weights4 = nn.Parameter(self.scale * torch.rand(in_channels, out_channels, self.modes1, self.modes2, self.modes3))
self.M1 = nn.Linear(self.dims[0], self.modes1)
self.M2 = nn.Linear(self.dims[1], self.modes2)
self.M3 = nn.Linear(self.dims[2]+6, self.modes3) # 6 refers to padding
self.N1 = nn.Linear(self.modes1, self.dims[0])
self.N2 = nn.Linear(self.modes2, self.dims[1])
self.N3 = nn.Linear(self.modes3, self.dims[2]+6)
def R(self, input, weights):
# (batch, in_channel, x,y,t ), (in_channel, out_channel, x,y,t) -> (batch, out_channel, x,y,t)
return torch.einsum("bixyz,ioxyz->boxyz", input, weights)
def forward(self, x):
batchsize = x.shape[0]
x_ft = self.M1(x.permute(0,1,3,4,2)).permute(0,1,4,2,3)
x_ft = self.M2(x_ft.permute(0,1,2,4,3)).permute(0,1,2,4,3)
x_ft = self.M3(x_ft)
out_ft = self.compl_mul3d(x_ft, self.weights1)
out_ft = self.compl_mul3d(out_ft, self.weights2)
out_ft = self.compl_mul3d(out_ft, self.weights3)
out_ft = self.compl_mul3d(out_ft, self.weights4)
x = self.N1(out_ft.permute(0,1,3,4,2)).permute(0,1,4,2,3)
x = self.N2(x.permute(0,1,2,4,3)).permute(0,1,2,4,3)
x = self.N3(x)
return x
class MLP(nn.Module):
def __init__(self, in_channels, out_channels, mid_channels):
super(MLP, self).__init__()
self.mlp1 = nn.Conv3d(in_channels, mid_channels, 1)
self.mlp2 = nn.Conv3d(mid_channels, out_channels, 1)
def forward(self, x):
x = self.mlp1(x)
x = F.gelu(x)
x = self.mlp2(x)
return x
class NO3d(nn.Module):
def __init__(self, modes1, modes2, modes3, dims, width):
super(NO3d, self).__init__()
self.modes1 = modes1
self.modes2 = modes2
self.modes3 = modes3
self.dims = dims
self.width = width
self.padding = 6 # pad the domain if input is non-periodic
self.p = nn.Linear(13, self.width) # input channel is 12: the solution of the first 10 timesteps + 3 locations (u(1, x, y), ..., u(10, x, y), x, y, t)
self.conv0 = NeuralConv3d(self.width, self.width, self.dims, self.modes1, self.modes2, self.modes3)
self.conv1 = NeuralConv3d(self.width, self.width, self.dims, self.modes1, self.modes2, self.modes3)
self.conv2 = NeuralConv3d(self.width, self.width, self.dims, self.modes1, self.modes2, self.modes3)
self.conv3 = NeuralConv3d(self.width, self.width, self.dims, self.modes1, self.modes2, self.modes3)
self.mlp0 = MLP(self.width, self.width, self.width)
self.mlp1 = MLP(self.width, self.width, self.width)
self.mlp2 = MLP(self.width, self.width, self.width)
self.mlp3 = MLP(self.width, self.width, self.width)
self.w0 = nn.Conv3d(self.width, self.width, 1)
self.w1 = nn.Conv3d(self.width, self.width, 1)
self.w2 = nn.Conv3d(self.width, self.width, 1)
self.w3 = nn.Conv3d(self.width, self.width, 1)
self.q = MLP(self.width, 1, self.width * 4) # output channel is 1: u(x, y)
def forward(self, x):
grid = self.get_grid(x.shape, x.device)
x = torch.cat((x, grid), dim=-1)
x = self.p(x)
x = x.permute(0, 4, 1, 2, 3)
x = F.pad(x, [0,self.padding]) # pad the domain if input is non-periodic
# x = F.avg_pool3d(x, kernel_size=(4,4,4)) # Downsampling from 256,256,80 -> 64,64,20
x1 = self.conv0(x)
x1 = self.mlp0(x1)
x2 = self.w0(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv1(x)
x1 = self.mlp1(x1)
x2 = self.w1(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv2(x)
x1 = self.mlp2(x1)
x2 = self.w2(x)
x = x1 + x2
x = F.gelu(x)
x1 = self.conv3(x)
x1 = self.mlp3(x1)
x2 = self.w3(x)
x = x1 + x2
# x = F.interpolate(x, scale_factor=(4,4,4), mode='trilinear') # Upsampling to 64,64,20 -> 256,256,80
x = x[..., :-self.padding]
x = self.q(x)
x = x.permute(0, 2, 3, 4, 1) # pad the domain if input is non-periodic
return x
def get_grid(self, shape, device):
batchsize, size_x, size_y, size_z = shape[0], shape[1], shape[2], shape[3]
gridx = torch.tensor(np.linspace(0, 1, size_x), dtype=torch.float)
gridx = gridx.reshape(1, size_x, 1, 1, 1).repeat([batchsize, 1, size_y, size_z, 1])
gridy = torch.tensor(np.linspace(0, 1, size_y), dtype=torch.float)
gridy = gridy.reshape(1, 1, size_y, 1, 1).repeat([batchsize, size_x, 1, size_z, 1])
gridz = torch.tensor(np.linspace(0, 1, size_z), dtype=torch.float)
gridz = gridz.reshape(1, 1, 1, size_z, 1).repeat([batchsize, size_x, size_y, 1, 1])
return torch.cat((gridx, gridy, gridz), dim=-1).to(device)
################################################################
# configs
################################################################
TRAIN_PATH = 'data/ns_data_V100_N1000_T50_1.mat'
TEST_PATH = 'data/ns_data_V100_N1000_T50_2.mat'
ntrain = 1000
ntest = 200
modes = 8
width = 32
batch_size = 10
learning_rate = 0.001
epochs = 500
iterations = epochs*(ntrain//batch_size)
t1 = default_timer()
sub = 1
S = 64 // sub
T_in = 10
T = 10
################################################################
# load data
################################################################
reader = MatReader(TRAIN_PATH)
train_a = reader.read_field('u')[:ntrain,::sub,::sub,:T_in]
train_u = reader.read_field('u')[:ntrain,::sub,::sub,T_in:T+T_in]
reader = MatReader(TEST_PATH)
test_a = reader.read_field('u')[-ntest:,::sub,::sub,:T_in]
test_u = reader.read_field('u')[-ntest:,::sub,::sub,T_in:T+T_in]
print(train_u.shape)
print(test_u.shape)
assert (S == train_u.shape[-2])
assert (T == train_u.shape[-1])
a_normalizer = UnitGaussianNormalizer(train_a)
train_a = a_normalizer.encode(train_a)
test_a = a_normalizer.encode(test_a)
y_normalizer = UnitGaussianNormalizer(train_u)
train_u = y_normalizer.encode(train_u)
train_a = train_a.reshape(ntrain,S,S,1,T_in).repeat([1,1,1,T,1])
test_a = test_a.reshape(ntest,S,S,1,T_in).repeat([1,1,1,T,1])
train_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(train_a, train_u), batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(test_a, test_u), batch_size=batch_size, shuffle=False)
t2 = default_timer()
print('preprocessing finished, time used:', t2-t1)
device = torch.device('cuda')
################################################################
# training and evaluation
################################################################
model = FNO3d(modes, modes, modes, [S,S,T], width).cuda()
print(count_params(model))
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones = [100,200,300,400,500], gamma=0.1)
myloss = LpLoss(size_average=False)
y_normalizer.cuda()
for ep in range(epochs):
model.train()
t1 = default_timer()
train_mse = 0
train_l2 = 0
for x, y in train_loader:
x, y = x.cuda(), y.cuda()
optimizer.zero_grad()
out = model(x).view(batch_size, S, S, T)
mse = F.mse_loss(out, y, reduction='mean')
# mse.backward()
y = y_normalizer.decode(y)
out = y_normalizer.decode(out)
l2 = myloss(out.view(batch_size, -1), y.view(batch_size, -1))
l2.backward()
optimizer.step()
scheduler.step()
train_mse += mse.item()
train_l2 += l2.item()
model.eval()
test_l2 = 0.0
with torch.no_grad():
for x, y in test_loader:
x, y = x.cuda(), y.cuda()
out = model(x).view(batch_size, S, S, T)
out = y_normalizer.decode(out)
test_l2 += myloss(out.view(batch_size, -1), y.view(batch_size, -1)).item()
train_mse /= len(train_loader)
train_l2 /= ntrain
test_l2 /= ntest
t2 = default_timer()
print(ep, t2-t1, train_mse, train_l2, test_l2)
pred = torch.zeros(test_u.shape)
index = 0
test_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(test_a, test_u), batch_size=1, shuffle=False)
with torch.no_grad():
for x, y in test_loader:
test_l2 = 0
x, y = x.cuda(), y.cuda()
out = model(x)
out = y_normalizer.decode(out)
pred[index] = out
test_l2 += myloss(out.view(1, -1), y.view(1, -1)).item()
print(index, test_l2)
index = index + 1