53
Differential Vector Calculus Steve Rotenberg CSE291: Physics Simulation UCSD Spring 2019

Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Differential Vector Calculus

Steve RotenbergCSE291: Physics Simulation

UCSDSpring 2019

Page 2: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 3: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Del Symbol

• The Del symbol 𝛻 is useful for defining several types of spatial derivatives of fields

𝛻 =𝜕

𝜕𝑥

𝜕

𝜕𝑦

𝜕

𝜕𝑧

𝑇

• Technically, 𝛻 by itself is neither a vector nor an operator, although it acts like both. It is used to define the gradient 𝛻, divergence 𝛻 ∙, curl 𝛻 ×, and Laplacian 𝛻2 operators

Page 4: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Gradient

• The gradient is a generalization of the concept of a derivative

𝛻𝑠 =𝜕𝑠

𝜕𝑥

𝜕𝑠

𝜕𝑦

𝜕𝑠

𝜕𝑧

𝑇

• When applied to a scalar field, the result is a vector pointing in the direction the field is increasing and the magnitude indicates the rate of increase

• In 1D, this reduces to the standard derivative (slope)

Page 5: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Gradient

• The gradient 𝛻𝑠 is a vector that points “uphill” in the direction that scalar field s is increasing

• The magnitude of 𝛻𝑠 is equal to the rate that s is increasing per unit of distance

𝛻𝑠

𝛻𝑠

𝛻𝑠

𝛻𝑠

𝛻𝑠=0

𝛻𝑠=0

Page 6: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Divergence

• The divergence of a vector field is a scalar measure ofhow much the vectors are expanding

𝛻 ∙ 𝐯 =𝜕𝑣𝑥𝜕𝑥

+𝜕𝑣𝑦

𝜕𝑦+𝜕𝑣𝑧𝜕𝑧

• For example, when air is heated in a region, it will locally expand, causing a positive divergence in the region of expansion

• The divergence operator works on a vector field and produces a scalar field as a result

Page 7: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Divergence

• The divergence is positive where the field is expanding:

𝛻 ∙ 𝐯 > 0

• The divergence is negative where the field is contracting:

𝛻 ∙ 𝐯 < 0

• A constant field has zero divergence, as can many others:

𝛻 ∙ 𝐯 = 0

Page 8: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Curl

• The curl operator produces a new vector field that measures the rotation of the original vector field

𝛻 × 𝐯 =𝜕𝑣𝑧𝜕𝑦

−𝜕𝑣𝑦

𝜕𝑧

𝜕𝑣𝑥𝜕𝑧

−𝜕𝑣𝑧𝜕𝑥

𝜕𝑣𝑦

𝜕𝑥−𝜕𝑣𝑥𝜕𝑦

𝑇

• For example, if the air is circulating in a particular region, then the curl in that region will represent the axis of rotation

• The magnitude of the curl is twice the angular velocity of the vector field

Page 9: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Curl

• A counter-clockwise rotating field has a curl vector pointing out of the screen towards the viewer, perpendicular to the rotation plane

• A constant vector field has zero curl: 𝛻 × 𝐯 = 0 0 0 𝑇

Page 10: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Laplacian

• The Laplacian operator is one type of second derivative of a scalar or vector field

𝛻2 = 𝛻 ∙ 𝛻 =𝜕2

𝜕𝑥2+

𝜕2

𝜕𝑦2+

𝜕2

𝜕𝑧2

• Just as in 1D where the second derivative relates to the curvature of a function, the Laplacian relates to the curvature of a field

• The Laplacian of a scalar field is another scalar field:

𝛻2𝑠 =𝜕2𝑠

𝜕𝑥2+𝜕2𝑠

𝜕𝑦2+𝜕2𝑠

𝜕𝑧2

• And the Laplacian of a vector field is another vector field

𝛻2𝐯 =𝜕2𝐯

𝜕𝑥2+𝜕2𝐯

𝜕𝑦2+𝜕2𝐯

