Exemple #1
0
def loss_func(input: torch.Tensor):
    ts = ansatz(input=input, net=net(input))

    ones = torch.unsqueeze(torch.ones(len(input), dtype=laplace_v3.DTYPE, device=device), 1)
    ts_d = torch.autograd.grad(ts, input, create_graph=True, grad_outputs=ones)[0]

    ts_dx = ts_d[:, 0:1]
    ts_dy = ts_d[:, 1:2]

    ts_dxx = torch.autograd.grad(ts_dx, input, create_graph=True, grad_outputs=ones, )
    ts_dxx = (ts_dxx[0])[:, 0:1]

    ts_dyy = (torch.autograd.grad(ts_dy, input, create_graph=True, grad_outputs=ones, ))
    ts_dyy = (ts_dyy[0])[:, 1:2]

    ts_laplace_error = ts_dxx + ts_dyy

    return torch.sum(ts_laplace_error ** 2) / len(ts_laplace_error)
def check_net(net):
    steps = 16
    lin_space = np.linspace(0, 1, steps)
    # zPredicted = np.zeros((steps, steps))
    zNet = np.zeros((steps, steps))
    zSolution = np.zeros((steps, steps))
    for ix in range(steps):
        for iy in range(steps):
            x = torch.tensor(lin_space[ix], dtype=laplace_v3.DTYPE)
            y = torch.tensor(lin_space[iy], dtype=laplace_v3.DTYPE)
            input = torch.tensor([[x, y]],
                                 dtype=laplace_v3.DTYPE,
                                 device=device)
            netxy = ansatz(input=input, net=net(input))
            zNet[iy][ix] = netxy.cpu()
            # zPredicted[iy][ix] = trial_solution(x, y, netxy)
            zSolution[iy][ix] = solution(x, y)

    levels = np.linspace(zSolution.min(), zSolution.max(), 100)
    cs = plt.contourf(lin_space,
                      lin_space,
                      zSolution,
                      levels=levels,
                      cmap='rainbow',
                      extend='both')
    plt.colorbar(cs)
    plt.title('analytical solution')
    plt.savefig('solution.svg')
    plt.savefig('solution.png')
    plt.show()

    levels = np.linspace(zNet.min(), zNet.max(), 100)
    cs = plt.contourf(lin_space,
                      lin_space,
                      zNet,
                      levels=levels,
                      cmap='rainbow',
                      extend='both')
    plt.colorbar(cs)
    plt.title('network')
    plt.savefig('network.svg')
    plt.savefig('network.png')
    plt.show()

    # levels = np.linspace(zPredicted.min(), zPredicted.max(), 100)
    # cs = plt.contourf(
    #     lin_space,
    #     lin_space,
    #     zPredicted,
    #     levels=levels,
    #     cmap='rainbow',
    #     extend='both')
    # plt.colorbar(cs)
    # plt.title('prediction')
    # plt.savefig('prediction.svg')
    # plt.savefig('prediction.png')
    # plt.show()

    zDifference = np.abs(zNet - zSolution)
    zDifferenceRel = np.divide(np.abs(zNet - zSolution), zSolution)

    levels = np.linspace(zDifference.min(), zDifference.max(), 100)
    cs = plt.contourf(lin_space,
                      lin_space,
                      zDifference,
                      levels=levels,
                      cmap='rainbow',
                      extend='both')
    plt.colorbar(cs)
    plt.title('difference to analytical solution')
    plt.savefig('difference.svg')
    plt.savefig('difference.png')
    plt.show()

    print("Max difference: " + str(zDifference.max()))
    print("Min difference: " + str(zDifference.min()))
    print("Avg difference: " + str(np.mean(zDifference)))

    zDifferenceRelCleaned = list(
        filter(lambda r: not np.isinf(r), zDifferenceRel.flatten()))
    print("Max rel. difference: " + str(max(zDifferenceRelCleaned)))
    print("Min rel. difference: " + str(min(zDifferenceRelCleaned)))
    print("Avg rel. difference: " + str(np.mean(zDifferenceRelCleaned)))