API Reference

multipoles.MultipoleExpansion

Bases: object

Perform a multipole expansion for a given charge or mass distribution.

Determines the spherical multipole moments of the given distribution and can calculate the solution of the electrostatic or gravitational potential based on the multipole expansion.

__init__(charge_dist, l_max, exterior=None, interior=None)

Create a MultipoleExpansion object for a given charge or mass distribution.

Parameters:
  • charge_dist (dict) –

    description of the charge distribution

    For discrete charge distributions (point charges) the dict MUST contain the following (key:value)-pairs:

    'discrete': True

    'q': the charge (positive or negative floating point number)

    'xyz': the location of the charge in cartesian coordinates (a tuple, list or array of length 3)

    For continuous charge distributions (charge density) the dict MUST contain the following items:

    'discrete': False 'rho': the 3D charge distribution (3D numpy array) 'xyz': the domain of the charge distribution (3-tuple of 3D coordinate arrays, see example below)

  • l_max (int) –

    the maximum multipole moment to consider (0=monopole, 1=dipole, etc.)

  • exterior (bool, default: None ) –

    whether to perform an exterior expansion (default). If false, interior expansion will be used.

  • interior (bool, default: None ) –

    syntactic override for exterior expansion parameter

Examples

As example for a discrete charge distribution we model two point charges with positive and negative unit charge located on the z-axis:

>>> from multipoles import MultipoleExpansion

Prepare the charge distribution dict for the MultipoleExpansion object:

>>> charge_dist = {'discrete': True, 'charges': [{'q': 1, 'xyz': (0, 0, 1)}, {'q': -1, 'xyz': (0, 0, -1)}]}
>>> l_max = 2
>>> Phi = MultipoleExpansion(charge_dist, l_max)

Then evaluate on any point desired using Phi(...) or Phi[]. See the docstrings of call and getitem, respectively.

As an example for a continuous charge distribution, we smear out the point charges from the previous example:

>>> from multipoles import MultipoleExpansion
>>> import numpy as np

First we set up our grid, a cube of length 10 centered at the origin:

>>> npoints = 101
>>> edge = 10
>>> x, y, z = [np.linspace(-edge/2., edge/2., npoints)]*3
>>> XYZ = np.meshgrid(x, y, z, indexing='ij')

We model our smeared out charges as gaussian functions:

>>> def gaussian(XYZ, xyz0, sigma):
>>>    g = np.ones_like(XYZ[0])
>>>    for k in range(3):
>>>        g *= np.exp(-(XYZ[k] - xyz0[k])**2 / sigma**2)
>>>    g *= (sigma**2*np.pi)**-1.5
>>>    return g

The width of our gaussians:

>>> sigma = 1.5

Initialize the charge density rho, which is a 3D numpy array:

>>> rho = gaussian(XYZ, (0, 0, 1), sigma) - gaussian(XYZ, (0, 0, -1), sigma)

Prepare the charge distribution dict for the MultipoleExpansion object:

>>> charge_dist = {'discrete': False, 'rho': rho, 'xyz': XYZ}

The rest is the same as for the discrete case:

>>> l_max = 2
>>> Phi = MultipoleExpansion(charge_dist, l_max)

Then evaluate on any point desired using Phi(...) or Phi[]. See the docstrings of call and getitem, respectively.

__call__(*xyz, l_max=None)

Evaluate multipole expansion at a point with given coordinates.

Parameters:
  • xyz (3-tuple of floats|array, default: () ) –

    The x,y,z coordinates of the points where to evaluate the expansion. If three floats are given, then only one point with coordinates (x, y, z) is evaluated. If three arrays xyz=(x, y, z) are given, the evaluation is done at each point (x[i], y[i], z[i]).

  • l_max (int, default: None ) –

    The maximum angular momentum to use for the expansion. If no value is given, use l_max from the original computation of the expansion. If l_max is given, only use contributions up to this angular momentum in the evaluation.