Example #1
0
def test_laplace():

    laplace = lambda u, x, y: diff(u, x, order=2) + diff(u, y, order=2)
    bc = DirichletBVP2D(x_min=0,
                        x_min_val=lambda y: torch.sin(np.pi * y),
                        x_max=1,
                        x_max_val=lambda y: 0,
                        y_min=0,
                        y_min_val=lambda x: 0,
                        y_max=1,
                        y_max_val=lambda x: 0)

    net = FCNN(n_input_units=2, hidden_units=(32, 32))
    solution_neural_net_laplace, _ = solve2D(pde=laplace,
                                             condition=bc,
                                             xy_min=(0, 0),
                                             xy_max=(1, 1),
                                             net=net,
                                             max_epochs=300,
                                             train_generator=Generator2D(
                                                 (32, 32), (0, 0), (1, 1),
                                                 method='equally-spaced-noisy',
                                                 xy_noise_std=(0.01, 0.01)),
                                             batch_size=64)
    solution_analytical_laplace = lambda x, y: np.sin(np.pi * y) * np.sinh(
        np.pi * (1 - x)) / np.sinh(np.pi)

    xs, ys = np.linspace(0, 1, 101), np.linspace(0, 1, 101)
    xx, yy = np.meshgrid(xs, ys)
    sol_net = solution_neural_net_laplace(xx, yy, as_type='np')
    sol_ana = solution_analytical_laplace(xx, yy)
    assert isclose(sol_net, sol_ana, atol=0.01).all()
    print('Laplace test passed.')
Example #2
0
def test_monitor():

    laplace = lambda u, x, y: diff(u, x, order=2) + diff(u, y, order=2)
    bc = DirichletBVP2D(x_min=0,
                        x_min_val=lambda y: torch.sin(np.pi * y),
                        x_max=1,
                        x_max_val=lambda y: 0,
                        y_min=0,
                        y_min_val=lambda x: 0,
                        y_max=1,
                        y_max_val=lambda x: 0)

    net = FCNN(n_input_units=2, hidden_units=(32, 32))
    solution_neural_net_laplace, _ = solve2D(
        pde=laplace,
        condition=bc,
        xy_min=(0, 0),
        xy_max=(1, 1),
        net=net,
        max_epochs=3,
        train_generator=Generator2D((32, 32), (0, 0), (1, 1),
                                    method='equally-spaced-noisy'),
        batch_size=64,
        monitor=Monitor2D(check_every=1, xy_min=(0, 0), xy_max=(1, 1)))
    print('Monitor test passed.')
Example #3
0
def test_train_generator():
    laplace = lambda u, x, y: diff(u, x, order=2) + diff(u, y, order=2)
    bc = DirichletBVP2D(x_min=0,
                        x_min_val=lambda y: torch.sin(np.pi * y),
                        x_max=1,
                        x_max_val=lambda y: 0,
                        y_min=0,
                        y_min_val=lambda x: 0,
                        y_max=1,
                        y_max_val=lambda x: 0)

    net = FCNN(n_input_units=2, n_hidden_units=32, n_hidden_layers=1)
    solution_neural_net_laplace, _ = solve2D(
        pde=laplace,
        condition=bc,
        xy_min=(0, 0),
        xy_max=(1, 1),
        net=net,
        max_epochs=3,
        train_generator=ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                           method='equally-spaced-noisy'),
        batch_size=64,
        monitor=Monitor2D(check_every=1, xy_min=(0, 0), xy_max=(1, 1)))

    train_gen = ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                   method='equally-spaced')
    solution_neural_net_laplace, _ = solve2D(pde=laplace,
                                             condition=bc,
                                             xy_min=(0, 0),
                                             xy_max=(1, 1),
                                             net=net,
                                             max_epochs=3,
                                             train_generator=train_gen,
                                             batch_size=64)
    train_gen = ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                   method='equally-spaced-noisy')
    solution_neural_net_laplace, _ = solve2D(pde=laplace,
                                             condition=bc,
                                             xy_min=(0, 0),
                                             xy_max=(1, 1),
                                             net=net,
                                             max_epochs=3,
                                             train_generator=train_gen,
                                             batch_size=64)

    with raises(ValueError):
        train_gen = ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                       method='magic')
    print('ExampleGenerator test passed.')

    valid_gen = ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                   method='equally-spaced-noisy')
    train_gen = ExampleGenerator2D((32, 32), (0, 0), (1, 1),
                                   method='equally-spaced')
    solution_neural_net_laplace, _ = solve2D(pde=laplace,
                                             condition=bc,
                                             net=net,
                                             max_epochs=3,
                                             train_generator=train_gen,
                                             valid_generator=valid_gen,
                                             batch_size=64)

    with raises(RuntimeError):
        solution_neural_net_laplace, _ = solve2D(pde=laplace,
                                                 condition=bc,
                                                 net=net,
                                                 max_epochs=3,
                                                 batch_size=64)