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
with Drichlet boundary conditions for a rectangular domain from -1 to +1. but even after trying different combinations of network architectures i.e deeper and wider networks, different NNs such as FNN, ResNets and different combinations of optimizers and different distributions of points I cannot get the model to predict better. Can someone help me below is the code
import deepxde as dde
import numpy as np
import tensorflow as tf
def pde(x, y):
""" Function to compute PDE residual """
dy_xx = dde.grad.hessian(y, x, component=0, i=0, j=0)
dy_yy = dde.grad.hessian(y, x, component=0, i=1, j=1)
# The source term S, using TensorFlow operations
S = -(2 + tf.square(np.pi) * x[:, 0:1] * (1 - x[:, 0:1])) * tf.cos(tf.constant(np.pi) * x[:, 1:2])
return dy_xx + dy_yy - S
Define the computational domain
geom = dde.geometry.Rectangle([-1, -1], [1, 1])
Define hard enforcement for the boundary conditions using output transformation
def output_transform(x, output):
""" Custom output transformation function for enforcing hard boundary conditions. """
# Extract x and y coordinates
x_coord = x[:, 0:1] # x coordinate
y_coord = x[:, 1:2] # y coordinate
# Boundary conditions
phi_x1 = 0 * tf.ones_like(output) # phi = 0 at x = 1
phi_x_1 = -2 * tf.cos(np.pi * y_coord) # phi = -2cos(pi*y) at x = -1
phi_y1_y_1 = -x_coord * (1 - x_coord) # phi = -x(1-x) at y = 1 and y = -1
# Apply boundary conditions
# Using tf.where to selectively apply boundary conditions
output = tf.where(tf.equal(x_coord, 1), phi_x1, output)
output = tf.where(tf.equal(x_coord, -1), phi_x_1, output)
output = tf.where(tf.logical_or(tf.equal(y_coord, 1), tf.equal(y_coord, -1)), phi_y1_y_1, output)
return output
data = dde.data.PDE(geom, pde, [], num_domain=20000, num_boundary=1000 , solution= func , num_test=2000) # No explicit boundary conditions are passed here
!# net = dde.maps.ResNet(input_size=2, output_size=1, num_neurons=50, num_blocks=num_res_blocks, activation=activation, kernel_initializer=kernel_initializer)
net = dde.maps.FNN([2] + [100]*10 + [1], "tanh", "Glorot normal")
Apply the output transformation to the neural network
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
-
Hello,
I am trying to solve this pde
with Drichlet boundary conditions for a rectangular domain from -1 to +1. but even after trying different combinations of network architectures i.e deeper and wider networks, different NNs such as FNN, ResNets and different combinations of optimizers and different distributions of points I cannot get the model to predict better. Can someone help me below is the code
import deepxde as dde
import numpy as np
import tensorflow as tf
def pde(x, y):
""" Function to compute PDE residual """
dy_xx = dde.grad.hessian(y, x, component=0, i=0, j=0)
dy_yy = dde.grad.hessian(y, x, component=0, i=1, j=1)
# The source term S, using TensorFlow operations
S = -(2 + tf.square(np.pi) * x[:, 0:1] * (1 - x[:, 0:1])) * tf.cos(tf.constant(np.pi) * x[:, 1:2])
return dy_xx + dy_yy - S
Define the computational domain
geom = dde.geometry.Rectangle([-1, -1], [1, 1])
Define hard enforcement for the boundary conditions using output transformation
def output_transform(x, output):
""" Custom output transformation function for enforcing hard boundary conditions. """
# Extract x and y coordinates
x_coord = x[:, 0:1] # x coordinate
y_coord = x[:, 1:2] # y coordinate
def func(x):
return x[:,0:1]*(1-x[:,0:1])np.cos(np.pix[:,1:])
data = dde.data.PDE(geom, pde, [], num_domain=20000, num_boundary=1000 , solution= func , num_test=2000) # No explicit boundary conditions are passed here
!# net = dde.maps.ResNet(input_size=2, output_size=1, num_neurons=50, num_blocks=num_res_blocks, activation=activation, kernel_initializer=kernel_initializer)
net = dde.maps.FNN([2] + [100]*10 + [1], "tanh", "Glorot normal")
Apply the output transformation to the neural network
net.apply_output_transform(output_transform)
Combine the PDE and the boundary conditions
!# resampler = dde.callbacks.PDEPointResampler(period=10)
Model
model = dde.Model(data, net)
Train the model
model.compile("adam", lr=1e-3, metrics=["l2 relative error"])
model.train(iterations=8000)
model.compile("L-BFGS-B", metrics=["l2 relative error"])
losshistory, train_state = model.train()
Plotting the results
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
Here the what the solution should look like. The exact solution is x*(1-x)cos(piy)
Beta Was this translation helpful? Give feedback.
All reactions