Module phi.physics.advect

Container for different advection schemes for grids and particles.

Examples:

  • semi_lagrangian (grid)
  • mac_cormack (grid)
  • runge_kutta_4 (particle)

Functions

def advect(field: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, integrator=<function euler>) ‑> phi.field._field.Field

Advect field along the velocity vectors using the specified integrator.

The behavior depends on the type of field:

Args

field
Field to be advected as Field.
velocity
Any Field that can be sampled in the elements of field.
dt
Time increment
integrator
ODE integrator for solving the movement.

Returns

Advected field of same type as field

def differential(u: phi.field._field.Field, velocity: phi.field._field.Field, density: float = 1.0, order=2, implicit: phiml.math._optimize.Solve = None, upwind=True) ‑> phi.field._field.Field

Computes the differential advection term using the differentiation Scheme indicated by order, ´implicit´ and upwind.

For a velocity field u, the advection term as it appears on the right-hand-side of a PDE is -u·∇u, including the negative sign.

For unstructured meshes, computes -1/V ∑_f (n·u_prev) u ρ A

Args

u
Scalar or vector-valued Field sampled on a CenteredGrid, StaggeredGrid or Mesh.
velocity
Field that can be sampled at the elements of u. For FVM, the advection term is typically linearized by setting velocity = previous_velocity. Passing velocity=u yields non-linear terms which cannot be traced inside linear functions.
order
Spatial order of accuracy. Higher orders entail larger stencils and more computation time but result in more accurate results assuming a large enough resolution. Supported for grids: 2 explicit, 4 explicit, 6 implicit (inherited from spatial_gradient() and resampling). Passing order=4 currently uses 2nd-order resampling. This is work-in-progress. For FVM, the order is used when interpolating centroid values to faces if needed.
implicit
When a Solve object is passed, performs an implicit operation with the specified solver and tolerances. Otherwise, an explicit stencil is used.
upwind
Whether to use upwind interpolation. Only supported for FVM at the moment.

Returns

Differential convection term as Field on the same geometry.

def euler(data: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, v0: phiml.math._tensors.Tensor = None) ‑> phiml.math._tensors.Tensor

Euler integrator.

def finite_difference(u: phi.field._field.Field, velocity: phi.field._field.Field, density: float = 1.0, order=2, implicit: phiml.math._optimize.Solve = None, upwind=True) ‑> phi.field._field.Field

Computes the differential advection term using the differentiation Scheme indicated by order, ´implicit´ and upwind.

For a velocity field u, the advection term as it appears on the right-hand-side of a PDE is -u·∇u, including the negative sign.

For unstructured meshes, computes -1/V ∑_f (n·u_prev) u ρ A

Args

u
Scalar or vector-valued Field sampled on a CenteredGrid, StaggeredGrid or Mesh.
velocity
Field that can be sampled at the elements of u. For FVM, the advection term is typically linearized by setting velocity = previous_velocity. Passing velocity=u yields non-linear terms which cannot be traced inside linear functions.
order
Spatial order of accuracy. Higher orders entail larger stencils and more computation time but result in more accurate results assuming a large enough resolution. Supported for grids: 2 explicit, 4 explicit, 6 implicit (inherited from spatial_gradient() and resampling). Passing order=4 currently uses 2nd-order resampling. This is work-in-progress. For FVM, the order is used when interpolating centroid values to faces if needed.
implicit
When a Solve object is passed, performs an implicit operation with the specified solver and tolerances. Otherwise, an explicit stencil is used.
upwind
Whether to use upwind interpolation. Only supported for FVM at the moment.

Returns

Differential convection term as Field on the same geometry.

def finite_rk4(data: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, v0: phiml.math._tensors.Tensor = None) ‑> phiml.math._tensors.Tensor

Runge-Kutta-4 integrator with Euler fallback where velocity values are NaN.

def mac_cormack(field: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, correction_strength=1.0, integrator=<function euler>) ‑> phi.field._field.Field

MacCormack advection uses a forward and backward lookup to determine the first-order error of semi-Lagrangian advection. It then uses that error estimate to correct the field values. To avoid overshoots, the resulting value is bounded by the neighbouring grid cells of the backward lookup.

Args

field
Field to be advected, one of (CenteredGrid, StaggeredGrid)
velocity
Vector field, need not be sampled at same locations as field.
dt
Time increment
correction_strength
The estimated error is multiplied by this factor before being applied. The case correction_strength=0 equals semi-lagrangian advection. Set lower than 1.0 to avoid oscillations.
integrator
ODE integrator for solving the movement.

Returns

Advected field of type type(field)

def points(points: Union[phi.field._field.Field, phi.geom._geom.Geometry, phiml.math._tensors.Tensor], velocity: phi.field._field.Field, dt: float, integrator=<function euler>)

Advects the sample points of a point cloud using a simple Euler step. Each point moves by an amount equal to the local velocity times dt.

Args

points
Points to be advected. Can be provided as position Tensor, Geometry or Field.
velocity
velocity sampled at the same points as the point cloud
dt
Euler step time increment
integrator
ODE integrator for solving the movement.

Returns

Advected points, same type as points().

def rk4(data: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, v0: phiml.math._tensors.Tensor = None) ‑> phiml.math._tensors.Tensor

Runge-Kutta-4 integrator.

def semi_lagrangian(field: phi.field._field.Field, velocity: phi.field._field.Field, dt: float, integrator=<function euler>) ‑> phi.field._field.Field

Semi-Lagrangian advection with simple backward lookup.

This method samples the velocity at the grid points of field to determine the lookup location for each grid point by walking backwards along the velocity vectors. The new values are then determined by sampling field at these lookup locations.

Args

field
quantity to be advected, stored on a grid (CenteredGrid or StaggeredGrid)
velocity
vector field, need not be compatible with with field.
dt
time increment
integrator
ODE integrator for solving the movement.

Returns

Field with same sample points as field