<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 The Devito domain specific language
Step1:  From equations to code in a few lines of Python
The main objective of this tutorial is to demonstrate how Devito and its SymPy-powered symbolic API can be used to solve partial differential equations using the finite difference method with highly optimized stencils in a few lines of Python. We demonstrate how computational stencils can be derived directly from the equation in an automated fashion and how Devito can be used to generate and execute, at runtime, the desired numerical scheme in the form of optimized C code.
Defining the physical domain
Before we can begin creating finite-difference (FD) stencils we will need to give Devito a few details regarding the computational domain within which we wish to solve our problem. For this purpose we create a Grid object that stores the physical extent (the size) of our domain and knows how many points we want to use in each dimension to discretise our data.
<img src="figures/grid.png" style="width
Step2:  Functions and data
To express our equation in symbolic form and discretise it using finite differences, Devito provides a set of Function types. A Function object
Step3:  Ok, let's create a function $f(x, y)$ and look at the data Devito has associated with it. Please note that it is important to use explicit keywords, such as name or grid when creating Function objects.
Step4:  By default, Devito Function objects use the spatial dimensions (x, y) for 2D grids and (x, y, z) for 3D grids. To solve a PDE over several timesteps a time dimension is also required by our symbolic function. For this Devito provides an additional function type, the TimeFunction, which incorporates the correct dimension along with some other intricacies needed to create a time stepping scheme.
Step5:  Since the default time order of a TimeFunction is 1, the shape of f is (2, 5, 6), i.e. Devito has allocated two buffers to represent g(t, x, y) and g(t + dt, x, y)
Step6:  Derivatives of symbolic functions
The functions we have created so far all act as sympy.Function objects, which means that we can form symbolic derivative expressions from them. Devito provides a set of shorthand expressions (implemented as Python properties) that allow us to generate finite differences in symbolic form. For example, the property f.dx denotes $\frac{\partial}{\partial x} f(x, y)$ - only that Devito has already discretised it with a finite difference expression. There are also a set of shorthand expressions for left (backward) and right (forward) derivatives
Step7:  We may also want to take a look at the stencil Devito will generate based on the chosen discretisation
Step8:  There also exist convenient shortcuts to express the forward and backward stencil points, g(t+dt, x, y) and g(t-dt, x, y).
Step9:  And of course, there's nothing to stop us taking derivatives on these objects
Step10:  A linear convection operator
Note
Step11:  Next, we wish to discretise our governing equation so that a functional Operator can be created from it. We begin by simply writing out the equation as a symbolic expression, while using shorthand expressions for the derivatives provided by the Function object. This will create a symbolic object of the dicretised equation.
Using the Devito shorthand notation, we can express the governing equations as
Step12:  We now need to rearrange our equation so that the term $u(t+dt, x, y)$ is on the left-hand side, since it represents the next point in time for our state variable $u$. Devito provides a utility called solve, built on top of SymPy's solve, to rearrange our equation so that it represents a valid state update for $u$. Here, we use solve to create a valid stencil for our update to u(t+dt, x, y)
Step13:  The right-hand side of the 'update' equation should be a stencil of the shape
<img src="figures/stencil_convection.png" style="width
Step14:  Note that the real power of Devito is hidden within Operator, it will automatically generate and compile the optimized C code. We can look at this code (noting that this is not a requirement of executing it) via
Step15:  Second derivatives and high-order stencils
In the above example only a combination of first derivatives was present in the governing equation. However, second (or higher) order derivatives are often present in scientific problems of interest, notably any PDE modeling diffusion. To generate second order derivatives we must give the devito.Function object another piece of information
Step16:  We can increase the discretisation arbitrarily if we wish to specify higher order FD stencils
Step17:  To implement the diffusion or wave equations, we must take the Laplacian $\nabla^2 u$, which is the sum of the second derivatives in all spatial dimensions. For this, Devito also provides a shorthand expression, which means we do not have to hard-code the problem dimension (2D or 3D) in the code. To change the problem dimension we can create another Grid object and use this to re-define our Function's
Step18:  We can re-define our function u with a different space_order argument to change the discretisation order of the stencil expression created. For example, we can derive an expression of the 12th-order Laplacian $\nabla^2 u$
Step19:  The same expression could also have been generated explicitly via
Step20:  Derivatives of composite expressions
Derivatives of any arbitrary expression can easily be generated
Step21:  Which can, depending on the chosen discretisation, lead to fairly complex stencils
<ASSISTANT_TASK:>
Python Code:
from devito import *
Explanation: The Devito domain specific language: an overview
This notebook presents an overview of the Devito symbolic language, used to express and discretise operators, in particular partial differential equations (PDEs).
For convenience, we import all Devito modules:
End of explanation
grid = Grid(shape=(5, 6), extent=(1., 1.))
grid
Explanation: From equations to code in a few lines of Python
The main objective of this tutorial is to demonstrate how Devito and its SymPy-powered symbolic API can be used to solve partial differential equations using the finite difference method with highly optimized stencils in a few lines of Python. We demonstrate how computational stencils can be derived directly from the equation in an automated fashion and how Devito can be used to generate and execute, at runtime, the desired numerical scheme in the form of optimized C code.
Defining the physical domain
Before we can begin creating finite-difference (FD) stencils we will need to give Devito a few details regarding the computational domain within which we wish to solve our problem. For this purpose we create a Grid object that stores the physical extent (the size) of our domain and knows how many points we want to use in each dimension to discretise our data.
<img src="figures/grid.png" style="width: 220px;"/>
End of explanation
print(Function.__doc__)
Explanation: Functions and data
To express our equation in symbolic form and discretise it using finite differences, Devito provides a set of Function types. A Function object:
Behaves like a sympy.Function symbol
Manages data associated with the symbol
To get more information on how to create and use a Function object, or any type provided by Devito, we can take a look at the documentation.
End of explanation
f = Function(name='f', grid=grid)
f
f.data
Explanation: Ok, let's create a function $f(x, y)$ and look at the data Devito has associated with it. Please note that it is important to use explicit keywords, such as name or grid when creating Function objects.
End of explanation
g = TimeFunction(name='g', grid=grid)
g
Explanation: By default, Devito Function objects use the spatial dimensions (x, y) for 2D grids and (x, y, z) for 3D grids. To solve a PDE over several timesteps a time dimension is also required by our symbolic function. For this Devito provides an additional function type, the TimeFunction, which incorporates the correct dimension along with some other intricacies needed to create a time stepping scheme.
End of explanation
g.shape
Explanation: Since the default time order of a TimeFunction is 1, the shape of f is (2, 5, 6), i.e. Devito has allocated two buffers to represent g(t, x, y) and g(t + dt, x, y):
End of explanation
g.dt
Explanation: Derivatives of symbolic functions
The functions we have created so far all act as sympy.Function objects, which means that we can form symbolic derivative expressions from them. Devito provides a set of shorthand expressions (implemented as Python properties) that allow us to generate finite differences in symbolic form. For example, the property f.dx denotes $\frac{\partial}{\partial x} f(x, y)$ - only that Devito has already discretised it with a finite difference expression. There are also a set of shorthand expressions for left (backward) and right (forward) derivatives:
| Derivative | Shorthand | Discretised | Stencil |
| ---------- |:---------:|:-----------:|:-------:|
| $\frac{\partial}{\partial x}f(x, y)$ (right) | f.dxr | $\frac{f(x+h_x,y)}{h_x} - \frac{f(x,y)}{h_x}$ | <img src="figures/stencil_forward.png" style="width: 180px;"/> |
| $\frac{\partial}{\partial x}f(x, y)$ (left) | f.dxl | $\frac{f(x,y)}{h_x} - \frac{f(x-h_x,y)}{h_x}$ | <img src="figures/stencil_backward.png" style="width: 180px;"/> |
A similar set of expressions exist for each spatial dimension defined on our grid, for example f.dy and f.dyl. Obviously, one can also take derivatives in time of TimeFunction objects. For example, to take the first derivative in time of g you can simply write:
End of explanation
g.dt.evaluate
Explanation: We may also want to take a look at the stencil Devito will generate based on the chosen discretisation:
End of explanation
g.forward
g.backward
Explanation: There also exist convenient shortcuts to express the forward and backward stencil points, g(t+dt, x, y) and g(t-dt, x, y).
End of explanation
g.forward.dt
g.forward.dy
Explanation: And of course, there's nothing to stop us taking derivatives on these objects:
End of explanation
from examples.cfd import init_smooth, plot_field
nt = 100  # Number of timesteps
dt = 0.2 * 2. / 80  # Timestep size (sigma=0.2)
c = 1  # Value for c
# Then we create a grid and our function
grid = Grid(shape=(81, 81), extent=(2., 2.))
u = TimeFunction(name='u', grid=grid)
# We can now set the initial condition and plot it
init_smooth(field=u.data[0], dx=grid.spacing[0], dy=grid.spacing[1])
init_smooth(field=u.data[1], dx=grid.spacing[0], dy=grid.spacing[1])
plot_field(u.data[0])
Explanation: A linear convection operator
Note: The following example is derived from step 5 in the excellent tutorial series CFD Python: 12 steps to Navier-Stokes.
In this simple example we will show how to derive a very simple convection operator from a high-level description of the governing equation. We will go through the process of deriving a discretised finite difference formulation of the state update for the field variable $u$, before creating a callable Operator object. Luckily, the automation provided by SymPy makes the derivation very nice and easy.
The governing equation we want to implement is the linear convection equation:
$$\frac{\partial u}{\partial t}+c\frac{\partial u}{\partial x} + c\frac{\partial u}{\partial y} = 0.$$
Before we begin, we must define some parameters including the grid, the number of timesteps and the timestep size. We will also initialize our velocity u with a smooth field:
End of explanation
eq = Eq(u.dt + c * u.dxl + c * u.dyl)
eq
Explanation: Next, we wish to discretise our governing equation so that a functional Operator can be created from it. We begin by simply writing out the equation as a symbolic expression, while using shorthand expressions for the derivatives provided by the Function object. This will create a symbolic object of the dicretised equation.
Using the Devito shorthand notation, we can express the governing equations as:
End of explanation
stencil = solve(eq, u.forward)
update = Eq(u.forward, stencil)
update
Explanation: We now need to rearrange our equation so that the term $u(t+dt, x, y)$ is on the left-hand side, since it represents the next point in time for our state variable $u$. Devito provides a utility called solve, built on top of SymPy's solve, to rearrange our equation so that it represents a valid state update for $u$. Here, we use solve to create a valid stencil for our update to u(t+dt, x, y):
End of explanation
op = Operator(update, opt='noop')
op(time=nt+1, dt=dt)
plot_field(u.data[0])
Explanation: The right-hand side of the 'update' equation should be a stencil of the shape
<img src="figures/stencil_convection.png" style="width: 160px;"/>
Once we have created this 'update' expression, we can create a Devito Operator. This Operator will basically behave like a Python function that we can call to apply the created stencil over our associated data, as long as we provide all necessary unknowns. In this case we need to provide the number of timesteps to compute via the keyword time and the timestep size via dt (both have been defined above):
End of explanation
print(op.ccode)
Explanation: Note that the real power of Devito is hidden within Operator, it will automatically generate and compile the optimized C code. We can look at this code (noting that this is not a requirement of executing it) via:
End of explanation
u = TimeFunction(name='u', grid=grid, space_order=2)
u.dx2
u.dx2.evaluate
Explanation: Second derivatives and high-order stencils
In the above example only a combination of first derivatives was present in the governing equation. However, second (or higher) order derivatives are often present in scientific problems of interest, notably any PDE modeling diffusion. To generate second order derivatives we must give the devito.Function object another piece of information: the desired discretisation of the stencil(s).
First, lets define a simple second derivative in x, for which we need to give $u$ a space_order of (at least) 2. The shorthand for this second derivative is u.dx2.
End of explanation
u = TimeFunction(name='u', grid=grid, space_order=4)
u.dx2
u.dx2.evaluate
Explanation: We can increase the discretisation arbitrarily if we wish to specify higher order FD stencils:
End of explanation
grid_3d = Grid(shape=(5, 6, 7), extent=(1., 1., 1.))
u = TimeFunction(name='u', grid=grid_3d, space_order=2)
u
Explanation: To implement the diffusion or wave equations, we must take the Laplacian $\nabla^2 u$, which is the sum of the second derivatives in all spatial dimensions. For this, Devito also provides a shorthand expression, which means we do not have to hard-code the problem dimension (2D or 3D) in the code. To change the problem dimension we can create another Grid object and use this to re-define our Function's:
End of explanation
u = TimeFunction(name='u', grid=grid_3d, space_order=12)
u.laplace
Explanation: We can re-define our function u with a different space_order argument to change the discretisation order of the stencil expression created. For example, we can derive an expression of the 12th-order Laplacian $\nabla^2 u$:
End of explanation
u.dx2 + u.dy2 + u.dz2
Explanation: The same expression could also have been generated explicitly via:
End of explanation
u = TimeFunction(name='u', grid=grid, space_order=2)
v = TimeFunction(name='v', grid=grid, space_order=2, time_order=2)
v.dt2 + u.laplace
(v.dt2 + u.laplace).dx2
Explanation: Derivatives of composite expressions
Derivatives of any arbitrary expression can easily be generated:
End of explanation
(v.dt2 + u.laplace).dx2.evaluate
Explanation: Which can, depending on the chosen discretisation, lead to fairly complex stencils:
End of explanation
<END_TASK>