Skip to content

Commit 0f1bf59

Browse files
committed
y_ground in simulator
1 parent a2b5f5c commit 0f1bf59

3 files changed

Lines changed: 28 additions & 28 deletions

File tree

contact/BarrierEnergy.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import math
22
import numpy as np
33

4-
y_ground = -1
54
dhat = 0.01
65
kappa = 1e5
76

8-
def val(x, contact_area):
7+
def val(x, y_ground, contact_area):
98
sum = 0.0
109
for i in range(0, len(x)):
1110
d = x[i][1] - y_ground
@@ -14,7 +13,7 @@ def val(x, contact_area):
1413
sum += contact_area[i] * dhat * kappa / 2 * (s - 1) * math.log(s)
1514
return sum
1615

17-
def grad(x, contact_area):
16+
def grad(x, y_ground, contact_area):
1817
g = np.array([[0.0, 0.0]] * len(x))
1918
for i in range(0, len(x)):
2019
d = x[i][1] - y_ground
@@ -23,7 +22,7 @@ def grad(x, contact_area):
2322
g[i][1] = contact_area[i] * dhat * (kappa / 2 * (math.log(s) / dhat + (s - 1) / d))
2423
return g
2524

26-
def hess(x, contact_area):
25+
def hess(x, y_ground, contact_area):
2726
IJV = [[0] * len(x), [0] * len(x), np.array([0.0] * len(x))]
2827
for i in range(0, len(x)):
2928
IJV[0][i] = i * 2 + 1
@@ -35,7 +34,7 @@ def hess(x, contact_area):
3534
IJV[2][i] = 0.0
3635
return IJV
3736

38-
def init_step_size(x, p):
37+
def init_step_size(x, y_ground, p):
3938
alpha = 1
4039
for i in range(0, len(x)):
4140
if p[i][1] < 0:

contact/simulator.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
# simulation setup
1111
side_len = 1
12-
rho = 1000 # density of square
13-
k = 2e4 # spring stiffness
14-
n_seg = 4 # num of segments per side of the square
15-
h = 0.01 # time step size in s
16-
DBC = [] # no nodes need to be fixed
12+
rho = 1000 # density of square
13+
k = 2e4 # spring stiffness
14+
n_seg = 4 # num of segments per side of the square
15+
h = 0.01 # time step size in s
16+
DBC = [] # no nodes need to be fixed
17+
y_ground = -1 # height of the planar ground
1718

1819
# initialize simulation
1920
[x, e] = square_mesh.generate(side_len, n_seg) # node positions and edge node indices
@@ -51,7 +52,7 @@ def screen_projection(x):
5152

5253
# fill the background and draw the square
5354
screen.fill((255, 255, 255))
54-
pygame.draw.aaline(screen, (0, 0, 255), [0, offset[1] + 1 * scale], [resolution[1], offset[1] + 1 * scale]) # ground
55+
pygame.draw.aaline(screen, (0, 0, 255), screen_projection([-2, y_ground]), screen_projection([2, y_ground])) # ground
5556
for eI in e:
5657
pygame.draw.aaline(screen, (0, 0, 255), screen_projection(x[eI[0]]), screen_projection(x[eI[1]]))
5758
for xI in x:
@@ -60,7 +61,7 @@ def screen_projection(x):
6061
pygame.display.flip() # flip the display
6162

6263
# step forward simulation and wait for screen refresh
63-
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, contact_area, is_DBC, h, 1e-2)
64+
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, y_ground, contact_area, is_DBC, h, 1e-2)
6465
time_step += 1
6566
pygame.time.wait(int(h * 1000))
6667

contact/time_integrator.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,52 @@
1111
import GravityEnergy
1212
import BarrierEnergy
1313

14-
def step_forward(x, e, v, m, l2, k, contact_area, is_DBC, h, tol):
14+
def step_forward(x, e, v, m, l2, k, y_ground, contact_area, is_DBC, h, tol):
1515
x_tilde = x + v * h # implicit Euler predictive position
1616
x_n = copy.deepcopy(x)
1717

1818
# Newton loop
1919
iter = 0
20-
E_last = IP_val(x, e, x_tilde, m, l2, k, contact_area, h)
21-
p = search_dir(x, e, x_tilde, m, l2, k, contact_area, is_DBC, h)
20+
E_last = IP_val(x, e, x_tilde, m, l2, k, y_ground, contact_area, h)
21+
p = search_dir(x, e, x_tilde, m, l2, k, y_ground, contact_area, is_DBC, h)
2222
while LA.norm(p, inf) / h > tol:
2323
print('Iteration', iter, ':')
2424
print('residual =', LA.norm(p, inf) / h)
2525

