1111import GravityEnergy
1212import BarrierEnergy
1313
14- def step_forward (x , e , v , m , l2 , k , y_ground , contact_area , is_DBC , h , tol ):
14+ def step_forward (x , e , v , m , l2 , k , y_ground , contact_area , is_DBC , reduced_basis , 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
2020 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 )
21+ p = search_dir (x , e , x_tilde , m , l2 , k , y_ground , contact_area , is_DBC , reduced_basis , h )
2222 while LA .norm (p , inf ) / h > tol :
2323 print ('Iteration' , iter , ':' )
2424 print ('residual =' , LA .norm (p , inf ) / h )
@@ -33,7 +33,7 @@ def step_forward(x, e, v, m, l2, k, y_ground, contact_area, is_DBC, h, tol):
3333
3434 x += alpha * p
3535 E_last = IP_val (x , e , x_tilde , m , l2 , k , y_ground , contact_area , h )
36- p = search_dir (x , e , x_tilde , m , l2 , k , y_ground , contact_area , is_DBC , h )
36+ p = search_dir (x , e , x_tilde , m , l2 , k , y_ground , contact_area , is_DBC , reduced_basis , h )
3737 iter += 1
3838
3939 v = (x - x_n ) / h # implicit Euler velocity update
@@ -56,7 +56,7 @@ def IP_hess(x, e, x_tilde, m, l2, k, y_ground, contact_area, h):
5656 H = sparse .coo_matrix ((IJV [2 ], (IJV [0 ], IJV [1 ])), shape = (len (x ) * 2 , len (x ) * 2 )).tocsr ()
5757 return H
5858
59- def search_dir (x , e , x_tilde , m , l2 , k , y_ground , contact_area , is_DBC , h ):
59+ def search_dir (x , e , x_tilde , m , l2 , k , y_ground , contact_area , is_DBC , reduced_basis , h ):
6060 projected_hess = IP_hess (x , e , x_tilde , m , l2 , k , y_ground , contact_area , h )
6161 reshaped_grad = IP_grad (x , e , x_tilde , m , l2 , k , y_ground , contact_area , h ).reshape (len (x ) * 2 , 1 )
6262 # eliminate DOF by modifying gradient and Hessian for DBC:
@@ -66,4 +66,6 @@ def search_dir(x, e, x_tilde, m, l2, k, y_ground, contact_area, is_DBC, h):
6666 for i in range (0 , len (x )):
6767 if is_DBC [i ]:
6868 reshaped_grad [i * 2 ] = reshaped_grad [i * 2 + 1 ] = 0.0
69- return spsolve (projected_hess , - reshaped_grad ).reshape (len (x ), 2 )
69+ reduced_hess = reduced_basis .T .dot (projected_hess .dot (reduced_basis )) # applying chain rule
70+ reduced_grad = reduced_basis .T .dot (reshaped_grad ) # applying chain rule
71+ return (reduced_basis .dot (spsolve (reduced_hess , - reduced_grad ))).reshape (len (x ), 2 ) # transform to full space after the solve
0 commit comments