# 10. Magnetostatics¶

## 10.1. Dipolar interaction¶

*ESPResSo* contains methods to calculate the interactions between point dipoles

where \(r=|\vec{r}|\). The prefactor \(D\) is can be set by the user and is given by

where \(\mu_0\) and \(\mu\) are the vacuum permittivity and the relative permittivity of the background material, respectively.

Magnetostatic interactions are activated via the actor framework:

```
import espressomd
import espressomd.magnetostatics
system = espressomd.System(box_l=[10, 10, 10])
system.time_step = 0.01
system.part.add(pos=[[0, 0, 0], [1, 1, 1]], dip=2 * [(1, 0, 0)],
rotation=2 * [(True, True, True)])
actor = espressomd.magnetostatics.DipolarDirectSumCpu(prefactor=1.)
system.actors.add(actor)
```

The list of actors can be cleared with
`system.actors.clear()`

and
`system.actors.remove(actor)`

.

### 10.1.1. Dipolar P3M¶

`espressomd.magnetostatics.DipolarP3M`

This is the dipolar version of the P3M algorithm, described in [Cerdà *et al.*, 2008].

Make sure that you know the relevance of the P3M parameters before using
P3M! If you are not sure, read the following references:
[Cerdà *et al.*, 2008, Deserno, 2000, Deserno and Holm, 1998, Deserno and Holm, 1998, Deserno *et al.*, 2000, Ewald, 1921, Hockney and Eastwood, 1988, Kolafa and Perram, 1992].

Note that dipolar P3M does not work with non-cubic boxes.

The parameters of the dipolar P3M method can be tuned automatically, by
providing `accuracy=<TARGET_ACCURACY>`

to the method. It is also possible to
pass a subset of the method parameters such as `mesh`

. In that case, only
the omitted parameters are tuned:

```
import espressomd.magnetostatics as magnetostatics
p3m = magnetostatics.DipolarP3M(prefactor=1, mesh=32, accuracy=1E-4)
system.actors.add(p3m)
```

It is important to note that the error estimates given in [Cerdà *et al.*, 2008]
used in the tuning contain assumptions about the system. In particular, a
homogeneous system is assumed. If this is no longer the case during the
simulation, actual force and torque errors can be significantly larger.

### 10.1.2. Dipolar Layer Correction (DLC)¶

The dipolar layer correction (DLC) is used in conjunction with the dipolar P3M method to calculate dipolar interactions in a 2D-periodic system. It is based on [Bródka, 2004] and the dipolar version of Electrostatic Layer Correction (ELC).

Usage notes:

The non-periodic direction is always the

**z-direction**.The method relies on a slab of the simulation box perpendicular to the z-direction not to contain particles. The size in z-direction of this slab is controlled by the

`gap_size`

parameter. The user has to ensure that no particles enter this region by means of constraints or by fixing the particles’ z-coordinate. When particles enter the slab of the specified size, an error will be thrown.The method can be tuned using the

`accuracy`

parameter. In contrast to the electrostatic method, it refers to the energy. Furthermore, it is assumed that all dipole moment are as large as the largest of the dipoles in the system.When the base solver is not a P3M method, metallic epsilon is assumed.

The method is used as follows:

```
import espressomd.magnetostatics
dp3m = espressomd.magnetostatics.DipolarP3M(prefactor=1, accuracy=1E-4)
mdlc = espressomd.magnetostatics.DLC(actor=dp3m, maxPWerror=1E-5, gap_size=2.)
system.actors.add(mdlc)
```

## 10.2. Dipolar direct sum¶

This interaction calculates energies and forces between dipoles by
explicitly summing over all pairs. For the directions in which the
system is periodic (as defined by `system.periodicity`

), it applies the
minimum image convention, i.e. the interaction is effectively cut off at
half a box length.

The direct summation methods are mainly intended for non-periodic systems which cannot be solved using the dipolar P3M method. Due to the long-range nature of dipolar interactions, direct summation with minimum image convention does not yield good accuracy with periodic systems.

Two methods are available:

`DipolarDirectSumCpu`

performs the calculation in double-precision on the CPU, optionally with replicas.`DipolarDirectSumGpu`

performs the calculations in single-precision on a CUDA-capable GPU. The implementation is optimized for large systems of several thousand particles. It makes use of one thread per particle. When there are fewer particles than the number of threads the GPU can execute simultaneously, the rest of the GPU remains idle. Hence, the method will perform poorly for small systems.

To use the methods, create an instance of either
`DipolarDirectSumCpu`

or
`DipolarDirectSumGpu`

and add it to the
system’s list of active actors. The only required parameter is the prefactor
(1):

```
import espressomd.magnetostatics
dds = espressomd.magnetostatics.DipolarDirectSumGpu(prefactor=1)
system.actors.add(dds)
```

The CPU implementation has an optional argument `n_replicas`

which
adds periodic copies to the system along periodic directions. In that
case, the minimum image convention is no longer used.

Both implementations support MPI-parallelization.

## 10.3. Barnes-Hut octree sum on GPU¶

`espressomd.magnetostatics.DipolarBarnesHutGpu`

This interaction calculates energies and forces between dipoles by
summing over the spatial octree cells (aka `leaves`

).
Far enough cells are considered as a single dipole with a cumulative
vector in the cell center of mass. Parameters which determine that the
cell is far enough are \(I_{\mathrm{tol}}^2\) and
\(\varepsilon^2\) which define a fraction of the cell and
an additive distance respectively. For the detailed description of the
Barnes-Hut method application to the dipole-dipole interactions, please
refer to [Polyakov *et al.*, 2013].

To use the method, create an instance of `DipolarBarnesHutGpu`

and add it to the system’s list of active actors:

```
import espressomd.magnetostatics
bh = espressomd.magnetostatics.DipolarBarnesHutGpu(prefactor=1., epssq=200.0, itolsq=8.0)
system.actors.add(bh)
```

## 10.4. ScaFaCoS magnetostatics¶

`espressomd.magnetostatics.Scafacos`

*ESPResSo* can use the methods from the ScaFaCoS *Scalable fast Coulomb solvers*
library for dipoles, if the methods support dipolar calculations. The feature
`SCAFACOS_DIPOLES`

has to be added to `myconfig.hpp`

to activate this
feature. Dipolar calculations are only included in the `dipoles`

branch of
the ScaFaCoS code. The specific methods available can be queried with
`espressomd.electrostatics.Scafacos.get_available_methods()`

.

To use ScaFaCoS, create an instance of `Scafacos`

and add it to the list of active actors. Three parameters have to be specified:
`prefactor`

, `method_name`

, `method_params`

. The method-specific
parameters are described in the ScaFaCoS manual. In addition, methods
supporting tuning have a parameter `tolerance_field`

which sets the desired
root mean square accuracy for the magnetic field.

For details of the various methods and their parameters please refer to the ScaFaCoS manual. To use this feature, ScaFaCoS has to be built as a shared library.