warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.1.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 (346) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +115 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3425 -3354
  8. warp/codegen.py +2878 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +45 -45
  11. warp/context.py +5194 -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 +383 -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 +146 -146
  34. warp/examples/benchmarks/benchmark_launches.py +295 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +234 -219
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -126
  39. warp/examples/core/example_marching_cubes.py +188 -174
  40. warp/examples/core/example_mesh.py +174 -155
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -170
  43. warp/examples/core/example_raycast.py +105 -90
  44. warp/examples/core/example_raymarch.py +199 -178
  45. warp/examples/core/example_render_opengl.py +185 -141
  46. warp/examples/core/example_sph.py +405 -387
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -389
  51. warp/examples/fem/example_convection_diffusion.py +182 -168
  52. warp/examples/fem/example_convection_diffusion_dg.py +219 -209
  53. warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
  54. warp/examples/fem/example_deformed_geometry.py +177 -159
  55. warp/examples/fem/example_diffusion.py +201 -173
  56. warp/examples/fem/example_diffusion_3d.py +177 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +221 -214
  58. warp/examples/fem/example_mixed_elasticity.py +244 -222
  59. warp/examples/fem/example_navier_stokes.py +259 -243
  60. warp/examples/fem/example_stokes.py +220 -192
  61. warp/examples/fem/example_stokes_transfer.py +265 -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 +260 -246
  65. warp/examples/optim/example_cloth_throw.py +222 -209
  66. warp/examples/optim/example_diffray.py +566 -536
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -169
  70. warp/examples/optim/example_spring_cage.py +239 -231
  71. warp/examples/optim/example_trajectory.py +223 -199
  72. warp/examples/optim/example_walker.py +306 -293
  73. warp/examples/sim/example_cartpole.py +139 -129
  74. warp/examples/sim/example_cloth.py +196 -186
  75. warp/examples/sim/example_granular.py +124 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -186
  77. warp/examples/sim/example_jacobian_ik.py +236 -214
  78. warp/examples/sim/example_particle_chain.py +118 -105
  79. warp/examples/sim/example_quadruped.py +193 -180
  80. warp/examples/sim/example_rigid_chain.py +197 -187
  81. warp/examples/sim/example_rigid_contact.py +189 -177
  82. warp/examples/sim/example_rigid_force.py +127 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -122
  85. warp/examples/sim/example_soft_body.py +190 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +60 -27
  88. warp/fem/cache.py +401 -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 +15 -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 +744 -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 +441 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/partition.py +374 -376
  106. warp/fem/geometry/quadmesh_2d.py +532 -532
  107. warp/fem/geometry/tetmesh.py +840 -840
  108. warp/fem/geometry/trimesh_2d.py +577 -577
  109. warp/fem/integrate.py +1630 -1615
  110. warp/fem/operator.py +190 -191
  111. warp/fem/polynomial.py +214 -213
  112. warp/fem/quadrature/__init__.py +2 -2
  113. warp/fem/quadrature/pic_quadrature.py +243 -245
  114. warp/fem/quadrature/quadrature.py +295 -294
  115. warp/fem/space/__init__.py +294 -292
  116. warp/fem/space/basis_space.py +488 -489
  117. warp/fem/space/collocated_function_space.py +100 -105
  118. warp/fem/space/dof_mapper.py +236 -236
  119. warp/fem/space/function_space.py +148 -145
  120. warp/fem/space/grid_2d_function_space.py +267 -267
  121. warp/fem/space/grid_3d_function_space.py +305 -306
  122. warp/fem/space/hexmesh_function_space.py +350 -352
  123. warp/fem/space/partition.py +350 -350
  124. warp/fem/space/quadmesh_2d_function_space.py +368 -369
  125. warp/fem/space/restriction.py +158 -160
  126. warp/fem/space/shape/__init__.py +13 -15
  127. warp/fem/space/shape/cube_shape_function.py +738 -738
  128. warp/fem/space/shape/shape_function.py +102 -103
  129. warp/fem/space/shape/square_shape_function.py +611 -611
  130. warp/fem/space/shape/tet_shape_function.py +565 -567
  131. warp/fem/space/shape/triangle_shape_function.py +429 -429
  132. warp/fem/space/tetmesh_function_space.py +294 -292
  133. warp/fem/space/topology.py +297 -295
  134. warp/fem/space/trimesh_2d_function_space.py +223 -221
  135. warp/fem/types.py +77 -77
  136. warp/fem/utils.py +495 -495
  137. warp/jax.py +166 -141
  138. warp/jax_experimental.py +341 -339
  139. warp/native/array.h +1072 -1025
  140. warp/native/builtin.h +1560 -1560
  141. warp/native/bvh.cpp +398 -398
  142. warp/native/bvh.cu +525 -525
  143. warp/native/bvh.h +429 -429
  144. warp/native/clang/clang.cpp +495 -464
  145. warp/native/crt.cpp +31 -31
  146. warp/native/crt.h +334 -334
  147. warp/native/cuda_crt.h +1049 -1049
  148. warp/native/cuda_util.cpp +549 -540
  149. warp/native/cuda_util.h +288 -203
  150. warp/native/cutlass_gemm.cpp +34 -34
  151. warp/native/cutlass_gemm.cu +372 -372
  152. warp/native/error.cpp +66 -66
  153. warp/native/error.h +27 -27
  154. warp/native/fabric.h +228 -228
  155. warp/native/hashgrid.cpp +301 -278
  156. warp/native/hashgrid.cu +78 -77
  157. warp/native/hashgrid.h +227 -227
  158. warp/native/initializer_array.h +32 -32
  159. warp/native/intersect.h +1204 -1204
  160. warp/native/intersect_adj.h +365 -365
  161. warp/native/intersect_tri.h +322 -322
  162. warp/native/marching.cpp +2 -2
  163. warp/native/marching.cu +497 -497
  164. warp/native/marching.h +2 -2
  165. warp/native/mat.h +1498 -1498
  166. warp/native/matnn.h +333 -333
  167. warp/native/mesh.cpp +203 -203
  168. warp/native/mesh.cu +293 -293
  169. warp/native/mesh.h +1887 -1887
  170. warp/native/nanovdb/NanoVDB.h +4782 -4782
  171. warp/native/nanovdb/PNanoVDB.h +2553 -2553
  172. warp/native/nanovdb/PNanoVDBWrite.h +294 -294
  173. warp/native/noise.h +850 -850
  174. warp/native/quat.h +1084 -1084
  175. warp/native/rand.h +299 -299
  176. warp/native/range.h +108 -108
  177. warp/native/reduce.cpp +156 -156
  178. warp/native/reduce.cu +348 -348
  179. warp/native/runlength_encode.cpp +61 -61
  180. warp/native/runlength_encode.cu +46 -46
  181. warp/native/scan.cpp +30 -30
  182. warp/native/scan.cu +36 -36
  183. warp/native/scan.h +7 -7
  184. warp/native/solid_angle.h +442 -442
  185. warp/native/sort.cpp +94 -94
  186. warp/native/sort.cu +97 -97
  187. warp/native/sort.h +14 -14
  188. warp/native/sparse.cpp +337 -337
  189. warp/native/sparse.cu +544 -544
  190. warp/native/spatial.h +630 -630
  191. warp/native/svd.h +562 -562
  192. warp/native/temp_buffer.h +30 -30
  193. warp/native/vec.h +1132 -1132
  194. warp/native/volume.cpp +297 -297
  195. warp/native/volume.cu +32 -32
  196. warp/native/volume.h +538 -538
  197. warp/native/volume_builder.cu +425 -425
  198. warp/native/volume_builder.h +19 -19
  199. warp/native/warp.cpp +1057 -1052
  200. warp/native/warp.cu +2943 -2828
  201. warp/native/warp.h +313 -305
  202. warp/optim/__init__.py +9 -9
  203. warp/optim/adam.py +120 -120
  204. warp/optim/linear.py +1104 -939
  205. warp/optim/sgd.py +104 -92
  206. warp/render/__init__.py +10 -10
  207. warp/render/render_opengl.py +3217 -3204
  208. warp/render/render_usd.py +768 -749
  209. warp/render/utils.py +152 -150
  210. warp/sim/__init__.py +52 -59
  211. warp/sim/articulation.py +685 -685
  212. warp/sim/collide.py +1594 -1590
  213. warp/sim/import_mjcf.py +489 -481
  214. warp/sim/import_snu.py +220 -221
  215. warp/sim/import_urdf.py +536 -516
  216. warp/sim/import_usd.py +887 -881
  217. warp/sim/inertia.py +316 -317
  218. warp/sim/integrator.py +234 -233
  219. warp/sim/integrator_euler.py +1956 -1956
  220. warp/sim/integrator_featherstone.py +1910 -1991
  221. warp/sim/integrator_xpbd.py +3294 -3312
  222. warp/sim/model.py +4473 -4314
  223. warp/sim/particles.py +113 -112
  224. warp/sim/render.py +417 -403
  225. warp/sim/utils.py +413 -410
  226. warp/sparse.py +1227 -1227
  227. warp/stubs.py +2109 -2469
  228. warp/tape.py +1162 -225
  229. warp/tests/__init__.py +1 -1
  230. warp/tests/__main__.py +4 -4
  231. warp/tests/assets/torus.usda +105 -105
  232. warp/tests/aux_test_class_kernel.py +26 -26
  233. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  234. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  235. warp/tests/aux_test_dependent.py +22 -22
  236. warp/tests/aux_test_grad_customs.py +23 -23
  237. warp/tests/aux_test_reference.py +11 -11
  238. warp/tests/aux_test_reference_reference.py +10 -10
  239. warp/tests/aux_test_square.py +17 -17
  240. warp/tests/aux_test_unresolved_func.py +14 -14
  241. warp/tests/aux_test_unresolved_symbol.py +14 -14
  242. warp/tests/disabled_kinematics.py +239 -239
  243. warp/tests/run_coverage_serial.py +31 -31
  244. warp/tests/test_adam.py +157 -157
  245. warp/tests/test_arithmetic.py +1124 -1124
  246. warp/tests/test_array.py +2417 -2326
  247. warp/tests/test_array_reduce.py +150 -150
  248. warp/tests/test_async.py +668 -656
  249. warp/tests/test_atomic.py +141 -141
  250. warp/tests/test_bool.py +204 -149
  251. warp/tests/test_builtins_resolution.py +1292 -1292
  252. warp/tests/test_bvh.py +164 -171
  253. warp/tests/test_closest_point_edge_edge.py +228 -228
  254. warp/tests/test_codegen.py +566 -553
  255. warp/tests/test_compile_consts.py +97 -101
  256. warp/tests/test_conditional.py +246 -246
  257. warp/tests/test_copy.py +232 -215
  258. warp/tests/test_ctypes.py +632 -632
  259. warp/tests/test_dense.py +67 -67
  260. warp/tests/test_devices.py +91 -98
  261. warp/tests/test_dlpack.py +530 -529
  262. warp/tests/test_examples.py +400 -378
  263. warp/tests/test_fabricarray.py +955 -955
  264. warp/tests/test_fast_math.py +62 -54
  265. warp/tests/test_fem.py +1277 -1278
  266. warp/tests/test_fp16.py +130 -130
  267. warp/tests/test_func.py +338 -337
  268. warp/tests/test_generics.py +571 -571
  269. warp/tests/test_grad.py +746 -640
  270. warp/tests/test_grad_customs.py +333 -336
  271. warp/tests/test_hash_grid.py +210 -164
  272. warp/tests/test_import.py +39 -39
  273. warp/tests/test_indexedarray.py +1134 -1134
  274. warp/tests/test_intersect.py +67 -67
  275. warp/tests/test_jax.py +307 -307
  276. warp/tests/test_large.py +167 -164
  277. warp/tests/test_launch.py +354 -354
  278. warp/tests/test_lerp.py +261 -261
  279. warp/tests/test_linear_solvers.py +191 -171
  280. warp/tests/test_lvalue.py +421 -493
  281. warp/tests/test_marching_cubes.py +65 -65
  282. warp/tests/test_mat.py +1801 -1827
  283. warp/tests/test_mat_lite.py +115 -115
  284. warp/tests/test_mat_scalar_ops.py +2907 -2889
  285. warp/tests/test_math.py +126 -193
  286. warp/tests/test_matmul.py +500 -499
  287. warp/tests/test_matmul_lite.py +410 -410
  288. warp/tests/test_mempool.py +188 -190
  289. warp/tests/test_mesh.py +284 -324
  290. warp/tests/test_mesh_query_aabb.py +228 -241
  291. warp/tests/test_mesh_query_point.py +692 -702
  292. warp/tests/test_mesh_query_ray.py +292 -303
  293. warp/tests/test_mlp.py +276 -276
  294. warp/tests/test_model.py +110 -110
  295. warp/tests/test_modules_lite.py +39 -39
  296. warp/tests/test_multigpu.py +163 -163
  297. warp/tests/test_noise.py +248 -248
  298. warp/tests/test_operators.py +250 -250
  299. warp/tests/test_options.py +123 -125
  300. warp/tests/test_peer.py +133 -137
  301. warp/tests/test_pinned.py +78 -78
  302. warp/tests/test_print.py +54 -54
  303. warp/tests/test_quat.py +2086 -2086
  304. warp/tests/test_rand.py +288 -288
  305. warp/tests/test_reload.py +217 -217
  306. warp/tests/test_rounding.py +179 -179
  307. warp/tests/test_runlength_encode.py +190 -190
  308. warp/tests/test_sim_grad.py +243 -0
  309. warp/tests/test_sim_kinematics.py +91 -97
  310. warp/tests/test_smoothstep.py +168 -168
  311. warp/tests/test_snippet.py +305 -266
  312. warp/tests/test_sparse.py +468 -460
  313. warp/tests/test_spatial.py +2148 -2148
  314. warp/tests/test_streams.py +486 -473
  315. warp/tests/test_struct.py +710 -675
  316. warp/tests/test_tape.py +173 -148
  317. warp/tests/test_torch.py +743 -743
  318. warp/tests/test_transient_module.py +87 -87
  319. warp/tests/test_types.py +556 -659
  320. warp/tests/test_utils.py +490 -499
  321. warp/tests/test_vec.py +1264 -1268
  322. warp/tests/test_vec_lite.py +73 -73
  323. warp/tests/test_vec_scalar_ops.py +2099 -2099
  324. warp/tests/test_verify_fp.py +94 -94
  325. warp/tests/test_volume.py +737 -736
  326. warp/tests/test_volume_write.py +255 -265
  327. warp/tests/unittest_serial.py +37 -37
  328. warp/tests/unittest_suites.py +363 -359
  329. warp/tests/unittest_utils.py +603 -578
  330. warp/tests/unused_test_misc.py +71 -71
  331. warp/tests/walkthrough_debug.py +85 -85
  332. warp/thirdparty/appdirs.py +598 -598
  333. warp/thirdparty/dlpack.py +143 -143
  334. warp/thirdparty/unittest_parallel.py +566 -561
  335. warp/torch.py +321 -295
  336. warp/types.py +4504 -4450
  337. warp/utils.py +1008 -821
  338. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
  340. warp_lang-1.1.0.dist-info/RECORD +352 -0
  341. warp/examples/assets/cube.usda +0 -42
  342. warp/examples/assets/sphere.usda +0 -56
  343. warp/examples/assets/torus.usda +0 -105
  344. warp_lang-1.0.2.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/tests/test_fem.py CHANGED
