|
1 | 1 | import pytest |
2 | 2 | import numpy as np |
| 3 | +import numpy.testing as npt |
3 | 4 | import unittest |
4 | 5 | import discretize |
5 | 6 | from scipy.sparse.linalg import spsolve |
@@ -321,5 +322,70 @@ def test_orderBackward(self): |
321 | 322 | self.orderTest() |
322 | 323 |
|
323 | 324 |
|
| 325 | +@pytest.fixture(params=[1, 2, 3], ids=["dims-1", "dims-2", "dims-3"]) |
| 326 | +def random_tensor_mesh(request): |
| 327 | + dim = request.param |
| 328 | + rng = np.random.default_rng(440122) |
| 329 | + shape = rng.integers(5, 10, dim) |
| 330 | + cell_widths = [rng.uniform(3.0, 872634.321, n) for n in shape] |
| 331 | + origin = rng.uniform(-101.031, 33.2, dim) |
| 332 | + |
| 333 | + return discretize.TensorMesh(cell_widths, origin) |
| 334 | + |
| 335 | + |
| 336 | +def test_tensor_point2index_inside_points(random_tensor_mesh): |
| 337 | + mesh = random_tensor_mesh |
| 338 | + dim = mesh.dim |
| 339 | + m_origin = mesh.origin |
| 340 | + m_extent = np.atleast_1d(np.max(mesh.nodes, axis=0)) |
| 341 | + |
| 342 | + nd = 15 |
| 343 | + points = np.stack(np.meshgrid(*np.linspace(m_origin, m_extent, nd).T), axis=-1) |
| 344 | + points = points.reshape((-1, dim)) |
| 345 | + |
| 346 | + npt.assert_array_equal(mesh.is_inside(points), True) |
| 347 | + |
| 348 | + cell_inds = mesh.point2index(points) |
| 349 | + for icell, p in zip(cell_inds, points): |
| 350 | + cell = mesh[icell] |
| 351 | + c_origin, c_extent = cell.bounds.reshape((dim, 2)).T |
| 352 | + dim_test = (p >= c_origin) & (p <= c_extent) |
| 353 | + npt.assert_equal(dim_test, True) |
| 354 | + |
| 355 | + |
| 356 | +def test_tensor_point2index_outside_points(random_tensor_mesh): |
| 357 | + mesh = random_tensor_mesh |
| 358 | + dim = mesh.dim |
| 359 | + m_origin = mesh.origin |
| 360 | + m_extent = np.atleast_1d(np.max(mesh.nodes, axis=0)) |
| 361 | + m_width = m_extent - m_origin |
| 362 | + |
| 363 | + nd = 15 |
| 364 | + points = np.stack( |
| 365 | + np.meshgrid(*np.linspace(m_origin - m_width * 2, m_extent + m_width * 2, nd).T), |
| 366 | + axis=-1, |
| 367 | + ) |
| 368 | + points = points.reshape((-1, dim)) |
| 369 | + outside_points = points[~mesh.is_inside(points)] |
| 370 | + |
| 371 | + npt.assert_array_equal(mesh.is_inside(outside_points), False) |
| 372 | + |
| 373 | + # manually check each point that is outside |
| 374 | + cell_inds = mesh.point2index(outside_points) |
| 375 | + for icell, p in zip(cell_inds, outside_points): |
| 376 | + cell = mesh[icell] |
| 377 | + c_origin, c_extent = cell.bounds.reshape((dim, 2)).T |
| 378 | + dim_test = np.zeros(dim, bool) |
| 379 | + for i in range(dim): |
| 380 | + p_d = p[i] |
| 381 | + if p_d < m_origin[i]: |
| 382 | + dim_test[i] = p_d < c_origin[i] |
| 383 | + elif p_d > m_extent[i]: |
| 384 | + dim_test[i] = p_d > c_extent[i] |
| 385 | + else: |
| 386 | + dim_test[i] = p_d >= c_origin[i] and p_d <= c_extent[i] |
| 387 | + npt.assert_equal(dim_test, True) |
| 388 | + |
| 389 | + |
324 | 390 | if __name__ == "__main__": |
325 | 391 | unittest.main() |
0 commit comments