Talk Developing a particle-based hydrodynamical simulation code with NumPy and f2py
Developing a particle-based hydrodynamical simulation code with NumPy and f2py
We present a Python module that performs hydrodynamical simulations using the
Multiparticle Collision Dynamics (MPCD), a particle-based algorithm for the
simulation of fluids .
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 , . 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.
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.
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
Emphasis is put on the use of existing tools from the NumPy distribution, as well as
the Python scientific libraries.
- Interactive usage is possible with ipython .
- Visualization of 1D and 2D data is provided by matplotlib .
- Visualization of volumetric data is provided by mayavi2 .
- Data storage is implemented with h5py .