Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fractional Poisson equation in 2D produces errors #1376

Closed
xuelanghanbao opened this issue Jul 8, 2023 · 0 comments
Closed

Fractional Poisson equation in 2D produces errors #1376

xuelanghanbao opened this issue Jul 8, 2023 · 0 comments

Comments

@xuelanghanbao
Copy link
Contributor

xuelanghanbao commented Jul 8, 2023

I tried to run the Fractional Poisson equation in 2D demo, but it produces the following error.

Traceback (most recent call last):
  File "deepxde\fractional_Poisson_2d.py", line 35, in <module>
    data = dde.data.FPDE(
  File "deepxde\deepxde\data\fpde.py", line 88, in __init__
    super().__init__(
  File "deepxde\deepxde\data\pde.py", line 127, in __init__
    self.train_next_batch()
  File "deepxde\deepxde\data\fpde.py", line 163, in train_next_batch
    X = self.frac_train.get_x()
  File "deepxde\deepxde\data\fpde.py", line 402, in get_x
    else self.get_x_dynamic()
  File "deepxde\deepxde\data\fpde.py", line 474, in get_x_dynamic
    self.w.append(array_ops_compat.hstack(wi))
  File "deepxde\deepxde\utils\array_ops_compat.py", line 25, in hstack
    if not is_tensor(tup[0]) and tup[0] == []:
ValueError: operands could not be broadcast together with shapes (172,) (0,)

I just changed the tf backend to deepxde.backend, as shown below:

import deepxde as dde
import numpy as np
import deepxde.backend as bkd
from scipy.special import gamma

alpha = 1.8

# Backend tensorflow.compat.v1
def fpde(x, y, int_mat):
    """\int_theta D_theta^alpha u(x)"""
    if isinstance(int_mat, (list, tuple)) and len(int_mat) == 3:
        int_mat = bkd.sparse_tensor(*int_mat)
        lhs = bkd.sparse_dense_matmul(int_mat, y)
    else:
        lhs = bkd.matmul(bkd.from_numpy(int_mat), bkd.from_numpy(y))
    lhs = lhs[:, 0]
    lhs *= gamma((1 - alpha) / 2) * gamma((2 + alpha) / 2) / (2 * np.pi ** 1.5)
    x = x[: bkd.size(lhs)]
    rhs = (
        2 ** alpha
        * gamma(2 + alpha / 2)
        * gamma(1 + alpha / 2)
        * (1 - (1 + alpha / 2) * bkd.from_numpy(np.sum( bkd.to_numpy(x ** 2),axis=1)))
    )
    return lhs - rhs

def func(x):
    return (np.abs(1 - np.linalg.norm(x, axis=1, keepdims=True) ** 2)) ** (
        1 + alpha / 2
    )

geom = dde.geometry.Disk([0, 0], 1)
bc = dde.icbc.DirichletBC(geom, func, lambda _, on_boundary: on_boundary)

data = dde.data.FPDE(
    geom, fpde, alpha, bc, [8, 100], num_domain=100, num_boundary=1, solution=func
)

net = dde.nn.FNN([2] + [20] * 4 + [1], "tanh", "Glorot normal")
net.apply_output_transform(
    lambda x, y: (1 - bkd.from_numpy(np.sum(bkd.to_numpy(x) ** 2, axis=1, keepdims=True))) * y
)

model = dde.Model(data, net)
model.compile("adam", lr=1e-3)
losshistory, train_state = model.train(iterations=20000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)

X = geom.random_points(1000)
y_true = func(X)
y_pred = model.predict(X)
print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))

I think, just replacing if not is_tensor(tup[0]) and tup[0] == []: with if not is_tensor(tup[0]) and isinstance(tup[0],list): can solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant