Ejemplo n.º 1
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, 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)))
    print('Monitor test passed.')
Ejemplo n.º 2
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, 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=300,
        train_generator=ExampleGenerator2D((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.')
Ejemplo n.º 3
0
def test_legacy_module():
    with warns(FutureWarning):
        import neurodiffeq.generator

    from neurodiffeq.ode import ExampleGenerator
    from neurodiffeq.pde import ExampleGenerator2D, PredefinedExampleGenerator2D
    from neurodiffeq.pde_spherical import ExampleGenerator3D, ExampleGeneratorSpherical
    with warns(FutureWarning):
        ExampleGenerator(100)
    with warns(FutureWarning):
        ExampleGenerator2D()
    with warns(FutureWarning):
        x = torch.rand(10)
        y = torch.rand(10)
        PredefinedExampleGenerator2D(x, y)
    with warns(FutureWarning):
        ExampleGenerator3D()
    with warns(FutureWarning):
        ExampleGeneratorSpherical(100)
Ejemplo n.º 4
0
def test_heat():

    k, L, T = 0.3, 2, 3
    heat = lambda u, x, t: diff(u, t) - k * diff(u, x, order=2)

    ibvp = IBVP1D(x_min=0,
                  x_min_val=lambda t: 0,
                  x_max=L,
                  x_max_val=lambda t: 0,
                  t_min=0,
                  t_min_val=lambda x: torch.sin(np.pi * x / L))
    net = FCNN(n_input_units=2, n_hidden_units=32, n_hidden_layers=1)

    def mse(u, x, y):
        true_u = torch.sin(np.pi * y) * torch.sinh(np.pi *
                                                   (1 - x)) / np.sinh(np.pi)
        return torch.mean((u - true_u)**2)

    solution_neural_net_heat, _ = solve2D(pde=heat,
                                          condition=ibvp,
                                          xy_min=(0, 0),
                                          xy_max=(L, T),
                                          net=net,
                                          max_epochs=300,
                                          train_generator=ExampleGenerator2D(
                                              (32, 32), (0, 0), (L, T),
                                              method='equally-spaced-noisy'),
                                          batch_size=64,
                                          metrics={'mse': mse})
    solution_analytical_heat = lambda x, t: np.sin(np.pi * x / L) * np.exp(
        -k * np.pi**2 * t / L**2)

    xs = np.linspace(0, L, 101)
    ts = np.linspace(0, T, 101)
    xx, tt = np.meshgrid(xs, ts)
    make_animation(solution_neural_net_heat, xs, ts)  # test animation
    sol_ana = solution_analytical_heat(xx, tt)
    sol_net = solution_neural_net_heat(xx, tt, as_type='np')
    assert isclose(sol_net, sol_ana, atol=0.01).all()
    print('Heat test passed.')
Ejemplo n.º 5
0
def test_neumann_boundaries_2():

    k, L, T = 0.3, 2, 3
    heat = lambda u, x, t: diff(u, t) - k * diff(u, x, order=2)
    solution_analytical_heat = lambda x, t: np.sin(np.pi * x / L) * np.exp(
        -k * np.pi**2 * t / L**2)

    # Neumann on the left Dirichlet on the right
    ibvp = IBVP1D(
        x_min=0,
        x_min_prime=lambda t: np.pi / L * torch.exp(-k * np.pi**2 * t / L**2),
        x_max=L,
        x_max_val=lambda t: 0,
        t_min=0,
        t_min_val=lambda x: torch.sin(np.pi * x / L))

    net = FCNN(n_input_units=2, n_hidden_units=32, n_hidden_layers=1)

    solution_neural_net_heat, _ = solve2D(pde=heat,
                                          condition=ibvp,
                                          xy_min=(0, 0),
                                          xy_max=(L, T),
                                          net=net,
                                          max_epochs=300,
                                          train_generator=ExampleGenerator2D(
                                              (32, 32), (0, 0), (L, T),
                                              method='equally-spaced-noisy'),
                                          batch_size=64)

    xs = np.linspace(0, L, 101)
    ts = np.linspace(0, T, 101)
    xx, tt = np.meshgrid(xs, ts)
    make_animation(solution_neural_net_heat, xs, ts)  # test animation
    sol_ana = solution_analytical_heat(xx, tt)
    sol_net = solution_neural_net_heat(xx, tt, as_type='np')
    assert isclose(sol_net, sol_ana, atol=0.1).all()
    print('Neumann on the left Dirichlet on the right test passed.')