Merge branch 'documentation' into 'master'
Documentation maintenance See merge request !479
%% Cell type:code id: tags: | %% Cell type:code id: tags: | ||
``` python | ``` python | ||
from pystencils.session import * | from pystencils.session import * | ||
import sympy as sp | import sympy as sp | ||
import numpy as np | import numpy as np | ||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
sp.init_printing() | sp.init_printing() | ||
``` | ``` | ||
%% Cell type:markdown id: tags: | %% Cell type:markdown id: tags: | ||
### Code Generation for the Heat Equation | ### Code Generation for the Heat Equation | ||
The <a target="_blank" href="https://en.wikipedia.org/wiki/Heat_equation">heat equation</a> which is a simple partial differential equation describing the flow of heat through a homogenous medium. We can write it as | The <a target="_blank" href="https://en.wikipedia.org/wiki/Heat_equation">heat equation</a> which is a simple partial differential equation describing the flow of heat through a homogenous medium. We can write it as | ||
$$ | $$ | ||
\frac{\partial u}{\partial t} = | \frac{\partial u}{\partial t} = | ||
\kappa \left( | \kappa \left( | ||
\frac{\partial^2 u}{\partial x^2} + | \frac{\partial^2 u}{\partial x^2} + | ||
\frac{\partial^2 u}{\partial y^2} | \frac{\partial^2 u}{\partial y^2} | ||
\right) | \right) | ||
$$ | $$ | ||
where $\kappa$ is the medium's diffusion coefficient and $u(x, y, t)$ is the unknown temperature distribution at the coordinate $(x,y)$ at time $t$. | where $\kappa$ is the medium's diffusion coefficient and $u(x, y, t)$ is the unknown temperature distribution at the coordinate $(x,y)$ at time $t$. | ||
To discretize this equation using pystencils, we first need to define all the fields and other symbols involved. | To discretize this equation using pystencils, we first need to define all the fields and other symbols involved. | ||
%% Cell type:code id: tags: | %% Cell type:code id: tags: | ||
``` python | ``` python | ||
u, u_tmp = ps.fields("u, u_tmp: [2D]", layout='fzyx') | u, u_tmp = ps.fields("u, u_tmp: [2D]", layout='fzyx') | ||
kappa = sp.Symbol("kappa") | kappa = sp.Symbol("kappa") | ||
dx = sp.Symbol("dx") | dx = sp.Symbol("dx") | ||
dt = sp.Symbol("dt") | dt = sp.Symbol("dt") | ||
``` | ``` | ||
%% Cell type:markdown id: tags: | %% Cell type:markdown id: tags: | ||
We define the PDE using the pystencils building blocks for transient and spatial derivatives. The definition is implicitly equalled to zero. We use `ps.fd.transient` for a first derivative by time and `ps.fd.diff` to express the second derivatives. `ps.fd.diff` takes a field and a list of spatial dimensions in which the field should be differentiated. | We define the PDE using the pystencils building blocks for transient and spatial derivatives. The definition is implicitly equalled to zero. We use `ps.fd.transient` for a first derivative by time and `ps.fd.diff` to express the second derivatives. `ps.fd.diff` takes a field and a list of spatial dimensions in which the field should be differentiated. | ||
%% Cell type:code id: tags: | %% Cell type:code id: tags: | ||
``` python | ``` python | ||
heat_pde = ps.fd.transient(u) - kappa * ( ps.fd.diff( u, 0, 0 ) + ps.fd.diff( u, 1, 1 ) ) | heat_pde = ps.fd.transient(u) - kappa * ( ps.fd.diff( u, 0, 0 ) + ps.fd.diff( u, 1, 1 ) ) | ||
heat_pde | heat_pde | ||
``` | ``` | ||
%% Output | %% Output | ||
$\displaystyle - \kappa \left({\partial_{0} {\partial_{0} {{u}_{(0,0)}}}} + {\partial_{1} {\partial_{1} {{u}_{(0,0)}}}}\right) + \partial_t u_{C}$ | $\displaystyle - \kappa \left({\partial_{0} {\partial_{0} {{u}_{(0,0)}}}} + {\partial_{1} {\partial_{1} {{u}_{(0,0)}}}}\right) + \partial_t u_{C}$ | ||
-κ⋅(D(D(u[0,0])) + D(D(u[0,0]))) + Transient(u_C) | -κ⋅(D(D(u[0,0])) + D(D(u[0,0]))) + Transient(u_C) | ||
%% Cell type:markdown id: tags: | %% Cell type:markdown id: tags: | ||
Next, the PDE will be discretized. We use the `Discretization2ndOrder` class to apply finite differences discretization to the spatial components, and explicit euler discretization to the transient components. | Next, the PDE will be discretized. We use the `Discretization2ndOrder` class to apply finite differences discretization to the spatial components, and explicit euler discretization to the transient components. | ||
%% Cell type:code id: tags: | %% Cell type:code id: tags: | ||
``` python | ``` python | ||
discretize = ps.fd.Discretization2ndOrder(dx=dx, dt=dt) | discretize = ps.fd.Discretization2ndOrder(dx=dx, dt=dt) | ||
heat_pde_discretized = discretize(heat_pde) | heat_pde_discretized = discretize(heat_pde) | ||
heat_pde_discretized | heat_pde_discretized | ||
``` | ``` | ||
%% Output | %% Output | ||