-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathns_uno3d_main.py
123 lines (108 loc) · 3.23 KB
/
ns_uno3d_main.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
# Codes for section: Results on Navier Stocks Equation (3D)
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from data_load_navier_stocks import *
import matplotlib.pyplot as plt
from navier_stokes_uno3d import Uno3D_T40, Uno3D_T20, Uno3D_T10, Uno3D_T9
import operator
import random
from functools import reduce
from functools import partial
from ns_train_3d import train_model_3d
from timeit import default_timer
from utilities3 import *
from Adam import Adam
from torchsummary import summary
# for conda: from torchinfo import summary
import gc
import math
torch.cuda.set_device(2)
random.seed(10102)
# Hyper parameters
# weight deacy = 1e-5
# learning rate 0.003
S = 64 # resolution SxS
T_in = 10 # input time interval (0 - T_in)
T_f = 10 # output time interval (T_in - = T_in+T_f)
ntrain = 9000 # number of training instances
ntest = 1000 # number of test instances
nval = 1000 # number of validation instances
batch_size = 16
width = 8 # Uplifting dimesion
inwidth = 6 # dimension of UNO input ( a(x,y,t), + positional embedding )
epochs = 500
# Following code load data from two separate files containing Navier-Stokes equation simulation
train_a_1, train_u_1, test_a_1, test_u_1 = load_NS_(
"path to navier stokes simulation with viscosity 1e-5 with 1000 instances",
4000,
2000,
Sample_num=6000,
T_in=T_in,
T=T_f,
size=S,
)
train_a_2, train_u_2, test_a_2, test_u_2 = load_NS_(
"path to navier stokes simulation with viscosity 1e-5 with 5000 instances",
4000,
1000,
Sample_num=5000,
T_in=T_in,
T=T_f,
size=S,
)
a = torch.cat([train_a_1, train_a_2, test_a_1, test_a_2], dim=0)
u = torch.cat([train_u_1, train_u_2, test_u_1, test_u_2], dim=0)
indexs = [i for i in range(a.shape[0])]
random.shuffle(indexs)
train_a, val_a, test_a = (
a[indexs[:ntrain]],
a[indexs[ntrain : ntrain + nval]],
a[indexs[ntrain + nval :]],
)
train_u, val_u, test_u = (
u[indexs[:ntrain]],
u[indexs[ntrain : ntrain + nval]],
u[indexs[ntrain + nval :]],
)
train_a = train_a.reshape(ntrain, S, S, T_in)
val_a = val_a.reshape(nval, S, S, T_in)
test_a = test_a.reshape(ntest, S, S, T_in)
train_a = train_a.reshape(ntrain, S, S, T_in, 1)
val_a = val_a.reshape(nval, S, S, T_in, 1)
test_a = test_a.reshape(ntest, S, S, T_in, 1)
gc.collect()
train_loader = torch.utils.data.DataLoader(
torch.utils.data.TensorDataset(train_a, train_u),
batch_size=batch_size,
shuffle=True,
)
val_loader = torch.utils.data.DataLoader(
torch.utils.data.TensorDataset(val_a, val_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
)
# any 3d models can be trained vai train_model_3d function
# model = Uno3D_T10(inwidth,width,pad = 3,factor = 1).cuda()
model = Uno3D_T40(inwidth, width, pad=3, factor=1).cuda()
summary(model, (64, 64, 6, 1))
train_model_3d(
model,
train_loader,
val_loader,
test_loader,
ntrain,
nval,
ntest,
weight_path="UNO3D.pt",
T_f=T_f,
batch_size=batch_size,
epochs=epochs,
learning_rate=0.003,
scheduler_step=100,
scheduler_gamma=0.5,
weight_decay=1e-5,
)