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

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

Potentially problematic release.


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

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