# Fractals/Mathematics/Vector field

Vector field

Here mainly numerical methods for time independent 2D vector fields are described.

# Dictionary

• vector function is a function that gives vector as an output
• field : space (plane, sphere, ... )
• field line is a line that is everywhere tangent to a given vector field
• scalar/ vector / tensor:
• Scalars are real numbers used in linear algebra. Scalar is a tensor of zero order
• Vector is a tensor of first order. Vector is an extension of scalar
• tensor is an extension of vector

## Vector

Forms of 2D vector:

• [z1] ( only one complex number when first point is known , for example z0 is origin
• [z0, z1] = two complex numbers
• 4 scalars ( real numbers)
• [x, y, dx , dy]
• [x0, y0, x1, y1]
• [x, y, angle, magnitude]
• 2 scalars : [x1, y1] for second complex number when first point is known , for example z0 is origin

The numerical gradient of a function:

• "is a way to estimate the values of the partial derivatives in each dimension using the known values of the function at certain points."

Gradient function G of function f at point (x0,y0)

  G(f,x0,y0) = (x1,y1)


Input

• function f
• point (x0,y0) where gradient is computed

Output:

• vector from (x0,yo) to (x1,y1) = gradient

See:

Computation:

• "The gradient is calculated as: (f(x + h) - f(x - h)) / (2*h) where h is a small number, typically 1e-5 f(x) will be called for each input elements with +h and -h pertubation. For gradient checking, recommend using float64 types to assure numerical precision."
• in matlab
• in R
• python

# Field types

• time independent (= stationary = Steady ) or time dependent ( unsteady flow)
• dimension: 2D, 3D, ...
• mesh ( grid) type
• scalar function
• potential
• electric, magnetic, ...
• vector function
• equation ( for symbolic computations) - ODE
• numerical values ( for numerical computations)
• field line integration method (scheme):

## Force field

A gravitational force fields

• the field lines are the solution of $g'=F$
• the trajectory of a test mass is the solution of $mg''=F$

where

• g is the standard gravity
• m is a mass
• F is a force field

An electric field

• The field lines are the paths that a point positive charge would follow as it is forced to move within the field. Field lines due to stationary charges have several important properties, including always originating from positive charges and terminating at negative charges, they enter all good conductors at right angles, and they never cross or close in on themselves.  The field lines are a representative concept; the field actually permeates all the intervening space between the lines. More or fewer lines may be drawn depending on the precision to which it is desired to represent the field. The study of electric fields created by stationary charges is called electrostatics.

# Algorithm

• plane (parameter plane or dynamic plane)
• scalar function
• vector function
• create scalar field using scalar function ( potential)
• create vector field from scalar field using vector function ( gradient of the potential)
• compute:
• filed lines ( stream lines )
• contour lines ( equipotential lines )
• map whole field using
• Line Integral Convolution (LIC)

## Field line computing

Problem Statement:

• Field line tracing ( not curve sketching}
• drawing contour maps ( in computer graphic) = Numerical continuation ( in math)
• compute an integral curve from a seed point through a vector field without any analysis of its structure on the uniform grid ( raster scan or pixels)

Methods ( solvers) available for the field-lines

• Euler
• RK2
• RK3
• RK4 - the original authors of those sampling algorithms: Runge und Kutta.
  None of these 4 methods generate an exact answer, but they are (from left to right) increasingly more accurate. They also take (from left to right) more and more time to finish as they require more samples for each iteration.
You won't be able to create reliably closed curves using iterative sampling methods as small errors at any step may be amplified in successive steps. There is also no guarantee that the field-line ends up in the exact coordinate where it started.
The Grasshopper metaball solver on the other hand uses a marching squares algorithm which is capable of finding closed loops because it is a grid-cell approach and sampling inaccuracy in one area doesn't carry over to another.
However the solving of iso-curves is a very different process    from the solving of particle trajectories through fields. ...
Typically field lines shoot to infinity rather than form closed loops. That is one reason why I chose the RK methods here, because marching-cubes is very bad at dealing with things that tend to infinity.

#### Construction

Given a vector field $\mathbf {F} (\mathbf {x} )$  and a starting point $\mathbf {x} _{\text{0}}$  a field line can be constructed iteratively by finding the field vector at that point $\mathbf {F} (\mathbf {x} _{\text{0}})$ . The unit tangent vector at that point is: $\mathbf {F} (\mathbf {x} _{\text{0}})/|\mathbf {F} (\mathbf {x} _{\text{0}})|$ . By moving a short distance $ds$  along the field direction a new point on the line can be found

$\mathbf {x} _{\text{1}}=\mathbf {x} _{\text{0}}+{\mathbf {F} (\mathbf {x} _{\text{0}}) \over |\mathbf {F} (\mathbf {x} _{\text{0}})|}ds$

Then the field at that point $\mathbf {F} (\mathbf {x} _{\text{1}})$  is found and moving a further distance $ds$  in that direction the next point of the field line is found

$\mathbf {x} _{\text{2}}=\mathbf {x} _{\text{1}}+{\mathbf {F} (\mathbf {x} _{\text{1}}) \over |\mathbf {F} (\mathbf {x} _{\text{1}})|}ds$

By repeating this and connecting the points,the field line can be extended as far as desired. This is only an approximation to the actual field line, since each straight segment isn't actually tangent to the field along its length, just at its starting point. But by using a small enough value for $ds$ , taking a greater number of shorter steps, the field line can be approximated as closely as desired. The field line can be extended in the opposite direction from $\mathbf {x} _{\text{0}}$  by taking each step in the opposite direction by using a negative step $-ds$ .

### rk4 numerical integration method

Fourth-order Runge-Kutta (RK4) in case of 2D time independent vector field

$F$  is a vector function that for each point p

p = (x, y)

in a domain assigns a vector v

$v=F(p)=F(x,y)=(F_{1}(x,y),F_{2}(x,y))$

where each of the functions $F_{i}$  is a scalar function:

$F_{i}:\mathbb {R} ^{2}\to \mathbb {R}$

A field line is a line that is everywhere tangent to a given vector field.

Let r(s) be a field line given by a system of ordinary differential equations, which written on vector form is:

${\frac {dr}{ds}}=F(r(s))$

where:

• s representing the arc length along the field line, like for example continous iteration count
• $r(0)=r_{0}$  is a seed point

#### 2 variables

Given a seed point $r_{0}$  on the field line, the update rule ( RK4) to find the next point $r_{i}$ along the field line is

$r_{i+1}=r_{i}+{\frac {h(k_{1}+2k_{2}+2k_{3}+k_{4})}{6}}$

where:

• h is the step size along field line = ds
• k are the intermediate vectors:

${\begin{array}{lcl}k_{1}=F(r_{i})\\k_{2}=F(r_{i}+{\frac {h}{2}}k_{1})\\k_{3}=F(r_{i}+{\frac {h}{2}}k_{2})\\k_{4}=F(r_{i}+hk_{3})\end{array}}$

#### only x

Here

$y'={\frac {dy}{dx}}=f(x)$ Given a seed point $r_{0}$  on the field line, the update rule ( RK4) to find the next point $r_{i}$ along the field line is

$x_{i+1}=x_{i}+dx=x_{i}+h$ $y_{i+1}=y_{i}+{\frac {h(k_{1}+2k_{2}+2k_{3}+k_{4})}{6}}$ where:

• h is the step size along field line = dx
• k are the intermediate vectors:

${\begin{array}{lcl}k_{1}=F(x_{i})\\k_{2}=F(x_{i}+{\frac {h}{2}}k_{1})\\k_{3}=F(x_{i}+{\frac {h}{2}}k_{2})\\k_{4}=F(x_{i}+hk_{3})\end{array}}$

Examples:

• ${\frac {dy}{dx}}=x$
• $y^{'}+x^{2}+x=0$ 
• ${\frac {dy}{dx}}=x^{2}-x-2$

## Visualisation of vector field

Plot types (Visualization Techniques for Flow Data) : 

• Glyphs = Icons or signs for visualizing vector fields
• simplest glyph = Line segment (hedgehog plots)
• arrow plot = quiver plot = Hedgehogs (global arrow plots)
• Characteristic Lines 
• streamlines = curve everywhere tangential to the instantaneous vector (velocity) field (time independent vector field). For time independent vector field streaklines = Path lines = streak lines 
• texture (line integral convolution = LIC)
• Topological skeleton 
• fixed point extraction ( Jacobian)
  "path lines, streak lines, and stream lines are identical for stationary flows" Leif Kobbelt

### quiver plot

Definition

• "A quiver plot displays velocity vectors as arrows with components (u,v) at the points (x,y)"

### stream plot

A stream plot uses stream lines

# Example fields

## fBM

fBM stands for Fractional Brownian Motion

## SDF

SDF = Signed Distance Function