warp-lang 1.0.0b2__py3-none-win_amd64.whl → 1.0.0b6__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 (271) hide show
  1. docs/conf.py +17 -5
  2. examples/env/env_ant.py +1 -1
  3. examples/env/env_cartpole.py +1 -1
  4. examples/env/env_humanoid.py +1 -1
  5. examples/env/env_usd.py +4 -1
  6. examples/env/environment.py +8 -9
  7. examples/example_dem.py +34 -33
  8. examples/example_diffray.py +364 -337
  9. examples/example_fluid.py +32 -23
  10. examples/example_jacobian_ik.py +97 -93
  11. examples/example_marching_cubes.py +6 -16
  12. examples/example_mesh.py +6 -16
  13. examples/example_mesh_intersect.py +16 -14
  14. examples/example_nvdb.py +14 -16
  15. examples/example_raycast.py +14 -13
  16. examples/example_raymarch.py +16 -23
  17. examples/example_render_opengl.py +19 -10
  18. examples/example_sim_cartpole.py +82 -78
  19. examples/example_sim_cloth.py +45 -48
  20. examples/example_sim_fk_grad.py +51 -44
  21. examples/example_sim_fk_grad_torch.py +47 -40
  22. examples/example_sim_grad_bounce.py +108 -133
  23. examples/example_sim_grad_cloth.py +99 -113
  24. examples/example_sim_granular.py +5 -6
  25. examples/{example_sim_sdf_shape.py → example_sim_granular_collision_sdf.py} +37 -26
  26. examples/example_sim_neo_hookean.py +51 -55
  27. examples/example_sim_particle_chain.py +4 -4
  28. examples/example_sim_quadruped.py +126 -81
  29. examples/example_sim_rigid_chain.py +54 -61
  30. examples/example_sim_rigid_contact.py +66 -70
  31. examples/example_sim_rigid_fem.py +3 -3
  32. examples/example_sim_rigid_force.py +1 -1
  33. examples/example_sim_rigid_gyroscopic.py +3 -4
  34. examples/example_sim_rigid_kinematics.py +28 -39
  35. examples/example_sim_trajopt.py +112 -110
  36. examples/example_sph.py +9 -8
  37. examples/example_wave.py +7 -7
  38. examples/fem/bsr_utils.py +30 -17
  39. examples/fem/example_apic_fluid.py +85 -69
  40. examples/fem/example_convection_diffusion.py +97 -93
  41. examples/fem/example_convection_diffusion_dg.py +142 -149
  42. examples/fem/example_convection_diffusion_dg0.py +141 -136
  43. examples/fem/example_deformed_geometry.py +146 -0
  44. examples/fem/example_diffusion.py +115 -84
  45. examples/fem/example_diffusion_3d.py +116 -86
  46. examples/fem/example_diffusion_mgpu.py +102 -79
  47. examples/fem/example_mixed_elasticity.py +139 -100
  48. examples/fem/example_navier_stokes.py +175 -162
  49. examples/fem/example_stokes.py +143 -111
  50. examples/fem/example_stokes_transfer.py +186 -157
  51. examples/fem/mesh_utils.py +59 -97
  52. examples/fem/plot_utils.py +138 -17
  53. tools/ci/publishing/build_nodes_info.py +54 -0
  54. warp/__init__.py +4 -3
  55. warp/__init__.pyi +1 -0
  56. warp/bin/warp-clang.dll +0 -0
  57. warp/bin/warp.dll +0 -0
  58. warp/build.py +5 -3
  59. warp/build_dll.py +29 -9
  60. warp/builtins.py +836 -492
  61. warp/codegen.py +864 -553
  62. warp/config.py +3 -1
  63. warp/context.py +389 -172
  64. warp/fem/__init__.py +24 -6
  65. warp/fem/cache.py +318 -25
  66. warp/fem/dirichlet.py +7 -3
  67. warp/fem/domain.py +14 -0
  68. warp/fem/field/__init__.py +30 -38
  69. warp/fem/field/field.py +149 -0
  70. warp/fem/field/nodal_field.py +244 -138
  71. warp/fem/field/restriction.py +8 -6
  72. warp/fem/field/test.py +127 -59
  73. warp/fem/field/trial.py +117 -60
  74. warp/fem/geometry/__init__.py +5 -1
  75. warp/fem/geometry/deformed_geometry.py +271 -0
  76. warp/fem/geometry/element.py +24 -1
  77. warp/fem/geometry/geometry.py +86 -14
  78. warp/fem/geometry/grid_2d.py +112 -54
  79. warp/fem/geometry/grid_3d.py +134 -65
  80. warp/fem/geometry/hexmesh.py +953 -0
  81. warp/fem/geometry/partition.py +85 -33
  82. warp/fem/geometry/quadmesh_2d.py +532 -0
  83. warp/fem/geometry/tetmesh.py +451 -115
  84. warp/fem/geometry/trimesh_2d.py +197 -92
  85. warp/fem/integrate.py +534 -268
  86. warp/fem/operator.py +58 -31
  87. warp/fem/polynomial.py +11 -0
  88. warp/fem/quadrature/__init__.py +1 -1
  89. warp/fem/quadrature/pic_quadrature.py +150 -58
  90. warp/fem/quadrature/quadrature.py +209 -57
  91. warp/fem/space/__init__.py +230 -53
  92. warp/fem/space/basis_space.py +489 -0
  93. warp/fem/space/collocated_function_space.py +105 -0
  94. warp/fem/space/dof_mapper.py +49 -2
  95. warp/fem/space/function_space.py +90 -39
  96. warp/fem/space/grid_2d_function_space.py +149 -496
  97. warp/fem/space/grid_3d_function_space.py +173 -538
  98. warp/fem/space/hexmesh_function_space.py +352 -0
  99. warp/fem/space/partition.py +129 -76
  100. warp/fem/space/quadmesh_2d_function_space.py +369 -0
  101. warp/fem/space/restriction.py +46 -34
  102. warp/fem/space/shape/__init__.py +15 -0
  103. warp/fem/space/shape/cube_shape_function.py +738 -0
  104. warp/fem/space/shape/shape_function.py +103 -0
  105. warp/fem/space/shape/square_shape_function.py +611 -0
  106. warp/fem/space/shape/tet_shape_function.py +567 -0
  107. warp/fem/space/shape/triangle_shape_function.py +429 -0
  108. warp/fem/space/tetmesh_function_space.py +132 -1039
  109. warp/fem/space/topology.py +295 -0
  110. warp/fem/space/trimesh_2d_function_space.py +104 -742
  111. warp/fem/types.py +13 -11
  112. warp/fem/utils.py +335 -60
  113. warp/native/array.h +120 -34
  114. warp/native/builtin.h +101 -72
  115. warp/native/bvh.cpp +73 -325
  116. warp/native/bvh.cu +406 -23
  117. warp/native/bvh.h +22 -40
  118. warp/native/clang/clang.cpp +1 -0
  119. warp/native/crt.h +2 -0
  120. warp/native/cuda_util.cpp +8 -3
  121. warp/native/cuda_util.h +1 -0
  122. warp/native/exports.h +1522 -1243
  123. warp/native/intersect.h +19 -4
  124. warp/native/intersect_adj.h +8 -8
  125. warp/native/mat.h +76 -17
  126. warp/native/mesh.cpp +33 -108
  127. warp/native/mesh.cu +114 -18
  128. warp/native/mesh.h +395 -40
  129. warp/native/noise.h +272 -329
  130. warp/native/quat.h +51 -8
  131. warp/native/rand.h +44 -34
  132. warp/native/reduce.cpp +1 -1
  133. warp/native/sparse.cpp +4 -4
  134. warp/native/sparse.cu +163 -155
  135. warp/native/spatial.h +2 -2
  136. warp/native/temp_buffer.h +18 -14
  137. warp/native/vec.h +103 -21
  138. warp/native/warp.cpp +2 -1
  139. warp/native/warp.cu +28 -3
  140. warp/native/warp.h +4 -3
  141. warp/render/render_opengl.py +261 -109
  142. warp/sim/__init__.py +1 -2
  143. warp/sim/articulation.py +385 -185
  144. warp/sim/import_mjcf.py +59 -48
  145. warp/sim/import_urdf.py +15 -15
  146. warp/sim/import_usd.py +174 -102
  147. warp/sim/inertia.py +17 -18
  148. warp/sim/integrator_xpbd.py +4 -3
  149. warp/sim/model.py +330 -250
  150. warp/sim/render.py +1 -1
  151. warp/sparse.py +625 -152
  152. warp/stubs.py +341 -309
  153. warp/tape.py +9 -6
  154. warp/tests/__main__.py +3 -6
  155. warp/tests/assets/curlnoise_golden.npy +0 -0
  156. warp/tests/assets/pnoise_golden.npy +0 -0
  157. warp/tests/{test_class_kernel.py → aux_test_class_kernel.py} +9 -1
  158. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -0
  159. warp/tests/{test_dependent.py → aux_test_dependent.py} +2 -2
  160. warp/tests/{test_reference.py → aux_test_reference.py} +1 -1
  161. warp/tests/aux_test_unresolved_func.py +14 -0
  162. warp/tests/aux_test_unresolved_symbol.py +14 -0
  163. warp/tests/disabled_kinematics.py +239 -0
  164. warp/tests/run_coverage_serial.py +31 -0
  165. warp/tests/test_adam.py +103 -106
  166. warp/tests/test_arithmetic.py +94 -74
  167. warp/tests/test_array.py +82 -101
  168. warp/tests/test_array_reduce.py +57 -23
  169. warp/tests/test_atomic.py +64 -28
  170. warp/tests/test_bool.py +22 -12
  171. warp/tests/test_builtins_resolution.py +1292 -0
  172. warp/tests/test_bvh.py +18 -18
  173. warp/tests/test_closest_point_edge_edge.py +54 -57
  174. warp/tests/test_codegen.py +165 -134
  175. warp/tests/test_compile_consts.py +28 -20
  176. warp/tests/test_conditional.py +108 -24
  177. warp/tests/test_copy.py +10 -12
  178. warp/tests/test_ctypes.py +112 -88
  179. warp/tests/test_dense.py +21 -14
  180. warp/tests/test_devices.py +98 -0
  181. warp/tests/test_dlpack.py +75 -75
  182. warp/tests/test_examples.py +237 -0
  183. warp/tests/test_fabricarray.py +22 -24
  184. warp/tests/test_fast_math.py +15 -11
  185. warp/tests/test_fem.py +1034 -124
  186. warp/tests/test_fp16.py +23 -16
  187. warp/tests/test_func.py +187 -86
  188. warp/tests/test_generics.py +194 -49
  189. warp/tests/test_grad.py +123 -181
  190. warp/tests/test_grad_customs.py +176 -0
  191. warp/tests/test_hash_grid.py +35 -34
  192. warp/tests/test_import.py +10 -23
  193. warp/tests/test_indexedarray.py +24 -25
  194. warp/tests/test_intersect.py +18 -9
  195. warp/tests/test_large.py +141 -0
  196. warp/tests/test_launch.py +14 -41
  197. warp/tests/test_lerp.py +64 -65
  198. warp/tests/test_lvalue.py +493 -0
  199. warp/tests/test_marching_cubes.py +12 -13
  200. warp/tests/test_mat.py +517 -2898
  201. warp/tests/test_mat_lite.py +115 -0
  202. warp/tests/test_mat_scalar_ops.py +2889 -0
  203. warp/tests/test_math.py +103 -9
  204. warp/tests/test_matmul.py +304 -69
  205. warp/tests/test_matmul_lite.py +410 -0
  206. warp/tests/test_mesh.py +60 -22
  207. warp/tests/test_mesh_query_aabb.py +21 -25
  208. warp/tests/test_mesh_query_point.py +111 -22
  209. warp/tests/test_mesh_query_ray.py +12 -24
  210. warp/tests/test_mlp.py +30 -22
  211. warp/tests/test_model.py +92 -89
  212. warp/tests/test_modules_lite.py +39 -0
  213. warp/tests/test_multigpu.py +88 -114
  214. warp/tests/test_noise.py +12 -11
  215. warp/tests/test_operators.py +16 -20
  216. warp/tests/test_options.py +11 -11
  217. warp/tests/test_pinned.py +17 -18
  218. warp/tests/test_print.py +32 -11
  219. warp/tests/test_quat.py +275 -129
  220. warp/tests/test_rand.py +18 -16
  221. warp/tests/test_reload.py +38 -34
  222. warp/tests/test_rounding.py +50 -43
  223. warp/tests/test_runlength_encode.py +168 -20
  224. warp/tests/test_smoothstep.py +9 -11
  225. warp/tests/test_snippet.py +143 -0
  226. warp/tests/test_sparse.py +261 -63
  227. warp/tests/test_spatial.py +276 -243
  228. warp/tests/test_streams.py +110 -85
  229. warp/tests/test_struct.py +268 -63
  230. warp/tests/test_tape.py +39 -21
  231. warp/tests/test_torch.py +90 -86
  232. warp/tests/test_transient_module.py +10 -12
  233. warp/tests/test_types.py +363 -0
  234. warp/tests/test_utils.py +451 -0
  235. warp/tests/test_vec.py +354 -2050
  236. warp/tests/test_vec_lite.py +73 -0
  237. warp/tests/test_vec_scalar_ops.py +2099 -0
  238. warp/tests/test_volume.py +418 -376
  239. warp/tests/test_volume_write.py +124 -134
  240. warp/tests/unittest_serial.py +35 -0
  241. warp/tests/unittest_suites.py +291 -0
  242. warp/tests/unittest_utils.py +342 -0
  243. warp/tests/{test_misc.py → unused_test_misc.py} +13 -5
  244. warp/tests/{test_debug.py → walkthough_debug.py} +3 -17
  245. warp/thirdparty/appdirs.py +36 -45
  246. warp/thirdparty/unittest_parallel.py +589 -0
  247. warp/types.py +622 -211
  248. warp/utils.py +54 -393
  249. warp_lang-1.0.0b6.dist-info/METADATA +238 -0
  250. warp_lang-1.0.0b6.dist-info/RECORD +409 -0
  251. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/WHEEL +1 -1
  252. examples/example_cache_management.py +0 -40
  253. examples/example_multigpu.py +0 -54
  254. examples/example_struct.py +0 -65
  255. examples/fem/example_stokes_transfer_3d.py +0 -210
  256. warp/bin/warp-clang.so +0 -0
  257. warp/bin/warp.so +0 -0
  258. warp/fem/field/discrete_field.py +0 -80
  259. warp/fem/space/nodal_function_space.py +0 -233
  260. warp/tests/test_all.py +0 -223
  261. warp/tests/test_array_scan.py +0 -60
  262. warp/tests/test_base.py +0 -208
  263. warp/tests/test_unresolved_func.py +0 -7
  264. warp/tests/test_unresolved_symbol.py +0 -7
  265. warp_lang-1.0.0b2.dist-info/METADATA +0 -26
  266. warp_lang-1.0.0b2.dist-info/RECORD +0 -380
  267. /warp/tests/{test_compile_consts_dummy.py → aux_test_compile_consts_dummy.py} +0 -0
  268. /warp/tests/{test_reference_reference.py → aux_test_reference_reference.py} +0 -0
  269. /warp/tests/{test_square.py → aux_test_square.py} +0 -0
  270. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/LICENSE.md +0 -0
  271. {warp_lang-1.0.0b2.dist-info → warp_lang-1.0.0b6.dist-info}/top_level.txt +0 -0
