Exemplo n.º 1
0
def test_pidon_operator_on_spherical_pde():
    set_random_seed(0)

    diff_eq = DiffusionEquation(3)
    mesh = Mesh(
        [(1., 11.), (0., 2 * np.pi), (.25 * np.pi, .75 * np.pi)],
        [2., np.pi / 5., np.pi / 4],
        CoordinateSystem.SPHERICAL)
    bcs = [
        (DirichletBoundaryCondition(
            lambda x, t: np.ones((len(x), 1)), is_static=True),
         DirichletBoundaryCondition(
             lambda x, t: np.full((len(x), 1), 1. / 11.), is_static=True)),
        (NeumannBoundaryCondition(
            lambda x, t: np.zeros((len(x), 1)), is_static=True),
         NeumannBoundaryCondition(
             lambda x, t: np.zeros((len(x), 1)), is_static=True)),
        (NeumannBoundaryCondition(
            lambda x, t: np.zeros((len(x), 1)), is_static=True),
         NeumannBoundaryCondition(
             lambda x, t: np.zeros((len(x), 1)), is_static=True))
    ]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = ContinuousInitialCondition(cp, lambda x: 1. / x[:, :1])
    t_interval = (0., .5)
    ivp = InitialValueProblem(cp, t_interval, ic)

    sampler = UniformRandomCollocationPointSampler()
    pidon = PIDONOperator(sampler, .001, True)

    training_loss_history, test_loss_history = pidon.train(
        cp,
        t_interval,
        training_data_args=DataArgs(
            y_0_functions=[ic.y_0],
            n_domain_points=20,
            n_boundary_points=10,
            n_batches=1
        ),
        model_args=ModelArgs(
            latent_output_size=20,
            branch_hidden_layer_sizes=[30, 30],
            trunk_hidden_layer_sizes=[30, 30],
        ),
        optimization_args=OptimizationArgs(
            optimizer=optimizers.Adam(learning_rate=2e-5),
            epochs=3,
            verbose=False
        )
    )

    assert len(training_loss_history) == 3
    for i in range(2):
        assert np.all(
            training_loss_history[i + 1].weighted_total_loss.numpy() <
            training_loss_history[i].weighted_total_loss.numpy())

    solution = pidon.solve(ivp)
    assert solution.d_t == .001
    assert solution.discrete_y().shape == (500, 6, 11, 3, 1)
Exemplo n.º 2
0
def test_discrete_initial_condition_2d_pde():
    diff_eq = WaveEquation(2)
    mesh = Mesh([(0., 2.), (0., 2.)], [1., 1.])
    bcs = [(DirichletBoundaryCondition(
        vectorize_bc_function(lambda x, t: (0., 2.)), is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (1., 2.)), is_static=True)),
           (DirichletBoundaryCondition(
               vectorize_bc_function(lambda x, t: (3., 2.)), is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (4., 2.)), is_static=True))]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    initial_condition = DiscreteInitialCondition(cp, np.zeros((3, 3, 2)), True)

    y = initial_condition.y_0(np.array([1.5, .5]).reshape((1, 2)))
    assert np.allclose(y, [1.75, 1.5])

    y_0_vertices = initial_condition.discrete_y_0(True)
    assert y_0_vertices.shape == (3, 3, 2)
    assert np.all(y_0_vertices[0, 1:-1, 0] == 0.)
    assert np.all(y_0_vertices[0, 1:-1, 1] == 2.)
    assert np.all(y_0_vertices[-1, 1:-1, 0] == 1.)
    assert np.all(y_0_vertices[-1, 1:-1, 1] == 2.)
    assert np.all(y_0_vertices[:, 0, 0] == 3.)
    assert np.all(y_0_vertices[:, 0, 1] == 2.)
    assert np.all(y_0_vertices[:, -1, 0] == 4.)
    assert np.all(y_0_vertices[:, -1, 1] == 2.)
    assert np.all(y_0_vertices[1:-1, 1:-1, :] == 0.)

    y_0_cell_centers = initial_condition.discrete_y_0(False)
    assert y_0_cell_centers.shape == (2, 2, 2)