𝜕𝑧2

Page 11: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Laplacian

• The Laplacian is positive in an area of the field that is surrounded by higher values

• The Laplacian is negative where the field is surrounded by lower values

• The Laplacian is zero where the field is either flat, linear sloped, or the positive and negative curvatures cancel out (saddle points)

Page 12: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Del Operations

• Del: 𝛻 =𝜕

𝜕𝑥

𝜕

𝜕𝑦

𝜕

𝜕𝑧𝑇

• Gradient: 𝛻𝑠 =𝜕𝑠

𝜕𝑥

𝜕𝑠

𝜕𝑦

𝜕𝑠

𝜕𝑧

𝑇

• Divergence: 𝛻 ∙ 𝐯 =𝜕𝑣𝑥

𝜕𝑥+

𝜕𝑣𝑦

𝜕𝑦+

𝜕𝑣𝑧

𝜕𝑧

• Curl: 𝛻 × 𝐯 =𝜕𝑣𝑧

𝜕𝑦−

𝜕𝑣𝑦

𝜕𝑧

𝜕𝑣𝑥

𝜕𝑧−

𝜕𝑣𝑧

𝜕𝑥

𝜕𝑣𝑦

𝜕𝑥−

𝜕𝑣𝑥

𝜕𝑦

𝑇

• Laplacian: 𝛻2𝑠 =𝜕2𝑠

𝜕𝑥2+

𝜕2𝑠

𝜕𝑦2+

𝜕2𝑠

𝜕𝑧2

Page 13: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Numerical Representation of Fields

Page 14: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 15: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 16: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 17: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Uniform Grids

Page 18: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 19: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Hierarchical Grids

Page 20: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Hierarchical Grids

Page 21: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 22: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Irregular Mesh

Page 23: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Adaptive Meshes

Page 24: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 25: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Particle Based

Page 26: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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)

Page 27: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Derivative Computation

Page 28: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 29: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference First Derivative

• The derivative (slope) of a 1D function 𝑠 𝑥stored uniformly spaced at values of 𝑥𝑖 can be approximated by finite differencing:

𝑑𝑠

𝑑𝑥𝑥𝑖 ≈

∆𝑠

∆𝑥𝑥𝑖 =

𝑠𝑖+1 − 𝑠𝑖−12ℎ

• Where ℎ is the grid size (ℎ = 𝑥𝑖+1 − 𝑥𝑖)

Page 30: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference First Derivative

𝑠𝑖𝑠𝑖+1

𝑠𝑖+2

𝑠𝑖−1

𝑠𝑖−2 𝑠𝑖+1 − 𝑠𝑖−12ℎ

𝑑𝑠

𝑑𝑥𝑥𝑖 ≈

∆𝑠

∆𝑥𝑥𝑖 =

𝑠𝑖+1 − 𝑠𝑖−12ℎ

Page 31: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 32: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Neighboring Grid Points

𝑠𝑖𝑗𝑘 𝑠𝑖+1𝑗𝑘𝑠𝑖−1𝑗𝑘

𝑠𝑖𝑗+1𝑘

𝑠𝑖𝑗−1𝑘

𝑠𝑖𝑗𝑘−1

𝑠𝑖𝑗𝑘+1

Page 33: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Gradient

• We can compute the finite difference gradient 𝛻𝑠 at grid point 𝑖𝑗𝑘 from 𝑠 values at neighboring grid points

𝛻𝑠 𝐱𝑖𝑗𝑘 =𝜕𝑠

𝜕𝑥

𝜕𝑠

𝜕𝑦

𝜕𝑠

𝜕𝑧

𝑇

≈∆𝑠

∆𝑥

∆𝑠

∆𝑦

∆𝑠

∆𝑧

𝑇

=𝑠𝑖+1𝑗𝑘 − 𝑠𝑖−1𝑗𝑘

2ℎ

𝑠𝑖𝑗+1𝑘 − 𝑠𝑖𝑗−1𝑘

