# Theory Reference

## Magnetic Fields Calculations

The primary purpose of this library is to compute the magnetic fields 
generated by a *source* domain and evaluated at a *target* domain. The source domain is 
generally assumed to be unaffected by the response of the target domain. 

### Biot-Savart Law 

The Biot-Savart Law (Ref [1](references.md)) underpins the majority of calculations:
$$ B(\vec{r}) = \frac{\mu_0}{4\pi} \iiint_V \frac{(\vec{J}dV) \times \vec{r'}}{|\vec{r'}|^3} $$

A closed-form solution to this volume integral does not exist for the vast majority of
geometries. Instead, this library operates on geometries that have been discretized into 
finite elements. However, finite element theory is generally not employed in the solvers.

### Point Sources

`oersted`'s solvers act on two types of sources: "point" representations of elements, 
and 4-node (linear) tetrahedra. Point representations of an element are developed by 
treating the Biot-Savart law as a discrete sum instead of an exact integral:

$$ B(\vec{r}) = \frac{\mu_0}{4\pi} \sum_{i=0}^{N_e} \frac{(\vec{J_i}\cdot v_i) \times \vec{r_i'}}{|\vec{r_i'}|^3} $$

In this formulation, the solution is assumed to converge when the number of elements in 
the mesh extends to infinity. In practice, this offers a high-quality representation of 
the far-field (several element lengths between source and target), but average errors 
with the mesh are much higher. However, the force calculation has a smoothing (integrating)
effect and resultant Lorentz force calculations typically converge easily. 

#### Current Density Source

Magnetic fields generated by point sources carrying electrical currents are computed 
according to: 
$$ B(\vec{r}) = \frac{\mu_0}{4\pi} \frac{(\vec{J_i}\cdot v_i) \times \vec{r_i'}}{|\vec{r_i'}|^3} $$

#### Magnetized Material (Dipole) Source

Magnetic fields generated by a point source with an associated magnetic moment are 
computed according to:
$$ H(r) = \frac{1}{4\pi} (\frac{3r(m\cdot r)}{|r|^5} - \frac{m}{|r|^3}) $$

### Finite Element Sources

Finite element sources are treated by computing the exact Biot-Savart integral over the
element. This means that the solution is *exact and analytic*, save for the following 
sources of error:

- When the edges of the element fail to adequately capture the surface of a geometry 
(e.g. a coarse mesh of a curved surface)  
- When the mesh discretization fails to properly resolve the current density distribution
with a conductor 

Otherwise, the calculation is numerically accurate to floating point precision.

Notes:

- Finite element integration is extremely computationally costly; use of the octree (hybrid)
solver is recommended for meshes > 25K elements

#### Current Density Source

This library follows the procedures described in Ref [3](references.md), equations 54 and 52. First, the 
volume integral is decomposed into a summation of surface integrals. Each of these surface
integrals is then decomposed into a summation of line integrals. 

$$ B(r) = - \frac{\mu_0}{4\pi} J \times \lbrace{ \sum_{i=1}^{N_f=4} \hat{n}_i (\sum_{j=1}^{N_e=3} y_P''[E(x''_{Q2} - x''_P, y''_P, z''_P) - E(x''_{Q1}, y''_P, z''_P)]) \rbrace}$$


#### Magnetization Source

$$ B(r) = - \frac{\mu_0}{4\pi} M \cdot \nabla \lbrace{ \sum_{i=1}^{N_f=4} \hat{n}_i (\sum_{j=1}^{N_e=3} y_P''[E(x''_{Q2} - x''_P, y''_P, z''_P) - E(x''_{Q1}, y''_P, z''_P)]) \rbrace}$$


## Force Calculation

Forces on magnetic materials and current-carrying conductors are calculated according 
to the following equations:

### Lorentz Force

$$ \vec{F} = \vec{J} \times \vec{B} $$

Notes:  

- Lorentz forces are volumetric and therefore calculated on a per-element basis within 
the mesh  


### Maxwell Force

The Maxwell stress tensor is defined as:

$$ S = \frac{1}{\mu_0} \begin{bmatrix} B_x^2 - B^2/2 & B_x B_y & B_x B_z \\ B_y B_x & B_y^2 - B^2/2 & B_y B_z \\ B_z B_x & B_z B_y & B_z^2 - B^2/2 \end{bmatrix} $$

$$B = \sqrt{B_x^2 + B_y^2 + B_z^2} $$

and can be integrated over a surface to find the total force acting on a body:
$$ F = \int\int{S\cdot \hat{n} \space dA }$$

Notes:  

- `oersted`'s implementation of the Maxwell stress tensor is mesh-sensitive and 
requires a refinement study. Be careful with the results.  
- The Maxwell stress tensor is the most general-purpose of the force outputs and 
can be used for both magnetized material and current-carrying conductors  

### Kelvin Force

The Kelvin force is defined as:

$$ F = (m \cdot \nabla)B $$

Where:  

- $m = M \cdot V$ is the magnetic moment  
- $B$ is the magnetic flux density from all background sources, *excluding the body itself*   
- $\nabla$ is the gradient operator  

Notes:  

- The Kelvin force is much less mesh-sensitive than the Maxwell stress tensor  
- Fields should be computed on the body as if the body itself was not magnetized  
- The Kelvin force cannot compute forces on current-carrying conductors with unity magnetic permeability. 