Skip to content

Commit

Permalink
Add optional MPI for ceptr
Browse files Browse the repository at this point in the history
  • Loading branch information
marchdf committed Oct 27, 2023
1 parent b5c4c90 commit be54f57
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
20 changes: 16 additions & 4 deletions Docs/sphinx/Ceptr.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. highlight:: rst

.. _sec:ceptr:

CEPTR: Chemistry Evaluation for Pele Through Recasting
======================================================

Expand All @@ -19,10 +19,12 @@ To install CEPTR dependencies::
$ cd ${PELE_PHYSICS_HOME}/Support/ceptr
$ poetry update


Usage
-----

Generating for a single chemistry
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

There are three ways to use CEPTR to generate C++ mechanism files for a given chemistry

1. Using CEPTR directly::
Expand All @@ -39,11 +41,23 @@ There are three ways to use CEPTR to generate C++ mechanism files for a given ch
$ bash ${PELE_PHYSICS_HOME}/Support/Mechanism/Models/converter.sh -f ./LiDryer/mechanism.yaml


Batched generation
^^^^^^^^^^^^^^^^^^

For non-reduced chemistries, CEPTR can take a file with a list of ``mechanism.yaml`` files to convert::

$ cd ${PELE_PHYSICS_HOME}/Support/ceptr
$ poetry run convert -l ${PELE_PHYSICS_HOME}/Support/Mechanism/Models/list_mech

.. note::

If you are generating many mechanisms at once (e.g., a list of mechanism files), then you may want to consider using MPI. `mpi4py` is an optional dependency that can be installed and then used like this::

$ cd ${PELE_PHYSICS_HOME}/Support/ceptr
$ poetry install -E mpi
$ poetry run mpirun -np 8 convert -l ${PELE_PHYSICS_HOME}/Support/Mechanism/Models/list_mech


For reduced chemistries, CEPTR can take a file with a list of ``qssa.yaml`` and ``qssa_input.toml`` to convert::

$ cd ${PELE_PHYSICS_HOME}/Support/ceptr
Expand Down Expand Up @@ -99,5 +113,3 @@ The full list of options is::
For a detailed description of these options and a further information on the way QSS mechanism are treated in `CEPTR` the reader may consult :ref:`the QSS section <sec_qss>`.

See Tutorials (:ref:`Generating NC12H26 QSS mechanism with analytical jacobian <sec_tutqss1>` and :ref:`Generating NC12H26 QSS mechanism without analytical jacobian <sec_tutqss2>`) for generating QSS mechanisms from the ``.yaml`` files.


64 changes: 40 additions & 24 deletions Support/ceptr/ceptr/ceptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
import cantera as ct

import ceptr.converter as converter
import ceptr.optional_mpi as ompi


def parse_lst_file(lst):
"""Parse a file containing a list of mechanism files."""
fnames = []
with open(lst, "r") as f:
for line in f:
if not line.startswith("#"):
fnames.append(line)

if ompi.use_mpi():
comm = ompi.world_comm()
rank = comm.Get_rank()
nprocs = comm.Get_size()
fnames = fnames[rank::nprocs]

return fnames


def convert(
Expand Down Expand Up @@ -35,17 +53,16 @@ def convert_lst(
):
"""Convert mechanisms from a file containing a list of directories."""
lpath = pathlib.Path(lst)
with open(lst, "r") as f:
for line in f:
if not line.startswith("#"):
mechname = lpath.parents[0] / line.strip()
print(f"""Converting file {mechname}""")
convert(
mechname,
jacobian,
qss_format_input,
qss_symbolic_jac,
)
lines = parse_lst_file(lst)
for line in lines:
mechname = lpath.parents[0] / line.strip()
print(f"""Converting file {mechname}""")
convert(
mechname,
jacobian,
qss_format_input,
qss_symbolic_jac,
)


def convert_lst_qss(
Expand All @@ -54,19 +71,18 @@ def convert_lst_qss(
):
"""Convert QSS mechanisms from a file of directories and format input."""
lpath = pathlib.Path(lst)
with open(lst, "r") as f:
for line in f:
if not line.startswith("#"):
mech_file, format_file, _, _ = line.split()
mechname = lpath.parents[0] / mech_file.strip()
qss_format_input = lpath.parents[0] / format_file.strip()
print(f"""Converting file {mechname}""")
convert(
mechname,
jacobian,
qss_format_input,
True,
)
lines = parse_lst_file(lst)
for line in lines:
mech_file, format_file, _, _ = line.split()
mechname = lpath.parents[0] / mech_file.strip()
qss_format_input = lpath.parents[0] / format_file.strip()
print(f"""Converting file {mechname}""")
convert(
mechname,
jacobian,
qss_format_input,
True,
)


def main():
Expand Down
19 changes: 19 additions & 0 deletions Support/ceptr/ceptr/optional_mpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Optional import of mpi4py, only if available in the environment."""

import sys

try:
from mpi4py import MPI

except ModuleNotFoundError:
pass


def use_mpi():
"""Detect if mpi4py was imported."""
return "mpi4py" in sys.modules


def world_comm():
"""Return the world communicator."""
return MPI.COMM_WORLD
4 changes: 4 additions & 0 deletions Support/ceptr/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sympy = "^1.12"
symengine = "^0.9.2"
pandas = ">=2.0.3"
toml = ">=0.10.2"
mpi4py = {version = "^3.1.5", optional = true}

[tool.poetry.dev-dependencies]
pytest = "^7.4.0"
Expand All @@ -30,6 +31,9 @@ flake8-docstrings = "^1.7.0"
flake8-use-fstring = "^1.4"
flynt = "^0.77"

[tool.poetry.extras]
mpi = ["mpi4py"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Expand Down

0 comments on commit be54f57

Please sign in to comment.