@@ -1,1278 +1,1277 @@
1
- # Copyright (c) 2023 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 math
9
- import unittest
10
-
11
- import numpy as np
12
- import warp as wp
13
- from warp.tests.unittest_utils import *
14
-
15
-
16
- from warp.fem import Field, Sample, Domain, Coords
17
- from warp.fem import integrand, div, grad, curl, D, normal
18
- import warp.fem as fem
19
-
20
- from warp.fem.types import make_free_sample
21
- from warp.fem.geometry.closest_point import project_on_tri_at_origin, project_on_tet_at_origin
22
- from warp.fem.geometry import DeformedGeometry
23
- from warp.fem.space import shape
24
- from warp.fem.cache import dynamic_kernel
25
- from warp.fem.utils import grid_to_tets, grid_to_tris, grid_to_quads, grid_to_hexes
26
-
27
- wp.init()
28
-
29
-
30
- @integrand
31
- def linear_form(s: Sample, u: Field):
32
- return u(s)
33
-
34
-
35
- def test_integrate_gradient(test_case, device):
36
- with wp.ScopedDevice(device):
37
- # Grid geometry
38
- geo = fem.Grid2D(res=wp.vec2i(5))
39
-
40
- # Domain and function spaces
41
- domain = fem.Cells(geometry=geo)
42
- quadrature = fem.RegularQuadrature(domain=domain, order=3)
43
-
44
- scalar_space = fem.make_polynomial_space(geo, degree=3)
45
-
46
- u = scalar_space.make_field()
47
- u.dof_values = wp.zeros_like(u.dof_values, requires_grad=True)
48
-
49
- result = wp.empty(dtype=wp.float64, shape=(1), requires_grad=True)
50
-
51
- tape = wp.Tape()
52
-
53
- # forward pass
54
- with tape:
55
- fem.integrate(linear_form, quadrature=quadrature, fields={"u": u}, output=result)
56
-
57
- tape.backward(result)
58
-
59
- test = fem.make_test(space=scalar_space, domain=domain)
60
- rhs = fem.integrate(linear_form, quadrature=quadrature, fields={"u": test})
61
-
62
- err = np.linalg.norm(rhs.numpy() - u.dof_values.grad.numpy())
63
- test_case.assertLess(err, 1.0e-8)
64
-
65
-
66
- @fem.integrand
67
- def bilinear_field(s: fem.Sample, domain: fem.Domain):
68
- x = domain(s)
69
- return x[0] * x[1]
70
-
71
-
72
- @fem.integrand
73
- def grad_field(s: fem.Sample, p: fem.Field):
74
- return fem.grad(p, s)
75
-
76
-
77
- def test_interpolate_gradient(test_case, device):
78
- with wp.ScopedDevice(device):
79
- # Quad mesh with single element
80
- # so we can test gradient with respect to vertex positions
81
- positions = wp.array([[0.0, 0.0], [0.0, 2.0], [2.0, 0.0], [2.0, 2.0]], dtype=wp.vec2, requires_grad=True)
82
- quads = wp.array([[0, 2, 3, 1]], dtype=int)
83
- geo = fem.Quadmesh2D(quads, positions)
84
-
85
- # Quadratic scalar space
86
- scalar_space = fem.make_polynomial_space(geo, degree=2)
87
-
88
- # Point-based vector space
89
- # So we can test gradient with respect to inteprolation point position
90
- point_coords = wp.array([[[0.5, 0.5, 0.0]]], dtype=fem.Coords, requires_grad=True)
91
- interpolation_nodes = fem.PointBasisSpace(
92
- fem.ExplicitQuadrature(domain=fem.Cells(geo), points=point_coords, weights=wp.array([[1.0]], dtype=float))
93
- )
94
- vector_space = fem.make_collocated_function_space(interpolation_nodes, dtype=wp.vec2)
95
-
96
- # Initialize scalar field with known function
97
- scalar_field = scalar_space.make_field()
98
- scalar_field.dof_values.requires_grad = True
99
- fem.interpolate(bilinear_field, dest=scalar_field)
100
-
101
- # Interpolate gradient at center point
102
- vector_field = vector_space.make_field()
103
- vector_field.dof_values.requires_grad = True
104
- vector_field_restriction = fem.make_restriction(vector_field)
105
- tape = wp.Tape()
106
- with tape:
107
- fem.interpolate(
108
- grad_field,
109
- dest=vector_field_restriction,
110
- fields={"p": scalar_field},
111
- kernel_options={"enable_backward": True},
112
- )
113
-
114
- assert_np_equal(vector_field.dof_values.numpy(), np.array([[1.0, 1.0]]))
115
-
116
- vector_field.dof_values.grad.assign([1.0, 0.0])
117
- tape.backward()
118
-
119
- assert_np_equal(scalar_field.dof_values.grad.numpy(), np.array([0.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.5, 0.0]))
120
- assert_np_equal(
121
- geo.positions.grad.numpy(),
122
- np.array(
123
- [
124
- [0.25, 0.25],
125
- [0.25, 0.25],
126
- [-0.25, -0.25],
127
- [-0.25, -0.25],
128
- ]
129
- ),
130
- )
131
- assert_np_equal(point_coords.grad.numpy(), np.array([[[0.0, 2.0, 0.0]]]))
132
-
133
- tape.zero()
134
- scalar_field.dof_values.grad.zero_()
135
- geo.positions.grad.zero_()
136
- point_coords.grad.zero_()
137
-
138
- vector_field.dof_values.grad.assign([0.0, 1.0])
139
- tape.backward()
140
-
141
- assert_np_equal(scalar_field.dof_values.grad.numpy(), np.array([0.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.5, 0.0, 0.0]))
142
- assert_np_equal(
143
- geo.positions.grad.numpy(),
144
- np.array(
145
- [
146
- [0.25, 0.25],
147
- [-0.25, -0.25],
148
- [0.25, 0.25],
149
- [-0.25, -0.25],
150
- ]
151
- ),
152
- )
153
- assert_np_equal(point_coords.grad.numpy(), np.array([[[2.0, 0.0, 0.0]]]))
154
-
155
-
156
- @integrand
157
- def vector_divergence_form(s: Sample, u: Field, q: Field):
158
- return div(u, s) * q(s)
159
-
160
-
161
- @integrand
162
- def vector_grad_form(s: Sample, u: Field, q: Field):
163
- return wp.dot(u(s), grad(q, s))
164
-
165
-
166
- @integrand
167
- def vector_boundary_form(domain: Domain, s: Sample, u: Field, q: Field):
168
- return wp.dot(u(s) * q(s), normal(domain, s))
169
-
170
-
171
- def test_vector_divergence_theorem(test_case, device):
172
- rng = np.random.default_rng(123)
173
-
174
- with wp.ScopedDevice(device):
175
- # Grid geometry
176
- geo = fem.Grid2D(res=wp.vec2i(5))
177
-
178
- # Domain and function spaces
179
- interior = fem.Cells(geometry=geo)
180
- boundary = fem.BoundarySides(geometry=geo)
181
-
182
- vector_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.vec2)
183
- scalar_space = fem.make_polynomial_space(geo, degree=1, dtype=float)
184
-
185
- u = vector_space.make_field()
186
- u.dof_values = rng.random(size=(u.dof_values.shape[0], 2))
187
-
188
- # Divergence theorem
189
- constant_one = scalar_space.make_field()
190
- constant_one.dof_values.fill_(1.0)
191
-
192
- interior_quadrature = fem.RegularQuadrature(domain=interior, order=vector_space.degree)
193
- boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=vector_space.degree)
194
- div_int = fem.integrate(
195
- vector_divergence_form,
196
- quadrature=interior_quadrature,
197
- fields={"u": u, "q": constant_one},
198
- kernel_options={"enable_backward": False},
199
- )
200
- boundary_int = fem.integrate(
201
- vector_boundary_form,
202
- quadrature=boundary_quadrature,
203
- fields={"u": u.trace(), "q": constant_one.trace()},
204
- kernel_options={"enable_backward": False},
205
- )
206
-
207
- test_case.assertAlmostEqual(div_int, boundary_int, places=5)
208
-
209
- # Integration by parts
210
- q = scalar_space.make_field()
211
- q.dof_values = rng.random(size=q.dof_values.shape[0])
212
-
213
- interior_quadrature = fem.RegularQuadrature(domain=interior, order=vector_space.degree + scalar_space.degree)
214
- boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=vector_space.degree + scalar_space.degree)
215
- div_int = fem.integrate(
216
- vector_divergence_form,
217
- quadrature=interior_quadrature,
218
- fields={"u": u, "q": q},
219
- kernel_options={"enable_backward": False},
220
- )
221
- grad_int = fem.integrate(
222
- vector_grad_form,
223
- quadrature=interior_quadrature,
224
- fields={"u": u, "q": q},
225
- kernel_options={"enable_backward": False},
226
- )
227
- boundary_int = fem.integrate(
228
- vector_boundary_form,
229
- quadrature=boundary_quadrature,
230
- fields={"u": u.trace(), "q": q.trace()},
231
- kernel_options={"enable_backward": False},
232
- )
233
-
234
- test_case.assertAlmostEqual(div_int + grad_int, boundary_int, places=5)
235
-
236
-
237
- @integrand
238
- def tensor_divergence_form(s: Sample, tau: Field, v: Field):
239
- return wp.dot(div(tau, s), v(s))
240
-
241
-
242
- @integrand
243
- def tensor_grad_form(s: Sample, tau: Field, v: Field):
244
- return wp.ddot(wp.transpose(tau(s)), grad(v, s))
245
-
246
-
247
- @integrand
248
- def tensor_boundary_form(domain: Domain, s: Sample, tau: Field, v: Field):
249
- return wp.dot(tau(s) * v(s), normal(domain, s))
250
-
251
-
252
- def test_tensor_divergence_theorem(test_case, device):
253
- rng = np.random.default_rng(123)
254
-
255
- with wp.ScopedDevice(device):
256
- # Grid geometry
257
- geo = fem.Grid2D(res=wp.vec2i(5))
258
-
259
- # Domain and function spaces
260
- interior = fem.Cells(geometry=geo)
261
- boundary = fem.BoundarySides(geometry=geo)
262
-
263
- tensor_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.mat22)
264
- vector_space = fem.make_polynomial_space(geo, degree=1, dtype=wp.vec2)
265
-
266
- tau = tensor_space.make_field()
267
- tau.dof_values = rng.random(size=(tau.dof_values.shape[0], 2, 2))
268
-
269
- # Divergence theorem
270
- constant_vec = vector_space.make_field()
271
- constant_vec.dof_values.fill_(wp.vec2(0.5, 2.0))
272
-
273
- interior_quadrature = fem.RegularQuadrature(domain=interior, order=tensor_space.degree)
274
- boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=tensor_space.degree)
275
- div_int = fem.integrate(
276
- tensor_divergence_form,
277
- quadrature=interior_quadrature,
278
- fields={"tau": tau, "v": constant_vec},
279
- kernel_options={"enable_backward": False},
280
- )
281
- boundary_int = fem.integrate(
282
- tensor_boundary_form,
283
- quadrature=boundary_quadrature,
284
- fields={"tau": tau.trace(), "v": constant_vec.trace()},
285
- kernel_options={"enable_backward": False},
286
- )
287
-
288
- test_case.assertAlmostEqual(div_int, boundary_int, places=5)
289
-
290
- # Integration by parts
291
- v = vector_space.make_field()
292
- v.dof_values = rng.random(size=(v.dof_values.shape[0], 2))
293
-
294
- interior_quadrature = fem.RegularQuadrature(domain=interior, order=tensor_space.degree + vector_space.degree)
295
- boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=tensor_space.degree + vector_space.degree)
296
- div_int = fem.integrate(
297
- tensor_divergence_form,
298
- quadrature=interior_quadrature,
299
- fields={"tau": tau, "v": v},
300
- kernel_options={"enable_backward": False},
301
- )
302
- grad_int = fem.integrate(
303
- tensor_grad_form,
304
- quadrature=interior_quadrature,
305
- fields={"tau": tau, "v": v},
306
- kernel_options={"enable_backward": False},
307
- )
308
- boundary_int = fem.integrate(
309
- tensor_boundary_form,
310
- quadrature=boundary_quadrature,
311
- fields={"tau": tau.trace(), "v": v.trace()},
312
- kernel_options={"enable_backward": False},
313
- )
314
-
315
- test_case.assertAlmostEqual(div_int + grad_int, boundary_int, places=5)
316
-
317
-
318
- @integrand
319
- def grad_decomposition(s: Sample, u: Field, v: Field):
320
- return wp.length_sq(grad(u, s) * v(s) - D(u, s) * v(s) - wp.cross(curl(u, s), v(s)))
321
-
322
-
323
- def test_grad_decomposition(test_case, device):
324
- rng = np.random.default_rng(123)
325
-
326
- with wp.ScopedDevice(device):
327
- # Grid geometry
328
- geo = fem.Grid3D(res=wp.vec3i(5))
329
-
330
- # Domain and function spaces
331
- domain = fem.Cells(geometry=geo)
332
- quadrature = fem.RegularQuadrature(domain=domain, order=4)
333
-
334
- vector_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.vec3)
335
- u = vector_space.make_field()
336
-
337
- u.dof_values = rng.random(size=(u.dof_values.shape[0], 3))
338
-
339
- err = fem.integrate(grad_decomposition, quadrature=quadrature, fields={"u": u, "v": u})
340
- test_case.assertLess(err, 1.0e-8)
341
-
342
-
343
- def _gen_trimesh(N):
344
- x = np.linspace(0.0, 1.0, N + 1)
345
- y = np.linspace(0.0, 1.0, N + 1)
346
-
347
- positions = np.transpose(np.meshgrid(x, y, indexing="ij")).reshape(-1, 2)
348
-
349
- vidx = grid_to_tris(N, N)
350
-
351
- return wp.array(positions, dtype=wp.vec2), wp.array(vidx, dtype=int)
352
-
353
-
354
- def _gen_quadmesh(N):
355
- x = np.linspace(0.0, 1.0, N + 1)
356
- y = np.linspace(0.0, 1.0, N + 1)
357
-
358
- positions = np.transpose(np.meshgrid(x, y, indexing="ij")).reshape(-1, 2)
359
-
360
- vidx = grid_to_quads(N, N)
361
-
362
- return wp.array(positions, dtype=wp.vec2), wp.array(vidx, dtype=int)
363
-
364
-
365
- def _gen_tetmesh(N):
366
- x = np.linspace(0.0, 1.0, N + 1)
367
- y = np.linspace(0.0, 1.0, N + 1)
368
- z = np.linspace(0.0, 1.0, N + 1)
369
-
370
- positions = np.transpose(np.meshgrid(x, y, z, indexing="ij")).reshape(-1, 3)
371
-
372
- vidx = grid_to_tets(N, N, N)
373
-
374
- return wp.array(positions, dtype=wp.vec3), wp.array(vidx, dtype=int)
375
-
376
-
377
- def _gen_hexmesh(N):
378
- x = np.linspace(0.0, 1.0, N + 1)
379
- y = np.linspace(0.0, 1.0, N + 1)
380
- z = np.linspace(0.0, 1.0, N + 1)
381
-
382
- positions = np.transpose(np.meshgrid(x, y, z, indexing="ij")).reshape(-1, 3)
383
-
384
- vidx = grid_to_hexes(N, N, N)
385
-
386
- return wp.array(positions, dtype=wp.vec3), wp.array(vidx, dtype=int)
387
-
388
-
389
- def _launch_test_geometry_kernel(geo: fem.Geometry, device):
390
- @dynamic_kernel(suffix=geo.name, kernel_options={"enable_backward": False})
391
- def test_geo_cells_kernel(
392
- cell_arg: geo.CellArg,
393
- qps: wp.array(dtype=Coords),
394
- qp_weights: wp.array(dtype=float),
395
- cell_measures: wp.array(dtype=float),
396
- ):
397
- cell_index, q = wp.tid()
398
-
399
- coords = qps[q]
400
- s = make_free_sample(cell_index, coords)
401
-
402
- wp.atomic_add(cell_measures, cell_index, geo.cell_measure(cell_arg, s) * qp_weights[q])
403
-
404
- REF_MEASURE = geo.reference_side().measure()
405
-
406
- @dynamic_kernel(suffix=geo.name, kernel_options={"enable_backward": False, "max_unroll": 1})
407
- def test_geo_sides_kernel(
408
- side_arg: geo.SideArg,
409
- qps: wp.array(dtype=Coords),
410
- qp_weights: wp.array(dtype=float),
411
- side_measures: wp.array(dtype=float),
412
- ):
413
- side_index, q = wp.tid()
414
-
415
- coords = qps[q]
416
- s = make_free_sample(side_index, coords)
417
-
418
- cell_arg = geo.side_to_cell_arg(side_arg)
419
- inner_cell_index = geo.side_inner_cell_index(side_arg, side_index)
420
- outer_cell_index = geo.side_outer_cell_index(side_arg, side_index)
421
- inner_cell_coords = geo.side_inner_cell_coords(side_arg, side_index, coords)
422
- outer_cell_coords = geo.side_outer_cell_coords(side_arg, side_index, coords)
423
-
424
- inner_s = make_free_sample(inner_cell_index, inner_cell_coords)
425
- outer_s = make_free_sample(outer_cell_index, outer_cell_coords)
426
-
427
- pos_side = geo.side_position(side_arg, s)
428
- pos_inner = geo.cell_position(cell_arg, inner_s)
429
- pos_outer = geo.cell_position(cell_arg, outer_s)
430
-
431
- for k in range(type(pos_side).length):
432
- wp.expect_near(pos_side[k], pos_inner[k], 0.0001)
433
- wp.expect_near(pos_side[k], pos_outer[k], 0.0001)
434
-
435
- inner_side_coords = geo.side_from_cell_coords(side_arg, side_index, inner_cell_index, inner_cell_coords)
436
- outer_side_coords = geo.side_from_cell_coords(side_arg, side_index, outer_cell_index, outer_cell_coords)
437
-
438
- wp.expect_near(coords, inner_side_coords, 0.0001)
439
- wp.expect_near(coords, outer_side_coords, 0.0001)
440
-
441
- vol = geo.side_measure(side_arg, s)
442
- wp.atomic_add(side_measures, side_index, vol * qp_weights[q])
443
-
444
- # test consistency of side normal, measure, and deformation gradient
445
- F = geo.side_deformation_gradient(side_arg, s)
446
- F_det = DeformedGeometry._side_measure(F)
447
- wp.expect_near(F_det * REF_MEASURE, vol)
448
-
449
- nor = geo.side_normal(side_arg, s)
450
- F_cross = DeformedGeometry._side_normal(F)
451
-
452
- for k in range(type(pos_side).length):
453
- wp.expect_near(F_cross[k], nor[k], 0.0001)
454
-
455
- cell_measures = wp.zeros(dtype=float, device=device, shape=geo.cell_count())
456
-
457
- cell_quadrature = fem.RegularQuadrature(fem.Cells(geo), order=2)
458
- cell_qps = wp.array(cell_quadrature.points, dtype=Coords, device=device)
459
- cell_qp_weights = wp.array(cell_quadrature.weights, dtype=float, device=device)
460
-
461
- wp.launch(
462
- kernel=test_geo_cells_kernel,
463
- dim=(geo.cell_count(), cell_qps.shape[0]),
464
- inputs=[geo.cell_arg_value(device), cell_qps, cell_qp_weights, cell_measures],
465
- device=device,
466
- )
467
-
468
- side_measures = wp.zeros(dtype=float, device=device, shape=geo.side_count())
469
-
470
- side_quadrature = fem.RegularQuadrature(fem.Sides(geo), order=2)
471
- side_qps = wp.array(side_quadrature.points, dtype=Coords, device=device)
472
- side_qp_weights = wp.array(side_quadrature.weights, dtype=float, device=device)
473
-
474
- wp.launch(
475
- kernel=test_geo_sides_kernel,
476
- dim=(geo.side_count(), side_qps.shape[0]),
477
- inputs=[geo.side_arg_value(device), side_qps, side_qp_weights, side_measures],
478
- device=device,
479
- )
480
-
481
- return side_measures, cell_measures
482
-
483
-
484
- def test_grid_2d(test_case, device):
485
- N = 3
486
-
487
- geo = fem.Grid2D(res=wp.vec2i(N))
488
-
489
- test_case.assertEqual(geo.cell_count(), N**2)
490
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 2)
491
- test_case.assertEqual(geo.side_count(), 2 * (N + 1) * N)
492
- test_case.assertEqual(geo.boundary_side_count(), 4 * N)
493
-
494
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
495
-
496
- assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N)), tol=1.0e-4)
497
- assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
498
-
499
-
500
- def test_triangle_mesh(test_case, device):
501
- N = 3
502
-
503
- with wp.ScopedDevice(device):
504
- positions, tri_vidx = _gen_trimesh(N)
505
-
506
- geo = fem.Trimesh2D(tri_vertex_indices=tri_vidx, positions=positions)
507
-
508
- test_case.assertEqual(geo.cell_count(), 2 * (N) ** 2)
509
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 2)
510
- test_case.assertEqual(geo.side_count(), 2 * (N + 1) * N + (N**2))
511
- test_case.assertEqual(geo.boundary_side_count(), 4 * N)
512
-
513
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
514
-
515
- assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 0.5 / (N**2)), tol=1.0e-4)
516
- test_case.assertAlmostEqual(np.sum(side_measures.numpy()), 2 * (N + 1) + N * math.sqrt(2.0), places=4)
517
-
518
-
519
- def test_quad_mesh(test_case, device):
520
- N = 3
521
-
522
- with wp.ScopedDevice(device):
523
- positions, quad_vidx = _gen_quadmesh(N)
524
-
525
- geo = fem.Quadmesh2D(quad_vertex_indices=quad_vidx, positions=positions)
526
-
527
- test_case.assertEqual(geo.cell_count(), N**2)
528
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 2)
529
- test_case.assertEqual(geo.side_count(), 2 * (N + 1) * N)
530
- test_case.assertEqual(geo.boundary_side_count(), 4 * N)
531
-
532
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
533
-
534
- assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N)), tol=1.0e-4)
535
- assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
536
-
537
-
538
- def test_grid_3d(test_case, device):
539
- N = 3
540
-
541
- geo = fem.Grid3D(res=wp.vec3i(N))
542
-
543
- test_case.assertEqual(geo.cell_count(), (N) ** 3)
544
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 3)
545
- test_case.assertEqual(geo.side_count(), 3 * (N + 1) * N**2)
546
- test_case.assertEqual(geo.boundary_side_count(), 6 * N * N)
547
- test_case.assertEqual(geo.edge_count(), 3 * N * (N + 1) ** 2)
548
-
549
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
550
-
551
- assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
552
- assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**3)), tol=1.0e-4)
553
-
554
-
555
- def test_tet_mesh(test_case, device):
556
- N = 3
557
-
558
- with wp.ScopedDevice(device):
559
- positions, tet_vidx = _gen_tetmesh(N)
560
-
561
- geo = fem.Tetmesh(tet_vertex_indices=tet_vidx, positions=positions)
562
-
563
- test_case.assertEqual(geo.cell_count(), 5 * (N) ** 3)
564
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 3)
565
- test_case.assertEqual(geo.side_count(), 6 * (N + 1) * N**2 + (N**3) * 4)
566
- test_case.assertEqual(geo.boundary_side_count(), 12 * N * N)
567
- test_case.assertEqual(geo.edge_count(), 3 * N * (N + 1) * (2 * N + 1))
568
-
569
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
570
-
571
- test_case.assertAlmostEqual(np.sum(cell_measures.numpy()), 1.0, places=4)
572
- test_case.assertAlmostEqual(np.sum(side_measures.numpy()), 0.5 * 6 * (N + 1) + N * 2 * math.sqrt(3.0), places=4)
573
-
574
-
575
- def test_hex_mesh(test_case, device):
576
- N = 3
577
-
578
- with wp.ScopedDevice(device):
579
- positions, tet_vidx = _gen_hexmesh(N)
580
-
581
- geo = fem.Hexmesh(hex_vertex_indices=tet_vidx, positions=positions)
582
-
583
- test_case.assertEqual(geo.cell_count(), (N) ** 3)
584
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 3)
585
- test_case.assertEqual(geo.side_count(), 3 * (N + 1) * N**2)
586
- test_case.assertEqual(geo.boundary_side_count(), 6 * N * N)
587
- test_case.assertEqual(geo.edge_count(), 3 * N * (N + 1) ** 2)
588
-
589
- side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
590
-
591
- assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
592
- assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**3)), tol=1.0e-4)
593
-
594
-
595
- @integrand
596
- def _rigid_deformation_field(s: Sample, domain: Domain, translation: wp.vec3, rotation: wp.vec3, scale: float):
597
- q = wp.quat_from_axis_angle(wp.normalize(rotation), wp.length(rotation))
598
- return translation + scale * wp.quat_rotate(q, domain(s)) - domain(s)
599
-
600
-
601
- def test_deformed_geometry(test_case, device):
602
- N = 3
603
-
604
- with wp.ScopedDevice(device):
605
- positions, tet_vidx = _gen_tetmesh(N)
606
-
607
- geo = fem.Tetmesh(tet_vertex_indices=tet_vidx, positions=positions)
608
-
609
- translation = [1.0, 2.0, 3.0]
610
- rotation = [0.0, math.pi / 4.0, 0.0]
611
- scale = 2.0
612
-
613
- vector_space = fem.make_polynomial_space(geo, dtype=wp.vec3, degree=2)
614
- pos_field = vector_space.make_field()
615
- fem.interpolate(
616
- _rigid_deformation_field,
617
- dest=pos_field,
618
- values={"translation": translation, "rotation": rotation, "scale": scale},
619
- )
620
-
621
- deformed_geo = pos_field.make_deformed_geometry()
622
-
623
- # rigidly-deformed geometry
624
-
625
- test_case.assertEqual(geo.cell_count(), 5 * (N) ** 3)
626
- test_case.assertEqual(geo.vertex_count(), (N + 1) ** 3)
627
- test_case.assertEqual(geo.side_count(), 6 * (N + 1) * N**2 + (N**3) * 4)
628
- test_case.assertEqual(geo.boundary_side_count(), 12 * N * N)
629
-
630
- side_measures, cell_measures = _launch_test_geometry_kernel(deformed_geo, device)
631
-
632
- test_case.assertAlmostEqual(np.sum(cell_measures.numpy()), scale**3, places=4)
633
- test_case.assertAlmostEqual(
634
- np.sum(side_measures.numpy()), scale**2 * (0.5 * 6 * (N + 1) + N * 2 * math.sqrt(3.0)), places=4
635
- )
636
-
637
- @wp.kernel
638
- def _test_deformed_geometry_normal(
639
- geo_index_arg: geo.SideIndexArg, geo_arg: geo.SideArg, def_arg: deformed_geo.SideArg, rotation: wp.vec3
640
- ):
641
- i = wp.tid()
642
- side_index = deformed_geo.boundary_side_index(geo_index_arg, i)
643
-
644
- s = make_free_sample(side_index, Coords(0.5, 0.5, 0.0))
645
- geo_n = geo.side_normal(geo_arg, s)
646
- def_n = deformed_geo.side_normal(def_arg, s)
647
-
648
- q = wp.quat_from_axis_angle(wp.normalize(rotation), wp.length(rotation))
649
- wp.expect_near(wp.quat_rotate(q, geo_n), def_n, 0.001)
650
-
651
- wp.launch(
652
- _test_deformed_geometry_normal,
653
- dim=geo.boundary_side_count(),
654
- device=device,
655
- inputs=[
656
- geo.side_index_arg_value(device),
657
- geo.side_arg_value(device),
658
- deformed_geo.side_arg_value(device),
659
- rotation,
660
- ],
661
- )
662
-
663
- wp.synchronize()
664
-
665
-
666
- @wp.kernel
667
- def _test_closest_point_on_tri_kernel(
668
- e0: wp.vec2,
669
- e1: wp.vec2,
670
- points: wp.array(dtype=wp.vec2),
671
- sq_dist: wp.array(dtype=float),
672
- coords: wp.array(dtype=Coords),
673
- ):
674
- i = wp.tid()
675
- d2, c = project_on_tri_at_origin(points[i], e0, e1)
676
- sq_dist[i] = d2
677
- coords[i] = c
678
-
679
-
680
- @wp.kernel
681
- def _test_closest_point_on_tet_kernel(
682
- e0: wp.vec3,
683
- e1: wp.vec3,
684
- e2: wp.vec3,
685
- points: wp.array(dtype=wp.vec3),
686
- sq_dist: wp.array(dtype=float),
687
- coords: wp.array(dtype=Coords),
688
- ):
689
- i = wp.tid()
690
- d2, c = project_on_tet_at_origin(points[i], e0, e1, e2)
691
- sq_dist[i] = d2
692
- coords[i] = c
693
-
694
-
695
- def test_closest_point_queries(test_case, device):
696
- # Test some simple lookup queries
697
- e0 = wp.vec2(2.0, 0.0)
698
- e1 = wp.vec2(0.0, 2.0)
699
-
700
- points = wp.array(
701
- (
702
- [-1.0, -1.0],
703
- [0.5, 0.5],
704
- [1.0, 1.0],
705
- [2.0, 2.0],
706
- ),
707
- dtype=wp.vec2,
708
- device=device,
709
- )
710
- expected_sq_dist = np.array([2.0, 0.0, 0.0, 2.0])
711
- expected_coords = np.array([[1.0, 0.0, 0.0], [0.5, 0.25, 0.25], [0.0, 0.5, 0.5], [0.0, 0.5, 0.5]])
712
-
713
- sq_dist = wp.empty(shape=points.shape, dtype=float, device=device)
714
- coords = wp.empty(shape=points.shape, dtype=Coords, device=device)
715
- wp.launch(
716
- _test_closest_point_on_tri_kernel, dim=points.shape, device=device, inputs=[e0, e1, points, sq_dist, coords]
717
- )
718
-
719
- assert_np_equal(coords.numpy(), expected_coords)
720
- assert_np_equal(sq_dist.numpy(), expected_sq_dist)
721
-
722
- # Tet
723
-
724
- e0 = wp.vec3(3.0, 0.0, 0.0)
725
- e1 = wp.vec3(0.0, 3.0, 0.0)
726
- e2 = wp.vec3(0.0, 0.0, 3.0)
727
-
728
- points = wp.array(
729
- (
730
- [-1.0, -1.0, -1.0],
731
- [0.5, 0.5, 0.5],
732
- [1.0, 1.0, 1.0],
733
- [2.0, 2.0, 2.0],
734
- ),
735
- dtype=wp.vec3,
736
- device=device,
737
- )
738
- expected_sq_dist = np.array([3.0, 0.0, 0.0, 3.0])
739
- expected_coords = np.array(
740
- [
741
- [0.0, 0.0, 0.0],
742
- [1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0],
743
- [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
744
- [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
745
- ]
746
- )
747
-
748
- sq_dist = wp.empty(shape=points.shape, dtype=float, device=device)
749
- coords = wp.empty(shape=points.shape, dtype=Coords, device=device)
750
- wp.launch(
751
- _test_closest_point_on_tet_kernel, dim=points.shape, device=device, inputs=[e0, e1, e2, points, sq_dist, coords]
752
- )
753
-
754
- assert_np_equal(coords.numpy(), expected_coords, tol=1.0e-4)
755
- assert_np_equal(sq_dist.numpy(), expected_sq_dist, tol=1.0e-4)
756
-
757
-
758
- def test_regular_quadrature(test_case, device):
759
- from warp.fem.geometry.element import LinearEdge, Triangle, Polynomial
760
-
761
- for family in Polynomial:
762
- # test integrating monomials
763
- for degree in range(8):
764
- coords, weights = LinearEdge().instantiate_quadrature(degree, family=family)
765
- res = sum(w * pow(c[0], degree) for w, c in zip(weights, coords))
766
- ref = 1.0 / (degree + 1)
767
-
768
- test_case.assertAlmostEqual(ref, res, places=4)
769
-
770
- # test integrating y^k1 (1 - x)^k2 on triangle using transformation to square
771
- for x_degree in range(4):
772
- for y_degree in range(4):
773
- coords, weights = Triangle().instantiate_quadrature(x_degree + y_degree, family=family)
774
- res = 0.5 * sum(w * pow(1.0 - c[1], x_degree) * pow(c[2], y_degree) for w, c in zip(weights, coords))
775
-
776
- ref = 1.0 / ((x_degree + y_degree + 2) * (y_degree + 1))
777
- # print(x_degree, y_degree, family, len(coords), res, ref)
778
- test_case.assertAlmostEqual(ref, res, places=4)
779
-
780
- # test integrating y^k1 (1 - x)^k2 on triangle using direct formulas
781
- for x_degree in range(5):
782
- for y_degree in range(5):
783
- coords, weights = Triangle().instantiate_quadrature(x_degree + y_degree, family=None)
784
- res = 0.5 * sum(w * pow(1.0 - c[1], x_degree) * pow(c[2], y_degree) for w, c in zip(weights, coords))
785
-
786
- ref = 1.0 / ((x_degree + y_degree + 2) * (y_degree + 1))
787
- test_case.assertAlmostEqual(ref, res, places=4)
788
-
789
-
790
- def test_dof_mapper(test_case, device):
791
- matrix_types = [wp.mat22, wp.mat33]
792
-
793
- # Symmetric mapper
794
- for mapping in fem.SymmetricTensorMapper.Mapping:
795
- for dtype in matrix_types:
796
- mapper = fem.SymmetricTensorMapper(dtype, mapping=mapping)
797
- dof_dtype = mapper.dof_dtype
798
-
799
- for k in range(dof_dtype._length_):
800
- elem = np.array(dof_dtype(0.0))
801
- elem[k] = 1.0
802
- dof_vec = dof_dtype(elem)
803
-
804
- mat = mapper.dof_to_value(dof_vec)
805
- dof_round_trip = mapper.value_to_dof(mat)
806
-
807
- # Check that value_to_dof(dof_to_value) is idempotent
808
- assert_np_equal(np.array(dof_round_trip), np.array(dof_vec))
809
-
810
- # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
811
- frob_norm2 = 0.5 * wp.ddot(mat, mat)
812
- test_case.assertAlmostEqual(frob_norm2, 1.0, places=6)
813
-
814
- # Skew-symmetric mapper
815
- for dtype in matrix_types:
816
- mapper = fem.SkewSymmetricTensorMapper(dtype)
817
- dof_dtype = mapper.dof_dtype
818
-
819
- if hasattr(dof_dtype, "_length_"):
820
- for k in range(dof_dtype._length_):
821
- elem = np.array(dof_dtype(0.0))
822
- elem[k] = 1.0
823
- dof_vec = dof_dtype(elem)
824
-
825
- mat = mapper.dof_to_value(dof_vec)
826
- dof_round_trip = mapper.value_to_dof(mat)
827
-
828
- # Check that value_to_dof(dof_to_value) is idempotent
829
- assert_np_equal(np.array(dof_round_trip), np.array(dof_vec))
830
-
831
- # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
832
- frob_norm2 = 0.5 * wp.ddot(mat, mat)
833
- test_case.assertAlmostEqual(frob_norm2, 1.0, places=6)
834
- else:
835
- dof_val = 1.0
836
-
837
- mat = mapper.dof_to_value(dof_val)
838
- dof_round_trip = mapper.value_to_dof(mat)
839
-
840
- test_case.assertAlmostEqual(dof_round_trip, dof_val)
841
-
842
- # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
843
- frob_norm2 = 0.5 * wp.ddot(mat, mat)
844
- test_case.assertAlmostEqual(frob_norm2, 1.0, places=6)
845
-
846
-
847
- def test_shape_function_weight(test_case, shape: shape.ShapeFunction, coord_sampler, CENTER_COORDS):
848
- NODE_COUNT = shape.NODES_PER_ELEMENT
849
- weight_fn = shape.make_element_inner_weight()
850
- node_coords_fn = shape.make_node_coords_in_element()
851
-
852
- # Weight at node should be 1
853
- @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
854
- def node_unity_test():
855
- n = wp.tid()
856
- node_w = weight_fn(node_coords_fn(n), n)
857
- wp.expect_near(node_w, 1.0, places=5)
858
-
859
- wp.launch(node_unity_test, dim=NODE_COUNT, inputs=[])
860
-
861
- # Sum of node quadrature weights should be one (order 0)
862
- # Sum of weighted quadrature coords should be element center (order 1)
863
- node_quadrature_weight_fn = shape.make_node_quadrature_weight()
864
-
865
- @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
866
- def node_quadrature_unity_test():
867
- sum_node_qp = float(0.0)
868
- sum_node_qp_coords = Coords(0.0)
869
-
870
- for n in range(NODE_COUNT):
871
- w = node_quadrature_weight_fn(n)
872
- sum_node_qp += w
873
- sum_node_qp_coords += w * node_coords_fn(n)
874
-
875
- wp.expect_near(sum_node_qp, 1.0, 0.0001)
876
- wp.expect_near(sum_node_qp_coords, CENTER_COORDS, 0.0001)
877
-
878
- wp.launch(node_quadrature_unity_test, dim=1, inputs=[])
879
-
880
- @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
881
- def partition_of_unity_test():
882
- rng_state = wp.rand_init(4321, wp.tid())
883
- coords = coord_sampler(rng_state)
884
-
885
- # sum of node weights anywhere should be 1.0
886
- w_sum = float(0.0)
887
- for n in range(NODE_COUNT):
888
- w_sum += weight_fn(coords, n)
889
-
890
- wp.expect_near(w_sum, 1.0, 0.0001)
891
-
892
- n_samples = 100
893
- wp.launch(partition_of_unity_test, dim=n_samples, inputs=[])
894
-
895
-
896
- def test_shape_function_trace(test_case, shape: shape.ShapeFunction, CENTER_COORDS):
897
- NODE_COUNT = shape.NODES_PER_ELEMENT
898
- node_coords_fn = shape.make_node_coords_in_element()
899
-
900
- # Sum of node quadrature weights should be one (order 0)
901
- # Sum of weighted quadrature coords should be element center (order 1)
902
- trace_node_quadrature_weight_fn = shape.make_trace_node_quadrature_weight()
903
-
904
- @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
905
- def trace_node_quadrature_unity_test():
906
- sum_node_qp = float(0.0)
907
- sum_node_qp_coords = Coords(0.0)
908
-
909
- for n in range(NODE_COUNT):
910
- coords = node_coords_fn(n)
911
-
912
- if wp.abs(coords[0]) < 1.0e-6:
913
- w = trace_node_quadrature_weight_fn(n)
914
- sum_node_qp += w
915
- sum_node_qp_coords += w * node_coords_fn(n)
916
-
917
- wp.expect_near(sum_node_qp, 1.0, 0.0001)
918
- wp.expect_near(sum_node_qp_coords, CENTER_COORDS, 0.0001)
919
-
920
- wp.launch(trace_node_quadrature_unity_test, dim=1, inputs=[])
921
-
922
-
923
- def test_shape_function_gradient(test_case, shape: shape.ShapeFunction, coord_sampler, coord_delta_sampler):
924
- weight_fn = shape.make_element_inner_weight()
925
- weight_gradient_fn = shape.make_element_inner_weight_gradient()
926
-
927
- @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
928
- def finite_difference_test():
929
- i, n = wp.tid()
930
- rng_state = wp.rand_init(1234, i)
931
-
932
- coords = coord_sampler(rng_state)
933
-
934
- epsilon = 0.003
935
- param_delta, coords_delta = coord_delta_sampler(epsilon, rng_state)
936
-
937
- w_p = weight_fn(coords + coords_delta, n)
938
- w_m = weight_fn(coords - coords_delta, n)
939
-
940
- gp = weight_gradient_fn(coords + coords_delta, n)
941
- gm = weight_gradient_fn(coords - coords_delta, n)
942
-
943
- # 2nd-order finite-difference test
944
- # See Schroeder 2019, Practical course on computing derivatives in code
945
- delta_ref = w_p - w_m
946
- delta_est = wp.dot(gp + gm, param_delta)
947
-
948
- # wp.printf("%d %f %f \n", n, delta_ref, delta_est)
949
- wp.expect_near(delta_ref, delta_est, 0.0001)
950
-
951
- n_samples = 100
952
- wp.launch(finite_difference_test, dim=(n_samples, shape.NODES_PER_ELEMENT), inputs=[])
953
-
954
-
955
- def test_square_shape_functions(test_case, device):
956
- SQUARE_CENTER_COORDS = wp.constant(Coords(0.5, 0.5, 0.0))
957
- SQUARE_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.0))
958
-
959
- @wp.func
960
- def square_coord_sampler(state: wp.uint32):
961
- return Coords(wp.randf(state), wp.randf(state), 0.0)
962
-
963
- @wp.func
964
- def square_coord_delta_sampler(epsilon: float, state: wp.uint32):
965
- param_delta = wp.normalize(wp.vec2(wp.randf(state), wp.randf(state))) * epsilon
966
- return param_delta, Coords(param_delta[0], param_delta[1], 0.0)
967
-
968
- Q_1 = shape.SquareBipolynomialShapeFunctions(degree=1, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
969
- Q_2 = shape.SquareBipolynomialShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
970
- Q_3 = shape.SquareBipolynomialShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
971
-
972
- test_shape_function_weight(test_case, Q_1, square_coord_sampler, SQUARE_CENTER_COORDS)
973
- test_shape_function_weight(test_case, Q_2, square_coord_sampler, SQUARE_CENTER_COORDS)
974
- test_shape_function_weight(test_case, Q_3, square_coord_sampler, SQUARE_CENTER_COORDS)
975
- test_shape_function_trace(test_case, Q_1, SQUARE_SIDE_CENTER_COORDS)
976
- test_shape_function_trace(test_case, Q_2, SQUARE_SIDE_CENTER_COORDS)
977
- test_shape_function_trace(test_case, Q_3, SQUARE_SIDE_CENTER_COORDS)
978
- test_shape_function_gradient(test_case, Q_1, square_coord_sampler, square_coord_delta_sampler)
979
- test_shape_function_gradient(test_case, Q_2, square_coord_sampler, square_coord_delta_sampler)
980
- test_shape_function_gradient(test_case, Q_3, square_coord_sampler, square_coord_delta_sampler)
981
-
982
- Q_1 = shape.SquareBipolynomialShapeFunctions(degree=1, family=fem.Polynomial.GAUSS_LEGENDRE)
983
- Q_2 = shape.SquareBipolynomialShapeFunctions(degree=2, family=fem.Polynomial.GAUSS_LEGENDRE)
984
- Q_3 = shape.SquareBipolynomialShapeFunctions(degree=3, family=fem.Polynomial.GAUSS_LEGENDRE)
985
-
986
- test_shape_function_weight(test_case, Q_1, square_coord_sampler, SQUARE_CENTER_COORDS)
987
- test_shape_function_weight(test_case, Q_2, square_coord_sampler, SQUARE_CENTER_COORDS)
988
- test_shape_function_weight(test_case, Q_3, square_coord_sampler, SQUARE_CENTER_COORDS)
989
- test_shape_function_gradient(test_case, Q_1, square_coord_sampler, square_coord_delta_sampler)
990
- test_shape_function_gradient(test_case, Q_2, square_coord_sampler, square_coord_delta_sampler)
991
- test_shape_function_gradient(test_case, Q_3, square_coord_sampler, square_coord_delta_sampler)
992
-
993
- S_2 = shape.SquareSerendipityShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
994
- S_3 = shape.SquareSerendipityShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
995
-
996
- test_shape_function_weight(test_case, S_2, square_coord_sampler, SQUARE_CENTER_COORDS)
997
- test_shape_function_weight(test_case, S_3, square_coord_sampler, SQUARE_CENTER_COORDS)
998
- test_shape_function_trace(test_case, S_2, SQUARE_SIDE_CENTER_COORDS)
999
- test_shape_function_trace(test_case, S_3, SQUARE_SIDE_CENTER_COORDS)
1000
- test_shape_function_gradient(test_case, S_2, square_coord_sampler, square_coord_delta_sampler)
1001
- test_shape_function_gradient(test_case, S_3, square_coord_sampler, square_coord_delta_sampler)
1002
-
1003
- P_c1 = shape.SquareNonConformingPolynomialShapeFunctions(degree=1)
1004
- P_c2 = shape.SquareNonConformingPolynomialShapeFunctions(degree=2)
1005
- P_c3 = shape.SquareNonConformingPolynomialShapeFunctions(degree=3)
1006
-
1007
- test_shape_function_weight(test_case, P_c1, square_coord_sampler, SQUARE_CENTER_COORDS)
1008
- test_shape_function_weight(test_case, P_c2, square_coord_sampler, SQUARE_CENTER_COORDS)
1009
- test_shape_function_weight(test_case, P_c3, square_coord_sampler, SQUARE_CENTER_COORDS)
1010
- test_shape_function_gradient(test_case, P_c1, square_coord_sampler, square_coord_delta_sampler)
1011
- test_shape_function_gradient(test_case, P_c2, square_coord_sampler, square_coord_delta_sampler)
1012
- test_shape_function_gradient(test_case, P_c3, square_coord_sampler, square_coord_delta_sampler)
1013
-
1014
- wp.synchronize()
1015
-
1016
-
1017
- def test_cube_shape_functions(test_case, device):
1018
- CUBE_CENTER_COORDS = wp.constant(Coords(0.5, 0.5, 0.5))
1019
- CUBE_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.5))
1020
-
1021
- @wp.func
1022
- def cube_coord_sampler(state: wp.uint32):
1023
- return Coords(wp.randf(state), wp.randf(state), wp.randf(state))
1024
-
1025
- @wp.func
1026
- def cube_coord_delta_sampler(epsilon: float, state: wp.uint32):
1027
- param_delta = wp.normalize(wp.vec3(wp.randf(state), wp.randf(state), wp.randf(state))) * epsilon
1028
- return param_delta, param_delta
1029
-
1030
- Q_1 = shape.CubeTripolynomialShapeFunctions(degree=1, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1031
- Q_2 = shape.CubeTripolynomialShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1032
- Q_3 = shape.CubeTripolynomialShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1033
-
1034
- test_shape_function_weight(test_case, Q_1, cube_coord_sampler, CUBE_CENTER_COORDS)
1035
- test_shape_function_weight(test_case, Q_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1036
- test_shape_function_weight(test_case, Q_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1037
- test_shape_function_trace(test_case, Q_1, CUBE_SIDE_CENTER_COORDS)
1038
- test_shape_function_trace(test_case, Q_2, CUBE_SIDE_CENTER_COORDS)
1039
- test_shape_function_trace(test_case, Q_3, CUBE_SIDE_CENTER_COORDS)
1040
- test_shape_function_gradient(test_case, Q_1, cube_coord_sampler, cube_coord_delta_sampler)
1041
- test_shape_function_gradient(test_case, Q_2, cube_coord_sampler, cube_coord_delta_sampler)
1042
- test_shape_function_gradient(test_case, Q_3, cube_coord_sampler, cube_coord_delta_sampler)
1043
-
1044
- Q_1 = shape.CubeTripolynomialShapeFunctions(degree=1, family=fem.Polynomial.GAUSS_LEGENDRE)
1045
- Q_2 = shape.CubeTripolynomialShapeFunctions(degree=2, family=fem.Polynomial.GAUSS_LEGENDRE)
1046
- Q_3 = shape.CubeTripolynomialShapeFunctions(degree=3, family=fem.Polynomial.GAUSS_LEGENDRE)
1047
-
1048
- test_shape_function_weight(test_case, Q_1, cube_coord_sampler, CUBE_CENTER_COORDS)
1049
- test_shape_function_weight(test_case, Q_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1050
- test_shape_function_weight(test_case, Q_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1051
- test_shape_function_gradient(test_case, Q_1, cube_coord_sampler, cube_coord_delta_sampler)
1052
- test_shape_function_gradient(test_case, Q_2, cube_coord_sampler, cube_coord_delta_sampler)
1053
- test_shape_function_gradient(test_case, Q_3, cube_coord_sampler, cube_coord_delta_sampler)
1054
-
1055
- S_2 = shape.CubeSerendipityShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1056
- S_3 = shape.CubeSerendipityShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1057
-
1058
- test_shape_function_weight(test_case, S_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1059
- test_shape_function_weight(test_case, S_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1060
- test_shape_function_trace(test_case, S_2, CUBE_SIDE_CENTER_COORDS)
1061
- test_shape_function_trace(test_case, S_3, CUBE_SIDE_CENTER_COORDS)
1062
- test_shape_function_gradient(test_case, S_2, cube_coord_sampler, cube_coord_delta_sampler)
1063
- test_shape_function_gradient(test_case, S_3, cube_coord_sampler, cube_coord_delta_sampler)
1064
-
1065
- P_c1 = shape.CubeNonConformingPolynomialShapeFunctions(degree=1)
1066
- P_c2 = shape.CubeNonConformingPolynomialShapeFunctions(degree=2)
1067
- P_c3 = shape.CubeNonConformingPolynomialShapeFunctions(degree=3)
1068
-
1069
- test_shape_function_weight(test_case, P_c1, cube_coord_sampler, CUBE_CENTER_COORDS)
1070
- test_shape_function_weight(test_case, P_c2, cube_coord_sampler, CUBE_CENTER_COORDS)
1071
- test_shape_function_weight(test_case, P_c3, cube_coord_sampler, CUBE_CENTER_COORDS)
1072
- test_shape_function_gradient(test_case, P_c1, cube_coord_sampler, cube_coord_delta_sampler)
1073
- test_shape_function_gradient(test_case, P_c2, cube_coord_sampler, cube_coord_delta_sampler)
1074
- test_shape_function_gradient(test_case, P_c3, cube_coord_sampler, cube_coord_delta_sampler)
1075
-
1076
- wp.synchronize()
1077
-
1078
-
1079
- def test_tri_shape_functions(test_case, device):
1080
- TRI_CENTER_COORDS = wp.constant(Coords(1 / 3.0, 1 / 3.0, 1 / 3.0))
1081
- TRI_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.5))
1082
-
1083
- @wp.func
1084
- def tri_coord_sampler(state: wp.uint32):
1085
- a = wp.randf(state)
1086
- b = wp.randf(state)
1087
- return Coords(1.0 - a - b, a, b)
1088
-
1089
- @wp.func
1090
- def tri_coord_delta_sampler(epsilon: float, state: wp.uint32):
1091
- param_delta = wp.normalize(wp.vec2(wp.randf(state), wp.randf(state))) * epsilon
1092
- a = param_delta[0]
1093
- b = param_delta[1]
1094
- return param_delta, Coords(-a - b, a, b)
1095
-
1096
- P_1 = shape.Triangle2DPolynomialShapeFunctions(degree=1)
1097
- P_2 = shape.Triangle2DPolynomialShapeFunctions(degree=2)
1098
- P_3 = shape.Triangle2DPolynomialShapeFunctions(degree=3)
1099
-
1100
- test_shape_function_weight(test_case, P_1, tri_coord_sampler, TRI_CENTER_COORDS)
1101
- test_shape_function_weight(test_case, P_2, tri_coord_sampler, TRI_CENTER_COORDS)
1102
- test_shape_function_weight(test_case, P_3, tri_coord_sampler, TRI_CENTER_COORDS)
1103
- test_shape_function_trace(test_case, P_1, TRI_SIDE_CENTER_COORDS)
1104
- test_shape_function_trace(test_case, P_2, TRI_SIDE_CENTER_COORDS)
1105
- test_shape_function_trace(test_case, P_3, TRI_SIDE_CENTER_COORDS)
1106
- test_shape_function_gradient(test_case, P_1, tri_coord_sampler, tri_coord_delta_sampler)
1107
- test_shape_function_gradient(test_case, P_2, tri_coord_sampler, tri_coord_delta_sampler)
1108
- test_shape_function_gradient(test_case, P_3, tri_coord_sampler, tri_coord_delta_sampler)
1109
-
1110
- P_1d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=1)
1111
- P_2d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=2)
1112
- P_3d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=3)
1113
-
1114
- test_shape_function_weight(test_case, P_1d, tri_coord_sampler, TRI_CENTER_COORDS)
1115
- test_shape_function_weight(test_case, P_2d, tri_coord_sampler, TRI_CENTER_COORDS)
1116
- test_shape_function_weight(test_case, P_3d, tri_coord_sampler, TRI_CENTER_COORDS)
1117
- test_shape_function_gradient(test_case, P_1d, tri_coord_sampler, tri_coord_delta_sampler)
1118
- test_shape_function_gradient(test_case, P_2d, tri_coord_sampler, tri_coord_delta_sampler)
1119
- test_shape_function_gradient(test_case, P_3d, tri_coord_sampler, tri_coord_delta_sampler)
1120
-
1121
- wp.synchronize()
1122
-
1123
-
1124
- def test_tet_shape_functions(test_case, device):
1125
- TET_CENTER_COORDS = wp.constant(Coords(1 / 4.0, 1 / 4.0, 1 / 4.0))
1126
- TET_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 1.0 / 3.0, 1.0 / 3.0))
1127
-
1128
- @wp.func
1129
- def tet_coord_sampler(state: wp.uint32):
1130
- return Coords(wp.randf(state), wp.randf(state), wp.randf(state))
1131
-
1132
- @wp.func
1133
- def tet_coord_delta_sampler(epsilon: float, state: wp.uint32):
1134
- param_delta = wp.normalize(wp.vec3(wp.randf(state), wp.randf(state), wp.randf(state))) * epsilon
1135
- return param_delta, param_delta
1136
-
1137
- P_1 = shape.TetrahedronPolynomialShapeFunctions(degree=1)
1138
- P_2 = shape.TetrahedronPolynomialShapeFunctions(degree=2)
1139
- P_3 = shape.TetrahedronPolynomialShapeFunctions(degree=3)
1140
-
1141
- test_shape_function_weight(test_case, P_1, tet_coord_sampler, TET_CENTER_COORDS)
1142
- test_shape_function_weight(test_case, P_2, tet_coord_sampler, TET_CENTER_COORDS)
1143
- test_shape_function_weight(test_case, P_3, tet_coord_sampler, TET_CENTER_COORDS)
1144
- test_shape_function_trace(test_case, P_1, TET_SIDE_CENTER_COORDS)
1145
- test_shape_function_trace(test_case, P_2, TET_SIDE_CENTER_COORDS)
1146
- test_shape_function_trace(test_case, P_3, TET_SIDE_CENTER_COORDS)
1147
- test_shape_function_gradient(test_case, P_1, tet_coord_sampler, tet_coord_delta_sampler)
1148
- test_shape_function_gradient(test_case, P_2, tet_coord_sampler, tet_coord_delta_sampler)
1149
- test_shape_function_gradient(test_case, P_3, tet_coord_sampler, tet_coord_delta_sampler)
1150
-
1151
- P_1d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=1)
1152
- P_2d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=2)
1153
- P_3d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=3)
1154
-
1155
- test_shape_function_weight(test_case, P_1d, tet_coord_sampler, TET_CENTER_COORDS)
1156
- test_shape_function_weight(test_case, P_2d, tet_coord_sampler, TET_CENTER_COORDS)
1157
- test_shape_function_weight(test_case, P_3d, tet_coord_sampler, TET_CENTER_COORDS)
1158
- test_shape_function_gradient(test_case, P_1d, tet_coord_sampler, tet_coord_delta_sampler)
1159
- test_shape_function_gradient(test_case, P_2d, tet_coord_sampler, tet_coord_delta_sampler)
1160
- test_shape_function_gradient(test_case, P_3d, tet_coord_sampler, tet_coord_delta_sampler)
1161
-
1162
- wp.synchronize()
1163
-
1164
-
1165
- def test_point_basis(test_case, device):
1166
- geo = fem.Grid2D(res=wp.vec2i(2))
1167
-
1168
- domain = fem.Cells(geo)
1169
-
1170
- quadrature = fem.RegularQuadrature(domain, order=2, family=fem.Polynomial.GAUSS_LEGENDRE)
1171
- point_basis = fem.PointBasisSpace(quadrature)
1172
-
1173
- point_space = fem.make_collocated_function_space(point_basis)
1174
- point_test = fem.make_test(point_space, domain=domain)
1175
-
1176
- # Sample at particle positions
1177
- ones = fem.integrate(linear_form, fields={"u": point_test}, nodal=True)
1178
- test_case.assertAlmostEqual(np.sum(ones.numpy()), 1.0, places=5)
1179
-
1180
- # Sampling outside of particle positions
1181
- other_quadrature = fem.RegularQuadrature(domain, order=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1182
- zeros = fem.integrate(linear_form, quadrature=other_quadrature, fields={"u": point_test})
1183
-
1184
- test_case.assertAlmostEqual(np.sum(zeros.numpy()), 0.0, places=5)
1185
-
1186
-
1187
- @fem.integrand
1188
- def _bicubic(s: Sample, domain: Domain):
1189
- x = domain(s)
1190
- return wp.pow(x[0], 3.0) * wp.pow(x[1], 3.0)
1191
-
1192
-
1193
- @fem.integrand
1194
- def _piecewise_constant(s: Sample):
1195
- return float(s.element_index)
1196
-
1197
-
1198
- def test_particle_quadratures(test_case, device):
1199
- geo = fem.Grid2D(res=wp.vec2i(2))
1200
-
1201
- domain = fem.Cells(geo)
1202
- points, weights = domain.reference_element().instantiate_quadrature(order=4, family=fem.Polynomial.GAUSS_LEGENDRE)
1203
- points_per_cell = len(points)
1204
-
1205
- points = points * domain.element_count()
1206
- weights = weights * domain.element_count()
1207
-
1208
- points = wp.array(points, shape=(domain.element_count(), points_per_cell), dtype=Coords, device=device)
1209
- weights = wp.array(weights, shape=(domain.element_count(), points_per_cell), dtype=float, device=device)
1210
-
1211
- explicit_quadrature = fem.ExplicitQuadrature(domain, points, weights)
1212
-
1213
- test_case.assertEqual(explicit_quadrature.points_per_element(), points_per_cell)
1214
- test_case.assertEqual(explicit_quadrature.total_point_count(), points_per_cell * geo.cell_count())
1215
-
1216
- val = fem.integrate(_bicubic, quadrature=explicit_quadrature)
1217
- test_case.assertAlmostEqual(val, 1.0 / 16, places=5)
1218
-
1219
- element_indices = wp.array([3, 3, 2], dtype=int, device=device)
1220
- element_coords = wp.array(
1221
- [
1222
- [0.25, 0.5, 0.0],
1223
- [0.5, 0.25, 0.0],
1224
- [0.5, 0.5, 0.0],
1225
- ],
1226
- dtype=Coords,
1227
- device=device,
1228
- )
1229
-
1230
- pic_quadrature = fem.PicQuadrature(domain, positions=(element_indices, element_coords))
1231
-
1232
- test_case.assertIsNone(pic_quadrature.points_per_element())
1233
- test_case.assertEqual(pic_quadrature.total_point_count(), 3)
1234
- test_case.assertEqual(pic_quadrature.active_cell_count(), 2)
1235
-
1236
- val = fem.integrate(_piecewise_constant, quadrature=pic_quadrature)
1237
- test_case.assertAlmostEqual(val, 1.25, places=5)
1238
-
1239
-
1240
- devices = get_test_devices()
1241
-
1242
-
1243
- class TestFem(unittest.TestCase):
1244
- pass
1245
-
1246
-
1247
- add_function_test(TestFem, "test_regular_quadrature", test_regular_quadrature)
1248
- add_function_test(TestFem, "test_closest_point_queries", test_closest_point_queries)
1249
- add_function_test(TestFem, "test_grad_decomposition", test_grad_decomposition, devices=devices)
1250
- add_function_test(TestFem, "test_integrate_gradient", test_integrate_gradient, devices=devices)
1251
- add_function_test(TestFem, "test_interpolate_gradient", test_interpolate_gradient, devices=devices)
1252
- add_function_test(TestFem, "test_vector_divergence_theorem", test_vector_divergence_theorem, devices=devices)
1253
- add_function_test(TestFem, "test_tensor_divergence_theorem", test_tensor_divergence_theorem, devices=devices)
1254
- add_function_test(TestFem, "test_grid_2d", test_grid_2d, devices=devices)
1255
- add_function_test(TestFem, "test_triangle_mesh", test_triangle_mesh, devices=devices)
1256
- add_function_test(TestFem, "test_quad_mesh", test_quad_mesh, devices=devices)
1257
- add_function_test(TestFem, "test_grid_3d", test_grid_3d, devices=devices)
1258
- add_function_test(TestFem, "test_tet_mesh", test_tet_mesh, devices=devices)
1259
- add_function_test(TestFem, "test_hex_mesh", test_hex_mesh, devices=devices)
1260
- add_function_test(TestFem, "test_deformed_geometry", test_deformed_geometry, devices=devices)
1261
- add_function_test(TestFem, "test_dof_mapper", test_dof_mapper)
1262
- add_function_test(TestFem, "test_point_basis", test_point_basis)
1263
- add_function_test(TestFem, "test_particle_quadratures", test_particle_quadratures)
1264
-
1265
-
1266
- class TestFemShapeFunctions(unittest.TestCase):
1267
- pass
1268
-
1269
-
1270
- add_function_test(TestFemShapeFunctions, "test_square_shape_functions", test_square_shape_functions)
1271
- add_function_test(TestFemShapeFunctions, "test_cube_shape_functions", test_cube_shape_functions)
1272
- add_function_test(TestFemShapeFunctions, "test_tri_shape_functions", test_tri_shape_functions)
1273
- add_function_test(TestFemShapeFunctions, "test_tet_shape_functions", test_tet_shape_functions)
1274
-
1275
-
1276
- if __name__ == "__main__":
1277
- wp.build.clear_kernel_cache()
1278
- unittest.main(verbosity=2)
1
+ # Copyright (c) 2023 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 math
9
+ import unittest
10
+
11
+ import numpy as np
12
+
13
+ import warp as wp
14
+ import warp.fem as fem
15
+ from warp.fem import Coords, D, Domain, Field, Sample, curl, div, grad, integrand, normal
16
+ from warp.fem.cache import dynamic_kernel
17
+ from warp.fem.geometry import DeformedGeometry
18
+ from warp.fem.geometry.closest_point import project_on_tet_at_origin, project_on_tri_at_origin
19
+ from warp.fem.space import shape
20
+ from warp.fem.types import make_free_sample
21
+ from warp.fem.utils import grid_to_hexes, grid_to_quads, grid_to_tets, grid_to_tris
22
+ from warp.tests.unittest_utils import *
23
+
24
+ wp.init()
25
+
26
+
27
+ @integrand
28
+ def linear_form(s: Sample, u: Field):
29
+ return u(s)
30
+
31
+
32
+ def test_integrate_gradient(test, device):
33
+ with wp.ScopedDevice(device):
34
+ # Grid geometry
35
+ geo = fem.Grid2D(res=wp.vec2i(5))
36
+
37
+ # Domain and function spaces
38
+ domain = fem.Cells(geometry=geo)
39
+ quadrature = fem.RegularQuadrature(domain=domain, order=3)
40
+
41
+ scalar_space = fem.make_polynomial_space(geo, degree=3)
42
+
43
+ u = scalar_space.make_field()
44
+ u.dof_values = wp.zeros_like(u.dof_values, requires_grad=True)
45
+
46
+ result = wp.empty(dtype=wp.float64, shape=(1), requires_grad=True)
47
+
48
+ tape = wp.Tape()
49
+
50
+ # forward pass
51
+ with tape:
52
+ fem.integrate(linear_form, quadrature=quadrature, fields={"u": u}, output=result)
53
+
54
+ tape.backward(result)
55
+
56
+ test_field = fem.make_test(space=scalar_space, domain=domain)
57
+ rhs = fem.integrate(linear_form, quadrature=quadrature, fields={"u": test_field})
58
+
59
+ err = np.linalg.norm(rhs.numpy() - u.dof_values.grad.numpy())
60
+ test.assertLess(err, 1.0e-8)
61
+
62
+
63
+ @fem.integrand
64
+ def bilinear_field(s: fem.Sample, domain: fem.Domain):
65
+ x = domain(s)
66
+ return x[0] * x[1]
67
+
68
+
69
+ @fem.integrand
70
+ def grad_field(s: fem.Sample, p: fem.Field):
71
+ return fem.grad(p, s)
72
+
73
+
74
+ def test_interpolate_gradient(test, device):
75
+ with wp.ScopedDevice(device):
76
+ # Quad mesh with single element
77
+ # so we can test gradient with respect to vertex positions
78
+ positions = wp.array([[0.0, 0.0], [0.0, 2.0], [2.0, 0.0], [2.0, 2.0]], dtype=wp.vec2, requires_grad=True)
79
+ quads = wp.array([[0, 2, 3, 1]], dtype=int)
80
+ geo = fem.Quadmesh2D(quads, positions)
81
+
82
+ # Quadratic scalar space
83
+ scalar_space = fem.make_polynomial_space(geo, degree=2)
84
+
85
+ # Point-based vector space
86
+ # So we can test gradient with respect to inteprolation point position
87
+ point_coords = wp.array([[[0.5, 0.5, 0.0]]], dtype=fem.Coords, requires_grad=True)
88
+ interpolation_nodes = fem.PointBasisSpace(
89
+ fem.ExplicitQuadrature(domain=fem.Cells(geo), points=point_coords, weights=wp.array([[1.0]], dtype=float))
90
+ )
91
+ vector_space = fem.make_collocated_function_space(interpolation_nodes, dtype=wp.vec2)
92
+
93
+ # Initialize scalar field with known function
94
+ scalar_field = scalar_space.make_field()
95
+ scalar_field.dof_values.requires_grad = True
96
+ fem.interpolate(bilinear_field, dest=scalar_field)
97
+
98
+ # Interpolate gradient at center point
99
+ vector_field = vector_space.make_field()
100
+ vector_field.dof_values.requires_grad = True
101
+ vector_field_restriction = fem.make_restriction(vector_field)
102
+ tape = wp.Tape()
103
+ with tape:
104
+ fem.interpolate(
105
+ grad_field,
106
+ dest=vector_field_restriction,
107
+ fields={"p": scalar_field},
108
+ kernel_options={"enable_backward": True},
109
+ )
110
+
111
+ assert_np_equal(vector_field.dof_values.numpy(), np.array([[1.0, 1.0]]))
112
+
113
+ vector_field.dof_values.grad.assign([1.0, 0.0])
114
+ tape.backward()
115
+
116
+ assert_np_equal(scalar_field.dof_values.grad.numpy(), np.array([0.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.5, 0.0]))
117
+ assert_np_equal(
118
+ geo.positions.grad.numpy(),
119
+ np.array(
120
+ [
121
+ [0.25, 0.25],
122
+ [0.25, 0.25],
123
+ [-0.25, -0.25],
124
+ [-0.25, -0.25],
125
+ ]
126
+ ),
127
+ )
128
+ assert_np_equal(point_coords.grad.numpy(), np.array([[[0.0, 2.0, 0.0]]]))
129
+
130
+ tape.zero()
131
+ scalar_field.dof_values.grad.zero_()
132
+ geo.positions.grad.zero_()
133
+ point_coords.grad.zero_()
134
+
135
+ vector_field.dof_values.grad.assign([0.0, 1.0])
136
+ tape.backward()
137
+
138
+ assert_np_equal(scalar_field.dof_values.grad.numpy(), np.array([0.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.5, 0.0, 0.0]))
139
+ assert_np_equal(
140
+ geo.positions.grad.numpy(),
141
+ np.array(
142
+ [
143
+ [0.25, 0.25],
144
+ [-0.25, -0.25],
145
+ [0.25, 0.25],
146
+ [-0.25, -0.25],
147
+ ]
148
+ ),
149
+ )
150
+ assert_np_equal(point_coords.grad.numpy(), np.array([[[2.0, 0.0, 0.0]]]))
151
+
152
+
153
+ @integrand
154
+ def vector_divergence_form(s: Sample, u: Field, q: Field):
155
+ return div(u, s) * q(s)
156
+
157
+
158
+ @integrand
159
+ def vector_grad_form(s: Sample, u: Field, q: Field):
160
+ return wp.dot(u(s), grad(q, s))
161
+
162
+
163
+ @integrand
164
+ def vector_boundary_form(domain: Domain, s: Sample, u: Field, q: Field):
165
+ return wp.dot(u(s) * q(s), normal(domain, s))
166
+
167
+
168
+ def test_vector_divergence_theorem(test, device):
169
+ rng = np.random.default_rng(123)
170
+
171
+ with wp.ScopedDevice(device):
172
+ # Grid geometry
173
+ geo = fem.Grid2D(res=wp.vec2i(5))
174
+
175
+ # Domain and function spaces
176
+ interior = fem.Cells(geometry=geo)
177
+ boundary = fem.BoundarySides(geometry=geo)
178
+
179
+ vector_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.vec2)
180
+ scalar_space = fem.make_polynomial_space(geo, degree=1, dtype=float)
181
+
182
+ u = vector_space.make_field()
183
+ u.dof_values = rng.random(size=(u.dof_values.shape[0], 2))
184
+
185
+ # Divergence theorem
186
+ constant_one = scalar_space.make_field()
187
+ constant_one.dof_values.fill_(1.0)
188
+
189
+ interior_quadrature = fem.RegularQuadrature(domain=interior, order=vector_space.degree)
190
+ boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=vector_space.degree)
191
+ div_int = fem.integrate(
192
+ vector_divergence_form,
193
+ quadrature=interior_quadrature,
194
+ fields={"u": u, "q": constant_one},
195
+ kernel_options={"enable_backward": False},
196
+ )
197
+ boundary_int = fem.integrate(
198
+ vector_boundary_form,
199
+ quadrature=boundary_quadrature,
200
+ fields={"u": u.trace(), "q": constant_one.trace()},
201
+ kernel_options={"enable_backward": False},
202
+ )
203
+
204
+ test.assertAlmostEqual(div_int, boundary_int, places=5)
205
+
206
+ # Integration by parts
207
+ q = scalar_space.make_field()
208
+ q.dof_values = rng.random(size=q.dof_values.shape[0])
209
+
210
+ interior_quadrature = fem.RegularQuadrature(domain=interior, order=vector_space.degree + scalar_space.degree)
211
+ boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=vector_space.degree + scalar_space.degree)
212
+ div_int = fem.integrate(
213
+ vector_divergence_form,
214
+ quadrature=interior_quadrature,
215
+ fields={"u": u, "q": q},
216
+ kernel_options={"enable_backward": False},
217
+ )
218
+ grad_int = fem.integrate(
219
+ vector_grad_form,
220
+ quadrature=interior_quadrature,
221
+ fields={"u": u, "q": q},
222
+ kernel_options={"enable_backward": False},
223
+ )
224
+ boundary_int = fem.integrate(
225
+ vector_boundary_form,
226
+ quadrature=boundary_quadrature,
227
+ fields={"u": u.trace(), "q": q.trace()},
228
+ kernel_options={"enable_backward": False},
229
+ )
230
+
231
+ test.assertAlmostEqual(div_int + grad_int, boundary_int, places=5)
232
+
233
+
234
+ @integrand
235
+ def tensor_divergence_form(s: Sample, tau: Field, v: Field):
236
+ return wp.dot(div(tau, s), v(s))
237
+
238
+
239
+ @integrand
240
+ def tensor_grad_form(s: Sample, tau: Field, v: Field):
241
+ return wp.ddot(wp.transpose(tau(s)), grad(v, s))
242
+
243
+
244
+ @integrand
245
+ def tensor_boundary_form(domain: Domain, s: Sample, tau: Field, v: Field):
246
+ return wp.dot(tau(s) * v(s), normal(domain, s))
247
+
248
+
249
+ def test_tensor_divergence_theorem(test, device):
250
+ rng = np.random.default_rng(123)
251
+
252
+ with wp.ScopedDevice(device):
253
+ # Grid geometry
254
+ geo = fem.Grid2D(res=wp.vec2i(5))
255
+
256
+ # Domain and function spaces
257
+ interior = fem.Cells(geometry=geo)
258
+ boundary = fem.BoundarySides(geometry=geo)
259
+
260
+ tensor_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.mat22)
261
+ vector_space = fem.make_polynomial_space(geo, degree=1, dtype=wp.vec2)
262
+
263
+ tau = tensor_space.make_field()
264
+ tau.dof_values = rng.random(size=(tau.dof_values.shape[0], 2, 2))
265
+
266
+ # Divergence theorem
267
+ constant_vec = vector_space.make_field()
268
+ constant_vec.dof_values.fill_(wp.vec2(0.5, 2.0))
269
+
270
+ interior_quadrature = fem.RegularQuadrature(domain=interior, order=tensor_space.degree)
271
+ boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=tensor_space.degree)
272
+ div_int = fem.integrate(
273
+ tensor_divergence_form,
274
+ quadrature=interior_quadrature,
275
+ fields={"tau": tau, "v": constant_vec},
276
+ kernel_options={"enable_backward": False},
277
+ )
278
+ boundary_int = fem.integrate(
279
+ tensor_boundary_form,
280
+ quadrature=boundary_quadrature,
281
+ fields={"tau": tau.trace(), "v": constant_vec.trace()},
282
+ kernel_options={"enable_backward": False},
283
+ )
284
+
285
+ test.assertAlmostEqual(div_int, boundary_int, places=5)
286
+
287
+ # Integration by parts
288
+ v = vector_space.make_field()
289
+ v.dof_values = rng.random(size=(v.dof_values.shape[0], 2))
290
+
291
+ interior_quadrature = fem.RegularQuadrature(domain=interior, order=tensor_space.degree + vector_space.degree)
292
+ boundary_quadrature = fem.RegularQuadrature(domain=boundary, order=tensor_space.degree + vector_space.degree)
293
+ div_int = fem.integrate(
294
+ tensor_divergence_form,
295
+ quadrature=interior_quadrature,
296
+ fields={"tau": tau, "v": v},
297
+ kernel_options={"enable_backward": False},
298
+ )
299
+ grad_int = fem.integrate(
300
+ tensor_grad_form,
301
+ quadrature=interior_quadrature,
302
+ fields={"tau": tau, "v": v},
303
+ kernel_options={"enable_backward": False},
304
+ )
305
+ boundary_int = fem.integrate(
306
+ tensor_boundary_form,
307
+ quadrature=boundary_quadrature,
308
+ fields={"tau": tau.trace(), "v": v.trace()},
309
+ kernel_options={"enable_backward": False},
310
+ )
311
+
312
+ test.assertAlmostEqual(div_int + grad_int, boundary_int, places=5)
313
+
314
+
315
+ @integrand
316
+ def grad_decomposition(s: Sample, u: Field, v: Field):
317
+ return wp.length_sq(grad(u, s) * v(s) - D(u, s) * v(s) - wp.cross(curl(u, s), v(s)))
318
+
319
+
320
+ def test_grad_decomposition(test, device):
321
+ rng = np.random.default_rng(123)
322
+
323
+ with wp.ScopedDevice(device):
324
+ # Grid geometry
325
+ geo = fem.Grid3D(res=wp.vec3i(5))
326
+
327
+ # Domain and function spaces
328
+ domain = fem.Cells(geometry=geo)
329
+ quadrature = fem.RegularQuadrature(domain=domain, order=4)
330
+
331
+ vector_space = fem.make_polynomial_space(geo, degree=2, dtype=wp.vec3)
332
+ u = vector_space.make_field()
333
+
334
+ u.dof_values = rng.random(size=(u.dof_values.shape[0], 3))
335
+
336
+ err = fem.integrate(grad_decomposition, quadrature=quadrature, fields={"u": u, "v": u})
337
+ test.assertLess(err, 1.0e-8)
338
+
339
+
340
+ def _gen_trimesh(N):
341
+ x = np.linspace(0.0, 1.0, N + 1)
342
+ y = np.linspace(0.0, 1.0, N + 1)
343
+
344
+ positions = np.transpose(np.meshgrid(x, y, indexing="ij")).reshape(-1, 2)
345
+
346
+ vidx = grid_to_tris(N, N)
347
+
348
+ return wp.array(positions, dtype=wp.vec2), wp.array(vidx, dtype=int)
349
+
350
+
351
+ def _gen_quadmesh(N):
352
+ x = np.linspace(0.0, 1.0, N + 1)
353
+ y = np.linspace(0.0, 1.0, N + 1)
354
+
355
+ positions = np.transpose(np.meshgrid(x, y, indexing="ij")).reshape(-1, 2)
356
+
357
+ vidx = grid_to_quads(N, N)
358
+
359
+ return wp.array(positions, dtype=wp.vec2), wp.array(vidx, dtype=int)
360
+
361
+
362
+ def _gen_tetmesh(N):
363
+ x = np.linspace(0.0, 1.0, N + 1)
364
+ y = np.linspace(0.0, 1.0, N + 1)
365
+ z = np.linspace(0.0, 1.0, N + 1)
366
+
367
+ positions = np.transpose(np.meshgrid(x, y, z, indexing="ij")).reshape(-1, 3)
368
+
369
+ vidx = grid_to_tets(N, N, N)
370
+
371
+ return wp.array(positions, dtype=wp.vec3), wp.array(vidx, dtype=int)
372
+
373
+
374
+ def _gen_hexmesh(N):
375
+ x = np.linspace(0.0, 1.0, N + 1)
376
+ y = np.linspace(0.0, 1.0, N + 1)
377
+ z = np.linspace(0.0, 1.0, N + 1)
378
+
379
+ positions = np.transpose(np.meshgrid(x, y, z, indexing="ij")).reshape(-1, 3)
380
+
381
+ vidx = grid_to_hexes(N, N, N)
382
+
383
+ return wp.array(positions, dtype=wp.vec3), wp.array(vidx, dtype=int)
384
+
385
+
386
+ def _launch_test_geometry_kernel(geo: fem.Geometry, device):
387
+ @dynamic_kernel(suffix=geo.name, kernel_options={"enable_backward": False})
388
+ def test_geo_cells_kernel(
389
+ cell_arg: geo.CellArg,
390
+ qps: wp.array(dtype=Coords),
391
+ qp_weights: wp.array(dtype=float),
392
+ cell_measures: wp.array(dtype=float),
393
+ ):
394
+ cell_index, q = wp.tid()
395
+
396
+ coords = qps[q]
397
+ s = make_free_sample(cell_index, coords)
398
+
399
+ wp.atomic_add(cell_measures, cell_index, geo.cell_measure(cell_arg, s) * qp_weights[q])
400
+
401
+ REF_MEASURE = geo.reference_side().measure()
402
+
403
+ @dynamic_kernel(suffix=geo.name, kernel_options={"enable_backward": False, "max_unroll": 1})
404
+ def test_geo_sides_kernel(
405
+ side_arg: geo.SideArg,
406
+ qps: wp.array(dtype=Coords),
407
+ qp_weights: wp.array(dtype=float),
408
+ side_measures: wp.array(dtype=float),
409
+ ):
410
+ side_index, q = wp.tid()
411
+
412
+ coords = qps[q]
413
+ s = make_free_sample(side_index, coords)
414
+
415
+ cell_arg = geo.side_to_cell_arg(side_arg)
416
+ inner_cell_index = geo.side_inner_cell_index(side_arg, side_index)
417
+ outer_cell_index = geo.side_outer_cell_index(side_arg, side_index)
418
+ inner_cell_coords = geo.side_inner_cell_coords(side_arg, side_index, coords)
419
+ outer_cell_coords = geo.side_outer_cell_coords(side_arg, side_index, coords)
420
+
421
+ inner_s = make_free_sample(inner_cell_index, inner_cell_coords)
422
+ outer_s = make_free_sample(outer_cell_index, outer_cell_coords)
423
+
424
+ pos_side = geo.side_position(side_arg, s)
425
+ pos_inner = geo.cell_position(cell_arg, inner_s)
426
+ pos_outer = geo.cell_position(cell_arg, outer_s)
427
+
428
+ for k in range(type(pos_side).length):
429
+ wp.expect_near(pos_side[k], pos_inner[k], 0.0001)
430
+ wp.expect_near(pos_side[k], pos_outer[k], 0.0001)
431
+
432
+ inner_side_coords = geo.side_from_cell_coords(side_arg, side_index, inner_cell_index, inner_cell_coords)
433
+ outer_side_coords = geo.side_from_cell_coords(side_arg, side_index, outer_cell_index, outer_cell_coords)
434
+
435
+ wp.expect_near(coords, inner_side_coords, 0.0001)
436
+ wp.expect_near(coords, outer_side_coords, 0.0001)
437
+
438
+ vol = geo.side_measure(side_arg, s)
439
+ wp.atomic_add(side_measures, side_index, vol * qp_weights[q])
440
+
441
+ # test consistency of side normal, measure, and deformation gradient
442
+ F = geo.side_deformation_gradient(side_arg, s)
443
+ F_det = DeformedGeometry._side_measure(F)
444
+ wp.expect_near(F_det * REF_MEASURE, vol)
445
+
446
+ nor = geo.side_normal(side_arg, s)
447
+ F_cross = DeformedGeometry._side_normal(F)
448
+
449
+ for k in range(type(pos_side).length):
450
+ wp.expect_near(F_cross[k], nor[k], 0.0001)
451
+
452
+ cell_measures = wp.zeros(dtype=float, device=device, shape=geo.cell_count())
453
+
454
+ cell_quadrature = fem.RegularQuadrature(fem.Cells(geo), order=2)
455
+ cell_qps = wp.array(cell_quadrature.points, dtype=Coords, device=device)
456
+ cell_qp_weights = wp.array(cell_quadrature.weights, dtype=float, device=device)
457
+
458
+ wp.launch(
459
+ kernel=test_geo_cells_kernel,
460
+ dim=(geo.cell_count(), cell_qps.shape[0]),
461
+ inputs=[geo.cell_arg_value(device), cell_qps, cell_qp_weights, cell_measures],
462
+ device=device,
463
+ )
464
+
465
+ side_measures = wp.zeros(dtype=float, device=device, shape=geo.side_count())
466
+
467
+ side_quadrature = fem.RegularQuadrature(fem.Sides(geo), order=2)
468
+ side_qps = wp.array(side_quadrature.points, dtype=Coords, device=device)
469
+ side_qp_weights = wp.array(side_quadrature.weights, dtype=float, device=device)
470
+
471
+ wp.launch(
472
+ kernel=test_geo_sides_kernel,
473
+ dim=(geo.side_count(), side_qps.shape[0]),
474
+ inputs=[geo.side_arg_value(device), side_qps, side_qp_weights, side_measures],
475
+ device=device,
476
+ )
477
+
478
+ return side_measures, cell_measures
479
+
480
+
481
+ def test_grid_2d(test, device):
482
+ N = 3
483
+
484
+ geo = fem.Grid2D(res=wp.vec2i(N))
485
+
486
+ test.assertEqual(geo.cell_count(), N**2)
487
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 2)
488
+ test.assertEqual(geo.side_count(), 2 * (N + 1) * N)
489
+ test.assertEqual(geo.boundary_side_count(), 4 * N)
490
+
491
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
492
+
493
+ assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N)), tol=1.0e-4)
494
+ assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
495
+
496
+
497
+ def test_triangle_mesh(test, device):
498
+ N = 3
499
+
500
+ with wp.ScopedDevice(device):
501
+ positions, tri_vidx = _gen_trimesh(N)
502
+
503
+ geo = fem.Trimesh2D(tri_vertex_indices=tri_vidx, positions=positions)
504
+
505
+ test.assertEqual(geo.cell_count(), 2 * (N) ** 2)
506
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 2)
507
+ test.assertEqual(geo.side_count(), 2 * (N + 1) * N + (N**2))
508
+ test.assertEqual(geo.boundary_side_count(), 4 * N)
509
+
510
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
511
+
512
+ assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 0.5 / (N**2)), tol=1.0e-4)
513
+ test.assertAlmostEqual(np.sum(side_measures.numpy()), 2 * (N + 1) + N * math.sqrt(2.0), places=4)
514
+
515
+
516
+ def test_quad_mesh(test, device):
517
+ N = 3
518
+
519
+ with wp.ScopedDevice(device):
520
+ positions, quad_vidx = _gen_quadmesh(N)
521
+
522
+ geo = fem.Quadmesh2D(quad_vertex_indices=quad_vidx, positions=positions)
523
+
524
+ test.assertEqual(geo.cell_count(), N**2)
525
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 2)
526
+ test.assertEqual(geo.side_count(), 2 * (N + 1) * N)
527
+ test.assertEqual(geo.boundary_side_count(), 4 * N)
528
+
529
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
530
+
531
+ assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N)), tol=1.0e-4)
532
+ assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
533
+
534
+
535
+ def test_grid_3d(test, device):
536
+ N = 3
537
+
538
+ geo = fem.Grid3D(res=wp.vec3i(N))
539
+
540
+ test.assertEqual(geo.cell_count(), (N) ** 3)
541
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 3)
542
+ test.assertEqual(geo.side_count(), 3 * (N + 1) * N**2)
543
+ test.assertEqual(geo.boundary_side_count(), 6 * N * N)
544
+ test.assertEqual(geo.edge_count(), 3 * N * (N + 1) ** 2)
545
+
546
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
547
+
548
+ assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
549
+ assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**3)), tol=1.0e-4)
550
+
551
+
552
+ def test_tet_mesh(test, device):
553
+ N = 3
554
+
555
+ with wp.ScopedDevice(device):
556
+ positions, tet_vidx = _gen_tetmesh(N)
557
+
558
+ geo = fem.Tetmesh(tet_vertex_indices=tet_vidx, positions=positions)
559
+
560
+ test.assertEqual(geo.cell_count(), 5 * (N) ** 3)
561
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 3)
562
+ test.assertEqual(geo.side_count(), 6 * (N + 1) * N**2 + (N**3) * 4)
563
+ test.assertEqual(geo.boundary_side_count(), 12 * N * N)
564
+ test.assertEqual(geo.edge_count(), 3 * N * (N + 1) * (2 * N + 1))
565
+
566
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
567
+
568
+ test.assertAlmostEqual(np.sum(cell_measures.numpy()), 1.0, places=4)
569
+ test.assertAlmostEqual(np.sum(side_measures.numpy()), 0.5 * 6 * (N + 1) + N * 2 * math.sqrt(3.0), places=4)
570
+
571
+
572
+ def test_hex_mesh(test, device):
573
+ N = 3
574
+
575
+ with wp.ScopedDevice(device):
576
+ positions, tet_vidx = _gen_hexmesh(N)
577
+
578
+ geo = fem.Hexmesh(hex_vertex_indices=tet_vidx, positions=positions)
579
+
580
+ test.assertEqual(geo.cell_count(), (N) ** 3)
581
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 3)
582
+ test.assertEqual(geo.side_count(), 3 * (N + 1) * N**2)
583
+ test.assertEqual(geo.boundary_side_count(), 6 * N * N)
584
+ test.assertEqual(geo.edge_count(), 3 * N * (N + 1) ** 2)
585
+
586
+ side_measures, cell_measures = _launch_test_geometry_kernel(geo, device)
587
+
588
+ assert_np_equal(side_measures.numpy(), np.full(side_measures.shape, 1.0 / (N**2)), tol=1.0e-4)
589
+ assert_np_equal(cell_measures.numpy(), np.full(cell_measures.shape, 1.0 / (N**3)), tol=1.0e-4)
590
+
591
+
592
+ @integrand
593
+ def _rigid_deformation_field(s: Sample, domain: Domain, translation: wp.vec3, rotation: wp.vec3, scale: float):
594
+ q = wp.quat_from_axis_angle(wp.normalize(rotation), wp.length(rotation))
595
+ return translation + scale * wp.quat_rotate(q, domain(s)) - domain(s)
596
+
597
+
598
+ def test_deformed_geometry(test, device):
599
+ N = 3
600
+
601
+ with wp.ScopedDevice(device):
602
+ positions, tet_vidx = _gen_tetmesh(N)
603
+
604
+ geo = fem.Tetmesh(tet_vertex_indices=tet_vidx, positions=positions)
605
+
606
+ translation = [1.0, 2.0, 3.0]
607
+ rotation = [0.0, math.pi / 4.0, 0.0]
608
+ scale = 2.0
609
+
610
+ vector_space = fem.make_polynomial_space(geo, dtype=wp.vec3, degree=2)
611
+ pos_field = vector_space.make_field()
612
+ fem.interpolate(
613
+ _rigid_deformation_field,
614
+ dest=pos_field,
615
+ values={"translation": translation, "rotation": rotation, "scale": scale},
616
+ )
617
+
618
+ deformed_geo = pos_field.make_deformed_geometry()
619
+
620
+ # rigidly-deformed geometry
621
+
622
+ test.assertEqual(geo.cell_count(), 5 * (N) ** 3)
623
+ test.assertEqual(geo.vertex_count(), (N + 1) ** 3)
624
+ test.assertEqual(geo.side_count(), 6 * (N + 1) * N**2 + (N**3) * 4)
625
+ test.assertEqual(geo.boundary_side_count(), 12 * N * N)
626
+
627
+ side_measures, cell_measures = _launch_test_geometry_kernel(deformed_geo, device)
628
+
629
+ test.assertAlmostEqual(
630
+ np.sum(cell_measures.numpy()), scale**3, places=4, msg=f"cell_measures = {cell_measures.numpy()}"
631
+ )
632
+ test.assertAlmostEqual(
633
+ np.sum(side_measures.numpy()), scale**2 * (0.5 * 6 * (N + 1) + N * 2 * math.sqrt(3.0)), places=4
634
+ )
635
+
636
+ @wp.kernel
637
+ def _test_deformed_geometry_normal(
638
+ geo_index_arg: geo.SideIndexArg, geo_arg: geo.SideArg, def_arg: deformed_geo.SideArg, rotation: wp.vec3
639
+ ):
640
+ i = wp.tid()
641
+ side_index = deformed_geo.boundary_side_index(geo_index_arg, i)
642
+
643
+ s = make_free_sample(side_index, Coords(0.5, 0.5, 0.0))
644
+ geo_n = geo.side_normal(geo_arg, s)
645
+ def_n = deformed_geo.side_normal(def_arg, s)
646
+
647
+ q = wp.quat_from_axis_angle(wp.normalize(rotation), wp.length(rotation))
648
+ wp.expect_near(wp.quat_rotate(q, geo_n), def_n, 0.001)
649
+
650
+ wp.launch(
651
+ _test_deformed_geometry_normal,
652
+ dim=geo.boundary_side_count(),
653
+ device=device,
654
+ inputs=[
655
+ geo.side_index_arg_value(device),
656
+ geo.side_arg_value(device),
657
+ deformed_geo.side_arg_value(device),
658
+ rotation,
659
+ ],
660
+ )
661
+
662
+ wp.synchronize()
663
+
664
+
665
+ @wp.kernel
666
+ def _test_closest_point_on_tri_kernel(
667
+ e0: wp.vec2,
668
+ e1: wp.vec2,
669
+ points: wp.array(dtype=wp.vec2),
670
+ sq_dist: wp.array(dtype=float),
671
+ coords: wp.array(dtype=Coords),
672
+ ):
673
+ i = wp.tid()
674
+ d2, c = project_on_tri_at_origin(points[i], e0, e1)
675
+ sq_dist[i] = d2
676
+ coords[i] = c
677
+
678
+
679
+ @wp.kernel
680
+ def _test_closest_point_on_tet_kernel(
681
+ e0: wp.vec3,
682
+ e1: wp.vec3,
683
+ e2: wp.vec3,
684
+ points: wp.array(dtype=wp.vec3),
685
+ sq_dist: wp.array(dtype=float),
686
+ coords: wp.array(dtype=Coords),
687
+ ):
688
+ i = wp.tid()
689
+ d2, c = project_on_tet_at_origin(points[i], e0, e1, e2)
690
+ sq_dist[i] = d2
691
+ coords[i] = c
692
+
693
+
694
+ def test_closest_point_queries(test, device):
695
+ # Test some simple lookup queries
696
+ e0 = wp.vec2(2.0, 0.0)
697
+ e1 = wp.vec2(0.0, 2.0)
698
+
699
+ points = wp.array(
700
+ (
701
+ [-1.0, -1.0],
702
+ [0.5, 0.5],
703
+ [1.0, 1.0],
704
+ [2.0, 2.0],
705
+ ),
706
+ dtype=wp.vec2,
707
+ device=device,
708
+ )
709
+ expected_sq_dist = np.array([2.0, 0.0, 0.0, 2.0])
710
+ expected_coords = np.array([[1.0, 0.0, 0.0], [0.5, 0.25, 0.25], [0.0, 0.5, 0.5], [0.0, 0.5, 0.5]])
711
+
712
+ sq_dist = wp.empty(shape=points.shape, dtype=float, device=device)
713
+ coords = wp.empty(shape=points.shape, dtype=Coords, device=device)
714
+ wp.launch(
715
+ _test_closest_point_on_tri_kernel, dim=points.shape, device=device, inputs=[e0, e1, points, sq_dist, coords]
716
+ )
717
+
718
+ assert_np_equal(coords.numpy(), expected_coords)
719
+ assert_np_equal(sq_dist.numpy(), expected_sq_dist)
720
+
721
+ # Tet
722
+
723
+ e0 = wp.vec3(3.0, 0.0, 0.0)
724
+ e1 = wp.vec3(0.0, 3.0, 0.0)
725
+ e2 = wp.vec3(0.0, 0.0, 3.0)
726
+
727
+ points = wp.array(
728
+ (
729
+ [-1.0, -1.0, -1.0],
730
+ [0.5, 0.5, 0.5],
731
+ [1.0, 1.0, 1.0],
732
+ [2.0, 2.0, 2.0],
733
+ ),
734
+ dtype=wp.vec3,
735
+ device=device,
736
+ )
737
+ expected_sq_dist = np.array([3.0, 0.0, 0.0, 3.0])
738
+ expected_coords = np.array(
739
+ [
740
+ [0.0, 0.0, 0.0],
741
+ [1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0],
742
+ [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
743
+ [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
744
+ ]
745
+ )
746
+
747
+ sq_dist = wp.empty(shape=points.shape, dtype=float, device=device)
748
+ coords = wp.empty(shape=points.shape, dtype=Coords, device=device)
749
+ wp.launch(
750
+ _test_closest_point_on_tet_kernel, dim=points.shape, device=device, inputs=[e0, e1, e2, points, sq_dist, coords]
751
+ )
752
+
753
+ assert_np_equal(coords.numpy(), expected_coords, tol=1.0e-4)
754
+ assert_np_equal(sq_dist.numpy(), expected_sq_dist, tol=1.0e-4)
755
+
756
+
757
+ def test_regular_quadrature(test, device):
758
+ from warp.fem.geometry.element import LinearEdge, Polynomial, Triangle
759
+
760
+ for family in Polynomial:
761
+ # test integrating monomials
762
+ for degree in range(8):
763
+ coords, weights = LinearEdge().instantiate_quadrature(degree, family=family)
764
+ res = sum(w * pow(c[0], degree) for w, c in zip(weights, coords))
765
+ ref = 1.0 / (degree + 1)
766
+
767
+ test.assertAlmostEqual(ref, res, places=4)
768
+
769
+ # test integrating y^k1 (1 - x)^k2 on triangle using transformation to square
770
+ for x_degree in range(4):
771
+ for y_degree in range(4):
772
+ coords, weights = Triangle().instantiate_quadrature(x_degree + y_degree, family=family)
773
+ res = 0.5 * sum(w * pow(1.0 - c[1], x_degree) * pow(c[2], y_degree) for w, c in zip(weights, coords))
774
+
775
+ ref = 1.0 / ((x_degree + y_degree + 2) * (y_degree + 1))
776
+ # print(x_degree, y_degree, family, len(coords), res, ref)
777
+ test.assertAlmostEqual(ref, res, places=4)
778
+
779
+ # test integrating y^k1 (1 - x)^k2 on triangle using direct formulas
780
+ for x_degree in range(5):
781
+ for y_degree in range(5):
782
+ coords, weights = Triangle().instantiate_quadrature(x_degree + y_degree, family=None)
783
+ res = 0.5 * sum(w * pow(1.0 - c[1], x_degree) * pow(c[2], y_degree) for w, c in zip(weights, coords))
784
+
785
+ ref = 1.0 / ((x_degree + y_degree + 2) * (y_degree + 1))
786
+ test.assertAlmostEqual(ref, res, places=4)
787
+
788
+
789
+ def test_dof_mapper(test, device):
790
+ matrix_types = [wp.mat22, wp.mat33]
791
+
792
+ # Symmetric mapper
793
+ for mapping in fem.SymmetricTensorMapper.Mapping:
794
+ for dtype in matrix_types:
795
+ mapper = fem.SymmetricTensorMapper(dtype, mapping=mapping)
796
+ dof_dtype = mapper.dof_dtype
797
+
798
+ for k in range(dof_dtype._length_):
799
+ elem = np.array(dof_dtype(0.0))
800
+ elem[k] = 1.0
801
+ dof_vec = dof_dtype(elem)
802
+
803
+ mat = mapper.dof_to_value(dof_vec)
804
+ dof_round_trip = mapper.value_to_dof(mat)
805
+
806
+ # Check that value_to_dof(dof_to_value) is idempotent
807
+ assert_np_equal(np.array(dof_round_trip), np.array(dof_vec))
808
+
809
+ # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
810
+ frob_norm2 = 0.5 * wp.ddot(mat, mat)
811
+ test.assertAlmostEqual(frob_norm2, 1.0, places=6)
812
+
813
+ # Skew-symmetric mapper
814
+ for dtype in matrix_types:
815
+ mapper = fem.SkewSymmetricTensorMapper(dtype)
816
+ dof_dtype = mapper.dof_dtype
817
+
818
+ if hasattr(dof_dtype, "_length_"):
819
+ for k in range(dof_dtype._length_):
820
+ elem = np.array(dof_dtype(0.0))
821
+ elem[k] = 1.0
822
+ dof_vec = dof_dtype(elem)
823
+
824
+ mat = mapper.dof_to_value(dof_vec)
825
+ dof_round_trip = mapper.value_to_dof(mat)
826
+
827
+ # Check that value_to_dof(dof_to_value) is idempotent
828
+ assert_np_equal(np.array(dof_round_trip), np.array(dof_vec))
829
+
830
+ # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
831
+ frob_norm2 = 0.5 * wp.ddot(mat, mat)
832
+ test.assertAlmostEqual(frob_norm2, 1.0, places=6)
833
+ else:
834
+ dof_val = 1.0
835
+
836
+ mat = mapper.dof_to_value(dof_val)
837
+ dof_round_trip = mapper.value_to_dof(mat)
838
+
839
+ test.assertAlmostEqual(dof_round_trip, dof_val)
840
+
841
+ # Check that value is unitary for Frobenius norm 0.5 * |tau:tau|
842
+ frob_norm2 = 0.5 * wp.ddot(mat, mat)
843
+ test.assertAlmostEqual(frob_norm2, 1.0, places=6)
844
+
845
+
846
+ def test_shape_function_weight(test, shape: shape.ShapeFunction, coord_sampler, CENTER_COORDS):
847
+ NODE_COUNT = shape.NODES_PER_ELEMENT
848
+ weight_fn = shape.make_element_inner_weight()
849
+ node_coords_fn = shape.make_node_coords_in_element()
850
+
851
+ # Weight at node should be 1
852
+ @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
853
+ def node_unity_test():
854
+ n = wp.tid()
855
+ node_w = weight_fn(node_coords_fn(n), n)
856
+ wp.expect_near(node_w, 1.0, places=5)
857
+
858
+ wp.launch(node_unity_test, dim=NODE_COUNT, inputs=[])
859
+
860
+ # Sum of node quadrature weights should be one (order 0)
861
+ # Sum of weighted quadrature coords should be element center (order 1)
862
+ node_quadrature_weight_fn = shape.make_node_quadrature_weight()
863
+
864
+ @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
865
+ def node_quadrature_unity_test():
866
+ sum_node_qp = float(0.0)
867
+ sum_node_qp_coords = Coords(0.0)
868
+
869
+ for n in range(NODE_COUNT):
870
+ w = node_quadrature_weight_fn(n)
871
+ sum_node_qp += w
872
+ sum_node_qp_coords += w * node_coords_fn(n)
873
+
874
+ wp.expect_near(sum_node_qp, 1.0, 0.0001)
875
+ wp.expect_near(sum_node_qp_coords, CENTER_COORDS, 0.0001)
876
+
877
+ wp.launch(node_quadrature_unity_test, dim=1, inputs=[])
878
+
879
+ @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
880
+ def partition_of_unity_test():
881
+ rng_state = wp.rand_init(4321, wp.tid())
882
+ coords = coord_sampler(rng_state)
883
+
884
+ # sum of node weights anywhere should be 1.0
885
+ w_sum = float(0.0)
886
+ for n in range(NODE_COUNT):
887
+ w_sum += weight_fn(coords, n)
888
+
889
+ wp.expect_near(w_sum, 1.0, 0.0001)
890
+
891
+ n_samples = 100
892
+ wp.launch(partition_of_unity_test, dim=n_samples, inputs=[])
893
+
894
+
895
+ def test_shape_function_trace(test, shape: shape.ShapeFunction, CENTER_COORDS):
896
+ NODE_COUNT = shape.NODES_PER_ELEMENT
897
+ node_coords_fn = shape.make_node_coords_in_element()
898
+
899
+ # Sum of node quadrature weights should be one (order 0)
900
+ # Sum of weighted quadrature coords should be element center (order 1)
901
+ trace_node_quadrature_weight_fn = shape.make_trace_node_quadrature_weight()
902
+
903
+ @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
904
+ def trace_node_quadrature_unity_test():
905
+ sum_node_qp = float(0.0)
906
+ sum_node_qp_coords = Coords(0.0)
907
+
908
+ for n in range(NODE_COUNT):
909
+ coords = node_coords_fn(n)
910
+
911
+ if wp.abs(coords[0]) < 1.0e-6:
912
+ w = trace_node_quadrature_weight_fn(n)
913
+ sum_node_qp += w
914
+ sum_node_qp_coords += w * node_coords_fn(n)
915
+
916
+ wp.expect_near(sum_node_qp, 1.0, 0.0001)
917
+ wp.expect_near(sum_node_qp_coords, CENTER_COORDS, 0.0001)
918
+
919
+ wp.launch(trace_node_quadrature_unity_test, dim=1, inputs=[])
920
+
921
+
922
+ def test_shape_function_gradient(test, shape: shape.ShapeFunction, coord_sampler, coord_delta_sampler):
923
+ weight_fn = shape.make_element_inner_weight()
924
+ weight_gradient_fn = shape.make_element_inner_weight_gradient()
925
+
926
+ @dynamic_kernel(suffix=shape.name, kernel_options={"enable_backward": False})
927
+ def finite_difference_test():
928
+ i, n = wp.tid()
929
+ rng_state = wp.rand_init(1234, i)
930
+
931
+ coords = coord_sampler(rng_state)
932
+
933
+ epsilon = 0.003
934
+ param_delta, coords_delta = coord_delta_sampler(epsilon, rng_state)
935
+
936
+ w_p = weight_fn(coords + coords_delta, n)
937
+ w_m = weight_fn(coords - coords_delta, n)
938
+
939
+ gp = weight_gradient_fn(coords + coords_delta, n)
940
+ gm = weight_gradient_fn(coords - coords_delta, n)
941
+
942
+ # 2nd-order finite-difference test
943
+ # See Schroeder 2019, Practical course on computing derivatives in code
944
+ delta_ref = w_p - w_m
945
+ delta_est = wp.dot(gp + gm, param_delta)
946
+
947
+ # wp.printf("%d %f %f \n", n, delta_ref, delta_est)
948
+ wp.expect_near(delta_ref, delta_est, 0.0001)
949
+
950
+ n_samples = 100
951
+ wp.launch(finite_difference_test, dim=(n_samples, shape.NODES_PER_ELEMENT), inputs=[])
952
+
953
+
954
+ def test_square_shape_functions(test, device):
955
+ SQUARE_CENTER_COORDS = wp.constant(Coords(0.5, 0.5, 0.0))
956
+ SQUARE_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.0))
957
+
958
+ @wp.func
959
+ def square_coord_sampler(state: wp.uint32):
960
+ return Coords(wp.randf(state), wp.randf(state), 0.0)
961
+
962
+ @wp.func
963
+ def square_coord_delta_sampler(epsilon: float, state: wp.uint32):
964
+ param_delta = wp.normalize(wp.vec2(wp.randf(state), wp.randf(state))) * epsilon
965
+ return param_delta, Coords(param_delta[0], param_delta[1], 0.0)
966
+
967
+ Q_1 = shape.SquareBipolynomialShapeFunctions(degree=1, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
968
+ Q_2 = shape.SquareBipolynomialShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
969
+ Q_3 = shape.SquareBipolynomialShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
970
+
971
+ test_shape_function_weight(test, Q_1, square_coord_sampler, SQUARE_CENTER_COORDS)
972
+ test_shape_function_weight(test, Q_2, square_coord_sampler, SQUARE_CENTER_COORDS)
973
+ test_shape_function_weight(test, Q_3, square_coord_sampler, SQUARE_CENTER_COORDS)
974
+ test_shape_function_trace(test, Q_1, SQUARE_SIDE_CENTER_COORDS)
975
+ test_shape_function_trace(test, Q_2, SQUARE_SIDE_CENTER_COORDS)
976
+ test_shape_function_trace(test, Q_3, SQUARE_SIDE_CENTER_COORDS)
977
+ test_shape_function_gradient(test, Q_1, square_coord_sampler, square_coord_delta_sampler)
978
+ test_shape_function_gradient(test, Q_2, square_coord_sampler, square_coord_delta_sampler)
979
+ test_shape_function_gradient(test, Q_3, square_coord_sampler, square_coord_delta_sampler)
980
+
981
+ Q_1 = shape.SquareBipolynomialShapeFunctions(degree=1, family=fem.Polynomial.GAUSS_LEGENDRE)
982
+ Q_2 = shape.SquareBipolynomialShapeFunctions(degree=2, family=fem.Polynomial.GAUSS_LEGENDRE)
983
+ Q_3 = shape.SquareBipolynomialShapeFunctions(degree=3, family=fem.Polynomial.GAUSS_LEGENDRE)
984
+
985
+ test_shape_function_weight(test, Q_1, square_coord_sampler, SQUARE_CENTER_COORDS)
986
+ test_shape_function_weight(test, Q_2, square_coord_sampler, SQUARE_CENTER_COORDS)
987
+ test_shape_function_weight(test, Q_3, square_coord_sampler, SQUARE_CENTER_COORDS)
988
+ test_shape_function_gradient(test, Q_1, square_coord_sampler, square_coord_delta_sampler)
989
+ test_shape_function_gradient(test, Q_2, square_coord_sampler, square_coord_delta_sampler)
990
+ test_shape_function_gradient(test, Q_3, square_coord_sampler, square_coord_delta_sampler)
991
+
992
+ S_2 = shape.SquareSerendipityShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
993
+ S_3 = shape.SquareSerendipityShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
994
+
995
+ test_shape_function_weight(test, S_2, square_coord_sampler, SQUARE_CENTER_COORDS)
996
+ test_shape_function_weight(test, S_3, square_coord_sampler, SQUARE_CENTER_COORDS)
997
+ test_shape_function_trace(test, S_2, SQUARE_SIDE_CENTER_COORDS)
998
+ test_shape_function_trace(test, S_3, SQUARE_SIDE_CENTER_COORDS)
999
+ test_shape_function_gradient(test, S_2, square_coord_sampler, square_coord_delta_sampler)
1000
+ test_shape_function_gradient(test, S_3, square_coord_sampler, square_coord_delta_sampler)
1001
+
1002
+ P_c1 = shape.SquareNonConformingPolynomialShapeFunctions(degree=1)
1003
+ P_c2 = shape.SquareNonConformingPolynomialShapeFunctions(degree=2)
1004
+ P_c3 = shape.SquareNonConformingPolynomialShapeFunctions(degree=3)
1005
+
1006
+ test_shape_function_weight(test, P_c1, square_coord_sampler, SQUARE_CENTER_COORDS)
1007
+ test_shape_function_weight(test, P_c2, square_coord_sampler, SQUARE_CENTER_COORDS)
1008
+ test_shape_function_weight(test, P_c3, square_coord_sampler, SQUARE_CENTER_COORDS)
1009
+ test_shape_function_gradient(test, P_c1, square_coord_sampler, square_coord_delta_sampler)
1010
+ test_shape_function_gradient(test, P_c2, square_coord_sampler, square_coord_delta_sampler)
1011
+ test_shape_function_gradient(test, P_c3, square_coord_sampler, square_coord_delta_sampler)
1012
+
1013
+ wp.synchronize()
1014
+
1015
+
1016
+ def test_cube_shape_functions(test, device):
1017
+ CUBE_CENTER_COORDS = wp.constant(Coords(0.5, 0.5, 0.5))
1018
+ CUBE_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.5))
1019
+
1020
+ @wp.func
1021
+ def cube_coord_sampler(state: wp.uint32):
1022
+ return Coords(wp.randf(state), wp.randf(state), wp.randf(state))
1023
+
1024
+ @wp.func
1025
+ def cube_coord_delta_sampler(epsilon: float, state: wp.uint32):
1026
+ param_delta = wp.normalize(wp.vec3(wp.randf(state), wp.randf(state), wp.randf(state))) * epsilon
1027
+ return param_delta, param_delta
1028
+
1029
+ Q_1 = shape.CubeTripolynomialShapeFunctions(degree=1, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1030
+ Q_2 = shape.CubeTripolynomialShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1031
+ Q_3 = shape.CubeTripolynomialShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1032
+
1033
+ test_shape_function_weight(test, Q_1, cube_coord_sampler, CUBE_CENTER_COORDS)
1034
+ test_shape_function_weight(test, Q_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1035
+ test_shape_function_weight(test, Q_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1036
+ test_shape_function_trace(test, Q_1, CUBE_SIDE_CENTER_COORDS)
1037
+ test_shape_function_trace(test, Q_2, CUBE_SIDE_CENTER_COORDS)
1038
+ test_shape_function_trace(test, Q_3, CUBE_SIDE_CENTER_COORDS)
1039
+ test_shape_function_gradient(test, Q_1, cube_coord_sampler, cube_coord_delta_sampler)
1040
+ test_shape_function_gradient(test, Q_2, cube_coord_sampler, cube_coord_delta_sampler)
1041
+ test_shape_function_gradient(test, Q_3, cube_coord_sampler, cube_coord_delta_sampler)
1042
+
1043
+ Q_1 = shape.CubeTripolynomialShapeFunctions(degree=1, family=fem.Polynomial.GAUSS_LEGENDRE)
1044
+ Q_2 = shape.CubeTripolynomialShapeFunctions(degree=2, family=fem.Polynomial.GAUSS_LEGENDRE)
1045
+ Q_3 = shape.CubeTripolynomialShapeFunctions(degree=3, family=fem.Polynomial.GAUSS_LEGENDRE)
1046
+
1047
+ test_shape_function_weight(test, Q_1, cube_coord_sampler, CUBE_CENTER_COORDS)
1048
+ test_shape_function_weight(test, Q_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1049
+ test_shape_function_weight(test, Q_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1050
+ test_shape_function_gradient(test, Q_1, cube_coord_sampler, cube_coord_delta_sampler)
1051
+ test_shape_function_gradient(test, Q_2, cube_coord_sampler, cube_coord_delta_sampler)
1052
+ test_shape_function_gradient(test, Q_3, cube_coord_sampler, cube_coord_delta_sampler)
1053
+
1054
+ S_2 = shape.CubeSerendipityShapeFunctions(degree=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1055
+ S_3 = shape.CubeSerendipityShapeFunctions(degree=3, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1056
+
1057
+ test_shape_function_weight(test, S_2, cube_coord_sampler, CUBE_CENTER_COORDS)
1058
+ test_shape_function_weight(test, S_3, cube_coord_sampler, CUBE_CENTER_COORDS)
1059
+ test_shape_function_trace(test, S_2, CUBE_SIDE_CENTER_COORDS)
1060
+ test_shape_function_trace(test, S_3, CUBE_SIDE_CENTER_COORDS)
1061
+ test_shape_function_gradient(test, S_2, cube_coord_sampler, cube_coord_delta_sampler)
1062
+ test_shape_function_gradient(test, S_3, cube_coord_sampler, cube_coord_delta_sampler)
1063
+
1064
+ P_c1 = shape.CubeNonConformingPolynomialShapeFunctions(degree=1)
1065
+ P_c2 = shape.CubeNonConformingPolynomialShapeFunctions(degree=2)
1066
+ P_c3 = shape.CubeNonConformingPolynomialShapeFunctions(degree=3)
1067
+
1068
+ test_shape_function_weight(test, P_c1, cube_coord_sampler, CUBE_CENTER_COORDS)
1069
+ test_shape_function_weight(test, P_c2, cube_coord_sampler, CUBE_CENTER_COORDS)
1070
+ test_shape_function_weight(test, P_c3, cube_coord_sampler, CUBE_CENTER_COORDS)
1071
+ test_shape_function_gradient(test, P_c1, cube_coord_sampler, cube_coord_delta_sampler)
1072
+ test_shape_function_gradient(test, P_c2, cube_coord_sampler, cube_coord_delta_sampler)
1073
+ test_shape_function_gradient(test, P_c3, cube_coord_sampler, cube_coord_delta_sampler)
1074
+
1075
+ wp.synchronize()
1076
+
1077
+
1078
+ def test_tri_shape_functions(test, device):
1079
+ TRI_CENTER_COORDS = wp.constant(Coords(1 / 3.0, 1 / 3.0, 1 / 3.0))
1080
+ TRI_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 0.5, 0.5))
1081
+
1082
+ @wp.func
1083
+ def tri_coord_sampler(state: wp.uint32):
1084
+ a = wp.randf(state)
1085
+ b = wp.randf(state)
1086
+ return Coords(1.0 - a - b, a, b)
1087
+
1088
+ @wp.func
1089
+ def tri_coord_delta_sampler(epsilon: float, state: wp.uint32):
1090
+ param_delta = wp.normalize(wp.vec2(wp.randf(state), wp.randf(state))) * epsilon
1091
+ a = param_delta[0]
1092
+ b = param_delta[1]
1093
+ return param_delta, Coords(-a - b, a, b)
1094
+
1095
+ P_1 = shape.Triangle2DPolynomialShapeFunctions(degree=1)
1096
+ P_2 = shape.Triangle2DPolynomialShapeFunctions(degree=2)
1097
+ P_3 = shape.Triangle2DPolynomialShapeFunctions(degree=3)
1098
+
1099
+ test_shape_function_weight(test, P_1, tri_coord_sampler, TRI_CENTER_COORDS)
1100
+ test_shape_function_weight(test, P_2, tri_coord_sampler, TRI_CENTER_COORDS)
1101
+ test_shape_function_weight(test, P_3, tri_coord_sampler, TRI_CENTER_COORDS)
1102
+ test_shape_function_trace(test, P_1, TRI_SIDE_CENTER_COORDS)
1103
+ test_shape_function_trace(test, P_2, TRI_SIDE_CENTER_COORDS)
1104
+ test_shape_function_trace(test, P_3, TRI_SIDE_CENTER_COORDS)
1105
+ test_shape_function_gradient(test, P_1, tri_coord_sampler, tri_coord_delta_sampler)
1106
+ test_shape_function_gradient(test, P_2, tri_coord_sampler, tri_coord_delta_sampler)
1107
+ test_shape_function_gradient(test, P_3, tri_coord_sampler, tri_coord_delta_sampler)
1108
+
1109
+ P_1d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=1)
1110
+ P_2d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=2)
1111
+ P_3d = shape.Triangle2DNonConformingPolynomialShapeFunctions(degree=3)
1112
+
1113
+ test_shape_function_weight(test, P_1d, tri_coord_sampler, TRI_CENTER_COORDS)
1114
+ test_shape_function_weight(test, P_2d, tri_coord_sampler, TRI_CENTER_COORDS)
1115
+ test_shape_function_weight(test, P_3d, tri_coord_sampler, TRI_CENTER_COORDS)
1116
+ test_shape_function_gradient(test, P_1d, tri_coord_sampler, tri_coord_delta_sampler)
1117
+ test_shape_function_gradient(test, P_2d, tri_coord_sampler, tri_coord_delta_sampler)
1118
+ test_shape_function_gradient(test, P_3d, tri_coord_sampler, tri_coord_delta_sampler)
1119
+
1120
+ wp.synchronize()
1121
+
1122
+
1123
+ def test_tet_shape_functions(test, device):
1124
+ TET_CENTER_COORDS = wp.constant(Coords(1 / 4.0, 1 / 4.0, 1 / 4.0))
1125
+ TET_SIDE_CENTER_COORDS = wp.constant(Coords(0.0, 1.0 / 3.0, 1.0 / 3.0))
1126
+
1127
+ @wp.func
1128
+ def tet_coord_sampler(state: wp.uint32):
1129
+ return Coords(wp.randf(state), wp.randf(state), wp.randf(state))
1130
+
1131
+ @wp.func
1132
+ def tet_coord_delta_sampler(epsilon: float, state: wp.uint32):
1133
+ param_delta = wp.normalize(wp.vec3(wp.randf(state), wp.randf(state), wp.randf(state))) * epsilon
1134
+ return param_delta, param_delta
1135
+
1136
+ P_1 = shape.TetrahedronPolynomialShapeFunctions(degree=1)
1137
+ P_2 = shape.TetrahedronPolynomialShapeFunctions(degree=2)
1138
+ P_3 = shape.TetrahedronPolynomialShapeFunctions(degree=3)
1139
+
1140
+ test_shape_function_weight(test, P_1, tet_coord_sampler, TET_CENTER_COORDS)
1141
+ test_shape_function_weight(test, P_2, tet_coord_sampler, TET_CENTER_COORDS)
1142
+ test_shape_function_weight(test, P_3, tet_coord_sampler, TET_CENTER_COORDS)
1143
+ test_shape_function_trace(test, P_1, TET_SIDE_CENTER_COORDS)
1144
+ test_shape_function_trace(test, P_2, TET_SIDE_CENTER_COORDS)
1145
+ test_shape_function_trace(test, P_3, TET_SIDE_CENTER_COORDS)
1146
+ test_shape_function_gradient(test, P_1, tet_coord_sampler, tet_coord_delta_sampler)
1147
+ test_shape_function_gradient(test, P_2, tet_coord_sampler, tet_coord_delta_sampler)
1148
+ test_shape_function_gradient(test, P_3, tet_coord_sampler, tet_coord_delta_sampler)
1149
+
1150
+ P_1d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=1)
1151
+ P_2d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=2)
1152
+ P_3d = shape.TetrahedronNonConformingPolynomialShapeFunctions(degree=3)
1153
+
1154
+ test_shape_function_weight(test, P_1d, tet_coord_sampler, TET_CENTER_COORDS)
1155
+ test_shape_function_weight(test, P_2d, tet_coord_sampler, TET_CENTER_COORDS)
1156
+ test_shape_function_weight(test, P_3d, tet_coord_sampler, TET_CENTER_COORDS)
1157
+ test_shape_function_gradient(test, P_1d, tet_coord_sampler, tet_coord_delta_sampler)
1158
+ test_shape_function_gradient(test, P_2d, tet_coord_sampler, tet_coord_delta_sampler)
1159
+ test_shape_function_gradient(test, P_3d, tet_coord_sampler, tet_coord_delta_sampler)
1160
+
1161
+ wp.synchronize()
1162
+
1163
+
1164
+ def test_point_basis(test, device):
1165
+ geo = fem.Grid2D(res=wp.vec2i(2))
1166
+
1167
+ domain = fem.Cells(geo)
1168
+
1169
+ quadrature = fem.RegularQuadrature(domain, order=2, family=fem.Polynomial.GAUSS_LEGENDRE)
1170
+ point_basis = fem.PointBasisSpace(quadrature)
1171
+
1172
+ point_space = fem.make_collocated_function_space(point_basis)
1173
+ point_test = fem.make_test(point_space, domain=domain)
1174
+
1175
+ # Sample at particle positions
1176
+ ones = fem.integrate(linear_form, fields={"u": point_test}, nodal=True)
1177
+ test.assertAlmostEqual(np.sum(ones.numpy()), 1.0, places=5)
1178
+
1179
+ # Sampling outside of particle positions
1180
+ other_quadrature = fem.RegularQuadrature(domain, order=2, family=fem.Polynomial.LOBATTO_GAUSS_LEGENDRE)
1181
+ zeros = fem.integrate(linear_form, quadrature=other_quadrature, fields={"u": point_test})
1182
+
1183
+ test.assertAlmostEqual(np.sum(zeros.numpy()), 0.0, places=5)
1184
+
1185
+
1186
+ @fem.integrand
1187
+ def _bicubic(s: Sample, domain: Domain):
1188
+ x = domain(s)
1189
+ return wp.pow(x[0], 3.0) * wp.pow(x[1], 3.0)
1190
+
1191
+
1192
+ @fem.integrand
1193
+ def _piecewise_constant(s: Sample):
1194
+ return float(s.element_index)
1195
+
1196
+
1197
+ def test_particle_quadratures(test, device):
1198
+ geo = fem.Grid2D(res=wp.vec2i(2))
1199
+
1200
+ domain = fem.Cells(geo)
1201
+ points, weights = domain.reference_element().instantiate_quadrature(order=4, family=fem.Polynomial.GAUSS_LEGENDRE)
1202
+ points_per_cell = len(points)
1203
+
1204
+ points = points * domain.element_count()
1205
+ weights = weights * domain.element_count()
1206
+
1207
+ points = wp.array(points, shape=(domain.element_count(), points_per_cell), dtype=Coords, device=device)
1208
+ weights = wp.array(weights, shape=(domain.element_count(), points_per_cell), dtype=float, device=device)
1209
+
1210
+ explicit_quadrature = fem.ExplicitQuadrature(domain, points, weights)
1211
+
1212
+ test.assertEqual(explicit_quadrature.points_per_element(), points_per_cell)
1213
+ test.assertEqual(explicit_quadrature.total_point_count(), points_per_cell * geo.cell_count())
1214
+
1215
+ val = fem.integrate(_bicubic, quadrature=explicit_quadrature)
1216
+ test.assertAlmostEqual(val, 1.0 / 16, places=5)
1217
+
1218
+ element_indices = wp.array([3, 3, 2], dtype=int, device=device)
1219
+ element_coords = wp.array(
1220
+ [
1221
+ [0.25, 0.5, 0.0],
1222
+ [0.5, 0.25, 0.0],
1223
+ [0.5, 0.5, 0.0],
1224
+ ],
1225
+ dtype=Coords,
1226
+ device=device,
1227
+ )
1228
+
1229
+ pic_quadrature = fem.PicQuadrature(domain, positions=(element_indices, element_coords))
1230
+
1231
+ test.assertIsNone(pic_quadrature.points_per_element())
1232
+ test.assertEqual(pic_quadrature.total_point_count(), 3)
1233
+ test.assertEqual(pic_quadrature.active_cell_count(), 2)
1234
+
1235
+ val = fem.integrate(_piecewise_constant, quadrature=pic_quadrature)
1236
+ test.assertAlmostEqual(val, 1.25, places=5)
1237
+
1238
+
1239
+ devices = get_test_devices()
1240
+
1241
+
1242
+ class TestFem(unittest.TestCase):
1243
+ pass
1244
+
1245
+
1246
+ add_function_test(TestFem, "test_regular_quadrature", test_regular_quadrature)
1247
+ add_function_test(TestFem, "test_closest_point_queries", test_closest_point_queries)
1248
+ add_function_test(TestFem, "test_grad_decomposition", test_grad_decomposition, devices=devices)
1249
+ add_function_test(TestFem, "test_integrate_gradient", test_integrate_gradient, devices=devices)
1250
+ add_function_test(TestFem, "test_interpolate_gradient", test_interpolate_gradient, devices=devices)
1251
+ add_function_test(TestFem, "test_vector_divergence_theorem", test_vector_divergence_theorem, devices=devices)
1252
+ add_function_test(TestFem, "test_tensor_divergence_theorem", test_tensor_divergence_theorem, devices=devices)
1253
+ add_function_test(TestFem, "test_grid_2d", test_grid_2d, devices=devices)
1254
+ add_function_test(TestFem, "test_triangle_mesh", test_triangle_mesh, devices=devices)
1255
+ add_function_test(TestFem, "test_quad_mesh", test_quad_mesh, devices=devices)
1256
+ add_function_test(TestFem, "test_grid_3d", test_grid_3d, devices=devices)
1257
+ add_function_test(TestFem, "test_tet_mesh", test_tet_mesh, devices=devices)
1258
+ add_function_test(TestFem, "test_hex_mesh", test_hex_mesh, devices=devices)
1259
+ add_function_test(TestFem, "test_deformed_geometry", test_deformed_geometry, devices=devices)
1260
+ add_function_test(TestFem, "test_dof_mapper", test_dof_mapper)
1261
+ add_function_test(TestFem, "test_point_basis", test_point_basis)
1262
+ add_function_test(TestFem, "test_particle_quadratures", test_particle_quadratures)
1263
+
1264
+
1265
+ class TestFemShapeFunctions(unittest.TestCase):
1266
+ pass
1267
+
1268
+
1269
+ add_function_test(TestFemShapeFunctions, "test_square_shape_functions", test_square_shape_functions)
1270
+ add_function_test(TestFemShapeFunctions, "test_cube_shape_functions", test_cube_shape_functions)
1271
+ add_function_test(TestFemShapeFunctions, "test_tri_shape_functions", test_tri_shape_functions)
1272
+ add_function_test(TestFemShapeFunctions, "test_tet_shape_functions", test_tet_shape_functions)
1273
+
1274
+
1275
+ if __name__ == "__main__":
1276
+ wp.build.clear_kernel_cache()
1277
+ unittest.main(verbosity=2)