Skip to content

Commit 9111056

Browse files
committed
added anchors
1 parent 814874e commit 9111056

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

5_mov_dirichlet/BarrierEnergy.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ def val(x, n, o, contact_area):
1212
if d < dhat:
1313
s = d / dhat
1414
sum += contact_area[i] * dhat * kappa / 2 * (s - 1) * math.log(s)
15-
# ceil:
15+
# ANCHOR: ceiling_val
1616
n = np.array([0.0, -1.0])
1717
for i in range(0, len(x) - 1):
1818
d = n.dot(x[i] - x[-1])
1919
if d < dhat:
2020
s = d / dhat
2121
sum += contact_area[i] * dhat * kappa / 2 * (s - 1) * math.log(s)
22+
# ANCHOR_END: ceiling_val
2223
return sum
2324

2425
def grad(x, n, o, contact_area):
@@ -29,7 +30,7 @@ def grad(x, n, o, contact_area):
2930
if d < dhat:
3031
s = d / dhat
3132
g[i] = contact_area[i] * dhat * (kappa / 2 * (math.log(s) / dhat + (s - 1) / d)) * n
32-
# ceil:
33+
# ANCHOR: ceiling_grad
3334
n = np.array([0.0, -1.0])
3435
for i in range(0, len(x) - 1):
3536
d = n.dot(x[i] - x[-1])
@@ -38,6 +39,7 @@ def grad(x, n, o, contact_area):
3839
local_grad = contact_area[i] * dhat * (kappa / 2 * (math.log(s) / dhat + (s - 1) / d)) * n
3940
g[i] += local_grad
4041
g[-1] -= local_grad
42+
# ANCHOR_END: ceiling_grad
4143
return g
4244

4345
def hess(x, n, o, contact_area):
@@ -52,7 +54,7 @@ def hess(x, n, o, contact_area):
5254
IJV[0].append(i * 2 + r)
5355
IJV[1].append(i * 2 + c)
5456
IJV[2] = np.append(IJV[2], local_hess[r, c])
55-
# ceil:
57+
# ANCHOR: ceiling_hess
5658
n = np.array([0.0, -1.0])
5759
for i in range(0, len(x) - 1):
5860
d = n.dot(x[i] - x[-1])
@@ -66,6 +68,7 @@ def hess(x, n, o, contact_area):
6668
IJV[0].append(index[nI] * 2 + r)
6769
IJV[1].append(index[nJ] * 2 + c)
6870
IJV[2] = np.append(IJV[2], ((-1) ** (nI != nJ)) * local_hess[r, c])
71+
# ANCHOR_END: ceiling_hess
6972
return IJV
7073

7174
def init_step_size(x, n, o, p):
@@ -75,12 +78,13 @@ def init_step_size(x, n, o, p):
7578
p_n = p[i].dot(n)
7679
if p_n < 0:
7780
alpha = min(alpha, 0.9 * n.dot(x[i] - o) / -p_n)
78-
# ceil:
81+
# ANCHOR: ceiling_ccd
7982
n = np.array([0.0, -1.0])
8083
for i in range(0, len(x) - 1):
8184
p_n = (p[i] - p[-1]).dot(n)
8285
if p_n < 0:
8386
alpha = min(alpha, 0.9 * n.dot(x[i] - x[-1]) / -p_n)
87+
# ANCHOR_END: ceiling_ccd
8488
return alpha
8589

8690
def compute_mu_lambda(x, n, o, contact_area, mu):

5_mov_dirichlet/simulator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
k = 2e4 # spring stiffness
1414
n_seg = 4 # num of segments per side of the square
1515
h = 0.01 # time step size in s
16+
# ANCHOR: ceiling_dbc_setup
1617
DBC = [(n_seg + 1) * (n_seg + 1)] # dirichlet node index
1718
DBC_v = [np.array([0.0, -0.5])] # dirichlet node velocity
1819
DBC_limit = [np.array([0.0, -0.6])] # dirichlet node limit position
20+
# ANCHOR_END: ceiling_dbc_setup
1921
ground_n = np.array([0.0, 1.0]) # normal of the slope
2022
ground_n /= np.linalg.norm(ground_n) # normalize ground normal vector just in case
2123
ground_o = np.array([0.0, -1.0]) # a point on the slope
2224
mu = 0.11 # friction coefficient of the slope
2325

2426
# initialize simulation
27+
# ANCHOR: ceiling_dof
2528
[x, e] = square_mesh.generate(side_len, n_seg) # node positions and edge node indices
2629
x = np.append(x, [[0.0, side_len * 0.6]], axis=0) # ceil origin (with normal [0.0, -1.0])
30+
# ANCHOR_END: ceiling_dof
2731
v = np.array([[0.0, 0.0]] * len(x)) # velocity
2832
m = [rho * side_len * side_len / ((n_seg + 1) * (n_seg + 1))] * len(x) # calculate node mass evenly
2933
# rest length squared

