|
| 1 | +.. _pydefixModule: |
| 2 | + |
| 3 | +Pydefix module |
| 4 | +============== |
| 5 | + |
| 6 | +The Pydefix module allows Idefix to talk directly to a Python interpreter while running. It can be used to create your own initial condition |
| 7 | +and/or for custom outputs produced from Python. Pydefix relies on the pybind11 python package |
| 8 | + |
| 9 | +The essence of Pydefix is to allows the user to have a direct access to Idefix data structure from Python without writing/accessing any file. In particular, IdefixArrays are viewed as numpy arrays in Python. |
| 10 | +Note however that to keep things simple, Pydefix works on the host memory space only, and hence sync data to/from the GPU (if used) before invoking Python functions. Hence, using Pydefix for outputs induces |
| 11 | +a significant loss of performances. |
| 12 | + |
| 13 | + |
| 14 | +Before you start |
| 15 | +---------------- |
| 16 | +Pybind11 installation |
| 17 | ++++++++++++++++++++++ |
| 18 | + |
| 19 | +In order to use pydefix, you need to be working in a python>=3.8 environement that includes `pybind11 <https://pybind11.readthedocs.io>`_. Follow the instruction of your package manager to install pybind11>=2.12. |
| 20 | + |
| 21 | +Pydefix usage |
| 22 | +------------- |
| 23 | +Idefix Configuration |
| 24 | +++++++++++++++++++++ |
| 25 | + |
| 26 | +In order to use Pydefix, you need to switch on ``Idefix_PYTHON`` in cmake. This will auto-detect Python and check that pybind11 can be used effectively. |
| 27 | + |
| 28 | + |
| 29 | +Run Idefix with Pydefix |
| 30 | ++++++++++++++++++++++++ |
| 31 | + |
| 32 | +Pydefix is typically enabled from your input file `idefix.ini` in the block ``[Python]``. The following parameters are available: |
| 33 | + |
| 34 | ++------------------------+-----------------------+-----------------------------------------------------------------------------------------------------------+ |
| 35 | +| Entry name | Parameter type | Comment | |
| 36 | ++========================+=======================+===========================================================================================================+ |
| 37 | +| script | string | | (Mandatory) Filename (*without ".py"!*) of the python script that Idefix should use. | |
| 38 | +| | | | The script should be in the same location as the Idefix executable file. | |
| 39 | ++------------------------+-----------------------+-----------------------------------------------------------------------------------------------------------+ |
| 40 | +| output_function | string | | (Optional) Name of the function that will be called for each output event (the function should be | |
| 41 | +| | | | defined in the python script above). When ommited, pydefix output functions are disabled. | |
| 42 | +| | | | The periodicity of the pydefix output routine is set in the block:entry `[Output]:python` | |
| 43 | ++------------------------+-----------------------+-----------------------------------------------------------------------------------------------------------+ |
| 44 | +| initflow_function | string | | (Optional) Name of the python function that will be called to initialize the flow in place of the C++ | |
| 45 | +| | | | function ``Setup::InitFlow``. Revert to ``Setup::Initflow`` when ommited. | |
| 46 | ++------------------------+-----------------------+-----------------------------------------------------------------------------------------------------------+ |
| 47 | + |
| 48 | +Python script |
| 49 | ++++++++++++++ |
| 50 | + |
| 51 | +When using Pydefix, idefix expects a python script to be specified in the input file (see ``script`` parameter above). To be fully functionnal, you should import the ``pydefix`` module at the beginning |
| 52 | +of your python script (you can also import other python module, as any python script). |
| 53 | + |
| 54 | +Your python script should define functions that will be called while Idefix is running: |
| 55 | +* The signature of the ``initflow`` function should be ``(data)`` where ``data`` is a python structure matching Idefix's ``DataBlockHost`` class. |
| 56 | +* The signature of the ``output`` function should be ``(data,grid,n)`` where ``data`` is a python structure matching Idefix's ``DataBlockHost`` class, ``grid`` is Idefix's ``GridHost`` class, and ``n`` is an integer representing the current number of the output |
| 57 | + |
| 58 | +MPI parallelism |
| 59 | ++++++++++++++++ |
| 60 | +When Idefix runs with MPI parallelism enabled, a python interpreter and script is launched by each MPI process. Each of these script is independent |
| 61 | +and have access to its local ``dataBlockHost``. The `pydefix` module however gives access to the local rank ``prank`` and total MPI size ``psize``. In addition, |
| 62 | +pydefix provides the function ``GatherIdefixArray`` to gather the data distributed among each process without invoking MPI directly in python. This function |
| 63 | +expects a 3D distributed IdefixArray in entry following the signature |
| 64 | + |
| 65 | +.. code-block:: c++ |
| 66 | + |
| 67 | + GatherIdefixArray(IdefixHostArray3D<real> in, // 3D distributed array |
| 68 | + DataBlockHost dataHost, // dataBlock structure |
| 69 | + bool keepBoundaries = true, // Whether we keep the ghost zones in the returned array |
| 70 | + bool broadcast = true) // Whether the returned array is available only in proc #0 or in every proc (caution! possibly requires lots of memory) |
| 71 | + |
| 72 | +This function is used as follows: |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + import pydefix as pdfx # mandatory |
| 77 | + import numpy as np |
| 78 | + import matplotlib.pyplot as plt |
| 79 | +
|
| 80 | + def output(data,grid,n): |
| 81 | + # Gather pressure field from every process in process #0 (set broadcast to True to distribute the result to all processes) |
| 82 | + prs = pdfx.GatherIdefixArray(data.Vc[pdfx.PRS,:,:,:],data,broadcast=False) |
| 83 | +
|
| 84 | + # Only root process performs this |
| 85 | + if pdfx.prank==0: |
| 86 | + x=grid.x[pdfx.IDIR] # The grid contains the full domain size, while the datablock contains only local information |
| 87 | + y=grid.x[pdfx.JDIR] |
| 88 | + plt.figure() |
| 89 | + plt.pcolormesh(x,y,prs[0,:,:],cmap='plasma') |
| 90 | +
|
| 91 | +
|
| 92 | +.. note:: |
| 93 | + For more advanced usage, it is also possibly to directly call MPI routines from python using the `Mpi4py <https://pypi.org/project/mpi4py/>`_ module. |
| 94 | + |
| 95 | +Example |
| 96 | ++++++++ |
| 97 | + |
| 98 | +An example is provided in the directory `test/IO/python`. This example shows how to use Idefix with pure python initial conditions and outputs. |
| 99 | +It reproduces the 2D OrszagTang vortex available in MHD/OrszagTang without requiring any single line of C++ code from the user. |
| 100 | + |
| 101 | +The python script `pydefix_example.py` initializes the initial condition of the OT test (``initflow``) and produces a series of PNG files through matplotlib (`output`). |
| 102 | + |
| 103 | +Troubleshooting |
| 104 | +--------------- |
| 105 | + |
| 106 | +It during configuration stage, you get:: |
| 107 | + |
| 108 | + CMake Error at CMakeLists.txt:122 (find_package): |
| 109 | + By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has |
| 110 | + asked CMake to find a package configuration file provided by "pybind11", |
| 111 | + but CMake did not find one. |
| 112 | + |
| 113 | +It means that cmake cannot find the location of pybind11 (this typically happens on MacOs). In order to locate pybind11, open a python interpreter, and get pybind11 install dir through: |
| 114 | + |
| 115 | +.. code-block:: python |
| 116 | +
|
| 117 | + import pybind11 |
| 118 | + print(pybind11.__file__) |
| 119 | +
|
| 120 | +You can then exit the interpreter and set the pybind11_DIR environement variable to the right path: |
| 121 | + |
| 122 | +.. code-block:: bash |
| 123 | +
|
| 124 | + export pybind11_DIR=env/lib/python3.10/site-packages/pybind11 |
| 125 | +
|
| 126 | +you can then run cmake which should be able to find pybind11, and compile the code. |
0 commit comments