PINN as a PDE Solver

By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

# 1. Lab 3: Euler Beam (Solid Mechanics)Â¶

## 1.1. Problem SetupÂ¶

• We will solve a Euler beam problem:

• Problem properties
$$E = 1 \operatorname{pa}, \quad I = 1 \operatorname{kg\cdot m^2}, \quad q = 1 \operatorname{N/m}, \quad l = 1 \operatorname{m}$$

• Partial differential equations & boundary conditions

$${\partial^4 y \over \partial x^4} + 1 = 0, \qquad \text{where} \quad x \in [0,1]$$

• One Dirichlet boundary condition on the left boundary:

$$y(0) = 0$$

• One Neumann boundary condition on the left boundary:

$$y'(0) = 0$$

• Two boundary conditions on the right boundary:

$$y''(1) = 0, \quad y'''(1) = 0$$

• The exact solution is

$$y(x) = -{1 \over 24}x^4 + {1 \over 6}x^3 - {1 \over 4}x^2$$

• Make a neural network and loss functions like below :

## 1.2. Solve the Euler Beam problemÂ¶

InÂ [Â ]:
!pip install deepxde

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting deepxde
|â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆ| 132 kB 31.9 MB/s
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from deepxde) (1.4.1)
Collecting scikit-optimize
|â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆ| 100 kB 11.6 MB/s
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from deepxde) (1.21.6)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from deepxde) (3.2.2)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from deepxde) (1.0.2)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deepxde) (0.11.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deepxde) (3.0.9)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deepxde) (2.8.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deepxde) (1.4.2)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from kiwisolver>=1.0.1->matplotlib->deepxde) (4.2.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.1->matplotlib->deepxde) (1.15.0)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->deepxde) (1.1.0)
Collecting pyaml>=16.9
Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from pyaml>=16.9->scikit-optimize->deepxde) (3.13)
Installing collected packages: pyaml, scikit-optimize, deepxde
Successfully installed deepxde-1.4.0 pyaml-21.10.1 scikit-optimize-0.9.0

InÂ [Â ]:
# from deepxde.backend.set_default_backend import set_default_backend
# set_default_backend("tensorflow")


### 1.2.1. Import LibraryÂ¶

InÂ [Â ]:
import deepxde as dde
import numpy as np

Deepxde backend not selected or invalid. Assuming tensorflow.compat.v1 for now.
Using backend: tensorflow.compat.v1


Setting the default backend to "tensorflow.compat.v1". You can change it in the ~/.deepxde/config.json file or export the DDEBACKEND environment variable. Valid options are: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle (all lowercase)
WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term



### 1.2.2. Define PDE SystemÂ¶

InÂ [Â ]:
def dy(x, y):

def ddy(x, y):

def dddy(x, y):

def pde(x, y):
dy_xx = ddy(x, y)
return dy_xxxx + 1


### 1.2.3. Define Boundary ConditionÂ¶

InÂ [Â ]:
def boundary_left(x, on_boundary):
return on_boundary and np.isclose(x[0], 0)

def boundary_right(x, on_boundary):
return on_boundary and np.isclose(x[0], 1)


### 1.2.4. Define Geometry, Implement Boundary ConditionÂ¶

InÂ [Â ]:
geom = dde.geometry.Interval(0, 1)

bc1 = dde.DirichletBC(geom, lambda x: 0, boundary_left) # u(0) = 0
bc2 = dde.OperatorBC(geom, lambda x, y, _: dy(x, y), boundary_left) # u'(0) = 0
bc3 = dde.OperatorBC(geom, lambda x, y, _: ddy(x, y), boundary_right) # u''(1) = 0
bc4 = dde.OperatorBC(geom, lambda x, y, _: dddy(x, y), boundary_right) # u'''(1) = 0

# Reference solution to compute the error
def true_solution(x):
return -(x ** 4) / 24 + x ** 3 / 6 - x ** 2 / 4

data = dde.data.PDE(geom,
pde,
[bc1, bc2, bc3, bc4],
num_domain = 10,
num_boundary = 2,
solution = true_solution,
num_test = 100)

/usr/local/lib/python3.7/dist-packages/skopt/sampler/sobol.py:250: UserWarning: The balance properties of Sobol' points require n to be a power of 2. 0 points have been previously generated, then: n=0+12=12.
total_n_samples))


### 1.2.5. Define Network and Hyper-parametersÂ¶

InÂ [Â ]:
layer_size = [1] + [20] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"

net = dde.maps.FNN(layer_size, activation, initializer)

InÂ [Â ]:
model = dde.Model(data, net)

Compiling model...
Building feed-forward neural network...
'build' took 0.061704 s


/usr/local/lib/python3.7/dist-packages/deepxde/nn/tensorflow_compat_v1/fnn.py:110: UserWarning: tf.layers.dense is deprecated and will be removed in a future version. Please use tf.keras.layers.Dense instead.
kernel_constraint=self.kernel_constraint,
/usr/local/lib/python3.7/dist-packages/keras/legacy_tf_layers/core.py:261: UserWarning: layer.apply is deprecated and will be removed in a future version. Please use layer.__call__ method instead.
return layer.apply(inputs)

