Skip to content

Add physical context to examples, and use backend-agnostic functions#2066

Open
vishnu-2206 wants to merge 7 commits intolululxvi:masterfrom
vishnu-2206:doc-physics-diffusion-vsr
Open

Add physical context to examples, and use backend-agnostic functions#2066
vishnu-2206 wants to merge 7 commits intolululxvi:masterfrom
vishnu-2206:doc-physics-diffusion-vsr

Conversation

@vishnu-2206
Copy link
Copy Markdown

As a physicist working with PINNs, I noticed that the 1D diffusion example lacked a physical description of the source term.

I have :

  1. Added a detailed Module Docstring explaining the 1D Heat Equation context.
  2. Refactored the pde function to clearly identify the forcing term $f(x, t)$.
  3. Ensured that the source term is derived to satisfy the analytical solution $y = e^{-t} \sin(\pi x)$.

This makes the example more accessible for students and researchers using DeepXDE for scientific modeling.

Comment thread examples/pinn_forward/diffusion_1d.py Outdated
* (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

Comment thread examples/pinn_forward/diffusion_1d.py Outdated
∂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

Comment thread examples/pinn_forward/diffusion_1d.py Outdated

# 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.

@vishnu-2206
Copy link
Copy Markdown
Author

vishnu-2206 commented Mar 19, 2026 via email

Comment thread examples/pinn_forward/diffusion_1d.py Outdated
∂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.

I meant just remove L9, not remove the whole thing. The rest is useful.

@echen5503
Copy link
Copy Markdown
Contributor

echen5503 commented Mar 19, 2026

@vishnu-2206 it should look like this

"""
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

def pde(x, y):
    # Most backends
    dy_t = dde.grad.jacobian(y, x, j=1)
    dy_xx = dde.grad.hessian(y, x, j=0)
    
    # 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
        + source_term_val
    ) 

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


geom = dde.geometry.Interval(-1, 1)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)

bc = dde.icbc.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary)
ic = dde.icbc.IC(geomtime, func, lambda _, on_initial: on_initial)
data = dde.data.TimePDE(
    geomtime,
    pde,
    [bc, ic],
    num_domain=40,
    num_boundary=20,
    num_initial=10,
    solution=func,
    num_test=10000,
)

layer_size = [2] + [32] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)

model = dde.Model(data, net)

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

dde.saveplot(losshistory, train_state, issave=True, isplot=True)

@echen5503
Copy link
Copy Markdown
Contributor

By only using deepXDE functions we can make examples backend-agnostic without casework.

@echen5503
Copy link
Copy Markdown
Contributor

It seems you missed some of my comments in the previous iterations. The point of the PR is to add documentation and keep the code concise with dde.backend functions. However, I requested you to add back the documentation and you did not do so.

Secondly, please focus on one file at a time. We can fix others in future PRs. Changing more and more files makes the PR hard to review.

Finally, please make sure to clean your AI output. AI will write comments to denote where things have changed, but it is not necessary here.

To reiterate, your code should look like this:

"""
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

def pde(x, y):
    # Most backends
    dy_t = dde.grad.jacobian(y, x, j=1)
    dy_xx = dde.grad.hessian(y, x, j=0)
    
    # 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
        + source_term_val
    ) 

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


geom = dde.geometry.Interval(-1, 1)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)

bc = dde.icbc.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary)
ic = dde.icbc.IC(geomtime, func, lambda _, on_initial: on_initial)
data = dde.data.TimePDE(
    geomtime,
    pde,
    [bc, ic],
    num_domain=40,
    num_boundary=20,
    num_initial=10,
    solution=func,
    num_test=10000,
)

layer_size = [2] + [32] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)

model = dde.Model(data, net)

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

dde.saveplot(losshistory, train_state, issave=True, isplot=True)

In summary, please follow the format which I have described, and only change 1 file. Once this is merged, feel free to open a PR for the other 2.

