コード例 #1
0
def _mesh_to_MeshInfo(mesh, location):
    # Returns a MeshInfo object describing the mesh of the cube.
    assert mesh.topology_dimension == 2
    if None in mesh.face_coords:
        elem_coords = None
    else:
        elem_coords = np.stack([coord.points for coord in mesh.face_coords], axis=-1)
    meshinfo = MeshInfo(
        np.stack([coord.points for coord in mesh.node_coords], axis=-1),
        mesh.face_node_connectivity.indices_by_location(),
        mesh.face_node_connectivity.start_index,
        elem_coords=elem_coords,
        location=location,
    )
    return meshinfo
コード例 #2
0
def test_expanded_lons_with_mesh():
    """
    Basic test for regridding with :meth:`~esmf_regrid.esmf_regridder.RefinedGridInfo.make_esmf_field`.

    Mirrors the tests in :func:`~esmf_regrid.tests.unit.experimental.unstructured_regrid.test_MeshInfo.test_regrid_with_mesh`
    but with slightly different expected values due to increased accuracy.
    """
    mesh_args = _make_small_mesh_args()
    mesh = MeshInfo(*mesh_args)

    grid_args = make_grid_args(2, 3)
    grid = RefinedGridInfo(*grid_args[2:4], resolution=4)

    mesh_to_grid_regridder = Regridder(mesh, grid)
    mesh_input = np.array([3, 2])
    grid_output = mesh_to_grid_regridder.regrid(mesh_input)
    expected_grid_output = np.array([
        [2.671534474734418, 3.0],
        [2.088765949748455, 2.922517356506756],
        [2.0, 2.340882413622917],
    ])
    assert ma.allclose(expected_grid_output, grid_output)

    grid_to_mesh_regridder = Regridder(grid, mesh)
    grid_input = np.array([[0, 0], [1, 0], [2, 1]])
    mesh_output = grid_to_mesh_regridder.regrid(grid_input)
    expected_mesh_output = np.array([0.14117205318254747, 1.1976140197893996])
    assert ma.allclose(expected_mesh_output, mesh_output)

    def _give_extra_dims(array):
        result = np.stack([array, array + 1])
        result = np.stack([result, result + 10, result + 100])
        return result

    extra_dim_mesh_input = _give_extra_dims(mesh_input)
    extra_dim_grid_output = mesh_to_grid_regridder.regrid(extra_dim_mesh_input)
    extra_dim_expected_grid_output = _give_extra_dims(expected_grid_output)
    assert ma.allclose(extra_dim_expected_grid_output, extra_dim_grid_output)

    extra_dim_grid_input = _give_extra_dims(grid_input)
    extra_dim_mesh_output = grid_to_mesh_regridder.regrid(extra_dim_grid_input)
    extra_dim_expected_mesh_output = _give_extra_dims(expected_mesh_output)
    assert ma.allclose(extra_dim_expected_mesh_output, extra_dim_mesh_output)
