You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi , I just started with Deepxde and I m trying out some examples. Here I'm trying to solve the 1D time dependent heat equation with the following initial and boundary condtions.
I am encountering an error 'Auxiilary variable function not defined' at the model.compile().
I am not really able to figure it out what the issue is.
I am just new to this field and I would really appreciate if you are able point out what am I doing wrong here.
Here is the code:
import deepxde as dde
import numpy as np
from deepxde.backend import tf
# import tensorflow as tf
"""Returns the exact solution for a given x and t """
def heat_eq_exact_solution(x, t):
return np.exp(x + 2*t)
def gen_exact_solution():
"""Generates exact solution for the heat equation for the given values of x and t."""
# Number of points in each dimension:
x_dim, t_dim = (256, 201)
# Bounds of 'x' and 't':
x_min, t_min = (0, 0.0)
x_max, t_max = (L, 1.0)
# Create tensors:
t = np.linspace(t_min, t_max, num=t_dim).reshape(t_dim, 1)
x = np.linspace(x_min, x_max, num=x_dim).reshape(x_dim, 1)
usol = np.zeros((x_dim, t_dim)).reshape(x_dim, t_dim)
# Obtain the value of the exact solution for each generated point:
for i in range(x_dim):
for j in range(t_dim):
usol[i][j] = heat_eq_exact_solution(x[i], t[j])
# Save solution:
np.savez("heat_eq_data", x=x, t=t, usol=usol)
def gen_testdata():
"""Import and preprocess the dataset with the exact solution."""
# Load the data:
data = np.load("heat_eq_data.npz")
# Obtain the values for t, x, and the excat solution:
t, x, exact = data["t"], data["x"], data["usol"].T
# Process the data and flatten it out (like labels and features):
xx, tt = np.meshgrid(x, t)
X = np.vstack((np.ravel(xx), np.ravel(tt))).T
y = exact.flatten()[:, None]
return X, y
# Problem parameters:
a = 1.0 # Thermal diffusivity
L = 1.0 # Length of the bar
n = 1.0 # Frequency of the sinusoidal initial conditions
gen_exact_solution()
def func(x):
return np.exp(x[:,0:1] + 2*x[:,1:2])
"""Expresses the PDE residual of the heat equation."""
def pde(x, y, t):
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 - dy_xx - tf.exp(x[:,0:1] + 2*x[:,1:])
# Computational geometry:
geom = dde.geometry.Interval(0, L)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
#Initial and boundary conditions:
bc = dde.icbc.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary)
ic = dde.icbc.IC( geomtime, func, lambda _, on_initial: on_initial)
# Define the PDE problem and configurations of the network:
data = dde.data.TimePDE(
geomtime,
pde,
[bc,ic],
num_domain=2540,
num_boundary=80,
num_initial=160,
solution=func,
num_test=2540,
)
net = dde.nn.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
model = dde.Model(data, net)
# Build and train the model:
model.compile(optimizer="adam", lr=1e-3, loss_weights = None)
losshistory, train_state = model.train(iterations=15000)
model.compile("L-BFGS")
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
X, y_true = gen_testdata()
y_pred = model.predict(X)
f = model.predict(X, operator=pde)
print("Mean residual:", np.mean(np.absolute(f)))
print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))``
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi , I just started with Deepxde and I m trying out some examples. Here I'm trying to solve the 1D time dependent heat equation with the following initial and boundary condtions.
I am encountering an error 'Auxiilary variable function not defined' at the model.compile().
I am not really able to figure it out what the issue is.
I am just new to this field and I would really appreciate if you are able point out what am I doing wrong here.
Here is the code:
Beta Was this translation helpful? Give feedback.
All reactions