MCPcopy
hub / github.com/zju3dv/4K4D / perform_simulation

Function perform_simulation

scripts/torchxpbd/learning_simulation.py:27–122  ·  view source on GitHub ↗
(
    time=1/60,  # 1s for the simulation (physics time)
    steps=360,  # 5s for the simulation (physics steps
    substeps=1,  # 1ms for simulation substep
    quality_step=1.0,

    # opt_iter * lr -> how compliant the mesh is
    relaxation=1.0,
    lr=4e-2,
    opt_iter=80,  # the number of iterations for optimization leads to compliance: i.e. stiffness of the simulation
    lambda_smooth=1,  # will make the object much silky or stiff
)

Source from the content-addressed store, hash-verified

25
26
27def perform_simulation(
28 time=1/60, # 1s for the simulation (physics time)
29 steps=360, # 5s for the simulation (physics steps
30 substeps=1, # 1ms for simulation substep
31 quality_step=1.0,
32
33 # opt_iter * lr -> how compliant the mesh is
34 relaxation=1.0,
35 lr=4e-2,
36 opt_iter=80, # the number of iterations for optimization leads to compliance: i.e. stiffness of the simulation
37 lambda_smooth=1, # will make the object much silky or stiff
38):
39 global position, garment
40 opt_iter = int(opt_iter * quality_step)
41 lr = lr / quality_step
42 delta_t = time / substeps
43
44 velocity = torch.zeros_like(position)
45
46 M = compute_matrix(v, f, lambda_smooth)
47 position_145 = position[145]
48
49 animation = []
50 residuals = []
51 velocities = []
52 bending_gradients = []
53 stretch_gradients = []
54
55 pbar = tqdm(total=steps * substeps * opt_iter)
56 for i in range(steps):
57 log(f'\nTimestep: {(i+1) * time:.3f}')
58
59 # compute input values
60 initial = position.detach().requires_grad_()
61 with torch.enable_grad():
62 energy_stretch = stretch_energy(initial[None], garment)
63 energy_bending = bending_energy(initial[None], garment)
64 stretch_gradient = take_gradient(energy_stretch, initial)
65 bending_gradient = take_gradient(energy_bending, initial)
66 velocities.append(velocity)
67 stretch_gradients.append(stretch_gradient.detach())
68 bending_gradients.append(bending_gradient.detach())
69
70 for j in range(substeps):
71 velocity = velocity + delta_t * gravity
72 previous = position
73 position = position + delta_t * velocity
74 position[145] = position_145
75 y = position[..., 1]
76 position[..., 1] = y * (y > 0)
77
78 # this part can also be considered as an optimization problem
79 # solve constraints: with conditional gradient descent for optimization purpose
80 p = to_differential(M, position)
81 p = p.requires_grad_()
82 o = AdamUniform([p], lr=lr)
83 for k in range(opt_iter):
84 q = from_differential(M, p) # NOTE: not possible to naively keep the vertices here?

Callers 1

Calls 7

logFunction · 0.90
stretch_energyFunction · 0.90
bending_energyFunction · 0.90
take_gradientFunction · 0.90
backwardMethod · 0.45
stepMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected