-
Notifications
You must be signed in to change notification settings - Fork 15
/
brinkman_forchheimer_1.py
172 lines (128 loc) · 4.68 KB
/
brinkman_forchheimer_1.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import re
import deepxde as dde
from deepxde.backend import tf
g = 1
v = 1e-3
K = 1e-3
e = 0.4
H = 1
#PINN
def sol(x):
r = (v*e/(1e-3*K))**(0.5)
return g*K/v * (1- np.cosh(r*(x-H/2))/np.cosh(r*H/2))
def gen_traindata(num):
xvals = np.linspace(1/(num+1), 1, num, endpoint = False)
yvals = sol(xvals)
return np.reshape(xvals, (-1,1)), np.reshape(yvals, (-1,1))
def output_transform(x, y):
return tf.math.tanh(x)*tf.math.tanh(1-x) * y
geom = dde.geometry.Interval(0, 1)
ob_x, ob_u = gen_traindata(5)
observe_u = dde.PointSetBC(ob_x, ob_u, component = 0)
v_e = tf.math.softplus(tf.Variable(0, trainable=True, dtype=tf.float32))*0.1
def PINNpde(x,y):
u = y
du_xx = dde.grad.hessian(y, x)
return -v_e/e * du_xx + v*u/K - g
data = dde.data.PDE(
geom,
PINNpde,
solution = sol,
bcs = [observe_u],
num_domain = 10,
num_boundary = 0,
train_distribution = "uniform",
num_test = 1000,
)
net = dde.maps.FNN([1] + [20] * 3 + [1], "tanh", "Glorot uniform")
net.apply_output_transform(output_transform)
PINNmodel = dde.Model(data, net)
PINNmodel.compile("adam", lr = 0.001, metrics = ["l2 relative error"])
variable = dde.callbacks.VariableValue(
[v_e], period=200, filename="variables1.dat"
)
losshistory, train_state = PINNmodel.train(epochs = 50000, callbacks = [variable])
dde.saveplot(losshistory, train_state, issave = True, isplot = False)
#gPINN
g = 1
v = 1e-3
K = 1e-3
e = 0.4
H = 1
def sol(x):
r = (v*e/(1e-3*K))**(0.5)
return g*K/v * (1- np.cosh(r*(x-H/2))/np.cosh(r*H/2))
def gen_traindata(num):
xvals = np.linspace(1/(num+1), 1, num, endpoint = False)
yvals = sol(xvals)
return np.reshape(xvals, (-1,1)), np.reshape(yvals, (-1,1))
def output_transform(x, y):
return tf.math.tanh(x)*tf.math.tanh(1-x) * y
geom = dde.geometry.Interval(0, 1)
bc = dde.DirichletBC(geom, sol, lambda x, on_boundary: on_boundary)
ob_x, ob_u = gen_traindata(5)
observe_u = dde.PointSetBC(ob_x, ob_u, component = 0)
v_e = tf.math.softplus(tf.Variable(0, trainable=True, dtype=tf.float32))*0.1
def gPINNpde(x,y):
u = y
du_x = dde.grad.jacobian(y, x)
du_xx = dde.grad.hessian(y, x)
du_xxx = dde.grad.jacobian(du_xx, x)
return [-v_e/e * du_xx + v*u/K - g, -v_e/e * du_xxx + v/K * du_x]
data = dde.data.PDE(
geom,
gPINNpde,
solution = sol,
bcs = [observe_u],
num_domain = 10,
num_boundary = 0,
train_distribution = "uniform",
num_test = 1000,
)
net = dde.maps.FNN([1] + [20] * 3 + [1], "tanh", "Glorot uniform")
net.apply_output_transform(output_transform)
gPINNmodel = dde.Model(data, net)
gPINNmodel.compile("adam", lr = 0.001, metrics = ["l2 relative error"], loss_weights = [1, 0.1, 1])
variable = dde.callbacks.VariableValue(
[v_e], period=200, filename="variables2.dat"
)
losshistory, train_state = gPINNmodel.train(epochs = 50000, callbacks = [variable])
dde.saveplot(losshistory, train_state, issave = True, isplot = False)
#plots
plt.rcParams.update({'font.size': 16})
x = geom.uniform_points(1000)
yhat1 = PINNmodel.predict(x)
uhat1, v_ehat1 = yhat1[:, 0:1], yhat1[:, 0:1]
yhat2 = gPINNmodel.predict(x)
uhat2, v_ehat1 = yhat2[:, 0:1], yhat2[:, 0:1]
plt.figure()
plt.plot(x, sol(x), label = "Exact", color = "black")
plt.plot(x, uhat1, label = "PINN", linestyle = "dashed", color = 'blue')
plt.plot(x, uhat2, label = "gPINN", linestyle = "dashed", color = 'red')
x = geom.uniform_points(5, boundary = False)
plt.plot(x, sol(x), color = "black", marker = "s", linestyle = "none", label = "Observed")
plt.legend(frameon=False)
plt.xlabel("x")
plt.ylabel("u")
import re
lines = open("variables1.dat", "r").readlines()
v_ehat1 = np.array([np.fromstring(min(re.findall(re.escape('[')+"(.*?)"+re.escape(']'),line), key=len), sep=',') for line in lines])
lines = open("variables2.dat", "r").readlines()
v_ehat2 = np.array([np.fromstring(min(re.findall(re.escape('[')+"(.*?)"+re.escape(']'),line), key=len), sep=',') for line in lines])
l, c= v_ehat1.shape
v_etrue = 1e-3
plt.figure()
plt.plot(range(0, 200*l, 200), np.ones(v_ehat1[:,0].shape)*v_etrue, color = "black", label = "Exact")
plt.plot(range(0, 200*l, 200), v_ehat1[:,0],'b--', label = "PINN")
plt.plot(range(0, 200*l, 200), v_ehat2[:,0],'r--', label = "gPINN")
plt.xlabel('Epoch')
plt.yscale("log")
plt.ylim(top = 1e-1)
plt.legend(frameon=False)
plt.ylabel(r'$\nu_e$')
plt.show()