def get_solution(use_single: bool) -> Solution2D: conditions = [ DirichletBVP2D(x0, ux0, x1, ux1, y0, uy0, y1, uy1), DirichletBVP2D(x0, vx0, x1, vx1, y0, vy0, y1, vy1), ] if use_single: net = FCNN(2, 2) for i, cond in enumerate(conditions): cond.set_impose_on(i) return Solution2D(net, conditions) else: nets = [FCNN(2, 1), FCNN(2, 1)] return Solution2D(nets, conditions)
def test_dirichlet_bvp_2d(): # fix u(x, y) at the four corners (x0, y0), (x0, y1), (x1, y0), (x1, y1), u00, u01, u10, u11 = [random.random() for _ in range(4)] # set the boundary conditions on the four sides net_f0, net_f1, net_g0, net_g1 = [FCNN(1, 1) for _ in range(4)] cond_f0 = DirichletBVP(y0, u00, y1, u01) cond_f1 = DirichletBVP(y0, u10, y1, u11) cond_g0 = DirichletBVP(x0, u00, x1, u10) cond_g1 = DirichletBVP(x0, u01, x1, u11) f0 = lambda y: cond_f0.enforce(net_f0, y) f1 = lambda y: cond_f1.enforce(net_f1, y) g0 = lambda x: cond_g0.enforce(net_g0, x) g1 = lambda x: cond_g1.enforce(net_g1, x) # test whether condition is enforced condition = DirichletBVP2D(x0, f0, x1, f1, y0, g0, y1, g1) net = FCNN(2, 1) x = x0 * ones y = torch.linspace(y0, y1, ones.numel(), requires_grad=True).reshape(-1, 1) assert all_close(condition.enforce(net, x, y), f0(y)), "left boundary not satisfied" x = x1 * ones y = torch.linspace(y0, y1, ones.numel(), requires_grad=True).reshape(-1, 1) assert all_close(condition.enforce(net, x, y), f1(y)), "right boundary not satisfied" x = torch.linspace(x0, x1, ones.numel(), requires_grad=True).reshape(-1, 1) y = y0 * ones assert all_close(condition.enforce(net, x, y), g0(x)), "lower boundary not satisfied" x = torch.linspace(x0, x1, ones.numel(), requires_grad=True).reshape(-1, 1) y = y1 * ones assert all_close(condition.enforce(net, x, y), g1(x)), "upper boundary not satisfied"
def test_dirichlet_bvp_2d(x0, x1, y0, y1, u00, u01, u10, u11, ones, net21, boundary_functions_2d): # set the boundary conditions on the four sides f0, f1, g0, g1 = boundary_functions_2d # test whether condition is enforced condition = DirichletBVP2D(x0, f0, x1, f1, y0, g0, y1, g1) x = x0 * ones y = torch.linspace(y0, y1, ones.numel(), requires_grad=True).reshape(-1, 1) assert all_close(condition.enforce(net21, x, y), f0(y)), "left boundary not satisfied" x = x1 * ones y = torch.linspace(y0, y1, ones.numel(), requires_grad=True).reshape(-1, 1) assert all_close(condition.enforce(net21, x, y), f1(y)), "right boundary not satisfied" x = torch.linspace(x0, x1, ones.numel(), requires_grad=True).reshape(-1, 1) y = y0 * ones assert all_close(condition.enforce(net21, x, y), g0(x)), "lower boundary not satisfied" x = torch.linspace(x0, x1, ones.numel(), requires_grad=True).reshape(-1, 1) y = y1 * ones assert all_close(condition.enforce(net21, x, y), g1(x)), "upper boundary not satisfied"
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, loss_history = 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', xy_noise_std=(0.01, 0.01)), batch_size=64) assert isinstance(solution_neural_net_laplace, Solution2D) assert isinstance(loss_history, dict) keys = ['train_loss', 'valid_loss'] for key in keys: assert key in loss_history assert isinstance(loss_history[key], list) assert len(loss_history[keys[0]]) == len(loss_history[keys[1]])
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)) )
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, hidden_units=(32, 32)) with pytest.raises(ValueError), pytest.warns(FutureWarning): solution_neural_net_laplace, _ = solve2D(pde=laplace, condition=bc, net=net, max_epochs=3, batch_size=64)