5_mov_dirichlet/time_integrator.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ def step_forward(x, e, v, m, l2, k, n, o, contact_area, mu, is_DBC, DBC, DBC_v,
1717
x_tilde = x + v * h # implicit Euler predictive position
1818
x_n = copy.deepcopy(x)
1919
mu_lambda = BarrierEnergy.compute_mu_lambda(x, n, o, contact_area, mu) # compute mu * lambda for each node using x^n
20+
# ANCHOR: dbc_initialization
2021
DBC_target = [] # target position of each DBC in the current time step
2122
for i in range(0, len(DBC)):
2223
if (DBC_limit[i] - x_n[DBC[i]]).dot(DBC_v[i]) > 0:
2324
DBC_target.append(x_n[DBC[i]] + h * DBC_v[i])
2425
else:
2526
DBC_target.append(x_n[DBC[i]])
2627
DBC_stiff = 10 # initialize stiffness for DBC springs
28+
# ANCHOR_END: dbc_initialization
2729

2830
# Newton loop
2931
iter = 0
3032
E_last = IP_val(x, e, x_tilde, m, l2, k, n, o, contact_area, (x - x_n) / h, mu_lambda, DBC, DBC_target, DBC_stiff, h)
33+
# ANCHOR: convergence_criteria
3134
[p, DBC_satisfied] = search_dir(x, e, x_tilde, m, l2, k, n, o, contact_area, (x - x_n) / h, mu_lambda, is_DBC, DBC, DBC_target, DBC_stiff, tol, h)
3235
while (LA.norm(p, inf) / h > tol) | (sum(DBC_satisfied) != len(DBC)): # also check whether all DBCs are satisfied
3336
print('Iteration', iter, ':')
@@ -37,6 +40,7 @@ def step_forward(x, e, v, m, l2, k, n, o, contact_area, mu, is_DBC, DBC, DBC_v,
3740
# increase DBC stiffness and recompute energy value record
3841
DBC_stiff *= 2
3942
E_last = IP_val(x, e, x_tilde, m, l2, k, n, o, contact_area, (x - x_n) / h, mu_lambda, DBC, DBC_target, DBC_stiff, h)
43+
# ANCHOR_END: convergence_criteria
4044

4145
# filter line search
4246
alpha = BarrierEnergy.init_step_size(x, n, o, p) # avoid interpenetration and tunneling
@@ -87,16 +91,20 @@ def IP_hess(x, e, x_tilde, m, l2, k, n, o, contact_area, v, mu_lambda, DBC, DBC_
8791
def search_dir(x, e, x_tilde, m, l2, k, n, o, contact_area, v, mu_lambda, is_DBC, DBC, DBC_target, DBC_stiff, tol, h):
8892
projected_hess = IP_hess(x, e, x_tilde, m, l2, k, n, o, contact_area, v, mu_lambda, DBC, DBC_target, DBC_stiff, h)
8993
reshaped_grad = IP_grad(x, e, x_tilde, m, l2, k, n, o, contact_area, v, mu_lambda, DBC, DBC_target, DBC_stiff, h).reshape(len(x) * 2, 1)
94+
# ANCHOR: dbc_check
9095
# check whether each DBC is satisfied
9196
DBC_satisfied = [False] * len(x)
9297
for i in range(0, len(DBC)):
9398
if LA.norm(x[DBC[i]] - DBC_target[i]) / h < tol:
9499
DBC_satisfied[DBC[i]] = True
100+
# ANCHOR_END: dbc_check
101+
# ANCHOR: dof_elimination
95102
# eliminate DOF if it's a satisfied DBC by modifying gradient and Hessian for DBC:
96103
for i, j in zip(*projected_hess.nonzero()):
97104
if (is_DBC[int(i / 2)] & DBC_satisfied[int(i / 2)]) | (is_DBC[int(j / 2)] & DBC_satisfied[int(i / 2)]):
98105
projected_hess[i, j] = (i == j)
99106
for i in range(0, len(x)):
100107
if is_DBC[i] & DBC_satisfied[i]:
101108
reshaped_grad[i * 2] = reshaped_grad[i * 2 + 1] = 0.0
102-
return [spsolve(projected_hess, -reshaped_grad).reshape(len(x), 2), DBC_satisfied]
109+
return [spsolve(projected_hess, -reshaped_grad).reshape(len(x), 2), DBC_satisfied]
110+
# ANCHOR_END: dof_elimination

0 commit comments

Comments
 (0)