-
Notifications
You must be signed in to change notification settings - Fork 5
/
soft_constraints.py
190 lines (149 loc) · 5.57 KB
/
soft_constraints.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
import deepxde as dde
import numpy as np
from deepxde.backend import tf
import pandas as pd
dde.config.set_default_float("float64")
dde.config.disable_xla_jit()
def gen_data(num):
data = pd.read_csv("FEA/linearElasticDisp_fea_m1.csv")
X = data["x"].values.flatten()[:, None]
Y = data["y"].values.flatten()[:, None]
ux = data["ux"].values.flatten()[:, None]
uy = data["uy"].values.flatten()[:, None]
data = pd.read_csv("FEA/linearElasticCauchyStress_fea_m1.csv")
sxx = data["sxx"].values.flatten()[:, None]
syy = data["syy"].values.flatten()[:, None]
sxy = data["sxy"].values.flatten()[:, None]
syx = data["sxy"].values.flatten()[:, None]
X_star = np.hstack((X.flatten()[:, None], Y.flatten()[:, None]))
ux = ux.flatten()[:, None]
uy = uy.flatten()[:, None]
sxx = sxx.flatten()[:, None]
syy = syy.flatten()[:, None]
sxy = sxy.flatten()[:, None]
num = int(num / 2)
samplingRegion1 = X_star[:, 0] <= 1
idx1 = np.where(samplingRegion1)[0]
# print(idx1.shape[0])
if idx1.shape[0] < num:
idx1 = idx1
num = int(num * 2 - idx1.shape[0])
else:
idx1 = np.random.choice(np.where(samplingRegion1)[0], num, replace=False)
samplingRegion2 = X_star[:, 0] > 1
idx2 = np.random.choice(np.where(samplingRegion2)[0], num, replace=False)
nb = 10
b1 = X_star[:, 0] == 10
idx3 = np.random.choice(np.where(b1)[0], nb, replace=False)
nb = 90
b2 = X_star[:, 1] == 0
idx4 = np.random.choice(np.where(b2)[0], nb, replace=False)
b3 = X_star[:, 1] == 1
idx5 = np.random.choice(np.where(b3)[0], nb, replace=False)
XY_star = np.vstack(
(X_star[idx1], X_star[idx2], X_star[idx3], X_star[idx4], X_star[idx5])
)
ux_star = np.vstack((ux[idx1], ux[idx2], ux[idx3], ux[idx4], ux[idx5]))
uy_star = np.vstack((uy[idx1], uy[idx2], uy[idx3], uy[idx4], uy[idx5]))
sxx_star = np.vstack((sxx[idx1], sxx[idx2], sxx[idx3], sxx[idx4], sxx[idx5]))
syy_star = np.vstack((syy[idx1], syy[idx2], syy[idx3], syy[idx4], syy[idx5]))
sxy_star = np.vstack((sxy[idx1], sxy[idx2], sxy[idx3], sxy[idx4], sxy[idx5]))
return XY_star, ux_star, uy_star, sxx_star, syy_star, sxy_star
def main():
E_ = dde.Variable(1.0)
nu_ = dde.Variable(1.0)
rho_g = 1
observe_xy, ux, uy, sxx, syy, sxy = gen_data(250)
def pde(x, f):
"""
x: Network input
x[:,0] is the x-coordinate
x[:,1] is the y-coordinate
f: Network output
f[:,0] is Nux
f[:,1] is Nuy
f[:,2] is Nsxx
f[:,3] is Nsyy
f[:,4] is Nsxy
"""
Nux, Nuy = f[:, 0:1], f[:, 1:2]
Exx = dde.grad.jacobian(Nux, x, i=0, j=0)
Eyy = dde.grad.jacobian(Nuy, x, i=0, j=1)
Exy = 0.5 * (
dde.grad.jacobian(Nux, x, i=0, j=1) + dde.grad.jacobian(Nuy, x, i=0, j=0)
)
E = (tf.tanh(E_) + 1.0) * 3e5
nu = (tf.tanh(nu_) + 1.0) / 4
E = tf.cast(E, tf.float64)
nu = tf.cast(nu, tf.float64)
Sxx = E / (1 - nu ** 2) * (Exx + nu * Eyy)
Syy = E / (1 - nu ** 2) * (Eyy + nu * Exx)
Sxy = E / (1 + nu) * Exy
Sxx_x = dde.grad.jacobian(f, x, i=2, j=0)
Syy_y = dde.grad.jacobian(f, x, i=3, j=1)
Sxy_x = dde.grad.jacobian(f, x, i=4, j=0)
Syx_y = dde.grad.jacobian(f, x, i=4, j=1)
Fx = 0
Fy = -rho_g
momentum_x = Sxx_x + Syx_y - Fx
momentum_y = Sxy_x + Syy_y - Fy
stress_x = Sxx - f[:, 2:3]
stress_y = Syy - f[:, 3:4]
stress_xy = Sxy - f[:, 4:5]
return [momentum_x, momentum_y, stress_x, stress_y, stress_xy]
geom = dde.geometry.Rectangle([0, 0], [10, 1])
def left_boundary(x, on_boundary):
return on_boundary and np.isclose(x[0], 0)
bc_l1 = dde.DirichletBC(geom, lambda x: 0, left_boundary, component=0)
bc_l2 = dde.DirichletBC(geom, lambda x: 0, left_boundary, component=1)
observe_ux = dde.PointSetBC(observe_xy, ux, component=0)
observe_uy = dde.PointSetBC(observe_xy, uy, component=1)
observe_sxx = dde.PointSetBC(observe_xy, sxx, component=2)
observe_syy = dde.PointSetBC(observe_xy, syy, component=3)
observe_sxy = dde.PointSetBC(observe_xy, sxy, component=4)
data = dde.data.PDE(
geom,
pde,
[bc_l1, bc_l2, observe_ux, observe_uy, observe_sxx, observe_syy, observe_sxy],
num_domain=100,
num_boundary=50,
num_test=100,
)
net = dde.nn.PFNN(
[2, [15, 15, 15, 15, 15], [15, 15, 15, 15, 15], [15, 15, 15, 15, 15], 5],
"tanh",
"Glorot normal",
)
def output_transform(x, f):
Nux, Nuy, Nsxx, Nsyy, Nsxy = (
f[:, 0:1],
f[:, 1:2],
f[:, 2:3],
f[:, 3:4],
f[:, 4:5],
)
return tf.concat(
[
Nux * np.max(np.abs(ux)),
Nuy * np.max(np.abs(uy)),
Nsxx * np.max(np.abs(sxx)),
Nsyy * np.max(np.abs(syy)),
Nsxy * np.max(np.abs(sxy)),
],
axis=1,
)
net.apply_output_transform(output_transform)
model = dde.Model(data, net)
model.compile(
"adam",
lr=1e-3,
loss_weights=[1e-10, 1e-10, 1e-10, 1e-10, 1e-10, 1, 1, 1, 1, 1, 1, 1],
)
variable = dde.callbacks.VariableValue(
[E_, nu_], period=1000, filename="2D_elastic_static_variables.dat"
)
losshistory, train_state = model.train(epochs=1000000, callbacks=[variable])
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
return
if __name__ == "__main__":
main()