-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxpbd.py
295 lines (274 loc) · 8.88 KB
/
xpbd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import pbatoolkit as pbat
import meshio
import numpy as np
import scipy as sp
import igl
import polyscope as ps
import polyscope.imgui as imgui
import argparse
import networkx as nx
import itertools
def combine(V: list, C: list):
NV = [Vi.shape[0] for Vi in V]
offsets = list(itertools.accumulate(NV))
C = [C[i] + offsets[i] - NV[i] for i in range(len(C))]
C = np.vstack(C)
V = np.vstack(V)
BV = np.hstack([np.full(NV[i], i) for i in range(len(NV))])
return V, C, BV
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="XPBD elastic simulation using linear FEM tetrahedra",
)
parser.add_argument(
"-i",
"--input",
help="Paths to input mesh",
nargs="+",
dest="inputs",
required=True,
)
parser.add_argument(
"-o", "--output", help="Path to output meshes", dest="output", default="."
)
parser.add_argument(
"-m",
"--mass-density",
help="Mass density",
type=float,
dest="rho",
default=1000.0,
)
parser.add_argument(
"-Y",
"--young-modulus",
help="Young's modulus",
type=float,
dest="Y",
default=1e6,
)
parser.add_argument(
"-n",
"--poisson-ratio",
help="Poisson's ratio",
type=float,
dest="nu",
default=0.45,
)
parser.add_argument(
"--betaSNH",
help="Stable Neo-Hookean constraint damping",
type=float,
dest="betaSNH",
default=0.0,
)
parser.add_argument(
"--alphaC",
help="Vertex collision constraint compliance",
type=float,
dest="alphaC",
default=0.0,
)
parser.add_argument(
"--betaC",
help="Vertex collision constraint damping",
type=float,
dest="betaC",
default=0.0,
)
parser.add_argument(
"--muS", help="Static friction coefficient", type=float, dest="muS", default=0.6
)
parser.add_argument(
"--muD",
help="Dynamic friction coefficient",
type=float,
dest="muD",
default=0.4,
)
parser.add_argument(
"--muC", help="Vertex collision penalty", type=float, dest="muC", default=1e1
)
parser.add_argument(
"--contact-set-update-frequency",
help="Contact set update frequency",
type=int,
dest="contact_set_update_frequency",
default=10,
)
parser.add_argument(
"-t",
"--translation",
help="Distance in z axis between every input mesh as multiplier of input mesh extents",
type=float,
dest="translation",
default=0.1,
)
parser.add_argument(
"--fixed-axis",
help="Axis of scene to fix",
type=int,
dest="fixed_axis",
default=2,
)
parser.add_argument(
"--percent-fixed",
help="Percentage, in the z-axis, of scene mesh to fix",
type=float,
dest="percent_fixed",
default=0.01,
)
parser.add_argument(
"--use-gpu",
help="Use GPU implementation",
action="store_true",
dest="gpu",
default=False,
)
parser.add_argument(
"--use-clustering",
help="Use Ton-That et al. 2023's clustered constraint graph parallelization strategy",
action="store_true",
dest="cluster",
default=False,
)
args = parser.parse_args()
# Construct FEM quantities for simulation
imeshes = [meshio.read(input) for input in args.inputs]
V, C = [
imesh.points / (imesh.points.max() - imesh.points.min()) for imesh in imeshes
], [imesh.cells_dict["tetra"] for imesh in imeshes]
for i in range(len(V) - 1):
extent = V[i][:, -1].max() - V[i][:, -1].min()
offset = V[i][:, -1].max() - V[i + 1][:, -1].min()
V[i + 1][:, -1] += offset + extent * args.translation
V, C, BV = combine(V, C)
mesh = pbat.fem.Mesh(V.T, C.T, element=pbat.fem.Element.Tetrahedron, order=1)
F = igl.boundary_facets(C)
F[:, :2] = np.roll(F[:, :2], shift=1, axis=1)
# Compute mass
M, detJeM = pbat.fem.mass_matrix(mesh, rho=args.rho, dims=1, lump=True)
m = np.array(M.diagonal()).squeeze()
# Compute material (Lame) constants
Y = np.full(mesh.E.shape[1], args.Y)
nu = np.full(mesh.E.shape[1], args.nu)
mue = Y / (2 * (1 + nu))
lambdae = (Y * nu) / ((1 + nu) * (1 - 2 * nu))
# Set Dirichlet boundary conditions
Xmin = mesh.X.min(axis=1)
Xmax = mesh.X.max(axis=1)
extent = Xmax - Xmin
Xmax[args.fixed_axis] = (
Xmin[args.fixed_axis] + args.percent_fixed * extent[args.fixed_axis]
)
aabb = pbat.geometry.aabb(np.vstack((Xmin, Xmax)).T)
vdbc = aabb.contained(mesh.X)
minv = 1 / m
# Setup XPBD
VC = np.unique(F)
# dblA = igl.doublearea(V, F)
# muC = np.zeros(V.shape[0])
# for d in range(3):
# muC[F[:, d]] += (1 / 6) * dblA
# muC = args.muC * muC[VC]
gc_ordering = pbat.graph.GreedyColorOrderingStrategy.LargestDegree
gc_selection = pbat.graph.GreedyColorSelectionStrategy.LeastUsed
Pptr, Padj, GC = pbat.sim.xpbd.partition_mesh_constraints(
mesh.X, mesh.E, ordering=gc_ordering, selection=gc_selection
)
data = (
pbat.sim.xpbd.Data()
.with_volume_mesh(mesh.X, mesh.E)
.with_surface_mesh(VC, F.T)
.with_bodies(BV)
.with_mass_inverse(minv)
.with_elastic_material(np.vstack((mue, lambdae)))
.with_damping(
np.full(mesh.E.shape[1] * 2, args.betaSNH),
pbat.sim.xpbd.Constraint.StableNeoHookean,
)
.with_compliance(
np.full(VC.shape[0], args.alphaC), pbat.sim.xpbd.Constraint.Collision
)
.with_damping(
np.full(VC.shape[0], args.betaC), pbat.sim.xpbd.Constraint.Collision
)
# .with_collision_penalties(muC)
.with_friction_coefficients(args.muS, args.muD)
.with_active_set_update_frequency(args.contact_set_update_frequency)
.with_dirichlet_vertices(vdbc)
.with_partitions(Pptr, Padj)
)
ps.set_verbosity(0)
ps.set_up_dir("z_up")
ps.set_front_dir("neg_y_front")
ps.set_ground_plane_mode("shadow_only")
ps.set_ground_plane_height_factor(0.5)
ps.set_program_name("eXtended Position Based Dynamics")
ps.init()
vm = ps.register_volume_mesh("Simulation mesh", mesh.X.T, mesh.E.T)
vm.add_scalar_quantity("Coloring", GC, defined_on="cells", cmap="jet")
pc = ps.register_point_cloud("Dirichlet", mesh.X[:, vdbc].T)
dt = 0.01
iterations = 1
substeps = 50
animate = False
export = False
t = 0
has_partitioning = getattr(pbat.graph, "partition") is not None
if has_partitioning and args.cluster:
SGptr, SGadj, Cptr, Cadj, clustering, SGC = (
pbat.sim.xpbd.partition_clustered_mesh_constraint_graph(
mesh.X, mesh.E, ordering=vc_ordering, selection=vc_selection
)
)
data.with_cluster_partitions(SGptr, SGadj, Cptr, Cadj)
ecolors = SGC[clustering]
max_color = GC.max()
vm.add_scalar_quantity(
"Clustered Coloring",
ecolors,
defined_on="cells",
cmap="jet",
vminmax=(0, max_color),
)
data.construct()
integrator_type = pbat.gpu.xpbd.Integrator if args.gpu else pbat.sim.xpbd.Integrator
xpbd = integrator_type(data)
profiler = pbat.profiling.Profiler()
def callback():
global dt, iterations, substeps, alphac
global animate, export, t
global profiler
changed, dt = imgui.InputFloat("dt", dt)
changed, iterations = imgui.InputInt("Iterations", iterations)
changed, substeps = imgui.InputInt("Substeps", substeps)
changed, animate = imgui.Checkbox("Animate", animate)
changed, export = imgui.Checkbox("Export", export)
step = imgui.Button("Step")
reset = imgui.Button("Reset")
if reset:
xpbd.x = mesh.X
xpbd.v = np.zeros(mesh.X.shape)
vm.update_vertex_positions(mesh.X.T)
t = 0
if animate or step:
profiler.begin_frame("Physics")
xpbd.step(dt, iterations, substeps)
profiler.end_frame("Physics")
# Update visuals
V = xpbd.x.T
if hasattr(pbat.gpu.xpbd, "Integrator") and isinstance(
xpbd, pbat.gpu.xpbd.Integrator
):
min, max = np.min(V, axis=0), np.max(V, axis=0)
xpbd.scene_bounding_box = min, max
if export:
ps.screenshot(f"{args.output}/{t}.png")
# omesh = meshio.Mesh(V, {"tetra": mesh.E.T})
# meshio.write(f"{args.output}/{t}.mesh", omesh)
vm.update_vertex_positions(V)
t = t + 1
imgui.Text(f"Frame={t}")
ps.set_user_callback(callback)
ps.show()