warp/fem/types.py CHANGED
@@ -1,13 +1,9 @@
1
1
  import warp as wp
2
2
 
3
-
4
- vec1i = wp.types.vector(length=1, dtype=wp.int32)
5
- vec2i = wp.types.vector(length=2, dtype=wp.int32)
6
- vec3i = wp.types.vector(length=3, dtype=wp.int32)
7
- vec4i = wp.types.vector(length=4, dtype=wp.int32)
8
- vec8i = wp.types.vector(length=8, dtype=wp.int32)
9
-
10
- vec6 = wp.types.vector(length=6, dtype=wp.float32)
3
+ # kept to avoid breaking existing example code, no longer used internally
4
+ vec2i = wp.vec2i
5
+ vec3i = wp.vec3i
6
+ vec4i = wp.vec4i
11
7
 
12
8
  Coords = wp.vec3
13
9
  OUTSIDE = wp.constant(-1.0e8)
@@ -20,7 +16,7 @@ NULL_ELEMENT_INDEX = wp.constant(-1)
20
16
  NULL_QP_INDEX = wp.constant(-1)
21
17
  NULL_NODE_INDEX = wp.constant(-1)
22
18
 
23
- DofIndex = vec2i
19
+ DofIndex = wp.vec2i
24
20
  """Opaque descriptor for indexing degrees of freedom within elements"""
