Solving euler beam by pinns

username: fedele_Morrone
cooked: <p>Hi, I’m new to using PINNs, and I’m attempting to solve the Euler beam equation with them. However, I’m unable to achieve a similar solution to the analytical, and I can’t understand where I’ve gone wrong. Therefore, I’m reaching out for help. I suspect that the issue may lie in how I’ve calculated the gradients, or perhaps I need to normalize something for my model to work properly.</p>
<p>The problem is d4w/dx4=q(x), with BC w(0)=0,w’(0)=0, w’‘(L)=0, w’‘’(L)=0 [ A simple clamped  beam]<br>
.<br>
Here is my code</p>
<p>from itertools import filterfalse<br>
from pickle import FALSE<br>
from tkinter import FLAT<br>
import torch<br>
import torch.nn as nn<br>
from torch.autograd import Variable<br>
import numpy as np<br>
import matplotlib.pyplot as plt</p>
<h1><a name="so-we-solve-d4wdx4-qx0-1" class="anchor" href="#so-we-solve-d4wdx4-qx0-1"></a>So we solve d4w/dx4-q(x)=0</h1>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
print(f"Using {device} device")</p>
<h1><a name="defining-class-to-create-neural-networks-from-torchnnmodules-as-nn-2" class="anchor" href="#defining-class-to-create-neural-networks-from-torchnnmodules-as-nn-2"></a>Defining class to create neural networks from torch.nn.Modules as nn</h1>
<p>class NeuralNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.flatten=nn.Flatten()<br>
self.linear_stack = nn.Sequential(<br>
nn.Linear(1,10), # Send one input x<br>
nn.Tanh(),<br>
nn.Linear(10, 10),<br>
nn.Tanh(),<br>
nn.Linear(10, 10),<br>
nn.Tanh(),<br>
nn.Linear(10, 10),<br>
nn.Tanh(),<br>
nn.Linear(10, 1),# retunr one output y<br>
)</p>
<pre><code>def forward(self,x):
    
    x=self.flatten(x)
    return self.linear_stack(x)
</code></pre>
<p>model=NeuralNetwork().to(device)</p>
<p>print(model)</p>
<h1><a name="define-loss-estimate-errore-to-minimize-3" class="anchor" href="#define-loss-estimate-errore-to-minimize-3"></a>define loss estimate errore to minimize</h1>
<p>lossd=nn.MSELoss()</p>
<h1><a name="define-optimizer-alghoritm-4" class="anchor" href="#define-optimizer-alghoritm-4"></a>define optimizer alghoritm</h1>
<p>optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)</p>
<h1><a name="define-routine-of-training-5" class="anchor" href="#define-routine-of-training-5"></a>define routine of training</h1>
<h1><a name="bc-6" class="anchor" href="#bc-6"></a>BC</h1>
<p>x0=torch.tensor(np.array([[0.0001]])).float()<br>
print(x0.size())<br>
x0.requires_grad=True<br>
xl=torch.tensor(np.array([[1]])).float()<br>
print(xl.size())<br>
xl.requires_grad=True<br>
print(x0.size())<br>
print(xl.size())</p>
<p>def pde(x,model):</p>
<pre><code>u=model(x) # compute prediction by net
u.size()
u_x  = torch.autograd.grad(u, x, torch.ones_like(u),retain_graph=True, create_graph=True)[0]

u_xx  = torch.autograd.grad(u_x, x, torch.ones_like(u_x), retain_graph=True,create_graph=True)[0]

u_xxx  = torch.autograd.grad(u_xx, x, torch.ones_like(u_xx), retain_graph=True,create_graph=True)[0]
 
u_xxxx  = torch.autograd.grad(u_xxx, x, torch.ones_like(u_xxx),retain_graph=True, create_graph=True)[0]

return u,u_x,u_xx,u_xxx,u_xxxx
</code></pre>
<p>E = 1  # Young’s modulus ¶<br>
I = 1   # Moment of inertia (m^4)<br>
q0=-0.001<br>
xl.to(device)<br>
<span class="hashtag-raw">#print</span>(xl.size())<br>
x0.to(device)</p>
<h1><a name="compute-derivate-to-impose-bc-7" class="anchor" href="#compute-derivate-to-impose-bc-7"></a>Compute derivate to impose bC</h1>
<p><span class="hashtag-raw">#generate</span> collocation point<br>
X = np.linspace(0.0001,1,num=100)<br>
X=np.reshape(X,(-1,1))<br>
qxx=np.ones_like(X)*q0<br>
qx=torch.from_numpy(qxx).float()<br>
qx.require_grad=False<br>
qx.to(device)</p>
<pre><code>#print(qx.require_grad)
#print(qx.size())
</code></pre>
<p>X=torch.tensor(X, requires_grad=True).float().to(device)<br>
nepoch=20000<br>
stop=True<br>
epoch=1<br>
while stop:<br>
optimizer.zero_grad()<br>
# Valutate model on BC<br>
bc1,bc2,<em>,</em>,_=pde(x0,model) # u’0=0<br>
<em>,</em>,bc3,bc4,bbb=pde(xl,model) # u(l)‘’=0</p>
<pre><code>#print(X.size())
_,_,_,_,f=pde(X,model) 
#print(f.size())
#print(qx.size())
#print(uxxxx)
loss2=torch.mean(bc1**2)+torch.mean(bc2**2)+torch.mean(bc3**2)+torch.mean(bc4**2)
#print(f)
#print(bbb)
loss1=lossd(f,qx)
loss=loss1+loss2
loss.backward()


optimizer.step() # This is equivalent to : theta_new = theta_old - alpha * derivative of J w.r.t theta
with torch.autograd.no_grad():
	print(f"Traning Loss: {epoch}", loss.data)
epoch+=+1
if loss&lt;1e-6:
    stop=False
</code></pre>
<p><span class="hashtag-raw">#y</span>,yx,yxx,yxxx,yxxxx=pde(X,model)<br>
model.eval()</p>
<p>yh = model(X).detach().cpu().numpy().reshape(-1,1)<br>
x=np.linspace(0.001,1,num=100)<br>
print(yh)<br>
L=1</p>
<p>yhat = q0<em>x**2/(24</em>E<em>I)</em>(6<em>L**2-4</em>L*x+x**2)</p>
<p>plt.figure(figsize = (10,8))<br>
plt.plot(x,yh,‘-k’)<br>
plt.plot(x,yhat,‘-r’)</p>
<p>plt.show()</p>