Field Integration Techniques

Many analysis techniques for vector fields require solving an initial value problem for an arbitrary set of seed points and evaluating such solutions at a chosen resolution. Kamodo makes it easy to generate fieldline solutions by providing a function decorator that wraps scipy's powerful solve_ivp function. Each family of solutions is represented by a single function of a complex parameter. We illustrate the flexibility of this approach in the example below.

# initialize
from plotly.offline import iplot, plot, init_notebook_mode
init_notebook_mode(connected = True)

from kamodo import Kamodo, event, pointlike, kamodofy, solve
import numpy as np
import pandas as pd
kj/filesystem-disk-unix.c++:1690: warning: PWD environment variable doesn't match current directory; pwd = /Users/asherp/git/ensemblegovservices/kamodo-core

Dipole field model

We use the following dipole field model that can accept (m,) and (1,m), and (n,m) arrays.

def Bdip(rvec):
    """Need math to work in a variety of arg shapes"""
    muvec = Bdip.muvec    
    r = np.linalg.norm(rvec, axis = 1)
    r[r==0] = np.nan

    try:
        rhat = rvec/r
    except:
        rhat = (rvec.T/r).T

    try:
        result = 3*np.dot(rhat, muvec.T)
    except:
        result = 3*np.dot(rhat.T, muvec.T).T


    result = (rhat.T*result).T

    try:
        result = result - muvec
    except:
        result = (result - muvec.T).T

    try:
        result = result/r**3
    except:
        result = (result.T/r**3).T

    return result

# set dipole moment
Bdip.muvec = np.array([0, 0, -1]) 

# pointlike enforces dimensionality
Bdip = pointlike(Bdip, '(n,m)->(n,m)', [float], squeeze = 0)
kamodo = Kamodo()
kamodo['Bvec'] = Bdip # register the dipole field
kamodo
\begin{equation}\vec{B}{\left(\vec{r} \right)} = \lambda{\left(\vec{r} \right)}\end{equation}

Bvec works on a list of points and on individual points

kamodo.Bvec([[1,0,0], # x,y,z
             [2,0,0],
             [0,0,1],
             [0,0,2],])
array([[ 0.   ,  0.   ,  1.   ],
       [ 0.   ,  0.   ,  0.125],
       [-0.   , -0.   , -2.   ],
       [-0.   , -0.   , -0.25 ]])
kamodo.Bvec([1,0,0])
array([0., 0., 1.])

Normalization

Instead of solving the initial value problem on the original field, we will be solving on the normalized field. This will mean that the integral path is the same as the arclength, allowing us to control the visual fidelity of the resulting field.

Create a normalization function to be applied to our field

@kamodofy(equation = "\\vec{y}/\\sqrt{\\vec{y} \\cdot \\vec{y}}")
@pointlike(signature = '(m,n)->(m,n)', squeeze = 0)
def normalized(yvec):   
    r = np.linalg.norm(yvec, axis = 1)
    r[r==0] = np.nan

    try:
        return yvec/r
    except:
        return (yvec.T/r).T


kamodo['nhat'] = normalized

Create a normalized field

kamodo['bhat'] = "nhat(Bvec)"
kamodo
\begin{equation}\vec{B}{\left(\vec{r} \right)} = \lambda{\left(\vec{r} \right)}\end{equation} \begin{equation}\hat{n}{\left(\vec{y} \right)} = \vec{y}/\sqrt{\vec{y} \cdot \vec{y}}\end{equation} \begin{equation}\hat{b}{\left(\vec{r} \right)} = \hat{n}{\left(\vec{B}{\left(\vec{r} \right)} \right)}\end{equation}
kamodo.bhat([[1,0,0], # x,y,z
             [2,0,0],
             [0,0,1],
             [0,0,2]])
