|
| 1 | +# pylint:disable=missing-module-docstring,missing-function-docstring,duplicate-code |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +from PyMPDATA import Options, ScalarField, VectorField |
| 6 | +from PyMPDATA.boundary_conditions import Periodic |
| 7 | +from PyMPDATA.impl.enumerations import MAX_DIM_NUM |
| 8 | +from PyMPDATA.impl.traversals import Traversals |
| 9 | + |
| 10 | +JIT_FLAGS = Options().jit_flags |
| 11 | + |
| 12 | + |
| 13 | +def assert_slice_size(used_slice: slice, halo): |
| 14 | + if used_slice.stop is None: |
| 15 | + if abs(used_slice.start) != halo and abs(used_slice.start) != halo - 1: |
| 16 | + raise AssertionError("Slice and halo size mismatch") |
| 17 | + elif used_slice.stop is not None: |
| 18 | + if ( |
| 19 | + abs(used_slice.stop - used_slice.start) != halo |
| 20 | + and abs(used_slice.stop - used_slice.start) != halo - 1 |
| 21 | + ): |
| 22 | + raise AssertionError("Slice and halo size mismatch") |
| 23 | + else: |
| 24 | + assert False |
| 25 | + |
| 26 | + |
| 27 | +def assert_array_not_equal(arr_a, arr_b): |
| 28 | + return np.testing.assert_raises( |
| 29 | + AssertionError, np.testing.assert_array_equal, arr_a, arr_b |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("boundary_condition", (Periodic(),)) |
| 34 | +@pytest.mark.parametrize("n_threads", (1, 2)) |
| 35 | +@pytest.mark.parametrize("halo", (1, 2, 3)) |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "field_factory", |
| 38 | + ( |
| 39 | + lambda halo, boundary_condition: ScalarField( |
| 40 | + np.zeros(3), halo, boundary_condition |
| 41 | + ), # 1d |
| 42 | + lambda halo, boundary_condition: VectorField( |
| 43 | + (np.zeros(3),), halo, boundary_condition |
| 44 | + ), # 1d |
| 45 | + lambda halo, boundary_condition: ScalarField( |
| 46 | + np.zeros((3, 3)), halo, boundary_condition |
| 47 | + ), # 2d |
| 48 | + lambda halo, boundary_condition: VectorField( |
| 49 | + ( |
| 50 | + np.zeros( |
| 51 | + (4, 3), |
| 52 | + ), |
| 53 | + np.zeros( |
| 54 | + (3, 4), |
| 55 | + ), |
| 56 | + ), |
| 57 | + halo, |
| 58 | + boundary_condition, |
| 59 | + ), # 2d |
| 60 | + ), |
| 61 | +) |
| 62 | +def test_explicit_fill_halos(field_factory, halo, boundary_condition, n_threads): |
| 63 | + # arange |
| 64 | + field = field_factory(halo, (boundary_condition, boundary_condition)) |
| 65 | + if len(field.grid) == 1 and n_threads > 1: |
| 66 | + pytest.skip("Skip 1D tests with n_threads > 1") |
| 67 | + traversals = Traversals( |
| 68 | + grid=field.grid, |
| 69 | + halo=halo, |
| 70 | + jit_flags=JIT_FLAGS, |
| 71 | + n_threads=n_threads, |
| 72 | + left_first=tuple([True] * MAX_DIM_NUM), |
| 73 | + buffer_size=0, |
| 74 | + ) |
| 75 | + field.assemble(traversals) |
| 76 | + if isinstance(field, ScalarField): |
| 77 | + field.get()[:] = np.arange(1, field.grid[0] + 1) |
| 78 | + left_halo = slice(0, halo) |
| 79 | + right_halo = slice(-halo, None) |
| 80 | + left_edge = slice(halo, 2 * halo) |
| 81 | + right_edge = slice(-2 * halo, -halo) |
| 82 | + slices = [left_halo, right_halo, left_edge, right_edge] |
| 83 | + for slice_to_check in slices: |
| 84 | + assert_slice_size(slice_to_check, halo) |
| 85 | + data = field.data |
| 86 | + elif isinstance(field, VectorField): |
| 87 | + if field.get_component(0)[:].ndim > 1: |
| 88 | + field.get_component(0)[0][:] = np.arange(1, field.grid[0] + 1) |
| 89 | + field.get_component(0)[1][:] = np.arange(1, field.grid[0] + 1) |
| 90 | + else: |
| 91 | + field.get_component(0)[:] = np.arange(1, field.grid[0] + 2) |
| 92 | + if halo == 1: |
| 93 | + pytest.skip("Skip VectorField test if halo == 1") |
| 94 | + left_halo = slice(0, halo - 1) |
| 95 | + right_halo = slice(-(halo - 1), None) |
| 96 | + left_edge = slice(halo, 2 * (halo - 1) + 1) |
| 97 | + right_edge = slice(-2 * (halo - 1) - 1, -(halo - 1) - 1) |
| 98 | + slices = [left_halo, right_halo, left_edge, right_edge] |
| 99 | + for slice_to_check in slices: |
| 100 | + assert_slice_size(slice_to_check, halo) |
| 101 | + data = field.data[0] |
| 102 | + else: |
| 103 | + assert False |
| 104 | + assert_array_not_equal(data[left_halo], data[right_edge]) |
| 105 | + assert_array_not_equal(data[right_halo], data[left_edge]) |
| 106 | + |
| 107 | + # act |
| 108 | + # pylint:disable=protected-access |
| 109 | + field._debug_fill_halos(traversals, range(n_threads)) |
| 110 | + |
| 111 | + # assert |
| 112 | + np.testing.assert_array_equal(data[left_halo], data[right_edge], verbose=True) |
| 113 | + np.testing.assert_array_equal(data[right_halo], data[left_edge], verbose=True) |
0 commit comments