Exemplo n.º 3
0
def test_fdm_operator_on_2d_pde():
    diff_eq = NavierStokesEquation(5000.)
    mesh = Mesh([(0., 10.), (0., 10.)], [1., 1.])
    bcs = [(DirichletBoundaryCondition(
        vectorize_bc_function(lambda x, t: (1., .1, None, None)),
        is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (0., 0., None, None)),
                is_static=True)),
           (DirichletBoundaryCondition(
               vectorize_bc_function(lambda x, t: (0., 0., None, None)),
               is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (0., 0., None, None)),
                is_static=True))]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = ContinuousInitialCondition(cp, lambda x: np.zeros((len(x), 4)))
    ivp = InitialValueProblem(cp, (0., 10.), ic)
    op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .25)
    solution = op.solve(ivp)

    assert solution.vertex_oriented
    assert solution.d_t == .25
    assert solution.discrete_y().shape == (40, 11, 11, 4)
    assert solution.discrete_y(False).shape == (40, 10, 10, 4)
Exemplo n.º 4
0
def test_gaussian_initial_condition_2d_pde():
    diff_eq = WaveEquation(2)
    mesh = Mesh([(0., 2.), (0., 2.)], [1., 1.])
    bcs = [(DirichletBoundaryCondition(
        vectorize_bc_function(lambda x, t: (0., 2.)), is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (1., 2.)), is_static=True)),
           (DirichletBoundaryCondition(
               vectorize_bc_function(lambda x, t: (3., 2.)), is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (4., 2.)), is_static=True))]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    initial_condition = GaussianInitialCondition(cp, [
        (np.array([1., 1.]), np.array([[1., 0.], [0., 1.]])),
        (np.array([1., 1.]), np.array([[.75, .25], [.25, .75]])),
    ], [1., 2.])

    x_coordinates = np.array([[1., 1.], [.5, 1.5]])
    expected_y_0 = [[.15915494, .45015816], [.12394999, .27303472]]
    actual_y_0 = initial_condition.y_0(x_coordinates)
    assert np.allclose(actual_y_0, expected_y_0)

    expected_vertex_discrete_y_0 = [[[3., 2.], [0., 2.], [4., 2.]],
                                    [[3., 2.], [.15915494, .45015816],
                                     [4., 2.]], [[3., 2.], [1., 2.], [4., 2.]]]
    actual_vertex_discrete_y_0 = initial_condition.discrete_y_0(True)
    assert np.allclose(actual_vertex_discrete_y_0,
                       expected_vertex_discrete_y_0)

    expected_cell_discrete_y_0 = [[[.12394999, .35058353],
                                   [.12394999, .27303472]],
                                  [[.12394999, .27303472],
                                   [.12394999, .35058353]]]
    actual_cell_discrete_y_0 = initial_condition.discrete_y_0(False)
    assert np.allclose(actual_cell_discrete_y_0, expected_cell_discrete_y_0)
Exemplo n.º 5
0
def test_continuous_initial_condition_1d_pde():
    diff_eq = DiffusionEquation(1)
    mesh = Mesh([(0., 20.)], [.1])
    bcs = [(DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 1)),
                                       is_static=True),
            DirichletBoundaryCondition(lambda x, t: np.full((len(x), 1), 1.5),
                                       is_static=True))]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    initial_condition = ContinuousInitialCondition(
        cp, lambda x: np.exp(-np.square(np.array(x) - 10.) / (2 * 5**2)))

    assert np.isclose(initial_condition.y_0(np.full((1, 1), 10.)), 1.)
    assert np.isclose(
        initial_condition.y_0(np.full((1, 1),
                                      np.sqrt(50) + 10.)), np.e**-1)
    assert np.allclose(initial_condition.y_0(np.full((5, 1), 10.)),
                       np.ones((5, 1)))

    y_0_vertices = initial_condition.discrete_y_0(True)
    assert y_0_vertices.shape == (201, 1)
    assert y_0_vertices[0, 0] == 0.
    assert y_0_vertices[-1, 0] == 1.5
    assert y_0_vertices[100, 0] == 1.
    assert np.all(0. < y_0_vertices[1:100, 0]) \
        and np.all(y_0_vertices[1:100, 0] < 1.)
    assert np.all(0. < y_0_vertices[101:-1, 0]) \
        and np.all(y_0_vertices[101:-1, 0] < 1.)

    y_0_cell_centers = initial_condition.discrete_y_0(False)
    assert y_0_cell_centers.shape == (200, 1)
    assert np.all(0. < y_0_cell_centers) and np.all(y_0_cell_centers < 1.)