25
21
  NULL_DOF_INDEX = wp.constant(DofIndex(-1, -1))
26
22
 
@@ -59,12 +55,18 @@ class Sample:
59
55
  """For bilinear form assembly, index of the trial degree-of-freedom currently being considered"""
60
56
 
61
57
 
58
+ @wp.func
59
+ def make_free_sample(element_index: ElementIndex, element_coords: Coords):
60
+ """Returns a :class:`Sample` that is not associated to any quadrature point or dof"""
61
+ return Sample(element_index, element_coords, NULL_QP_INDEX, 0.0, NULL_DOF_INDEX, NULL_DOF_INDEX)
62
+
63
+
62
64
  class Field:
63
65
  """
64
66
  Tag for field-like integrand arguments
65
67
  """
66
68
 
67
- call_operator: "wp.fem.Operator" = None # Set in operator.py
69
+ call_operator: "warp.fem.operator.Operator" = None # Set in operator.py
68
70
 
69
71
 
70
72
  class Domain:
@@ -72,4 +74,4 @@ class Domain:
72
74
  Tag for domain-like integrand arguments
73
75
  """
74
76
 
75
- call_operator: "wp.fem.Operator" = None # Set in operator.py
77
+ call_operator: "warp.fem.operator.Operator" = None # Set in operator.py
warp/fem/utils.py CHANGED
@@ -1,83 +1,123 @@
1
1
  from typing import Any, Tuple
2
2
 
3
- import warp as wp
4
- from warp.utils import radix_sort_pairs, runlength_encode, array_scan
3
+ import numpy as np
5
4
 
6
- from .types import vec6
5
+ import warp as wp
6
+ from warp.fem.cache import (
7
+ Temporary,
8
+ TemporaryStore,
9
+ borrow_temporary,
10
+ borrow_temporary_like,
11
+ )
12
+ from warp.utils import array_scan, radix_sort_pairs, runlength_encode
7
13
 
8
14
 
9
15
  @wp.func
10
- def generalized_outer(x: Any, y: wp.vec2):
16
+ def generalized_outer(x: Any, y: Any):
17
+ """Generalized outer product allowing for the first argument to be a scalar"""
11
18
  return wp.outer(x, y)
12
19
 
13
20
 
14
21
  @wp.func
15
- def generalized_outer(x: Any, y: wp.vec3):
16
- return wp.outer(x, y)
22
+ def generalized_outer(x: wp.float32, y: wp.vec2):
23
+ return x * y
17
24
 
18
25
 
19
26
  @wp.func
20
- def generalized_outer(x: Any, y: wp.float32):
27
+ def generalized_outer(x: wp.float32, y: wp.vec3):
21
28
  return x * y
22
29
 
23
30
 
24
31
  @wp.func
25
- def unit_element(template_type: wp.float32, coord: int):
26
- return 1.0
32
+ def generalized_inner(x: Any, y: Any):
33
+ """Generalized inner product allowing for the first argument to be a tensor"""
34
+ return wp.dot(x, y)
27
35
 
28
36
 
29
37
  @wp.func
30
- def unit_element(template_type: wp.vec2, coord: int):
31
- t = wp.vec2(0.0)
32
- t[coord] = 1.0
33
- return t
38
+ def generalized_inner(x: wp.mat22, y: wp.vec2):
39
+ return x[0] * y[0] + x[1] * y[1]
34
40
 
35
41
 
36
42
  @wp.func
37
- def unit_element(template_type: wp.vec3, coord: int):
38
- t = wp.vec3(0.0)
39
- t[coord] = 1.0
40
- return t
43
+ def generalized_inner(x: wp.mat33, y: wp.vec3):
44
+ return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]
45
+
46
+
47
+ @wp.func
48
+ def apply_right(x: Any, y: Any):
49
+ """Performs x y multiplication with y a square matrix and x either a row-vector or a matrix.
50
+ Will be removed once native @ operator is implemented.
51
+ """
52
+ return x * y
41
53
 
42
54
 
43
55
  @wp.func
44
- def unit_element(template_type: vec6, coord: int):
45
- t = vec6(0.0)
56
+ def apply_right(x: wp.vec2, y: wp.mat22):
57
+ return x[0] * y[0] + x[1] * y[1]
58
+
59
+
60
+ @wp.func
61
+ def apply_right(x: wp.vec3, y: wp.mat33):
62
+ return x[0] * y[0] + x[1] * y[1] + x[2] * y[2]
63
+
64
+
65
+ @wp.func
66
+ def unit_element(template_type: Any, coord: int):
67
+ """Returns a instance of `template_type` with a single coordinate set to 1 in the canonical basis"""
68
+
69
+ t = type(template_type)(0.0)
46
70
  t[coord] = 1.0
47
71
  return t
48
72
 
49
73
 
74
+ @wp.func
75
+ def unit_element(template_type: wp.float32, coord: int):
76
+ return 1.0
77
+
78
+
50
79
  @wp.func
51
80
  def unit_element(template_type: wp.mat22, coord: int):
52
81
  t = wp.mat22(0.0)
53
- t[coord // 2, coord % 2] = 1.0
82
+ row = coord // 2
83
+ col = coord - 2 * row
84
+ t[row, col] = 1.0
54
85
  return t
55
86
 
56
87
 
57
88
  @wp.func
58
89
  def unit_element(template_type: wp.mat33, coord: int):
59
- t = wp.mat22(0.0)
60
- t[coord // 3, coord % 3] = 1.0
90
+ t = wp.mat33(0.0)
91
+ row = coord // 3
92
+ col = coord - 3 * row
93
+ t[row, col] = 1.0
61
94
  return t
62
95
 
63
96
 
64
97
  @wp.func
65
- def symmetric_part(x: wp.mat22):
66
- off_diag = 0.5 * (x[0, 1] + x[1, 0])
67
- return wp.mat22(x[0, 0], off_diag, off_diag, x[1, 1])
98
+ def symmetric_part(x: Any):
99
+ """Symmetric part of a square tensor"""
100
+ return 0.5 * (x + wp.transpose(x))
68
101
 
69
102
 
70
103
  @wp.func
71
- def symmetric_part(x: wp.mat33):
72
- d = 0.5 * (x[1, 2] + x[2, 1])
73
- e = 0.5 * (x[2, 0] + x[0, 2])
74
- f = 0.5 * (x[0, 1] + x[1, 0])
75
- return wp.mat33(x[0, 0], f, e, f, x[1, 1], d, e, d, x[2, 2])
104
+ def skew_part(x: wp.mat22):
105
+ """Skew part of a 2x2 tensor as corresponding rotation angle"""
106
+ return 0.5 * (x[1, 0] - x[0, 1])
107
+
108
+
109
+ @wp.func
110
+ def skew_part(x: wp.mat33):
111
+ """Skew part of a 3x3 tensor as the corresponding rotation vector"""
112
+ a = 0.5 * (x[2, 1] - x[1, 2])
113
+ b = 0.5 * (x[0, 2] - x[2, 0])
114
+ c = 0.5 * (x[1, 0] - x[0, 1])
115
+ return wp.vec3(a, b, c)
76
116
 
77
117
 
78
118
  def compress_node_indices(
79
- node_count: int, node_indices: wp.array(dtype=int)
80
- ) -> Tuple[wp.array, wp.array, int, wp.array]:
119
+ node_count: int, node_indices: wp.array(dtype=int), temporary_store: TemporaryStore = None
120
+ ) -> Tuple[Temporary, Temporary, int, Temporary]:
81
121
  """
