xref: /petsc/src/binding/petsc4py/docs/source/petsc_options.rst (revision 8564c97f3fe574910f676ffb31bf76fa44548916)
1.. _petsc_options:
2
3Working with PETSc options
4==========================
5
6A very powerful feature of PETSc is that objects can be configured via command-line options. In this way, one can choose the method to be used or set different parameters.
7
8In order to use command-line options in a petsc4py program, it is important to initialize the module as follows:
9
10.. code-block:: python
11
12  # We first import petsc4py and sys to initialize PETSc
13  import sys, petsc4py
14  petsc4py.init(sys.argv)
15
16  # Import the PETSc module
17  from petsc4py import PETSc
18
19Then one can provide command-line options when running the script:
20
21.. code-block:: console
22
23  $ python ex1.py -ksp_type gmres -ksp_gmres_restart 100 -ksp_view
24
25Note that in order to configure a given object from the command-line options, the ``setFromOptions()`` method must be called, that is:
26
27.. code-block:: python
28
29  ksp.setFromOptions()
30
31It is also possible to add new, user-defined options, via the ``Options`` class. For instance:
32
33.. code-block:: python
34
35  OptDB = PETSc.Options()
36  n     = OptDB.getInt('n', 16)
37  eta   = OptDB.getReal('eta', 0.014)
38  alpha = OptDB.getScalar('alpha', -12.3)
39
40In this way, if the program is run with the following options, ``n`` and ``alpha`` will get the values ``50`` and ``8.8``, respectively, while ``eta`` will be assigned the value specified as default, ``0.014``.
41
42.. code-block:: console
43
44  $ python ex1.py -n 50 -alpha 8.8
45
46The options database is accessible also as a Python dictionary, so that one can for instance override an option or insert a new option:
47
48.. code-block:: python
49
50  OptDB['draw_pause'] = 1
51