warp-lang 0.11.0__py3-none-manylinux2014_x86_64.whl → 1.0.0__py3-none-manylinux2014_x86_64.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 (170) hide show
  1. warp/__init__.py +8 -0
  2. warp/bin/warp-clang.so +0 -0
  3. warp/bin/warp.so +0 -0
  4. warp/build.py +7 -6
  5. warp/build_dll.py +70 -79
  6. warp/builtins.py +10 -6
  7. warp/codegen.py +51 -19
  8. warp/config.py +7 -8
  9. warp/constants.py +3 -0
  10. warp/context.py +948 -245
  11. warp/dlpack.py +198 -113
  12. warp/examples/assets/bunny.usd +0 -0
  13. warp/examples/assets/cartpole.urdf +110 -0
  14. warp/examples/assets/crazyflie.usd +0 -0
  15. warp/examples/assets/cube.usda +42 -0
  16. warp/examples/assets/nv_ant.xml +92 -0
  17. warp/examples/assets/nv_humanoid.xml +183 -0
  18. warp/examples/assets/quadruped.urdf +268 -0
  19. warp/examples/assets/rocks.nvdb +0 -0
  20. warp/examples/assets/rocks.usd +0 -0
  21. warp/examples/assets/sphere.usda +56 -0
  22. warp/examples/assets/torus.usda +105 -0
  23. warp/examples/benchmarks/benchmark_api.py +383 -0
  24. warp/examples/benchmarks/benchmark_cloth.py +279 -0
  25. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -0
  26. warp/examples/benchmarks/benchmark_cloth_jax.py +100 -0
  27. warp/examples/benchmarks/benchmark_cloth_numba.py +142 -0
  28. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -0
  29. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -0
  30. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -0
  31. warp/examples/benchmarks/benchmark_cloth_warp.py +146 -0
  32. warp/examples/benchmarks/benchmark_launches.py +295 -0
  33. warp/examples/core/example_dem.py +221 -0
  34. warp/examples/core/example_fluid.py +267 -0
  35. warp/examples/core/example_graph_capture.py +129 -0
  36. warp/examples/core/example_marching_cubes.py +177 -0
  37. warp/examples/core/example_mesh.py +154 -0
  38. warp/examples/core/example_mesh_intersect.py +193 -0
  39. warp/examples/core/example_nvdb.py +169 -0
  40. warp/examples/core/example_raycast.py +89 -0
  41. warp/examples/core/example_raymarch.py +178 -0
  42. warp/examples/core/example_render_opengl.py +141 -0
  43. warp/examples/core/example_sph.py +389 -0
  44. warp/examples/core/example_torch.py +181 -0
  45. warp/examples/core/example_wave.py +249 -0
  46. warp/examples/fem/bsr_utils.py +380 -0
  47. warp/examples/fem/example_apic_fluid.py +391 -0
  48. warp/examples/fem/example_convection_diffusion.py +168 -0
  49. warp/examples/fem/example_convection_diffusion_dg.py +209 -0
  50. warp/examples/fem/example_convection_diffusion_dg0.py +194 -0
  51. warp/examples/fem/example_deformed_geometry.py +159 -0
  52. warp/examples/fem/example_diffusion.py +173 -0
  53. warp/examples/fem/example_diffusion_3d.py +152 -0
  54. warp/examples/fem/example_diffusion_mgpu.py +214 -0
  55. warp/examples/fem/example_mixed_elasticity.py +222 -0
  56. warp/examples/fem/example_navier_stokes.py +243 -0
  57. warp/examples/fem/example_stokes.py +192 -0
  58. warp/examples/fem/example_stokes_transfer.py +249 -0
  59. warp/examples/fem/mesh_utils.py +109 -0
  60. warp/examples/fem/plot_utils.py +287 -0
  61. warp/examples/optim/example_bounce.py +248 -0
  62. warp/examples/optim/example_cloth_throw.py +210 -0
  63. warp/examples/optim/example_diffray.py +535 -0
  64. warp/examples/optim/example_drone.py +850 -0
  65. warp/examples/optim/example_inverse_kinematics.py +169 -0
  66. warp/examples/optim/example_inverse_kinematics_torch.py +170 -0
  67. warp/examples/optim/example_spring_cage.py +234 -0
  68. warp/examples/optim/example_trajectory.py +201 -0
  69. warp/examples/sim/example_cartpole.py +128 -0
  70. warp/examples/sim/example_cloth.py +184 -0
  71. warp/examples/sim/example_granular.py +113 -0
  72. warp/examples/sim/example_granular_collision_sdf.py +185 -0
  73. warp/examples/sim/example_jacobian_ik.py +213 -0
  74. warp/examples/sim/example_particle_chain.py +106 -0
  75. warp/examples/sim/example_quadruped.py +179 -0
  76. warp/examples/sim/example_rigid_chain.py +191 -0
  77. warp/examples/sim/example_rigid_contact.py +176 -0
  78. warp/examples/sim/example_rigid_force.py +126 -0
  79. warp/examples/sim/example_rigid_gyroscopic.py +97 -0
  80. warp/examples/sim/example_rigid_soft_contact.py +124 -0
  81. warp/examples/sim/example_soft_body.py +178 -0
  82. warp/fabric.py +29 -20
  83. warp/fem/cache.py +0 -1
  84. warp/fem/dirichlet.py +0 -2
  85. warp/fem/integrate.py +0 -1
  86. warp/jax.py +45 -0
  87. warp/jax_experimental.py +339 -0
  88. warp/native/builtin.h +12 -0
  89. warp/native/bvh.cu +18 -18
  90. warp/native/clang/clang.cpp +8 -3
  91. warp/native/cuda_util.cpp +94 -5
  92. warp/native/cuda_util.h +35 -6
  93. warp/native/cutlass_gemm.cpp +1 -1
  94. warp/native/cutlass_gemm.cu +4 -1
  95. warp/native/error.cpp +66 -0
  96. warp/native/error.h +27 -0
  97. warp/native/mesh.cu +2 -2
  98. warp/native/reduce.cu +4 -4
  99. warp/native/runlength_encode.cu +2 -2
  100. warp/native/scan.cu +2 -2
  101. warp/native/sparse.cu +0 -1
  102. warp/native/temp_buffer.h +2 -2
  103. warp/native/warp.cpp +95 -60
  104. warp/native/warp.cu +1053 -218
  105. warp/native/warp.h +49 -32
  106. warp/optim/linear.py +33 -16
  107. warp/render/render_opengl.py +202 -101
  108. warp/render/render_usd.py +82 -40
  109. warp/sim/__init__.py +13 -4
  110. warp/sim/articulation.py +4 -5
  111. warp/sim/collide.py +320 -175
  112. warp/sim/import_mjcf.py +25 -30
  113. warp/sim/import_urdf.py +94 -63
  114. warp/sim/import_usd.py +51 -36
  115. warp/sim/inertia.py +3 -2
  116. warp/sim/integrator.py +233 -0
  117. warp/sim/integrator_euler.py +447 -469
  118. warp/sim/integrator_featherstone.py +1991 -0
  119. warp/sim/integrator_xpbd.py +1420 -640
  120. warp/sim/model.py +765 -487
  121. warp/sim/particles.py +2 -1
  122. warp/sim/render.py +35 -13
  123. warp/sim/utils.py +222 -11
  124. warp/stubs.py +8 -0
  125. warp/tape.py +16 -1
  126. warp/tests/aux_test_grad_customs.py +23 -0
  127. warp/tests/test_array.py +190 -1
  128. warp/tests/test_async.py +656 -0
  129. warp/tests/test_bool.py +50 -0
  130. warp/tests/test_dlpack.py +164 -11
  131. warp/tests/test_examples.py +166 -74
  132. warp/tests/test_fem.py +8 -1
  133. warp/tests/test_generics.py +15 -5
  134. warp/tests/test_grad.py +1 -1
  135. warp/tests/test_grad_customs.py +172 -12
  136. warp/tests/test_jax.py +254 -0
  137. warp/tests/test_large.py +29 -6
  138. warp/tests/test_launch.py +25 -0
  139. warp/tests/test_linear_solvers.py +20 -3
  140. warp/tests/test_matmul.py +61 -16
  141. warp/tests/test_matmul_lite.py +13 -13
  142. warp/tests/test_mempool.py +186 -0
  143. warp/tests/test_multigpu.py +3 -0
  144. warp/tests/test_options.py +16 -2
  145. warp/tests/test_peer.py +137 -0
  146. warp/tests/test_print.py +3 -1
  147. warp/tests/test_quat.py +23 -0
  148. warp/tests/test_sim_kinematics.py +97 -0
  149. warp/tests/test_snippet.py +126 -3
  150. warp/tests/test_streams.py +108 -79
  151. warp/tests/test_torch.py +16 -8
  152. warp/tests/test_utils.py +32 -27
  153. warp/tests/test_verify_fp.py +65 -0
  154. warp/tests/test_volume.py +1 -1
  155. warp/tests/unittest_serial.py +2 -0
  156. warp/tests/unittest_suites.py +12 -0
  157. warp/tests/unittest_utils.py +14 -7
  158. warp/thirdparty/unittest_parallel.py +15 -3
  159. warp/torch.py +10 -8
  160. warp/types.py +363 -246
  161. warp/utils.py +143 -19
  162. warp_lang-1.0.0.dist-info/LICENSE.md +126 -0
  163. warp_lang-1.0.0.dist-info/METADATA +394 -0
  164. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/RECORD +167 -86
  165. warp/sim/optimizer.py +0 -138
  166. warp_lang-0.11.0.dist-info/LICENSE.md +0 -36
  167. warp_lang-0.11.0.dist-info/METADATA +0 -238
  168. /warp/tests/{walkthough_debug.py → walkthrough_debug.py} +0 -0
  169. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/WHEEL +0 -0
  170. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,88 @@
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
+ import cupy as cp
9
+ import cupyx as cpx
10
+
11
+
12
+ def eval_springs(x, v, indices, rest, ke, kd, f):
13
+ i = indices[:, 0]
14
+ j = indices[:, 1]
15
+
16
+ xi = x[i]
17
+ xj = x[j]
18
+
19
+ vi = v[i]
20
+ vj = v[j]
21
+
22
+ xij = xi - xj
23
+ vij = vi - vj
24
+
25
+ l = cp.linalg.norm(xij, axis=1)
26
+ l_inv = 1.0 / l
27
+
28
+ # normalized spring direction
29
+ dir = (xij.T * l_inv).T
30
+
31
+ c = l - rest
32
+ dcdt = cp.sum(dir * vij, axis=1)
33
+
34
+ # damping based on relative velocity.
35
+ fs = dir.T * (ke * c + kd * dcdt)
36
+
37
+ cpx.scatter_add(f, i, -fs.T)
38
+ cpx.scatter_add(f, j, fs.T)
39
+
40
+
41
+ def integrate_particles(x, v, f, w, dt):
42
+ g = cp.array((0.0, 0.0 - 9.8, 0.0))
43
+ s = w > 0.0
44
+
45
+ a_ext = g * s[:, None]
46
+
47
+ # simple semi-implicit Euler. v1 = v0 + a dt, x1 = x0 + v1 dt
48
+ v += ((f.T * w).T + a_ext) * dt
49
+ x += v * dt
50
+
51
+ # clear forces
52
+ f *= 0.0
53
+
54
+
55
+ class CpIntegrator:
56
+ def __init__(self, cloth):
57
+ self.cloth = cloth
58
+
59
+ self.positions = cp.array(self.cloth.positions)
60
+ self.velocities = cp.array(self.cloth.velocities)
61
+ self.inv_mass = cp.array(self.cloth.inv_masses)
62
+
63
+ self.spring_indices = cp.array(self.cloth.spring_indices)
64
+ self.spring_lengths = cp.array(self.cloth.spring_lengths)
65
+ self.spring_stiffness = cp.array(self.cloth.spring_stiffness)
66
+ self.spring_damping = cp.array(self.cloth.spring_damping)
67
+
68
+ self.forces = cp.zeros((self.cloth.num_particles, 3), dtype=cp.float32)
69
+
70
+ def simulate(self, dt, substeps):
71
+ sim_dt = dt / substeps
72
+
73
+ for s in range(substeps):
74
+ eval_springs(
75
+ self.positions,
76
+ self.velocities,
77
+ self.spring_indices.reshape((self.cloth.num_springs, 2)),
78
+ self.spring_lengths,
79
+ self.spring_stiffness,
80
+ self.spring_damping,
81
+ self.forces,
82
+ )
83
+
84
+ # integrate
85
+ integrate_particles(self.positions, self.velocities, self.forces, self.inv_mass, sim_dt)
86
+
87
+ # return np.array(self.positions)
88
+ return self.positions.get()
@@ -0,0 +1,100 @@
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
+ import numpy as np
9
+
10
+ import jax.lax
11
+ import jax.numpy as jnp
12
+
13
+ import os
14
+
15
+
16
+ @jax.jit
17
+ def eval_springs(x, v, indices, rest, ke, kd):
18
+ i = indices[:, 0]
19
+ j = indices[:, 1]
20
+
21
+ xi = x[i]
22
+ xj = x[j]
23
+
24
+ vi = v[i]
25
+ vj = v[j]
26
+
27
+ xij = xi - xj
28
+ vij = vi - vj
29
+
30
+ l = jnp.linalg.norm(xij, axis=1)
31
+ l_inv = 1.0 / l
32
+
33
+ # normalized spring direction
34
+ dir = (xij.T * l_inv).T
35
+
36
+ c = l - rest
37
+ dcdt = jnp.sum(dir * vij, axis=1)
38
+
39
+ # damping based on relative velocity.
40
+ fs = dir.T * (ke * c + kd * dcdt)
41
+
42
+ f = jnp.zeros_like(v)
43
+
44
+ # f = jax.ops.index_add(f, i, -fs.T, indices_are_sorted=False, unique_indices=False)
45
+ # f = jax.ops.index_add(f, j, fs.T, indices_are_sorted=False, unique_indices=False)
46
+
47
+ f.at[i].add(-fs.T)
48
+ f.at[j].add(fs.T)
49
+
50
+ return f
51
+
52
+
53
+ @jax.jit
54
+ def integrate_particles(x, v, f, w, dt):
55
+ g = jnp.array((0.0, 0.0 - 9.8, 0.0))
56
+ s = w > 0.0
57
+
58
+ a_ext = g * s[:, None]
59
+
60
+ # simple semi-implicit Euler. v1 = v0 + a dt, x1 = x0 + v1 dt
61
+ v += ((f.T * w).T + a_ext) * dt
62
+ x += v * dt
63
+
64
+ return (x, v)
65
+
66
+
67
+ class JxIntegrator:
68
+ def __init__(self, cloth):
69
+ self.cloth = cloth
70
+
71
+ self.positions = jnp.array(self.cloth.positions)
72
+ self.velocities = jnp.array(self.cloth.velocities)
73
+ self.inv_mass = jnp.array(self.cloth.inv_masses)
74
+
75
+ print(self.positions.device_buffer.device())
76
+
77
+ self.spring_indices = jnp.array(self.cloth.spring_indices)
78
+ self.spring_lengths = jnp.array(self.cloth.spring_lengths)
79
+ self.spring_stiffness = jnp.array(self.cloth.spring_stiffness)
80
+ self.spring_damping = jnp.array(self.cloth.spring_damping)
81
+
82
+ def simulate(self, dt, substeps):
83
+ sim_dt = dt / substeps
84
+
85
+ for s in range(substeps):
86
+ f = eval_springs(
87
+ self.positions,
88
+ self.velocities,
89
+ self.spring_indices.reshape((self.cloth.num_springs, 2)),
90
+ self.spring_lengths,
91
+ self.spring_stiffness,
92
+ self.spring_damping,
93
+ )
94
+
95
+ # integrate
96
+ (self.positions, self.velocities) = integrate_particles(
97
+ self.positions, self.velocities, f, self.inv_mass, sim_dt
98
+ )
99
+
100
+ return np.array(self.positions)
@@ -0,0 +1,142 @@
1
+ from numba import cuda, float32
2
+ import numpy as np
3
+ import cupy as cp
4
+
5
+ import math
6
+
7
+ # Notes:
8
+ #
9
+ # Current implementation requires some familarity of writing custom cuda kernels
10
+ # May be improved with cuda ufuncs and/or writing custom numba type extensions.
11
+
12
+
13
+ @cuda.jit(device=True)
14
+ def norm(x):
15
+ s = float32(0.0)
16
+ for i in range(3):
17
+ s += x[i] * x[i]
18
+ return math.sqrt(s)
19
+
20
+
21
+ @cuda.jit(device=True)
22
+ def dot(x, y):
23
+ s = float32(0.0)
24
+ for i in range(3):
25
+ s += x[i] * y[i]
26
+ return s
27
+
28
+
29
+ @cuda.jit
30
+ def eval_springs_cuda(
31
+ num_springs, # (1,)
32
+ xs, # position (N, 3)
33
+ vs, # velocities (N, 3)
34
+ indices, # spring indices (S, 2)
35
+ rests, # spring rest length (S,)
36
+ kes, # stiffness (S,)
37
+ kds, # damping (S,)
38
+ fs,
39
+ ): # forces (N, 3)
40
+ tidx = cuda.grid(1)
41
+
42
+ if tidx < num_springs:
43
+ i, j = indices[tidx][0], indices[tidx][1]
44
+ xi, xj = xs[i], xs[j]
45
+ vi, vj = vs[i], vs[j]
46
+ rest, ke, kd = rests[tidx], kes[tidx], kds[tidx]
47
+
48
+ xij = cuda.local.array(3, dtype=cp.float32)
49
+ vij = cuda.local.array(3, dtype=cp.float32)
50
+ for k in range(3):
51
+ xij[k] = xi[k] - xj[k]
52
+ for k in range(3):
53
+ vij[k] = vi[k] - vj[k]
54
+
55
+ l = norm(xij)
56
+
57
+ l_inv = float32(1.0) / l
58
+
59
+ # normalized spring direction
60
+ xij_unit = cuda.local.array(3, dtype=cp.float32)
61
+ for k in range(3):
62
+ xij_unit[k] = xij[k] * l_inv
63
+ c = l - rest
64
+ dcdt = dot(xij_unit, vij)
65
+
66
+ # mass-spring-damper model
67
+ fac = ke * c + kd * dcdt
68
+ df = cuda.local.array(3, dtype=cp.float32)
69
+ for k in range(3):
70
+ df[k] = xij_unit[k] * fac
71
+
72
+ for k in range(3):
73
+ cuda.atomic.add(fs[i], k, -df[k])
74
+ cuda.atomic.add(fs[j], k, df[k])
75
+
76
+
77
+ # Support const array with cp array?
78
+ g = np.array([0.0, 0.0 - 9.8, 0.0], dtype=np.float32)
79
+ z = np.array([0.0, 0.0, 0.0], dtype=np.float32)
80
+
81
+
82
+ @cuda.jit
83
+ def integrate_particles_cuda(
84
+ xs, vs, fs, ws, dt # position (N, 3) # velocity (N, 3) # force (N, 3) # inverse of mass (N,)
85
+ ): # dt (1,)
86
+ i = cuda.grid(1)
87
+
88
+ if i < xs.shape[0]:
89
+ w = ws[i]
90
+ a = cuda.const.array_like(g) if w > 0.0 else cuda.const.array_like(z)
91
+
92
+ for j in range(3):
93
+ # vs[i] += ((f * w) + a) * dt (ideally)
94
+ vs[i][j] = vs[i][j] + ((fs[i][j] * w) + a[j]) * dt
95
+ xs[i][j] = xs[i][j] + vs[i][j] * dt
96
+
97
+ fs[i] = 0.0
98
+
99
+
100
+ class NbIntegrator:
101
+ def __init__(self, cloth):
102
+ self.cloth = cloth
103
+
104
+ self.positions = cp.array(self.cloth.positions)
105
+ self.velocities = cp.array(self.cloth.velocities)
106
+ self.inv_mass = cp.array(self.cloth.inv_masses)
107
+
108
+ self.spring_indices = cp.array(self.cloth.spring_indices)
109
+ self.spring_lengths = cp.array(self.cloth.spring_lengths)
110
+ self.spring_stiffness = cp.array(self.cloth.spring_stiffness)
111
+ self.spring_damping = cp.array(self.cloth.spring_damping)
112
+
113
+ self.forces = cp.zeros((self.cloth.num_particles, 3), dtype=cp.float32)
114
+
115
+ self.num_particles = self.positions.shape[0]
116
+ self.integrate_tpb = 4
117
+ self.integrate_nb = self.num_particles // self.integrate_tpb + 1
118
+
119
+ self.spring_tpb = 4
120
+ self.spring_nb = self.cloth.num_springs // self.spring_tpb + 1
121
+
122
+ def simulate(self, dt, substeps):
123
+ sim_dt = dt / substeps
124
+
125
+ for s in range(substeps):
126
+ eval_springs_cuda[self.spring_nb, self.spring_tpb](
127
+ self.cloth.num_springs,
128
+ self.positions,
129
+ self.velocities,
130
+ self.spring_indices.reshape((self.cloth.num_springs, 2)),
131
+ self.spring_lengths,
132
+ self.spring_stiffness,
133
+ self.spring_damping,
134
+ self.forces,
135
+ )
136
+
137
+ # integrate
138
+ integrate_particles_cuda[self.integrate_nb, self.integrate_tpb](
139
+ self.positions, self.velocities, self.forces, self.inv_mass, sim_dt
140
+ )
141
+
142
+ return self.positions.get()
@@ -0,0 +1,77 @@
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
+ import numpy as np
9
+
10
+
11
+ def eval_springs(x, v, indices, rest, ke, kd, f):
12
+ i = indices[:, 0]
13
+ j = indices[:, 1]
14
+
15
+ xi = x[i]
16
+ xj = x[j]
17
+
18
+ vi = v[i]
19
+ vj = v[j]
20
+
21
+ xij = xi - xj
22
+ vij = vi - vj
23
+
24
+ l = np.linalg.norm(xij, axis=1)
25
+ l_inv = 1.0 / l
26
+
27
+ # normalized spring direction
28
+ dir = (xij.T * l_inv).T
29
+
30
+ c = l - rest
31
+ dcdt = np.sum(dir * vij, axis=1)
32
+
33
+ # damping based on relative velocity.
34
+ fs = dir.T * (ke * c + kd * dcdt)
35
+
36
+ np.add.at(f, i, -fs.T)
37
+ np.add.at(f, j, fs.T)
38
+
39
+
40
+ def integrate_particles(x, v, f, w, dt):
41
+ g = np.array((0.0, 0.0 - 9.8, 0.0))
42
+ s = w > 0.0
43
+
44
+ a_ext = g * s[:, None]
45
+
46
+ # simple semi-implicit Euler. v1 = v0 + a dt, x1 = x0 + v1 dt
47
+ v += ((f.T * w).T + a_ext) * dt
48
+ x += v * dt
49
+
50
+ # clear forces
51
+ f *= 0.0
52
+
53
+
54
+ class NpIntegrator:
55
+ def __init__(self, cloth):
56
+ self.cloth = cloth
57
+
58
+ self.forces = np.zeros((self.cloth.num_particles, 3), dtype=np.float32)
59
+
60
+ def simulate(self, dt, substeps):
61
+ sim_dt = dt / substeps
62
+
63
+ for s in range(substeps):
64
+ eval_springs(
65
+ self.cloth.positions,
66
+ self.cloth.velocities,
67
+ self.cloth.spring_indices.reshape((self.cloth.num_springs, 2)),
68
+ self.cloth.spring_lengths,
69
+ self.cloth.spring_stiffness,
70
+ self.cloth.spring_damping,
71
+ self.forces,
72
+ )
73
+
74
+ # integrate
75
+ integrate_particles(self.cloth.positions, self.cloth.velocities, self.forces, self.cloth.inv_masses, sim_dt)
76
+
77
+ return self.cloth.positions
@@ -0,0 +1,86 @@
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
+ import torch
9
+
10
+
11
+ def eval_springs(x, v, indices, rest, ke, kd, f):
12
+ i = indices[:, 0]
13
+ j = indices[:, 1]
14
+
15
+ xi = x[i]
16
+ xj = x[j]
17
+
18
+ vi = v[i]
19
+ vj = v[j]
20
+
21
+ xij = xi - xj
22
+ vij = vi - vj
23
+
24
+ l = torch.linalg.norm(xij, axis=1)
25
+ l_inv = 1.0 / l
26
+
27
+ # normalized spring direction
28
+ dir = (xij.T * l_inv).T
29
+
30
+ c = l - rest
31
+ dcdt = torch.sum(dir * vij, axis=1)
32
+
33
+ # damping based on relative velocity.
34
+ fs = dir.T * (ke * c + kd * dcdt)
35
+
36
+ f.index_add_(dim=0, index=i, source=-fs.T)
37
+ f.index_add_(dim=0, index=j, source=fs.T)
38
+
39
+
40
+ def integrate_particles(x, v, f, g, w, dt):
41
+ s = w > 0.0
42
+
43
+ a_ext = g * s[:, None]
44
+
45
+ # simple semi-implicit Euler. v1 = v0 + a dt, x1 = x0 + v1 dt
46
+ v += ((f.T * w).T + a_ext) * dt
47
+ x += v * dt
48
+
49
+ # clear forces
50
+ f *= 0.0
51
+
52
+
53
+ class TrIntegrator:
54
+ def __init__(self, cloth, device):
55
+ self.cloth = cloth
56
+
57
+ self.positions = torch.tensor(self.cloth.positions, device=device)
58
+ self.velocities = torch.tensor(self.cloth.velocities, device=device)
59
+ self.inv_mass = torch.tensor(self.cloth.inv_masses, device=device)
60
+
61
+ self.spring_indices = torch.tensor(self.cloth.spring_indices, device=device, dtype=torch.long)
62
+ self.spring_lengths = torch.tensor(self.cloth.spring_lengths, device=device)
63
+ self.spring_stiffness = torch.tensor(self.cloth.spring_stiffness, device=device)
64
+ self.spring_damping = torch.tensor(self.cloth.spring_damping, device=device)
65
+
66
+ self.forces = torch.zeros((self.cloth.num_particles, 3), dtype=torch.float32, device=device)
67
+ self.gravity = g = torch.tensor((0.0, 0.0 - 9.8, 0.0), dtype=torch.float32, device=device)
68
+
69
+ def simulate(self, dt, substeps):
70
+ sim_dt = dt / substeps
71
+
72
+ for s in range(substeps):
73
+ eval_springs(
74
+ self.positions,
75
+ self.velocities,
76
+ self.spring_indices.reshape((self.cloth.num_springs, 2)),
77
+ self.spring_lengths,
78
+ self.spring_stiffness,
79
+ self.spring_damping,
80
+ self.forces,
81
+ )
82
+
83
+ # integrate
84
+ integrate_particles(self.positions, self.velocities, self.forces, self.gravity, self.inv_mass, sim_dt)
85
+
86
+ return self.positions.cpu().numpy()
@@ -0,0 +1,112 @@
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
+ import taichi as ti
9
+ import numpy as np
10
+
11
+
12
+ @ti.func
13
+ def step(x):
14
+ ret = 0.0
15
+ if x < 0:
16
+ ret = 1
17
+ return ret
18
+
19
+
20
+ @ti.data_oriented
21
+ class TiIntegrator:
22
+ @ti.kernel
23
+ def eval_springs(self):
24
+ for tid in range(self.cloth.num_springs):
25
+ i = self.spring_indices[2 * tid]
26
+ j = self.spring_indices[2 * tid + 1]
27
+
28
+ ke = self.spring_stiffness[tid]
29
+ kd = self.spring_damping[tid]
30
+ rest = self.spring_lengths[tid]
31
+
32
+ xi = self.positions[i]
33
+ xj = self.positions[j]
34
+
35
+ vi = self.velocities[i]
36
+ vj = self.velocities[j]
37
+
38
+ xij = xi - xj
39
+ vij = vi - vj
40
+
41
+ l = xij.norm()
42
+ dir = xij.normalized()
43
+
44
+ c = l - rest
45
+ dcdt = dir.dot(vij)
46
+
47
+ fs = dir * (ke * c + kd * dcdt)
48
+
49
+ self.forces[i] -= fs
50
+ self.forces[j] += fs
51
+
52
+ @ti.kernel
53
+ def integrate_particles(self, dt: ti.f32):
54
+ for tid in range(self.cloth.num_particles):
55
+ x0 = self.positions[tid]
56
+ v0 = self.velocities[tid]
57
+ f0 = self.forces[tid]
58
+ w = self.inv_mass[tid]
59
+
60
+ g = ti.Vector([0.0, 0.0, 0.0])
61
+
62
+ if w > 0.0:
63
+ g = ti.Vector([0.0, -9.81, 0.0])
64
+
65
+ v1 = v0 + (f0 * w + g) * dt
66
+ x1 = x0 + v1 * dt
67
+
68
+ self.positions[tid] = x1
69
+ self.velocities[tid] = v1
70
+ self.forces[tid] = ti.Vector([0.0, 0.0, 0.0])
71
+
72
+ def __init__(self, cloth, device):
73
+ if device == "cpu":
74
+ ti.init(arch=ti.cpu)
75
+ elif device == "cuda":
76
+ ti.init(arch=ti.gpu)
77
+ else:
78
+ raise RuntimeError("Unsupported Taichi device")
79
+
80
+ self.cloth = cloth
81
+
82
+ self.positions = ti.Vector.field(3, dtype=ti.f32, shape=self.cloth.num_particles)
83
+ self.velocities = ti.Vector.field(3, dtype=ti.f32, shape=self.cloth.num_particles)
84
+ self.inv_mass = ti.field(ti.f32, shape=self.cloth.num_particles)
85
+
86
+ self.spring_indices = ti.field(ti.i32, shape=self.cloth.num_springs * 2)
87
+ self.spring_lengths = ti.field(ti.f32, shape=self.cloth.num_springs)
88
+ self.spring_stiffness = ti.field(ti.f32, shape=self.cloth.num_springs)
89
+ self.spring_damping = ti.field(ti.f32, shape=self.cloth.num_springs)
90
+
91
+ self.forces = ti.Vector.field(3, dtype=ti.f32, shape=self.cloth.num_particles)
92
+
93
+ # upload data
94
+ self.positions.from_numpy(cloth.positions)
95
+ self.velocities.from_numpy(cloth.velocities)
96
+ self.inv_mass.from_numpy(cloth.inv_masses)
97
+ self.forces.from_numpy(np.zeros_like(self.cloth.velocities))
98
+
99
+ self.spring_indices.from_numpy(cloth.spring_indices)
100
+ self.spring_lengths.from_numpy(cloth.spring_lengths)
101
+ self.spring_stiffness.from_numpy(cloth.spring_stiffness)
102
+ self.spring_damping.from_numpy(cloth.spring_damping)
103
+
104
+ def simulate(self, dt, substeps):
105
+ sim_dt = dt / substeps
106
+
107
+ for s in range(substeps):
108
+ self.eval_springs()
109
+
110
+ self.integrate_particles(sim_dt)
111
+
112
+ return self.positions.to_numpy()