Exemplo n.º 6
0
def test_pidon_operator_on_pde_system():
    set_random_seed(0)

    diff_eq = NavierStokesEquation()
    mesh = Mesh([(-2.5, 2.5), (0., 4.)], [1., 1.])
    ic_function = vectorize_ic_function(lambda x: [
        2. * x[0] - 4.,
        2. * x[0] ** 2 + 3. * x[1] - x[0] * x[1] ** 2,
        4. * x[0] - x[1] ** 2,
        2. * x[0] * x[1] - 3.
    ])
    bcs = [
        (DirichletBoundaryCondition(
            lambda x, t: ic_function(x),
            is_static=True),
         DirichletBoundaryCondition(
             lambda x, t: ic_function(x),
             is_static=True))
    ] * 2
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = ContinuousInitialCondition(cp, ic_function)
    t_interval = (0., .5)
    ivp = InitialValueProblem(cp, t_interval, ic)

    sampler = UniformRandomCollocationPointSampler()
    pidon = PIDONOperator(sampler, .001, True)

    training_loss_history, test_loss_history = pidon.train(
        cp,
        t_interval,
        training_data_args=DataArgs(
            y_0_functions=[ic.y_0],
            n_domain_points=20,
            n_boundary_points=10,
            n_batches=1
        ),
        model_args=ModelArgs(
            latent_output_size=20,
            branch_hidden_layer_sizes=[20, 20],
            trunk_hidden_layer_sizes=[20, 20],
        ),
        optimization_args=OptimizationArgs(
            optimizer=optimizers.Adam(learning_rate=1e-5),
            epochs=3,
            verbose=False
        )
    )

    assert len(training_loss_history) == 3
    for i in range(2):
        assert np.all(
            training_loss_history[i + 1].weighted_total_loss.numpy() <
            training_loss_history[i].weighted_total_loss.numpy())

    solution = pidon.solve(ivp)
    assert solution.d_t == .001
    assert solution.discrete_y().shape == (500, 6, 5, 4)
Exemplo n.º 7
0
def test_dirichlet_boundary_condition():
    bc = DirichletBoundaryCondition(lambda x, t: t * x)
    assert not bc.is_static
    assert bc.has_y_condition
    assert not bc.has_d_y_condition

    assert np.allclose(
        bc._y_condition(np.ones((2, 1)), 5.),
        np.full((2, 1), 5.))

    with pytest.raises(RuntimeError):
        bc.d_y_condition(np.ones((2, 1)), 5.)
def test_cp_pde_with_wrong_boundary_constraint_width():
    diff_eq = WaveEquation(2)
    mesh = Mesh([(0., 5.), (-5., 5.)], [.1, .2])
    bcs = [(DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 1)),
                                       is_static=True), ) * 2] * 2
    with pytest.raises(ValueError):
        ConstrainedProblem(diff_eq, mesh, bcs)

    bcs = [(DirichletBoundaryCondition(
        vectorize_bc_function(lambda x, t: [0.]), is_static=True), ) * 2] * 2
    with pytest.raises(ValueError):
        ConstrainedProblem(diff_eq, mesh, bcs)
def test_cp_pde_with_wrong_boundary_constraint_length():
    diff_eq = DiffusionEquation(2)
    mesh = Mesh([(0., 5.), (-5., 5.)], [.1, .2])
    static_bcs = [(DirichletBoundaryCondition(lambda x, t: np.zeros((13, 1)),
                                              is_static=True), ) * 2] * 2
    with pytest.raises(ValueError):
        ConstrainedProblem(diff_eq, mesh, static_bcs)

    dynamic_bcs = [
        (DirichletBoundaryCondition(lambda x, t: np.zeros((13, 1))), ) * 2
    ] * 2
    cp = ConstrainedProblem(diff_eq, mesh, dynamic_bcs)
    with pytest.raises(ValueError):
        cp.create_boundary_constraints(True, 0.)
Exemplo n.º 10
0
def test_discrete_initial_condition_pde_with_wrong_shape():
    diff_eq = WaveEquation(1)
    mesh = Mesh([(0., 10.)], [1.])
    bcs = [(DirichletBoundaryCondition(lambda x: np.zeros((len(x), 2))), ) * 2]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    with pytest.raises(ValueError):
        DiscreteInitialCondition(cp, np.zeros((10, 2)), vertex_oriented=True)
