##### Abstract

### Developing a particle-based hydrodynamical simulation code with NumPy and f2py

### Introduction

We present a Python module that performs hydrodynamical simulations using the Multiparticle Collision Dynamics (MPCD), a particle-based algorithm for the simulation of fluids [1]. The module may be used either in a simulation script or interactively thanks to the ipython shell.

The simulation steps are controlled directly from Python, while the actual computations are performed using NumPy or Fortran, with the help of f2py. The configuration file parsing and the setup of the system can thus be done in Python, as well as the main logic of the code.

The MPCD method has been developed since a decade, and allows the modeling of a wide range of fluid phenomena [2], [3]. Among the applications provided by the present module are:

- The periodic boundaries may be replaced by a reflecting wall.
- The method of virtual particles allows to implement thermostatted walls with stick boundary conditions.
- Bulk chemical reactions are implemented to model reaction-diffusion systems, while taking thermal fluctuations into account.
- A gravitational potential allows to obtain a Poiseuille flow.

### Linking Fortran and NumPy

The data structures of the code are based entirely on arrays (NumPy or Fortran arrays), with appropriate use of views to avoid data copies during execution. The notable issue of array indexing is tackled by using a strategy that provides a similar memory layout in NumPy and in Fortran. The ordering chosen in the following way: whenever a [N,3] array is used in NumPy to represent the three coordinates of the particles, a (3,N) array is used in Fortran. The x, y and z components of a particle are thus contiguous in memory in both languages. Providing the transposed view of NumPy array, via the "T" method, to a Fortran subroutine realizes that strategy.

Two approaches for the use of f2py are presented:

- "Gluing" a Fortran module: the arrays are declared within the Fortran code and are accessed via NumPy views and module-scoped parameters can be defined.
- "Speeding up" a Python code with Fortran: all parameters are explicitly passed to the Fortran subroutines with care so that arrays are contiguous.

### Development process

Python enables rapid prototyping of simulation algorithms, with the benefit of automatic error handling through exceptions. Modifications can then be recoded in Fortran to improve the speed of the computation.

Emphasis is put on the use of existing tools from the NumPy distribution, as well as the Python scientific libraries.

### References

[1] | Malevanets & Kapral, J. Chem. Phys. 110, 8605 (1999) |

[2] | Kapral, Adv. Chem. Phys. 140, 89 (2008) |

[3] | Gompper, Ihle, Kroll & Winkler, Adv. Polym. Sci. 221, 1 (2009) |

[4] | IPpython : An Interactive Python Environment |

[5] | matplotlib: python plotting |

[6] | Mayavi Project: 3D Scientific Data Visualization and Plotting |

[7] | h5py: HDF5 for Python |