Exemple #1
0
def test_mesh():
    x_intervals = [(-10., 10.), (0., 50.)]
    d_x = [.1, .2]
    mesh = Mesh(x_intervals, d_x)

    assert mesh.vertices_shape == (201, 251)
    assert mesh.cells_shape == (200, 250)
    assert mesh.shape(True) == mesh.vertices_shape
    assert mesh.shape(False) == mesh.cells_shape

    for axis in range(2):
        assert np.array_equal(
            mesh.vertex_axis_coordinates[axis],
            np.linspace(*x_intervals[axis], mesh.vertices_shape[axis]))
        assert np.array_equal(
            mesh.cell_center_axis_coordinates[axis],
            np.linspace(x_intervals[axis][0] + d_x[axis] / 2.,
                        x_intervals[axis][1] - d_x[axis] / 2.,
                        mesh.cells_shape[axis]))
        assert np.array_equal(
            mesh.axis_coordinates(True)[axis],
            mesh.vertex_axis_coordinates[axis])
        assert np.array_equal(
            mesh.axis_coordinates(False)[axis],
            mesh.cell_center_axis_coordinates[axis])

    all_vertex_x = mesh.all_index_coordinates(True)
    assert np.allclose(all_vertex_x[2, 3], (-9.8, .6))

    all_vertex_x_flattened = mesh.all_index_coordinates(True, True)
    assert np.array_equal(all_vertex_x[2, 3],
                          all_vertex_x_flattened[2 * 251 + 3])

    all_cell_x = mesh.all_index_coordinates(False)
    assert np.allclose(all_cell_x[2, 3], (-9.75, .7))

    all_cell_x_flattened = mesh.all_index_coordinates(False, True)
    assert np.array_equal(all_cell_x[2, 3], all_cell_x_flattened[2 * 250 + 3])
Exemple #2
0
    def _verify_pde_solution_shape_matches_problem(
            y: np.ndarray,
            mesh: Mesh,
            vertex_oriented: bool,
            expected_x_dims: Union[int, Tuple[int, int]],
            is_vector_field: bool):
        """
        Verifies that the shape of the input array representing the solution
        of a partial differential equation over the provided mesh and with the
        specified vertex orientation matches expectations.

        :param y: an array representing the solution of the partial
            differential equation
        :param mesh: the spatial mesh over which the solution is evaluated
        :param vertex_oriented: whether the solution is evaluated over the
            vertices or the cell centers of the mesh
        :param expected_x_dims: the expected number of spatial dimensions
        :param is_vector_field: whether the solution is supposed to be a vector
            field or a scalar field
        """
        if isinstance(expected_x_dims, int):
            if mesh.dimensions != expected_x_dims:
                raise ValueError(f'mesh must be {expected_x_dims} dimensional')
        elif not (expected_x_dims[0] <= mesh.dimensions <= expected_x_dims[1]):
            raise ValueError(
                f'mesh must be between {expected_x_dims[0]} and '
                f'{expected_x_dims[1]} dimensional')

        if y.ndim != mesh.dimensions + 2:
            raise ValueError(
                f'number of y axes ({y.ndim}) must be two larger than mesh '
                f'dimensions ({mesh.dimensions})')

        if y.shape[1:-1] != mesh.shape(vertex_oriented):
            raise ValueError(
                f'y shape {y.shape} must be compatible with mesh shape '
                f'{mesh.shape(vertex_oriented)}')

        if is_vector_field:
            if y.shape[-1] != mesh.dimensions:
                raise ValueError(
                    f'number of y components ({y.shape[-1]}) must match '
                    f'x dimensions {mesh.dimensions}')
        elif y.shape[-1] != 1:
            raise ValueError(
                f'number of y components ({y.shape[-1]}) must be one')
