xref: /petsc/src/binding/petsc4py/docs/source/petsc_options.rst (revision 671e90c6fa8d41be1a749e4415c38493bbe81e81)
155a74a43SLisandro Dalcin.. _petsc_options:
255a74a43SLisandro Dalcin
3*671e90c6SJose E. RomanWorking with PETSc options
4*671e90c6SJose E. Roman==========================
555a74a43SLisandro Dalcin
6*671e90c6SJose E. RomanA 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*671e90c6SJose E. Roman
8*671e90c6SJose E. RomanIn order to use command-line options in a petsc4py program, it is important to initialize the module as follows:
9*671e90c6SJose E. Roman
10*671e90c6SJose E. Roman.. code-block:: python
11*671e90c6SJose E. Roman
12*671e90c6SJose E. Roman  # We first import petsc4py and sys to initialize PETSc
13*671e90c6SJose E. Roman  import sys, petsc4py
14*671e90c6SJose E. Roman  petsc4py.init(sys.argv)
15*671e90c6SJose E. Roman
16*671e90c6SJose E. Roman  # Import the PETSc module
17*671e90c6SJose E. Roman  from petsc4py import PETSc
18*671e90c6SJose E. Roman
19*671e90c6SJose E. RomanThen one can provide command-line options when running the script:
20*671e90c6SJose E. Roman
21*671e90c6SJose E. Roman.. code-block:: console
22*671e90c6SJose E. Roman
23*671e90c6SJose E. Roman  $ python ex1.py -ksp_type gmres -ksp_gmres_restart 100 -ksp_view
24*671e90c6SJose E. Roman
25*671e90c6SJose E. RomanNote that in order to configure a given object from the command-line options, the ``setFromOptions()`` method must be called, that is:
26*671e90c6SJose E. Roman
27*671e90c6SJose E. Roman.. code-block:: python
28*671e90c6SJose E. Roman
29*671e90c6SJose E. Roman  ksp.setFromOptions()
30*671e90c6SJose E. Roman
31*671e90c6SJose E. RomanIt is also possible to add new, user-defined options, via the ``Options`` class. For instance:
32*671e90c6SJose E. Roman
33*671e90c6SJose E. Roman.. code-block:: python
34*671e90c6SJose E. Roman
35*671e90c6SJose E. Roman  OptDB = PETSc.Options()
36*671e90c6SJose E. Roman  n     = OptDB.getInt('n', 16)
37*671e90c6SJose E. Roman  eta   = OptDB.getReal('eta', 0.014)
38*671e90c6SJose E. Roman  alpha = OptDB.getScalar('alpha', -12.3)
39*671e90c6SJose E. Roman
40*671e90c6SJose E. RomanIn 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*671e90c6SJose E. Roman
42*671e90c6SJose E. Roman.. code-block:: console
43*671e90c6SJose E. Roman
44*671e90c6SJose E. Roman  $ python ex1.py -n 50 -alpha 8.8
45*671e90c6SJose E. Roman
46*671e90c6SJose E. RomanThe options database is accessible also as a Python dictionary, so that one can for instance override an option or insert a new option:
47*671e90c6SJose E. Roman
48*671e90c6SJose E. Roman.. code-block:: python
49*671e90c6SJose E. Roman
50*671e90c6SJose E. Roman  OptDB['draw_pause'] = 1
51