2ℎ

𝑠𝑖𝑗𝑘+1 − 𝑠𝑖𝑗𝑘−1

2ℎ

𝑇

=1

2ℎ

𝑠𝑖+1𝑗𝑘 − 𝑠𝑖−1𝑗𝑘𝑠𝑖𝑗+1𝑘 − 𝑠𝑖𝑗−1𝑘𝑠𝑖𝑗𝑘+1 − 𝑠𝑖𝑗𝑘−1

Page 34: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Divergence

• We can compute a finite difference of the divergence at grid point ijk in a similar fashion:

𝛻 ∙ 𝐯 𝐱𝑖𝑗𝑘 =𝜕𝑣𝑥𝜕𝑥

+𝜕𝑣𝑦

𝜕𝑦+𝜕𝑣𝑧𝜕𝑧

≈∆𝑣𝑥∆𝑥

+∆𝑣𝑦

∆𝑦+∆𝑣𝑧∆𝑧

=𝑣𝑥𝑖+1𝑗𝑘 − 𝑣𝑥𝑖−1𝑗𝑘

2ℎ+𝑣𝑦𝑖𝑗+1𝑘 − 𝑣𝑦𝑖𝑗−1𝑘

2ℎ+𝑣𝑧𝑖𝑗𝑘+1 − 𝑣𝑧𝑖𝑗𝑘−1

2ℎ

=1

2ℎ𝑣𝑥𝑖+1𝑗𝑘 − 𝑣𝑥𝑖−1𝑗𝑘 + 𝑣𝑦𝑖𝑗+1𝑘 − 𝑣𝑦𝑖𝑗−1𝑘 + 𝑣𝑧𝑖𝑗𝑘+1 − 𝑣𝑧𝑖𝑗𝑘−1

Page 35: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Curl

• For the finite difference curl at grid point ijk we have:

𝛻 × 𝐯 𝐱𝑖𝑗𝑘 =𝜕𝑣𝑧𝜕𝑦

−𝜕𝑣𝑦

𝜕𝑧

𝜕𝑣𝑥𝜕𝑧

−𝜕𝑣𝑧𝜕𝑥

𝜕𝑣𝑦

𝜕𝑥−𝜕𝑣𝑥𝜕𝑦

𝑇

≈∆𝑣𝑧∆𝑦

−∆𝑣𝑦

∆𝑧

∆𝑣𝑥∆𝑧

−∆𝑣𝑧∆𝑥

∆𝑣𝑦

∆𝑥−∆𝑣𝑥∆𝑦

𝑇

=1

2ℎ

𝑣𝑧𝑖𝑗+1𝑘 − 𝑣𝑧𝑖𝑗−1𝑘 − 𝑣𝑦𝑖𝑗𝑘+1 − 𝑣𝑦𝑖𝑗𝑘−1

𝑣𝑥𝑖𝑗𝑘+1 − 𝑣𝑥𝑖𝑗𝑘−1 − 𝑣𝑧𝑖+1𝑗𝑘 − 𝑣𝑧𝑖−1𝑗𝑘

𝑣𝑦𝑖+1𝑗𝑘 − 𝑣𝑦𝑖−1𝑗𝑘 − 𝑣𝑥𝑖𝑗+1𝑘 − 𝑣𝑥𝑖𝑗−1𝑘

Page 36: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Second Derivative

• The second derivative can be approximated by finite differencing in a similar way:

𝑑2𝑠

𝑑𝑥2𝑥𝑖 ≈

∆2𝑠

∆𝑥2=∆

∆𝑠∆𝑥

∆𝑥

=

𝑠𝑖+1 − 𝑠𝑖ℎ

−𝑠𝑖 − 𝑠𝑖−1

ℎℎ

=𝑠𝑖+1 − 2𝑠𝑖 + 𝑠𝑖−1

ℎ2

Page 37: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Second Derivative

𝑠𝑖𝑠𝑖+1

𝑠𝑖+2

