-
Notifications
You must be signed in to change notification settings - Fork 959
Add physical context to examples, and use backend-agnostic functions #2066
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
1508cad
64a176f
f1fbe3a
f355cb5
f0d08ae
5321b8f
b2f54be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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). | ||
| """ | ||
|
|
||
| import deepxde as dde | ||
| import numpy as np | ||
| # Backend tensorflow.compat.v1 or tensorflow | ||
|
|
@@ -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])) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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