## 3.3.1. Boundary conditions¶

A crucial aspect of partial differential equations are boundary conditions, which need to be specified at the domain boundaries. For th simple domains contained in py-pde, all boundaries are orthogonal to one of the axes in the domain, so boundary conditions need to be applied to both sides of each axis. Here, the lower side of an axis can have a differnt condition than the upper side. For instance, one can enforce the value of a field to be 4 at the lower side and its derivative (in the outward direction) to be 2 on the upper side using the following code:

```bc_lower = {"value": 4}
bc_upper = {"derivative": 2}
bc = [bc_lower, bc_upper]

grid = pde.UnitGrid([16])
field = pde.ScalarField(grid)
field.laplace(bc)
```

Here, the Laplace operator applied to the field in the last line will respect the boundary conditions. Note that it suffices to give one condition if both sides of the axis require the same condition. For instance, to enforce a value of 3 on both side, one could simply use `bc = {'value': 3}`. Vectorial boundary conditions, e.g., to calculate the vector gradient or tensor divergence, can have vectorial values for the boundary condition.

Boundary values that depend on space can be set by specifying a mathematical expression, which may depend on the coordinates of all axes:

```# two different conditions for lower and upper end of x-axis
bc_x = [{"derivative": 0.1}, {"value": "sin(y / 2)"}]
# the same condition on the lower and upper end of the y-axis
bc_y = {"value": "sqrt(1 + cos(x))"}

grid = UnitGrid([32, 32])
field = pde.ScalarField(grid)
field.laplace(bc=[bc_x, bc_y])
```

Warning

To interpret arbitrary expressions, the package uses `exec()`. It should therefore not be used in a context where malicious input could occur.

Inhomogeneous values can also be specified by directly supplying an array, whose shape needs to be compatible with the boundary, i.e., it needs to have the same shape as the grid but with the dimension of the axis along which the boundary is specified removed.

There exist also special boundary conditions that impose a time-dependent value (`bc='value_expression'`) of the field or its derivative (`bc='derivative_expression'`). Beyond the spatial coordinates that are already supported for the constant conditiosn above, the expressions of these boundary conditions can depend on the time variable `t`. Note that PDEs need to supply the current time when setting the boundary conditions, e.g., when applying the differential operators. The pre-defined PDEs and the general class `PDE` already support time-dependent boundary conditions.

One important aspect about boundary conditions is that they need to respect the periodicity of the underlying grid. For instance, in a 2d grid with one periodic axis, the following boundary condition can be used:

```grid = pde.UnitGrid([16, 16], periodic=[True, False])
field = pde.ScalarField(grid)
bc = ["periodic", {"derivative": 0}]
field.laplace(bc)
```

For convenience, this typical situation can be described with the special boundary condition natural, e.g., calling the Laplace operator using `field.laplace("natural")` is identical to the example above. Alternatively, this condition can be called auto_periodic_neumann to stress that this chooses between periodic and Neumann boundary conditions automatically. Similarly, the special condition auto_periodic_dirichlet enforces periodic boundary conditions or Dirichlet boundary condition (vanishing value), depending on the periodicity of the underlying grid.

## 3.3.2. Custom PDE classes¶

To implement a new PDE in a way that all of the machinery of py-pde can be used, one needs to subclass `PDEBase` and overwrite at least the `evolution_rate()` method. A simple implementation for the Kuramoto–Sivashinsky equation could read

```class KuramotoSivashinskyPDE(PDEBase):

def evolution_rate(self, state, t=0):
""" numpy implementation of the evolution equation """
state_lapacian = state.laplace(bc="auto_periodic_neumann")
return (- state_lapacian.laplace(bc="auto_periodic_neumann")
- state_lapacian
```

A slightly more advanced example would allow for attributes that for instance define the boundary conditions and the diffusivity:

```class KuramotoSivashinskyPDE(PDEBase):

def __init__(self, diffusivity=1, bc="auto_periodic_neumann", bc_laplace="auto_periodic_neumann"):
""" initialize the class with a diffusivity and boundary conditions
for the actual field and its second derivative """
self.diffusivity = diffusivity
self.bc = bc
self.bc_laplace = bc_laplace

def evolution_rate(self, state, t=0):
""" numpy implementation of the evolution equation """
state_lapacian = state.laplace(bc=self.bc)
return (- state_lapacian.laplace(bc=self.bc_laplace)
- state_lapacian
```

