Add physical context to examples, and use backend-agnostic functions#2066
Add physical context to examples, and use backend-agnostic functions#2066vishnu-2206 wants to merge 7 commits intolululxvi:masterfrom
Conversation
| * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])) | ||
| + source_term_val | ||
| ) | ||
|
|
There was a problem hiding this comment.
Minimal changes, don't change any spaces not explicitly part of PR
| ∂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). |
There was a problem hiding this comment.
No need for this, this doesn't exist in other examples
|
|
||
| # 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])) |
There was a problem hiding this comment.
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.
|
Thank you for the guidance @echen5503. I have updated the PR to:
1. Revert the docstring and spacing to match the existing minimalist style
of the library.
2. Implement the source term using dde.backend to ensure consistency and
compatibility across all supported backends (TF, PyTorch, JAX, Paddle).
Let me know if any other adjustments are required.
…On Thu, 19 Mar 2026 at 19:19, Edwin Chen ***@***.***> wrote:
***@***.**** requested changes on this pull request.
------------------------------
In examples/pinn_forward/diffusion_1d.py
<#2066 (comment)>:
> )
+
Minimal changes, don't change any spaces not explicitly part of PR
------------------------------
In examples/pinn_forward/diffusion_1d.py
<#2066 (comment)>:
> @@ -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).
No need for this, this doesn't exist in other examples
------------------------------
In examples/pinn_forward/diffusion_1d.py
<#2066 (comment)>:
> @@ -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]))
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.
—
Reply to this email directly, view it on GitHub
<#2066 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPVR7W22GDEMIXVNK5V6QID4RP3FRAVCNFSM6AAAAACWXVW2JGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTSNZVGIYTIMBQHA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
| ∂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). |
There was a problem hiding this comment.
I meant just remove L9, not remove the whole thing. The rest is useful.
|
@vishnu-2206 it should look like this |
|
By only using deepXDE functions we can make examples backend-agnostic without casework. |
|
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 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: 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. |
|
You can just copy-paste the code I provided verbatim. In future PRs, please keep my comments in mind. |
|
Thank you for the guidance and the template. I have updated diffusion_1d.py
to use source_term_val and dde.backend to ensure it is fully
backend-agnostic as suggested.
Once you've had a chance to verify this structure, I will apply the exact
same logic to the other 1D diffusion examples (exactBC and resample) for
consistency. Ready for review!
…On Thu, 19 Mar 2026 at 22:29, Edwin Chen ***@***.***> wrote:
*echen5503* left a comment (lululxvi/deepxde#2066)
<#2066 (comment)>
You can just copy-paste the code I provided verbatim. In future PRs,
please keep my comments in mind.
—
Reply to this email directly, view it on GitHub
<#2066 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPVR7W7AKY3WESJLVMGHJ6T4RQRPNAVCNFSM6AAAAACWXVW2JGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DAOJRGY4TSMZXHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
Thank you, this looks good. You can modify the other files to have the same structure. |
|
Thank you for the feedback. Will modify the rest as well.
…On Thu, 19 Mar 2026 at 22:37, Edwin Chen ***@***.***> wrote:
*echen5503* left a comment (lululxvi/deepxde#2066)
<#2066 (comment)>
Thank you, this looks good. You can modify the other files to have the
same structure.
—
Reply to this email directly, view it on GitHub
<#2066 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPVR7W5YJ4SREFUMWZNASXD4RQSMVAVCNFSM6AAAAACWXVW2JGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DAOJRG43TMNRSHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
LGTM. Care to add physics docs to 1d_resample as well? |
|
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". |
|
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. |
echen5503
left a comment
There was a problem hiding this comment.
Just a small nitpick then we can merge this. Thanks for your help!
| 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 |
There was a problem hiding this comment.
| # Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
| # 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 |
There was a problem hiding this comment.
| # 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 |
|
Thank you for the review! I've removed the redundant backend comments and
ensured a cleaner structure across diffusion_1d.py, exactBC.py, and
resample.py. Ready for the final merge!
…On Mon, 23 Mar 2026 at 04:49, Edwin Chen ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Just a small nitpick then we can merge this. Thanks for your help!
------------------------------
In examples/pinn_forward/diffusion_1d.py
<#2066 (comment)>:
> @@ -18,34 +18,16 @@ 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
+ # Cross-backend source term
+ 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
⬇️ Suggested change
- # Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
------------------------------
In examples/pinn_forward/diffusion_1d_exactBC.py
<#2066 (comment)>:
> @@ -18,34 +18,16 @@ def pde(x, y):
# 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
+ # Cross-backend source term
+ 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
⬇️ Suggested change
- # Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
------------------------------
In examples/pinn_forward/diffusion_1d_resample.py
<#2066 (comment)>:
> @@ -18,36 +18,18 @@ def pde(x, y):
# 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
+ # Cross-backend source term
+ 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
⬇️ Suggested change
- # Backend tensorflow.compat.v1 or tensorflow, pytorch, jax, paddle
------------------------------
In examples/pinn_forward/diffusion_1d_exactBC.py
<#2066 (comment)>:
> @@ -63,14 +50,8 @@ 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])
+ # This works for TensorFlow, PyTorch, JAX, and Paddle
No need to say this.
------------------------------
In examples/pinn_forward/diffusion_1d.py
<#2066 (comment)>:
> @@ -18,34 +18,16 @@ 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
+ # Cross-backend source term
+ 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
No need to say this, because it is implied
—
Reply to this email directly, view it on GitHub
<#2066?email_source=notifications&email_token=BPVR7W2I6OZYNTKSRBWBGIT4SBYIHA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTGOJXGY2TANZTGQ22M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#pullrequestreview-3976507345>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPVR7W6AUMGGCSUFREFC67T4SBYIHAVCNFSM6AAAAACWXVW2JGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTSNZWGUYDOMZUGU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
@lululxvi looks ready for merge. Please credit @vishnu2206 (first), and @echen5503 (second). |
|
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! |
6ca275d to
0ea5d74
Compare
|
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. |
|
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>
0ea5d74 to
b2f54be
Compare
|
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. |
|
Looks good. |
As a physicist working with PINNs, I noticed that the 1D diffusion example lacked a physical description of the source term.
I have :
pdefunction to clearly identify the forcing termThis makes the example more accessible for students and researchers using DeepXDE for scientific modeling.