@@ -78,7 +78,7 @@ def save(self):
7878dt = 1.0 / 60.0
7979num_substeps = 15
8080sdt = dt / num_substeps
81- solver_iterations = 1
81+ solver_iterations = 1 # can increase
8282
8383export_enabled = False
8484export_frame_count = 0
@@ -112,6 +112,10 @@ def save(self):
112112stretching_lengths = ti .field (dtype = ti .f64 , shape = num_stretching_constraints )
113113bending_lengths = ti .field (dtype = ti .f64 , shape = num_bending_constraints )
114114
115+ # XPBD accumulated lambdas (one per constraint, reset each substep)
116+ stretching_lambdas = ti .field (dtype = ti .f64 , shape = num_stretching_constraints )
117+ bending_lambdas = ti .field (dtype = ti .f64 , shape = num_bending_constraints )
118+
115119# Visualization fields
116120ground_vertices = ti .Vector .field (3 , dtype = ti .f64 , shape = 4 )
117121ground_indices = ti .field (ti .i32 , shape = 6 )
@@ -133,12 +137,28 @@ def save(self):
133137# Simulation Substep Function
134138# ============================================================================
135139
140+ @ti .kernel
141+ def reset_constraint_lambdas (
142+ num_s : ti .i32 , num_b : ti .i32 ,
143+ s_lambdas : ti .template (), b_lambdas : ti .template ()
144+ ):
145+ for i in range (num_s ):
146+ s_lambdas [i ] = 0.0
147+ for i in range (num_b ):
148+ b_lambdas [i ] = 0.0
149+
150+
136151def substep (grab_id , grab_x , grab_y , grab_z ):
137152 """Perform one simulation substep."""
138153 pre_solve (sdt , num_particles , gravity , pos , prev_pos , vel , inv_mass )
154+ # XPBD: reset lambdas once per substep (they accumulate across solver iterations)
155+ reset_constraint_lambdas (
156+ num_stretching_constraints , num_bending_constraints ,
157+ stretching_lambdas , bending_lambdas
158+ )
139159 for _ in range (solver_iterations ):
140- solve_stretching_constraints (stretching_compliance , sdt , num_stretching_constraints , pos , stretching_ids , stretching_lengths , inv_mass )
141- solve_bending_constraints (bending_compliance , sdt , num_bending_constraints , pos , bending_ids , bending_lengths , inv_mass )
160+ solve_stretching_constraints (stretching_compliance , sdt , num_stretching_constraints , pos , stretching_ids , stretching_lengths , inv_mass , stretching_lambdas )
161+ solve_bending_constraints (bending_compliance , sdt , num_bending_constraints , pos , bending_ids , bending_lengths , inv_mass , bending_lambdas )
142162 apply_grab (grab_id , grab_x , grab_y , grab_z , pos , vel , grab_indicator_pos )
143163 post_solve (sdt , num_particles , pos , prev_pos , vel , inv_mass )
144164
0 commit comments