warp-lang 1.0.2__py3-none-win_amd64.whl → 1.2.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.dll +0 -0
  4. warp/bin/warp.dll +0 -0
  5. warp/build.py +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
@@ -1,1956 +1,1956 @@
1
- # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- """This module contains time-integration objects for simulating
9
- models + state forward in time.
10
-
11
- """
12
-
13
- import warp as wp
14
-
15
- from .integrator import Integrator
16
- from .collide import triangle_closest_point_barycentric
17
- from .model import Model, State, Control, PARTICLE_FLAG_ACTIVE, ModelShapeGeometry, ModelShapeMaterials
18
- from .particles import eval_particle_forces
19
- from .utils import quat_decompose, quat_twist
20
-
21
-
22
- @wp.kernel
23
- def eval_springs(
24
- x: wp.array(dtype=wp.vec3),
25
- v: wp.array(dtype=wp.vec3),
26
- spring_indices: wp.array(dtype=int),
27
- spring_rest_lengths: wp.array(dtype=float),
28
- spring_stiffness: wp.array(dtype=float),
29
- spring_damping: wp.array(dtype=float),
30
- f: wp.array(dtype=wp.vec3),
31
- ):
32
- tid = wp.tid()
33
-
34
- i = spring_indices[tid * 2 + 0]
35
- j = spring_indices[tid * 2 + 1]
36
-
37
- ke = spring_stiffness[tid]
38
- kd = spring_damping[tid]
39
- rest = spring_rest_lengths[tid]
40
-
41
- xi = x[i]
42
- xj = x[j]
43
-
44
- vi = v[i]
45
- vj = v[j]
46
-
47
- xij = xi - xj
48
- vij = vi - vj
49
-
50
- l = wp.length(xij)
51
- l_inv = 1.0 / l
52
-
53
- # normalized spring direction
54
- dir = xij * l_inv
55
-
56
- c = l - rest
57
- dcdt = wp.dot(dir, vij)
58
-
59
- # damping based on relative velocity
60
- fs = dir * (ke * c + kd * dcdt)
61
-
62
- wp.atomic_sub(f, i, fs)
63
- wp.atomic_add(f, j, fs)
64
-
65
-
66
- @wp.kernel
67
- def eval_triangles(
68
- x: wp.array(dtype=wp.vec3),
69
- v: wp.array(dtype=wp.vec3),
70
- indices: wp.array2d(dtype=int),
71
- pose: wp.array(dtype=wp.mat22),
72
- activation: wp.array(dtype=float),
73
- materials: wp.array2d(dtype=float),
74
- f: wp.array(dtype=wp.vec3),
75
- ):
76
- tid = wp.tid()
77
-
78
- k_mu = materials[tid, 0]
79
- k_lambda = materials[tid, 1]
80
- k_damp = materials[tid, 2]
81
- k_drag = materials[tid, 3]
82
- k_lift = materials[tid, 4]
83
-
84
- i = indices[tid, 0]
85
- j = indices[tid, 1]
86
- k = indices[tid, 2]
87
-
88
- x0 = x[i] # point zero
89
- x1 = x[j] # point one
90
- x2 = x[k] # point two
91
-
92
- v0 = v[i] # vel zero
93
- v1 = v[j] # vel one
94
- v2 = v[k] # vel two
95
-
96
- x10 = x1 - x0 # barycentric coordinates (centered at p)
97
- x20 = x2 - x0
98
-
99
- v10 = v1 - v0
100
- v20 = v2 - v0
101
-
102
- Dm = pose[tid]
103
-
104
- inv_rest_area = wp.determinant(Dm) * 2.0 # 1 / det(A) = det(A^-1)
105
- rest_area = 1.0 / inv_rest_area
106
-
107
- # scale stiffness coefficients to account for area
108
- k_mu = k_mu * rest_area
109
- k_lambda = k_lambda * rest_area
110
- k_damp = k_damp * rest_area
111
-
112
- # F = Xs*Xm^-1
113
- F1 = x10 * Dm[0, 0] + x20 * Dm[1, 0]
114
- F2 = x10 * Dm[0, 1] + x20 * Dm[1, 1]
115
-
116
- # dFdt = Vs*Xm^-1
117
- dFdt1 = v10 * Dm[0, 0] + v20 * Dm[1, 0]
118
- dFdt2 = v10 * Dm[0, 1] + v20 * Dm[1, 1]
119
-
120
- # deviatoric PK1 + damping term
121
- P1 = F1 * k_mu + dFdt1 * k_damp
122
- P2 = F2 * k_mu + dFdt2 * k_damp
123
-
124
- # -----------------------------
125
- # St. Venant-Kirchoff
126
-
127
- # # Green strain, F'*F-I
128
- # e00 = dot(f1, f1) - 1.0
129
- # e10 = dot(f2, f1)
130
- # e01 = dot(f1, f2)
131
- # e11 = dot(f2, f2) - 1.0
132
-
133
- # E = wp.mat22(e00, e01,
134
- # e10, e11)
135
-
136
- # # local forces (deviatoric part)
137
- # T = wp.mul(E, wp.transpose(Dm))
138
-
139
- # # spatial forces, F*T
140
- # fq = (f1*T[0,0] + f2*T[1,0])*k_mu*2.0
141
- # fr = (f1*T[0,1] + f2*T[1,1])*k_mu*2.0
142
- # alpha = 1.0
143
-
144
- # -----------------------------
145
- # Baraff & Witkin, note this model is not isotropic
146
-
147
- # c1 = length(f1) - 1.0
148
- # c2 = length(f2) - 1.0
149
- # f1 = normalize(f1)*c1*k1
150
- # f2 = normalize(f2)*c2*k1
151
-
152
- # fq = f1*Dm[0,0] + f2*Dm[0,1]
153
- # fr = f1*Dm[1,0] + f2*Dm[1,1]
154
-
155
- # -----------------------------
156
- # Neo-Hookean (with rest stability)
157
-
158
- # force = P*Dm'
159
- f1 = P1 * Dm[0, 0] + P2 * Dm[0, 1]
160
- f2 = P1 * Dm[1, 0] + P2 * Dm[1, 1]
161
- alpha = 1.0 + k_mu / k_lambda
162
-
163
- # -----------------------------
164
- # Area Preservation
165
-
166
- n = wp.cross(x10, x20)
167
- area = wp.length(n) * 0.5
168
-
169
- # actuation
170
- act = activation[tid]
171
-
172
- # J-alpha
173
- c = area * inv_rest_area - alpha + act
174
-
175
- # dJdx
176
- n = wp.normalize(n)
177
- dcdq = wp.cross(x20, n) * inv_rest_area * 0.5
178
- dcdr = wp.cross(n, x10) * inv_rest_area * 0.5
179
-
180
- f_area = k_lambda * c
181
-
182
- # -----------------------------
183
- # Area Damping
184
-
185
- dcdt = wp.dot(dcdq, v1) + wp.dot(dcdr, v2) - wp.dot(dcdq + dcdr, v0)
186
- f_damp = k_damp * dcdt
187
-
188
- f1 = f1 + dcdq * (f_area + f_damp)
189
- f2 = f2 + dcdr * (f_area + f_damp)
190
- f0 = f1 + f2
191
-
192
- # -----------------------------
193
- # Lift + Drag
194
-
195
- vmid = (v0 + v1 + v2) * 0.3333
196
- vdir = wp.normalize(vmid)
197
-
198
- f_drag = vmid * (k_drag * area * wp.abs(wp.dot(n, vmid)))
199
- f_lift = n * (k_lift * area * (wp.HALF_PI - wp.acos(wp.dot(n, vdir)))) * wp.dot(vmid, vmid)
200
-
201
- f0 = f0 - f_drag - f_lift
202
- f1 = f1 + f_drag + f_lift
203
- f2 = f2 + f_drag + f_lift
204
-
205
- # apply forces
206
- wp.atomic_add(f, i, f0)
207
- wp.atomic_sub(f, j, f1)
208
- wp.atomic_sub(f, k, f2)
209
-
210
-
211
- # @wp.func
212
- # def triangle_closest_point(a: wp.vec3, b: wp.vec3, c: wp.vec3, p: wp.vec3):
213
- # ab = b - a
214
- # ac = c - a
215
- # ap = p - a
216
-
217
- # d1 = wp.dot(ab, ap)
218
- # d2 = wp.dot(ac, ap)
219
-
220
- # if (d1 <= 0.0 and d2 <= 0.0):
221
- # return a
222
-
223
- # bp = p - b
224
- # d3 = wp.dot(ab, bp)
225
- # d4 = wp.dot(ac, bp)
226
-
227
- # if (d3 >= 0.0 and d4 <= d3):
228
- # return b
229
-
230
- # vc = d1 * d4 - d3 * d2
231
- # v = d1 / (d1 - d3)
232
- # if (vc <= 0.0 and d1 >= 0.0 and d3 <= 0.0):
233
- # return a + ab * v
234
-
235
- # cp = p - c
236
- # d5 = dot(ab, cp)
237
- # d6 = dot(ac, cp)
238
-
239
- # if (d6 >= 0.0 and d5 <= d6):
240
- # return c
241
-
242
- # vb = d5 * d2 - d1 * d6
243
- # w = d2 / (d2 - d6)
244
- # if (vb <= 0.0 and d2 >= 0.0 and d6 <= 0.0):
245
- # return a + ac * w
246
-
247
- # va = d3 * d6 - d5 * d4
248
- # w = (d4 - d3) / ((d4 - d3) + (d5 - d6))
249
- # if (va <= 0.0 and (d4 - d3) >= 0.0 and (d5 - d6) >= 0.0):
250
- # return b + (c - b) * w
251
-
252
- # denom = 1.0 / (va + vb + vc)
253
- # v = vb * denom
254
- # w = vc * denom
255
-
256
- # return a + ab * v + ac * w
257
-
258
-
259
- @wp.kernel
260
- def eval_triangles_contact(
261
- # idx : wp.array(dtype=int), # list of indices for colliding particles
262
- num_particles: int, # size of particles
263
- x: wp.array(dtype=wp.vec3),
264
- v: wp.array(dtype=wp.vec3),
265
- indices: wp.array2d(dtype=int),
266
- materials: wp.array2d(dtype=float),
267
- f: wp.array(dtype=wp.vec3),
268
- ):
269
- tid = wp.tid()
270
- face_no = tid // num_particles # which face
271
- particle_no = tid % num_particles # which particle
272
-
273
- k_mu = materials[face_no, 0]
274
- k_lambda = materials[face_no, 1]
275
- k_damp = materials[face_no, 2]
276
- k_drag = materials[face_no, 3]
277
- k_lift = materials[face_no, 4]
278
-
279
- # at the moment, just one particle
280
- pos = x[particle_no]
281
-
282
- i = indices[face_no, 0]
283
- j = indices[face_no, 1]
284
- k = indices[face_no, 2]
285
-
286
- if i == particle_no or j == particle_no or k == particle_no:
287
- return
288
-
289
- p = x[i] # point zero
290
- q = x[j] # point one
291
- r = x[k] # point two
292
-
293
- # vp = v[i] # vel zero
294
- # vq = v[j] # vel one
295
- # vr = v[k] # vel two
296
-
297
- # qp = q-p # barycentric coordinates (centered at p)
298
- # rp = r-p
299
-
300
- bary = triangle_closest_point_barycentric(p, q, r, pos)
301
- closest = p * bary[0] + q * bary[1] + r * bary[2]
302
-
303
- diff = pos - closest
304
- dist = wp.dot(diff, diff)
305
- n = wp.normalize(diff)
306
- c = wp.min(dist - 0.01, 0.0) # 0 unless within 0.01 of surface
307
- # c = wp.leaky_min(dot(n, x0)-0.01, 0.0, 0.0)
308
- fn = n * c * 1e5
309
-
310
- wp.atomic_sub(f, particle_no, fn)
311
-
312
- # # apply forces (could do - f / 3 here)
313
- wp.atomic_add(f, i, fn * bary[0])
314
- wp.atomic_add(f, j, fn * bary[1])
315
- wp.atomic_add(f, k, fn * bary[2])
316
-
317
-
318
- @wp.kernel
319
- def eval_triangles_body_contacts(
320
- num_particles: int, # number of particles (size of contact_point)
321
- x: wp.array(dtype=wp.vec3), # position of particles
322
- v: wp.array(dtype=wp.vec3),
323
- indices: wp.array(dtype=int), # triangle indices
324
- body_x: wp.array(dtype=wp.vec3), # body body positions
325
- body_r: wp.array(dtype=wp.quat),
326
- body_v: wp.array(dtype=wp.vec3),
327
- body_w: wp.array(dtype=wp.vec3),
328
- contact_body: wp.array(dtype=int),
329
- contact_point: wp.array(dtype=wp.vec3), # position of contact points relative to body
330
- contact_dist: wp.array(dtype=float),
331
- contact_mat: wp.array(dtype=int),
332
- materials: wp.array(dtype=float),
333
- # body_f : wp.array(dtype=wp.vec3),
334
- # body_t : wp.array(dtype=wp.vec3),
335
- tri_f: wp.array(dtype=wp.vec3),
336
- ):
337
- tid = wp.tid()
338
-
339
- face_no = tid // num_particles # which face
340
- particle_no = tid % num_particles # which particle
341
-
342
- # -----------------------
343
- # load body body point
344
- c_body = contact_body[particle_no]
345
- c_point = contact_point[particle_no]
346
- c_dist = contact_dist[particle_no]
347
- c_mat = contact_mat[particle_no]
348
-
349
- # hard coded surface parameter tensor layout (ke, kd, kf, mu)
350
- ke = materials[c_mat * 4 + 0] # restitution coefficient
351
- kd = materials[c_mat * 4 + 1] # damping coefficient
352
- kf = materials[c_mat * 4 + 2] # friction coefficient
353
- mu = materials[c_mat * 4 + 3] # coulomb friction
354
-
355
- x0 = body_x[c_body] # position of colliding body
356
- r0 = body_r[c_body] # orientation of colliding body
357
-
358
- v0 = body_v[c_body]
359
- w0 = body_w[c_body]
360
-
361
- # transform point to world space
362
- pos = x0 + wp.quat_rotate(r0, c_point)
363
- # use x0 as center, everything is offset from center of mass
364
-
365
- # moment arm
366
- r = pos - x0 # basically just c_point in the new coordinates
367
- rhat = wp.normalize(r)
368
- pos = pos + rhat * c_dist # add on 'thickness' of shape, e.g.: radius of sphere/capsule
369
-
370
- # contact point velocity
371
- dpdt = v0 + wp.cross(w0, r) # this is body velocity cross offset, so it's the velocity of the contact point.
372
-
373
- # -----------------------
374
- # load triangle
375
- i = indices[face_no * 3 + 0]
376
- j = indices[face_no * 3 + 1]
377
- k = indices[face_no * 3 + 2]
378
-
379
- p = x[i] # point zero
380
- q = x[j] # point one
381
- r = x[k] # point two
382
-
383
- vp = v[i] # vel zero
384
- vq = v[j] # vel one
385
- vr = v[k] # vel two
386
-
387
- bary = triangle_closest_point_barycentric(p, q, r, pos)
388
- closest = p * bary[0] + q * bary[1] + r * bary[2]
389
-
390
- diff = pos - closest # vector from tri to point
391
- dist = wp.dot(diff, diff) # squared distance
392
- n = wp.normalize(diff) # points into the object
393
- c = wp.min(dist - 0.05, 0.0) # 0 unless within 0.05 of surface
394
- # c = wp.leaky_min(wp.dot(n, x0)-0.01, 0.0, 0.0)
395
- # fn = n * c * 1e6 # points towards cloth (both n and c are negative)
396
-
397
- # wp.atomic_sub(tri_f, particle_no, fn)
398
-
399
- fn = c * ke # normal force (restitution coefficient * how far inside for ground) (negative)
400
-
401
- vtri = vp * bary[0] + vq * bary[1] + vr * bary[2] # bad approximation for centroid velocity
402
- vrel = vtri - dpdt
403
-
404
- vn = wp.dot(n, vrel) # velocity component of body in negative normal direction
405
- vt = vrel - n * vn # velocity component not in normal direction
406
-
407
- # contact damping
408
- fd = -wp.max(vn, 0.0) * kd * wp.step(c) # again, negative, into the ground
409
-
410
- # # viscous friction
411
- # ft = vt*kf
412
-
413
- # Coulomb friction (box)
414
- lower = mu * (fn + fd)
415
- upper = -lower
416
-
417
- nx = wp.cross(n, wp.vec3(0.0, 0.0, 1.0)) # basis vectors for tangent
418
- nz = wp.cross(n, wp.vec3(1.0, 0.0, 0.0))
419
-
420
- vx = wp.clamp(wp.dot(nx * kf, vt), lower, upper)
421
- vz = wp.clamp(wp.dot(nz * kf, vt), lower, upper)
422
-
423
- ft = (nx * vx + nz * vz) * (-wp.step(c)) # wp.vec3(vx, 0.0, vz)*wp.step(c)
424
-
425
- # # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
426
- # #ft = wp.normalize(vt)*wp.min(kf*wp.length(vt), -mu*c*ke)
427
-
428
- f_total = n * (fn + fd) + ft
429
-
430
- wp.atomic_add(tri_f, i, f_total * bary[0])
431
- wp.atomic_add(tri_f, j, f_total * bary[1])
432
- wp.atomic_add(tri_f, k, f_total * bary[2])
433
-
434
-
435
- @wp.kernel
436
- def eval_bending(
437
- x: wp.array(dtype=wp.vec3),
438
- v: wp.array(dtype=wp.vec3),
439
- indices: wp.array2d(dtype=int),
440
- rest: wp.array(dtype=float),
441
- bending_properties: wp.array2d(dtype=float),
442
- f: wp.array(dtype=wp.vec3),
443
- ):
444
- tid = wp.tid()
445
- ke = bending_properties[tid, 0]
446
- kd = bending_properties[tid, 1]
447
-
448
- i = indices[tid, 0]
449
- j = indices[tid, 1]
450
- k = indices[tid, 2]
451
- l = indices[tid, 3]
452
-
453
- rest_angle = rest[tid]
454
-
455
- x1 = x[i]
456
- x2 = x[j]
457
- x3 = x[k]
458
- x4 = x[l]
459
-
460
- v1 = v[i]
461
- v2 = v[j]
462
- v3 = v[k]
463
- v4 = v[l]
464
-
465
- n1 = wp.cross(x3 - x1, x4 - x1) # normal to face 1
466
- n2 = wp.cross(x4 - x2, x3 - x2) # normal to face 2
467
-
468
- n1_length = wp.length(n1)
469
- n2_length = wp.length(n2)
470
-
471
- if n1_length < 1.0e-6 or n2_length < 1.0e-6:
472
- return
473
-
474
- rcp_n1 = 1.0 / n1_length
475
- rcp_n2 = 1.0 / n2_length
476
-
477
- cos_theta = wp.dot(n1, n2) * rcp_n1 * rcp_n2
478
-
479
- n1 = n1 * rcp_n1 * rcp_n1
480
- n2 = n2 * rcp_n2 * rcp_n2
481
-
482
- e = x4 - x3
483
- e_hat = wp.normalize(e)
484
- e_length = wp.length(e)
485
-
486
- s = wp.sign(wp.dot(wp.cross(n2, n1), e_hat))
487
- angle = wp.acos(cos_theta) * s
488
-
489
- d1 = n1 * e_length
490
- d2 = n2 * e_length
491
- d3 = n1 * wp.dot(x1 - x4, e_hat) + n2 * wp.dot(x2 - x4, e_hat)
492
- d4 = n1 * wp.dot(x3 - x1, e_hat) + n2 * wp.dot(x3 - x2, e_hat)
493
-
494
- # elastic
495
- f_elastic = ke * (angle - rest_angle)
496
-
497
- # damping
498
- f_damp = kd * (wp.dot(d1, v1) + wp.dot(d2, v2) + wp.dot(d3, v3) + wp.dot(d4, v4))
499
-
500
- # total force, proportional to edge length
501
- f_total = -e_length * (f_elastic + f_damp)
502
-
503
- wp.atomic_add(f, i, d1 * f_total)
504
- wp.atomic_add(f, j, d2 * f_total)
505
- wp.atomic_add(f, k, d3 * f_total)
506
- wp.atomic_add(f, l, d4 * f_total)
507
-
508
-
509
- @wp.kernel
510
- def eval_tetrahedra(
511
- x: wp.array(dtype=wp.vec3),
512
- v: wp.array(dtype=wp.vec3),
513
- indices: wp.array2d(dtype=int),
514
- pose: wp.array(dtype=wp.mat33),
515
- activation: wp.array(dtype=float),
516
- materials: wp.array2d(dtype=float),
517
- f: wp.array(dtype=wp.vec3),
518
- ):
519
- tid = wp.tid()
520
-
521
- i = indices[tid, 0]
522
- j = indices[tid, 1]
523
- k = indices[tid, 2]
524
- l = indices[tid, 3]
525
-
526
- act = activation[tid]
527
-
528
- k_mu = materials[tid, 0]
529
- k_lambda = materials[tid, 1]
530
- k_damp = materials[tid, 2]
531
-
532
- x0 = x[i]
533
- x1 = x[j]
534
- x2 = x[k]
535
- x3 = x[l]
536
-
537
- v0 = v[i]
538
- v1 = v[j]
539
- v2 = v[k]
540
- v3 = v[l]
541
-
542
- x10 = x1 - x0
543
- x20 = x2 - x0
544
- x30 = x3 - x0
545
-
546
- v10 = v1 - v0
547
- v20 = v2 - v0
548
- v30 = v3 - v0
549
-
550
- Ds = wp.mat33(x10, x20, x30)
551
- Dm = pose[tid]
552
-
553
- inv_rest_volume = wp.determinant(Dm) * 6.0
554
- rest_volume = 1.0 / inv_rest_volume
555
-
556
- alpha = 1.0 + k_mu / k_lambda - k_mu / (4.0 * k_lambda)
557
-
558
- # scale stiffness coefficients to account for area
559
- k_mu = k_mu * rest_volume
560
- k_lambda = k_lambda * rest_volume
561
- k_damp = k_damp * rest_volume
562
-
563
- # F = Xs*Xm^-1
564
- F = Ds * Dm
565
- dFdt = wp.mat33(v10, v20, v30) * Dm
566
-
567
- col1 = wp.vec3(F[0, 0], F[1, 0], F[2, 0])
568
- col2 = wp.vec3(F[0, 1], F[1, 1], F[2, 1])
569
- col3 = wp.vec3(F[0, 2], F[1, 2], F[2, 2])
570
-
571
- # -----------------------------
572
- # Neo-Hookean (with rest stability [Smith et al 2018])
573
-
574
- Ic = wp.dot(col1, col1) + wp.dot(col2, col2) + wp.dot(col3, col3)
575
-
576
- # deviatoric part
577
- P = F * k_mu * (1.0 - 1.0 / (Ic + 1.0)) + dFdt * k_damp
578
- H = P * wp.transpose(Dm)
579
-
580
- f1 = wp.vec3(H[0, 0], H[1, 0], H[2, 0])
581
- f2 = wp.vec3(H[0, 1], H[1, 1], H[2, 1])
582
- f3 = wp.vec3(H[0, 2], H[1, 2], H[2, 2])
583
-
584
- # -----------------------------
585
- # C_sqrt
586
-
587
- # alpha = 1.0
588
-
589
- # r_s = wp.sqrt(wp.abs(dot(col1, col1) + dot(col2, col2) + dot(col3, col3) - 3.0))
590
-
591
- # f1 = wp.vec3()
592
- # f2 = wp.vec3()
593
- # f3 = wp.vec3()
594
-
595
- # if (r_s > 0.0):
596
- # r_s_inv = 1.0/r_s
597
-
598
- # C = r_s
599
- # dCdx = F*wp.transpose(Dm)*r_s_inv*wp.sign(r_s)
600
-
601
- # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
602
- # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
603
- # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
604
-
605
- # f1 = grad1*C*k_mu
606
- # f2 = grad2*C*k_mu
607
- # f3 = grad3*C*k_mu
608
-
609
- # -----------------------------
610
- # C_spherical
611
-
612
- # alpha = 1.0
613
-
614
- # r_s = wp.sqrt(dot(col1, col1) + dot(col2, col2) + dot(col3, col3))
615
- # r_s_inv = 1.0/r_s
616
-
617
- # C = r_s - wp.sqrt(3.0)
618
- # dCdx = F*wp.transpose(Dm)*r_s_inv
619
-
620
- # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
621
- # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
622
- # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
623
-
624
- # f1 = grad1*C*k_mu
625
- # f2 = grad2*C*k_mu
626
- # f3 = grad3*C*k_mu
627
-
628
- # ----------------------------
629
- # C_D
630
-
631
- # alpha = 1.0
632
-
633
- # r_s = wp.sqrt(dot(col1, col1) + dot(col2, col2) + dot(col3, col3))
634
-
635
- # C = r_s*r_s - 3.0
636
- # dCdx = F*wp.transpose(Dm)*2.0
637
-
638
- # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
639
- # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
640
- # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
641
-
642
- # f1 = grad1*C*k_mu
643
- # f2 = grad2*C*k_mu
644
- # f3 = grad3*C*k_mu
645
-
646
- # ----------------------------
647
- # Hookean
648
-
649
- # alpha = 1.0
650
-
651
- # I = wp.mat33(wp.vec3(1.0, 0.0, 0.0),
652
- # wp.vec3(0.0, 1.0, 0.0),
653
- # wp.vec3(0.0, 0.0, 1.0))
654
-
655
- # P = (F + wp.transpose(F) + I*(0.0-2.0))*k_mu
656
- # H = P * wp.transpose(Dm)
657
-
658
- # f1 = wp.vec3(H[0, 0], H[1, 0], H[2, 0])
659
- # f2 = wp.vec3(H[0, 1], H[1, 1], H[2, 1])
660
- # f3 = wp.vec3(H[0, 2], H[1, 2], H[2, 2])
661
-
662
- # hydrostatic part
663
- J = wp.determinant(F)
664
-
665
- # print(J)
666
- s = inv_rest_volume / 6.0
667
- dJdx1 = wp.cross(x20, x30) * s
668
- dJdx2 = wp.cross(x30, x10) * s
669
- dJdx3 = wp.cross(x10, x20) * s
670
-
671
- f_volume = (J - alpha + act) * k_lambda
672
- f_damp = (wp.dot(dJdx1, v1) + wp.dot(dJdx2, v2) + wp.dot(dJdx3, v3)) * k_damp
673
-
674
- f_total = f_volume + f_damp
675
-
676
- f1 = f1 + dJdx1 * f_total
677
- f2 = f2 + dJdx2 * f_total
678
- f3 = f3 + dJdx3 * f_total
679
- f0 = -(f1 + f2 + f3)
680
-
681
- # apply forces
682
- wp.atomic_sub(f, i, f0)
683
- wp.atomic_sub(f, j, f1)
684
- wp.atomic_sub(f, k, f2)
685
- wp.atomic_sub(f, l, f3)
686
-
687
-
688
- @wp.kernel
689
- def eval_particle_ground_contacts(
690
- particle_x: wp.array(dtype=wp.vec3),
691
- particle_v: wp.array(dtype=wp.vec3),
692
- particle_radius: wp.array(dtype=float),
693
- particle_flags: wp.array(dtype=wp.uint32),
694
- ke: float,
695
- kd: float,
696
- kf: float,
697
- mu: float,
698
- ground: wp.array(dtype=float),
699
- # outputs
700
- f: wp.array(dtype=wp.vec3),
701
- ):
702
- tid = wp.tid()
703
- if (particle_flags[tid] & PARTICLE_FLAG_ACTIVE) == 0:
704
- return
705
-
706
- x = particle_x[tid]
707
- v = particle_v[tid]
708
- radius = particle_radius[tid]
709
-
710
- n = wp.vec3(ground[0], ground[1], ground[2])
711
- c = wp.min(wp.dot(n, x) + ground[3] - radius, 0.0)
712
-
713
- vn = wp.dot(n, v)
714
- jn = c * ke
715
-
716
- if c >= 0.0:
717
- return
718
-
719
- jd = min(vn, 0.0) * kd
720
-
721
- # contact force
722
- fn = jn + jd
723
-
724
- # friction force
725
- vt = v - n * vn
726
- vs = wp.length(vt)
727
-
728
- if vs > 0.0:
729
- vt = vt / vs
730
-
731
- # Coulomb condition
732
- ft = wp.min(vs * kf, mu * wp.abs(fn))
733
-
734
- # total force
735
- f[tid] = f[tid] - n * fn - vt * ft
736
-
737
-
738
- @wp.kernel
739
- def eval_particle_contacts(
740
- particle_x: wp.array(dtype=wp.vec3),
741
- particle_v: wp.array(dtype=wp.vec3),
742
- body_q: wp.array(dtype=wp.transform),
743
- body_qd: wp.array(dtype=wp.spatial_vector),
744
- particle_radius: wp.array(dtype=float),
745
- particle_flags: wp.array(dtype=wp.uint32),
746
- body_com: wp.array(dtype=wp.vec3),
747
- shape_body: wp.array(dtype=int),
748
- shape_materials: ModelShapeMaterials,
749
- particle_ke: float,
750
- particle_kd: float,
751
- particle_kf: float,
752
- particle_mu: float,
753
- particle_ka: float,
754
- contact_count: wp.array(dtype=int),
755
- contact_particle: wp.array(dtype=int),
756
- contact_shape: wp.array(dtype=int),
757
- contact_body_pos: wp.array(dtype=wp.vec3),
758
- contact_body_vel: wp.array(dtype=wp.vec3),
759
- contact_normal: wp.array(dtype=wp.vec3),
760
- contact_max: int,
761
- # outputs
762
- particle_f: wp.array(dtype=wp.vec3),
763
- body_f: wp.array(dtype=wp.spatial_vector),
764
- ):
765
- tid = wp.tid()
766
-
767
- count = min(contact_max, contact_count[0])
768
- if tid >= count:
769
- return
770
-
771
- shape_index = contact_shape[tid]
772
- body_index = shape_body[shape_index]
773
- particle_index = contact_particle[tid]
774
- if (particle_flags[particle_index] & PARTICLE_FLAG_ACTIVE) == 0:
775
- return
776
-
777
- px = particle_x[particle_index]
778
- pv = particle_v[particle_index]
779
-
780
- X_wb = wp.transform_identity()
781
- X_com = wp.vec3()
782
- body_v_s = wp.spatial_vector()
783
-
784
- if body_index >= 0:
785
- X_wb = body_q[body_index]
786
- X_com = body_com[body_index]
787
- body_v_s = body_qd[body_index]
788
-
789
- # body position in world space
790
- bx = wp.transform_point(X_wb, contact_body_pos[tid])
791
- r = bx - wp.transform_point(X_wb, X_com)
792
-
793
- n = contact_normal[tid]
794
- c = wp.dot(n, px - bx) - particle_radius[tid]
795
-
796
- if c > particle_ka:
797
- return
798
-
799
- # take average material properties of shape and particle parameters
800
- ke = 0.5 * (particle_ke + shape_materials.ke[shape_index])
801
- kd = 0.5 * (particle_kd + shape_materials.kd[shape_index])
802
- kf = 0.5 * (particle_kf + shape_materials.kf[shape_index])
803
- mu = 0.5 * (particle_mu + shape_materials.mu[shape_index])
804
-
805
- body_w = wp.spatial_top(body_v_s)
806
- body_v = wp.spatial_bottom(body_v_s)
807
-
808
- # compute the body velocity at the particle position
809
- bv = body_v + wp.cross(body_w, r) + wp.transform_vector(X_wb, contact_body_vel[tid])
810
-
811
- # relative velocity
812
- v = pv - bv
813
-
814
- # decompose relative velocity
815
- vn = wp.dot(n, v)
816
- vt = v - n * vn
817
-
818
- # contact elastic
819
- fn = n * c * ke
820
-
821
- # contact damping
822
- fd = n * wp.min(vn, 0.0) * kd
823
-
824
- # viscous friction
825
- # ft = vt*kf
826
-
827
- # Coulomb friction (box)
828
- # lower = mu * c * ke
829
- # upper = -lower
830
-
831
- # vx = wp.clamp(wp.dot(wp.vec3(kf, 0.0, 0.0), vt), lower, upper)
832
- # vz = wp.clamp(wp.dot(wp.vec3(0.0, 0.0, kf), vt), lower, upper)
833
-
834
- # ft = wp.vec3(vx, 0.0, vz)
835
-
836
- # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
837
- ft = wp.normalize(vt) * wp.min(kf * wp.length(vt), abs(mu * c * ke))
838
-
839
- f_total = fn + (fd + ft)
840
- t_total = wp.cross(r, f_total)
841
-
842
- wp.atomic_sub(particle_f, particle_index, f_total)
843
-
844
- if body_index >= 0:
845
- wp.atomic_add(body_f, body_index, wp.spatial_vector(t_total, f_total))
846
-
847
-
848
- @wp.kernel
849
- def eval_rigid_contacts(
850
- body_q: wp.array(dtype=wp.transform),
851
- body_qd: wp.array(dtype=wp.spatial_vector),
852
- body_com: wp.array(dtype=wp.vec3),
853
- shape_materials: ModelShapeMaterials,
854
- geo: ModelShapeGeometry,
855
- shape_body: wp.array(dtype=int),
856
- contact_count: wp.array(dtype=int),
857
- contact_point0: wp.array(dtype=wp.vec3),
858
- contact_point1: wp.array(dtype=wp.vec3),
859
- contact_normal: wp.array(dtype=wp.vec3),
860
- contact_shape0: wp.array(dtype=int),
861
- contact_shape1: wp.array(dtype=int),
862
- force_in_world_frame: bool,
863
- # outputs
864
- body_f: wp.array(dtype=wp.spatial_vector),
865
- ):
866
- tid = wp.tid()
867
-
868
- count = contact_count[0]
869
- if tid >= count:
870
- return
871
-
872
- # retrieve contact thickness, compute average contact material properties
873
- ke = 0.0 # contact normal force stiffness
874
- kd = 0.0 # damping coefficient
875
- kf = 0.0 # friction force stiffness
876
- ka = 0.0 # adhesion distance
877
- mu = 0.0 # friction coefficient
878
- mat_nonzero = 0
879
- thickness_a = 0.0
880
- thickness_b = 0.0
881
- shape_a = contact_shape0[tid]
882
- shape_b = contact_shape1[tid]
883
- if shape_a == shape_b:
884
- return
885
- body_a = -1
886
- body_b = -1
887
- if shape_a >= 0:
888
- mat_nonzero += 1
889
- ke += shape_materials.ke[shape_a]
890
- kd += shape_materials.kd[shape_a]
891
- kf += shape_materials.kf[shape_a]
892
- ka += shape_materials.ka[shape_a]
893
- mu += shape_materials.mu[shape_a]
894
- thickness_a = geo.thickness[shape_a]
895
- body_a = shape_body[shape_a]
896
- if shape_b >= 0:
897
- mat_nonzero += 1
898
- ke += shape_materials.ke[shape_b]
899
- kd += shape_materials.kd[shape_b]
900
- kf += shape_materials.kf[shape_b]
901
- ka += shape_materials.ka[shape_b]
902
- mu += shape_materials.mu[shape_b]
903
- thickness_b = geo.thickness[shape_b]
904
- body_b = shape_body[shape_b]
905
- if mat_nonzero > 0:
906
- ke /= float(mat_nonzero)
907
- kd /= float(mat_nonzero)
908
- kf /= float(mat_nonzero)
909
- ka /= float(mat_nonzero)
910
- mu /= float(mat_nonzero)
911
-
912
- # contact normal in world space
913
- n = contact_normal[tid]
914
- bx_a = contact_point0[tid]
915
- bx_b = contact_point1[tid]
916
- if body_a >= 0:
917
- X_wb_a = body_q[body_a]
918
- X_com_a = body_com[body_a]
919
- bx_a = wp.transform_point(X_wb_a, bx_a) - thickness_a * n
920
- r_a = bx_a - wp.transform_point(X_wb_a, X_com_a)
921
-
922
- if body_b >= 0:
923
- X_wb_b = body_q[body_b]
924
- X_com_b = body_com[body_b]
925
- bx_b = wp.transform_point(X_wb_b, bx_b) + thickness_b * n
926
- r_b = bx_b - wp.transform_point(X_wb_b, X_com_b)
927
-
928
- d = wp.dot(n, bx_a - bx_b)
929
-
930
- if d >= ka:
931
- return
932
-
933
- # compute contact point velocity
934
- bv_a = wp.vec3(0.0)
935
- bv_b = wp.vec3(0.0)
936
- if body_a >= 0:
937
- body_v_s_a = body_qd[body_a]
938
- body_w_a = wp.spatial_top(body_v_s_a)
939
- body_v_a = wp.spatial_bottom(body_v_s_a)
940
- if force_in_world_frame:
941
- bv_a = body_v_a + wp.cross(body_w_a, bx_a)
942
- else:
943
- bv_a = body_v_a + wp.cross(body_w_a, r_a)
944
-
945
- if body_b >= 0:
946
- body_v_s_b = body_qd[body_b]
947
- body_w_b = wp.spatial_top(body_v_s_b)
948
- body_v_b = wp.spatial_bottom(body_v_s_b)
949
- if force_in_world_frame:
950
- bv_b = body_v_b + wp.cross(body_w_b, bx_b)
951
- else:
952
- bv_b = body_v_b + wp.cross(body_w_b, r_b)
953
-
954
- # relative velocity
955
- v = bv_a - bv_b
956
-
957
- # print(v)
958
-
959
- # decompose relative velocity
960
- vn = wp.dot(n, v)
961
- vt = v - n * vn
962
-
963
- # contact elastic
964
- fn = d * ke
965
-
966
- # contact damping
967
- fd = wp.min(vn, 0.0) * kd * wp.step(d)
968
-
969
- # viscous friction
970
- # ft = vt*kf
971
-
972
- # Coulomb friction (box)
973
- # lower = mu * d * ke
974
- # upper = -lower
975
-
976
- # vx = wp.clamp(wp.dot(wp.vec3(kf, 0.0, 0.0), vt), lower, upper)
977
- # vz = wp.clamp(wp.dot(wp.vec3(0.0, 0.0, kf), vt), lower, upper)
978
-
979
- # ft = wp.vec3(vx, 0.0, vz)
980
-
981
- # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
982
- # ft = wp.normalize(vt)*wp.min(kf*wp.length(vt), abs(mu*d*ke))
983
- ft = wp.vec3(0.0)
984
- if d < 0.0:
985
- ft = wp.normalize(vt) * wp.min(kf * wp.length(vt), -mu * (fn + fd))
986
-
987
- f_total = n * (fn + fd) + ft
988
- # f_total = n * fn
989
-
990
- if body_a >= 0:
991
- if force_in_world_frame:
992
- wp.atomic_add(body_f, body_a, wp.spatial_vector(wp.cross(bx_a, f_total), f_total))
993
- else:
994
- wp.atomic_sub(body_f, body_a, wp.spatial_vector(wp.cross(r_a, f_total), f_total))
995
-
996
- if body_b >= 0:
997
- if force_in_world_frame:
998
- wp.atomic_sub(body_f, body_b, wp.spatial_vector(wp.cross(bx_b, f_total), f_total))
999
- else:
1000
- wp.atomic_add(body_f, body_b, wp.spatial_vector(wp.cross(r_b, f_total), f_total))
1001
-
1002
-
1003
- @wp.func
1004
- def eval_joint_force(
1005
- q: float,
1006
- qd: float,
1007
- act: float,
1008
- target_ke: float,
1009
- target_kd: float,
1010
- limit_lower: float,
1011
- limit_upper: float,
1012
- limit_ke: float,
1013
- limit_kd: float,
1014
- mode: wp.int32,
1015
- ) -> float:
1016
- """Joint force evaluation for a single degree of freedom."""
1017
-
1018
- limit_f = 0.0
1019
- damping_f = 0.0
1020
- target_f = 0.0
1021
-
1022
- if mode == wp.sim.JOINT_MODE_FORCE:
1023
- target_f = act
1024
- elif mode == wp.sim.JOINT_MODE_TARGET_POSITION:
1025
- target_f = target_ke * (act - q) - target_kd * qd
1026
- elif mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1027
- target_f = target_ke * (act - qd)
1028
-
1029
- # compute limit forces, damping only active when limit is violated
1030
- if q < limit_lower:
1031
- limit_f = limit_ke * (limit_lower - q)
1032
- damping_f = -limit_kd * qd
1033
- if mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1034
- target_f = 0.0 # override target force when limit is violated
1035
- elif q > limit_upper:
1036
- limit_f = limit_ke * (limit_upper - q)
1037
- damping_f = -limit_kd * qd
1038
- if mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1039
- target_f = 0.0 # override target force when limit is violated
1040
-
1041
- return limit_f + damping_f + target_f
1042
-
1043
-
1044
- @wp.kernel
1045
- def eval_body_joints(
1046
- body_q: wp.array(dtype=wp.transform),
1047
- body_qd: wp.array(dtype=wp.spatial_vector),
1048
- body_com: wp.array(dtype=wp.vec3),
1049
- joint_qd_start: wp.array(dtype=int),
1050
- joint_type: wp.array(dtype=int),
1051
- joint_enabled: wp.array(dtype=int),
1052
- joint_child: wp.array(dtype=int),
1053
- joint_parent: wp.array(dtype=int),
1054
- joint_X_p: wp.array(dtype=wp.transform),
1055
- joint_X_c: wp.array(dtype=wp.transform),
1056
- joint_axis: wp.array(dtype=wp.vec3),
1057
- joint_axis_start: wp.array(dtype=int),
1058
- joint_axis_dim: wp.array(dtype=int, ndim=2),
1059
- joint_axis_mode: wp.array(dtype=int),
1060
- joint_act: wp.array(dtype=float),
1061
- joint_target_ke: wp.array(dtype=float),
1062
- joint_target_kd: wp.array(dtype=float),
1063
- joint_limit_lower: wp.array(dtype=float),
1064
- joint_limit_upper: wp.array(dtype=float),
1065
- joint_limit_ke: wp.array(dtype=float),
1066
- joint_limit_kd: wp.array(dtype=float),
1067
- joint_attach_ke: float,
1068
- joint_attach_kd: float,
1069
- body_f: wp.array(dtype=wp.spatial_vector),
1070
- ):
1071
- tid = wp.tid()
1072
- type = joint_type[tid]
1073
-
1074
- # early out for free joints
1075
- if joint_enabled[tid] == 0 or type == wp.sim.JOINT_FREE:
1076
- return
1077
-
1078
- c_child = joint_child[tid]
1079
- c_parent = joint_parent[tid]
1080
-
1081
- X_pj = joint_X_p[tid]
1082
- X_cj = joint_X_c[tid]
1083
-
1084
- X_wp = X_pj
1085
- r_p = wp.vec3()
1086
- w_p = wp.vec3()
1087
- v_p = wp.vec3()
1088
-
1089
- # parent transform and moment arm
1090
- if c_parent >= 0:
1091
- X_wp = body_q[c_parent] * X_wp
1092
- r_p = wp.transform_get_translation(X_wp) - wp.transform_point(body_q[c_parent], body_com[c_parent])
1093
-
1094
- twist_p = body_qd[c_parent]
1095
-
1096
- w_p = wp.spatial_top(twist_p)
1097
- v_p = wp.spatial_bottom(twist_p) + wp.cross(w_p, r_p)
1098
-
1099
- # child transform and moment arm
1100
- X_wc = body_q[c_child] * X_cj
1101
- r_c = wp.transform_get_translation(X_wc) - wp.transform_point(body_q[c_child], body_com[c_child])
1102
-
1103
- twist_c = body_qd[c_child]
1104
-
1105
- w_c = wp.spatial_top(twist_c)
1106
- v_c = wp.spatial_bottom(twist_c) + wp.cross(w_c, r_c)
1107
-
1108
- # joint properties (for 1D joints)
1109
- # q_start = joint_q_start[tid]
1110
- qd_start = joint_qd_start[tid]
1111
- axis_start = joint_axis_start[tid]
1112
-
1113
- lin_axis_count = joint_axis_dim[tid, 0]
1114
- ang_axis_count = joint_axis_dim[tid, 1]
1115
-
1116
- x_p = wp.transform_get_translation(X_wp)
1117
- x_c = wp.transform_get_translation(X_wc)
1118
-
1119
- q_p = wp.transform_get_rotation(X_wp)
1120
- q_c = wp.transform_get_rotation(X_wc)
1121
-
1122
- # translational error
1123
- x_err = x_c - x_p
1124
- r_err = wp.quat_inverse(q_p) * q_c
1125
- v_err = v_c - v_p
1126
- w_err = w_c - w_p
1127
-
1128
- # total force/torque on the parent
1129
- t_total = wp.vec3()
1130
- f_total = wp.vec3()
1131
-
1132
- # reduce angular damping stiffness for stability
1133
- angular_damping_scale = 0.01
1134
-
1135
- if type == wp.sim.JOINT_FIXED:
1136
- ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1137
-
1138
- f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1139
- t_total += (
1140
- wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1141
- )
1142
-
1143
- if type == wp.sim.JOINT_PRISMATIC:
1144
- axis = joint_axis[axis_start]
1145
-
1146
- # world space joint axis
1147
- axis_p = wp.transform_vector(X_wp, axis)
1148
-
1149
- # evaluate joint coordinates
1150
- q = wp.dot(x_err, axis_p)
1151
- qd = wp.dot(v_err, axis_p)
1152
- act = joint_act[axis_start]
1153
-
1154
- f_total = axis_p * -eval_joint_force(
1155
- q,
1156
- qd,
1157
- act,
1158
- joint_target_ke[axis_start],
1159
- joint_target_kd[axis_start],
1160
- joint_limit_lower[axis_start],
1161
- joint_limit_upper[axis_start],
1162
- joint_limit_ke[axis_start],
1163
- joint_limit_kd[axis_start],
1164
- joint_axis_mode[axis_start],
1165
- )
1166
-
1167
- # attachment dynamics
1168
- ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1169
-
1170
- # project off any displacement along the joint axis
1171
- f_total += (x_err - q * axis_p) * joint_attach_ke + (v_err - qd * axis_p) * joint_attach_kd
1172
- t_total += (
1173
- wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1174
- )
1175
-
1176
- if type == wp.sim.JOINT_REVOLUTE:
1177
- axis = joint_axis[axis_start]
1178
-
1179
- axis_p = wp.transform_vector(X_wp, axis)
1180
- axis_c = wp.transform_vector(X_wc, axis)
1181
-
1182
- # swing twist decomposition
1183
- twist = quat_twist(axis, r_err)
1184
-
1185
- q = wp.acos(twist[3]) * 2.0 * wp.sign(wp.dot(axis, wp.vec3(twist[0], twist[1], twist[2])))
1186
- qd = wp.dot(w_err, axis_p)
1187
- act = joint_act[axis_start]
1188
-
1189
- t_total = axis_p * -eval_joint_force(
1190
- q,
1191
- qd,
1192
- act,
1193
- joint_target_ke[axis_start],
1194
- joint_target_kd[axis_start],
1195
- joint_limit_lower[axis_start],
1196
- joint_limit_upper[axis_start],
1197
- joint_limit_ke[axis_start],
1198
- joint_limit_kd[axis_start],
1199
- joint_axis_mode[axis_start],
1200
- )
1201
-
1202
- # attachment dynamics
1203
- swing_err = wp.cross(axis_p, axis_c)
1204
-
1205
- f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1206
- t_total += swing_err * joint_attach_ke + (w_err - qd * axis_p) * joint_attach_kd * angular_damping_scale
1207
-
1208
- if type == wp.sim.JOINT_BALL:
1209
- ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1210
-
1211
- # TODO joint limits
1212
- # TODO expose target_kd or target_ke for ball joints
1213
- # t_total += target_kd * w_err + target_ke * wp.transform_vector(X_wp, ang_err)
1214
- f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1215
-
1216
- if type == wp.sim.JOINT_COMPOUND:
1217
- q_pc = wp.quat_inverse(q_p) * q_c
1218
-
1219
- # decompose to a compound rotation each axis
1220
- angles = quat_decompose(q_pc)
1221
-
1222
- # reconstruct rotation axes
1223
- axis_0 = wp.vec3(1.0, 0.0, 0.0)
1224
- q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1225
-
1226
- axis_1 = wp.quat_rotate(q_0, wp.vec3(0.0, 1.0, 0.0))
1227
- q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1228
-
1229
- axis_2 = wp.quat_rotate(q_1 * q_0, wp.vec3(0.0, 0.0, 1.0))
1230
- # q_2 = wp.quat_from_axis_angle(axis_2, angles[2])
1231
-
1232
- # q_w = q_p
1233
-
1234
- axis_0 = wp.transform_vector(X_wp, axis_0)
1235
- axis_1 = wp.transform_vector(X_wp, axis_1)
1236
- axis_2 = wp.transform_vector(X_wp, axis_2)
1237
-
1238
- # joint dynamics
1239
- # # TODO remove wp.quat_rotate(q_w, ...)?
1240
- # t_total += eval_joint_force(angles[0], wp.dot(wp.quat_rotate(q_w, axis_0), w_err), joint_target[axis_start+0], joint_target_ke[axis_start+0],joint_target_kd[axis_start+0], joint_act[axis_start+0], joint_limit_lower[axis_start+0], joint_limit_upper[axis_start+0], joint_limit_ke[axis_start+0], joint_limit_kd[axis_start+0], wp.quat_rotate(q_w, axis_0))
1241
- # t_total += eval_joint_force(angles[1], wp.dot(wp.quat_rotate(q_w, axis_1), w_err), joint_target[axis_start+1], joint_target_ke[axis_start+1],joint_target_kd[axis_start+1], joint_act[axis_start+1], joint_limit_lower[axis_start+1], joint_limit_upper[axis_start+1], joint_limit_ke[axis_start+1], joint_limit_kd[axis_start+1], wp.quat_rotate(q_w, axis_1))
1242
- # t_total += eval_joint_force(angles[2], wp.dot(wp.quat_rotate(q_w, axis_2), w_err), joint_target[axis_start+2], joint_target_ke[axis_start+2],joint_target_kd[axis_start+2], joint_act[axis_start+2], joint_limit_lower[axis_start+2], joint_limit_upper[axis_start+2], joint_limit_ke[axis_start+2], joint_limit_kd[axis_start+2], wp.quat_rotate(q_w, axis_2))
1243
-
1244
- t_total += axis_0 * -eval_joint_force(
1245
- angles[0],
1246
- wp.dot(axis_0, w_err),
1247
- joint_act[axis_start + 0],
1248
- joint_target_ke[axis_start + 0],
1249
- joint_target_kd[axis_start + 0],
1250
- joint_limit_lower[axis_start + 0],
1251
- joint_limit_upper[axis_start + 0],
1252
- joint_limit_ke[axis_start + 0],
1253
- joint_limit_kd[axis_start + 0],
1254
- joint_axis_mode[axis_start + 0],
1255
- )
1256
- t_total += axis_1 * -eval_joint_force(
1257
- angles[1],
1258
- wp.dot(axis_1, w_err),
1259
- joint_act[axis_start + 1],
1260
- joint_target_ke[axis_start + 1],
1261
- joint_target_kd[axis_start + 1],
1262
- joint_limit_lower[axis_start + 1],
1263
- joint_limit_upper[axis_start + 1],
1264
- joint_limit_ke[axis_start + 1],
1265
- joint_limit_kd[axis_start + 1],
1266
- joint_axis_mode[axis_start + 1],
1267
- )
1268
- t_total += axis_2 * -eval_joint_force(
1269
- angles[2],
1270
- wp.dot(axis_2, w_err),
1271
- joint_act[axis_start + 2],
1272
- joint_target_ke[axis_start + 2],
1273
- joint_target_kd[axis_start + 2],
1274
- joint_limit_lower[axis_start + 2],
1275
- joint_limit_upper[axis_start + 2],
1276
- joint_limit_ke[axis_start + 2],
1277
- joint_limit_kd[axis_start + 2],
1278
- joint_axis_mode[axis_start + 2],
1279
- )
1280
-
1281
- f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1282
-
1283
- if type == wp.sim.JOINT_UNIVERSAL:
1284
- q_pc = wp.quat_inverse(q_p) * q_c
1285
-
1286
- # decompose to a compound rotation each axis
1287
- angles = quat_decompose(q_pc)
1288
-
1289
- # reconstruct rotation axes
1290
- axis_0 = wp.vec3(1.0, 0.0, 0.0)
1291
- q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1292
-
1293
- axis_1 = wp.quat_rotate(q_0, wp.vec3(0.0, 1.0, 0.0))
1294
- q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1295
-
1296
- axis_2 = wp.quat_rotate(q_1 * q_0, wp.vec3(0.0, 0.0, 1.0))
1297
-
1298
- axis_0 = wp.transform_vector(X_wp, axis_0)
1299
- axis_1 = wp.transform_vector(X_wp, axis_1)
1300
- axis_2 = wp.transform_vector(X_wp, axis_2)
1301
-
1302
- # joint dynamics
1303
-
1304
- t_total += axis_0 * -eval_joint_force(
1305
- angles[0],
1306
- wp.dot(axis_0, w_err),
1307
- joint_act[axis_start + 0],
1308
- joint_target_ke[axis_start + 0],
1309
- joint_target_kd[axis_start + 0],
1310
- joint_limit_lower[axis_start + 0],
1311
- joint_limit_upper[axis_start + 0],
1312
- joint_limit_ke[axis_start + 0],
1313
- joint_limit_kd[axis_start + 0],
1314
- joint_axis_mode[axis_start + 0],
1315
- )
1316
- t_total += axis_1 * -eval_joint_force(
1317
- angles[1],
1318
- wp.dot(axis_1, w_err),
1319
- joint_act[axis_start + 1],
1320
- joint_target_ke[axis_start + 1],
1321
- joint_target_kd[axis_start + 1],
1322
- joint_limit_lower[axis_start + 1],
1323
- joint_limit_upper[axis_start + 1],
1324
- joint_limit_ke[axis_start + 1],
1325
- joint_limit_kd[axis_start + 1],
1326
- joint_axis_mode[axis_start + 1],
1327
- )
1328
-
1329
- # last axis (fixed)
1330
- t_total += axis_2 * -eval_joint_force(
1331
- angles[2],
1332
- wp.dot(axis_2, w_err),
1333
- 0.0,
1334
- joint_attach_ke,
1335
- joint_attach_kd * angular_damping_scale,
1336
- 0.0,
1337
- 0.0,
1338
- 0.0,
1339
- 0.0,
1340
- wp.sim.JOINT_MODE_FORCE,
1341
- )
1342
-
1343
- f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1344
-
1345
- if type == wp.sim.JOINT_D6:
1346
- pos = wp.vec3(0.0)
1347
- vel = wp.vec3(0.0)
1348
- if lin_axis_count >= 1:
1349
- axis_0 = wp.transform_vector(X_wp, joint_axis[axis_start + 0])
1350
- q0 = wp.dot(x_err, axis_0)
1351
- qd0 = wp.dot(v_err, axis_0)
1352
-
1353
- f_total += axis_0 * -eval_joint_force(
1354
- q0,
1355
- qd0,
1356
- joint_act[axis_start + 0],
1357
- joint_target_ke[axis_start + 0],
1358
- joint_target_kd[axis_start + 0],
1359
- joint_limit_lower[axis_start + 0],
1360
- joint_limit_upper[axis_start + 0],
1361
- joint_limit_ke[axis_start + 0],
1362
- joint_limit_kd[axis_start + 0],
1363
- joint_axis_mode[axis_start + 0],
1364
- )
1365
-
1366
- pos += q0 * axis_0
1367
- vel += qd0 * axis_0
1368
-
1369
- if lin_axis_count >= 2:
1370
- axis_1 = wp.transform_vector(X_wp, joint_axis[axis_start + 1])
1371
- q1 = wp.dot(x_err, axis_1)
1372
- qd1 = wp.dot(v_err, axis_1)
1373
-
1374
- f_total += axis_1 * -eval_joint_force(
1375
- q1,
1376
- qd1,
1377
- joint_act[axis_start + 1],
1378
- joint_target_ke[axis_start + 1],
1379
- joint_target_kd[axis_start + 1],
1380
- joint_limit_lower[axis_start + 1],
1381
- joint_limit_upper[axis_start + 1],
1382
- joint_limit_ke[axis_start + 1],
1383
- joint_limit_kd[axis_start + 1],
1384
- joint_axis_mode[axis_start + 1],
1385
- )
1386
-
1387
- pos += q1 * axis_1
1388
- vel += qd1 * axis_1
1389
-
1390
- if lin_axis_count == 3:
1391
- axis_2 = wp.transform_vector(X_wp, joint_axis[axis_start + 2])
1392
- q2 = wp.dot(x_err, axis_2)
1393
- qd2 = wp.dot(v_err, axis_2)
1394
-
1395
- f_total += axis_2 * -eval_joint_force(
1396
- q2,
1397
- qd2,
1398
- joint_act[axis_start + 2],
1399
- joint_target_ke[axis_start + 2],
1400
- joint_target_kd[axis_start + 2],
1401
- joint_limit_lower[axis_start + 2],
1402
- joint_limit_upper[axis_start + 2],
1403
- joint_limit_ke[axis_start + 2],
1404
- joint_limit_kd[axis_start + 2],
1405
- joint_axis_mode[axis_start + 2],
1406
- )
1407
-
1408
- pos += q2 * axis_2
1409
- vel += qd2 * axis_2
1410
-
1411
- f_total += (x_err - pos) * joint_attach_ke + (v_err - vel) * joint_attach_kd
1412
-
1413
- if ang_axis_count == 0:
1414
- ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1415
- t_total += (
1416
- wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1417
- )
1418
-
1419
- i_0 = lin_axis_count + axis_start + 0
1420
- i_1 = lin_axis_count + axis_start + 1
1421
- i_2 = lin_axis_count + axis_start + 2
1422
-
1423
- if ang_axis_count == 1:
1424
- axis = joint_axis[i_0]
1425
-
1426
- axis_p = wp.transform_vector(X_wp, axis)
1427
- axis_c = wp.transform_vector(X_wc, axis)
1428
-
1429
- # swing twist decomposition
1430
- twist = quat_twist(axis, r_err)
1431
-
1432
- q = wp.acos(twist[3]) * 2.0 * wp.sign(wp.dot(axis, wp.vec3(twist[0], twist[1], twist[2])))
1433
- qd = wp.dot(w_err, axis_p)
1434
-
1435
- t_total = axis_p * -eval_joint_force(
1436
- q,
1437
- qd,
1438
- joint_act[i_0],
1439
- joint_target_ke[i_0],
1440
- joint_target_kd[i_0],
1441
- joint_limit_lower[i_0],
1442
- joint_limit_upper[i_0],
1443
- joint_limit_ke[i_0],
1444
- joint_limit_kd[i_0],
1445
- joint_axis_mode[i_0],
1446
- )
1447
-
1448
- # attachment dynamics
1449
- swing_err = wp.cross(axis_p, axis_c)
1450
-
1451
- t_total += swing_err * joint_attach_ke + (w_err - qd * axis_p) * joint_attach_kd * angular_damping_scale
1452
-
1453
- if ang_axis_count == 2:
1454
- q_pc = wp.quat_inverse(q_p) * q_c
1455
-
1456
- # decompose to a compound rotation each axis
1457
- angles = quat_decompose(q_pc)
1458
-
1459
- orig_axis_0 = joint_axis[i_0]
1460
- orig_axis_1 = joint_axis[i_1]
1461
- orig_axis_2 = wp.cross(orig_axis_0, orig_axis_1)
1462
-
1463
- # reconstruct rotation axes
1464
- axis_0 = orig_axis_0
1465
- q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1466
-
1467
- axis_1 = wp.quat_rotate(q_0, orig_axis_1)
1468
- q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1469
-
1470
- axis_2 = wp.quat_rotate(q_1 * q_0, orig_axis_2)
1471
-
1472
- axis_0 = wp.transform_vector(X_wp, axis_0)
1473
- axis_1 = wp.transform_vector(X_wp, axis_1)
1474
- axis_2 = wp.transform_vector(X_wp, axis_2)
1475
-
1476
- # joint dynamics
1477
-
1478
- t_total += axis_0 * -eval_joint_force(
1479
- angles[0],
1480
- wp.dot(axis_0, w_err),
1481
- joint_act[i_0],
1482
- joint_target_ke[i_0],
1483
- joint_target_kd[i_0],
1484
- joint_limit_lower[i_0],
1485
- joint_limit_upper[i_0],
1486
- joint_limit_ke[i_0],
1487
- joint_limit_kd[i_0],
1488
- joint_axis_mode[i_0],
1489
- )
1490
- t_total += axis_1 * -eval_joint_force(
1491
- angles[1],
1492
- wp.dot(axis_1, w_err),
1493
- joint_act[i_1],
1494
- joint_target_ke[i_1],
1495
- joint_target_kd[i_1],
1496
- joint_limit_lower[i_1],
1497
- joint_limit_upper[i_1],
1498
- joint_limit_ke[i_1],
1499
- joint_limit_kd[i_1],
1500
- joint_axis_mode[i_1],
1501
- )
1502
-
1503
- # last axis (fixed)
1504
- t_total += axis_2 * -eval_joint_force(
1505
- angles[2],
1506
- wp.dot(axis_2, w_err),
1507
- 0.0,
1508
- joint_attach_ke,
1509
- joint_attach_kd * angular_damping_scale,
1510
- 0.0,
1511
- 0.0,
1512
- 0.0,
1513
- 0.0,
1514
- wp.sim.JOINT_MODE_FORCE,
1515
- )
1516
-
1517
- if ang_axis_count == 3:
1518
- q_pc = wp.quat_inverse(q_p) * q_c
1519
-
1520
- # decompose to a compound rotation each axis
1521
- angles = quat_decompose(q_pc)
1522
-
1523
- orig_axis_0 = joint_axis[i_0]
1524
- orig_axis_1 = joint_axis[i_1]
1525
- orig_axis_2 = joint_axis[i_2]
1526
-
1527
- # reconstruct rotation axes
1528
- axis_0 = orig_axis_0
1529
- q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1530
-
1531
- axis_1 = wp.quat_rotate(q_0, orig_axis_1)
1532
- q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1533
-
1534
- axis_2 = wp.quat_rotate(q_1 * q_0, orig_axis_2)
1535
-
1536
- axis_0 = wp.transform_vector(X_wp, axis_0)
1537
- axis_1 = wp.transform_vector(X_wp, axis_1)
1538
- axis_2 = wp.transform_vector(X_wp, axis_2)
1539
-
1540
- t_total += axis_0 * -eval_joint_force(
1541
- angles[0],
1542
- wp.dot(axis_0, w_err),
1543
- joint_act[i_0],
1544
- joint_target_ke[i_0],
1545
- joint_target_kd[i_0],
1546
- joint_limit_lower[i_0],
1547
- joint_limit_upper[i_0],
1548
- joint_limit_ke[i_0],
1549
- joint_limit_kd[i_0],
1550
- joint_axis_mode[i_0],
1551
- )
1552
- t_total += axis_1 * -eval_joint_force(
1553
- angles[1],
1554
- wp.dot(axis_1, w_err),
1555
- joint_act[i_1],
1556
- joint_target_ke[i_1],
1557
- joint_target_kd[i_1],
1558
- joint_limit_lower[i_1],
1559
- joint_limit_upper[i_1],
1560
- joint_limit_ke[i_1],
1561
- joint_limit_kd[i_1],
1562
- joint_axis_mode[i_1],
1563
- )
1564
- t_total += axis_2 * -eval_joint_force(
1565
- angles[2],
1566
- wp.dot(axis_2, w_err),
1567
- joint_act[i_2],
1568
- joint_target_ke[i_2],
1569
- joint_target_kd[i_2],
1570
- joint_limit_lower[i_2],
1571
- joint_limit_upper[i_2],
1572
- joint_limit_ke[i_2],
1573
- joint_limit_kd[i_2],
1574
- joint_axis_mode[i_2],
1575
- )
1576
-
1577
- # write forces
1578
- if c_parent >= 0:
1579
- wp.atomic_add(body_f, c_parent, wp.spatial_vector(t_total + wp.cross(r_p, f_total), f_total))
1580
-
1581
- wp.atomic_sub(body_f, c_child, wp.spatial_vector(t_total + wp.cross(r_c, f_total), f_total))
1582
-
1583
-
1584
- @wp.func
1585
- def compute_muscle_force(
1586
- i: int,
1587
- body_X_s: wp.array(dtype=wp.transform),
1588
- body_v_s: wp.array(dtype=wp.spatial_vector),
1589
- body_com: wp.array(dtype=wp.vec3),
1590
- muscle_links: wp.array(dtype=int),
1591
- muscle_points: wp.array(dtype=wp.vec3),
1592
- muscle_activation: float,
1593
- body_f_s: wp.array(dtype=wp.spatial_vector),
1594
- ):
1595
- link_0 = muscle_links[i]
1596
- link_1 = muscle_links[i + 1]
1597
-
1598
- if link_0 == link_1:
1599
- return 0
1600
-
1601
- r_0 = muscle_points[i]
1602
- r_1 = muscle_points[i + 1]
1603
-
1604
- xform_0 = body_X_s[link_0]
1605
- xform_1 = body_X_s[link_1]
1606
-
1607
- pos_0 = wp.transform_point(xform_0, r_0 - body_com[link_0])
1608
- pos_1 = wp.transform_point(xform_1, r_1 - body_com[link_1])
1609
-
1610
- n = wp.normalize(pos_1 - pos_0)
1611
-
1612
- # todo: add passive elastic and viscosity terms
1613
- f = n * muscle_activation
1614
-
1615
- wp.atomic_sub(body_f_s, link_0, wp.spatial_vector(f, wp.cross(pos_0, f)))
1616
- wp.atomic_add(body_f_s, link_1, wp.spatial_vector(f, wp.cross(pos_1, f)))
1617
-
1618
-
1619
- @wp.kernel
1620
- def eval_muscles(
1621
- body_X_s: wp.array(dtype=wp.transform),
1622
- body_v_s: wp.array(dtype=wp.spatial_vector),
1623
- body_com: wp.array(dtype=wp.vec3),
1624
- muscle_start: wp.array(dtype=int),
1625
- muscle_params: wp.array(dtype=float),
1626
- muscle_links: wp.array(dtype=int),
1627
- muscle_points: wp.array(dtype=wp.vec3),
1628
- muscle_activation: wp.array(dtype=float),
1629
- # output
1630
- body_f_s: wp.array(dtype=wp.spatial_vector),
1631
- ):
1632
- tid = wp.tid()
1633
-
1634
- m_start = muscle_start[tid]
1635
- m_end = muscle_start[tid + 1] - 1
1636
-
1637
- activation = muscle_activation[tid]
1638
-
1639
- for i in range(m_start, m_end):
1640
- compute_muscle_force(i, body_X_s, body_v_s, body_com, muscle_links, muscle_points, activation, body_f_s)
1641
-
1642
-
1643
- def eval_spring_forces(model: Model, state: State, particle_f: wp.array):
1644
- if model.spring_count:
1645
- wp.launch(
1646
- kernel=eval_springs,
1647
- dim=model.spring_count,
1648
- inputs=[
1649
- state.particle_q,
1650
- state.particle_qd,
1651
- model.spring_indices,
1652
- model.spring_rest_length,
1653
- model.spring_stiffness,
1654
- model.spring_damping,
1655
- ],
1656
- outputs=[particle_f],
1657
- device=model.device,
1658
- )
1659
-
1660
-
1661
- def eval_triangle_forces(model: Model, state: State, control: Control, particle_f: wp.array):
1662
- if model.tri_count:
1663
- wp.launch(
1664
- kernel=eval_triangles,
1665
- dim=model.tri_count,
1666
- inputs=[
1667
- state.particle_q,
1668
- state.particle_qd,
1669
- model.tri_indices,
1670
- model.tri_poses,
1671
- control.tri_activations,
1672
- model.tri_materials,
1673
- ],
1674
- outputs=[particle_f],
1675
- device=model.device,
1676
- )
1677
-
1678
-
1679
- def eval_triangle_contact_forces(model: Model, state: State, particle_f: wp.array):
1680
- if model.enable_tri_collisions:
1681
- wp.launch(
1682
- kernel=eval_triangles_contact,
1683
- dim=model.tri_count * model.particle_count,
1684
- inputs=[
1685
- model.particle_count,
1686
- state.particle_q,
1687
- state.particle_qd,
1688
- model.tri_indices,
1689
- model.tri_materials,
1690
- ],
1691
- outputs=[particle_f],
1692
- device=model.device,
1693
- )
1694
-
1695
-
1696
- def eval_bending_forces(model: Model, state: State, particle_f: wp.array):
1697
- if model.edge_count:
1698
- wp.launch(
1699
- kernel=eval_bending,
1700
- dim=model.edge_count,
1701
- inputs=[
1702
- state.particle_q,
1703
- state.particle_qd,
1704
- model.edge_indices,
1705
- model.edge_rest_angle,
1706
- model.edge_bending_properties,
1707
- ],
1708
- outputs=[particle_f],
1709
- device=model.device,
1710
- )
1711
-
1712
-
1713
- def eval_particle_ground_contact_forces(model: Model, state: State, particle_f: wp.array):
1714
- if model.ground and model.particle_count:
1715
- wp.launch(
1716
- kernel=eval_particle_ground_contacts,
1717
- dim=model.particle_count,
1718
- inputs=[
1719
- state.particle_q,
1720
- state.particle_qd,
1721
- model.particle_radius,
1722
- model.particle_flags,
1723
- model.soft_contact_ke,
1724
- model.soft_contact_kd,
1725
- model.soft_contact_kf,
1726
- model.soft_contact_mu,
1727
- model.ground_plane,
1728
- ],
1729
- outputs=[particle_f],
1730
- device=model.device,
1731
- )
1732
-
1733
-
1734
- def eval_tetrahedral_forces(model: Model, state: State, control: Control, particle_f: wp.array):
1735
- if model.tet_count:
1736
- wp.launch(
1737
- kernel=eval_tetrahedra,
1738
- dim=model.tet_count,
1739
- inputs=[
1740
- state.particle_q,
1741
- state.particle_qd,
1742
- model.tet_indices,
1743
- model.tet_poses,
1744
- control.tet_activations,
1745
- model.tet_materials,
1746
- ],
1747
- outputs=[particle_f],
1748
- device=model.device,
1749
- )
1750
-
1751
-
1752
- def eval_body_contact_forces(model: Model, state: State, particle_f: wp.array):
1753
- if model.rigid_contact_max and (
1754
- model.ground and model.shape_ground_contact_pair_count or model.shape_contact_pair_count
1755
- ):
1756
- wp.launch(
1757
- kernel=eval_rigid_contacts,
1758
- dim=model.rigid_contact_max,
1759
- inputs=[
1760
- state.body_q,
1761
- state.body_qd,
1762
- model.body_com,
1763
- model.shape_materials,
1764
- model.shape_geo,
1765
- model.shape_body,
1766
- model.rigid_contact_count,
1767
- model.rigid_contact_point0,
1768
- model.rigid_contact_point1,
1769
- model.rigid_contact_normal,
1770
- model.rigid_contact_shape0,
1771
- model.rigid_contact_shape1,
1772
- False,
1773
- ],
1774
- outputs=[state.body_f],
1775
- device=model.device,
1776
- )
1777
-
1778
-
1779
- def eval_body_joint_forces(model: Model, state: State, control: Control, body_f: wp.array):
1780
- if model.joint_count:
1781
- wp.launch(
1782
- kernel=eval_body_joints,
1783
- dim=model.joint_count,
1784
- inputs=[
1785
- state.body_q,
1786
- state.body_qd,
1787
- model.body_com,
1788
- model.joint_qd_start,
1789
- model.joint_type,
1790
- model.joint_enabled,
1791
- model.joint_child,
1792
- model.joint_parent,
1793
- model.joint_X_p,
1794
- model.joint_X_c,
1795
- model.joint_axis,
1796
- model.joint_axis_start,
1797
- model.joint_axis_dim,
1798
- model.joint_axis_mode,
1799
- control.joint_act,
1800
- model.joint_target_ke,
1801
- model.joint_target_kd,
1802
- model.joint_limit_lower,
1803
- model.joint_limit_upper,
1804
- model.joint_limit_ke,
1805
- model.joint_limit_kd,
1806
- model.joint_attach_ke,
1807
- model.joint_attach_kd,
1808
- ],
1809
- outputs=[body_f],
1810
- device=model.device,
1811
- )
1812
-
1813
-
1814
- def eval_particle_body_contact_forces(model: Model, state: State, particle_f: wp.array, body_f: wp.array):
1815
- if model.particle_count and model.shape_count > 1:
1816
- wp.launch(
1817
- kernel=eval_particle_contacts,
1818
- dim=model.soft_contact_max,
1819
- inputs=[
1820
- state.particle_q,
1821
- state.particle_qd,
1822
- state.body_q,
1823
- state.body_qd,
1824
- model.particle_radius,
1825
- model.particle_flags,
1826
- model.body_com,
1827
- model.shape_body,
1828
- model.shape_materials,
1829
- model.soft_contact_ke,
1830
- model.soft_contact_kd,
1831
- model.soft_contact_kf,
1832
- model.soft_contact_mu,
1833
- model.particle_adhesion,
1834
- model.soft_contact_count,
1835
- model.soft_contact_particle,
1836
- model.soft_contact_shape,
1837
- model.soft_contact_body_pos,
1838
- model.soft_contact_body_vel,
1839
- model.soft_contact_normal,
1840
- model.soft_contact_max,
1841
- ],
1842
- # outputs
1843
- outputs=[particle_f, body_f],
1844
- device=model.device,
1845
- )
1846
-
1847
-
1848
- def eval_muscle_forces(model: Model, state: State, control: Control, body_f: wp.array):
1849
- if model.muscle_count:
1850
- wp.launch(
1851
- kernel=eval_muscles,
1852
- dim=model.muscle_count,
1853
- inputs=[
1854
- state.body_q,
1855
- state.body_qd,
1856
- model.body_com,
1857
- model.muscle_start,
1858
- model.muscle_params,
1859
- model.muscle_bodies,
1860
- model.muscle_points,
1861
- control.muscle_activations,
1862
- ],
1863
- outputs=[body_f],
1864
- device=model.device,
1865
- )
1866
-
1867
-
1868
- def compute_forces(model: Model, state: State, control: Control, particle_f: wp.array, body_f: wp.array, dt: float):
1869
- # damped springs
1870
- eval_spring_forces(model, state, particle_f)
1871
-
1872
- # triangle elastic and lift/drag forces
1873
- eval_triangle_forces(model, state, control, particle_f)
1874
-
1875
- # triangle/triangle contacts
1876
- eval_triangle_contact_forces(model, state, particle_f)
1877
-
1878
- # triangle bending
1879
- eval_bending_forces(model, state, particle_f)
1880
-
1881
- # tetrahedral FEM
1882
- eval_tetrahedral_forces(model, state, control, particle_f)
1883
-
1884
- # body joints
1885
- eval_body_joint_forces(model, state, control, body_f)
1886
-
1887
- # particle-particle interactions
1888
- eval_particle_forces(model, state, particle_f)
1889
-
1890
- # particle ground contacts
1891
- eval_particle_ground_contact_forces(model, state, particle_f)
1892
-
1893
- # body contacts
1894
- eval_body_contact_forces(model, state, particle_f)
1895
-
1896
- # particle shape contact
1897
- eval_particle_body_contact_forces(model, state, particle_f, body_f)
1898
-
1899
- # muscles
1900
- if False:
1901
- eval_muscle_forces(model, state, control, body_f)
1902
-
1903
-
1904
- class SemiImplicitIntegrator(Integrator):
1905
- """A semi-implicit integrator using symplectic Euler
1906
-
1907
- After constructing `Model` and `State` objects this time-integrator
1908
- may be used to advance the simulation state forward in time.
1909
-
1910
- Semi-implicit time integration is a variational integrator that
1911
- preserves energy, however it not unconditionally stable, and requires a time-step
1912
- small enough to support the required stiffness and damping forces.
1913
-
1914
- See: https://en.wikipedia.org/wiki/Semi-implicit_Euler_method
1915
-
1916
- Example
1917
- -------
1918
-
1919
- .. code-block:: python
1920
-
1921
- integrator = wp.SemiImplicitIntegrator()
1922
-
1923
- # simulation loop
1924
- for i in range(100):
1925
- state = integrator.simulate(model, state_in, state_out, dt)
1926
-
1927
- """
1928
-
1929
- def __init__(self, angular_damping: float = 0.05):
1930
- """
1931
- Args:
1932
- angular_damping (float, optional): Angular damping factor. Defaults to 0.05.
1933
- """
1934
- self.angular_damping = angular_damping
1935
-
1936
- def simulate(self, model: Model, state_in: State, state_out: State, dt: float, control: Control = None):
1937
- with wp.ScopedTimer("simulate", False):
1938
- particle_f = None
1939
- body_f = None
1940
-
1941
- if state_in.particle_count:
1942
- particle_f = state_in.particle_f
1943
-
1944
- if state_in.body_count:
1945
- body_f = state_in.body_f
1946
-
1947
- if control is None:
1948
- control = model.control(clone_variables=False)
1949
-
1950
- compute_forces(model, state_in, control, particle_f, body_f, dt)
1951
-
1952
- self.integrate_bodies(model, state_in, state_out, dt, self.angular_damping)
1953
-
1954
- self.integrate_particles(model, state_in, state_out, dt)
1955
-
1956
- return state_out
1
+ # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ """This module contains time-integration objects for simulating
9
+ models + state forward in time.
10
+
11
+ """
12
+
13
+ import warp as wp
14
+
15
+ from .collide import triangle_closest_point_barycentric
16
+ from .integrator import Integrator
17
+ from .model import PARTICLE_FLAG_ACTIVE, Control, Model, ModelShapeGeometry, ModelShapeMaterials, State
18
+ from .particles import eval_particle_forces
19
+ from .utils import quat_decompose, quat_twist
20
+
21
+
22
+ @wp.kernel
23
+ def eval_springs(
24
+ x: wp.array(dtype=wp.vec3),
25
+ v: wp.array(dtype=wp.vec3),
26
+ spring_indices: wp.array(dtype=int),
27
+ spring_rest_lengths: wp.array(dtype=float),
28
+ spring_stiffness: wp.array(dtype=float),
29
+ spring_damping: wp.array(dtype=float),
30
+ f: wp.array(dtype=wp.vec3),
31
+ ):
32
+ tid = wp.tid()
33
+
34
+ i = spring_indices[tid * 2 + 0]
35
+ j = spring_indices[tid * 2 + 1]
36
+
37
+ ke = spring_stiffness[tid]
38
+ kd = spring_damping[tid]
39
+ rest = spring_rest_lengths[tid]
40
+
41
+ xi = x[i]
42
+ xj = x[j]
43
+
44
+ vi = v[i]
45
+ vj = v[j]
46
+
47
+ xij = xi - xj
48
+ vij = vi - vj
49
+
50
+ l = wp.length(xij)
51
+ l_inv = 1.0 / l
52
+
53
+ # normalized spring direction
54
+ dir = xij * l_inv
55
+
56
+ c = l - rest
57
+ dcdt = wp.dot(dir, vij)
58
+
59
+ # damping based on relative velocity
60
+ fs = dir * (ke * c + kd * dcdt)
61
+
62
+ wp.atomic_sub(f, i, fs)
63
+ wp.atomic_add(f, j, fs)
64
+
65
+
66
+ @wp.kernel
67
+ def eval_triangles(
68
+ x: wp.array(dtype=wp.vec3),
69
+ v: wp.array(dtype=wp.vec3),
70
+ indices: wp.array2d(dtype=int),
71
+ pose: wp.array(dtype=wp.mat22),
72
+ activation: wp.array(dtype=float),
73
+ materials: wp.array2d(dtype=float),
74
+ f: wp.array(dtype=wp.vec3),
75
+ ):
76
+ tid = wp.tid()
77
+
78
+ k_mu = materials[tid, 0]
79
+ k_lambda = materials[tid, 1]
80
+ k_damp = materials[tid, 2]
81
+ k_drag = materials[tid, 3]
82
+ k_lift = materials[tid, 4]
83
+
84
+ i = indices[tid, 0]
85
+ j = indices[tid, 1]
86
+ k = indices[tid, 2]
87
+
88
+ x0 = x[i] # point zero
89
+ x1 = x[j] # point one
90
+ x2 = x[k] # point two
91
+
92
+ v0 = v[i] # vel zero
93
+ v1 = v[j] # vel one
94
+ v2 = v[k] # vel two
95
+
96
+ x10 = x1 - x0 # barycentric coordinates (centered at p)
97
+ x20 = x2 - x0
98
+
99
+ v10 = v1 - v0
100
+ v20 = v2 - v0
101
+
102
+ Dm = pose[tid]
103
+
104
+ inv_rest_area = wp.determinant(Dm) * 2.0 # 1 / det(A) = det(A^-1)
105
+ rest_area = 1.0 / inv_rest_area
106
+
107
+ # scale stiffness coefficients to account for area
108
+ k_mu = k_mu * rest_area
109
+ k_lambda = k_lambda * rest_area
110
+ k_damp = k_damp * rest_area
111
+
112
+ # F = Xs*Xm^-1
113
+ F1 = x10 * Dm[0, 0] + x20 * Dm[1, 0]
114
+ F2 = x10 * Dm[0, 1] + x20 * Dm[1, 1]
115
+
116
+ # dFdt = Vs*Xm^-1
117
+ dFdt1 = v10 * Dm[0, 0] + v20 * Dm[1, 0]
118
+ dFdt2 = v10 * Dm[0, 1] + v20 * Dm[1, 1]
119
+
120
+ # deviatoric PK1 + damping term
121
+ P1 = F1 * k_mu + dFdt1 * k_damp
122
+ P2 = F2 * k_mu + dFdt2 * k_damp
123
+
124
+ # -----------------------------
125
+ # St. Venant-Kirchoff
126
+
127
+ # # Green strain, F'*F-I
128
+ # e00 = dot(f1, f1) - 1.0
129
+ # e10 = dot(f2, f1)
130
+ # e01 = dot(f1, f2)
131
+ # e11 = dot(f2, f2) - 1.0
132
+
133
+ # E = wp.mat22(e00, e01,
134
+ # e10, e11)
135
+
136
+ # # local forces (deviatoric part)
137
+ # T = wp.mul(E, wp.transpose(Dm))
138
+
139
+ # # spatial forces, F*T
140
+ # fq = (f1*T[0,0] + f2*T[1,0])*k_mu*2.0
141
+ # fr = (f1*T[0,1] + f2*T[1,1])*k_mu*2.0
142
+ # alpha = 1.0
143
+
144
+ # -----------------------------
145
+ # Baraff & Witkin, note this model is not isotropic
146
+
147
+ # c1 = length(f1) - 1.0
148
+ # c2 = length(f2) - 1.0
149
+ # f1 = normalize(f1)*c1*k1
150
+ # f2 = normalize(f2)*c2*k1
151
+
152
+ # fq = f1*Dm[0,0] + f2*Dm[0,1]
153
+ # fr = f1*Dm[1,0] + f2*Dm[1,1]
154
+
155
+ # -----------------------------
156
+ # Neo-Hookean (with rest stability)
157
+
158
+ # force = P*Dm'
159
+ f1 = P1 * Dm[0, 0] + P2 * Dm[0, 1]
160
+ f2 = P1 * Dm[1, 0] + P2 * Dm[1, 1]
161
+ alpha = 1.0 + k_mu / k_lambda
162
+
163
+ # -----------------------------
164
+ # Area Preservation
165
+
166
+ n = wp.cross(x10, x20)
167
+ area = wp.length(n) * 0.5
168
+
169
+ # actuation
170
+ act = activation[tid]
171
+
172
+ # J-alpha
173
+ c = area * inv_rest_area - alpha + act
174
+
175
+ # dJdx
176
+ n = wp.normalize(n)
177
+ dcdq = wp.cross(x20, n) * inv_rest_area * 0.5
178
+ dcdr = wp.cross(n, x10) * inv_rest_area * 0.5
179
+
180
+ f_area = k_lambda * c
181
+
182
+ # -----------------------------
183
+ # Area Damping
184
+
185
+ dcdt = wp.dot(dcdq, v1) + wp.dot(dcdr, v2) - wp.dot(dcdq + dcdr, v0)
186
+ f_damp = k_damp * dcdt
187
+
188
+ f1 = f1 + dcdq * (f_area + f_damp)
189
+ f2 = f2 + dcdr * (f_area + f_damp)
190
+ f0 = f1 + f2
191
+
192
+ # -----------------------------
193
+ # Lift + Drag
194
+
195
+ vmid = (v0 + v1 + v2) * 0.3333
196
+ vdir = wp.normalize(vmid)
197
+
198
+ f_drag = vmid * (k_drag * area * wp.abs(wp.dot(n, vmid)))
199
+ f_lift = n * (k_lift * area * (wp.HALF_PI - wp.acos(wp.dot(n, vdir)))) * wp.dot(vmid, vmid)
200
+
201
+ f0 = f0 - f_drag - f_lift
202
+ f1 = f1 + f_drag + f_lift
203
+ f2 = f2 + f_drag + f_lift
204
+
205
+ # apply forces
206
+ wp.atomic_add(f, i, f0)
207
+ wp.atomic_sub(f, j, f1)
208
+ wp.atomic_sub(f, k, f2)
209
+
210
+
211
+ # @wp.func
212
+ # def triangle_closest_point(a: wp.vec3, b: wp.vec3, c: wp.vec3, p: wp.vec3):
213
+ # ab = b - a
214
+ # ac = c - a
215
+ # ap = p - a
216
+
217
+ # d1 = wp.dot(ab, ap)
218
+ # d2 = wp.dot(ac, ap)
219
+
220
+ # if (d1 <= 0.0 and d2 <= 0.0):
221
+ # return a
222
+
223
+ # bp = p - b
224
+ # d3 = wp.dot(ab, bp)
225
+ # d4 = wp.dot(ac, bp)
226
+
227
+ # if (d3 >= 0.0 and d4 <= d3):
228
+ # return b
229
+
230
+ # vc = d1 * d4 - d3 * d2
231
+ # v = d1 / (d1 - d3)
232
+ # if (vc <= 0.0 and d1 >= 0.0 and d3 <= 0.0):
233
+ # return a + ab * v
234
+
235
+ # cp = p - c
236
+ # d5 = dot(ab, cp)
237
+ # d6 = dot(ac, cp)
238
+
239
+ # if (d6 >= 0.0 and d5 <= d6):
240
+ # return c
241
+
242
+ # vb = d5 * d2 - d1 * d6
243
+ # w = d2 / (d2 - d6)
244
+ # if (vb <= 0.0 and d2 >= 0.0 and d6 <= 0.0):
245
+ # return a + ac * w
246
+
247
+ # va = d3 * d6 - d5 * d4
248
+ # w = (d4 - d3) / ((d4 - d3) + (d5 - d6))
249
+ # if (va <= 0.0 and (d4 - d3) >= 0.0 and (d5 - d6) >= 0.0):
250
+ # return b + (c - b) * w
251
+
252
+ # denom = 1.0 / (va + vb + vc)
253
+ # v = vb * denom
254
+ # w = vc * denom
255
+
256
+ # return a + ab * v + ac * w
257
+
258
+
259
+ @wp.kernel
260
+ def eval_triangles_contact(
261
+ # idx : wp.array(dtype=int), # list of indices for colliding particles
262
+ num_particles: int, # size of particles
263
+ x: wp.array(dtype=wp.vec3),
264
+ v: wp.array(dtype=wp.vec3),
265
+ indices: wp.array2d(dtype=int),
266
+ materials: wp.array2d(dtype=float),
267
+ f: wp.array(dtype=wp.vec3),
268
+ ):
269
+ tid = wp.tid()
270
+ face_no = tid // num_particles # which face
271
+ particle_no = tid % num_particles # which particle
272
+
273
+ # k_mu = materials[face_no, 0]
274
+ # k_lambda = materials[face_no, 1]
275
+ # k_damp = materials[face_no, 2]
276
+ # k_drag = materials[face_no, 3]
277
+ # k_lift = materials[face_no, 4]
278
+
279
+ # at the moment, just one particle
280
+ pos = x[particle_no]
281
+
282
+ i = indices[face_no, 0]
283
+ j = indices[face_no, 1]
284
+ k = indices[face_no, 2]
285
+
286
+ if i == particle_no or j == particle_no or k == particle_no:
287
+ return
288
+
289
+ p = x[i] # point zero
290
+ q = x[j] # point one
291
+ r = x[k] # point two
292
+
293
+ # vp = v[i] # vel zero
294
+ # vq = v[j] # vel one
295
+ # vr = v[k] # vel two
296
+
297
+ # qp = q-p # barycentric coordinates (centered at p)
298
+ # rp = r-p
299
+
300
+ bary = triangle_closest_point_barycentric(p, q, r, pos)
301
+ closest = p * bary[0] + q * bary[1] + r * bary[2]
302
+
303
+ diff = pos - closest
304
+ dist = wp.dot(diff, diff)
305
+ n = wp.normalize(diff)
306
+ c = wp.min(dist - 0.01, 0.0) # 0 unless within 0.01 of surface
307
+ # c = wp.leaky_min(dot(n, x0)-0.01, 0.0, 0.0)
308
+ fn = n * c * 1e5
309
+
310
+ wp.atomic_sub(f, particle_no, fn)
311
+
312
+ # # apply forces (could do - f / 3 here)
313
+ wp.atomic_add(f, i, fn * bary[0])
314
+ wp.atomic_add(f, j, fn * bary[1])
315
+ wp.atomic_add(f, k, fn * bary[2])
316
+
317
+
318
+ @wp.kernel
319
+ def eval_triangles_body_contacts(
320
+ num_particles: int, # number of particles (size of contact_point)
321
+ x: wp.array(dtype=wp.vec3), # position of particles
322
+ v: wp.array(dtype=wp.vec3),
323
+ indices: wp.array(dtype=int), # triangle indices
324
+ body_x: wp.array(dtype=wp.vec3), # body body positions
325
+ body_r: wp.array(dtype=wp.quat),
326
+ body_v: wp.array(dtype=wp.vec3),
327
+ body_w: wp.array(dtype=wp.vec3),
328
+ contact_body: wp.array(dtype=int),
329
+ contact_point: wp.array(dtype=wp.vec3), # position of contact points relative to body
330
+ contact_dist: wp.array(dtype=float),
331
+ contact_mat: wp.array(dtype=int),
332
+ materials: wp.array(dtype=float),
333
+ # body_f : wp.array(dtype=wp.vec3),
334
+ # body_t : wp.array(dtype=wp.vec3),
335
+ tri_f: wp.array(dtype=wp.vec3),
336
+ ):
337
+ tid = wp.tid()
338
+
339
+ face_no = tid // num_particles # which face
340
+ particle_no = tid % num_particles # which particle
341
+
342
+ # -----------------------
343
+ # load body body point
344
+ c_body = contact_body[particle_no]
345
+ c_point = contact_point[particle_no]
346
+ c_dist = contact_dist[particle_no]
347
+ c_mat = contact_mat[particle_no]
348
+
349
+ # hard coded surface parameter tensor layout (ke, kd, kf, mu)
350
+ ke = materials[c_mat * 4 + 0] # restitution coefficient
351
+ kd = materials[c_mat * 4 + 1] # damping coefficient
352
+ kf = materials[c_mat * 4 + 2] # friction coefficient
353
+ mu = materials[c_mat * 4 + 3] # coulomb friction
354
+
355
+ x0 = body_x[c_body] # position of colliding body
356
+ r0 = body_r[c_body] # orientation of colliding body
357
+
358
+ v0 = body_v[c_body]
359
+ w0 = body_w[c_body]
360
+
361
+ # transform point to world space
362
+ pos = x0 + wp.quat_rotate(r0, c_point)
363
+ # use x0 as center, everything is offset from center of mass
364
+
365
+ # moment arm
366
+ r = pos - x0 # basically just c_point in the new coordinates
367
+ rhat = wp.normalize(r)
368
+ pos = pos + rhat * c_dist # add on 'thickness' of shape, e.g.: radius of sphere/capsule
369
+
370
+ # contact point velocity
371
+ dpdt = v0 + wp.cross(w0, r) # this is body velocity cross offset, so it's the velocity of the contact point.
372
+
373
+ # -----------------------
374
+ # load triangle
375
+ i = indices[face_no * 3 + 0]
376
+ j = indices[face_no * 3 + 1]
377
+ k = indices[face_no * 3 + 2]
378
+
379
+ p = x[i] # point zero
380
+ q = x[j] # point one
381
+ r = x[k] # point two
382
+
383
+ vp = v[i] # vel zero
384
+ vq = v[j] # vel one
385
+ vr = v[k] # vel two
386
+
387
+ bary = triangle_closest_point_barycentric(p, q, r, pos)
388
+ closest = p * bary[0] + q * bary[1] + r * bary[2]
389
+
390
+ diff = pos - closest # vector from tri to point
391
+ dist = wp.dot(diff, diff) # squared distance
392
+ n = wp.normalize(diff) # points into the object
393
+ c = wp.min(dist - 0.05, 0.0) # 0 unless within 0.05 of surface
394
+ # c = wp.leaky_min(wp.dot(n, x0)-0.01, 0.0, 0.0)
395
+ # fn = n * c * 1e6 # points towards cloth (both n and c are negative)
396
+
397
+ # wp.atomic_sub(tri_f, particle_no, fn)
398
+
399
+ fn = c * ke # normal force (restitution coefficient * how far inside for ground) (negative)
400
+
401
+ vtri = vp * bary[0] + vq * bary[1] + vr * bary[2] # bad approximation for centroid velocity
402
+ vrel = vtri - dpdt
403
+
404
+ vn = wp.dot(n, vrel) # velocity component of body in negative normal direction
405
+ vt = vrel - n * vn # velocity component not in normal direction
406
+
407
+ # contact damping
408
+ fd = -wp.max(vn, 0.0) * kd * wp.step(c) # again, negative, into the ground
409
+
410
+ # # viscous friction
411
+ # ft = vt*kf
412
+
413
+ # Coulomb friction (box)
414
+ lower = mu * (fn + fd)
415
+ upper = -lower
416
+
417
+ nx = wp.cross(n, wp.vec3(0.0, 0.0, 1.0)) # basis vectors for tangent
418
+ nz = wp.cross(n, wp.vec3(1.0, 0.0, 0.0))
419
+
420
+ vx = wp.clamp(wp.dot(nx * kf, vt), lower, upper)
421
+ vz = wp.clamp(wp.dot(nz * kf, vt), lower, upper)
422
+
423
+ ft = (nx * vx + nz * vz) * (-wp.step(c)) # wp.vec3(vx, 0.0, vz)*wp.step(c)
424
+
425
+ # # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
426
+ # #ft = wp.normalize(vt)*wp.min(kf*wp.length(vt), -mu*c*ke)
427
+
428
+ f_total = n * (fn + fd) + ft
429
+
430
+ wp.atomic_add(tri_f, i, f_total * bary[0])
431
+ wp.atomic_add(tri_f, j, f_total * bary[1])
432
+ wp.atomic_add(tri_f, k, f_total * bary[2])
433
+
434
+
435
+ @wp.kernel
436
+ def eval_bending(
437
+ x: wp.array(dtype=wp.vec3),
438
+ v: wp.array(dtype=wp.vec3),
439
+ indices: wp.array2d(dtype=int),
440
+ rest: wp.array(dtype=float),
441
+ bending_properties: wp.array2d(dtype=float),
442
+ f: wp.array(dtype=wp.vec3),
443
+ ):
444
+ tid = wp.tid()
445
+ ke = bending_properties[tid, 0]
446
+ kd = bending_properties[tid, 1]
447
+
448
+ i = indices[tid, 0]
449
+ j = indices[tid, 1]
450
+ k = indices[tid, 2]
451
+ l = indices[tid, 3]
452
+
453
+ rest_angle = rest[tid]
454
+
455
+ x1 = x[i]
456
+ x2 = x[j]
457
+ x3 = x[k]
458
+ x4 = x[l]
459
+
460
+ v1 = v[i]
461
+ v2 = v[j]
462
+ v3 = v[k]
463
+ v4 = v[l]
464
+
465
+ n1 = wp.cross(x3 - x1, x4 - x1) # normal to face 1
466
+ n2 = wp.cross(x4 - x2, x3 - x2) # normal to face 2
467
+
468
+ n1_length = wp.length(n1)
469
+ n2_length = wp.length(n2)
470
+
471
+ if n1_length < 1.0e-6 or n2_length < 1.0e-6:
472
+ return
473
+
474
+ rcp_n1 = 1.0 / n1_length
475
+ rcp_n2 = 1.0 / n2_length
476
+
477
+ cos_theta = wp.dot(n1, n2) * rcp_n1 * rcp_n2
478
+
479
+ n1 = n1 * rcp_n1 * rcp_n1
480
+ n2 = n2 * rcp_n2 * rcp_n2
481
+
482
+ e = x4 - x3
483
+ e_hat = wp.normalize(e)
484
+ e_length = wp.length(e)
485
+
486
+ s = wp.sign(wp.dot(wp.cross(n2, n1), e_hat))
487
+ angle = wp.acos(cos_theta) * s
488
+
489
+ d1 = n1 * e_length
490
+ d2 = n2 * e_length
491
+ d3 = n1 * wp.dot(x1 - x4, e_hat) + n2 * wp.dot(x2 - x4, e_hat)
492
+ d4 = n1 * wp.dot(x3 - x1, e_hat) + n2 * wp.dot(x3 - x2, e_hat)
493
+
494
+ # elastic
495
+ f_elastic = ke * (angle - rest_angle)
496
+
497
+ # damping
498
+ f_damp = kd * (wp.dot(d1, v1) + wp.dot(d2, v2) + wp.dot(d3, v3) + wp.dot(d4, v4))
499
+
500
+ # total force, proportional to edge length
501
+ f_total = -e_length * (f_elastic + f_damp)
502
+
503
+ wp.atomic_add(f, i, d1 * f_total)
504
+ wp.atomic_add(f, j, d2 * f_total)
505
+ wp.atomic_add(f, k, d3 * f_total)
506
+ wp.atomic_add(f, l, d4 * f_total)
507
+
508
+
509
+ @wp.kernel
510
+ def eval_tetrahedra(
511
+ x: wp.array(dtype=wp.vec3),
512
+ v: wp.array(dtype=wp.vec3),
513
+ indices: wp.array2d(dtype=int),
514
+ pose: wp.array(dtype=wp.mat33),
515
+ activation: wp.array(dtype=float),
516
+ materials: wp.array2d(dtype=float),
517
+ f: wp.array(dtype=wp.vec3),
518
+ ):
519
+ tid = wp.tid()
520
+
521
+ i = indices[tid, 0]
522
+ j = indices[tid, 1]
523
+ k = indices[tid, 2]
524
+ l = indices[tid, 3]
525
+
526
+ act = activation[tid]
527
+
528
+ k_mu = materials[tid, 0]
529
+ k_lambda = materials[tid, 1]
530
+ k_damp = materials[tid, 2]
531
+
532
+ x0 = x[i]
533
+ x1 = x[j]
534
+ x2 = x[k]
535
+ x3 = x[l]
536
+
537
+ v0 = v[i]
538
+ v1 = v[j]
539
+ v2 = v[k]
540
+ v3 = v[l]
541
+
542
+ x10 = x1 - x0
543
+ x20 = x2 - x0
544
+ x30 = x3 - x0
545
+
546
+ v10 = v1 - v0
547
+ v20 = v2 - v0
548
+ v30 = v3 - v0
549
+
550
+ Ds = wp.mat33(x10, x20, x30)
551
+ Dm = pose[tid]
552
+
553
+ inv_rest_volume = wp.determinant(Dm) * 6.0
554
+ rest_volume = 1.0 / inv_rest_volume
555
+
556
+ alpha = 1.0 + k_mu / k_lambda - k_mu / (4.0 * k_lambda)
557
+
558
+ # scale stiffness coefficients to account for area
559
+ k_mu = k_mu * rest_volume
560
+ k_lambda = k_lambda * rest_volume
561
+ k_damp = k_damp * rest_volume
562
+
563
+ # F = Xs*Xm^-1
564
+ F = Ds * Dm
565
+ dFdt = wp.mat33(v10, v20, v30) * Dm
566
+
567
+ col1 = wp.vec3(F[0, 0], F[1, 0], F[2, 0])
568
+ col2 = wp.vec3(F[0, 1], F[1, 1], F[2, 1])
569
+ col3 = wp.vec3(F[0, 2], F[1, 2], F[2, 2])
570
+
571
+ # -----------------------------
572
+ # Neo-Hookean (with rest stability [Smith et al 2018])
573
+
574
+ Ic = wp.dot(col1, col1) + wp.dot(col2, col2) + wp.dot(col3, col3)
575
+
576
+ # deviatoric part
577
+ P = F * k_mu * (1.0 - 1.0 / (Ic + 1.0)) + dFdt * k_damp
578
+ H = P * wp.transpose(Dm)
579
+
580
+ f1 = wp.vec3(H[0, 0], H[1, 0], H[2, 0])
581
+ f2 = wp.vec3(H[0, 1], H[1, 1], H[2, 1])
582
+ f3 = wp.vec3(H[0, 2], H[1, 2], H[2, 2])
583
+
584
+ # -----------------------------
585
+ # C_sqrt
586
+
587
+ # alpha = 1.0
588
+
589
+ # r_s = wp.sqrt(wp.abs(dot(col1, col1) + dot(col2, col2) + dot(col3, col3) - 3.0))
590
+
591
+ # f1 = wp.vec3()
592
+ # f2 = wp.vec3()
593
+ # f3 = wp.vec3()
594
+
595
+ # if (r_s > 0.0):
596
+ # r_s_inv = 1.0/r_s
597
+
598
+ # C = r_s
599
+ # dCdx = F*wp.transpose(Dm)*r_s_inv*wp.sign(r_s)
600
+
601
+ # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
602
+ # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
603
+ # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
604
+
605
+ # f1 = grad1*C*k_mu
606
+ # f2 = grad2*C*k_mu
607
+ # f3 = grad3*C*k_mu
608
+
609
+ # -----------------------------
610
+ # C_spherical
611
+
612
+ # alpha = 1.0
613
+
614
+ # r_s = wp.sqrt(dot(col1, col1) + dot(col2, col2) + dot(col3, col3))
615
+ # r_s_inv = 1.0/r_s
616
+
617
+ # C = r_s - wp.sqrt(3.0)
618
+ # dCdx = F*wp.transpose(Dm)*r_s_inv
619
+
620
+ # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
621
+ # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
622
+ # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
623
+
624
+ # f1 = grad1*C*k_mu
625
+ # f2 = grad2*C*k_mu
626
+ # f3 = grad3*C*k_mu
627
+
628
+ # ----------------------------
629
+ # C_D
630
+
631
+ # alpha = 1.0
632
+
633
+ # r_s = wp.sqrt(dot(col1, col1) + dot(col2, col2) + dot(col3, col3))
634
+
635
+ # C = r_s*r_s - 3.0
636
+ # dCdx = F*wp.transpose(Dm)*2.0
637
+
638
+ # grad1 = vec3(dCdx[0,0], dCdx[1,0], dCdx[2,0])
639
+ # grad2 = vec3(dCdx[0,1], dCdx[1,1], dCdx[2,1])
640
+ # grad3 = vec3(dCdx[0,2], dCdx[1,2], dCdx[2,2])
641
+
642
+ # f1 = grad1*C*k_mu
643
+ # f2 = grad2*C*k_mu
644
+ # f3 = grad3*C*k_mu
645
+
646
+ # ----------------------------
647
+ # Hookean
648
+
649
+ # alpha = 1.0
650
+
651
+ # I = wp.mat33(wp.vec3(1.0, 0.0, 0.0),
652
+ # wp.vec3(0.0, 1.0, 0.0),
653
+ # wp.vec3(0.0, 0.0, 1.0))
654
+
655
+ # P = (F + wp.transpose(F) + I*(0.0-2.0))*k_mu
656
+ # H = P * wp.transpose(Dm)
657
+
658
+ # f1 = wp.vec3(H[0, 0], H[1, 0], H[2, 0])
659
+ # f2 = wp.vec3(H[0, 1], H[1, 1], H[2, 1])
660
+ # f3 = wp.vec3(H[0, 2], H[1, 2], H[2, 2])
661
+
662
+ # hydrostatic part
663
+ J = wp.determinant(F)
664
+
665
+ # print(J)
666
+ s = inv_rest_volume / 6.0
667
+ dJdx1 = wp.cross(x20, x30) * s
668
+ dJdx2 = wp.cross(x30, x10) * s
669
+ dJdx3 = wp.cross(x10, x20) * s
670
+
671
+ f_volume = (J - alpha + act) * k_lambda
672
+ f_damp = (wp.dot(dJdx1, v1) + wp.dot(dJdx2, v2) + wp.dot(dJdx3, v3)) * k_damp
673
+
674
+ f_total = f_volume + f_damp
675
+
676
+ f1 = f1 + dJdx1 * f_total
677
+ f2 = f2 + dJdx2 * f_total
678
+ f3 = f3 + dJdx3 * f_total
679
+ f0 = -(f1 + f2 + f3)
680
+
681
+ # apply forces
682
+ wp.atomic_sub(f, i, f0)
683
+ wp.atomic_sub(f, j, f1)
684
+ wp.atomic_sub(f, k, f2)
685
+ wp.atomic_sub(f, l, f3)
686
+
687
+
688
+ @wp.kernel
689
+ def eval_particle_ground_contacts(
690
+ particle_x: wp.array(dtype=wp.vec3),
691
+ particle_v: wp.array(dtype=wp.vec3),
692
+ particle_radius: wp.array(dtype=float),
693
+ particle_flags: wp.array(dtype=wp.uint32),
694
+ ke: float,
695
+ kd: float,
696
+ kf: float,
697
+ mu: float,
698
+ ground: wp.array(dtype=float),
699
+ # outputs
700
+ f: wp.array(dtype=wp.vec3),
701
+ ):
702
+ tid = wp.tid()
703
+ if (particle_flags[tid] & PARTICLE_FLAG_ACTIVE) == 0:
704
+ return
705
+
706
+ x = particle_x[tid]
707
+ v = particle_v[tid]
708
+ radius = particle_radius[tid]
709
+
710
+ n = wp.vec3(ground[0], ground[1], ground[2])
711
+ c = wp.min(wp.dot(n, x) + ground[3] - radius, 0.0)
712
+
713
+ vn = wp.dot(n, v)
714
+ jn = c * ke
715
+
716
+ if c >= 0.0:
717
+ return
718
+
719
+ jd = min(vn, 0.0) * kd
720
+
721
+ # contact force
722
+ fn = jn + jd
723
+
724
+ # friction force
725
+ vt = v - n * vn
726
+ vs = wp.length(vt)
727
+
728
+ if vs > 0.0:
729
+ vt = vt / vs
730
+
731
+ # Coulomb condition
732
+ ft = wp.min(vs * kf, mu * wp.abs(fn))
733
+
734
+ # total force
735
+ f[tid] = f[tid] - n * fn - vt * ft
736
+
737
+
738
+ @wp.kernel
739
+ def eval_particle_contacts(
740
+ particle_x: wp.array(dtype=wp.vec3),
741
+ particle_v: wp.array(dtype=wp.vec3),
742
+ body_q: wp.array(dtype=wp.transform),
743
+ body_qd: wp.array(dtype=wp.spatial_vector),
744
+ particle_radius: wp.array(dtype=float),
745
+ particle_flags: wp.array(dtype=wp.uint32),
746
+ body_com: wp.array(dtype=wp.vec3),
747
+ shape_body: wp.array(dtype=int),
748
+ shape_materials: ModelShapeMaterials,
749
+ particle_ke: float,
750
+ particle_kd: float,
751
+ particle_kf: float,
752
+ particle_mu: float,
753
+ particle_ka: float,
754
+ contact_count: wp.array(dtype=int),
755
+ contact_particle: wp.array(dtype=int),
756
+ contact_shape: wp.array(dtype=int),
757
+ contact_body_pos: wp.array(dtype=wp.vec3),
758
+ contact_body_vel: wp.array(dtype=wp.vec3),
759
+ contact_normal: wp.array(dtype=wp.vec3),
760
+ contact_max: int,
761
+ # outputs
762
+ particle_f: wp.array(dtype=wp.vec3),
763
+ body_f: wp.array(dtype=wp.spatial_vector),
764
+ ):
765
+ tid = wp.tid()
766
+
767
+ count = min(contact_max, contact_count[0])
768
+ if tid >= count:
769
+ return
770
+
771
+ shape_index = contact_shape[tid]
772
+ body_index = shape_body[shape_index]
773
+ particle_index = contact_particle[tid]
774
+ if (particle_flags[particle_index] & PARTICLE_FLAG_ACTIVE) == 0:
775
+ return
776
+
777
+ px = particle_x[particle_index]
778
+ pv = particle_v[particle_index]
779
+
780
+ X_wb = wp.transform_identity()
781
+ X_com = wp.vec3()
782
+ body_v_s = wp.spatial_vector()
783
+
784
+ if body_index >= 0:
785
+ X_wb = body_q[body_index]
786
+ X_com = body_com[body_index]
787
+ body_v_s = body_qd[body_index]
788
+
789
+ # body position in world space
790
+ bx = wp.transform_point(X_wb, contact_body_pos[tid])
791
+ r = bx - wp.transform_point(X_wb, X_com)
792
+
793
+ n = contact_normal[tid]
794
+ c = wp.dot(n, px - bx) - particle_radius[tid]
795
+
796
+ if c > particle_ka:
797
+ return
798
+
799
+ # take average material properties of shape and particle parameters
800
+ ke = 0.5 * (particle_ke + shape_materials.ke[shape_index])
801
+ kd = 0.5 * (particle_kd + shape_materials.kd[shape_index])
802
+ kf = 0.5 * (particle_kf + shape_materials.kf[shape_index])
803
+ mu = 0.5 * (particle_mu + shape_materials.mu[shape_index])
804
+
805
+ body_w = wp.spatial_top(body_v_s)
806
+ body_v = wp.spatial_bottom(body_v_s)
807
+
808
+ # compute the body velocity at the particle position
809
+ bv = body_v + wp.cross(body_w, r) + wp.transform_vector(X_wb, contact_body_vel[tid])
810
+
811
+ # relative velocity
812
+ v = pv - bv
813
+
814
+ # decompose relative velocity
815
+ vn = wp.dot(n, v)
816
+ vt = v - n * vn
817
+
818
+ # contact elastic
819
+ fn = n * c * ke
820
+
821
+ # contact damping
822
+ fd = n * wp.min(vn, 0.0) * kd
823
+
824
+ # viscous friction
825
+ # ft = vt*kf
826
+
827
+ # Coulomb friction (box)
828
+ # lower = mu * c * ke
829
+ # upper = -lower
830
+
831
+ # vx = wp.clamp(wp.dot(wp.vec3(kf, 0.0, 0.0), vt), lower, upper)
832
+ # vz = wp.clamp(wp.dot(wp.vec3(0.0, 0.0, kf), vt), lower, upper)
833
+
834
+ # ft = wp.vec3(vx, 0.0, vz)
835
+
836
+ # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
837
+ ft = wp.normalize(vt) * wp.min(kf * wp.length(vt), abs(mu * c * ke))
838
+
839
+ f_total = fn + (fd + ft)
840
+ t_total = wp.cross(r, f_total)
841
+
842
+ wp.atomic_sub(particle_f, particle_index, f_total)
843
+
844
+ if body_index >= 0:
845
+ wp.atomic_add(body_f, body_index, wp.spatial_vector(t_total, f_total))
846
+
847
+
848
+ @wp.kernel
849
+ def eval_rigid_contacts(
850
+ body_q: wp.array(dtype=wp.transform),
851
+ body_qd: wp.array(dtype=wp.spatial_vector),
852
+ body_com: wp.array(dtype=wp.vec3),
853
+ shape_materials: ModelShapeMaterials,
854
+ geo: ModelShapeGeometry,
855
+ shape_body: wp.array(dtype=int),
856
+ contact_count: wp.array(dtype=int),
857
+ contact_point0: wp.array(dtype=wp.vec3),
858
+ contact_point1: wp.array(dtype=wp.vec3),
859
+ contact_normal: wp.array(dtype=wp.vec3),
860
+ contact_shape0: wp.array(dtype=int),
861
+ contact_shape1: wp.array(dtype=int),
862
+ force_in_world_frame: bool,
863
+ # outputs
864
+ body_f: wp.array(dtype=wp.spatial_vector),
865
+ ):
866
+ tid = wp.tid()
867
+
868
+ count = contact_count[0]
869
+ if tid >= count:
870
+ return
871
+
872
+ # retrieve contact thickness, compute average contact material properties
873
+ ke = 0.0 # contact normal force stiffness
874
+ kd = 0.0 # damping coefficient
875
+ kf = 0.0 # friction force stiffness
876
+ ka = 0.0 # adhesion distance
877
+ mu = 0.0 # friction coefficient
878
+ mat_nonzero = 0
879
+ thickness_a = 0.0
880
+ thickness_b = 0.0
881
+ shape_a = contact_shape0[tid]
882
+ shape_b = contact_shape1[tid]
883
+ if shape_a == shape_b:
884
+ return
885
+ body_a = -1
886
+ body_b = -1
887
+ if shape_a >= 0:
888
+ mat_nonzero += 1
889
+ ke += shape_materials.ke[shape_a]
890
+ kd += shape_materials.kd[shape_a]
891
+ kf += shape_materials.kf[shape_a]
892
+ ka += shape_materials.ka[shape_a]
893
+ mu += shape_materials.mu[shape_a]
894
+ thickness_a = geo.thickness[shape_a]
895
+ body_a = shape_body[shape_a]
896
+ if shape_b >= 0:
897
+ mat_nonzero += 1
898
+ ke += shape_materials.ke[shape_b]
899
+ kd += shape_materials.kd[shape_b]
900
+ kf += shape_materials.kf[shape_b]
901
+ ka += shape_materials.ka[shape_b]
902
+ mu += shape_materials.mu[shape_b]
903
+ thickness_b = geo.thickness[shape_b]
904
+ body_b = shape_body[shape_b]
905
+ if mat_nonzero > 0:
906
+ ke /= float(mat_nonzero)
907
+ kd /= float(mat_nonzero)
908
+ kf /= float(mat_nonzero)
909
+ ka /= float(mat_nonzero)
910
+ mu /= float(mat_nonzero)
911
+
912
+ # contact normal in world space
913
+ n = contact_normal[tid]
914
+ bx_a = contact_point0[tid]
915
+ bx_b = contact_point1[tid]
916
+ if body_a >= 0:
917
+ X_wb_a = body_q[body_a]
918
+ X_com_a = body_com[body_a]
919
+ bx_a = wp.transform_point(X_wb_a, bx_a) - thickness_a * n
920
+ r_a = bx_a - wp.transform_point(X_wb_a, X_com_a)
921
+
922
+ if body_b >= 0:
923
+ X_wb_b = body_q[body_b]
924
+ X_com_b = body_com[body_b]
925
+ bx_b = wp.transform_point(X_wb_b, bx_b) + thickness_b * n
926
+ r_b = bx_b - wp.transform_point(X_wb_b, X_com_b)
927
+
928
+ d = wp.dot(n, bx_a - bx_b)
929
+
930
+ if d >= ka:
931
+ return
932
+
933
+ # compute contact point velocity
934
+ bv_a = wp.vec3(0.0)
935
+ bv_b = wp.vec3(0.0)
936
+ if body_a >= 0:
937
+ body_v_s_a = body_qd[body_a]
938
+ body_w_a = wp.spatial_top(body_v_s_a)
939
+ body_v_a = wp.spatial_bottom(body_v_s_a)
940
+ if force_in_world_frame:
941
+ bv_a = body_v_a + wp.cross(body_w_a, bx_a)
942
+ else:
943
+ bv_a = body_v_a + wp.cross(body_w_a, r_a)
944
+
945
+ if body_b >= 0:
946
+ body_v_s_b = body_qd[body_b]
947
+ body_w_b = wp.spatial_top(body_v_s_b)
948
+ body_v_b = wp.spatial_bottom(body_v_s_b)
949
+ if force_in_world_frame:
950
+ bv_b = body_v_b + wp.cross(body_w_b, bx_b)
951
+ else:
952
+ bv_b = body_v_b + wp.cross(body_w_b, r_b)
953
+
954
+ # relative velocity
955
+ v = bv_a - bv_b
956
+
957
+ # print(v)
958
+
959
+ # decompose relative velocity
960
+ vn = wp.dot(n, v)
961
+ vt = v - n * vn
962
+
963
+ # contact elastic
964
+ fn = d * ke
965
+
966
+ # contact damping
967
+ fd = wp.min(vn, 0.0) * kd * wp.step(d)
968
+
969
+ # viscous friction
970
+ # ft = vt*kf
971
+
972
+ # Coulomb friction (box)
973
+ # lower = mu * d * ke
974
+ # upper = -lower
975
+
976
+ # vx = wp.clamp(wp.dot(wp.vec3(kf, 0.0, 0.0), vt), lower, upper)
977
+ # vz = wp.clamp(wp.dot(wp.vec3(0.0, 0.0, kf), vt), lower, upper)
978
+
979
+ # ft = wp.vec3(vx, 0.0, vz)
980
+
981
+ # Coulomb friction (smooth, but gradients are numerically unstable around |vt| = 0)
982
+ # ft = wp.normalize(vt)*wp.min(kf*wp.length(vt), abs(mu*d*ke))
983
+ ft = wp.vec3(0.0)
984
+ if d < 0.0:
985
+ ft = wp.normalize(vt) * wp.min(kf * wp.length(vt), -mu * (fn + fd))
986
+
987
+ f_total = n * (fn + fd) + ft
988
+ # f_total = n * fn
989
+
990
+ if body_a >= 0:
991
+ if force_in_world_frame:
992
+ wp.atomic_add(body_f, body_a, wp.spatial_vector(wp.cross(bx_a, f_total), f_total))
993
+ else:
994
+ wp.atomic_sub(body_f, body_a, wp.spatial_vector(wp.cross(r_a, f_total), f_total))
995
+
996
+ if body_b >= 0:
997
+ if force_in_world_frame:
998
+ wp.atomic_sub(body_f, body_b, wp.spatial_vector(wp.cross(bx_b, f_total), f_total))
999
+ else:
1000
+ wp.atomic_add(body_f, body_b, wp.spatial_vector(wp.cross(r_b, f_total), f_total))
1001
+
1002
+
1003
+ @wp.func
1004
+ def eval_joint_force(
1005
+ q: float,
1006
+ qd: float,
1007
+ act: float,
1008
+ target_ke: float,
1009
+ target_kd: float,
1010
+ limit_lower: float,
1011
+ limit_upper: float,
1012
+ limit_ke: float,
1013
+ limit_kd: float,
1014
+ mode: wp.int32,
1015
+ ) -> float:
1016
+ """Joint force evaluation for a single degree of freedom."""
1017
+
1018
+ limit_f = 0.0
1019
+ damping_f = 0.0
1020
+ target_f = 0.0
1021
+
1022
+ if mode == wp.sim.JOINT_MODE_FORCE:
1023
+ target_f = act
1024
+ elif mode == wp.sim.JOINT_MODE_TARGET_POSITION:
1025
+ target_f = target_ke * (act - q) - target_kd * qd
1026
+ elif mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1027
+ target_f = target_ke * (act - qd)
1028
+
1029
+ # compute limit forces, damping only active when limit is violated
1030
+ if q < limit_lower:
1031
+ limit_f = limit_ke * (limit_lower - q)
1032
+ damping_f = -limit_kd * qd
1033
+ if mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1034
+ target_f = 0.0 # override target force when limit is violated
1035
+ elif q > limit_upper:
1036
+ limit_f = limit_ke * (limit_upper - q)
1037
+ damping_f = -limit_kd * qd
1038
+ if mode == wp.sim.JOINT_MODE_TARGET_VELOCITY:
1039
+ target_f = 0.0 # override target force when limit is violated
1040
+
1041
+ return limit_f + damping_f + target_f
1042
+
1043
+
1044
+ @wp.kernel
1045
+ def eval_body_joints(
1046
+ body_q: wp.array(dtype=wp.transform),
1047
+ body_qd: wp.array(dtype=wp.spatial_vector),
1048
+ body_com: wp.array(dtype=wp.vec3),
1049
+ joint_qd_start: wp.array(dtype=int),
1050
+ joint_type: wp.array(dtype=int),
1051
+ joint_enabled: wp.array(dtype=int),
1052
+ joint_child: wp.array(dtype=int),
1053
+ joint_parent: wp.array(dtype=int),
1054
+ joint_X_p: wp.array(dtype=wp.transform),
1055
+ joint_X_c: wp.array(dtype=wp.transform),
1056
+ joint_axis: wp.array(dtype=wp.vec3),
1057
+ joint_axis_start: wp.array(dtype=int),
1058
+ joint_axis_dim: wp.array(dtype=int, ndim=2),
1059
+ joint_axis_mode: wp.array(dtype=int),
1060
+ joint_act: wp.array(dtype=float),
1061
+ joint_target_ke: wp.array(dtype=float),
1062
+ joint_target_kd: wp.array(dtype=float),
1063
+ joint_limit_lower: wp.array(dtype=float),
1064
+ joint_limit_upper: wp.array(dtype=float),
1065
+ joint_limit_ke: wp.array(dtype=float),
1066
+ joint_limit_kd: wp.array(dtype=float),
1067
+ joint_attach_ke: float,
1068
+ joint_attach_kd: float,
1069
+ body_f: wp.array(dtype=wp.spatial_vector),
1070
+ ):
1071
+ tid = wp.tid()
1072
+ type = joint_type[tid]
1073
+
1074
+ # early out for free joints
1075
+ if joint_enabled[tid] == 0 or type == wp.sim.JOINT_FREE:
1076
+ return
1077
+
1078
+ c_child = joint_child[tid]
1079
+ c_parent = joint_parent[tid]
1080
+
1081
+ X_pj = joint_X_p[tid]
1082
+ X_cj = joint_X_c[tid]
1083
+
1084
+ X_wp = X_pj
1085
+ r_p = wp.vec3()
1086
+ w_p = wp.vec3()
1087
+ v_p = wp.vec3()
1088
+
1089
+ # parent transform and moment arm
1090
+ if c_parent >= 0:
1091
+ X_wp = body_q[c_parent] * X_wp
1092
+ r_p = wp.transform_get_translation(X_wp) - wp.transform_point(body_q[c_parent], body_com[c_parent])
1093
+
1094
+ twist_p = body_qd[c_parent]
1095
+
1096
+ w_p = wp.spatial_top(twist_p)
1097
+ v_p = wp.spatial_bottom(twist_p) + wp.cross(w_p, r_p)
1098
+
1099
+ # child transform and moment arm
1100
+ X_wc = body_q[c_child] * X_cj
1101
+ r_c = wp.transform_get_translation(X_wc) - wp.transform_point(body_q[c_child], body_com[c_child])
1102
+
1103
+ twist_c = body_qd[c_child]
1104
+
1105
+ w_c = wp.spatial_top(twist_c)
1106
+ v_c = wp.spatial_bottom(twist_c) + wp.cross(w_c, r_c)
1107
+
1108
+ # joint properties (for 1D joints)
1109
+ # q_start = joint_q_start[tid]
1110
+ # qd_start = joint_qd_start[tid]
1111
+ axis_start = joint_axis_start[tid]
1112
+
1113
+ lin_axis_count = joint_axis_dim[tid, 0]
1114
+ ang_axis_count = joint_axis_dim[tid, 1]
1115
+
1116
+ x_p = wp.transform_get_translation(X_wp)
1117
+ x_c = wp.transform_get_translation(X_wc)
1118
+
1119
+ q_p = wp.transform_get_rotation(X_wp)
1120
+ q_c = wp.transform_get_rotation(X_wc)
1121
+
1122
+ # translational error
1123
+ x_err = x_c - x_p
1124
+ r_err = wp.quat_inverse(q_p) * q_c
1125
+ v_err = v_c - v_p
1126
+ w_err = w_c - w_p
1127
+
1128
+ # total force/torque on the parent
1129
+ t_total = wp.vec3()
1130
+ f_total = wp.vec3()
1131
+
1132
+ # reduce angular damping stiffness for stability
1133
+ angular_damping_scale = 0.01
1134
+
1135
+ if type == wp.sim.JOINT_FIXED:
1136
+ ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1137
+
1138
+ f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1139
+ t_total += (
1140
+ wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1141
+ )
1142
+
1143
+ if type == wp.sim.JOINT_PRISMATIC:
1144
+ axis = joint_axis[axis_start]
1145
+
1146
+ # world space joint axis
1147
+ axis_p = wp.transform_vector(X_wp, axis)
1148
+
1149
+ # evaluate joint coordinates
1150
+ q = wp.dot(x_err, axis_p)
1151
+ qd = wp.dot(v_err, axis_p)
1152
+ act = joint_act[axis_start]
1153
+
1154
+ f_total = axis_p * -eval_joint_force(
1155
+ q,
1156
+ qd,
1157
+ act,
1158
+ joint_target_ke[axis_start],
1159
+ joint_target_kd[axis_start],
1160
+ joint_limit_lower[axis_start],
1161
+ joint_limit_upper[axis_start],
1162
+ joint_limit_ke[axis_start],
1163
+ joint_limit_kd[axis_start],
1164
+ joint_axis_mode[axis_start],
1165
+ )
1166
+
1167
+ # attachment dynamics
1168
+ ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1169
+
1170
+ # project off any displacement along the joint axis
1171
+ f_total += (x_err - q * axis_p) * joint_attach_ke + (v_err - qd * axis_p) * joint_attach_kd
1172
+ t_total += (
1173
+ wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1174
+ )
1175
+
1176
+ if type == wp.sim.JOINT_REVOLUTE:
1177
+ axis = joint_axis[axis_start]
1178
+
1179
+ axis_p = wp.transform_vector(X_wp, axis)
1180
+ axis_c = wp.transform_vector(X_wc, axis)
1181
+
1182
+ # swing twist decomposition
1183
+ twist = quat_twist(axis, r_err)
1184
+
1185
+ q = wp.acos(twist[3]) * 2.0 * wp.sign(wp.dot(axis, wp.vec3(twist[0], twist[1], twist[2])))
1186
+ qd = wp.dot(w_err, axis_p)
1187
+ act = joint_act[axis_start]
1188
+
1189
+ t_total = axis_p * -eval_joint_force(
1190
+ q,
1191
+ qd,
1192
+ act,
1193
+ joint_target_ke[axis_start],
1194
+ joint_target_kd[axis_start],
1195
+ joint_limit_lower[axis_start],
1196
+ joint_limit_upper[axis_start],
1197
+ joint_limit_ke[axis_start],
1198
+ joint_limit_kd[axis_start],
1199
+ joint_axis_mode[axis_start],
1200
+ )
1201
+
1202
+ # attachment dynamics
1203
+ swing_err = wp.cross(axis_p, axis_c)
1204
+
1205
+ f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1206
+ t_total += swing_err * joint_attach_ke + (w_err - qd * axis_p) * joint_attach_kd * angular_damping_scale
1207
+
1208
+ if type == wp.sim.JOINT_BALL:
1209
+ ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1210
+
1211
+ # TODO joint limits
1212
+ # TODO expose target_kd or target_ke for ball joints
1213
+ # t_total += target_kd * w_err + target_ke * wp.transform_vector(X_wp, ang_err)
1214
+ f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1215
+
1216
+ if type == wp.sim.JOINT_COMPOUND:
1217
+ q_pc = wp.quat_inverse(q_p) * q_c
1218
+
1219
+ # decompose to a compound rotation each axis
1220
+ angles = quat_decompose(q_pc)
1221
+
1222
+ # reconstruct rotation axes
1223
+ axis_0 = wp.vec3(1.0, 0.0, 0.0)
1224
+ q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1225
+
1226
+ axis_1 = wp.quat_rotate(q_0, wp.vec3(0.0, 1.0, 0.0))
1227
+ q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1228
+
1229
+ axis_2 = wp.quat_rotate(q_1 * q_0, wp.vec3(0.0, 0.0, 1.0))
1230
+ # q_2 = wp.quat_from_axis_angle(axis_2, angles[2])
1231
+
1232
+ # q_w = q_p
1233
+
1234
+ axis_0 = wp.transform_vector(X_wp, axis_0)
1235
+ axis_1 = wp.transform_vector(X_wp, axis_1)
1236
+ axis_2 = wp.transform_vector(X_wp, axis_2)
1237
+
1238
+ # joint dynamics
1239
+ # # TODO remove wp.quat_rotate(q_w, ...)?
1240
+ # t_total += eval_joint_force(angles[0], wp.dot(wp.quat_rotate(q_w, axis_0), w_err), joint_target[axis_start+0], joint_target_ke[axis_start+0],joint_target_kd[axis_start+0], joint_act[axis_start+0], joint_limit_lower[axis_start+0], joint_limit_upper[axis_start+0], joint_limit_ke[axis_start+0], joint_limit_kd[axis_start+0], wp.quat_rotate(q_w, axis_0))
1241
+ # t_total += eval_joint_force(angles[1], wp.dot(wp.quat_rotate(q_w, axis_1), w_err), joint_target[axis_start+1], joint_target_ke[axis_start+1],joint_target_kd[axis_start+1], joint_act[axis_start+1], joint_limit_lower[axis_start+1], joint_limit_upper[axis_start+1], joint_limit_ke[axis_start+1], joint_limit_kd[axis_start+1], wp.quat_rotate(q_w, axis_1))
1242
+ # t_total += eval_joint_force(angles[2], wp.dot(wp.quat_rotate(q_w, axis_2), w_err), joint_target[axis_start+2], joint_target_ke[axis_start+2],joint_target_kd[axis_start+2], joint_act[axis_start+2], joint_limit_lower[axis_start+2], joint_limit_upper[axis_start+2], joint_limit_ke[axis_start+2], joint_limit_kd[axis_start+2], wp.quat_rotate(q_w, axis_2))
1243
+
1244
+ t_total += axis_0 * -eval_joint_force(
1245
+ angles[0],
1246
+ wp.dot(axis_0, w_err),
1247
+ joint_act[axis_start + 0],
1248
+ joint_target_ke[axis_start + 0],
1249
+ joint_target_kd[axis_start + 0],
1250
+ joint_limit_lower[axis_start + 0],
1251
+ joint_limit_upper[axis_start + 0],
1252
+ joint_limit_ke[axis_start + 0],
1253
+ joint_limit_kd[axis_start + 0],
1254
+ joint_axis_mode[axis_start + 0],
1255
+ )
1256
+ t_total += axis_1 * -eval_joint_force(
1257
+ angles[1],
1258
+ wp.dot(axis_1, w_err),
1259
+ joint_act[axis_start + 1],
1260
+ joint_target_ke[axis_start + 1],
1261
+ joint_target_kd[axis_start + 1],
1262
+ joint_limit_lower[axis_start + 1],
1263
+ joint_limit_upper[axis_start + 1],
1264
+ joint_limit_ke[axis_start + 1],
1265
+ joint_limit_kd[axis_start + 1],
1266
+ joint_axis_mode[axis_start + 1],
1267
+ )
1268
+ t_total += axis_2 * -eval_joint_force(
1269
+ angles[2],
1270
+ wp.dot(axis_2, w_err),
1271
+ joint_act[axis_start + 2],
1272
+ joint_target_ke[axis_start + 2],
1273
+ joint_target_kd[axis_start + 2],
1274
+ joint_limit_lower[axis_start + 2],
1275
+ joint_limit_upper[axis_start + 2],
1276
+ joint_limit_ke[axis_start + 2],
1277
+ joint_limit_kd[axis_start + 2],
1278
+ joint_axis_mode[axis_start + 2],
1279
+ )
1280
+
1281
+ f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1282
+
1283
+ if type == wp.sim.JOINT_UNIVERSAL:
1284
+ q_pc = wp.quat_inverse(q_p) * q_c
1285
+
1286
+ # decompose to a compound rotation each axis
1287
+ angles = quat_decompose(q_pc)
1288
+
1289
+ # reconstruct rotation axes
1290
+ axis_0 = wp.vec3(1.0, 0.0, 0.0)
1291
+ q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1292
+
1293
+ axis_1 = wp.quat_rotate(q_0, wp.vec3(0.0, 1.0, 0.0))
1294
+ q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1295
+
1296
+ axis_2 = wp.quat_rotate(q_1 * q_0, wp.vec3(0.0, 0.0, 1.0))
1297
+
1298
+ axis_0 = wp.transform_vector(X_wp, axis_0)
1299
+ axis_1 = wp.transform_vector(X_wp, axis_1)
1300
+ axis_2 = wp.transform_vector(X_wp, axis_2)
1301
+
1302
+ # joint dynamics
1303
+
1304
+ t_total += axis_0 * -eval_joint_force(
1305
+ angles[0],
1306
+ wp.dot(axis_0, w_err),
1307
+ joint_act[axis_start + 0],
1308
+ joint_target_ke[axis_start + 0],
1309
+ joint_target_kd[axis_start + 0],
1310
+ joint_limit_lower[axis_start + 0],
1311
+ joint_limit_upper[axis_start + 0],
1312
+ joint_limit_ke[axis_start + 0],
1313
+ joint_limit_kd[axis_start + 0],
1314
+ joint_axis_mode[axis_start + 0],
1315
+ )
1316
+ t_total += axis_1 * -eval_joint_force(
1317
+ angles[1],
1318
+ wp.dot(axis_1, w_err),
1319
+ joint_act[axis_start + 1],
1320
+ joint_target_ke[axis_start + 1],
1321
+ joint_target_kd[axis_start + 1],
1322
+ joint_limit_lower[axis_start + 1],
1323
+ joint_limit_upper[axis_start + 1],
1324
+ joint_limit_ke[axis_start + 1],
1325
+ joint_limit_kd[axis_start + 1],
1326
+ joint_axis_mode[axis_start + 1],
1327
+ )
1328
+
1329
+ # last axis (fixed)
1330
+ t_total += axis_2 * -eval_joint_force(
1331
+ angles[2],
1332
+ wp.dot(axis_2, w_err),
1333
+ 0.0,
1334
+ joint_attach_ke,
1335
+ joint_attach_kd * angular_damping_scale,
1336
+ 0.0,
1337
+ 0.0,
1338
+ 0.0,
1339
+ 0.0,
1340
+ wp.sim.JOINT_MODE_FORCE,
1341
+ )
1342
+
1343
+ f_total += x_err * joint_attach_ke + v_err * joint_attach_kd
1344
+
1345
+ if type == wp.sim.JOINT_D6:
1346
+ pos = wp.vec3(0.0)
1347
+ vel = wp.vec3(0.0)
1348
+ if lin_axis_count >= 1:
1349
+ axis_0 = wp.transform_vector(X_wp, joint_axis[axis_start + 0])
1350
+ q0 = wp.dot(x_err, axis_0)
1351
+ qd0 = wp.dot(v_err, axis_0)
1352
+
1353
+ f_total += axis_0 * -eval_joint_force(
1354
+ q0,
1355
+ qd0,
1356
+ joint_act[axis_start + 0],
1357
+ joint_target_ke[axis_start + 0],
1358
+ joint_target_kd[axis_start + 0],
1359
+ joint_limit_lower[axis_start + 0],
1360
+ joint_limit_upper[axis_start + 0],
1361
+ joint_limit_ke[axis_start + 0],
1362
+ joint_limit_kd[axis_start + 0],
1363
+ joint_axis_mode[axis_start + 0],
1364
+ )
1365
+
1366
+ pos += q0 * axis_0
1367
+ vel += qd0 * axis_0
1368
+
1369
+ if lin_axis_count >= 2:
1370
+ axis_1 = wp.transform_vector(X_wp, joint_axis[axis_start + 1])
1371
+ q1 = wp.dot(x_err, axis_1)
1372
+ qd1 = wp.dot(v_err, axis_1)
1373
+
1374
+ f_total += axis_1 * -eval_joint_force(
1375
+ q1,
1376
+ qd1,
1377
+ joint_act[axis_start + 1],
1378
+ joint_target_ke[axis_start + 1],
1379
+ joint_target_kd[axis_start + 1],
1380
+ joint_limit_lower[axis_start + 1],
1381
+ joint_limit_upper[axis_start + 1],
1382
+ joint_limit_ke[axis_start + 1],
1383
+ joint_limit_kd[axis_start + 1],
1384
+ joint_axis_mode[axis_start + 1],
1385
+ )
1386
+
1387
+ pos += q1 * axis_1
1388
+ vel += qd1 * axis_1
1389
+
1390
+ if lin_axis_count == 3:
1391
+ axis_2 = wp.transform_vector(X_wp, joint_axis[axis_start + 2])
1392
+ q2 = wp.dot(x_err, axis_2)
1393
+ qd2 = wp.dot(v_err, axis_2)
1394
+
1395
+ f_total += axis_2 * -eval_joint_force(
1396
+ q2,
1397
+ qd2,
1398
+ joint_act[axis_start + 2],
1399
+ joint_target_ke[axis_start + 2],
1400
+ joint_target_kd[axis_start + 2],
1401
+ joint_limit_lower[axis_start + 2],
1402
+ joint_limit_upper[axis_start + 2],
1403
+ joint_limit_ke[axis_start + 2],
1404
+ joint_limit_kd[axis_start + 2],
1405
+ joint_axis_mode[axis_start + 2],
1406
+ )
1407
+
1408
+ pos += q2 * axis_2
1409
+ vel += qd2 * axis_2
1410
+
1411
+ f_total += (x_err - pos) * joint_attach_ke + (v_err - vel) * joint_attach_kd
1412
+
1413
+ if ang_axis_count == 0:
1414
+ ang_err = wp.normalize(wp.vec3(r_err[0], r_err[1], r_err[2])) * wp.acos(r_err[3]) * 2.0
1415
+ t_total += (
1416
+ wp.transform_vector(X_wp, ang_err) * joint_attach_ke + w_err * joint_attach_kd * angular_damping_scale
1417
+ )
1418
+
1419
+ i_0 = lin_axis_count + axis_start + 0
1420
+ i_1 = lin_axis_count + axis_start + 1
1421
+ i_2 = lin_axis_count + axis_start + 2
1422
+
1423
+ if ang_axis_count == 1:
1424
+ axis = joint_axis[i_0]
1425
+
1426
+ axis_p = wp.transform_vector(X_wp, axis)
1427
+ axis_c = wp.transform_vector(X_wc, axis)
1428
+
1429
+ # swing twist decomposition
1430
+ twist = quat_twist(axis, r_err)
1431
+
1432
+ q = wp.acos(twist[3]) * 2.0 * wp.sign(wp.dot(axis, wp.vec3(twist[0], twist[1], twist[2])))
1433
+ qd = wp.dot(w_err, axis_p)
1434
+
1435
+ t_total = axis_p * -eval_joint_force(
1436
+ q,
1437
+ qd,
1438
+ joint_act[i_0],
1439
+ joint_target_ke[i_0],
1440
+ joint_target_kd[i_0],
1441
+ joint_limit_lower[i_0],
1442
+ joint_limit_upper[i_0],
1443
+ joint_limit_ke[i_0],
1444
+ joint_limit_kd[i_0],
1445
+ joint_axis_mode[i_0],
1446
+ )
1447
+
1448
+ # attachment dynamics
1449
+ swing_err = wp.cross(axis_p, axis_c)
1450
+
1451
+ t_total += swing_err * joint_attach_ke + (w_err - qd * axis_p) * joint_attach_kd * angular_damping_scale
1452
+
1453
+ if ang_axis_count == 2:
1454
+ q_pc = wp.quat_inverse(q_p) * q_c
1455
+
1456
+ # decompose to a compound rotation each axis
1457
+ angles = quat_decompose(q_pc)
1458
+
1459
+ orig_axis_0 = joint_axis[i_0]
1460
+ orig_axis_1 = joint_axis[i_1]
1461
+ orig_axis_2 = wp.cross(orig_axis_0, orig_axis_1)
1462
+
1463
+ # reconstruct rotation axes
1464
+ axis_0 = orig_axis_0
1465
+ q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1466
+
1467
+ axis_1 = wp.quat_rotate(q_0, orig_axis_1)
1468
+ q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1469
+
1470
+ axis_2 = wp.quat_rotate(q_1 * q_0, orig_axis_2)
1471
+
1472
+ axis_0 = wp.transform_vector(X_wp, axis_0)
1473
+ axis_1 = wp.transform_vector(X_wp, axis_1)
1474
+ axis_2 = wp.transform_vector(X_wp, axis_2)
1475
+
1476
+ # joint dynamics
1477
+
1478
+ t_total += axis_0 * -eval_joint_force(
1479
+ angles[0],
1480
+ wp.dot(axis_0, w_err),
1481
+ joint_act[i_0],
1482
+ joint_target_ke[i_0],
1483
+ joint_target_kd[i_0],
1484
+ joint_limit_lower[i_0],
1485
+ joint_limit_upper[i_0],
1486
+ joint_limit_ke[i_0],
1487
+ joint_limit_kd[i_0],
1488
+ joint_axis_mode[i_0],
1489
+ )
1490
+ t_total += axis_1 * -eval_joint_force(
1491
+ angles[1],
1492
+ wp.dot(axis_1, w_err),
1493
+ joint_act[i_1],
1494
+ joint_target_ke[i_1],
1495
+ joint_target_kd[i_1],
1496
+ joint_limit_lower[i_1],
1497
+ joint_limit_upper[i_1],
1498
+ joint_limit_ke[i_1],
1499
+ joint_limit_kd[i_1],
1500
+ joint_axis_mode[i_1],
1501
+ )
1502
+
1503
+ # last axis (fixed)
1504
+ t_total += axis_2 * -eval_joint_force(
1505
+ angles[2],
1506
+ wp.dot(axis_2, w_err),
1507
+ 0.0,
1508
+ joint_attach_ke,
1509
+ joint_attach_kd * angular_damping_scale,
1510
+ 0.0,
1511
+ 0.0,
1512
+ 0.0,
1513
+ 0.0,
1514
+ wp.sim.JOINT_MODE_FORCE,
1515
+ )
1516
+
1517
+ if ang_axis_count == 3:
1518
+ q_pc = wp.quat_inverse(q_p) * q_c
1519
+
1520
+ # decompose to a compound rotation each axis
1521
+ angles = quat_decompose(q_pc)
1522
+
1523
+ orig_axis_0 = joint_axis[i_0]
1524
+ orig_axis_1 = joint_axis[i_1]
1525
+ orig_axis_2 = joint_axis[i_2]
1526
+
1527
+ # reconstruct rotation axes
1528
+ axis_0 = orig_axis_0
1529
+ q_0 = wp.quat_from_axis_angle(axis_0, angles[0])
1530
+
1531
+ axis_1 = wp.quat_rotate(q_0, orig_axis_1)
1532
+ q_1 = wp.quat_from_axis_angle(axis_1, angles[1])
1533
+
1534
+ axis_2 = wp.quat_rotate(q_1 * q_0, orig_axis_2)
1535
+
1536
+ axis_0 = wp.transform_vector(X_wp, axis_0)
1537
+ axis_1 = wp.transform_vector(X_wp, axis_1)
1538
+ axis_2 = wp.transform_vector(X_wp, axis_2)
1539
+
1540
+ t_total += axis_0 * -eval_joint_force(
1541
+ angles[0],
1542
+ wp.dot(axis_0, w_err),
1543
+ joint_act[i_0],
1544
+ joint_target_ke[i_0],
1545
+ joint_target_kd[i_0],
1546
+ joint_limit_lower[i_0],
1547
+ joint_limit_upper[i_0],
1548
+ joint_limit_ke[i_0],
1549
+ joint_limit_kd[i_0],
1550
+ joint_axis_mode[i_0],
1551
+ )
1552
+ t_total += axis_1 * -eval_joint_force(
1553
+ angles[1],
1554
+ wp.dot(axis_1, w_err),
1555
+ joint_act[i_1],
1556
+ joint_target_ke[i_1],
1557
+ joint_target_kd[i_1],
1558
+ joint_limit_lower[i_1],
1559
+ joint_limit_upper[i_1],
1560
+ joint_limit_ke[i_1],
1561
+ joint_limit_kd[i_1],
1562
+ joint_axis_mode[i_1],
1563
+ )
1564
+ t_total += axis_2 * -eval_joint_force(
1565
+ angles[2],
1566
+ wp.dot(axis_2, w_err),
1567
+ joint_act[i_2],
1568
+ joint_target_ke[i_2],
1569
+ joint_target_kd[i_2],
1570
+ joint_limit_lower[i_2],
1571
+ joint_limit_upper[i_2],
1572
+ joint_limit_ke[i_2],
1573
+ joint_limit_kd[i_2],
1574
+ joint_axis_mode[i_2],
1575
+ )
1576
+
1577
+ # write forces
1578
+ if c_parent >= 0:
1579
+ wp.atomic_add(body_f, c_parent, wp.spatial_vector(t_total + wp.cross(r_p, f_total), f_total))
1580
+
1581
+ wp.atomic_sub(body_f, c_child, wp.spatial_vector(t_total + wp.cross(r_c, f_total), f_total))
1582
+
1583
+
1584
+ @wp.func
1585
+ def compute_muscle_force(
1586
+ i: int,
1587
+ body_X_s: wp.array(dtype=wp.transform),
1588
+ body_v_s: wp.array(dtype=wp.spatial_vector),
1589
+ body_com: wp.array(dtype=wp.vec3),
1590
+ muscle_links: wp.array(dtype=int),
1591
+ muscle_points: wp.array(dtype=wp.vec3),
1592
+ muscle_activation: float,
1593
+ body_f_s: wp.array(dtype=wp.spatial_vector),
1594
+ ):
1595
+ link_0 = muscle_links[i]
1596
+ link_1 = muscle_links[i + 1]
1597
+
1598
+ if link_0 == link_1:
1599
+ return 0
1600
+
1601
+ r_0 = muscle_points[i]
1602
+ r_1 = muscle_points[i + 1]
1603
+
1604
+ xform_0 = body_X_s[link_0]
1605
+ xform_1 = body_X_s[link_1]
1606
+
1607
+ pos_0 = wp.transform_point(xform_0, r_0 - body_com[link_0])
1608
+ pos_1 = wp.transform_point(xform_1, r_1 - body_com[link_1])
1609
+
1610
+ n = wp.normalize(pos_1 - pos_0)
1611
+
1612
+ # todo: add passive elastic and viscosity terms
1613
+ f = n * muscle_activation
1614
+
1615
+ wp.atomic_sub(body_f_s, link_0, wp.spatial_vector(f, wp.cross(pos_0, f)))
1616
+ wp.atomic_add(body_f_s, link_1, wp.spatial_vector(f, wp.cross(pos_1, f)))
1617
+
1618
+
1619
+ @wp.kernel
1620
+ def eval_muscles(
1621
+ body_X_s: wp.array(dtype=wp.transform),
1622
+ body_v_s: wp.array(dtype=wp.spatial_vector),
1623
+ body_com: wp.array(dtype=wp.vec3),
1624
+ muscle_start: wp.array(dtype=int),
1625
+ muscle_params: wp.array(dtype=float),
1626
+ muscle_links: wp.array(dtype=int),
1627
+ muscle_points: wp.array(dtype=wp.vec3),
1628
+ muscle_activation: wp.array(dtype=float),
1629
+ # output
1630
+ body_f_s: wp.array(dtype=wp.spatial_vector),
1631
+ ):
1632
+ tid = wp.tid()
1633
+
1634
+ m_start = muscle_start[tid]
1635
+ m_end = muscle_start[tid + 1] - 1
1636
+
1637
+ activation = muscle_activation[tid]
1638
+
1639
+ for i in range(m_start, m_end):
1640
+ compute_muscle_force(i, body_X_s, body_v_s, body_com, muscle_links, muscle_points, activation, body_f_s)
1641
+
1642
+
1643
+ def eval_spring_forces(model: Model, state: State, particle_f: wp.array):
1644
+ if model.spring_count:
1645
+ wp.launch(
1646
+ kernel=eval_springs,
1647
+ dim=model.spring_count,
1648
+ inputs=[
1649
+ state.particle_q,
1650
+ state.particle_qd,
1651
+ model.spring_indices,
1652
+ model.spring_rest_length,
1653
+ model.spring_stiffness,
1654
+ model.spring_damping,
1655
+ ],
1656
+ outputs=[particle_f],
1657
+ device=model.device,
1658
+ )
1659
+
1660
+
1661
+ def eval_triangle_forces(model: Model, state: State, control: Control, particle_f: wp.array):
1662
+ if model.tri_count:
1663
+ wp.launch(
1664
+ kernel=eval_triangles,
1665
+ dim=model.tri_count,
1666
+ inputs=[
1667
+ state.particle_q,
1668
+ state.particle_qd,
1669
+ model.tri_indices,
1670
+ model.tri_poses,
1671
+ control.tri_activations,
1672
+ model.tri_materials,
1673
+ ],
1674
+ outputs=[particle_f],
1675
+ device=model.device,
1676
+ )
1677
+
1678
+
1679
+ def eval_triangle_contact_forces(model: Model, state: State, particle_f: wp.array):
1680
+ if model.enable_tri_collisions:
1681
+ wp.launch(
1682
+ kernel=eval_triangles_contact,
1683
+ dim=model.tri_count * model.particle_count,
1684
+ inputs=[
1685
+ model.particle_count,
1686
+ state.particle_q,
1687
+ state.particle_qd,
1688
+ model.tri_indices,
1689
+ model.tri_materials,
1690
+ ],
1691
+ outputs=[particle_f],
1692
+ device=model.device,
1693
+ )
1694
+
1695
+
1696
+ def eval_bending_forces(model: Model, state: State, particle_f: wp.array):
1697
+ if model.edge_count:
1698
+ wp.launch(
1699
+ kernel=eval_bending,
1700
+ dim=model.edge_count,
1701
+ inputs=[
1702
+ state.particle_q,
1703
+ state.particle_qd,
1704
+ model.edge_indices,
1705
+ model.edge_rest_angle,
1706
+ model.edge_bending_properties,
1707
+ ],
1708
+ outputs=[particle_f],
1709
+ device=model.device,
1710
+ )
1711
+
1712
+
1713
+ def eval_particle_ground_contact_forces(model: Model, state: State, particle_f: wp.array):
1714
+ if model.ground and model.particle_count:
1715
+ wp.launch(
1716
+ kernel=eval_particle_ground_contacts,
1717
+ dim=model.particle_count,
1718
+ inputs=[
1719
+ state.particle_q,
1720
+ state.particle_qd,
1721
+ model.particle_radius,
1722
+ model.particle_flags,
1723
+ model.soft_contact_ke,
1724
+ model.soft_contact_kd,
1725
+ model.soft_contact_kf,
1726
+ model.soft_contact_mu,
1727
+ model.ground_plane,
1728
+ ],
1729
+ outputs=[particle_f],
1730
+ device=model.device,
1731
+ )
1732
+
1733
+
1734
+ def eval_tetrahedral_forces(model: Model, state: State, control: Control, particle_f: wp.array):
1735
+ if model.tet_count:
1736
+ wp.launch(
1737
+ kernel=eval_tetrahedra,
1738
+ dim=model.tet_count,
1739
+ inputs=[
1740
+ state.particle_q,
1741
+ state.particle_qd,
1742
+ model.tet_indices,
1743
+ model.tet_poses,
1744
+ control.tet_activations,
1745
+ model.tet_materials,
1746
+ ],
1747
+ outputs=[particle_f],
1748
+ device=model.device,
1749
+ )
1750
+
1751
+
1752
+ def eval_body_contact_forces(model: Model, state: State, particle_f: wp.array):
1753
+ if model.rigid_contact_max and (
1754
+ model.ground and model.shape_ground_contact_pair_count or model.shape_contact_pair_count
1755
+ ):
1756
+ wp.launch(
1757
+ kernel=eval_rigid_contacts,
1758
+ dim=model.rigid_contact_max,
1759
+ inputs=[
1760
+ state.body_q,
1761
+ state.body_qd,
1762
+ model.body_com,
1763
+ model.shape_materials,
1764
+ model.shape_geo,
1765
+ model.shape_body,
1766
+ model.rigid_contact_count,
1767
+ model.rigid_contact_point0,
1768
+ model.rigid_contact_point1,
1769
+ model.rigid_contact_normal,
1770
+ model.rigid_contact_shape0,
1771
+ model.rigid_contact_shape1,
1772
+ False,
1773
+ ],
1774
+ outputs=[state.body_f],
1775
+ device=model.device,
1776
+ )
1777
+
1778
+
1779
+ def eval_body_joint_forces(model: Model, state: State, control: Control, body_f: wp.array):
1780
+ if model.joint_count:
1781
+ wp.launch(
1782
+ kernel=eval_body_joints,
1783
+ dim=model.joint_count,
1784
+ inputs=[
1785
+ state.body_q,
1786
+ state.body_qd,
1787
+ model.body_com,
1788
+ model.joint_qd_start,
1789
+ model.joint_type,
1790
+ model.joint_enabled,
1791
+ model.joint_child,
1792
+ model.joint_parent,
1793
+ model.joint_X_p,
1794
+ model.joint_X_c,
1795
+ model.joint_axis,
1796
+ model.joint_axis_start,
1797
+ model.joint_axis_dim,
1798
+ model.joint_axis_mode,
1799
+ control.joint_act,
1800
+ model.joint_target_ke,
1801
+ model.joint_target_kd,
1802
+ model.joint_limit_lower,
1803
+ model.joint_limit_upper,
1804
+ model.joint_limit_ke,
1805
+ model.joint_limit_kd,
1806
+ model.joint_attach_ke,
1807
+ model.joint_attach_kd,
1808
+ ],
1809
+ outputs=[body_f],
1810
+ device=model.device,
1811
+ )
1812
+
1813
+
1814
+ def eval_particle_body_contact_forces(model: Model, state: State, particle_f: wp.array, body_f: wp.array):
1815
+ if model.particle_count and model.shape_count > 1:
1816
+ wp.launch(
1817
+ kernel=eval_particle_contacts,
1818
+ dim=model.soft_contact_max,
1819
+ inputs=[
1820
+ state.particle_q,
1821
+ state.particle_qd,
1822
+ state.body_q,
1823
+ state.body_qd,
1824
+ model.particle_radius,
1825
+ model.particle_flags,
1826
+ model.body_com,
1827
+ model.shape_body,
1828
+ model.shape_materials,
1829
+ model.soft_contact_ke,
1830
+ model.soft_contact_kd,
1831
+ model.soft_contact_kf,
1832
+ model.soft_contact_mu,
1833
+ model.particle_adhesion,
1834
+ model.soft_contact_count,
1835
+ model.soft_contact_particle,
1836
+ model.soft_contact_shape,
1837
+ model.soft_contact_body_pos,
1838
+ model.soft_contact_body_vel,
1839
+ model.soft_contact_normal,
1840
+ model.soft_contact_max,
1841
+ ],
1842
+ # outputs
1843
+ outputs=[particle_f, body_f],
1844
+ device=model.device,
1845
+ )
1846
+
1847
+
1848
+ def eval_muscle_forces(model: Model, state: State, control: Control, body_f: wp.array):
1849
+ if model.muscle_count:
1850
+ wp.launch(
1851
+ kernel=eval_muscles,
1852
+ dim=model.muscle_count,
1853
+ inputs=[
1854
+ state.body_q,
1855
+ state.body_qd,
1856
+ model.body_com,
1857
+ model.muscle_start,
1858
+ model.muscle_params,
1859
+ model.muscle_bodies,
1860
+ model.muscle_points,
1861
+ control.muscle_activations,
1862
+ ],
1863
+ outputs=[body_f],
1864
+ device=model.device,
1865
+ )
1866
+
1867
+
1868
+ def compute_forces(model: Model, state: State, control: Control, particle_f: wp.array, body_f: wp.array, dt: float):
1869
+ # damped springs
1870
+ eval_spring_forces(model, state, particle_f)
1871
+
1872
+ # triangle elastic and lift/drag forces
1873
+ eval_triangle_forces(model, state, control, particle_f)
1874
+
1875
+ # triangle/triangle contacts
1876
+ eval_triangle_contact_forces(model, state, particle_f)
1877
+
1878
+ # triangle bending
1879
+ eval_bending_forces(model, state, particle_f)
1880
+
1881
+ # tetrahedral FEM
1882
+ eval_tetrahedral_forces(model, state, control, particle_f)
1883
+
1884
+ # body joints
1885
+ eval_body_joint_forces(model, state, control, body_f)
1886
+
1887
+ # particle-particle interactions
1888
+ eval_particle_forces(model, state, particle_f)
1889
+
1890
+ # particle ground contacts
1891
+ eval_particle_ground_contact_forces(model, state, particle_f)
1892
+
1893
+ # body contacts
1894
+ eval_body_contact_forces(model, state, particle_f)
1895
+
1896
+ # particle shape contact
1897
+ eval_particle_body_contact_forces(model, state, particle_f, body_f)
1898
+
1899
+ # muscles
1900
+ if False:
1901
+ eval_muscle_forces(model, state, control, body_f)
1902
+
1903
+
1904
+ class SemiImplicitIntegrator(Integrator):
1905
+ """A semi-implicit integrator using symplectic Euler
1906
+
1907
+ After constructing `Model` and `State` objects this time-integrator
1908
+ may be used to advance the simulation state forward in time.
1909
+
1910
+ Semi-implicit time integration is a variational integrator that
1911
+ preserves energy, however it not unconditionally stable, and requires a time-step
1912
+ small enough to support the required stiffness and damping forces.
1913
+
1914
+ See: https://en.wikipedia.org/wiki/Semi-implicit_Euler_method
1915
+
1916
+ Example
1917
+ -------
1918
+
1919
+ .. code-block:: python
1920
+
1921
+ integrator = wp.SemiImplicitIntegrator()
1922
+
1923
+ # simulation loop
1924
+ for i in range(100):
1925
+ state = integrator.simulate(model, state_in, state_out, dt)
1926
+
1927
+ """
1928
+
1929
+ def __init__(self, angular_damping: float = 0.05):
1930
+ """
1931
+ Args:
1932
+ angular_damping (float, optional): Angular damping factor. Defaults to 0.05.
1933
+ """
1934
+ self.angular_damping = angular_damping
1935
+
1936
+ def simulate(self, model: Model, state_in: State, state_out: State, dt: float, control: Control = None):
1937
+ with wp.ScopedTimer("simulate", False):
1938
+ particle_f = None
1939
+ body_f = None
1940
+
1941
+ if state_in.particle_count:
1942
+ particle_f = state_in.particle_f
1943
+
1944
+ if state_in.body_count:
1945
+ body_f = state_in.body_f
1946
+
1947
+ if control is None:
1948
+ control = model.control(clone_variables=False)
1949
+
1950
+ compute_forces(model, state_in, control, particle_f, body_f, dt)
1951
+
1952
+ self.integrate_bodies(model, state_in, state_out, dt, self.angular_damping)
1953
+
1954
+ self.integrate_particles(model, state_in, state_out, dt)
1955
+
1956
+ return state_out