-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
174 lines (136 loc) · 4.38 KB
/
main.py
File metadata and controls
174 lines (136 loc) · 4.38 KB
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
import time
import jax
import jax.numpy as jnp
import jax.random as jr
import numpy as np
import optax
import jaxopt
import equinox as eqx
from scipy.stats.qmc import Sobol
from src._geometry import HyperbolicParaboloid
from src._linear_elastic import LinearElastic
from src._linear_nagdhi import LinearNagdhi
from src.nn import MLP
seed = 1
rng = jr.PRNGKey(seed)
xi_col = Sobol(d=2).random(2**11) - 0.5
xi1 = jnp.asarray(xi_col[:, 0])
xi2 = jnp.asarray(xi_col[:, 1])
print(f"precision: {xi1.dtype}")
print("Pre-computing geometric quantities...")
tic = time.time()
geom = HyperbolicParaboloid(xi1, xi2)
shell_model = LinearNagdhi()
material_model = LinearElastic(1.0, 0.3)
C = jax.vmap(material_model._C)(geom.con_I) # precomputable
D = jax.vmap(material_model._D)(geom.con_I) # precomputable
toc = time.time()
print(f"Done! Elapsed time: {toc - tic:.2f}s")
thickness = 0.1
shear_factor = 5.0 / 6.0
def fully_clamped(xi1, xi2):
return (xi1**2 - 0.25) * (xi2**2 - 0.25)
rng, init_key = jr.split(rng)
pinn = MLP(50, 4, geom.T_u, for_bc=fully_clamped, key=init_key)
####################
# Energy calculation
####################
def surface_integral(
_integrand, _jacobian=geom.sqrt_det_a, _factor=geom.parametric_area
):
"""
Utility function for (Monte-Carlo) integration over a surface
"""
value = (_integrand * _jacobian).mean() * _factor
return value
def loss(pinn):
uhat, u, u_d, theta, theta_d = jax.vmap(pinn._u_and_theta_d)(xi1, xi2)
membrane_strain = shell_model._membrane_strain(
u, u_d, geom.cov_II, geom.christoffel_symbol
)
bending_strain = shell_model._bending_strain(
u, u_d, theta, theta_d, geom.mix_II, geom.cov_III, geom.christoffel_symbol
)
shear_strain = shell_model._shear_strain(u, u_d, theta, geom.mix_II)
# external energy
_external_energy = -1 * thickness * uhat[:, 2] # gravity
external_energy = surface_integral(_external_energy) # correct
# membrane energy
_membrane_energy = jnp.einsum(
"...ab,...abcd,...cd", membrane_strain, C, membrane_strain
)
membrane_energy = 0.5 * thickness * surface_integral(_membrane_energy)
# bending energy
_bending_energy = jnp.einsum(
"...ab,...abcd,...cd", bending_strain, C, bending_strain
)
bending_energy = 0.5 * (thickness**3 / 12) * surface_integral(_bending_energy)
# shear energy
_shear_energy = jnp.einsum("...a,...ab,...b", shear_strain, D, shear_strain)
shear_energy = 0.5 * thickness * shear_factor * surface_integral(_shear_energy)
inner_energy = membrane_energy + bending_energy + shear_energy
loss = inner_energy - external_energy
return loss, (
inner_energy,
external_energy,
membrane_energy / inner_energy,
bending_energy / inner_energy,
shear_energy / inner_energy,
)
data = np.genfromtxt(
"fem_sol/fenics_pred_hyperb_parab_fully_clamped.csv", delimiter=",", skip_header=1
)
xi1_ = data[:, 0]
xi2_ = data[:, 1]
ux = data[:, 2]
uy = data[:, 3]
uz = data[:, 4]
theta1 = data[:, 5]
theta2 = data[:, 6]
niter_adam = 10**3
opt = jaxopt.OptaxSolver(
loss,
optax.adam(optax.cosine_decay_schedule(1e-3, niter_adam)),
maxiter=niter_adam,
has_aux=True,
)
print("adam stage...")
tic = time.time()
adam_pinn, state = opt.run(pinn)
toc = time.time()
i, e, m, b, s = state.aux
print(
f"""Done! Elapsed time: {toc - tic:.2f}s
i: {i:.3e}, e: {e:.3e}, m: {m:.2f}, b: {b:.2f}, s: {s:.2f}"""
)
# save model parameters
eqx.tree_serialise_leaves("params/adam.eqx", adam_pinn)
opt = jaxopt.LBFGS(fun=loss, has_aux=True)
print("LBFGS running...")
state = opt.init_state(pinn)
@jax.jit
def step(pinn, state):
pinn, state = opt.update(pinn, state)
return pinn, state
tic = time.time()
min_loss = np.Inf
for it in range(1, 2000 + 1):
pinn, state = step(pinn, state)
if it % 100 == 0:
energy = state.value
if np.isnan(energy).sum() > 0:
print("NaN!")
break
if energy < min_loss:
min_loss = energy
i, e, m, b, s = state.aux
print(
f"it: {it}, i: {i:.3e}, e: {e:.3e}, m: {m:.2f}, b: {b:.2f}, s: {s:.2f}"
)
toc = time.time()
print(f"Done! Elapsed time: {toc - tic:.2f}s")
i, e, m, b, s = state.aux
print(
f"inner: {i:.3e}, external: {e:.3e}, memb: {m:.2f}, bend: {b:.2f}, shear: {s:.2f}"
)
eqx.tree_serialise_leaves("params/lbfgs.eqx", pinn)