|
| 1 | +import re |
1 | 2 | import discretize |
2 | 3 | import numpy as np |
| 4 | +import numpy.testing as npt |
3 | 5 | import pytest |
4 | 6 | from discretize.tests import assert_cell_intersects_geometric |
5 | 7 |
|
@@ -490,3 +492,149 @@ def test_refine_plane3D(): |
490 | 492 | mesh2.refine_triangle(tris, -1) |
491 | 493 |
|
492 | 494 | assert mesh1.equals(mesh2) |
| 495 | + |
| 496 | + |
| 497 | +def _make_quadrant_model(mesh, order): |
| 498 | + shape_cells = mesh.shape_cells |
| 499 | + model = np.zeros(shape_cells, order="F" if order == "flat" else order) |
| 500 | + if mesh.dim == 2: |
| 501 | + model[: shape_cells[0] // 2, : shape_cells[1] // 2] = 1.0 |
| 502 | + model[: shape_cells[0] // 4, : shape_cells[1] // 4] = 0.5 |
| 503 | + else: |
| 504 | + model[: shape_cells[0] // 2, : shape_cells[1] // 2, : shape_cells[2] // 2] = 1.0 |
| 505 | + model[: shape_cells[0] // 4, : shape_cells[1] // 4, : shape_cells[2] // 4] = 0.5 |
| 506 | + if order == "flat": |
| 507 | + model = model.reshape(-1, order="F") |
| 508 | + return model |
| 509 | + |
| 510 | + |
| 511 | +@pytest.mark.parametrize( |
| 512 | + "tens_inp", |
| 513 | + [ |
| 514 | + dict(h=[16, 16]), |
| 515 | + dict(h=[16, 32]), |
| 516 | + dict(h=[32, 16]), |
| 517 | + dict(h=[16, 16, 16]), |
| 518 | + dict(h=[16, 16, 8]), |
| 519 | + dict(h=[16, 8, 16]), |
| 520 | + dict(h=[8, 16, 16]), |
| 521 | + dict(h=[8, 8, 16]), |
| 522 | + dict(h=[8, 16, 8]), |
| 523 | + dict(h=[16, 8, 8]), |
| 524 | + ], |
| 525 | + ids=[ |
| 526 | + "16x16", |
| 527 | + "16x32", |
| 528 | + "32x16", |
| 529 | + "16x16x16", |
| 530 | + "16x16x8", |
| 531 | + "16x8x16", |
| 532 | + "8x16x16", |
| 533 | + "8x8x16", |
| 534 | + "8x16x8", |
| 535 | + "16x8x8", |
| 536 | + ], |
| 537 | +) |
| 538 | +def test_refine_image_input_ordering(tens_inp): |
| 539 | + base_mesh = discretize.TensorMesh(**tens_inp) |
| 540 | + model_0 = _make_quadrant_model(base_mesh, order="flat") |
| 541 | + model_1 = _make_quadrant_model(base_mesh, order="C") |
| 542 | + model_2 = _make_quadrant_model(base_mesh, order="F") |
| 543 | + |
| 544 | + tree0 = discretize.TreeMesh(base_mesh.h, base_mesh.origin) |
| 545 | + tree0.refine_image(model_0) |
| 546 | + |
| 547 | + tree1 = discretize.TreeMesh(base_mesh.h, base_mesh.origin) |
| 548 | + tree1.refine_image(model_1) |
| 549 | + |
| 550 | + tree2 = discretize.TreeMesh(base_mesh.h, base_mesh.origin) |
| 551 | + tree2.refine_image(model_2) |
| 552 | + |
| 553 | + assert tree0.n_cells == tree1.n_cells == tree2.n_cells |
| 554 | + |
| 555 | + for cell0, cell1, cell2 in zip(tree0, tree1, tree2): |
| 556 | + assert cell0.nodes == cell1.nodes == cell2.nodes |
| 557 | + |
| 558 | + |
| 559 | +@pytest.mark.parametrize( |
| 560 | + "tens_inp", |
| 561 | + [ |
| 562 | + dict(h=[16, 16]), |
| 563 | + dict(h=[16, 32]), |
| 564 | + dict(h=[32, 16]), |
| 565 | + dict(h=[16, 16, 16]), |
| 566 | + dict(h=[16, 16, 8]), |
| 567 | + dict(h=[16, 8, 16]), |
| 568 | + dict(h=[8, 16, 16]), |
| 569 | + dict(h=[8, 8, 16]), |
| 570 | + dict(h=[8, 16, 8]), |
| 571 | + dict(h=[16, 8, 8]), |
| 572 | + ], |
| 573 | + ids=[ |
| 574 | + "16x16", |
| 575 | + "16x32", |
| 576 | + "32x16", |
| 577 | + "16x16x16", |
| 578 | + "16x16x8", |
| 579 | + "16x8x16", |
| 580 | + "8x16x16", |
| 581 | + "8x8x16", |
| 582 | + "8x16x8", |
| 583 | + "16x8x8", |
| 584 | + ], |
| 585 | +) |
| 586 | +@pytest.mark.parametrize( |
| 587 | + "model_func", |
| 588 | + [ |
| 589 | + lambda mesh: np.zeros(mesh.n_cells), |
| 590 | + lambda mesh: np.arange(mesh.n_cells, dtype=float), |
| 591 | + lambda mesh: _make_quadrant_model(mesh, order="flat"), |
| 592 | + ], |
| 593 | + ids=["constant", "full", "quadrant"], |
| 594 | +) |
| 595 | +def test_refine_image(tens_inp, model_func): |
| 596 | + base_mesh = discretize.TensorMesh(**tens_inp) |
| 597 | + model = model_func(base_mesh) |
| 598 | + mesh = discretize.TreeMesh(base_mesh.h, base_mesh.origin, diagonal_balance=False) |
| 599 | + mesh.refine_image(model) |
| 600 | + |
| 601 | + # for every cell in the tree mesh, all aligned cells in the tensor mesh |
| 602 | + # should have a single unique value. |
| 603 | + # quickest way is to generate a volume interp operator and look at indices in the |
| 604 | + # csr matrix |
| 605 | + interp_mat = discretize.utils.volume_average(base_mesh, mesh) |
| 606 | + |
| 607 | + # ensure in canonical form: |
| 608 | + interp_mat.sum_duplicates() |
| 609 | + interp_mat.sort_indices() |
| 610 | + assert interp_mat.has_canonical_format |
| 611 | + |
| 612 | + model = model.reshape(-1, order="F") |
| 613 | + for row in interp_mat: |
| 614 | + vals = model[row.indices] |
| 615 | + npt.assert_equal(vals, vals[0]) |
| 616 | + |
| 617 | + |
| 618 | +def test_refine_image_bad_size(): |
| 619 | + mesh = discretize.TreeMesh([32, 32]) |
| 620 | + model = np.zeros(32 * 32 + 1) |
| 621 | + base_cells = np.prod(mesh.shape_cells) |
| 622 | + with pytest.raises( |
| 623 | + ValueError, |
| 624 | + match=re.escape( |
| 625 | + f"image array size: {len(model)} must match the total number of cells in the base tensor mesh: {base_cells}" |
| 626 | + ), |
| 627 | + ): |
| 628 | + mesh.refine_image(model) |
| 629 | + |
| 630 | + |
| 631 | +def test_refine_image_bad_shape(): |
| 632 | + mesh = discretize.TreeMesh([32, 32]) |
| 633 | + model = np.zeros((16, 64)) |
| 634 | + with pytest.raises( |
| 635 | + ValueError, |
| 636 | + match=re.escape( |
| 637 | + f"image array shape: {model.shape} must match the base cell shapes: {mesh.shape_cells}" |
| 638 | + ), |
| 639 | + ): |
| 640 | + mesh.refine_image(model) |
0 commit comments