# Vector Calculus with Python

In this little example we want to calculate the magnetic vector potential and field for a given distribution of magnetic moments in 3D.

For the case of rather large grids we use the multiprocessing capabilities of the Python interpreter in the calculations.

## Physics of Magnetism

We start out with a cylindrical distribution of magnetic moments. In the x>0 half plane these moments are pointing towards the positive z axis. In the other half plane in the opposite direction (see Eq. 1).

The magnetic vector potential A at the point ra is the sum or the integral of all present magnetic moments at positions rm (see Eq. 2). Finally the magnetic field B is given by the cross product of the Laplace operator with the vector potential.

## The Script

The example script basically contains of two parts: The first part does the number crunching and the second part handles the data display.

### Parallel calculations

In order to speed up the computation time we use the multiprocessing module of Python to allow multiple interpreters to start and perform calculations.

After determining the number of available CPU cores (lines 95-98) the three dimensional array -- representing the region of interest -- is split into smaller slices (line 106).

An number of Processes equal to the number of cores are started (lines 107-108), if no file containing data is present, and calculate Eq. 2 on subslices (lines 27-40) passed as arguments to the Process contructor (line 107).

When all processes are finished (lines 110-111) the vector potential is saved (line 113).

### Show the results

The magnetic field (Eq. 3) is calculated (lines 115-119) and displayed using Mayavi. The plot shows the magnetic moments (line 123), the vector potential (line 124) and the magnetic field (127-129), where Mayavi's pipelines are used to create a full vector field plot and a vector cut plane.