forked from maziarraissi/PINNs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_PINN_pytorch.py
161 lines (133 loc) · 4.81 KB
/
01_PINN_pytorch.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
import sys
from sklearn.model_selection import train_test_split
sys.path.append(".")
import numpy as np
import torch
from torch.autograd import grad
from network import DNN
from scipy.io import loadmat
import pandas as pd
import torch
import torch.nn as nn
import numpy as np
import os
current_path = os.getcwd()
torch.backends.cuda.matmul.allow_tf32 = (
False # This is for Nvidia Ampere GPU Architechture
)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
torch.manual_seed(1234)
np.random.seed(1234)
# current_path = os.path.dirname(os.path.abspath(__file__))
# print(current_path)
"""
Burgers Eqn.
f = u_t + lambda_1 * u * u_x - lambda_2 * u_xx = 0, x ~ [-1, 1], t ~ [0, 1]
lambda_1 = 1
lambda_2 = 0.01/pi = 0.0031831
f = w*u_t+w*u_x+
"""
N_u = 2000
data = loadmat(r"D:\02_github\PINNs-torch\Burgers\burgers_shock.mat")
x = data["x"]
t = data["t"]
u = data["usol"].T
ub = np.array([x.max(), t.max()]) ###上下限制
lb = np.array([x.min(), t.min()])
# Clean Data Preparation
x_, t_ = np.meshgrid(x, t)
x_ = x_.reshape(-1, 1)
t_ = t_.reshape(-1, 1)
u_ = u.reshape(-1, 1)
rand_idx = np.random.choice(len(u_), N_u, replace=False)
x = torch.tensor(x_[rand_idx], dtype=torch.float32).to(device)
t = torch.tensor(t_[rand_idx], dtype=torch.float32).to(device)
xt = torch.cat((x, t), dim=1)
u = torch.tensor(u_[rand_idx], dtype=torch.float32).to(device)
# 1% Noisy Data Preparation
noise = 0.01
noisy_u = u_ + noise * np.std(u_) * np.random.randn(*u_.shape)
noisy_u = torch.tensor(noisy_u[rand_idx], dtype=torch.float32).to(device)
print(u_.shape,u.shape)
class PINN:
def __init__(self, u):
self.u = u
self.lambda_1 = torch.tensor([0.0], requires_grad=True).to(device)
self.lambda_2 = torch.tensor([-6.0], requires_grad=True).to(device)
self.lambda_1 = torch.nn.Parameter(self.lambda_1)
self.lambda_2 = torch.nn.Parameter(self.lambda_2)
self.net = DNN(dim_in=2, dim_out=1, n_layer=7, n_node=20, ub=ub, lb=lb,).to(
device
)
self.net.register_parameter("lambda_1", self.lambda_1)
self.net.register_parameter("lambda_2", self.lambda_2)
self.optimizer = torch.optim.LBFGS(
self.net.parameters(),
lr=1.0,
max_iter=50000,
max_eval=50000,
history_size=50,
tolerance_grad=1e-5,
tolerance_change=1.0 * np.finfo(float).eps,
line_search_fn="strong_wolfe",
)
self.iter = 0
def f(self, xt):
# lambda_1 = self.lambda_1
# lambda_2 = torch.exp(self.lambda_2)
xt = xt.clone()
xt.requires_grad = True
u = self.net(xt)
f = grad(u.sum(), xt, create_graph=True)[0]
# u_x = u_xt[:, 0:1]
# u_t = u_xt[:, 1:2]
#
# u_xx = grad(u_x.sum(), xt, create_graph=True)[0][:, 0:1]
#
# f = u_t + lambda_1 * u * u_x - lambda_2 * u_xx
# Apply output bounds
f = torch.clamp(f, 0, 365)
return f
def closure(self):
self.optimizer.zero_grad()
u_pred = self.net(xt)
f_pred = self.f(xt)
mse_u = torch.mean(torch.square(u_pred - self.u))
mse_f = torch.mean(torch.square(f_pred))
loss = mse_u + mse_f
loss.backward()
self.iter += 1
print(
f"\r{self.iter} loss : {loss.item():.3e} l1 : {self.lambda_1.item():.5f}, l2 : {torch.exp(self.lambda_2).item():.5f}",
end="",
)
if self.iter % 500 == 0:
print("")
return loss
def calcError(pinn):
u_pred = pinn.net(torch.hstack((x, t)))
u_pred = u_pred.detach().cpu().numpy()
u_ = u.detach().cpu().numpy()
error_u = np.linalg.norm(u_ - u_pred, 2) / np.linalg.norm(u_, 2)
lambda1 = pinn.lambda_1.detach().cpu().item()
lambda2 = np.exp(pinn.lambda_2.detach().cpu().item())
error_lambda1 = np.abs(lambda1 - 1.0) * 100
error_lambda2 = np.abs(lambda2 - 0.01 / np.pi) * 100
print(
f"\nError u : {error_u:.5e}",
f"\nError l1 : {error_lambda1:.5f}%",
f"\nError l2 : {error_lambda2:.5f}%",
)
return (error_u, error_lambda1, error_lambda2)
if __name__ == "__main__":
pinn = PINN(u)
pinn.optimizer.step(pinn.closure)
torch.save(pinn.net.state_dict(), r"D:\02_github\PINNs-torch\Burgers\Identification\weight_clean3.pt")
pinn.net.load_state_dict(torch.load(r"D:\02_github\PINNs-torch\Burgers\Identification\weight_clean3.pt"))
calcError(pinn)
#
# pinn = PINN(noisy_u)
# pinn.optimizer.step(pinn.closure)
# torch.save(pinn.net.state_dict(), r"D:\02_github\PINNs-torch\Burgers\Identification\weight_noisy2.pt")
# calcError(pinn)
#