コード例 #3
0
def test_regrid_with_mesh():
    """Basic test for regridding with :meth:`~esmf_regrid.esmf_regridder.GridInfo.make_esmf_field`."""
    mesh_args = _make_small_mesh_args()
    mesh = MeshInfo(*mesh_args)

    grid_args = make_grid_args(2, 3)
    grid = GridInfo(*grid_args)

    mesh_to_grid_regridder = Regridder(mesh, grid)
    mesh_input = np.array([3, 2])
    grid_output = mesh_to_grid_regridder.regrid(mesh_input)
    expected_grid_output = np.array([
        [2.671294712940605, 3.0],
        [2.0885553467353097, 2.9222786250561574],
        [2.0, 2.3397940801753307],
    ])
    assert ma.allclose(expected_grid_output, grid_output)

    grid_to_mesh_regridder = Regridder(grid, mesh)
    grid_input = np.array([[0, 0], [1, 0], [2, 1]])
    mesh_output = grid_to_mesh_regridder.regrid(grid_input)
    expected_mesh_output = np.array([0.1408245341331448, 1.19732762534643])
    assert ma.allclose(expected_mesh_output, mesh_output)

    def _give_extra_dims(array):
        result = np.stack([array, array + 1])
        result = np.stack([result, result + 10, result + 100])
        return result

    extra_dim_mesh_input = _give_extra_dims(mesh_input)
    extra_dim_grid_output = mesh_to_grid_regridder.regrid(extra_dim_mesh_input)
    extra_dim_expected_grid_output = _give_extra_dims(expected_grid_output)
    assert ma.allclose(extra_dim_expected_grid_output, extra_dim_grid_output)

    extra_dim_grid_input = _give_extra_dims(grid_input)
    extra_dim_mesh_output = grid_to_mesh_regridder.regrid(extra_dim_grid_input)
    extra_dim_expected_mesh_output = _give_extra_dims(expected_mesh_output)
    assert ma.allclose(extra_dim_expected_mesh_output, extra_dim_mesh_output)
コード例 #4
0
def test_expanded_lats_with_mesh():
    """Basic test for regridding with :meth:`~esmf_regrid.esmf_regridder.RefinedGridInfo.make_esmf_field`."""
    mesh_args = _make_small_mesh_args()
    mesh = MeshInfo(*mesh_args)

    grid = RefinedGridInfo(np.array([0, 5, 10]),
                           np.array([-90, 90]),
                           resolution=4)

    mesh_to_grid_regridder = Regridder(mesh, grid)
    mesh_input = np.array([3, 2])
    grid_output = mesh_to_grid_regridder.regrid(mesh_input)
    expected_grid_output = np.array([
        [2.2024695514629724, 2.4336888097502642],
    ])
    assert ma.allclose(expected_grid_output, grid_output)

    grid_to_mesh_regridder = Regridder(grid, mesh)
    grid_input = np.array([[1, 2]])
    mesh_output = grid_to_mesh_regridder.regrid(grid_input)
    expected_mesh_output = np.array([1.7480791292591336, 1.496070008348207])
    assert ma.allclose(expected_mesh_output, mesh_output)

    def _give_extra_dims(array):
        result = np.stack([array, array + 1])
        result = np.stack([result, result + 10, result + 100])
        return result

    extra_dim_mesh_input = _give_extra_dims(mesh_input)
    extra_dim_grid_output = mesh_to_grid_regridder.regrid(extra_dim_mesh_input)
    extra_dim_expected_grid_output = _give_extra_dims(expected_grid_output)
    assert ma.allclose(extra_dim_expected_grid_output, extra_dim_grid_output)

    extra_dim_grid_input = _give_extra_dims(grid_input)
    extra_dim_mesh_output = grid_to_mesh_regridder.regrid(extra_dim_grid_input)
    extra_dim_expected_mesh_output = _give_extra_dims(expected_mesh_output)
    assert ma.allclose(extra_dim_expected_mesh_output, extra_dim_mesh_output)
コード例 #5
0
def test_make_mesh():
    """Basic test for creating :meth:`~esmf_regrid.esmf_regridder.GridInfo.make_esmf_field`."""
    coords, nodes, _ = _make_small_mesh_args()
    mesh_0 = MeshInfo(coords, nodes, 0)
    esmf_mesh_0 = mesh_0.make_esmf_field()
    esmf_mesh_0.data[:] = 0

    relative_path = (
        "experimental",
        "unstructured_regrid",
        "test_MeshInfo",
        "small_mesh.txt",
    )
    fname = get_result_path(relative_path)
    with open(fname) as file:
        expected_repr = file.read()

    one_indexed_nodes = nodes + 1
    mesh_1 = MeshInfo(coords, one_indexed_nodes, 1)
    esmf_mesh_1 = mesh_1.make_esmf_field()
    esmf_mesh_1.data[:] = 0

    assert esmf_mesh_0.__repr__() == esmf_mesh_1.__repr__() == expected_repr