Exemplo n.º 11
0
def test_continuous_initial_condition_pde_with_wrong_shape():
    diff_eq = WaveEquation(1)
    mesh = Mesh([(0., 10.)], [1.])
    bcs = [(DirichletBoundaryCondition(lambda x: np.zeros((len(x), 2))), ) * 2]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    with pytest.raises(ValueError):
        ContinuousInitialCondition(cp, lambda x: np.zeros((3, 2)))
Exemplo n.º 12
0
def test_gaussian_initial_condition_pde_with_wrong_means_and_cov_length():
    diff_eq = WaveEquation(1)
    mesh = Mesh([(0., 10.)], [1.])
    bcs = [(DirichletBoundaryCondition(lambda x: np.zeros((len(x), 2))), ) * 2]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    with pytest.raises(ValueError):
        GaussianInitialCondition(cp, [(np.array([1.]), np.array([[1.]]))] * 1)
Exemplo n.º 13
0
def test_auto_regression_operator_on_pde():
    set_random_seed(0)

    diff_eq = WaveEquation(2)
    mesh = Mesh([(-5., 5.), (-5., 5.)], [1., 1.])
    bcs = [(DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 2)),
                                       is_static=True),
            DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 2)),
                                       is_static=True))] * 2
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = GaussianInitialCondition(
        cp, [(np.array([0., 2.5]), np.array([[.1, 0.], [0., .1]]))] * 2,
        [3., .0])
    ivp = InitialValueProblem(cp, (0., 10.), ic)

    oracle = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .1)
    ref_solution = oracle.solve(ivp)

    ml_op = AutoRegressionOperator(2.5, True)
    ml_op.train(
        ivp, oracle,
        SKLearnKerasRegressor(
            DeepONet([
                np.prod(cp.y_shape(True)).item(), 100, 50,
                diff_eq.y_dimension * 10
            ], [1 + diff_eq.x_dimension, 50, 50, diff_eq.y_dimension * 10],
                     diff_eq.y_dimension),
            optimizer=optimizers.Adam(
                learning_rate=optimizers.schedules.ExponentialDecay(
                    1e-2, decay_steps=500, decay_rate=.95)),
            batch_size=968,
            epochs=500,
        ), 20, lambda t, y: y + np.random.normal(0., t / 75., size=y.shape))
    ml_solution = ml_op.solve(ivp)

    assert ml_solution.vertex_oriented
    assert ml_solution.d_t == 2.5
    assert ml_solution.discrete_y().shape == (4, 11, 11, 2)

    diff = ref_solution.diff([ml_solution])
    assert np.all(diff.matching_time_points == np.linspace(2.5, 10., 4))
    assert np.max(np.abs(diff.differences[0])) < .5
Exemplo n.º 14
0
def test_ode_operator_on_pde():
    diff_eq = DiffusionEquation(1, 1.5)
    mesh = Mesh([(0., 10.)], [.1])
    bcs = [
        (NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))),
         DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 1)))),
    ]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = GaussianInitialCondition(cp, [(np.array([5.]), np.array([[2.5]]))],
                                  [20.])
    ivp = InitialValueProblem(cp, (0., 10.), ic)
    op = ODEOperator('RK23', 2.5e-3)
    with pytest.raises(ValueError):
        op.solve(ivp)
Exemplo n.º 15
0
def test_fdm_operator_on_pde_with_dynamic_boundary_conditions():
    diff_eq = DiffusionEquation(1, 1.5)
    mesh = Mesh([(0., 10.)], [1.])
    bcs = [
        (NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))),
         DirichletBoundaryCondition(lambda x, t: np.full((len(x), 1), t / 5.))
         ),
    ]
    cp = ConstrainedProblem(diff_eq, mesh, bcs)
    ic = GaussianInitialCondition(cp, [(np.array([5.]), np.array([[2.5]]))],
                                  [20.])
    ivp = InitialValueProblem(cp, (0., 10.), ic)
    op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .5)
    solution = op.solve(ivp)
    y = solution.discrete_y()

    assert solution.vertex_oriented
    assert solution.d_t == .5
    assert y.shape == (20, 11, 1)
    assert solution.discrete_y(False).shape == (20, 10, 1)

    assert np.isclose(y[0, -1, 0], .1)
    assert np.isclose(y[-1, -1, 0], 2.)