'compile' took 6.221793 s



### 1.2.6. Train & PredictionÂ¶

InÂ [Â ]:
losshistory, train_state = model.train(epochs = 5000)
dde.saveplot(losshistory, train_state, issave = False, isplot = True)

Initializing variables...
Training model...

Step      Train loss                                            Test loss                                             Test metric
0         [4.03e-01, 0.00e+00, 7.41e-02, 2.80e-02, 3.91e-03]    [3.61e-01, 0.00e+00, 7.41e-02, 2.80e-02, 3.91e-03]    []
1000      [7.80e-04, 1.59e-08, 7.75e-08, 3.03e-06, 1.07e-06]    [6.18e-04, 1.59e-08, 7.75e-08, 3.03e-06, 1.07e-06]    []
2000      [4.11e-05, 1.14e-10, 1.63e-09, 1.90e-08, 2.45e-08]    [3.38e-05, 1.14e-10, 1.63e-09, 1.90e-08, 2.45e-08]    []
3000      [2.34e-05, 4.68e-11, 1.46e-09, 5.34e-09, 1.07e-08]    [1.69e-05, 4.68e-11, 1.46e-09, 5.34e-09, 1.07e-08]    []
4000      [1.51e-05, 4.16e-09, 3.13e-09, 7.29e-09, 7.22e-08]    [1.01e-05, 4.16e-09, 3.13e-09, 7.29e-09, 7.22e-08]    []
5000      [9.67e-05, 2.50e-06, 3.95e-07, 4.49e-07, 6.45e-06]    [8.98e-05, 2.50e-06, 3.95e-07, 4.49e-07, 6.45e-06]    []

Best model at step 4000:
train loss: 1.52e-05
test loss: 1.02e-05
test metric: []

'train' took 18.766083 s



# 2. Lab 4: Navier-Stokes Equations (Fluid Mechanics)Â¶

• Note: strongly recommend you to use GPU rather than CPU. (you can use CoLab GPU for free)
InÂ [Â ]:
# !pip install deepxde

InÂ [Â ]:
import deepxde as dde
import numpy as np
import matplotlib.pyplot as plt

InÂ [Â ]:
# from deepxde.backend.set_default_backend import set_default_backend
# set_default_backend("tensorflow")


## 2.1. Problem SetupÂ¶

• We will solve 2D Navier-Stokes Equations to find velocity profile in infinite parallel plates flow
• Any fluid flowing in plates had to enter at some location. The region of flow near where the fluid enters the plates is termed the entrance region and is illustrated in below figure
• The fluid typically enters the plates with a nearly uniform velocity profile
• As the fluid moves through the plates, viscous effects cause it to stick to the plates wall (no-slip boundary condition)
• Thus, a boundary layer is produced along the plates wall such that the initial velocity profile changes with distance along the plates, $x$, until the fluid reaches the end of the hydrodynamic entrance region (which is also called entrance length) beyond which the velocity profile does not vary with $x$

• Problem properties

$$\rho = 1\operatorname{kg/m^3}, \quad \mu = 1\operatorname{N\cdot s/m^2}, \quad D = 2h = 1\operatorname{m}, \quad L = 2\operatorname{m}, \quad u_{in} = 1\operatorname{m/s}, \quad \nu = \frac{\mu}{\rho}$$

• Hydraulic diameter is

$$\quad D_h = \lim\limits_{b\to\infty} {4(2bh) \over {2b+4h}} = 4h = 2\operatorname{m}$$

• So, the Reynolds number of this system is

$$Re = \frac{\rho u_{in} D_h}{\mu} = 2$$

• 2D Navier-Stokes Equations & boundary conditions (for steady state)

\begin{align*} u{\partial u \over \partial x} + v{\partial u \over \partial y} + {1 \over \rho}{\partial p \over \partial x} - \nu \ \left({\partial^2 u \over {\partial x^2}} + {\partial^2 u \over {\partial y^2}}\right) &= 0\\\\ u{\partial v \over \partial x} + v{\partial v \over \partial y} + {1 \over \rho}{\partial p \over \partial y} - \nu \ \left({\partial^2 v \over {\partial x^2}} + {\partial^2 v \over {\partial y^2}}\right) &= 0\\\\ {\partial u \over \partial x} + {\partial v \over \partial y} &= 0 \end{align*}

• Two Dirichlet boundary conditions on the plate boundary (no-slip condition),

$$u(x,y) = 0, \quad v(x,y) = 0 \qquad \text{at} \quad y = \frac{D}{2} \ \; \text{or} \; -\frac{D}{2}$$

• Two Dirichlet boundary conditions at the inlet boundary

$$u(-1,y) = u_{\text{in}}, \quad v(-1,y) = 0$$

• Two Dirichlet boundary conditions at the outlet boundary

$$p(1,y) = 0, \quad v(1,y) = 0$$

• Make a neural network and loss functions like below :

## 2.2. CFD SolutionÂ¶

• CFD solution of this problem is illustrated in below figures
• Velocity u and velocity v, and pressure p, respectively
• Solve this problem using PINN and then compare with CFD solutions