We here replaced the call to `to_scalar('squared_sum')` by a dot product with itself (using the @ notation), which is equivalent. Note that the numpy implementation of the right hand side of the PDE is rather slow since it runs mostly in pure python and constructs a lot of intermediate field classes. While such an implementation is helpful for testing initial ideas, actual computations should be performed with compiled PDEs as described below.

## 3.3.3. Low-level operators¶

This section explains how to use the low-level version of the field operators. This is necessary for the numba-accelerated implementations described above and it might be necessary to use parts of the py-pde package in other packages.

### 3.3.3.1. Differential operators¶

Applying a differential operator to an instance of `ScalarField` is a simple as calling `field.laplace(bc)`, where bc denotes the boundary conditions. Calling this method returns another `ScalarField`, which in this case contains the discretized Laplacian of the original field. The equivalent call using the low-level interface is

```apply_laplace = field.grid.make_operator("laplace", bc)

laplace_data = apply_laplace(field.data)
```

Here, the first line creates a function `apply_laplace` for the given grid `field.grid` and the boundary conditions bc. This function can be applied to `numpy.ndarray` instances, e.g. `field.data`. Note that the result of this call is again a `numpy.ndarray`.

Similarly, a gradient operator can be defined

```grid = UnitGrid([6, 8])

data = np.random.random((6, 8))
assert gradient_data.shape == (2, 6, 8)
```

Note that this example does not even use the field classes. Instead, it directly defines a grid and the respective gradient operator. This operator is then applied to a random field and the resulting `numpy.ndarray` represents the 2-dimensional vector field.

The `make_operator` method of the grids generally supports the following differential operators: `'laplacian'`, `'gradient'`, `'gradient_squared'`, `'divergence'`, `'vector_gradient'`, `'vector_laplace'`, and `'tensor_divergence'`. However, a complete list of operators supported by a certain grid class can be obtained from the class property `GridClass.operators`. New operators can be added using the class method `GridClass.register_operator()`.

### 3.3.3.2. Field integration¶

The integral of an instance of `ScalarField` is usually determined by accessing the property `field.integral`. Since the integral of a discretized field is basically a sum weighted by the cell volumes, calculating the integral using only `numpy` is easy:

```cell_volumes = field.grid.cell_volumes
integral = (field.data * cell_volumes).sum()
```

Note that `cell_volumes` is a simple number for Cartesian grids, but is an array for more complicated grids, where the cell volume is not uniform.

### 3.3.3.3. Field interpolation¶

The fields defined in the py-pde package also support linear interpolation by calling `field.interpolate(point)`. Similarly to the differential operators discussed above, this call can also be translated to code that does not use the full package:

```grid = UnitGrid([6, 8])
interpolate = grid.make_interpolator_compiled(bc="auto_periodic_neumann")

data = np.random.random((6, 8))
value = interpolate(data, np.array([3.5, 7.9]))
```

We first create a function `interpolate`, which is then used to interpolate the field data at a certain point. Note that the coordinates of the point need to be supplied as a `numpy.ndarray` and that only the interpolation at single points is supported. However, iteration over multiple points can be fast when the loop is compiled with `numba`.

### 3.3.3.4. Inner products¶

For vector and tensor fields, py-pde defines inner products that can be accessed conveniently using the @-syntax: `field1 @ field2` determines the scalar product between the two fields. The package also provides an implementation for an dot-operator:

```grid = UnitGrid([6, 8])
field1 = VectorField.random_normal(grid)
field2 = VectorField.random_normal(grid)

dot_operator = field1.make_dot_operator()

result = dot_operator(field1.data, field2.data)
assert result.shape == (6, 8)
```

Here, `result` is the data of the scalar field resulting from the dot product.

## 3.3.4. Numba-accelerated PDEs¶

The compiled operators introduced in the previous section can be used to implement a compiled method for the evolution rate of PDEs. As an example, we now extend the class `KuramotoSivashinskyPDE` introduced above:

