File: petsc_options.rst

package info (click to toggle)
petsc4py 3.23.1-1exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,448 kB
  • sloc: python: 12,503; ansic: 1,697; makefile: 343; f90: 313; sh: 14
file content (55 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.. _petsc_options:

Working with PETSc options
==========================

A 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 without changing the source code.
See the PETSc `manual <the_options_database>` for additional information.

In order to use command-line options in a petsc4py program, it is important to initialize the module as follows:

.. code-block:: python

  # We first import petsc4py and sys to initialize PETSc
  import sys, petsc4py
  petsc4py.init(sys.argv)

  # Import the PETSc module
  from petsc4py import PETSc

Then one can provide command-line options when running a script:

.. code-block:: console

  $ python foo.py -ksp_type gmres -ksp_gmres_restart 100 -ksp_view

When the above initialization method is not possible, PETSc options can be also specified via environment variables or configuration files, e.g.:

.. code-block:: console

  $ PETSC_OPTIONS='-ksp_type gmres -ksp_gmres_restart 100 -ksp_view' python foo.py

Command-line options can be read via an instance of the ``Options`` class. For instance:

.. code-block:: python

  OptDB = PETSc.Options()
  n     = OptDB.getInt('n', 16)
  eta   = OptDB.getReal('eta', 0.014)
  alpha = OptDB.getScalar('alpha', -12.3)

In this way, if the script is run with

.. code-block:: console

  $ python foo.py -n 50 -alpha 8.8

the 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``.

The options database is accessible also as a Python dictionary, so that one can for instance override, insert or delete an option:

.. code-block:: python

  OptDB['draw_pause'] = 1
  del OptDB['draw_pause']