Exemplo n.º 16
0
def test_cp_2d_pde():
    diff_eq = WaveEquation(2)
    mesh = Mesh([(2., 6.), (-3., 3.)], [.1, .2])
    bcs = ((DirichletBoundaryCondition(
        vectorize_bc_function(lambda x, t: (999., None)), is_static=True),
            NeumannBoundaryCondition(
                vectorize_bc_function(lambda x, t: (100., -100.)),
                is_static=True)),
           (NeumannBoundaryCondition(
               vectorize_bc_function(lambda x, t: (-x[0], None)),
               is_static=True),
            DirichletBoundaryCondition(
                vectorize_bc_function(lambda x, t: (x[0], x[1])),
                is_static=True)))
    cp = ConstrainedProblem(diff_eq, mesh, bcs)

    assert cp.are_all_boundary_conditions_static
    assert cp.are_there_boundary_conditions_on_y

    y_vertices = np.full(cp.y_shape(True), 13.)
    apply_constraints_along_last_axis(cp.static_y_vertex_constraints,
                                      y_vertices)

    assert np.all(y_vertices[0, :-1, 0] == 999.)
    assert np.all(y_vertices[0, :-1, 1] == 13.)
    assert np.all(y_vertices[-1, :-1, :] == 13.)
    assert np.all(y_vertices[1:, 0, :] == 13.)
    assert np.allclose(y_vertices[:, -1, 0],
                       np.linspace(2., 6., y_vertices.shape[0]))
    assert np.all(y_vertices[:, -1, 1] == 3.)

    y_vertices = np.zeros(cp.y_shape(True))
    diff = ThreePointCentralDifferenceMethod()
    d_y_boundary_constraints = cp.static_boundary_vertex_constraints[1]

    d_y_0_over_d_x_0 = diff.gradient(y_vertices[..., :1], mesh, 0,
                                     d_y_boundary_constraints[:, :1])

    assert np.all(d_y_0_over_d_x_0[-1, :, :] == 100.)
    assert np.all(d_y_0_over_d_x_0[:-1, :, :] == 0.)

    d_y_0_over_d_x_1 = diff.gradient(y_vertices[..., :1], mesh, 1,
                                     d_y_boundary_constraints[:, :1])

    assert np.allclose(d_y_0_over_d_x_1[:, 0, 0],
                       np.linspace(-2., -6., y_vertices.shape[0]))
    assert np.all(d_y_0_over_d_x_1[:, 1:, :] == 0.)

    d_y_1_over_d_x_0 = diff.gradient(y_vertices[..., 1:], mesh, 0,
                                     d_y_boundary_constraints[:, 1:])

    assert np.all(d_y_1_over_d_x_0[-1, :, :] == -100.)
    assert np.all(d_y_1_over_d_x_0[:-1, :, :] == 0.)

    d_y_1_over_d_x_1 = diff.gradient(y_vertices[..., 1:], mesh, 1,
                                     d_y_boundary_constraints[:, 1:])

    assert np.all(d_y_1_over_d_x_1 == 0.)

    y_boundary_cell_constraints = cp.static_boundary_cell_constraints[0]

    assert np.all(y_boundary_cell_constraints[0, 0][0].mask == [True] *
                  cp.y_cells_shape[1])
    assert np.all(y_boundary_cell_constraints[0, 0][0].values == 999.)
    assert np.all(y_boundary_cell_constraints[0, 1][0].mask == [False] *
                  cp.y_cells_shape[1])
    assert y_boundary_cell_constraints[0, 1][0].values.size == 0

    assert np.all(y_boundary_cell_constraints[1, 0][1].mask == [True] *
                  cp.y_cells_shape[0])
    assert np.allclose(y_boundary_cell_constraints[1, 0][1].values,
                       np.linspace(2.05, 5.95, cp.y_cells_shape[0]))
    assert np.all(y_boundary_cell_constraints[1, 1][1].mask == [True] *
                  cp.y_cells_shape[0])
    assert np.all(y_boundary_cell_constraints[1, 1][1].values == 3.)
    assert y_boundary_cell_constraints[1, 0][0] is None
Exemplo n.º 17
0
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.)