Tutorials / Fluids

Basic mathematical operator

Gradient, Divergence, Laplacian, Curl

This tutorial gives four basic mathematical operators commonly used in physical simulation, i.e., gradient, divergence, Laplacian and curl.

1) Gradient ($\nabla$)

The gradient operator acts on a scalar field and calculates the partial derivative of the scalar field with respect to each spatial coordinate.

In 3D,

\[ \nabla= \begin{bmatrix} \dfrac{\partial}{\partial x}\\[6pt] \dfrac{\partial}{\partial y}\\[6pt] \dfrac{\partial}{\partial z} \end{bmatrix}. \]

Given a scalar field $p(x,y,z)$, its gradient is

\[ \nabla p= \begin{bmatrix} \dfrac{\partial p}{\partial x}\\[6pt] \dfrac{\partial p}{\partial y}\\[6pt] \dfrac{\partial p}{\partial z} \end{bmatrix}. \]

The gradient points in the direction of the steepest increase of the scalar field, and its magnitude gives the maximum rate of increase.

2) Divergence ($\nabla\cdot$)

The divergence operator acts on a vector field and produces a scalar field.

In 3D,

\[ \nabla\cdot= \begin{bmatrix} \dfrac{\partial}{\partial x} & \dfrac{\partial}{\partial y} & \dfrac{\partial}{\partial z} \end{bmatrix}. \]

Given a 3D velocity field

\[ \mathbf{u} = \begin{bmatrix} u_x\\ u_y\\ u_z \end{bmatrix}, \]

its divergence is

\[ \nabla\cdot\mathbf{u} = \begin{bmatrix} \dfrac{\partial}{\partial x} & \dfrac{\partial}{\partial y} & \dfrac{\partial}{\partial z} \end{bmatrix} \begin{bmatrix} u_x\\ u_y\\ u_z \end{bmatrix} = \dfrac{\partial u_x}{\partial x} + \dfrac{\partial u_y}{\partial y} + \dfrac{\partial u_z}{\partial z}. \]

Divergence measures the local volumetric expansion or compression rate of a vector field.

For an incompressible fluid, the velocity field must satisfy

\[ \nabla\cdot\mathbf{u}=0. \]

This means that an infinitesimal material volume carried by the fluid does not expand or contract.

3) Laplacian ($\nabla^2$)

The Laplacian of a scalar field is the divergence of its gradient:

\[ \nabla^2 = \nabla\cdot\nabla. \]

In 3D,

\[ \nabla^2 = \begin{bmatrix} \dfrac{\partial}{\partial x} & \dfrac{\partial}{\partial y} & \dfrac{\partial}{\partial z} \end{bmatrix} \begin{bmatrix} \dfrac{\partial}{\partial x}\\ \dfrac{\partial}{\partial y}\\ \dfrac{\partial}{\partial z} \end{bmatrix} = \dfrac{\partial^2}{\partial x^2} + \dfrac{\partial^2}{\partial y^2} + \dfrac{\partial^2}{\partial z^2}. \]

Applying the Laplacian to a scalar field $p$ gives

\[ \nabla^2 p = \dfrac{\partial^2 p}{\partial x^2} + \dfrac{\partial^2 p}{\partial y^2} + \dfrac{\partial^2 p}{\partial z^2}. \]

Locally, the Laplacian measures how the value at a point compares with the average of nearby values.

To see why, consider a 1D function $f=f(x)$. Using the central finite-difference approximation,

\[ f''(x) \approx \frac{f(x-h)-2f(x)+f(x+h)}{h^2}. \]

This can be rewritten as

\[ f''(x) \approx \frac{2}{h^2} \left( \frac{f(x-h)+f(x+h)}{2} - f(x) \right). \]

The term inside the bracket measures the difference between the average of the two neighbouring values and the value at the center.

Therefore:

  • $\nabla^2 f>0$: the center value is lower than its local neighbourhood average.
  • $\nabla^2 f<0$: the center value is higher than its local neighbourhood average.
  • $\nabla^2 f\approx0$: the center value is approximately equal to its local neighbourhood average.

For a vector field, the vector Laplacian is usually applied component-wise:

\[ \nabla^2\mathbf{u} = \begin{bmatrix} \nabla^2u_x\\ \nabla^2u_y\\ \nabla^2u_z \end{bmatrix}. \]

Intuitively, it measures how each component differs from its local neighborhood average, just like viscosity in fluid.

4) Curl ($\nabla\times$)

Curl acts on a 3D vector field and produces another 3D vector field. It measures the local rotational tendency of the field.

The curl operator can be written as the skew-symmetric differential-operator matrix

\[ \nabla\times = \begin{bmatrix} 0 & -\dfrac{\partial}{\partial z} & \dfrac{\partial}{\partial y} \\[8pt] \dfrac{\partial}{\partial z} & 0 & -\dfrac{\partial}{\partial x} \\[8pt] -\dfrac{\partial}{\partial y} & \dfrac{\partial}{\partial x} & 0 \end{bmatrix}. \]

For a 3D vector field

\[ \mathbf{u} = \begin{bmatrix} u_x\\ u_y\\ u_z \end{bmatrix}, \]

the curl is

\[ \nabla\times\mathbf{u} = \begin{bmatrix} 0 & -\partial_z & \partial_y \\ \partial_z & 0 & -\partial_x \\ -\partial_y & \partial_x & 0 \end{bmatrix} \begin{bmatrix} u_x\\ u_y\\ u_z \end{bmatrix}. \]

Therefore,

\[ \nabla\times\mathbf{u} = \begin{bmatrix} \dfrac{\partial u_z}{\partial y} - \dfrac{\partial u_y}{\partial z} \\[8pt] \dfrac{\partial u_x}{\partial z} - \dfrac{\partial u_z}{\partial x} \\[8pt] \dfrac{\partial u_y}{\partial x} - \dfrac{\partial u_x}{\partial y} \end{bmatrix}. \]

For a fluid velocity field, the curl is called the vorticity:

\[ \boldsymbol{\omega} = \nabla\times\mathbf{u}. \]
  • The direction of $\boldsymbol{\omega}$ gives the local rotation axis according to the right-hand rule.
  • The magnitude $|\boldsymbol{\omega}|$ measures the strength of the local rotational motion.
  • $\boldsymbol{\omega}=0$ means that the flow is locally irrotational.