1D Diffusion Eq
#1512
Replies: 1 comment
-
|
Yes, search Issues and Discussions, and you will find the solution. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Dear Dr.lulu,
I have this code that is for 1D heat diffusion eq with homogenous domain. I want to divide the domain into two domains and assign to each domain a different value for the diffusion. Is it possible to introduce to the model an interface boundary condition to ensure a smooth transition between two domain with deepxde ?
import deepxde as dde
import numpy as np
import random
def gen_testdata():
"""Generate synthetic test data."""
t = np.linspace(0, 1, 100) # Time values from 0 to 1
x = np.linspace(0, 1, 100) # Spatial values from 0 to 1
xx, tt = np.meshgrid(x, t)
X = np.vstack((np.ravel(xx), np.ravel(tt))).T
Problem parameters:
a = 1
L = 1 # Length of the bar
n = 1 # Frequency of the sinusoidal initial conditions
def pde(x, y):
"""Expresses the PDE residual of the heat equation."""
dy_t = dde.grad.jacobian(y, x, i=0, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
return dy_t - a * dy_xx
Computational geometry:
geom = dde.geometry.Interval(0, L)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
Initial and boundary conditions:
ic = dde.IC(
geomtime, lambda x: np.zeros_like(x[:, 0:1]), lambda _, on_initial: on_initial
)
bc_left = dde.DirichletBC(geomtime, lambda x: 0, lambda x, _: np.isclose(x[0], 0))
bc_right = dde.DirichletBC(geomtime, lambda x: 1, lambda x, _: np.isclose(x[0], 1))
Define the PDE problem and configurations of the network:
data = dde.data.TimePDE(
geomtime,
pde,
[bc_left, bc_right, ic],
num_domain=2400,
num_boundary=275,
num_initial=290,
num_test=2400,
)
net = dde.nn.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
model = dde.Model(data, net)
Build and train the model:
model.compile("adam", lr=1e-3)
model.train(iterations=20000)
model.compile("L-BFGS")
losshistory, train_state = model.train()
model.save("trained_model.h5")
Plot/print the results
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
X = gen_testdata()[0] # Get only the input features
Generate predictions and calculate mean residual
y_pred = model.predict(X)
f = model.predict(X, operator=pde)
mean_residual = np.mean(np.absolute(f))
print("Mean residual:", mean_residual)
np.savetxt("predictions.dat", np.hstack((X, y_pred))) # Save only the predictions
Beta Was this translation helpful? Give feedback.
All reactions