𝑠𝑖−1

𝑠𝑖−2

𝑠𝑖+1 − 𝑠𝑖ℎ

𝑠𝑖 − 𝑠𝑖−1ℎ

𝑑2𝑠

𝑑𝑥2𝑥𝑖 ≈

∆ ∆𝑠∆𝑥∆𝑥

𝑥𝑖 =

𝑠𝑖+1 − 𝑠𝑖ℎ

−𝑠𝑖 − 𝑠𝑖−1

ℎℎ

=𝑠𝑖+1 − 2𝑠𝑖 + 𝑠𝑖−1

ℎ2

Page 38: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Laplacian

• The finite difference Laplacian at point ijk is:

𝛻2𝑠 𝐱𝑖𝑗𝑘 =𝜕2𝑠

𝜕𝑥2+𝜕2𝑠

𝜕𝑦2+𝜕2𝑠

𝜕𝑧2

≈∆2𝑠

∆𝑥2+∆2𝑠

∆𝑦2+∆2𝑠

∆𝑧2

=𝑠𝑖+1𝑗𝑘 − 2𝑠𝑖𝑗𝑘 + 𝑠𝑖−1𝑗𝑘

ℎ2+𝑠𝑖𝑗+1𝑘 − 2𝑠𝑖𝑗𝑘 + 𝑠𝑖𝑗−1𝑘

ℎ2+𝑠𝑖𝑗𝑘+1 − 2𝑠𝑖𝑗𝑘 + 𝑠𝑖𝑗𝑘−1

ℎ2

=1

ℎ2𝑠𝑖+1𝑗𝑘 + 𝑠𝑖−1𝑗𝑘 + 𝑠𝑖𝑗+1𝑘 + 𝑠𝑖𝑗−1𝑘 + 𝑠𝑖𝑗𝑘+1 + 𝑠𝑖𝑗𝑘−1 − 6𝑠𝑖𝑗𝑘

Page 39: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Finite Difference Operations

• Gradient: 𝛻𝑠 ≈1

2ℎ

𝑠𝑖+1𝑗𝑘 − 𝑠𝑖−1𝑗𝑘𝑠𝑖𝑗+1𝑘 − 𝑠𝑖𝑗−1𝑘𝑠𝑖𝑗𝑘+1 − 𝑠𝑖𝑗𝑘−1

• Divergence: 𝛻 ∙ 𝐯 ≈1

2ℎ𝑣𝑥𝑖+1𝑗𝑘 − 𝑣𝑥𝑖−1𝑗𝑘 + 𝑣𝑦𝑖𝑗+1𝑘 − 𝑣𝑦𝑖𝑗−1𝑘 + 𝑣𝑧𝑖𝑗𝑘+1 − 𝑣𝑧𝑖𝑗𝑘−1

• Curl: 𝛻 × 𝐯 ≈1

2ℎ

𝑣𝑧𝑖𝑗+1𝑘 − 𝑣𝑧𝑖𝑗−1𝑘 − 𝑣𝑦𝑖𝑗𝑘+1 − 𝑣𝑦𝑖𝑗𝑘−1

𝑣𝑥𝑖𝑗𝑘+1 − 𝑣𝑥𝑖𝑗𝑘−1 − 𝑣𝑧𝑖+1𝑗𝑘 − 𝑣𝑧𝑖−1𝑗𝑘

𝑣𝑦𝑖+1𝑗𝑘 − 𝑣𝑦𝑖−1𝑗𝑘 − 𝑣𝑥𝑖𝑗+1𝑘 − 𝑣𝑥𝑖𝑗−1𝑘

• Laplacian: 𝛻2𝑠 ≈1

ℎ2𝑠𝑖+1𝑗𝑘 + 𝑠𝑖−1𝑗𝑘 + 𝑠𝑖𝑗+1𝑘 + 𝑠𝑖𝑗−1𝑘 + 𝑠𝑖𝑗𝑘+1 + 𝑠𝑖𝑗𝑘−1 − 6𝑠𝑖𝑗𝑘

