CSE291 07 VectorCalculus
CSE291 07 VectorCalculus
Steve Rotenberg
CSE291: Physics Simulation
UCSD
Spring 2019
Fields
• A field is a function of position x and
may vary over time t
• A scalar field such as s(x,t) assigns a
scalar value to every point in space.
An example of a scalar field would be
the temperature throughout a room
• A vector field such as v(x,t) assigns a
vector to every point in space. An
example of a vector field would be the
velocity of the air
Del Symbol
• The Del symbol 𝛻 is useful for defining several
types of spatial derivatives of fields
𝑇
𝜕 𝜕 𝜕
𝛻=
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝛻𝑠 𝛻𝑠
𝛻𝑠=0
Divergence
• The divergence of a vector field is a scalar measure of
how much the vectors are expanding
𝛻∙𝐯>0
𝛻∙𝐯<0
𝛻∙𝐯=0
Curl
• The curl operator produces a new vector field that
measures the rotation of the original vector field
𝑇
𝜕𝑣𝑧 𝜕𝑣𝑦 𝜕𝑣𝑥 𝜕𝑣𝑧 𝜕𝑣𝑦 𝜕𝑣𝑥
𝛻×𝐯= − − −
𝜕𝑦 𝜕𝑧 𝜕𝑧 𝜕𝑥 𝜕𝑥 𝜕𝑦
𝜕2 𝜕2 𝜕2
𝛻2 =𝛻∙𝛻 = 2+ 2+ 2
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝜕 2𝑠 𝜕 2𝑠 𝜕 2𝑠
𝛻 2𝑠 = 2 + 2 + 2
𝜕𝑥 𝜕𝑦 𝜕𝑧
2𝐯 2𝐯 2𝐯
𝜕 𝜕 𝜕
𝛻 2𝐯 = 2 + 2 + 2
𝜕𝑥 𝜕𝑦 𝜕𝑧
Laplacian
• The Laplacian is positive in an area of the field that is
surrounded by higher values
𝜕𝑠 𝜕𝑠 𝜕𝑠 𝑇
• Gradient: 𝛻𝑠 = 𝜕𝑥 𝜕𝑦 𝜕𝑧
𝜕𝑣𝑦 𝜕𝑣𝑦 𝑇
𝜕𝑣𝑧 𝜕𝑣𝑥 𝜕𝑣𝑧 𝜕𝑣𝑥
• Curl: 𝛻×𝐯 = 𝜕𝑦
−
𝜕𝑧 𝜕𝑧
−
𝜕𝑥 𝜕𝑥
−
𝜕𝑦
2 𝜕2 𝑠 𝜕2 𝑠 𝜕2 𝑠
• Laplacian: 𝛻 𝑠 = + +
𝜕𝑥 2 𝜕𝑦 2 𝜕𝑧 2
Numerical Representation of
Fields
Computational Vector Calculus
• Now that we’ve seen the basic operations of
differential vector calculus, we turn to the
issue of computer implementation
• The Del operations are defined in terms of
general fields
• We must address the issue of how we
represent fields on the computer and how we
perform calculus operations on them
Numerical Representation of Fields
• Mathematically, a scalar or vector field represents a continuously
variable value across space that can have infinite detail
• Obviously, on the computer, we can’t truly represent the value of
the field everywhere to this level, so we must use some form of
approximation
• A standard approach to representing a continuous field is to sample
it at some number of discrete points and use some form of
interpolation to get the value between the points
• There are several choices of how to arrange our samples:
– Uniform grid
– Hierarchical grid
– Irregular mesh
– Particle based
Uniform Grids
• Uniform grids are easy to deal with and tend to
be computationally efficient due to their
simplicity
• It is very straightforward to compute derivatives
on uniform grids
• However, they require large amounts of memory
to represent large domains
• They don’t adapt well to varying levels of detail,
as they represent the field to an even level of
detail everywhere
Uniform Grids
Hierarchical Grids
• Hierarchical grids such as quadtrees and octrees
attempt to benefit from the simplicity of uniform
grids, but also have the additional benefit of
scaling well to large problems and varying levels
of detail
• The grid resolution can locally increase to handle
more detail in regions that require it
• This allows both memory and compute time to be
used efficiently and adapt automatically to the
problem complexity
Hierarchical Grids
Hierarchical Grids
Irregular Meshes
• Irregular meshes are built from primitive cells (usually
triangles in 2D and tetrahedra in 3D)
• Irregular meshes are used extensively in engineering
applications, but less so in computer animation
• One of the main benefits of irregular meshes is their
ability to adapt to complex domain geometry
• They also adapt well to varying levels of detail
• They can be quite complex to generate however and
can have a lot of computational overhead in highly
dynamic situations with moving objects
• If the irregular mesh changes over time to adapt to the
problem complexity, it is called an adaptive mesh
Irregular Mesh
Adaptive Meshes
Particle-Based (Meshless)
• Instead of using a mesh with well defined connectivity,
particle methods sample the field on a set of irregularly
distributed particles
• Particles aren’t meant to be 0 dimensional points- they are
assumed to represent a small ‘smear’ of the field, over
some radius, and the value of the field at any point is
determined by several nearby particles
• Calculating derivatives can be tricky and there are several
approaches
• Particle methods are very well suited to water and liquid
simulation for a variety of reasons and have been gaining a
lot of popularity in the computer graphics industry recently
Particle Based
Field Representations
• Each method uses its own way of sampling the field at some
interval
• Each method requires a way to interpolate the field between
sample points
• Each method requires a way to compute the different spatial
derivatives (𝛻, 𝛻 ⋅, 𝛻 ×, 𝛻 2 )
Derivative Computation
Uniform Grids & Finite Differencing
• For today, we will just consider the case of
uniform grid
• A scalar field is represented as a 2D/3D array
of floats and a vector field is a 2D/3D array of
vectors
• We will use a technique called finite
differencing to compute derivatives of the
fields
Finite Difference First Derivative
• The derivative (slope) of a 1D function 𝑠 𝑥
stored uniformly spaced at values of 𝑥𝑖 can be
approximated by finite differencing:
𝑑𝑠 ∆𝑠 𝑠𝑖+1 − 𝑠𝑖−1
𝑥𝑖 ≈ 𝑥𝑖 =
𝑑𝑥 ∆𝑥 2ℎ
𝑠𝑖−2
𝑠𝑖+1 − 𝑠𝑖−1
𝑠𝑖−1 2ℎ
𝑠𝑖 𝑠𝑖+2
𝑠𝑖+1
𝑑𝑠 ∆𝑠 𝑠𝑖+1 − 𝑠𝑖−1
𝑥𝑖 ≈ 𝑥𝑖 =
𝑑𝑥 ∆𝑥 2ℎ
Finite Difference Partial Derivatives
• If we have a scalar field 𝑠 𝐱, 𝑡 stored on a uniform 3D grid, we can
approximate the partial derivative along the x direction at grid cell
𝑖𝑗𝑘 as:
𝜕𝑠 ∆𝑠 𝑠𝑖+1𝑗𝑘 − 𝑠𝑖−1𝑗𝑘
𝐱 𝑖𝑗𝑘 ≈ 𝐱 𝑖𝑗𝑘 =
𝜕𝑥 ∆𝑥 2ℎ
• Where cell 𝑖 + 1𝑗𝑘 is the cell in the +x direction and cell 𝑖 − 1𝑗𝑘 is
in the –x direction
• Also ℎ is the cell size in the x direction
• The partials along y and z are done in the same fashion
• All of the partial derivatives in the gradient, divergence, and curl
can be computed in this way
Neighboring Grid Points
𝑠𝑖𝑗+1𝑘
𝑠𝑖𝑗𝑘−1
𝑠𝑖−1𝑗𝑘 𝑠𝑖𝑗𝑘 𝑠𝑖+1𝑗𝑘
𝑠𝑖𝑗𝑘+1
𝑠𝑖𝑗−1𝑘
Finite Difference Gradient
• We can compute the finite difference gradient 𝛻𝑠 at grid point 𝑖𝑗𝑘 from 𝑠 values at
neighboring grid points
𝑇
𝜕𝑠 𝜕𝑠 𝜕𝑠
𝛻𝑠 𝐱 𝑖𝑗𝑘 =
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝑇
∆𝑠 ∆𝑠 ∆𝑠
≈
∆𝑥 ∆𝑦 ∆𝑧
1
= 𝑣 − 𝑣𝑥𝑖−1𝑗𝑘 + 𝑣𝑦𝑖𝑗+1𝑘 − 𝑣𝑦𝑖𝑗−1𝑘 + 𝑣𝑧𝑖𝑗𝑘+1 − 𝑣𝑧𝑖𝑗𝑘−1
2ℎ 𝑥𝑖+1𝑗𝑘
Finite Difference Curl
• For the finite difference curl at grid point ijk we have:
𝑇
𝜕𝑣𝑧 𝜕𝑣𝑦 𝜕𝑣𝑥 𝜕𝑣𝑧 𝜕𝑣𝑦 𝜕𝑣𝑥
𝛻 × 𝐯 𝐱 𝑖𝑗𝑘 = − − −
𝜕𝑦 𝜕𝑧 𝜕𝑧 𝜕𝑥 𝜕𝑥 𝜕𝑦
𝑇
∆𝑣𝑧 ∆𝑣𝑦 ∆𝑣𝑥 ∆𝑣𝑧 ∆𝑣𝑦 ∆𝑣𝑥
≈ − − −
∆𝑦 ∆𝑧 ∆𝑧 ∆𝑥 ∆𝑥 ∆𝑦
∆𝑠
𝑑2 𝑠 ∆2 𝑠 ∆
∆𝑥
𝑥𝑖 ≈ =
𝑑𝑥 2 ∆𝑥 2 ∆𝑥
𝑠𝑖+1 − 𝑠𝑖 𝑠 − 𝑠𝑖−1
− 𝑖
= ℎ ℎ
ℎ
𝑠𝑖−1 𝑠𝑖+1 − 𝑠𝑖
ℎ
𝑠𝑖 𝑠𝑖+2
𝑠𝑖+1
∆ ∆𝑠 𝑠𝑖+1 − 𝑠𝑖 𝑠 − 𝑠𝑖−1
𝑑2 𝑠 ∆𝑥 − 𝑖 𝑠𝑖+1 − 2𝑠𝑖 + 𝑠𝑖−1
𝑥 ≈ 𝑥𝑖 = ℎ ℎ =
𝑑𝑥 2 𝑖 ∆𝑥 ℎ ℎ2
Finite Difference Laplacian
• The finite difference Laplacian at point ijk is:
∆2 𝑠 ∆2 𝑠 ∆2 𝑠
≈ 2+ 2+ 2
∆𝑥 ∆𝑦 ∆𝑧
1
= 2 𝑠𝑖+1𝑗𝑘 + 𝑠𝑖−1𝑗𝑘 + 𝑠𝑖𝑗+1𝑘 + 𝑠𝑖𝑗−1𝑘 + 𝑠𝑖𝑗𝑘+1 + 𝑠𝑖𝑗𝑘−1 − 6𝑠𝑖𝑗𝑘
ℎ
Finite Difference Operations
𝑠𝑖+1𝑗𝑘 − 𝑠𝑖−1𝑗𝑘
1 𝑠𝑖𝑗+1𝑘 − 𝑠𝑖𝑗−1𝑘
• Gradient: 𝛻𝑠 ≈
2ℎ
𝑠𝑖𝑗𝑘+1 − 𝑠𝑖𝑗𝑘−1
1
• Divergence: 𝛻∙𝐯≈ 𝑣𝑥𝑖+1𝑗𝑘 − 𝑣𝑥𝑖−1𝑗𝑘 + 𝑣𝑦𝑖𝑗+1𝑘 − 𝑣𝑦𝑖𝑗−1𝑘 + 𝑣𝑧𝑖𝑗𝑘+1 − 𝑣𝑧𝑖𝑗𝑘−1
2ℎ
1
• Laplacian: 𝛻2𝑠 ≈ 𝑠𝑖+1𝑗𝑘 + 𝑠𝑖−1𝑗𝑘 + 𝑠𝑖𝑗+1𝑘 + 𝑠𝑖𝑗−1𝑘 + 𝑠𝑖𝑗𝑘+1 + 𝑠𝑖𝑗𝑘−1 − 6𝑠𝑖𝑗𝑘
ℎ2
• NOTE: These are based on computing the derivatives at the grid points on a uniform grid
Boundary Conditions
• We saw that computing various spatial
derivatives requires using values from
neighboring grid points
• What do we do on the boundaries where we
might not have neighboring grid points?
• The answer is problem specific, but it falls
within the general subject of boundary
conditions
Boundary Conditions
• There are some options for dealing with
derivatives at the boundaries
– Use directionally biased methods that shift the
derivative computation to the right or left by using
values to the right or left of the boundary (or
up/down…)
– In some cases, boundary values can be set to known
values, such as 0 for the fluid velocity at a solid wall
boundary (and 0 for all velocity derivatives)
• We’ll talk about some more specifics when we
get into fluid dynamics in the next lecture
Grid Structures
First Derivative at Grid Point
𝑠𝑖−2
𝑠𝑖+1 − 𝑠𝑖−1
𝑠𝑖−1 2ℎ
𝑠𝑖 𝑠𝑖+2
𝑠𝑖+1
𝑥𝑖
ℎ
𝜕𝑠 ∆𝑠 𝑠𝑖+1 − 𝑠𝑖−1
𝑥𝑖 ≈ 𝑥𝑖 =
𝜕𝑥 ∆𝑥 2ℎ
First Derivative at Midpoint
𝑠𝑖−2
𝑠𝑖+1 − 𝑠𝑖
𝑠𝑖−1 ℎ
𝑠𝑖 𝑠𝑖+2
𝑠𝑖+1
𝑥𝑖+1/2
ℎ
𝜕𝑠 ∆𝑠 𝑠𝑖+1 − 𝑠𝑖
𝑥𝑖+1Τ2 ≈ 𝑥𝑖+1Τ2 =
𝜕𝑥 ∆𝑥 ℎ
Midpoint Derivative
• If we want to calculate the derivative at the grid points, we use:
∆𝑠 𝑠𝑖+1 − 𝑠𝑖−1
𝑥 =
∆𝑥 𝑖 2ℎ
• If we want to calculate the derivative halfway between grid points, we can use:
∆𝑠 𝑠𝑖+1 − 𝑠𝑖
𝑥𝑖+1Τ2 =
∆𝑥 ℎ
• The second method is usually better because it uses a more localized estimate of
the derivative. It also makes use of all nearby data, instead of the first method,
which ignores the closest value of the scalar field available
• To make use of this however, one must formulate the equations of interest in a
way that is compatible, which tends to be problem-specific
Collocated Grids
• The finite difference derivative computations we
looked at so far are based on the assumption that
we want to calculate the derivatives at the exact
same points that we are storing the field values
• This is known as a collocated grid, since all values
of interest and their derivatives are collocated at
the same points
• However, this leads to the same inaccuracy in
computing derivatives that we see in 1D
problems
Staggered Grids
• When possible, it is often better to use a staggered grid, where certain
values are stored at the grid points and other values are stored between
points
• In fact, values can be stored at the grid points, on segment edges, on cell
faces, or in cell centers
• The 3 values of a 3D vector don’t even have to be stored in the same place
• For example, some fluid simulation approaches store the x-component of
velocity on the x-face of each cell, and the y-component on the y-face, etc.
Pressures are computed at the cell centers, based on the velocities
through the 6 faces of the cell
𝑣𝑖𝑗+1/2𝑘
𝑝𝑖𝑗𝑘
𝑣𝑖−1/2𝑗𝑘 𝑣𝑖+1/2𝑗𝑘
𝑣𝑖𝑗−1/2𝑘
Staggered Divergence
• Consider the case where each component of a vector is stored on the corresponding face
• If a cell is indexed as ijk, the vectors will be at the halfway values
• We compute the divergence at the center of cell ijk as:
1
= 𝑣 − 𝑣𝑥𝑖−1/2𝑗𝑘 + 𝑣𝑦𝑖𝑗+1/2𝑘 − 𝑣𝑦𝑖𝑗−1/2𝑘 + 𝑣𝑧𝑖𝑗𝑘+1/2 − 𝑣𝑧𝑖𝑗𝑘−1/2
ℎ 𝑥𝑖+1/2𝑗𝑘
Higher Order Methods
Higher Order Approximations
• We chose to construct our finite derivative operators by
using only the nearest neighboring values and using linear
(slope) approximations to the derivatives
• We could optionally use more nearby points to get a better
approximation to a derivative
• Higher order spatial methods like this can produce better
quality and more accurate simulations
• However, they make certain assumptions about
smoothness and behave poorly in areas of rapid change
• They also require special handling near the boundaries
• Higher order spatial methods are not too hard to design for
uniform grids, but are tricky to derive for other geometries
High Order Derivative
• Consider estimating the derivative on a grid
• With a linear method, we looked at two values and fit a
straight line between them and used the slope of that
line as the derivative
• With high order methods, we use a weighted blend
(usually derived through a Taylor series) at more than
two values to compute the slope at the point we’re
interested in
• These methods are straightforward to implement as
they mainly just involve computing a blend of nearby
values weighted by pre-specified coefficients
Fourth Order Midpoint Method
𝑠𝑖−2
𝑠𝑖+1 − 𝑠𝑖
𝑠𝑖−1 ℎ
𝑠𝑖 𝑠𝑖+2
𝑠𝑖+1