2626
# filter line search
27-
alpha = BarrierEnergy.init_step_size(x, p) # avoid interpenetration and tunneling
28-
while IP_val(x + alpha * p, e, x_tilde, m, l2, k, contact_area, h) > E_last:
27+
alpha = BarrierEnergy.init_step_size(x, y_ground, p) # avoid interpenetration and tunneling
28+
while IP_val(x + alpha * p, e, x_tilde, m, l2, k, y_ground, contact_area, h) > E_last:
2929
alpha /= 2
3030
print('step size =', alpha)
3131

3232
x += alpha * p
33-
E_last = IP_val(x, e, x_tilde, m, l2, k, contact_area, h)
34-
p = search_dir(x, e, x_tilde, m, l2, k, contact_area, is_DBC, h)
33+
E_last = IP_val(x, e, x_tilde, m, l2, k, y_ground, contact_area, h)
34+
p = search_dir(x, e, x_tilde, m, l2, k, y_ground, contact_area, is_DBC, h)
3535
iter += 1
3636

3737
v = (x - x_n) / h # implicit Euler velocity update
3838
return [x, v]
3939

40-
def IP_val(x, e, x_tilde, m, l2, k, contact_area, h):
41-
return InertiaEnergy.val(x, x_tilde, m) + h * h * (MassSpringEnergy.val(x, e, l2, k) + GravityEnergy.val(x, m) + BarrierEnergy.val(x, contact_area)) # implicit Euler
40+
def IP_val(x, e, x_tilde, m, l2, k, y_ground, contact_area, h):
41+
return InertiaEnergy.val(x, x_tilde, m) + h * h * (MassSpringEnergy.val(x, e, l2, k) + GravityEnergy.val(x, m) + BarrierEnergy.val(x, y_ground, contact_area)) # implicit Euler
4242

43-
def IP_grad(x, e, x_tilde, m, l2, k, contact_area, h):
44-
return InertiaEnergy.grad(x, x_tilde, m) + h * h * (MassSpringEnergy.grad(x, e, l2, k) + GravityEnergy.grad(x, m) + BarrierEnergy.grad(x, contact_area)) # implicit Euler
43+
def IP_grad(x, e, x_tilde, m, l2, k, y_ground, contact_area, h):
44+
return InertiaEnergy.grad(x, x_tilde, m) + h * h * (MassSpringEnergy.grad(x, e, l2, k) + GravityEnergy.grad(x, m) + BarrierEnergy.grad(x, y_ground, contact_area)) # implicit Euler
4545

46-
def IP_hess(x, e, x_tilde, m, l2, k, contact_area, h):
46+
def IP_hess(x, e, x_tilde, m, l2, k, y_ground, contact_area, h):
4747
IJV_In = InertiaEnergy.hess(x, x_tilde, m)
4848
IJV_MS = MassSpringEnergy.hess(x, e, l2, k)
49-
IJV_B = BarrierEnergy.hess(x, contact_area)
49+
IJV_B = BarrierEnergy.hess(x, y_ground, contact_area)
5050
IJV_MS[2] *= h * h # implicit Euler
5151
IJV_B[2] *= h * h # implicit Euler
5252
IJV_In_MS = np.append(IJV_In, IJV_MS, axis=1)
5353
IJV = np.append(IJV_In_MS, IJV_B, axis=1)
5454
H = sparse.coo_matrix((IJV[2], (IJV[0], IJV[1])), shape=(len(x) * 2, len(x) * 2)).tocsr()
5555
return H
5656

57-
def search_dir(x, e, x_tilde, m, l2, k, contact_area, is_DBC, h):
58-
projected_hess = IP_hess(x, e, x_tilde, m, l2, k, contact_area, h)
59-
reshaped_grad = IP_grad(x, e, x_tilde, m, l2, k, contact_area, h).reshape(len(x) * 2, 1)
57+
def search_dir(x, e, x_tilde, m, l2, k, y_ground, contact_area, is_DBC, h):
58+
projected_hess = IP_hess(x, e, x_tilde, m, l2, k, y_ground, contact_area, h)
59+
reshaped_grad = IP_grad(x, e, x_tilde, m, l2, k, y_ground, contact_area, h).reshape(len(x) * 2, 1)
6060
# eliminate DOF by modifying gradient and Hessian for DBC:
6161
for i, j in zip(*projected_hess.nonzero()):
6262
if is_DBC[int(i / 2)] | is_DBC[int(j / 2)]:

0 commit comments

Comments
 (0)