Why my code works only at boundary conditions?

parisa71: <p>Hello everyone, I have written a code to solve the Laplace equation using Physics-Informed Neural Networks (PINNs) on a 10000*4 domain. the left and right boundary conditions are u(0, y) = 5 and u(10000, y) = 1, respectively.  When I run the code, the results are only displayed at the boundaries, but there are no contour lines inside the domain. Additionally, the top and bottom boundaries are specified as no-flow boundaries, but this is not reflected in the plot. Can anyone assist me in identifying the issue?<br>
here is my code:<br>
import numpy as np<br>
import matplotlib.pyplot as plt</p>
<p>import torch<br>
import torch.nn as nn</p>
<p>x_min, x_max, y_min, y_max = 0, 10000, 0, 4<br>
y_range, x_range =np.linspace(0,4,5)[1:-1] , np.linspace(0, 10000, 12)[1:-1]<br>
x_zeros = np.zeros(10)<br>
y_zeros = np.zeros(3)<br>
x_ones = np.ones(10)<br>
y_ones = np.ones(3)</p>
<p>bc_left = np.vstack([y_zeros, y_range, 5<em>y_ones]).T # Boundary left: h(x=0, y) = 5<br>
bc_right = np.vstack([10000</em>y_ones, y_range, y_ones]).T # Boundary right: u(x=10, y) = 1</p>
<p>boundary_conditions = np.vstack([bc_left, bc_right])<br>
x_bc = boundary_conditions[:, 0]<br>
y_bc = boundary_conditions[:, 1]<br>
u_bc = boundary_conditions[:, 2]</p>
<p>x_bc = torch.autograd.Variable(torch.from_numpy(x_bc).float(), requires_grad=True)<br>
y_bc = torch.autograd.Variable(torch.from_numpy(y_bc).float(), requires_grad=True)<br>
u_bc = torch.autograd.Variable(torch.from_numpy(u_bc).float(), requires_grad=True)<br>
x_range = torch.autograd.Variable(torch.from_numpy(x_range).float(), requires_grad=True)<br>
x_zeros = torch.autograd.Variable(torch.from_numpy(x_zeros).float(), requires_grad=True)<br>
x_ones = torch.autograd.Variable(torch.from_numpy(x_ones).float(), requires_grad=True)</p>
<p><span class="hashtag-raw">#collocation</span> points in the domain are 30 uniformly selected points</p>
<p>nx, ny = (30, 2)<br>
x = np.linspace(0, 10000, 12)[1:-1]<br>
y = np.linspace(0, 4, 5)[1:-1]<br>
xv, yv = np.meshgrid(x, y)<br>
a=np.reshape(xv, (30, 1))<br>
b=np.reshape(yv, (30, 1))<br>
r = np.hstack((a,b))<br>
x_collocation = r[:,0]<br>
y_collocation = r[:,1]<br>
x_collocation = torch.autograd.Variable(torch.from_numpy(x_collocation).float(), requires_grad=True)<br>
y_collocation = torch.autograd.Variable(torch.from_numpy(y_collocation).float(), requires_grad=True)</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.neurons_per_layer = 20<br>
self.fc1 = nn.Linear(2, self.neurons_per_layer)<br>
self.fc2 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc3 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc4 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc5 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc6 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc7 = nn.Linear(self.neurons_per_layer, self.neurons_per_layer)<br>
self.fc8 = nn.Linear(self.neurons_per_layer, 1)<br>
self.act_func = nn.Sigmoid()</p>
<pre><code>def forward(self, x, y):
    inputs = torch.cat([x.reshape(-1, 1), y.reshape(-1, 1)], axis=1)
    output = self.act_func(self.fc1(inputs))
    output = self.act_func(self.fc2(output))
    output = self.act_func(self.fc3(output))
    output = self.act_func(self.fc4(output))
    output = self.act_func(self.fc5(output))
    output = self.act_func(self.fc6(output))
    output = self.act_func(self.fc7(output))
    output = self.fc8(output)
    return output