• NOTE: These are based on computing the derivatives at the grid points on a uniform grid

Page 40: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 41: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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 weget into fluid dynamics in the next lecture

Page 42: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Grid Structures

Page 43: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

First Derivative at Grid Point

𝑠𝑖𝑠𝑖+1

𝑠𝑖+2

𝑠𝑖−1

𝑠𝑖−2 𝑠𝑖+1 − 𝑠𝑖−12ℎ

𝜕𝑠

𝜕𝑥𝑥𝑖 ≈

∆𝑠

∆𝑥𝑥𝑖 =

𝑠𝑖+1 − 𝑠𝑖−12ℎ

𝑥𝑖

Page 44: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

First Derivative at Midpoint

𝑠𝑖𝑠𝑖+1

𝑠𝑖+2

𝑠𝑖−1

𝑠𝑖−2𝑠𝑖+1 − 𝑠𝑖

𝜕𝑠

𝜕𝑥𝑥𝑖+ Τ1 2 ≈

∆𝑠

∆𝑥𝑥𝑖+ Τ1 2 =

𝑠𝑖+1 − 𝑠𝑖ℎ

𝑥𝑖+1/2

Page 45: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Midpoint Derivative

• If we want to calculate the derivative at the grid points, we use:

∆𝑠

∆𝑥𝑥𝑖 =

𝑠𝑖+1 − 𝑠𝑖−12ℎ

• If we want to calculate the derivative halfway between grid points, we can use:

∆𝑠

∆𝑥𝑥𝑖+ Τ1 2 =

𝑠𝑖+1 − 𝑠𝑖ℎ

• 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

Page 46: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 47: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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𝑘

𝑝𝑖𝑗𝑘

Page 48: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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/2𝑗𝑘 − 𝑣𝑥𝑖−1/2𝑗𝑘

ℎ+𝑣𝑦𝑖𝑗+1/2𝑘 − 𝑣𝑦𝑖𝑗−1/2𝑘

ℎ+𝑣𝑧𝑖𝑗𝑘+1/2 − 𝑣𝑧𝑖𝑗𝑘−1/2

=1

ℎ𝑣𝑥𝑖+1/2𝑗𝑘 − 𝑣𝑥𝑖−1/2𝑗𝑘 + 𝑣𝑦𝑖𝑗+1/2𝑘 − 𝑣𝑦𝑖𝑗−1/2𝑘 + 𝑣𝑧𝑖𝑗𝑘+1/2 − 𝑣𝑧𝑖𝑗𝑘−1/2

Page 49: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Higher Order Methods

Page 50: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 51: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

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

Page 52: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

Fourth Order Midpoint Method

𝑠𝑖𝑠𝑖+1

𝑠𝑖+2

𝑠𝑖−1

𝑠𝑖−2𝑠𝑖+1 − 𝑠𝑖

𝑑𝑠

𝑑𝑥𝑥𝑖+ Τ1 2 ≈

𝑠𝑖−1 − 27𝑠𝑖 + 27𝑠𝑖+1 − 𝑠𝑖+224ℎ

Page 53: Differential Vector Calculus - Computer Science · 2019-04-23 · differential vector calculus, we turn to the issue of computer implementation •The Del operations are defined in

High Order Derivatives

𝑑𝑠

𝑑𝑥𝑥𝑖 ≈

−𝑠𝑖−1 + 𝑠𝑖+12ℎ

𝑑𝑠

𝑑𝑥𝑥𝑖 ≈

𝑠𝑖−2 − 8𝑠𝑖−1 + 8𝑠𝑖+1 − 𝑠𝑖+212ℎ

𝑑𝑠

𝑑𝑥𝑥𝑖 ≈

−𝑠𝑖−3 + 9𝑠𝑖−2 − 45𝑠𝑖−1 + 45𝑠𝑖+1 − 9𝑠𝑖+2 + 𝑠𝑖+360ℎ