Skip to content
44 changes: 15 additions & 29 deletions examples/pinn_forward/diffusion_1d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle"""
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle
1D Diffusion Equation with a Time-Dependent Source Term.

This example solves the heat equation:
∂y/∂t - ∂²y/∂x² = f(x, t)
where the source term f(x, t) is chosen such that the analytical solution is y = e^(-t) * sin(πx).
"""
import deepxde as dde
import numpy as np
# Backend tensorflow.compat.v1 or tensorflow
Expand All @@ -15,37 +21,17 @@ def pde(x, y):
# Most backends
dy_t = dde.grad.jacobian(y, x, j=1)
dy_xx = dde.grad.hessian(y, x, j=0)
# Backend jax
# dy_t, _ = dde.grad.jacobian(y, x, j=1)
# dy_xx, _ = dde.grad.hessian(y, x, j=0)
# Backend tensorflow.compat.v1 or tensorflow

# Physics Note: The following term is the forced heat source f(x, t)
# required to satisfy the analytical solution y = e^(-t)sin(πx).
source_term_val = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
return (
dy_t
- dy_xx
+ tf.exp(-x[:, 1:])
* (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1]))
)
# Backend pytorch
# return (
# dy_t
# - dy_xx
# + torch.exp(-x[:, 1:])
# * (torch.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * torch.sin(np.pi * x[:, 0:1]))
# )
# Backend jax
# return (
# dy_t
# - dy_xx
# + jnp.exp(-x[:, 1:])
# * (jnp.sin(np.pi * x[..., 0:1]) - np.pi ** 2 * jnp.sin(np.pi * x[..., 0:1]))
# )
# Backend paddle
# return (
# dy_t
# - dy_xx
# + paddle.exp(-x[:, 1:])
# * (paddle.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * paddle.sin(np.pi * x[:, 0:1]))
# )
+ source_term_val
)


def func(x):
Expand Down
51 changes: 15 additions & 36 deletions examples/pinn_forward/diffusion_1d_exactBC.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle"""
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle
1D Diffusion Equation with a Time-Dependent Source Term.

This example solves the heat equation:
∂y/∂t - ∂²y/∂x² = f(x, t)
where the source term f(x, t) is chosen such that the analytical solution is y = e^(-t) * sin(πx).
"""
import deepxde as dde
import numpy as np
# Backend tensorflow.compat.v1 or tensorflow
Expand All @@ -15,37 +21,17 @@ def pde(x, y):
# Most backends
dy_t = dde.grad.jacobian(y, x, i=0, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
# Backend jax
# dy_t, _ = dde.grad.jacobian(y, x, i=0, j=1)
# dy_xx, _ = dde.grad.hessian(y, x, i=0, j=0)
# Backend tensorflow.compat.v1 or tensorflow

# Physics Note: The following term is the forced heat source f(x, t)
# required to satisfy the analytical solution y = e^(-t)sin(πx).
source_term_val = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
return (
dy_t
- dy_xx
+ tf.exp(-x[:, 1:])
* (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1]))
+ source_term_val
)
# Backend pytorch
# return (
# dy_t
# - dy_xx
# + torch.exp(-x[:, 1:])
# * (torch.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * torch.sin(np.pi * x[:, 0:1]))
# )
# Backend jax
# return (
# dy_t
# - dy_xx
# + jnp.exp(-x[:, 1:])
# * (jnp.sin(np.pi * x[..., 0:1]) - np.pi ** 2 * jnp.sin(np.pi * x[..., 0:1]))
# )
# Backend paddle
# return (
# dy_t
# - dy_xx
# + paddle.exp(-x[:, 1:])
# * (paddle.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * paddle.sin(np.pi * x[:, 0:1]))
# )


def func(x):
Expand All @@ -63,14 +49,7 @@ def func(x):
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)
net.apply_output_transform(
# Backend tensorflow.compat.v1 or tensorflow
lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + tf.sin(np.pi * x[:, 0:1])
# Backend pytorch
# lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + torch.sin(np.pi * x[:, 0:1])
# Backend jax
# lambda x, y: x[..., 1:2] * (1 - x[..., 0:1] ** 2) * y + jnp.sin(np.pi * x[..., 0:1])
# Backend paddle
# lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + paddle.sin(np.pi * x[:, 0:1])
lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + dde.backend.sin(np.pi * x[:, 0:1])
)

model = dde.Model(data, net)
Expand Down
50 changes: 20 additions & 30 deletions examples/pinn_forward/diffusion_1d_resample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle"""
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle
1D Diffusion Equation with Adaptive Point Resampling.

This example solves the heat equation using the PDEPointResampler callback:
∂y/∂t - ∂²y/∂x² = f(x, t)
Analytical solution: y = e^(-t) * sin(πx).
"""
import deepxde as dde
import numpy as np
# Backend tensorflow.compat.v1 or tensorflow
Expand All @@ -15,39 +21,19 @@ def pde(x, y):
# Most backends
dy_t = dde.grad.jacobian(y, x, i=0, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
# Backend jax
# dy_t, _ = dde.grad.jacobian(y, x, i=0, j=1)
# dy_xx, _ = dde.grad.hessian(y, x, i=0, j=0)
# Backend tensorflow.compat.v1 or tensorflow

# Physics Note: The following term is the forced heat source f(x, t)
# required to satisfy the analytical solution y = e^(-t)sin(πx).
source_term_val = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
return (
dy_t
- dy_xx
+ tf.exp(-x[:, 1:])
* (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1]))
+ source_term_val
)
# Backend pytorch
# return (
# dy_t
# - dy_xx
# + torch.exp(-x[:, 1:])
# * (torch.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * torch.sin(np.pi * x[:, 0:1]))
# )
# Backend jax
# return (
# dy_t
# - dy_xx
# + jnp.exp(-x[:, 1:])
# * (jnp.sin(np.pi * x[..., 0:1]) - np.pi ** 2 * jnp.sin(np.pi * x[..., 0:1]))
# )
# Backend paddle
# return (
# dy_t
# - dy_xx
# + paddle.exp(-x[:, 1:])
# * (paddle.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * paddle.sin(np.pi * x[:, 0:1]))
# )




def func(x):
return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:])

Expand Down Expand Up @@ -77,7 +63,11 @@ def func(x):

model = dde.Model(data, net)

# Adaptive Resampling Callback
# Period=100 means every 100 iterations, the model redistributes
# the domain points to where the PDE residual is highest.
resampler = dde.callbacks.PDEPointResampler(period=100)

model.compile("adam", lr=0.001, metrics=["l2 relative error"])
losshistory, train_state = model.train(iterations=2000, callbacks=[resampler])

Expand Down