82
122
  Compress an unsorted list of node indices into:
83
123
  - a node_offsets array, giving for each node the start offset of corresponding indices in sorted_array_indices
@@ -87,8 +127,14 @@ def compress_node_indices(
87
127
  """
88
128
 
89
129
  index_count = node_indices.size
90
- sorted_node_indices = wp.empty(2 * index_count, dtype=int, device=node_indices.device)
91
- sorted_array_indices = wp.empty_like(sorted_node_indices)
130
+
131
+ sorted_node_indices_temp = borrow_temporary(
132
+ temporary_store, shape=2 * index_count, dtype=int, device=node_indices.device
133
+ )
134
+ sorted_array_indices_temp = borrow_temporary_like(sorted_node_indices_temp, temporary_store)
135
+
136
+ sorted_node_indices = sorted_node_indices_temp.array
137
+ sorted_array_indices = sorted_array_indices_temp.array
92
138
 
93
139
  wp.copy(dest=sorted_node_indices, src=node_indices, count=index_count)
94
140
 
@@ -104,14 +150,44 @@ def compress_node_indices(
104
150
  radix_sort_pairs(sorted_node_indices, sorted_array_indices, count=index_count)
105
151
 
106
152
  # Build prefix sum of number of elements per node
107
- unique_node_indices = wp.empty(n=index_count, dtype=int, device=node_indices.device)
108
- node_element_counts = wp.empty(n=index_count, dtype=int, device=node_indices.device)
109
- unique_node_count = runlength_encode(
110
- sorted_node_indices, unique_node_indices, node_element_counts, value_count=index_count
153
+ unique_node_indices_temp = borrow_temporary(
154
+ temporary_store, shape=index_count, dtype=int, device=node_indices.device
155
+ )
156
+ node_element_counts_temp = borrow_temporary(
157
+ temporary_store, shape=index_count, dtype=int, device=node_indices.device
158
+ )
159
+
160
+ unique_node_indices = unique_node_indices_temp.array
161
+ node_element_counts = node_element_counts_temp.array
162
+
163
+ unique_node_count_dev = borrow_temporary(temporary_store, shape=(1,), dtype=int, device=sorted_node_indices.device)
164
+ runlength_encode(
165
+ sorted_node_indices,
166
+ unique_node_indices,
167
+ node_element_counts,
168
+ value_count=index_count,
169
+ run_count=unique_node_count_dev.array,
111
170
  )
112
171
 
172
+ # Transfer unique node count to host
173
+ if node_indices.device.is_cuda:
174
+ unique_node_count_host = borrow_temporary(temporary_store, shape=(1,), dtype=int, pinned=True, device="cpu")
175
+ wp.copy(src=unique_node_count_dev.array, dest=unique_node_count_host.array, count=1)
176
+ wp.synchronize_stream(wp.get_stream(node_indices.device))
177
+ unique_node_count_dev.release()
178
+ unique_node_count = int(unique_node_count_host.array.numpy()[0])
179
+ unique_node_count_host.release()
180
+ else:
181
+ unique_node_count = int(unique_node_count_dev.array.numpy()[0])
182
+ unique_node_count_dev.release()
183
+
113
184
  # Scatter seen run counts to global array of element count per node
114
- node_offsets = wp.zeros(shape=(node_count + 1), device=node_element_counts.device, dtype=int)
185
+ node_offsets_temp = borrow_temporary(
186
+ temporary_store, shape=(node_count + 1), device=node_element_counts.device, dtype=int
187
+ )
188
+ node_offsets = node_offsets_temp.array
189
+
190
+ node_offsets.zero_()
115
191
  wp.launch(
116
192
  kernel=_scatter_node_counts,
117
193
  dim=unique_node_count,
@@ -122,51 +198,47 @@ def compress_node_indices(
122
198
  # Prefix sum of number of elements per node
123
199
  array_scan(node_offsets, node_offsets, inclusive=True)
124
200
 
125
- return node_offsets, sorted_array_indices, unique_node_count, unique_node_indices
126
-
127
-
128
- _pinned_temp_count_buffer = {}
129
-
201
+ sorted_node_indices_temp.release()
202
+ node_element_counts_temp.release()
130
203
 
131
- def _get_pinned_temp_count_buffer(device):
132
- device = str(device)
133
- if device not in _pinned_temp_count_buffer:
134
- _pinned_temp_count_buffer[device] = wp.empty(shape=(1,), dtype=int, pinned=True, device="cpu")
204
+ return node_offsets_temp, sorted_array_indices_temp, unique_node_count, unique_node_indices_temp
135
205
 
136
- return _pinned_temp_count_buffer[device]
137
206
 
138
-
139
- def masked_indices(mask: wp.array(dtype=int), missing_index=-1) -> Tuple[wp.array, wp.array]:
207
+ def masked_indices(
208
+ mask: wp.array, missing_index=-1, temporary_store: TemporaryStore = None
209
+ ) -> Tuple[Temporary, Temporary]:
140
210
  """
141
211
  From an array of boolean masks (must be either 0 or 1), returns:
142
212
  - The list of indices for which the mask is 1
143
213
  - A map associating to each element of the input mask array its local index if non-zero, or missing_index if zero.
144
214
  """
145
215
 
146
- offsets = wp.empty_like(mask)
216
+ offsets_temp = borrow_temporary_like(mask, temporary_store)
217
+ offsets = offsets_temp.array
147
218
 
148
219
  wp.utils.array_scan(mask, offsets, inclusive=True)
149
220
 
150
221
  # Get back total counts on host
151
222
  if offsets.device.is_cuda:
152
- masked_count = _get_pinned_temp_count_buffer(offsets.device)
153
- wp.copy(dest=masked_count, src=offsets, src_offset=offsets.shape[0] - 1, count=1)
154
- wp.synchronize_stream(wp.get_stream())
155
- masked_count = int(masked_count.numpy()[0])
223
+ masked_count_temp = borrow_temporary(temporary_store, shape=1, dtype=int, pinned=True, device="cpu")
224
+ wp.copy(dest=masked_count_temp.array, src=offsets, src_offset=offsets.shape[0] - 1, count=1)
225
+ wp.synchronize_stream(wp.get_stream(offsets.device))
226
+ masked_count = int(masked_count_temp.array.numpy()[0])
227
+ masked_count_temp.release()
156
228
  else:
157
229
  masked_count = int(offsets.numpy()[-1])
158
230
 
159
231
  # Convert counts to indices
160
- indices = wp.empty(n=masked_count, device=mask.device, dtype=int)
232
+ indices_temp = borrow_temporary(temporary_store, shape=masked_count, device=mask.device, dtype=int)
161
233
 
162
234
  wp.launch(
163
235
  kernel=_masked_indices_kernel,
164
236
  dim=offsets.shape,
165
- inputs=[missing_index, mask, offsets, indices, offsets],
237
+ inputs=[missing_index, mask, offsets, indices_temp.array, offsets],
166
238
  device=mask.device,
167
239
  )
168
240
 
169
- return indices, offsets
241
+ return indices_temp, offsets_temp
170
242
 
171
243
 
172
244
  def array_axpy(x: wp.array, y: wp.array, alpha: float = 1.0, beta: float = 1.0):
@@ -177,7 +249,7 @@ def array_axpy(x: wp.array, y: wp.array, alpha: float = 1.0, beta: float = 1.0):
177
249
  alpha = dtype(alpha)
178
250
  beta = dtype(beta)
179
251
 
180
- if x.dtype != y.dtype or x.shape != y.shape or x.device != y.device:
252
+ if not wp.types.types_equal(x.dtype, y.dtype) or x.shape != y.shape or x.device != y.device:
181
253
  raise ValueError("x and y arrays must have same dat atype, shape and device")
182
254
 
183
255
  wp.launch(kernel=_array_axpy_kernel, dim=x.shape, device=x.device, inputs=[x, y, alpha, beta])
@@ -218,3 +290,206 @@ def _masked_indices_kernel(
218
290
  def _array_axpy_kernel(x: wp.array(dtype=Any), y: wp.array(dtype=Any), alpha: Any, beta: Any):
219
291
  i = wp.tid()
220
292
  y[i] = beta * y[i] + alpha * x[i]
293
+
294
+
295
+ def grid_to_tris(Nx: int, Ny: int):
296
+ """Constructs a triangular mesh topology by dividing each cell of a dense 2D grid into two triangles.
297
+
298
+ The resulting triangles will be oriented counter-clockwise assuming that `y` is the fastest moving index direction
299
+
300
+ Args:
301
+ Nx: Resolution of the grid along `x` dimension
302
+ Ny: Resolution of the grid along `y` dimension
303
+
304
+ Returns:
305
+ Array of shape (2 * Nx * Ny, 3) containing vertex indices for each triangle
306
+ """
307
+
308
+ cx, cy = np.meshgrid(np.arange(Nx, dtype=int), np.arange(Ny, dtype=int), indexing="ij")
309
+
310
+ vidx = np.transpose(
311
+ np.array(
312
+ [
313
+ (Ny + 1) * cx + cy,
314
+ (Ny + 1) * (cx + 1) + cy,
315
+ (Ny + 1) * (cx + 1) + (cy + 1),
316
+ (Ny + 1) * cx + cy,
317
+ (Ny + 1) * (cx + 1) + (cy + 1),
318
+ (Ny + 1) * (cx) + (cy + 1),
319
+ ]
320
+ )
321
+ ).reshape((-1, 3))
322
+
323
+ return vidx
324
+
325
+
326
+ def grid_to_tets(Nx: int, Ny: int, Nz: int):
327
+ """Constructs a tetrahedral mesh topology by diving each cell of a dense 3D grid into five tetrahedrons
328
+
329
+ The resulting tets have positive volume assuming that `z` is the fastest moving index direction
330
+
331
+ Args:
332
+ Nx: Resolution of the grid along `x` dimension
333
+ Ny: Resolution of the grid along `y` dimension
334
+ Nz: Resolution of the grid along `z` dimension
335
+
336
+ Returns:
337
+ Array of shape (5 * Nx * Ny * Nz, 4) containing vertex indices for each tet
338
+ """
339
+
340
+ # Global node indices for each cell
341
+ cx, cy, cz = np.meshgrid(
342
+ np.arange(Nx, dtype=int), np.arange(Ny, dtype=int), np.arange(Nz, dtype=int), indexing="ij"
343
+ )
344
+
345
+ grid_vidx = np.array(
346
+ [
347
+ (Ny + 1) * (Nz + 1) * cx + (Nz + 1) * cy + cz,
348
+ (Ny + 1) * (Nz + 1) * cx + (Nz + 1) * cy + cz + 1,
349
+ (Ny + 1) * (Nz + 1) * cx + (Nz + 1) * (cy + 1) + cz,
350
+ (Ny + 1) * (Nz + 1) * cx + (Nz + 1) * (cy + 1) + cz + 1,
351
+ (Ny + 1) * (Nz + 1) * (cx + 1) + (Nz + 1) * cy + cz,
352
+ (Ny + 1) * (Nz + 1) * (cx + 1) + (Nz + 1) * cy + cz + 1,
353
+ (Ny + 1) * (Nz + 1) * (cx + 1) + (Nz + 1) * (cy + 1) + cz,
354
+ (Ny + 1) * (Nz + 1) * (cx + 1) + (Nz + 1) * (cy + 1) + cz + 1,
355
+ ]
356
+ )
357
+
358
+ # decompose grid cells into 5 tets
359
+ tet_vidx = np.array(
360
+ [
361
+ [0, 1, 2, 4],
362
+ [3, 2, 1, 7],
363
+ [5, 1, 7, 4],
364
+ [6, 7, 4, 2],
365
+ [4, 1, 2, 7],
366
+ ]
367
+ )
368
+
369
+ # Convert to 3d index coordinates
370
+ vidx_coords = np.array(
371
+ [
372
+ [0, 0, 0],
373
+ [0, 0, 1],
374
+ [0, 1, 0],
375
+ [0, 1, 1],
376
+ [1, 0, 0],
377
+ [1, 0, 1],
378
+ [1, 1, 0],
379
+ [1, 1, 1],
380
+ ]
381
+ )
382
+ tet_coords = vidx_coords[tet_vidx]
383
+
384
+ # Symmetry bits for each cell
385
+ ox, oy, oz = np.meshgrid(
386
+ np.arange(Nx, dtype=int) % 2, np.arange(Ny, dtype=int) % 2, np.arange(Nz, dtype=int) % 2, indexing="ij"
387
+ )
388
+ tet_coords = np.broadcast_to(tet_coords, shape=(*ox.shape, *tet_coords.shape))
389
+
390
+ # Flip coordinates according to symmetry
391
+ ox_bk = np.broadcast_to(ox.reshape(*ox.shape, 1, 1), tet_coords.shape[:-1])
392
+ oy_bk = np.broadcast_to(oy.reshape(*oy.shape, 1, 1), tet_coords.shape[:-1])
393
+ oz_bk = np.broadcast_to(oz.reshape(*oz.shape, 1, 1), tet_coords.shape[:-1])
394
+
395
+ tet_coords_x = tet_coords[..., 0] ^ ox_bk
396
+ tet_coords_y = tet_coords[..., 1] ^ oy_bk
397
+ tet_coords_z = tet_coords[..., 2] ^ oz_bk
398
+
399
+ # Back to local vertex indices
400
+ corner_indices = 4 * tet_coords_x + 2 * tet_coords_y + tet_coords_z
401
+
402
+ # Now go from cell-local to global node indices
403
+ # There must be a nicer way than this, but for small grids this works
404
+
405
+ corner_indices = corner_indices.reshape(-1, 4)
406
+
407
+ grid_vidx = grid_vidx.reshape((8, -1, 1))
408
+ grid_vidx = np.broadcast_to(grid_vidx, shape=(8, grid_vidx.shape[1], 5))
409
+ grid_vidx = grid_vidx.reshape((8, -1))
410
+
411
+ node_indices = np.arange(corner_indices.shape[0])
412
+ tet_grid_vidx = np.transpose(
413
+ [
414
+ grid_vidx[corner_indices[:, 0], node_indices],
415
+ grid_vidx[corner_indices[:, 1], node_indices],
416
+ grid_vidx[corner_indices[:, 2], node_indices],
417
+ grid_vidx[corner_indices[:, 3], node_indices],
418
+ ]
419
+ )
420
+
421
+ return tet_grid_vidx
422
+
423
+
424
+ def grid_to_quads(Nx: int, Ny: int):
425
+ """Constructs a quadrilateral mesh topology from a dense 2D grid
426
+
427
+ The resulting quads will be indexed counter-clockwise
428
+
429
+ Args:
430
+ Nx: Resolution of the grid along `x` dimension
431
+ Ny: Resolution of the grid along `y` dimension
432
+
433
+ Returns:
434
+ Array of shape (Nx * Ny, 4) containing vertex indices for each quadrilateral
435
+ """
436
+
437
+ quad_vtx = np.array(
438
+ [
439
+ [0, 0],
440
+ [1, 0],
441
+ [1, 1],
442
+ [0, 1],
443
+ ]
444
+ ).T
445
+
446
+ quads = np.stack(np.meshgrid(np.arange(0, Nx), np.arange(0, Ny), indexing="ij"))
447
+
448
+ quads_vtx_shape = (*quads.shape, quad_vtx.shape[1])
449
+ quads_vtx = np.broadcast_to(quads.reshape(*quads.shape, 1), quads_vtx_shape) + np.broadcast_to(
450
+ quad_vtx.reshape(2, 1, 1, quad_vtx.shape[1]), quads_vtx_shape
451
+ )
452
+
453
+ quad_vtx_indices = quads_vtx[0] * (Ny + 1) + quads_vtx[1]
454
+
455
+ return quad_vtx_indices.reshape(-1, 4)
456
+
457
+
458
+ def grid_to_hexes(Nx: int, Ny: int, Nz: int):
459
+ """Constructs a hexahedral mesh topology from a dense 3D grid
460
+
461
+ The resulting hexes will be indexed following usual convention assuming that `z` is the fastest moving index direction
462
+ (counter-clockwise bottom vertices, then counter-clockwise top vertices)
463
+
464
+ Args:
465
+ Nx: Resolution of the grid along `x` dimension
466
+ Ny: Resolution of the grid along `y` dimension
467
+ Nz: Resolution of the grid along `z` dimension
468
+
469
+ Returns:
470
+ Array of shape (Nx * Ny * Nz, 8) containing vertex indices for each hexaedron
471
+ """
472
+
473
+ hex_vtx = np.array(
474
+ [
475
+ [0, 0, 0],
476
+ [1, 0, 0],
477
+ [1, 1, 0],
478
+ [0, 1, 0],
479
+ [0, 0, 1],
480
+ [1, 0, 1],
481
+ [1, 1, 1],
482
+ [0, 1, 1],
483
+ ]
484
+ ).T
485
+
486
+ hexes = np.stack(np.meshgrid(np.arange(0, Nx), np.arange(0, Ny), np.arange(0, Nz), indexing="ij"))
487
+
488
+ hexes_vtx_shape = (*hexes.shape, hex_vtx.shape[1])
489
+ hexes_vtx = np.broadcast_to(hexes.reshape(*hexes.shape, 1), hexes_vtx_shape) + np.broadcast_to(
490
+ hex_vtx.reshape(3, 1, 1, 1, hex_vtx.shape[1]), hexes_vtx_shape
491
+ )
492
+
493
+ hexes_vtx_indices = hexes_vtx[0] * (Nz + 1) * (Ny + 1) + hexes_vtx[1] * (Nz + 1) + hexes_vtx[2]
494
+
495
+ return hexes_vtx_indices.reshape(-1, 8)