xref: /petsc/src/binding/petsc4py/src/petsc4py/__init__.py (revision 6c006fdf708e254dd4f47e201e35f5a938d9ab7b)
1# Author:  Lisandro Dalcin
2# Contact: dalcinl@gmail.com
3"""The PETSc for Python package.
4
5This package is an interface to PETSc libraries.
6
7PETSc_ (the Portable, Extensible Toolkit for Scientific Computation)
8is a suite of data structures and routines for the scalable (parallel)
9solution of scientific applications modeled by partial differential
10equations. It employs the MPI_ standard for all message-passing
11communications.
12
13.. _PETSc: https://petsc.org
14.. _MPI:   https://www.mpi-forum.org
15
16"""
17
18__author__ = 'Lisandro Dalcin'
19__version__ = '3.24.4'
20__credits__ = 'PETSc Team <petsc-maint@mcs.anl.gov>'
21
22
23def init(args=None, arch=None, comm=None):
24    """Initialize PETSc.
25
26    Parameters
27    ----------
28    args
29        Command-line arguments, usually the `sys.argv` list
30    arch
31        Specific configuration to use
32    comm
33        MPI commmunicator
34
35    Notes
36    -----
37    This function should be called only once, typically at the very
38    beginning of the bootstrap script of an application.
39    """
40    import petsc4py.lib
41
42    PETSc = petsc4py.lib.ImportPETSc(arch)
43    args = petsc4py.lib.getInitArgs(args)
44    PETSc._initialize(args, comm)
45
46
47def get_include():
48    """Return the directory in the package that contains header files.
49
50    Extension modules that need to compile against petsc4py should use
51    this function to locate the appropriate include directory.
52
53    Example
54    -------
55    Using Python distutils or NumPy distutils::
56
57        import petsc4py
58        Extension('extension_name', ...
59                  include_dirs=[..., petsc4py.get_include()])
60
61    """
62    from os.path import dirname, join
63
64    return join(dirname(__file__), 'include')
65
66
67def get_config():
68    """Return a dictionary with information about PETSc."""
69    import os
70
71    from io import StringIO
72    from configparser import ConfigParser
73    pgkdir = os.path.dirname(__file__)
74    filename = os.path.join(pgkdir, 'lib', 'petsc.cfg')
75    with open(filename) as fp:
76        stream = StringIO('[petsc]\n' + fp.read())
77    parser = ConfigParser()
78    parser.optionxform = str
79    parser.read_file(stream, filename)
80    return dict(parser.items('petsc'))
81