def test_cp_3d_pde():
    mesh = Mesh([(2., 6.), (-3., 3.), (10., 12.)], [.1, .2, .5])

    assert mesh.shape(True) == (41, 31, 5)
    assert mesh.shape(False) == (40, 30, 4)

    diff_eq = WaveEquation(3)
    cp = ConstrainedProblem(
        diff_eq, mesh,
        ((DirichletBoundaryCondition(
            vectorize_bc_function(lambda x, t: (999., None)), is_static=True),
          NeumannBoundaryCondition(
              vectorize_bc_function(lambda x, t: (None, None)),
              is_static=True)),
         (DirichletBoundaryCondition(
             vectorize_bc_function(lambda x, t: (0., 0.)), is_static=True),
          NeumannBoundaryCondition(lambda x, t: np.full((len(x), 2), t))),
         (NeumannBoundaryCondition(lambda x, t: -x[:, :2] * x[:, 1:3],
                                   is_static=True),
          DirichletBoundaryCondition(
              vectorize_bc_function(lambda x, t: (-999., None))))))

    assert cp.y_shape(True) == (41, 31, 5, 2)
    assert cp.y_shape(False) == (40, 30, 4, 2)

    assert not cp.are_all_boundary_conditions_static
    assert cp.are_there_boundary_conditions_on_y

    assert cp.static_y_vertex_constraints.shape == (2, )

    y = np.full(cp._y_vertices_shape, -1)
    cp.static_y_vertex_constraints[0].apply(y[..., :1])
    cp.static_y_vertex_constraints[1].apply(y[..., 1:])

    assert np.all(y[0, 1:, :, 0] == 999.)
    assert np.all(y[:, 0, :, 0] == 0.)
    assert np.all(y[1:, 1:, :, 0] == -1.)
    assert np.all(y[:, 0, :, 1] == 0.)
    assert np.all(y[:, 1:, :, 1] == -1.)

    vertex_boundary_constraints = cp.static_boundary_vertex_constraints
    cell_boundary_constraints = cp.static_boundary_cell_constraints

    for y_boundary_constraints in \
            [vertex_boundary_constraints[0], cell_boundary_constraints[0]]:
        assert y_boundary_constraints.shape == (3, 2)
        assert y_boundary_constraints[0, 0][0] is not None
        assert y_boundary_constraints[0, 1][0] is not None
        assert y_boundary_constraints[0, 0][1] is None
        assert y_boundary_constraints[0, 1][1] is None
        assert y_boundary_constraints[1, 0][0] is not None
        assert y_boundary_constraints[1, 1][0] is not None
        assert y_boundary_constraints[1, 0][1] is None
        assert y_boundary_constraints[1, 1][1] is None
        assert y_boundary_constraints[2, 0][0] is None
        assert y_boundary_constraints[2, 1][0] is None
        assert y_boundary_constraints[2, 0][1] is None
        assert y_boundary_constraints[2, 1][1] is None

    for d_y_boundary_constraints in \
            [vertex_boundary_constraints[1], cell_boundary_constraints[1]]:
        assert d_y_boundary_constraints.shape == (3, 2)
        assert d_y_boundary_constraints[0, 0][0] is None
        assert d_y_boundary_constraints[0, 1][0] is None
        assert d_y_boundary_constraints[0, 0][1] is not None
        assert d_y_boundary_constraints[0, 1][1] is not None
        assert d_y_boundary_constraints[1, 0][0] is None
        assert d_y_boundary_constraints[1, 1][0] is None
        assert d_y_boundary_constraints[1, 0][1] is None
        assert d_y_boundary_constraints[1, 1][1] is None
        assert d_y_boundary_constraints[2, 0][0] is not None
        assert d_y_boundary_constraints[2, 1][0] is not None
        assert d_y_boundary_constraints[2, 0][1] is None
        assert d_y_boundary_constraints[2, 1][1] is None

    new_vertex_boundary_constraints = cp.create_boundary_constraints(True, 1.)
    new_y_boundary_constraints = new_vertex_boundary_constraints[0]
    new_d_y_boundary_constraints = new_vertex_boundary_constraints[1]
    assert new_y_boundary_constraints[2, 0][1] is not None
    assert new_y_boundary_constraints[2, 1][1] is not None
    assert new_d_y_boundary_constraints[1, 0][1] is not None
    assert new_d_y_boundary_constraints[1, 1][1] is not None

    d_y_boundary = np.full((41, 1, 5, 2), np.nan)
    new_d_y_boundary_constraints[1, 0][1].apply(d_y_boundary[..., :1])
    new_d_y_boundary_constraints[1, 1][1].apply(d_y_boundary[..., 1:])
    assert np.all(d_y_boundary == 1.)

    new_y_vertex_constraints = \
        cp.create_y_vertex_constraints(new_y_boundary_constraints)
    assert new_y_vertex_constraints.shape == (2, )

    y = np.full(cp._y_vertices_shape, -1)
    new_y_vertex_constraints[0].apply(y[..., :1])
    new_y_vertex_constraints[1].apply(y[..., 1:])

    assert np.all(y[0, 1:, :-1, 0] == 999.)
    assert np.all(y[:, 0, :-1, 0] == 0.)
    assert np.all(y[:, :, -1, 0] == -999.)
    assert np.all(y[1:, 1:, :-1, 0] == -1.)
    assert np.all(y[:, 0, :, 1] == 0.)
    assert np.all(y[:, 1:, :, 1] == -1.)