array([[ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [-0., -0., -1.],
       [-0., -0., -1.]])

Solving the initial value problem

Generate a set of seed points for integration

x0 = np.linspace(-np.pi,np.pi,6)
y0 = np.linspace(-np.pi,np.pi,6)
z0 = 1

seeds = np.array(np.column_stack([c.ravel() for c in np.meshgrid(x0,y0,z0)]))

Create a stopping boundary for field line integrator

@event
def boundary(s, rvec):
    r = np.linalg.norm(rvec)

    if np.isnan(r):
        result = 0
    else:
        result = r - 1
    return result

Solve the initial value problem for the normalized field

kamodo['svec'] = solve(kamodo.bhat, # the field to be solved
                       seeds, # the initial positions
                       's', # the name of the integration parameter
                       (0,30), # the span to integrate over
                       npoints = 60, # the number of points to evaluate the solution
                       events = boundary, # stop at the boundary
                      )
kamodo
\begin{equation}\vec{B}{\left(\vec{r} \right)} = \lambda{\left(\vec{r} \right)}\end{equation} \begin{equation}\hat{n}{\left(\vec{y} \right)} = \vec{y}/\sqrt{\vec{y} \cdot \vec{y}}\end{equation} \begin{equation}\hat{b}{\left(\vec{r} \right)} = \hat{n}{\left(\vec{B}{\left(\vec{r} \right)} \right)}\end{equation} \begin{equation}\vec{s}{\left(s \right)} = \lambda{\left(s \right)}\end{equation}

The solver returns a family of solutions, represented as a single function of a complex array, where is a complex array.

Evaluating the Solutions

On evaluation, returns a pandas dataframe.

kamodo.svec().head()


seed, integral
0

 
1

 
2

 
(0.0, -6.610169491525424) -0.347547 -0.347547 -0.924155
(0.0, -6.101694915254237) -0.615886 -0.615886 -1.26158
(0.0, -5.59322033898305) -0.922735 -0.922735 -1.52547
(0.0, -5.084745762711864) -1.25696 -1.25696 -1.71314
(0.0, -4.576271186440678) -1.60841 -1.60841 -1.82219

When using the default argument above, the solution evaluates at a resolution of npoints/span, stopping at the boundary.

Complex parameterization

Kamodo represents the family of solutions to the initial value problem as a single function of a complex array.

The floor of the real part of the input parameter corresponds to the original seed array:

kamodo.svec([0,1,2]).values
array([[-3.14159265, -3.14159265,  1.        ],
       [-1.88495559, -3.14159265,  1.        ],
       [-0.62831853, -3.14159265,  1.        ]])

compare with original seeds:

seeds[[0,1,2]]
array([[-3.14159265, -3.14159265,  1.        ],
       [-1.88495559, -3.14159265,  1.        ],
       [-0.62831853, -3.14159265,  1.        ]])

The imaginary part denotes the integral along the corresponding solution. Here, we can choose evaluation points that were not in the original solution. Parameters outside the original span will be extrapolated.

kamodo.svec([-6j, -5j, 0, 5j, 6j, 4 + 4j, 4 -5.777j])


seed, integral
0

 
1

 
2

 
(0.0, -6.0) -0.674502 -0.674502 -1.3205
(0.0, -5.0) -1.31457 -1.31457 -1.73723
(0.0, 0.0) -3.14159 -3.14159 1
(0.0, 5.0) -0.120606 -0.120606 0.491892
(0.0, 6.0) 0.125472 0.125472 -0.393292
(4.0, 4.0) 0.094223 -0.157038 0.48174
(4.0, -5.777) 0.234804 -0.39134 -0.827054

Plotting Fieldlines

We can quickly generate plots for all fieldlines at the default resolution by calling plot with the name of the fieldlines solution.

import plotly.io as pio
fig = kamodo.plot('svec')
pio.write_image(fig, './images/fieldlines.svg')

images/fieldlines.svg

To show the direction of the field at each point, we can evaluate

fig = kamodo.plot('svec', 
                  bhat = dict(rvec = kamodo.svec()))
pio.write_image(fig,'./images/fieldlines_vectors.svg')

fieldlines

Integration totals

To compute the total integral for each fieldline individually, we need a function to subtract the integration results at the endpoints.

def integral(fieldline):
    endpoints = fieldline.reset_index().integral.iloc[[0,-1]]
    return endpoints.values[-1] - endpoints.values[0]
totals = []
for seed, fieldline in kamodo.svec().groupby(level = 'seed'):
    totals.append(integral(fieldline))

totals[:5]
[10.677966101694915,
 8.64406779661017,
 7.627118644067796,
 7.627118644067796,
 8.64406779661017]

Alternatively, we can use pandas' aggregation methods to apply our function on each fieldline.

kamodo.svec().groupby(level='seed').aggregate(integral)


seed
0

 
1

 
2

 
0 10.678 10.678 10.678
1 8.64407 8.64407 8.64407
2 7.62712 7.62712 7.62712
3 7.62712 7.62712 7.62712
4 8.64407 8.64407 8.64407
5 10.678 10.678 10.678
6 8.64407 8.64407 8.64407
7 6.61017 6.61017 6.61017
8 5.08475 5.08475 5.08475
9 5.08475 5.08475 5.08475
10 6.61017 6.61017 6.61017
11 8.64407 8.64407 8.64407
12 7.62712 7.62712 7.62712
13 5.08475 5.08475 5.08475
14 5.59322 5.59322 5.59322
15 5.59322 5.59322 5.59322
16 5.08475 5.08475 5.08475
17 7.62712 7.62712 7.62712
18 7.62712 7.62712 7.62712
19 5.08475 5.08475 5.08475
20 5.59322 5.59322 5.59322
21 5.59322 5.59322 5.59322
22 5.08475 5.08475 5.08475
23 7.62712 7.62712 7.62712
24 8.64407 8.64407 8.64407
25 6.61017 6.61017 6.61017
26 5.08475 5.08475 5.08475
27 5.08475 5.08475 5.08475
28 6.61017 6.61017 6.61017
29 8.64407 8.64407 8.64407
30 10.678 10.678 10.678
31 8.64407 8.64407 8.64407
32 7.62712 7.62712 7.62712
33 7.62712 7.62712 7.62712
34 8.64407 8.64407 8.64407
35 10.678 10.678 10.678