コード例 #6
0
def test_regrid_bilinear_with_mesh():
    """Basic test for regridding with :meth:`~esmf_regrid.esmf_regridder.GridInfo.make_esmf_field`."""
    # We create a mesh with the following shape:
    # 20     ,+                  ,+ 4
    #       / |  with nodes:    / |
    # 10  +' ,+             1 +' ,+ 3
    #     | / |               | / |
    #  0  +---+             0 +---+ 2
    #     0  10
    mesh_args = _make_small_mesh_args()
    elem_coords = np.array([[5, 0], [5, 10]])
    node_mesh = MeshInfo(*mesh_args, location="node")
    face_mesh = MeshInfo(*mesh_args, elem_coords=elem_coords, location="face")

    # We create a grid with the following shape:
    # 20/3 +---+
    #      |   |
    # 10/3 +---+
    #      |   |
    #  0   +---+
    #      0  10
    grid_args = [ar * 2 for ar in make_grid_args(2, 3)]
    grid = GridInfo(*grid_args, center=True)

    mesh_to_grid_regridder = Regridder(node_mesh, grid, method="bilinear")
    mesh_input = np.arange(5)
    grid_output = mesh_to_grid_regridder.regrid(mesh_input)
    # For a flat surface, we would expect the fractional part of these values
    # to be either 1/3 or 2/3. Since the actual surface lies on a sphere, and
    # due to the way ESMF calculates these values, the expected output is
    # slightly different. It's worth noting that the finer the resolution, the
    # closer these numbers are. Since the grids/meshes lie on coarse steps of
    # about 10 degrees, we can expect most cases to behave more similarly to
    # bilinear regridders on flat surfaces.
    expected_grid_output = np.array([
        [0.0, 2.0],
        [0.6662902773937054, 2.6662902773105808],
        [-1, 3.333709722689418],
    ])
    expected_grid_mask = np.array([[0, 0], [0, 0], [1, 0]])
    expected_grid_output = ma.array(expected_grid_output,
                                    mask=expected_grid_mask)
    assert ma.allclose(expected_grid_output, grid_output)

    grid_to_mesh_regridder = Regridder(grid, node_mesh, method="bilinear")
    grid_input = np.array([[0, 0], [1, 0], [2, 1]])
    mesh_output = grid_to_mesh_regridder.regrid(grid_input)
    expected_mesh_output = ma.array([0.0, 1.5, 0.0, 0.5, -1],
                                    mask=[0, 0, 0, 0, 1])
    assert ma.allclose(expected_mesh_output, mesh_output)

    grid_to_face_mesh_regridder = Regridder(grid, face_mesh, method="bilinear")
    grid_input_2 = np.array([[0, 0], [1, 0], [4, 1]])
    face_mesh_output = grid_to_face_mesh_regridder.regrid(grid_input_2)
    expected_face_mesh_output = np.array([0.0, 1.4888258584989558])
    assert ma.allclose(expected_face_mesh_output, face_mesh_output)

    def _give_extra_dims(array):
        result = np.stack([array, array + 1])
        result = np.stack([result, result + 10, result + 100])
        return result

    extra_dim_mesh_input = _give_extra_dims(mesh_input)
    extra_dim_grid_output = mesh_to_grid_regridder.regrid(extra_dim_mesh_input)
    extra_dim_expected_grid_output = _give_extra_dims(expected_grid_output)
    assert ma.allclose(extra_dim_expected_grid_output, extra_dim_grid_output)

    extra_dim_grid_input = _give_extra_dims(grid_input)
    extra_dim_mesh_output = grid_to_mesh_regridder.regrid(extra_dim_grid_input)
    extra_dim_expected_mesh_output = _give_extra_dims(expected_mesh_output)
    assert ma.allclose(extra_dim_expected_mesh_output, extra_dim_mesh_output)