</code></pre>
<p>net = Net()<br>
epochs = 1500<br>
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)<br>
criterion = torch.nn.MSELoss()</p>
<h1><a name="pde-as-a-loss-function-this-function-calculates-the-first-and-second-derivatives-then-the-pde-based-loss-1" class="anchor" href="#pde-as-a-loss-function-this-function-calculates-the-first-and-second-derivatives-then-the-pde-based-loss-1"></a>PDE as a loss function, this function calculates the first and second derivatives, then the PDE-based loss</h1>
<p>def f(x, y, net):<br>
u = net(x, y)<br>
u_x = torch.autograd.grad(u, x, create_graph=True, retain_graph=True, grad_outputs=torch.ones_like(u))[0]<br>
u_xx = torch.autograd.grad(u_x, x, create_graph=True, retain_graph=True, grad_outputs=torch.ones_like(u_x))[0]<br>
u_y = torch.autograd.grad(u, y, create_graph=True, retain_graph=True, grad_outputs=torch.ones_like(u))[0]<br>
u_yy = torch.autograd.grad(u_y, y, create_graph=True, retain_graph=True, grad_outputs=torch.ones_like(u_y))[0]<br>
loss_f = u_xx + u_yy<br>
return loss_f</p>
<p>def neumann_bc_loss(net, x, y, target_gradient_value):<br>
u = net(x, y)<br>
u_y = torch.autograd.grad(u, y, retain_graph=True, grad_outputs=torch.ones_like(u))[0]</p>
<pre><code>neumann_loss = torch.nn.functional.mse_loss(u_y, target_gradient_value * torch.ones_like(u_y))
return neumann_loss
</code></pre>
<p>neumann_loss_top = neumann_bc_loss(net, x_range, 4*x_ones, 0)<br>
neumann_loss_bottom = neumann_bc_loss(net, x_range, x_zeros, 0)</p>
<p>losses = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>for epoch in range(epochs):<br>
optimizer.zero_grad()<br>
predictions_initial_bc = net(x_bc, y_bc)<br>
mse_u = criterion(predictions_initial_bc.reshape(-1,), u_bc) # This is the loss from boundary and initial conditions<br>
f_out = f(x_collocation, y_collocation, net)<br>
mse_f = criterion(torch.zeros_like(f_out), f_out) # This is the PDE-based loss evaluated at the randomly sampled collocation points</p>
<pre><code>loss = mse_u + mse_f + neumann_loss_bottom + neumann_loss_top
losses.append(loss.item())
loss.backward()
optimizer.step()
if epoch % 100 == 0:


    print(f'Epoch {epoch}/{epochs}: Loss = {loss.item()}')
</code></pre>
<h1><a name="create-meshgrid-points-for-plotting-2" class="anchor" href="#create-meshgrid-points-for-plotting-2"></a>Create meshgrid points for plotting</h1>
<p>x_plot, y_plot = np.meshgrid(np.linspace(0, 10000, 100), np.linspace(0, 4, 100))</p>
<p>x_plot = torch.autograd.Variable(torch.from_numpy(np.reshape(x_plot, (-1, 1)).astype(np.float32)))</p>
<p>y_plot = torch.autograd.Variable(torch.from_numpy(np.reshape(y_plot, (-1, 1)).astype(np.float32)))</p>
<h1><a name="get-network-predictions-for-meshgrid-points-3" class="anchor" href="#get-network-predictions-for-meshgrid-points-3"></a>Get network predictions for meshgrid points</h1>
<p>net.eval()</p>
<p>u_values = net(x_plot, y_plot).detach().numpy().reshape(100, 100)</p>
<h1><a name="plotting-4" class="anchor" href="#plotting-4"></a>Plotting</h1>
<p>plt.figure(figsize=(10, 6))</p>
<p>plt.pcolormesh(x_plot[:,0].detach().numpy().reshape(100, 100), y_plot[:,0].detach().numpy().reshape(100, 100), u_values, shading=‘auto’, cmap=‘viridis’)</p>
<p>plt.colorbar()</p>
<p>plt.xlabel(‘x’)</p>
<p>plt.ylabel(‘y’)</p>
<p>plt.title(‘Surface Plot of u = net(x, y)’)</p>
<p>plt.show()</p>
<p>this is what I get:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/3/3331c6c7287362366fa884f9db6db731e5231da6.png" data-download-href="https://discuss.pytorch.org/uploads/default/3331c6c7287362366fa884f9db6db731e5231da6" title="plot111"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/3/3331c6c7287362366fa884f9db6db731e5231da6_2_690x467.png" alt="plot111" data-base62-sha1="7iSZMW6ygMYURgWJsiWtXxPObIi" width="690" height="467" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/3/3331c6c7287362366fa884f9db6db731e5231da6_2_690x467.png, https://discuss.pytorch.org/uploads/default/original/3X/3/3/3331c6c7287362366fa884f9db6db731e5231da6.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/3/3331c6c7287362366fa884f9db6db731e5231da6.png 2x" data-dominant-color="6D3C78"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">plot111</span><span class="informations">979×663 31 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>