```from pde.tools.numba import jit

class KuramotoSivashinskyPDE(PDEBase):

def __init__(self, diffusivity=1, bc="auto_periodic_neumann", bc_laplace="auto_periodic_neumann"):
""" initialize the class with a diffusivity and boundary conditions
for the actual field and its second derivative """
self.diffusivity = diffusivity
self.bc = bc
self.bc_laplace = bc_laplace

def evolution_rate(self, state, t=0):
""" numpy implementation of the evolution equation """
state_lapacian = state.laplace(bc=self.bc)
return (- state_lapacian.laplace(bc=self.bc_laplace)
- state_lapacian

def _make_pde_rhs_numba(self, state):
""" the numba-accelerated evolution equation """
# make attributes locally available
diffusivity = self.diffusivity

# create operators
laplace_u = state.grid.make_operator("laplace", bc=self.bc)
laplace2_u = state.grid.make_operator("laplace", bc=self.bc_laplace)
dot = VectorField(state.grid).make_dot_operator()

@jit
def pde_rhs(state_data, t=0):
""" compiled helper function evaluating right hand side """
state_lapacian = laplace_u(state_data)
return (- laplace2_u(state_lapacian)
- state_lapacian

return pde_rhs
```

To activate the compiled implementation of the evolution rate, we simply have to overwrite the `_make_pde_rhs_numba()` method. This method expects an example of the state class (e.g., an instance of `ScalarField`) and returns a function that calculates the evolution rate. The state argument is necessary to define the grid and the dimensionality of the data that the returned function is supposed to be handling. The implementation of the compiled function is split in several parts, where we first copy the attributes that are required by the implementation. This is necessary, since `numba` freezes the values when compiling the function, so that in the example above the diffusivity cannot be altered without recompiling. In the next step, we create all operators that we need subsequently. Here, we use the boundary conditions defined by the attributes, which requires two different laplace operators, since their boundary conditions might differ. In the last step, we define the actual implementation of the evolution rate as a local function that is compiled using the `jit` decorator. Here, we use the implementation shipped with py-pde, which sets some default values. However, we could have also used the usual numba implementation. It is important that the implementation of the evolution rate only uses python constructs that numba can compile.

One advantage of the numba compiled implementation is that we can now use loops, which will be much faster than their python equivalents. For instance, we could have written the dot product in the last line as an explicit loop:

```[...]

def _make_pde_rhs_numba(self, state):
""" the numba-accelerated evolution equation """
# make attributes locally available
diffusivity = self.diffusivity

# create operators
laplace_u = state.grid.make_operator("laplace", bc=self.bc)
laplace2_u = state.grid.make_operator("laplace", bc=self.bc_laplace)
dot = VectorField(state.grid).make_dot_operator()
dim = state.grid.dim

@jit
def pde_rhs(state_data, t=0):
""" compiled helper function evaluating right hand side """
state_lapacian = laplace_u(state_data)
result = - laplace2_u(state_lapacian) - state_lapacian

for i in range(state_data.size):
for j in range(dim):
result.flat[i] -= diffusivity / 2 * state_grad[j].flat[i]**2

return result

return pde_rhs
```

Here, we extract the total number of elements in the state using its `size` attribute and we obtain the dimensionality of the space from the grid attribute `dim`. Note that we access numpy arrays using their `flat` attribute to provide an implementation that works for all dimensions.

## 3.3.5. Configuration parameters¶

Configuration parameters affect how the package behaves. They can be set using a dictionary-like interface of the configuration `config`, which can be imported from the base package. Here is a list of all configuration options that can be adjusted in the package:

numba.debug

Determines whether numba used the debug mode for compilation. If enabled, this emits extra information that might be useful for debugging. (Default value: False)

numba.fastmath

Determines whether the fastmath flag is set during compilation. This affects the precision of the mathematical calculations. (Default value: True)

numba.parallel

Determines whether multiple cores are used in numba-compiled code. (Default value: True)

numba.parallel_threshold

Minimal number of support points before multithreading or multiprocessing is enabled in the numba compilations. (Default value: 65536)

Tip

To disable parallel computing in the package, the following code could be added to the start of the script:

```from pde import config
config['numba.parallel'] = False

# actual code using py-pde
```