Replies: 1 comment
-
|
Then simply implement |
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.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello Dear Dr @lululxvi,
Thanks for the Deepxde.
I am trying to solve 1D heat diffusion eq with a heterogeneous domain using Deepxde. However, I tried a lot to solve it, but I couldn't. Can deepxde solve it?
This is the code for solving a 1D diffusion problem with a homogenous domain, and I want to make it with a heterogeneous domain by assigning two different values of the parameter a ( Diffusion) to each of the domains x in the code, but it is not working. Also, how is it possible to make the parameter "a" as function of x, increasing linearly, and put it in the loss function?
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 , I tried this code for the parameter a: a =x, but it is not working
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
y = np.sin(2 * np.pi * xx) # Example exact solution (sinusoidal)
return X, y
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=2412,
num_boundary=220,
num_initial=245,
num_test=2412,
)
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