Skip to content
25 changes: 21 additions & 4 deletions examples/pinn_forward/diffusion_1d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
"""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).

Physical context: This represents a 1D rod with a decaying heat source, where the ends are kept at zero temperature (Dirichlet Boundary Conditions).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, this doesn't exist in other examples

"""

import deepxde as dde
import numpy as np
# Backend tensorflow.compat.v1 or tensorflow
Expand All @@ -18,27 +28,34 @@ def pde(x, y):
# 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 = tf.exp(-x[:, 1:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1]))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, note that you are making the tensorflow code different than the other backends. Also, note that this isn't the main point of the PR. If you really want to make this change, make it consistent for all backends, or use dde.backend.sin, dde.backend.exp, etc.


# Backend tensorflow.compat.v1 or tensorflow
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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimal changes, don't change any spaces not explicitly part of PR

# 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
Expand Down