-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeoHookean_elasticity_map.py
372 lines (303 loc) · 10.5 KB
/
NeoHookean_elasticity_map.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Backend supported: pytorch"""
import deepxde as dde
import numpy as np
from sys import exit
from deepxde.backend import torch
from deepxde.nn import activations
from deepxde.nn import initializers
from deepxde import config
import argparse
dde.config.disable_xla_jit()
dde.config.set_random_seed(9999)
dde.config.set_default_float("float32")
# torch.cuda.set_device(0)
class MPFNN(dde.nn.PFNN):
def __init__(self, layer_sizes, second_layer_sizes, activation, kernel_initializer):
super(MPFNN, self).__init__(layer_sizes, activation, kernel_initializer)
self.first_layer_sizes = layer_sizes
self.second_layer_sizes = second_layer_sizes
self.activation = activations.get(activation)
# Fully connected network
self.firstFNN = dde.nn.PFNN(
self.first_layer_sizes, self.activation, kernel_initializer
)
self.secondFNN = dde.nn.PFNN(
self.second_layer_sizes, self.activation, kernel_initializer
)
def forward(self, inputs):
x = inputs
if self._input_transform is not None:
x = self._input_transform(x)
x_firstFNN = self.firstFNN(x)
x_secondFNN = self.secondFNN(x)
x = torch.cat((x_firstFNN, x_secondFNN), dim=1)
if self._output_transform is not None:
x = self._output_transform(inputs, x)
return x
def main(refData, network):
data = np.load(refData, allow_pickle="TRUE")
nodal_coor, disp, strain = (
data.item()["nodal_coor"],
data.item()["nodal_disp"],
data.item()["nodal_strain"],
)
ux_mean, ux_std = np.mean(disp[:, 0]), np.std(disp[:, 0])
uy_mean, uy_std = np.mean(disp[:, 1]), np.std(disp[:, 1])
geom = dde.geometry.Rectangle([0, 0], [1, 1])
losses = [
dde.PointSetBC(nodal_coor[:, :2], strain[:, :3], component=[7, 8, 9]),
]
def pde(x, f):
Fxx, Fyy, Fxy, Fyx = f[:, 3:4], f[:, 4:5], f[:, 5:6], f[:, 6:7]
detF = Fxx * Fyy - Fxy * Fyx
invFxx = Fyy / detF
invFyy = Fxx / detF
invFxy = -Fxy / detF
invFyx = -Fyx / detF
E = f[:, 2:3]
nu = 0.3
lmbd = E * nu / ((1 + nu) * (1 - 2 * nu))
mu = E / (2 * (1 + nu))
# compressible 1st PK Stress(incompressible 1st PK stress: P = -pF^(-T)+muF)
# Derivation based on strain energy function from
# Javier Bonet, Richard D. Wood. Nonlinear continuum mechanics for
# finite element analysis. Cambridge University Press, 1997.
lnF = torch.log(detF)
Pxx = mu * Fxx + (lmbd * lnF - mu) * invFxx
Pxy = mu * Fxy + (lmbd * lnF - mu) * invFyx
Pyx = mu * Fyx + (lmbd * lnF - mu) * invFxy
Pyy = mu * Fyy + (lmbd * lnF - mu) * invFyy
NPxx, NPyy, NPxy, NPyx = f[:, 10:11], f[:, 11:12], f[:, 12:13], f[:, 13:14]
Sxx_x = dde.grad.jacobian(Pxx, x, i=0, j=0)
Syy_y = dde.grad.jacobian(Pyy, x, i=0, j=1)
Syx_y = dde.grad.jacobian(Pyx, x, i=0, j=1)
Sxy_x = dde.grad.jacobian(Pxy, x, i=0, j=0)
momentum_x = Sxx_x + Syx_y
momentum_y = Sxy_x + Syy_y
if "1" in network or "2" in network:
stress_xx = (Pxx - NPxx) / E
stress_yy = (Pyy - NPyy) / E
stress_xy = (Pxy - NPxy) / E
stress_yx = (Pyx - NPyx) / E
pde_losses = [
momentum_x,
momentum_y,
stress_xx,
stress_yy,
stress_xy,
stress_yx,
]
else:
pde_losses = [momentum_x, momentum_y]
return pde_losses
data = dde.data.PDE(geom, pde, losses, anchors=nodal_coor[:, :2])
def feature_transform(x):
y = x[:, 1:2]
x = x[:, 0:1]
pi = np.pi
return torch.concat(
(
x,
torch.sin(pi * x),
torch.sin(pi * 2 * x),
torch.sin(pi * 3 * x),
torch.sin(pi * 4 * x),
torch.sin(pi * 5 * x),
y,
torch.sin(pi * y),
torch.sin(pi * 2 * y),
torch.sin(pi * 3 * y),
torch.sin(pi * 4 * y),
torch.sin(pi * 5 * y),
),
axis=1,
)
def output_transform(x, y):
Nux, Nuy, NE = y[:, 0:1], y[:, 1:2], y[:, 2:3]
Pxx, Pyy, Pxy, Pyx = y[:, 3:4], y[:, 4:5], y[:, 5:6], y[:, 6:7]
Nux = Nux * ux_std + ux_mean
Nuy = Nuy * uy_std + uy_mean
if "A" in network or "C" in network:
# enforce displacement boundary conditions
Nux = x[:, 0:1] * Nux - 0.2
Nux = (1 - x[:, 0:1]) * Nux + x[:, 0:1] * 0.2
Nuy = x[:, 1:2] * Nuy - 0.2
Nuy = (1 - x[:, 1:2]) * Nuy + x[:, 1:2] * 0.2
NE = 1 + 4 * torch.sigmoid(NE)
duxdx = dde.grad.jacobian(Nux, x, i=0, j=0)
duydy = dde.grad.jacobian(Nuy, x, i=0, j=1)
duxdy = dde.grad.jacobian(Nux, x, i=0, j=1)
duydx = dde.grad.jacobian(Nuy, x, i=0, j=0)
Fxx = duxdx + 1.0
Fxy = duxdy
Fyx = duydx
Fyy = duydy + 1.0
Exx = 0.5 * (Fxx**2 + Fxy**2 - 1)
Eyy = 0.5 * (Fyx**2 + Fyy**2 - 1)
Exy = 0.5 * (Fxx * Fyx + Fxy * Fyy)
return torch.concat(
[Nux, Nuy, NE, Fxx, Fyy, Fxy, Fyx, Exx, Eyy, Exy, Pxx, Pyy, Pxy, Pyx],
axis=1,
)
# Standard PINN
if "A" in network or "B" in network:
num_input_var = 2
# Fourier-feature PINN
elif "C" in network or "D" in network:
num_input_var = 12
if "1" in network:
net = MPFNN(
[num_input_var, 50, 50, [25] * 2, [25] * 2, [25] * 2, 2],
[num_input_var] + [75] * 5 + [5],
"swish",
"Glorot normal",
)
elif "2" in network:
net = MPFNN(
[num_input_var] + [75] * 5 + [2],
[num_input_var] + [75] * 5 + [5],
"swish",
"Glorot normal",
)
elif "3" in network:
net = dde.nn.PFNN(
[num_input_var, 50, 50, [25] * 3, [25] * 3, [25] * 3, 3],
"swish",
"Glorot normal",
)
elif "4" in network:
net = dde.nn.PFNN(
[num_input_var, [25] * 3, [25] * 3, [25] * 3, [25] * 3, [25] * 3, 3],
"swish",
"Glorot normal",
)
elif "5" in network:
net = dde.nn.FNN([num_input_var] + [75] * 5 + [3], "swish", "Glorot normal")
if "C" in network or "D" in network:
net.apply_feature_transform(feature_transform)
net.apply_output_transform(output_transform)
model = dde.Model(data, net)
iteration = 500000
if "1" in network or "2" in network:
model.compile("adam", lr=1e-3, loss_weights=[1, 1, 1, 1, 1, 1, 1e2])
else:
model.compile("adam", lr=1e-3, loss_weights=[1, 1, 1e2])
losshistory, train_state = model.train(epochs=iteration)
dde.saveplot(losshistory, train_state, issave=True, isplot=False)
x = np.linspace(0, 1, 50)
y = np.linspace(0, 1, 50)
X, Y = np.meshgrid(x, y)
X_star = np.hstack((X.flatten()[:, None], Y.flatten()[:, None]))
output = model.predict(X_star)
data = {"Coor": X_star, "PINN output": output}
np.save("PINN_Prediction.npy", data)
def plot_data(refData):
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 50)
y = np.linspace(0, 1, 50)
X, Y = np.meshgrid(x, y)
ground_truth = np.load(refData, allow_pickle="TRUE")
gt_coor, gt_elasticity_map = (
ground_truth.item()["nodal_coor"],
ground_truth.item()["property_map"],
)
PINN = np.load("PINN_Prediction.npy", allow_pickle=True)
pinn_coor, pinn_elasticity_map = (
PINN.item()["Coor"],
PINN.item()["PINN output"][:, 2],
)
print(pinn_coor.shape, pinn_elasticity_map.shape)
gt_elasticity_map = griddata(
gt_coor[:, 0:2], gt_elasticity_map, (X, Y), method="cubic"
)
pinn_elasticity_map = griddata(
pinn_coor, pinn_elasticity_map.reshape(-1), (X, Y), method="cubic"
)
print(
"Young's modulus L2 relative error (%): ",
np.linalg.norm(gt_elasticity_map - pinn_elasticity_map)
/ np.linalg.norm(gt_elasticity_map)
* 100,
)
fig, axes = plt.subplots(1, 3, figsize=(10.5, 2.75))
ax = axes[0]
im0 = ax.imshow(
gt_elasticity_map,
interpolation="nearest",
extent=[0, 1, 0, 1],
origin="lower",
aspect="equal",
cmap="gray",
)
plt.colorbar(
im0,
ax=ax,
ticks=[
np.min(gt_elasticity_map),
(np.max(gt_elasticity_map) + np.min(gt_elasticity_map)) / 2,
np.max(gt_elasticity_map),
],
format="%.2f",
)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r"$E$")
ax = axes[1]
im1 = ax.imshow(
pinn_elasticity_map,
interpolation="nearest",
extent=[0, 1, 0, 1],
origin="lower",
aspect="equal",
cmap="gray",
)
plt.colorbar(
im1,
ax=ax,
ticks=[
np.min(pinn_elasticity_map),
(np.max(pinn_elasticity_map) + np.min(pinn_elasticity_map)) / 2,
np.max(pinn_elasticity_map),
],
format="%.2f",
)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r"$E^*$")
ax = axes[2]
abs_error = np.abs(gt_elasticity_map - pinn_elasticity_map)
max_error = np.max(abs_error)
min_error = np.min(abs_error)
im2 = ax.imshow(
abs_error,
interpolation="nearest",
extent=[0, 1, 0, 1],
origin="lower",
aspect="equal",
cmap="jet",
)
plt.colorbar(
im2,
ax=ax,
ticks=[min_error, (max_error + min_error) / 2, max_error],
format="%.1e",
)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r"|$E$ - $E^*$|")
plt.tight_layout()
plt.savefig("compare_E.pdf")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=str, default="../data/GRF_equi_disp0.4_neo.npy")
parser.add_argument("--network", type=str, default="2B")
args = parser.parse_args()
if int(args.network[0]) > 5 or args.network[1].capitalize() > "D":
print("Network architecture invalid.")
exit()
if "neo" not in args.data:
print("Reference data are not characterized by Neo-Hookean model.")
exit()
main(args.data, args.network)
plot_data(args.data)