@echen5503
Copy link
Copy Markdown
Contributor

You can just copy-paste the code I provided verbatim. In future PRs, please keep my comments in mind.

@vishnu-2206
Copy link
Copy Markdown
Author

vishnu-2206 commented Mar 19, 2026 via email

@echen5503
Copy link
Copy Markdown
Contributor

Thank you, this looks good. You can modify the other files to have the same structure.

@vishnu-2206
Copy link
Copy Markdown
Author

vishnu-2206 commented Mar 19, 2026 via email

@echen5503
Copy link
Copy Markdown
Contributor

LGTM. Care to add physics docs to 1d_resample as well?

@echen5503
Copy link
Copy Markdown
Contributor

echen5503 commented Mar 20, 2026

Once added, I think this will be ready to merge. Please update the title of the PR accordingly, to something like "Add physical context to examples, and use backend-agnostic functions".

@vishnu-2206 vishnu-2206 changed the title doc: add physical context and clarify 1D diffusion source term Add physical context to examples, and use backend-agnostic functions Mar 21, 2026
@vishnu-2206
Copy link
Copy Markdown
Author

I have updated the title and applied the same logic and structure to diffusion_1d_resample.py for consistency across the suite. These updates ensure physical accuracy and full compatibility with all DeepXDE backends by using dde.backend functions.

Copy link
Copy Markdown
Contributor

@echen5503 echen5503 left a comment

Choose a reason for hiding this comment

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

Just a small nitpick then we can merge this. Thanks for your help!

Comment thread examples/pinn_forward/diffusion_1d.py Outdated
f = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
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.

Suggested change
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle

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 to say this, because it is implied

f = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
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.

Suggested change
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle

f = dde.backend.exp(-x[:, 1:]) * (
dde.backend.sin(np.pi * x[:, 0:1]) - np.pi**2 * dde.backend.sin(np.pi * x[:, 0:1])
)
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
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.

Suggested change
# Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle

# 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])
# This works for TensorFlow, PyTorch, JAX, and Paddle
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 to say this.

@vishnu-2206
Copy link
Copy Markdown
Author

vishnu-2206 commented Mar 23, 2026 via email

@echen5503
Copy link
Copy Markdown
Contributor

echen5503 commented Mar 23, 2026

@lululxvi looks ready for merge.

Please credit @vishnu2206 (first), and @echen5503 (second).

@vishnu-2206
Copy link
Copy Markdown
Author

Thank you! Please credit @vishnu2206 as the primary author and @echen5503 for the guidance and review. I appreciate the help in getting these examples to the library standard!

@vishnu-2206 vishnu-2206 force-pushed the doc-physics-diffusion-vsr branch from 6ca275d to 0ea5d74 Compare March 31, 2026 18:07
@vishnu-2206
Copy link
Copy Markdown
Author

I have amended the commit to include the co-author trailers as requested, crediting @vishnu-2206 (primary) and @echen5503 (secondary). The metadata should now be correctly reflected in the PR. Ready for final review.

@echen5503
Copy link
Copy Markdown
Contributor

Hi, suggested code changes look good. But who is this Carrot-Bear account?

Co-authored-by: vishnu-2206 <48398436+vishnu-2206@users.noreply.github.com>
Co-authored-by: echen5503 <87834510+echen5503@users.noreply.github.com>
@vishnu-2206 vishnu-2206 force-pushed the doc-physics-diffusion-vsr branch from 0ea5d74 to b2f54be Compare April 3, 2026 05:11
@vishnu-2206
Copy link
Copy Markdown
Author

I have corrected my local Git configuration and amended the commit. The placeholder 'Carrot-Bear' has been replaced with my professional name, and the co-author metadata has been verified. The PR is now ready for your final review and merge.

@echen5503
Copy link
Copy Markdown
Contributor

Looks good.

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

Successfully merging this pull request may close these issues.

3 participants