Skip to content

Commit db4b005

Browse files
committed
added anchors
1 parent 9111056 commit db4b005

5 files changed

Lines changed: 22 additions & 2 deletions

File tree

6_inv_free/NeoHookeanEnergy.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ANCHOR: helper_func
12
import utils
23
import numpy as np
34
import math
@@ -78,7 +79,9 @@ def d2Psi_div_dF2(F, mu, lam):
7879
+ M[3, 0] * U[i, 1] * VT[1, j] * U[r, 0] * VT[0, s] \
7980
+ M[3, 3] * U[i, 1] * VT[1, j] * U[r, 1] * VT[1, s]
8081
return dP_div_dF
82+
# ANCHOR_END: helper_func
8183

84+
# ANCHOR: stress_deriv
8285
def deformation_grad(x, elemVInd, IB):
8386
F = [x[elemVInd[1]] - x[elemVInd[0]], x[elemVInd[2]] - x[elemVInd[0]]]
8487
return np.transpose(F).dot(IB)
@@ -124,7 +127,9 @@ def d2Psi_div_dx2(dP_div_dF, IB): # applying chain-rule, d2Psi_div_dx2 = dF_div
124127
result[0, colI] = -_000 - _101 - _010 - _111
125128
result[1, colI] = -_200 - _301 - _210 - _311
126129
return result
130+
# ANCHOR_END: stress_deriv
127131

132+
# ANCHOR: val_grad_hess
128133
def val(x, e, vol, IB, mu, lam):
129134
sum = 0.0
130135
for i in range(0, len(e)):
@@ -157,7 +162,9 @@ def hess(x, e, vol, IB, mu, lam):
157162
IJV[1][ind] = e[i][xJ] * 2 + dJ
158163
IJV[2][ind] = local_hess[xI * 2 + dI, xJ * 2 + dJ]
159164
return IJV
165+
# ANCHOR_END: val_grad_hess
160166

167+
# ANCHOR: filter_line_search
161168
def init_step_size(x, e, p):
162169
alpha = 1
163170
for i in range(0, len(e)):
@@ -172,4 +179,5 @@ def init_step_size(x, e, p):
172179
critical_alpha = utils.smallest_positive_real_root_quad(a, b, c)
173180
if critical_alpha > 0:
174181
alpha = min(alpha, critical_alpha)
175-
return alpha
182+
return alpha
183+
# ANCHOR_END: filter_line_search

6_inv_free/simulator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
# simulation setup
1111
side_len = 1
1212
rho = 1000 # density of square
13+
# ANCHOR: lame_param
1314
E = 1e5 # Young's modulus
1415
nu = 0.4 # Poisson's ratio
16+
# ANCHOR_END: lame_param
1517
n_seg = 4 # num of segments per side of the square
1618
h = 0.01 # time step size in s
1719
DBC = [(n_seg + 1) * (n_seg + 1)] # dirichlet node index
@@ -27,6 +29,7 @@
2729
x = np.append(x, [[0.0, side_len * 0.6]], axis=0) # ceil origin (with normal [0.0, -1.0])
2830
v = np.array([[0.0, 0.0]] * len(x)) # velocity
2931
m = [rho * side_len * side_len / ((n_seg + 1) * (n_seg + 1))] * len(x) # calculate node mass evenly
32+
# ANCHOR: elem_precomp
3033
# rest shape basis, volume, and lame parameters
3134
vol = [0.0] * len(e)
3235
IB = [np.array([[0.0, 0.0]] * 2)] * len(e)
@@ -36,6 +39,7 @@
3639
IB[i] = np.linalg.inv(np.transpose(TB))
3740
mu_lame = [0.5 * E / (1 + nu)] * len(e)
3841
lam = [E * nu / ((1 + nu) * (1 - 2 * nu))] * len(e)
42+
# ANCHOR_END: elem_precomp
3943
# identify whether a node is Dirichlet
4044
is_DBC = [False] * len(x)
4145
for i in DBC:
@@ -68,9 +72,11 @@ def screen_projection(x):
6872
pygame.draw.aaline(screen, (0, 0, 255), screen_projection([x[-1][0] + 3.0, x[-1][1]]),
6973
screen_projection([x[-1][0] - 3.0, x[-1][1]])) # ceil
7074
for eI in e:
75+
# ANCHOR: draw_tri
7176
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[0]]), screen_projection(x[eI[1]]))
7277
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[1]]), screen_projection(x[eI[2]]))
7378
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[2]]), screen_projection(x[eI[0]]))
79+
# ANCHOR_END: draw_tri
7480
for xId in range(0, len(x) - 1):
7581
xI = x[xId]
7682
pygame.draw.circle(screen, (0, 0, 255), screen_projection(xI), 0.1 * side_len / n_seg * scale)

6_inv_free/square_mesh.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def generate(side_length, n_seg):
99
for j in range(0, n_seg + 1):
1010
x[i * (n_seg + 1) + j] = [-side_length / 2 + i * step, -side_length / 2 + j * step]
1111

12+
# ANCHOR: tri_vert_ind
1213
# connect the nodes with triangle elements
1314
e = []
1415
for i in range(0, n_seg):
@@ -20,6 +21,7 @@ def generate(side_length, n_seg):
2021
else:
2122
e.append([i * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j + 1])
2223
e.append([i * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j + 1, i * (n_seg + 1) + j + 1])
24+
# ANCHOR_END: tri_vert_ind
2325

2426
return [x, e]
2527

6_inv_free/time_integrator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def step_forward(x, e, v, m, vol, IB, mu_lame, lam, n, o, contact_area, mu, is_D
3939
E_last = IP_val(x, e, x_tilde, m, vol, IB, mu_lame, lam, n, o, contact_area, (x - x_n) / h, mu_lambda, DBC, DBC_target, DBC_stiff, h)
4040

4141
# filter line search
42+
# ANCHOR: apply_filter
4243
alpha = min(BarrierEnergy.init_step_size(x, n, o, p), NeoHookeanEnergy.init_step_size(x, e, p)) # avoid interpenetration, tunneling, and inversion
44+
# ANCHOR_END: apply_filter
4345
while IP_val(x + alpha * p, e, x_tilde, m, vol, IB, mu_lame, lam, n, o, contact_area, (x + alpha * p - x_n) / h, mu_lambda, DBC, DBC_target, DBC_stiff, h) > E_last:
4446
alpha /= 2
4547
print('step size =', alpha)

6_inv_free/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def make_PSD(hess):
99
lam[i] = max(0, lam[i])
1010
return np.matmul(np.matmul(V, np.diag(lam)), np.transpose(V))
1111

12+
# ANCHOR: find_positive_real_root
1213
def smallest_positive_real_root_quad(a, b, c, tol = 1e-6):
1314
# return negative value if no positive real root is found
1415
t = 0
@@ -25,4 +26,5 @@ def smallest_positive_real_root_quad(a, b, c, tol = 1e-6):
2526
t = (-b + math.sqrt(desc)) / (2 * a)
2627
else: # desv<0 ==> imag, f(x) > 0 for all x > 0
2728
t = -1
28-
return t
29+
return t
30+
# ANCHOR_END: find_positive_real_root

0 commit comments

Comments
 (0)