diff --git a/content/posts/2021-07-10-jeonsanyuceyeoghag-cfd-with-python-navier-stokes-equation.md b/content/posts/2021-07-10-jeonsanyuceyeoghag-cfd-with-python-navier-stokes-equation.md index 703b296..eec99fc 100644 --- a/content/posts/2021-07-10-jeonsanyuceyeoghag-cfd-with-python-navier-stokes-equation.md +++ b/content/posts/2021-07-10-jeonsanyuceyeoghag-cfd-with-python-navier-stokes-equation.md @@ -1,13 +1,10 @@ -+++ -date = 2021-07-10T08:23:47Z -description = "" -draft = false -slug = "jeonsanyuceyeoghag-cfd-with-python-navier-stokes-equation" -title = "[전산유체역학] CFD with Python (Navier-Stokes Equation)" - -+++ - - +--- +date: 2021-07-10T08:23:47.000Z +description: "" +draft: false +title: CFD with Python (Navier-Stokes Equation) +slug: fd-with-python-navier-stokes-equation +--- ## 1-D Linear Convection 1차원 선형 열전도 방정식은 가장 심플하면서도 가장 기초적인 방정식입니다. @@ -16,7 +13,7 @@ $$ \frac{\partial u}{\partial t} + c \frac{\partial u}{\partial x} = 0 $$ 이 식을 오일러 방정식으로 변환하여 수치해석적으로 해를 구할 수 있도록 변환을 해줍니다. -$$ u_i^{n+1} = u_i^n - c \frac{\Delta t}{\Delta x}(u_i^n-u_{i-1}^n) $$ +$$ u*i^{n+1} = u_i^n - c \frac{\Delta t}{\Delta x}(u_i^n-u*{i-1}^n) $$ 이제 이 오일러 방정식을 파이썬으로 구현해봅니다. @@ -52,7 +49,7 @@ pyplot.plot(numpy.linspace(0, 2, nx), u); $$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} = 0 $$ -$$ u_i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u_{i-1}^n) $$ +$$ u*i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u*{i-1}^n) $$ ```python import numpy # we're importing numpy @@ -87,7 +84,7 @@ pyplot.plot(numpy.linspace(0, 2, nx), u) ##Plot the results $$ \frac{\partial u}{\partial t}= \nu \frac{\partial^2 u}{\partial x^2} $$ -$$ u_{i}^{n+1}=u_{i}^{n}+\frac{\nu\Delta t}{\Delta x^2}(u_{i+1}^{n}-2u_{i}^{n}+u_{i-1}^{n}) $$ +$$ u*{i}^{n+1}=u*{i}^{n}+\frac{\nu\Delta t}{\Delta x^2}(u*{i+1}^{n}-2u*{i}^{n}+u_{i-1}^{n}) $$ ```python import numpy #loading our favorite library @@ -117,13 +114,11 @@ pyplot.plot(numpy.linspace(0, 2, nx), u); {{< figure src="https://blog.kakaocdn.net/dn/oaKgN/btq9iA57vBH/weCFyYoImjFkasFiDMir3k/img.png" >}} - - ## Burger's Equation $$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} = \nu \frac{\partial ^2u}{\partial x^2} $$ -$$ u_i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u_{i-1}^n) + \nu \frac{\Delta t}{\Delta x^2}(u_{i+1}^n - 2u_i^n + u_{i-1}^n) $$ +$$ u*i^{n+1} = u_i^n - u_i^n \frac{\Delta t}{\Delta x} (u_i^n - u*{i-1}^n) + \nu \frac{\Delta t}{\Delta x^2}(u*{i+1}^n - 2u_i^n + u*{i-1}^n) $$ ```python import numpy @@ -182,7 +177,7 @@ pyplot.legend(); $$ \frac{\partial u}{\partial t}+c\frac{\partial u}{\partial x} + c\frac{\partial u}{\partial y} = 0 $$ -$$ u_{i,j}^{n+1} = u_{i,j}^n-c \frac{\Delta t}{\Delta x}(u_{i,j}^n-u_{i-1,j}^n)-c \frac{\Delta t}{\Delta y}(u_{i,j}^n-u_{i,j-1}^n) $$ +$$ u*{i,j}^{n+1} = u*{i,j}^n-c \frac{\Delta t}{\Delta x}(u*{i,j}^n-u*{i-1,j}^n)-c \frac{\Delta t}{\Delta y}(u*{i,j}^n-u*{i,j-1}^n) $$ ```python from mpl_toolkits.mplot3d import Axes3D ##New Library required for projected 3d plots @@ -224,7 +219,7 @@ surf = ax.plot_surface(X, Y, u[:], cmap=cm.viridis) ## 2-D Convection -$$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} + v \frac{\partial u}{\partial y} = 0 $$$$ \frac{\partial v}{\partial t} + u \frac{\partial v}{\partial x} + v \frac{\partial v}{\partial y} = 0 $$$$ u_{i,j}^{n+1} = u_{i,j}^n - u_{i,j} \frac{\Delta t}{\Delta x} (u_{i,j}^n-u_{i-1,j}^n) - v_{i,j}^n \frac{\Delta t}{\Delta y} (u_{i,j}^n-u_{i,j-1}^n) $$$$ v_{i,j}^{n+1} = v_{i,j}^n - u_{i,j} \frac{\Delta t}{\Delta x} (v_{i,j}^n-v_{i-1,j}^n) - v_{i,j}^n \frac{\Delta t}{\Delta y} (v_{i,j}^n-v_{i,j-1}^n) $$ +$$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} + v \frac{\partial u}{\partial y} = 0 $$$$ \frac{\partial v}{\partial t} + u \frac{\partial v}{\partial x} + v \frac{\partial v}{\partial y} = 0 $$$$ u*{i,j}^{n+1} = u*{i,j}^n - u*{i,j} \frac{\Delta t}{\Delta x} (u*{i,j}^n-u*{i-1,j}^n) - v*{i,j}^n \frac{\Delta t}{\Delta y} (u*{i,j}^n-u*{i,j-1}^n) $$$$ v*{i,j}^{n+1} = v*{i,j}^n - u*{i,j} \frac{\Delta t}{\Delta x} (v*{i,j}^n-v*{i-1,j}^n) - v*{i,j}^n \frac{\Delta t}{\Delta y} (v*{i,j}^n-v*{i,j-1}^n) $$ ```python from mpl_toolkits.mplot3d import Axes3D @@ -266,11 +261,9 @@ ax.set_ylabel('$y$'); {{< figure src="https://blog.kakaocdn.net/dn/bbiKmO/btq9hyAHR99/3KeBvxXPvCzXXYqrTlSj9k/img.png" >}} - - ## 2D Diffusion -$$ \frac{\partial u}{\partial t} = \nu \frac{\partial ^2 u}{\partial x^2} + \nu \frac{\partial ^2 u}{\partial y^2} $$$$ \begin{split}u_{i,j}^{n+1} = u_{i,j}^n &+ \frac{\nu \Delta t}{\Delta x^2}(u_{i+1,j}^n - 2 u_{i,j}^n + u_{i-1,j}^n) \\&+ \frac{\nu \Delta t}{\Delta y^2}(u_{i,j+1}^n-2 u_{i,j}^n + u_{i,j-1}^n)\end{split} $$ +$$ \frac{\partial u}{\partial t} = \nu \frac{\partial ^2 u}{\partial x^2} + \nu \frac{\partial ^2 u}{\partial y^2} $$$$ \begin{split}u*{i,j}^{n+1} = u*{i,j}^n &+ \frac{\nu \Delta t}{\Delta x^2}(u*{i+1,j}^n - 2 u*{i,j}^n + u*{i-1,j}^n) \&+ \frac{\nu \Delta t}{\Delta y^2}(u*{i,j+1}^n-2 u*{i,j}^n + u*{i,j-1}^n)\end{split} $$ ```python import numpy @@ -327,11 +320,9 @@ diffuse(14) {{< figure src="https://blog.kakaocdn.net/dn/eLwQEW/btq9e0ysgnx/YVHruuNPlMa6pJODaIGJdK/img.png" >}} - - ## Burgers' Equation in 2D -$$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} + v \frac{\partial u}{\partial y} = \nu \; \left(\frac{\partial ^2 u}{\partial x^2} + \frac{\partial ^2 u}{\partial y^2}\right) $$$$ \frac{\partial v}{\partial t} + u \frac{\partial v}{\partial x} + v \frac{\partial v}{\partial y} = \nu \; \left(\frac{\partial ^2 v}{\partial x^2} + \frac{\partial ^2 v}{\partial y^2}\right) $$$$ \begin{split}v_{i,j}^{n+1} = & v_{i,j}^n - \frac{\Delta t}{\Delta x} u_{i,j}^n (v_{i,j}^n - v_{i-1,j}^n) - \frac{\Delta t}{\Delta y} v_{i,j}^n (v_{i,j}^n - v_{i,j-1}^n) \\&+ \frac{\nu \Delta t}{\Delta x^2}(v_{i+1,j}^n-2v_{i,j}^n+v_{i-1,j}^n) + \frac{\nu \Delta t}{\Delta y^2} (v_{i,j+1}^n - 2v_{i,j}^n + v_{i,j-1}^n)\end{split} $$ +$$ \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} + v \frac{\partial u}{\partial y} = \nu ; \left(\frac{\partial ^2 u}{\partial x^2} + \frac{\partial ^2 u}{\partial y^2}\right) $$$$ \frac{\partial v}{\partial t} + u \frac{\partial v}{\partial x} + v \frac{\partial v}{\partial y} = \nu ; \left(\frac{\partial ^2 v}{\partial x^2} + \frac{\partial ^2 v}{\partial y^2}\right) $$$$ \begin{split}v*{i,j}^{n+1} = & v*{i,j}^n - \frac{\Delta t}{\Delta x} u*{i,j}^n (v*{i,j}^n - v*{i-1,j}^n) - \frac{\Delta t}{\Delta y} v*{i,j}^n (v*{i,j}^n - v*{i,j-1}^n) \&+ \frac{\nu \Delta t}{\Delta x^2}(v*{i+1,j}^n-2v*{i,j}^n+v*{i-1,j}^n) + \frac{\nu \Delta t}{\Delta y^2} (v*{i,j+1}^n - 2v*{i,j}^n + v*{i,j-1}^n)\end{split} $$ ```python import numpy @@ -410,11 +401,9 @@ ax.set_ylabel('$y$'); {{< figure src="https://blog.kakaocdn.net/dn/PL2CD/btq9fcyC1VV/MC1B8I2YedaaCFr5Lr06KK/img.png" >}} - - ## 2D Laplace Equation -$$ \frac{\partial ^2 p}{\partial x^2} + \frac{\partial ^2 p}{\partial y^2} = 0 $$$$ p_{i,j}^n = \frac{\Delta y^2(p_{i+1,j}^n+p_{i-1,j}^n)+\Delta x^2(p_{i,j+1}^n + p_{i,j-1}^n)}{2(\Delta x^2 + \Delta y^2)} $$ +$$ \frac{\partial ^2 p}{\partial x^2} + \frac{\partial ^2 p}{\partial y^2} = 0 $$$$ p*{i,j}^n = \frac{\Delta y^2(p*{i+1,j}^n+p*{i-1,j}^n)+\Delta x^2(p*{i,j+1}^n + p_{i,j-1}^n)}{2(\Delta x^2 + \Delta y^2)} $$ ```python import numpy @@ -481,11 +470,9 @@ plot2D(x, y, p) {{< figure src="https://blog.kakaocdn.net/dn/bxvdGX/btq9goyjEC1/YkjgRDKkIZuAe2isKNsv60/img.png" >}} - - ## 2D Poisson Equation -$$ \frac{\partial ^2 p}{\partial x^2} + \frac{\partial ^2 p}{\partial y^2} = b $$$$ p_{i,j}^{n}=\frac{(p_{i+1,j}^{n}+p_{i-1,j}^{n})\Delta y^2+(p_{i,j+1}^{n}+p_{i,j-1}^{n})\Delta x^2-b_{i,j}^{n}\Delta x^2\Delta y^2}{2(\Delta x^2+\Delta y^2)} $$ +$$ \frac{\partial ^2 p}{\partial x^2} + \frac{\partial ^2 p}{\partial y^2} = b $$$$ p*{i,j}^{n}=\frac{(p*{i+1,j}^{n}+p*{i-1,j}^{n})\Delta y^2+(p*{i,j+1}^{n}+p*{i,j-1}^{n})\Delta x^2-b*{i,j}^{n}\Delta x^2\Delta y^2}{2(\Delta x^2+\Delta y^2)} $$ ```python import numpy @@ -542,11 +529,9 @@ def plot2D(x, y, p): plot2D(x, y, p) ``` -## - ## Cavity Flow with Navier–Stokes -$$ \frac{\partial \vec{v}}{\partial t}+(\vec{v}\cdot\nabla)\vec{v}=-\frac{1}{\rho}\nabla p + \nu \nabla^2\vec{v} $$$$ \frac{\partial u}{\partial t}+u\frac{\partial u}{\partial x}+v\frac{\partial u}{\partial y} = -\frac{1}{\rho}\frac{\partial p}{\partial x}+\nu \left(\frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2} \right) $$$$ \frac{\partial^2 p}{\partial x^2}+\frac{\partial^2 p}{\partial y^2} = -\rho\left(\frac{\partial u}{\partial x}\frac{\partial u}{\partial x}+2\frac{\partial u}{\partial y}\frac{\partial v}{\partial x}+\frac{\partial v}{\partial y}\frac{\partial v}{\partial y} \right) $$$$ \begin{split}p_{i,j}^{n} = & \frac{\left(p_{i+1,j}^{n}+p_{i-1,j}^{n}\right) \Delta y^2 + \left(p_{i,j+1}^{n}+p_{i,j-1}^{n}\right) \Delta x^2}{2\left(\Delta x^2+\Delta y^2\right)} \\& -\frac{\rho\Delta x^2\Delta y^2}{2\left(\Delta x^2+\Delta y^2\right)} \\& \times \left[\frac{1}{\Delta t}\left(\frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x}+\frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\right)-\frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x}\frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x} -2\frac{u_{i,j+1}-u_{i,j-1}}{2\Delta y}\frac{v_{i+1,j}-v_{i-1,j}}{2\Delta x}-\frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\right]\end{split} $$ +$$ \frac{\partial \vec{v}}{\partial t}+(\vec{v}\cdot\nabla)\vec{v}=-\frac{1}{\rho}\nabla p + \nu \nabla^2\vec{v} $$$$ \frac{\partial u}{\partial t}+u\frac{\partial u}{\partial x}+v\frac{\partial u}{\partial y} = -\frac{1}{\rho}\frac{\partial p}{\partial x}+\nu \left(\frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2} \right) $$$$ \frac{\partial^2 p}{\partial x^2}+\frac{\partial^2 p}{\partial y^2} = -\rho\left(\frac{\partial u}{\partial x}\frac{\partial u}{\partial x}+2\frac{\partial u}{\partial y}\frac{\partial v}{\partial x}+\frac{\partial v}{\partial y}\frac{\partial v}{\partial y} \right) $$$$ \begin{split}p*{i,j}^{n} = & \frac{\left(p*{i+1,j}^{n}+p*{i-1,j}^{n}\right) \Delta y^2 + \left(p*{i,j+1}^{n}+p*{i,j-1}^{n}\right) \Delta x^2}{2\left(\Delta x^2+\Delta y^2\right)} \& -\frac{\rho\Delta x^2\Delta y^2}{2\left(\Delta x^2+\Delta y^2\right)} \& \times \left[\frac{1}{\Delta t}\left(\frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x}+\frac{v*{i,j+1}-v*{i,j-1}}{2\Delta y}\right)-\frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x}\frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x} -2\frac{u*{i,j+1}-u*{i,j-1}}{2\Delta y}\frac{v*{i+1,j}-v*{i-1,j}}{2\Delta x}-\frac{v*{i,j+1}-v*{i,j-1}}{2\Delta y}\frac{v*{i,j+1}-v_{i,j-1}}{2\Delta y}\right]\end{split} $$ ```python import numpy @@ -702,7 +687,7 @@ pyplot.ylabel('Y'); ## Channel Flow with Navier–Stokes -$$ \frac{\partial u}{\partial t}+u\frac{\partial u}{\partial x}+v\frac{\partial u}{\partial y}=-\frac{1}{\rho}\frac{\partial p}{\partial x}+\nu\left(\frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2}\right)+F $$$$ \frac{\partial^2 p}{\partial x^2}+\frac{\partial^2 p}{\partial y^2}=-\rho\left(\frac{\partial u}{\partial x}\frac{\partial u}{\partial x}+2\frac{\partial u}{\partial y}\frac{\partial v}{\partial x}+\frac{\partial v}{\partial y}\frac{\partial v}{\partial y}\right) $$$$ \begin{split}p_{i,j}^{n} = & \frac{\left(p_{i+1,j}^{n}+p_{i-1,j}^{n}\right) \Delta y^2 + \left(p_{i,j+1}^{n}+p_{i,j-1}^{n}\right) \Delta x^2}{2(\Delta x^2+\Delta y^2)} \\& -\frac{\rho\Delta x^2\Delta y^2}{2\left(\Delta x^2+\Delta y^2\right)} \\& \times \left[\frac{1}{\Delta t} \left(\frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x} + \frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\right) - \frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x}\frac{u_{i+1,j}-u_{i-1,j}}{2\Delta x} - 2\frac{u_{i,j+1}-u_{i,j-1}}{2\Delta y}\frac{v_{i+1,j}-v_{i-1,j}}{2\Delta x} - \frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\frac{v_{i,j+1}-v_{i,j-1}}{2\Delta y}\right]\end{split} $$ +$$ \frac{\partial u}{\partial t}+u\frac{\partial u}{\partial x}+v\frac{\partial u}{\partial y}=-\frac{1}{\rho}\frac{\partial p}{\partial x}+\nu\left(\frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2}\right)+F $$$$ \frac{\partial^2 p}{\partial x^2}+\frac{\partial^2 p}{\partial y^2}=-\rho\left(\frac{\partial u}{\partial x}\frac{\partial u}{\partial x}+2\frac{\partial u}{\partial y}\frac{\partial v}{\partial x}+\frac{\partial v}{\partial y}\frac{\partial v}{\partial y}\right) $$$$ \begin{split}p*{i,j}^{n} = & \frac{\left(p*{i+1,j}^{n}+p*{i-1,j}^{n}\right) \Delta y^2 + \left(p*{i,j+1}^{n}+p*{i,j-1}^{n}\right) \Delta x^2}{2(\Delta x^2+\Delta y^2)} \& -\frac{\rho\Delta x^2\Delta y^2}{2\left(\Delta x^2+\Delta y^2\right)} \& \times \left[\frac{1}{\Delta t} \left(\frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x} + \frac{v*{i,j+1}-v*{i,j-1}}{2\Delta y}\right) - \frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x}\frac{u*{i+1,j}-u*{i-1,j}}{2\Delta x} - 2\frac{u*{i,j+1}-u*{i,j-1}}{2\Delta y}\frac{v*{i+1,j}-v*{i-1,j}}{2\Delta x} - \frac{v*{i,j+1}-v*{i,j-1}}{2\Delta y}\frac{v*{i,j+1}-v_{i,j-1}}{2\Delta y}\right]\end{split} $$ ```python import numpy @@ -895,5 +880,4 @@ pyplot.quiver(X[::3, ::3], Y[::3, ::3], u[::3, ::3], v[::3, ::3]); {{< figure src="https://blog.kakaocdn.net/dn/du6hla/btq9fdKZP6o/ifKi67Tsr8khMmReNSHn5K/img.png" >}} -출처> [CFD Python: 12 steps to Navier-Stokes :: Lorena A. Barba Group (lorenabarba.com)](https://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/) - +출처> [CFD Python: 12 steps to Navier-Stokes :: Lorena A. Barba Group (lorenabarba.com)](https://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/) \ No newline at end of file