xref: /petsc/doc/manual/other.md (revision af0e4c05a9d095ddef457b485b531206e788d438)
17f296bb3SBarry Smith# Other PETSc Features
27f296bb3SBarry Smith
37f296bb3SBarry Smith## PETSc on a process subset
47f296bb3SBarry Smith
57f296bb3SBarry SmithUsers who wish to employ PETSc on only a subset of MPI processes
67f296bb3SBarry Smithwithin a larger parallel job, or who wish to use a “manager” process to
77f296bb3SBarry Smithcoordinate the work of “worker” PETSc processes, should specify an
87f296bb3SBarry Smithalternative communicator for `PETSC_COMM_WORLD` by directly setting
97f296bb3SBarry Smithits value, for example to use an existing MPI communicator `comm`,
107f296bb3SBarry Smith
117f296bb3SBarry Smith```
127f296bb3SBarry SmithPETSC_COMM_WORLD = comm; /* To use a previously-defined MPI_Comm */
137f296bb3SBarry Smith```
147f296bb3SBarry Smith
157f296bb3SBarry Smith*before* calling `PetscInitialize()`, but, obviously, after calling
167f296bb3SBarry Smith`MPI_Init()`.
177f296bb3SBarry Smith
187f296bb3SBarry Smith(sec_options)=
197f296bb3SBarry Smith
207f296bb3SBarry Smith## Runtime Options
217f296bb3SBarry Smith
227f296bb3SBarry SmithAllowing the user to modify parameters and options easily at runtime is
237f296bb3SBarry Smithvery desirable for many applications. PETSc provides a simple mechanism
247f296bb3SBarry Smithto enable such customization. To print a list of available options for a
257f296bb3SBarry Smithgiven program, simply specify the option `-help` at
267f296bb3SBarry Smithruntime, e.g.,
277f296bb3SBarry Smith
287f296bb3SBarry Smith```console
297f296bb3SBarry Smith$ mpiexec -n 1 ./ex1 -help
307f296bb3SBarry Smith```
317f296bb3SBarry Smith
327f296bb3SBarry SmithNote that all runtime options correspond to particular PETSc routines
337f296bb3SBarry Smiththat can be explicitly called from within a program to set compile-time
347f296bb3SBarry Smithdefaults. For many applications it is natural to use a combination of
357f296bb3SBarry Smithcompile-time and runtime choices. For example, when solving a linear
367f296bb3SBarry Smithsystem, one could explicitly specify use of the Krylov subspace
377f296bb3SBarry Smithsolver BiCGStab by calling
387f296bb3SBarry Smith
397f296bb3SBarry Smith```
407f296bb3SBarry SmithKSPSetType(ksp, KSPBCGS);
417f296bb3SBarry Smith```
427f296bb3SBarry Smith
437f296bb3SBarry SmithOne could then override this choice at runtime with the option
447f296bb3SBarry Smith
457f296bb3SBarry Smith```
467f296bb3SBarry Smith-ksp_type tfqmr
477f296bb3SBarry Smith```
487f296bb3SBarry Smith
497f296bb3SBarry Smithto select the Transpose-Free QMR algorithm. (See
507f296bb3SBarry Smith{any}`ch_ksp` for details.)
517f296bb3SBarry Smith
527f296bb3SBarry SmithThe remainder of this section discusses details of runtime options.
537f296bb3SBarry Smith
547f296bb3SBarry Smith(the_options_database_1)=
557f296bb3SBarry Smith
567f296bb3SBarry Smith### The Options Database
577f296bb3SBarry Smith
587f296bb3SBarry SmithEach PETSc process maintains a database of option names and values
597f296bb3SBarry Smith(stored as text strings). This database is generated with the command
607f296bb3SBarry Smith`PetscInitialize()`, which is listed below in its C/C++ and Fortran
617f296bb3SBarry Smithvariants, respectively:
627f296bb3SBarry Smith
637f296bb3SBarry Smith```
647f296bb3SBarry SmithPetscInitialize(int *argc, char ***args, const char *file, const char *help); // C
657f296bb3SBarry Smith```
667f296bb3SBarry Smith
677f296bb3SBarry Smith```fortran
687f296bb3SBarry Smithcall PetscInitialize(integer ierr) ! Fortran
697f296bb3SBarry Smith```
707f296bb3SBarry Smith
717f296bb3SBarry SmithThe arguments `argc` and `args` (in the C/C++ version only) are the
727f296bb3SBarry Smithaddresses of the usual command line arguments, while the `file` is a name
737f296bb3SBarry Smithof an optional file that can contain additional options. By default this file is
747f296bb3SBarry Smithcalled `.petscrc` in the user’s home directory. The user can also
757f296bb3SBarry Smithspecify options via the environmental variable `PETSC_OPTIONS`. The
767f296bb3SBarry Smithoptions are processed in the following order:
777f296bb3SBarry Smith
787f296bb3SBarry Smith1. file
797f296bb3SBarry Smith2. environmental variable
807f296bb3SBarry Smith3. command line
817f296bb3SBarry Smith
827f296bb3SBarry SmithThus, the command line options supersede the environmental variable
837f296bb3SBarry Smithoptions, which in turn supersede the options file.
847f296bb3SBarry Smith
857f296bb3SBarry SmithThe file format for specifying options is
867f296bb3SBarry Smith
877f296bb3SBarry Smith```none
887f296bb3SBarry Smith-optionname possible_value
897f296bb3SBarry Smith-anotheroptionname possible_value
907f296bb3SBarry Smith...
917f296bb3SBarry Smith```
927f296bb3SBarry Smith
937f296bb3SBarry SmithAll of the option names must begin with a dash (-) and have no
947f296bb3SBarry Smithintervening spaces. Note that the option values cannot have intervening
957f296bb3SBarry Smithspaces either, and tab characters cannot be used between the option
967f296bb3SBarry Smithnames and values. For
977f296bb3SBarry Smithuniformity throughout PETSc, we employ the format
987f296bb3SBarry Smith`-[prefix_]package_option` (for instance, `-ksp_type`,
997f296bb3SBarry Smith`-mat_view ::info`, or `-mg_levels_ksp_type`).
1007f296bb3SBarry Smith
1017f296bb3SBarry SmithUsers can specify an alias for any option name (to avoid typing the
1027f296bb3SBarry Smithsometimes lengthy default name) by adding an alias to the `.petscrc`
1037f296bb3SBarry Smithfile in the format
1047f296bb3SBarry Smith
1057f296bb3SBarry Smith```none
1067f296bb3SBarry Smithalias -newname -oldname
1077f296bb3SBarry Smith```
1087f296bb3SBarry Smith
1097f296bb3SBarry SmithFor example,
1107f296bb3SBarry Smith
1117f296bb3SBarry Smith```none
1127f296bb3SBarry Smithalias -kspt -ksp_type
1137f296bb3SBarry Smithalias -sd -start_in_debugger
1147f296bb3SBarry Smith```
1157f296bb3SBarry Smith
1167f296bb3SBarry SmithComments can be placed in the `.petscrc` file by using `#` in the
1177f296bb3SBarry Smithfirst column of a line.
1187f296bb3SBarry Smith
1197f296bb3SBarry Smith### Options Prefixes
1207f296bb3SBarry Smith
1217f296bb3SBarry SmithOptions prefixes allow specific objects to be controlled from the
1227f296bb3SBarry Smithoptions database. For instance, `PCMG` gives prefixes to its nested
1237f296bb3SBarry Smith`KSP` objects; one may control the coarse grid solver by adding the
1247f296bb3SBarry Smith`mg_coarse` prefix, for example `-mg_coarse_ksp_type preonly`. One
1257f296bb3SBarry Smithmay also use `KSPSetOptionsPrefix()`,`DMSetOptionsPrefix()` ,
1267f296bb3SBarry Smith`SNESSetOptionsPrefix()`, `TSSetOptionsPrefix()`, and similar
1277f296bb3SBarry Smithfunctions to assign custom prefixes, useful for applications with
1287f296bb3SBarry Smithmultiple or nested solvers.
1297f296bb3SBarry Smith
1307f296bb3SBarry Smith### Adding options from a file
1317f296bb3SBarry Smith
1327f296bb3SBarry SmithPETSc can load additional options from a file using `PetscOptionsInsertFile()`,
1337f296bb3SBarry Smithwhich can also be used from the command line, e.g. `-options_file my_options.opts`.
1347f296bb3SBarry Smith
1357f296bb3SBarry SmithOne can also use YAML files with `PetscOptionsInsertFileYAML()`.
1367f296bb3SBarry SmithFor example, the following file:
1377f296bb3SBarry Smith
1387f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-options.yaml
1397f296bb3SBarry Smith:language: yaml
1407f296bb3SBarry Smith```
1417f296bb3SBarry Smith
1427f296bb3SBarry Smithcorresponds to the following PETSc options:
1437f296bb3SBarry Smith
1447f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/output/ex47_3_options.out
1457f296bb3SBarry Smith:end-before: '#End'
1467f296bb3SBarry Smith:language: none
1477f296bb3SBarry Smith:start-after: '#'
1487f296bb3SBarry Smith```
1497f296bb3SBarry Smith
1507f296bb3SBarry SmithWith `-options_file`, PETSc will parse the file as YAML if it ends in a standard
1517f296bb3SBarry SmithYAML or JSON [^json] extension or if one uses a `:yaml` postfix,
1527f296bb3SBarry Smithe.g. `-options_file my_options.yaml` or `-options_file my_options.txt:yaml`
1537f296bb3SBarry Smith
1547f296bb3SBarry SmithPETSc will also check the first line of the options file itself and
1557f296bb3SBarry Smithparse the file as YAML if it matches certain criteria, for example.
1567f296bb3SBarry Smith
1577f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-yaml_tag
1587f296bb3SBarry Smith:language: yaml
1597f296bb3SBarry Smith```
1607f296bb3SBarry Smith
1617f296bb3SBarry Smithand
1627f296bb3SBarry Smith
1637f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/ex47-yaml_doc
1647f296bb3SBarry Smith:language: yaml
1657f296bb3SBarry Smith```
1667f296bb3SBarry Smith
1677f296bb3SBarry Smithboth correspond to options
1687f296bb3SBarry Smith
1697f296bb3SBarry Smith```{literalinclude} /../src/sys/tests/output/ex47_2_auto.out
1707f296bb3SBarry Smith:end-before: '#End'
1717f296bb3SBarry Smith:language: none
1727f296bb3SBarry Smith:start-after: '#'
1737f296bb3SBarry Smith```
1747f296bb3SBarry Smith
1757f296bb3SBarry Smith### User-Defined PetscOptions
1767f296bb3SBarry Smith
1777f296bb3SBarry SmithAny subroutine in a PETSc program can add entries to the database with
1787f296bb3SBarry Smiththe command
1797f296bb3SBarry Smith
1807f296bb3SBarry Smith```
1817f296bb3SBarry SmithPetscOptionsSetValue(PetscOptions options, char *name, char *value);
1827f296bb3SBarry Smith```
1837f296bb3SBarry Smith
1847f296bb3SBarry Smiththough this is rarely done. To locate options in the database, one
1857f296bb3SBarry Smithshould use the commands
1867f296bb3SBarry Smith
1877f296bb3SBarry Smith```
1887f296bb3SBarry SmithPetscOptionsHasName(PetscOptions options, char *pre, char *name, PetscBool *flg);
1897f296bb3SBarry SmithPetscOptionsGetInt(PetscOptions options, char *pre, char *name, PetscInt *value, PetscBool *flg);
1907f296bb3SBarry SmithPetscOptionsGetReal(PetscOptions options, char *pre, char *name, PetscReal *value, PetscBool *flg);
1917f296bb3SBarry SmithPetscOptionsGetString(PetscOptions options, char *pre, char *name, char *value, size_t maxlen, PetscBool  *flg);
1927f296bb3SBarry SmithPetscOptionsGetStringArray(PetscOptions options, char *pre, char *name, char **values, PetscInt *nmax, PetscBool *flg);
1937f296bb3SBarry SmithPetscOptionsGetIntArray(PetscOptions options, char *pre, char *name, PetscInt *value, PetscInt *nmax, PetscBool *flg);
1947f296bb3SBarry SmithPetscOptionsGetRealArray(PetscOptions options, char *pre, char *name, PetscReal *value, PetscInt *nmax, PetscBool *flg);
1957f296bb3SBarry Smith```
1967f296bb3SBarry Smith
1977f296bb3SBarry SmithAll of these routines set `flg=PETSC_TRUE` if the corresponding option
1987f296bb3SBarry Smithwas found, `flg=PETSC_FALSE` if it was not found. The optional
1997f296bb3SBarry Smithargument `pre` indicates that the true name of the option is the given
2007f296bb3SBarry Smithname (with the dash “-” removed) prepended by the prefix `pre`.
2017f296bb3SBarry SmithUsually `pre` should be set to `NULL` (or `PETSC_NULL_CHARACTER`
2027f296bb3SBarry Smithfor Fortran); its purpose is to allow someone to rename all the options
2037f296bb3SBarry Smithin a package without knowing the names of the individual options. For
2047f296bb3SBarry Smithexample, when using block Jacobi preconditioning, the `KSP` and `PC`
2057f296bb3SBarry Smithmethods used on the individual blocks can be controlled via the options
2067f296bb3SBarry Smith`-sub_ksp_type` and `-sub_pc_type`.
2077f296bb3SBarry Smith
2087f296bb3SBarry Smith### Keeping Track of Options
2097f296bb3SBarry Smith
2107f296bb3SBarry SmithOne useful means of keeping track of user-specified runtime options is
2117f296bb3SBarry Smithuse of `-options_view`, which prints to `stdout` during
2127f296bb3SBarry Smith`PetscFinalize()` a table of all runtime options that the user has
2137f296bb3SBarry Smithspecified. A related option is `-options_left`, which prints the
2147f296bb3SBarry Smithoptions table and indicates any options that have *not* been requested
2157f296bb3SBarry Smithupon a call to `PetscFinalize()`. This feature is useful to check
2167f296bb3SBarry Smithwhether an option has been activated for a particular PETSc object (such
2177f296bb3SBarry Smithas a solver or matrix format), or whether an option name may have been
2187f296bb3SBarry Smithaccidentally misspelled.
2197f296bb3SBarry Smith
2207f296bb3SBarry SmithThe option `-options_monitor` `<viewer>` turns on the default monitoring of options.
2217f296bb3SBarry Smith`PetscOptionsMonitorSet()` can be used to provide custom monitors.
2227f296bb3SBarry SmithThe option `-options_monitor_cancel` prevents any monitoring by monitors set with `PetscOptionsMonitorSet()` (but not that set with `-options_monitor`).
2237f296bb3SBarry Smith
2247f296bb3SBarry Smith(sec_viewers)=
2257f296bb3SBarry Smith
2267f296bb3SBarry Smith## Viewers: Looking at PETSc Objects
2277f296bb3SBarry Smith
2287f296bb3SBarry SmithPETSc employs a consistent scheme for examining, printing, and saving
2297f296bb3SBarry Smithobjects through commands of the form
2307f296bb3SBarry Smith
2317f296bb3SBarry Smith```
2327f296bb3SBarry SmithXXXView(XXX obj, PetscViewer viewer);
2337f296bb3SBarry Smith```
2347f296bb3SBarry Smith
2357f296bb3SBarry SmithHere `obj` is a PETSc object of type `XXX`, where `XXX` is
2367f296bb3SBarry Smith`Mat`, `Vec`, `SNES`, etc. There are several predefined viewers.
2377f296bb3SBarry Smith
2387f296bb3SBarry Smith- Passing in a zero (`0`) for the viewer causes the object to be
2397f296bb3SBarry Smith  printed to the screen; this is useful when viewing an object in a
2407f296bb3SBarry Smith  debugger but should be avoided in source code.
2417f296bb3SBarry Smith- `PETSC_VIEWER_STDOUT_SELF` and `PETSC_VIEWER_STDOUT_WORLD` causes
2427f296bb3SBarry Smith  the object to be printed to the screen.
2437f296bb3SBarry Smith- `PETSC_VIEWER_DRAW_SELF` `PETSC_VIEWER_DRAW_WORLD` causes the
2447f296bb3SBarry Smith  object to be drawn in a default X window.
2457f296bb3SBarry Smith- Passing in a viewer obtained by `PetscViewerDrawOpen()` causes the
2467f296bb3SBarry Smith  object to be displayed graphically. See
2477f296bb3SBarry Smith  {any}`sec_graphics` for more on PETSc’s graphics support.
2487f296bb3SBarry Smith- To save an object to a file in ASCII format, the user creates the
2497f296bb3SBarry Smith  viewer object with the command
2507f296bb3SBarry Smith  `PetscViewerASCIIOpen(MPI_Comm comm, char* file, PetscViewer *viewer)`.
2517f296bb3SBarry Smith  This object is analogous to `PETSC_VIEWER_STDOUT_SELF` (for a
2527f296bb3SBarry Smith  communicator of `MPI_COMM_SELF`) and `PETSC_VIEWER_STDOUT_WORLD`
2537f296bb3SBarry Smith  (for a parallel communicator).
2547f296bb3SBarry Smith- To save an object to a file in binary format, the user creates the
2557f296bb3SBarry Smith  viewer object with the command
2567f296bb3SBarry Smith  `PetscViewerBinaryOpen(MPI_Comm comm, char* file, PetscViewerBinaryType type, PetscViewer *viewer)`.
2577f296bb3SBarry Smith  Details of binary I/O are discussed below.
2587f296bb3SBarry Smith- Vector and matrix objects can be passed to a running MATLAB process
2597f296bb3SBarry Smith  with a viewer created by
2607f296bb3SBarry Smith  `PetscViewerSocketOpen(MPI_Comm comm, char *machine, int port, PetscViewer *viewer)`.
2617f296bb3SBarry Smith  See {any}`sec_matlabsocket`.
2627f296bb3SBarry Smith
2637f296bb3SBarry SmithThe user can control the format of ASCII printed objects with viewers
2647f296bb3SBarry Smithcreated by `PetscViewerASCIIOpen()` by calling
2657f296bb3SBarry Smith
2667f296bb3SBarry Smith```
2677f296bb3SBarry SmithPetscViewerPushFormat(PetscViewer viewer, PetscViewerFormat format);
2687f296bb3SBarry Smith```
2697f296bb3SBarry Smith
2707f296bb3SBarry SmithFormats include `PETSC_VIEWER_DEFAULT`, `PETSC_VIEWER_ASCII_MATLAB`,
2717f296bb3SBarry Smithand `PETSC_VIEWER_ASCII_IMPL`. The implementation-specific format,
2727f296bb3SBarry Smith`PETSC_VIEWER_ASCII_IMPL`, displays the object in the most natural way
2737f296bb3SBarry Smithfor a particular implementation.
2747f296bb3SBarry Smith
2757f296bb3SBarry SmithThe routines
2767f296bb3SBarry Smith
2777f296bb3SBarry Smith```
2787f296bb3SBarry SmithPetscViewerPushFormat(PetscViewer viewer, PetscViewerFormat format);
2797f296bb3SBarry SmithPetscViewerPopFormat(PetscViewer viewer);
2807f296bb3SBarry Smith```
2817f296bb3SBarry Smith
2827f296bb3SBarry Smithallow one to temporarily change the format of a viewer.
2837f296bb3SBarry Smith
2847f296bb3SBarry SmithAs discussed above, one can output PETSc objects in binary format by
2857f296bb3SBarry Smithfirst opening a binary viewer with `PetscViewerBinaryOpen()` and then
2867f296bb3SBarry Smithusing `MatView()`, `VecView()`, etc. The corresponding routines for
2877f296bb3SBarry Smithinput of a binary object have the form `XXXLoad()`. In particular,
2887f296bb3SBarry Smithmatrix and vector binary input is handled by the following routines:
2897f296bb3SBarry Smith
2907f296bb3SBarry Smith```
2917f296bb3SBarry SmithMatLoad(Mat newmat, PetscViewer viewer);
2927f296bb3SBarry SmithVecLoad(Vec newvec, PetscViewer viewer);
2937f296bb3SBarry Smith```
2947f296bb3SBarry Smith
2957f296bb3SBarry SmithThese routines generate parallel matrices and vectors if the viewer’s
2967f296bb3SBarry Smithcommunicator has more than one process. The particular matrix and vector
2977f296bb3SBarry Smithformats are determined from the options database; see the manual pages
2987f296bb3SBarry Smithfor details.
2997f296bb3SBarry Smith
3007f296bb3SBarry SmithOne can provide additional information about matrix data for matrices
3017f296bb3SBarry Smithstored on disk by providing an optional file `matrixfilename.info`,
3027f296bb3SBarry Smithwhere `matrixfilename` is the name of the file containing the matrix.
3037f296bb3SBarry SmithThe format of the optional file is the same as the `.petscrc` file and
3047f296bb3SBarry Smithcan (currently) contain the following:
3057f296bb3SBarry Smith
3067f296bb3SBarry Smith```none
3077f296bb3SBarry Smith-matload_block_size <bs>
3087f296bb3SBarry Smith```
3097f296bb3SBarry Smith
3107f296bb3SBarry SmithThe block size indicates the size of blocks to use if the matrix is read
3117f296bb3SBarry Smithinto a block oriented data structure (for example, `MATMPIBAIJ`). The
3127f296bb3SBarry Smithdiagonal information `s1,s2,s3,...` indicates which (block) diagonals
3137f296bb3SBarry Smithin the matrix have nonzero values. The info file is automatically created
3147f296bb3SBarry Smithwhen `VecView()` or `MatView()` is used with a binary viewer; hence if you
3157f296bb3SBarry Smithsave a matrix with a given block size with `MatView()`, then a `MatLoad()`
3167f296bb3SBarry Smithon that file will automatically use the saved block size.
3177f296bb3SBarry Smith
3187f296bb3SBarry Smith(sec_viewfromoptions)=
3197f296bb3SBarry Smith
3207f296bb3SBarry Smith### Viewing From Options
3217f296bb3SBarry Smith
3227f296bb3SBarry SmithCommand-line options provide a particularly convenient way to view PETSc
3237f296bb3SBarry Smithobjects. All options of the form `-xxx_view` accept
3247f296bb3SBarry Smithcolon(`:`)-separated compound arguments which specify a viewer type,
3257f296bb3SBarry Smithformat, and/or destination (e.g. file name or socket) if appropriate.
3267f296bb3SBarry SmithFor example, to quickly export a binary file containing a matrix, one
3277f296bb3SBarry Smithmay use `-mat_view binary:matrix.out`, or to output to a
3287f296bb3SBarry SmithMATLAB-compatible ASCII file, one may use
3297f296bb3SBarry Smith`-mat_view ascii:matrix.m:ascii_matlab`. See the
3307f296bb3SBarry Smith`PetscOptionsCreateViewer()` man page for full details, as well as the
3317f296bb3SBarry Smith`XXXViewFromOptions()` man pages (for instance,
3327f296bb3SBarry Smith`PetscDrawSetFromOptions()`) for many other convenient command-line
3337f296bb3SBarry Smithoptions.
3347f296bb3SBarry Smith
3357f296bb3SBarry Smith### Using Viewers to Check Load Imbalance
3367f296bb3SBarry Smith
3377f296bb3SBarry SmithThe `PetscViewer` format `PETSC_VIEWER_LOAD_BALANCE` will cause certain
3387f296bb3SBarry Smithobjects to display simple measures of their imbalance. For example
3397f296bb3SBarry Smith
3407f296bb3SBarry Smith```none
3417f296bb3SBarry Smith-n 4 ./ex32 -ksp_view_mat ::load_balance
3427f296bb3SBarry Smith```
3437f296bb3SBarry Smith
3447f296bb3SBarry Smithwill display
3457f296bb3SBarry Smith
3467f296bb3SBarry Smith```none
3477f296bb3SBarry SmithNonzeros: Min 162  avg 168  max 174
3487f296bb3SBarry Smith```
3497f296bb3SBarry Smith
3507f296bb3SBarry Smithindicating that one process has 162 nonzero entries in the matrix, the
3517f296bb3SBarry Smithaverage number of nonzeros per process is 168 and the maximum number of
3527f296bb3SBarry Smithnonzeros is 174. Similar for vectors one can see the load balancing
3537f296bb3SBarry Smithwith, for example,
3547f296bb3SBarry Smith
3557f296bb3SBarry Smith```none
3567f296bb3SBarry Smith-n 4 ./ex32 -ksp_view_rhs ::load_balance
3577f296bb3SBarry Smith```
3587f296bb3SBarry Smith
3597f296bb3SBarry SmithThe measurements of load balancing can also be done within the program
3607f296bb3SBarry Smithwith calls to the appropriate object viewer with the viewer format
3617f296bb3SBarry Smith`PETSC_VIEWER_LOAD_BALANCE`.
3627f296bb3SBarry Smith
3637f296bb3SBarry Smith(sec_saws)=
3647f296bb3SBarry Smith
3657f296bb3SBarry Smith## Using SAWs with PETSc
3667f296bb3SBarry Smith
3677f296bb3SBarry SmithThe Scientific Application Web server, SAWs [^saws], allows one to monitor
3687f296bb3SBarry Smithrunning PETSc applications from a browser. To use SAWs you must `configure` PETSc with
3697f296bb3SBarry Smiththe option `--download-saws`. Options to use SAWs include
3707f296bb3SBarry Smith
3717f296bb3SBarry Smith- `-saws_options` - allows setting values in the PETSc options
3727f296bb3SBarry Smith  database via the browser (works only on one process).
3737f296bb3SBarry Smith- `-stack_view saws` - allows monitoring the current stack frame that
3747f296bb3SBarry Smith  PETSc is in; refresh to see the new location.
3757f296bb3SBarry Smith- `-snes_monitor_saws, -ksp_monitor_saws` - monitor the solvers’
3767f296bb3SBarry Smith  iterations from the web browser.
3777f296bb3SBarry Smith
3787f296bb3SBarry SmithFor each of these you need to point your browser to
3797f296bb3SBarry Smith`http://hostname:8080`, for example `http://localhost:8080`. Options
3807f296bb3SBarry Smiththat control behavior of SAWs include
3817f296bb3SBarry Smith
3827f296bb3SBarry Smith- `-saws_log filename` - log all SAWs actions in a file.
3837f296bb3SBarry Smith- `-saws_https certfile` - use HTTPS instead of HTTP with a
3847f296bb3SBarry Smith  certificate.
3857f296bb3SBarry Smith- `-saws_port_auto_select` - have SAWs pick a port number instead of
3867f296bb3SBarry Smith  using 8080.
3877f296bb3SBarry Smith- `-saws_port port` - use `port` instead of 8080.
3887f296bb3SBarry Smith- `-saws_root rootdirectory` - local directory to which the SAWs
3897f296bb3SBarry Smith  browser will have read access.
3907f296bb3SBarry Smith- `-saws_local` - use the local file system to obtain the SAWS
3917f296bb3SBarry Smith  javascript files (they much be in `rootdirectory/js`).
3927f296bb3SBarry Smith
3937f296bb3SBarry SmithAlso see the manual pages for `PetscSAWsBlock()`,
3947f296bb3SBarry Smith`PetscObjectSAWsTakeAccess()`, `PetscObjectSAWsGrantAccess()`,
3957f296bb3SBarry Smith`PetscObjectSAWsSetBlock()`, `PetscStackSAWsGrantAccess()`
3967f296bb3SBarry Smith`PetscStackSAWsTakeAccess()`, `KSPMonitorSAWs()`, and
3977f296bb3SBarry Smith`SNESMonitorSAWs()`.
3987f296bb3SBarry Smith
3997f296bb3SBarry Smith(sec_debugging)=
4007f296bb3SBarry Smith
4017f296bb3SBarry Smith## Debugging
4027f296bb3SBarry Smith
4037f296bb3SBarry SmithPETSc programs may be debugged using one of the two options below.
4047f296bb3SBarry Smith
4057f296bb3SBarry Smith- `-start_in_debugger` `[noxterm,dbx,xxgdb,xdb,xldb,lldb]`
4067f296bb3SBarry Smith  `[-display name]` - start all processes in debugger
4077f296bb3SBarry Smith- `-on_error_attach_debugger` `[noxterm,dbx,xxgdb,xdb,xldb,lldb]`
4087f296bb3SBarry Smith  `[-display name]` - start debugger only on encountering an error
4097f296bb3SBarry Smith
4107f296bb3SBarry SmithNote that, in general, debugging MPI programs cannot be done in the
4117f296bb3SBarry Smithusual manner of starting the programming in the debugger (because then
4127f296bb3SBarry Smithit cannot set up the MPI communication and remote processes).
4137f296bb3SBarry Smith
4147f296bb3SBarry SmithBy default on Linux systems the GNU debugger `gdb` is used, on macOS systems `lldb` is used
4157f296bb3SBarry Smith
4167f296bb3SBarry SmithBy default, the debugger will be started in a new
4177f296bb3SBarry Smithxterm (Apple Terminal on macOS), to enable running separate debuggers on each process, unless the
4187f296bb3SBarry Smithoption `noxterm` is used. In order to handle the MPI startup phase,
4197f296bb3SBarry Smiththe debugger command `cont` should be used to continue execution of
4207f296bb3SBarry Smiththe program within the debugger. Rerunning the program through the
4217f296bb3SBarry Smithdebugger requires terminating the first job and restarting the
4227f296bb3SBarry Smithprocessor(s); the usual `run` option in the debugger will not
4237f296bb3SBarry Smithcorrectly handle the MPI startup and should not be used. Not all
4247f296bb3SBarry Smithdebuggers work on all machines, the user may have to experiment to find
4257f296bb3SBarry Smithone that works correctly.
4267f296bb3SBarry Smith
4277f296bb3SBarry SmithYou can select a subset of the processes to be debugged (the rest just
4287f296bb3SBarry Smithrun without the debugger) with the option
4297f296bb3SBarry Smith
4307f296bb3SBarry Smith```none
4317f296bb3SBarry Smith-debugger_ranks rank1,rank2,...
4327f296bb3SBarry Smith```
4337f296bb3SBarry Smith
4347f296bb3SBarry Smithwhere you simply list the ranks you want the debugger to run with.
4357f296bb3SBarry Smith
4367f296bb3SBarry Smith(sec_errors)=
4377f296bb3SBarry Smith
4387f296bb3SBarry Smith## Error Handling
4397f296bb3SBarry Smith
4407f296bb3SBarry SmithErrors are handled through the routine `PetscError()`. This routine
4417f296bb3SBarry Smithchecks a stack of error handlers and calls the one on the top. If the
4427f296bb3SBarry Smithstack is empty, it selects `PetscTraceBackErrorHandler()`, which tries
4437f296bb3SBarry Smithto print a traceback. A new error handler can be put on the stack with
4447f296bb3SBarry Smith
4457f296bb3SBarry Smith```
4467f296bb3SBarry SmithPetscPushErrorHandler(PetscErrorCode (*HandlerFunction)(int line, char *dir, char *file, char *message, int number, void*), void *HandlerContext)
4477f296bb3SBarry Smith```
4487f296bb3SBarry Smith
4497f296bb3SBarry SmithThe arguments to `HandlerFunction()` are the line number where the
4507f296bb3SBarry Smitherror occurred, the file in which the error was detected, the
4517f296bb3SBarry Smithcorresponding directory, the error message, the error integer, and the
4527f296bb3SBarry Smith`HandlerContext.` The routine
4537f296bb3SBarry Smith
4547f296bb3SBarry Smith```
4557f296bb3SBarry SmithPetscPopErrorHandler()
4567f296bb3SBarry Smith```
4577f296bb3SBarry Smith
4587f296bb3SBarry Smithremoves the last error handler and discards it.
4597f296bb3SBarry Smith
4607f296bb3SBarry SmithPETSc provides two additional error handlers besides
4617f296bb3SBarry Smith`PetscTraceBackErrorHandler()`:
4627f296bb3SBarry Smith
4637f296bb3SBarry Smith```
4647f296bb3SBarry SmithPetscAbortErrorHandler()
4657f296bb3SBarry SmithPetscAttachDebuggerErrorHandler()
4667f296bb3SBarry Smith```
4677f296bb3SBarry Smith
4687f296bb3SBarry SmithThe function `PetscAbortErrorHandler()` calls abort on encountering an
4697f296bb3SBarry Smitherror, while `PetscAttachDebuggerErrorHandler()` attaches a debugger to the
4707f296bb3SBarry Smithrunning process if an error is detected. At runtime, these error
4717f296bb3SBarry Smithhandlers can be set with the options `-on_error_abort` or
4727f296bb3SBarry Smith`-on_error_attach_debugger` `[noxterm, dbx, xxgdb, xldb]`
4737f296bb3SBarry Smith`[-display DISPLAY]`.
4747f296bb3SBarry Smith
4757f296bb3SBarry SmithAll PETSc calls can be traced (useful for determining where a program is
4767f296bb3SBarry Smithhanging without running in the debugger) with the option
4777f296bb3SBarry Smith
4787f296bb3SBarry Smith```none
4797f296bb3SBarry Smith-log_trace [filename]
4807f296bb3SBarry Smith```
4817f296bb3SBarry Smith
4827f296bb3SBarry Smithwhere `filename` is optional. By default the traces are printed to the
4837f296bb3SBarry Smithscreen. This can also be set with the command
4847f296bb3SBarry Smith`PetscLogTraceBegin(FILE*)`.
4857f296bb3SBarry Smith
4867f296bb3SBarry SmithIt is also possible to trap signals by using the command
4877f296bb3SBarry Smith
4887f296bb3SBarry Smith```
4892a8381b2SBarry SmithPetscPushSignalHandler(PetscErrorCode (*Handler)(int, PetscCtx), PetscCtx ctx);
4907f296bb3SBarry Smith```
4917f296bb3SBarry Smith
4927f296bb3SBarry SmithThe default handler `PetscSignalHandlerDefault()` calls
4937f296bb3SBarry Smith`PetscError()` and then terminates. In general, a signal in PETSc
4947f296bb3SBarry Smithindicates a catastrophic failure. Any error handler that the user
4957f296bb3SBarry Smithprovides should try to clean up only before exiting. By default all
4967f296bb3SBarry SmithPETSc programs turn on the default PETSc signal handler in `PetscInitialize()`,
4977f296bb3SBarry Smiththis can be prevented with the option `-no_signal_handler` that can be provided on the command line,
4987f296bb3SBarry Smithin the ~./petscrc file, or with the call
4997f296bb3SBarry Smith
5007f296bb3SBarry Smith```
5017f296bb3SBarry SmithPetscCall(PetscOptionsSetValue(NULL, "-no_signal_handler", "true"));
5027f296bb3SBarry Smith```
5037f296bb3SBarry Smith
5047f296bb3SBarry SmithOnce the first PETSc signal handler has been pushed it is impossible to go back to
5057f296bb3SBarry Smithto a signal handler that was set directly by the user with the UNIX signal handler API or by
5067f296bb3SBarry Smiththe loader.
5077f296bb3SBarry Smith
5087f296bb3SBarry SmithSome Fortran compilers/loaders cause, by default, a traceback of the Fortran call stack when a
5097f296bb3SBarry Smithsegmentation violation occurs to be printed. This is handled by them setting a special signal handler
5107f296bb3SBarry Smithwhen the program is started up. This feature is useful for debugging without needing to start up a debugger.
5117f296bb3SBarry SmithIf `PetscPushSignalHandler()` has been called this traceback will not occur, hence if the Fortran traceback
5127f296bb3SBarry Smithis desired one should put
5137f296bb3SBarry Smith
5147f296bb3SBarry Smith```
5157f296bb3SBarry SmithPetscCallA(PetscOptionsSetValue(PETSC_NULL_OPTIONS,"-no_signal_handler","true",ierr))
5167f296bb3SBarry Smith```
5177f296bb3SBarry Smith
5187f296bb3SBarry Smith**before** the call to `PetscInitialize()`. This prevents PETSc from defaulting to using a signal handler.
5197f296bb3SBarry Smith
5207f296bb3SBarry SmithThere is a separate signal handler for floating-point exceptions. The
5217f296bb3SBarry Smithoption `-fp_trap` turns on the floating-point trap at runtime, and the
5227f296bb3SBarry Smithroutine
5237f296bb3SBarry Smith
5247f296bb3SBarry Smith```
5257f296bb3SBarry SmithPetscFPTrapPush(PetscFPTrap flag);
5267f296bb3SBarry Smith```
5277f296bb3SBarry Smith
5287f296bb3SBarry Smithcan be used within a program. A `flag` of `PETSC_FP_TRAP_ON` indicates that
5297f296bb3SBarry Smithfloating-point exceptions should be trapped, while a value of
5307f296bb3SBarry Smith`PETSC_FP_TRAP_OFF` (the default) indicates that they should be
5317f296bb3SBarry Smithignored.
5327f296bb3SBarry Smith
5337f296bb3SBarry Smith```
5347f296bb3SBarry SmithPetscFPTrapPop(void);
5357f296bb3SBarry Smith```
5367f296bb3SBarry Smith
5377f296bb3SBarry Smithshould be used to revert to the previous handling of floating point exceptions before the call to `PetscFPTrapPush()`.
5387f296bb3SBarry Smith
5397f296bb3SBarry SmithA small set of macros is used to make the error handling lightweight.
5407f296bb3SBarry SmithThese macros are used throughout the PETSc libraries and can be employed
5417f296bb3SBarry Smithby the application programmer as well. When an error is first detected,
5427f296bb3SBarry Smithone should set it by calling
5437f296bb3SBarry Smith
5447f296bb3SBarry Smith```
5457f296bb3SBarry SmithSETERRQ(MPI_Comm comm, PetscErrorCode flag, char *message);
5467f296bb3SBarry Smith```
5477f296bb3SBarry Smith
5487f296bb3SBarry SmithThe user should check the return codes for all PETSc routines (and
5497f296bb3SBarry Smithpossibly user-defined routines as well) with
5507f296bb3SBarry Smith
5517f296bb3SBarry Smith```
5527f296bb3SBarry SmithPetscCall(PetscRoutine(...));
5537f296bb3SBarry Smith```
5547f296bb3SBarry Smith
5557f296bb3SBarry SmithLikewise, all memory allocations should be checked with
5567f296bb3SBarry Smith
5577f296bb3SBarry Smith```
5587f296bb3SBarry SmithPetscCall(PetscMalloc1(n, &ptr));
5597f296bb3SBarry Smith```
5607f296bb3SBarry Smith
5617f296bb3SBarry SmithIf this procedure is followed throughout all of the user’s libraries and
5627f296bb3SBarry Smithcodes, any error will by default generate a clean traceback of the
5637f296bb3SBarry Smithlocation of the error.
5647f296bb3SBarry Smith
5657f296bb3SBarry SmithNote that the macro `PETSC_FUNCTION_NAME` is used to keep track of
5667f296bb3SBarry Smithroutine names during error tracebacks. Users need not worry about this
5677f296bb3SBarry Smithmacro in their application codes; however, users can take advantage of
5687f296bb3SBarry Smiththis feature if desired by setting this macro before each user-defined
5697f296bb3SBarry Smithroutine that may call `SETERRQ()`, `PetscCall()`. A simple example of
5707f296bb3SBarry Smithusage is given below.
5717f296bb3SBarry Smith
5727f296bb3SBarry Smith```
5737f296bb3SBarry SmithPetscErrorCode MyRoutine1()
5747f296bb3SBarry Smith{
5757f296bb3SBarry Smith    /* Declarations Here */
5767f296bb3SBarry Smith    PetscFunctionBeginUser;
5777f296bb3SBarry Smith    /* code here */
5787f296bb3SBarry Smith    PetscFunctionReturn(PETSC_SUCCESS);
5797f296bb3SBarry Smith}
5807f296bb3SBarry Smith```
5817f296bb3SBarry Smith
5827f296bb3SBarry Smith(sec_complex)=
5837f296bb3SBarry Smith
5847f296bb3SBarry Smith## Numbers
5857f296bb3SBarry Smith
5867f296bb3SBarry SmithPETSc supports the use of complex numbers in application programs
5877f296bb3SBarry Smithwritten in C, C++, and Fortran. To do so, we employ either the C99
5887f296bb3SBarry Smith`complex` type or the C++ versions of the PETSc libraries in which the
5897f296bb3SBarry Smithbasic “scalar” datatype, given in PETSc codes by `PetscScalar`, is
5907f296bb3SBarry Smithdefined as `complex` (or `complex<double>` for machines using
5917f296bb3SBarry Smithtemplated complex class libraries). To work with complex numbers, the
5927f296bb3SBarry Smithuser should run `configure` with the additional option
5937f296bb3SBarry Smith`--with-scalar-type=complex`. The
5947f296bb3SBarry Smith{doc}`installation instructions </install/index>`
5957f296bb3SBarry Smithprovide detailed instructions for installing PETSc. You can use
5967f296bb3SBarry Smith`--with-clanguage=c` (the default) to use the C99 complex numbers or
5977f296bb3SBarry Smith`--with-clanguage=c++` to use the C++ complex type [^cxx-note].
5987f296bb3SBarry Smith
5997f296bb3SBarry SmithRecall that each configuration of the PETSc libraries is stored in a different
6007f296bb3SBarry Smithdirectory, given by `$PETSC_DIR/$PETSC_ARCH`
6017f296bb3SBarry Smithaccording to the architecture. Thus, the libraries for complex numbers
6027f296bb3SBarry Smithare maintained separately from those for real numbers. When using any of
6037f296bb3SBarry Smiththe complex numbers versions of PETSc, *all* vector and matrix elements
6047f296bb3SBarry Smithare treated as complex, even if their imaginary components are zero. Of
6057f296bb3SBarry Smithcourse, one can elect to use only the real parts of the complex numbers
6067f296bb3SBarry Smithwhen using the complex versions of the PETSc libraries; however, when
6077f296bb3SBarry Smithworking *only* with real numbers in a code, one should use a version of
6087f296bb3SBarry SmithPETSc for real numbers for best efficiency.
6097f296bb3SBarry Smith
6107f296bb3SBarry SmithThe program
6117f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11.c.html">KSP Tutorial ex11</a>
6127f296bb3SBarry Smithsolves a linear system with a complex coefficient matrix. Its Fortran
6137f296bb3SBarry Smithcounterpart is
6147f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11f.F90.html">KSP Tutorial ex11f</a>.
6157f296bb3SBarry Smith
6167f296bb3SBarry Smith## Parallel Communication
6177f296bb3SBarry Smith
6187f296bb3SBarry SmithWhen used in a message-passing environment, all communication within
6197f296bb3SBarry SmithPETSc is done through MPI, the message-passing interface standard
6207f296bb3SBarry Smith{cite}`mpi-final`. Any file that includes `petscsys.h` (or
6217f296bb3SBarry Smithany other PETSc include file) can freely use any MPI routine.
6227f296bb3SBarry Smith
6237f296bb3SBarry Smith(sec_graphics)=
6247f296bb3SBarry Smith
6257f296bb3SBarry Smith## Graphics
6267f296bb3SBarry Smith
6277f296bb3SBarry SmithThe PETSc graphics library is not intended to compete with high-quality
6287f296bb3SBarry Smithgraphics packages. Instead, it is intended to be easy to use
6297f296bb3SBarry Smithinteractively with PETSc programs. We urge users to generate their
6307f296bb3SBarry Smithpublication-quality graphics using a professional graphics package. If a
6317f296bb3SBarry Smithuser wants to hook certain packages into PETSc, he or she should send a
6327f296bb3SBarry Smithmessage to
6337f296bb3SBarry Smith[petsc-maint@mcs.anl.gov](mailto:petsc-maint@mcs.anl.gov); we
6347f296bb3SBarry Smithwill see whether it is reasonable to try to provide direct interfaces.
6357f296bb3SBarry Smith
6367f296bb3SBarry Smith### Windows as PetscViewers
6377f296bb3SBarry Smith
6387f296bb3SBarry SmithFor drawing predefined PETSc objects such as matrices and vectors, one
6397f296bb3SBarry Smithmay first create a viewer using the command
6407f296bb3SBarry Smith
6417f296bb3SBarry Smith```
6427f296bb3SBarry SmithPetscViewerDrawOpen(MPI_Comm comm, char *display, char *title, int x, int y, int w, int h, PetscViewer *viewer);
6437f296bb3SBarry Smith```
6447f296bb3SBarry Smith
6457f296bb3SBarry SmithThis viewer may be passed to any of the `XXXView()` routines.
6467f296bb3SBarry SmithAlternately, one may use command-line options to quickly specify viewer
6477f296bb3SBarry Smithformats, including `PetscDraw`-based ones; see
6487f296bb3SBarry Smith{any}`sec_viewfromoptions`.
6497f296bb3SBarry Smith
6507f296bb3SBarry SmithTo draw directly into the viewer, one must obtain the `PetscDraw`
6517f296bb3SBarry Smithobject with the command
6527f296bb3SBarry Smith
6537f296bb3SBarry Smith```
6547f296bb3SBarry SmithPetscViewerDrawGetDraw(PetscViewer viewer, PetscDraw *draw);
6557f296bb3SBarry Smith```
6567f296bb3SBarry Smith
6577f296bb3SBarry SmithThen one can call any of the `PetscDrawXXX()` commands on the `draw`
6587f296bb3SBarry Smithobject. If one obtains the `draw` object in this manner, one does not
6597f296bb3SBarry Smithcall the `PetscDrawOpenX()` command discussed below.
6607f296bb3SBarry Smith
6617f296bb3SBarry SmithPredefined viewers, `PETSC_VIEWER_DRAW_WORLD` and
6627f296bb3SBarry Smith`PETSC_VIEWER_DRAW_SELF`, may be used at any time. Their initial use
6637f296bb3SBarry Smithwill cause the appropriate window to be created.
6647f296bb3SBarry Smith
6657f296bb3SBarry SmithImplementations using OpenGL, TikZ, and other formats may be selected
6667f296bb3SBarry Smithwith `PetscDrawSetType()`. PETSc can also produce movies; see
6677f296bb3SBarry Smith`PetscDrawSetSaveMovie()`, and note that command-line options can also
6687f296bb3SBarry Smithbe convenient; see the `PetscDrawSetFromOptions()` man page.
6697f296bb3SBarry Smith
6707f296bb3SBarry SmithBy default, PETSc drawing tools employ a private colormap, which
6717f296bb3SBarry Smithremedies the problem of poor color choices for contour plots due to an
6727f296bb3SBarry Smithexternal program’s mangling of the colormap. Unfortunately, this may
6737f296bb3SBarry Smithcause flashing of colors as the mouse is moved between the PETSc windows
6747f296bb3SBarry Smithand other windows. Alternatively, a shared colormap can be used via the
6757f296bb3SBarry Smithoption `-draw_x_shared_colormap`.
6767f296bb3SBarry Smith
6777f296bb3SBarry Smith### Simple PetscDrawing
6787f296bb3SBarry Smith
6797f296bb3SBarry SmithWith the default format, one can open a window that is not associated
6807f296bb3SBarry Smithwith a viewer directly under the X11 Window System with the
6817f296bb3SBarry Smithcommand
6827f296bb3SBarry Smith
6837f296bb3SBarry Smith```
6847f296bb3SBarry SmithPetscDrawCreate(MPI_Comm comm, char *display, char *title, int x, int y, int w, int h, PetscDraw *win);
6857f296bb3SBarry SmithPetscDrawSetFromOptions(win);
6867f296bb3SBarry Smith```
6877f296bb3SBarry Smith
6887f296bb3SBarry SmithAll drawing routines are performed relative to the window’s coordinate
6897f296bb3SBarry Smithsystem and viewport. By default, the drawing coordinates are from
6907f296bb3SBarry Smith`(0,0)` to `(1,1)`, where `(0,0)` indicates the lower left corner
6917f296bb3SBarry Smithof the window. The application program can change the window coordinates
6927f296bb3SBarry Smithwith the command
6937f296bb3SBarry Smith
6947f296bb3SBarry Smith```
6957f296bb3SBarry SmithPetscDrawSetCoordinates(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr);
6967f296bb3SBarry Smith```
6977f296bb3SBarry Smith
6987f296bb3SBarry SmithBy default, graphics will be drawn in the entire window. To restrict the
6997f296bb3SBarry Smithdrawing to a portion of the window, one may use the command
7007f296bb3SBarry Smith
7017f296bb3SBarry Smith```
7027f296bb3SBarry SmithPetscDrawSetViewPort(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr);
7037f296bb3SBarry Smith```
7047f296bb3SBarry Smith
7057f296bb3SBarry SmithThese arguments, which indicate the fraction of the window in which the
7067f296bb3SBarry Smithdrawing should be done, must satisfy
7077f296bb3SBarry Smith$0 \leq {\tt xl} \leq {\tt xr} \leq 1$ and
7087f296bb3SBarry Smith$0 \leq {\tt yl} \leq {\tt yr} \leq 1.$
7097f296bb3SBarry Smith
7107f296bb3SBarry SmithTo draw a line, one uses the command
7117f296bb3SBarry Smith
7127f296bb3SBarry Smith```
7137f296bb3SBarry SmithPetscDrawLine(PetscDraw win, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl);
7147f296bb3SBarry Smith```
7157f296bb3SBarry Smith
7167f296bb3SBarry SmithThe argument `cl` indicates the color (which is an integer between 0
7177f296bb3SBarry Smithand 255) of the line. A list of predefined colors may be found in
7187f296bb3SBarry Smith`include/petscdraw.h` and includes `PETSC_DRAW_BLACK`,
7197f296bb3SBarry Smith`PETSC_DRAW_RED`, `PETSC_DRAW_BLUE` etc.
7207f296bb3SBarry Smith
7217f296bb3SBarry SmithTo ensure that all graphics actually have been displayed, one should use
7227f296bb3SBarry Smiththe command
7237f296bb3SBarry Smith
7247f296bb3SBarry Smith```
7257f296bb3SBarry SmithPetscDrawFlush(PetscDraw win);
7267f296bb3SBarry Smith```
7277f296bb3SBarry Smith
7287f296bb3SBarry SmithWhen displaying by using double buffering, which is set with the command
7297f296bb3SBarry Smith
7307f296bb3SBarry Smith```
7317f296bb3SBarry SmithPetscDrawSetDoubleBuffer(PetscDraw win);
7327f296bb3SBarry Smith```
7337f296bb3SBarry Smith
7347f296bb3SBarry Smith*all* processes must call
7357f296bb3SBarry Smith
7367f296bb3SBarry Smith```
7377f296bb3SBarry SmithPetscDrawFlush(PetscDraw win);
7387f296bb3SBarry Smith```
7397f296bb3SBarry Smith
7407f296bb3SBarry Smithin order to swap the buffers. From the options database one may use
7417f296bb3SBarry Smith`-draw_pause` `n`, which causes the PETSc application to pause `n`
7427f296bb3SBarry Smithseconds at each `PetscDrawPause()`. A time of `-1` indicates that
7437f296bb3SBarry Smiththe application should pause until receiving mouse input from the user.
7447f296bb3SBarry Smith
7457f296bb3SBarry SmithText can be drawn with commands
7467f296bb3SBarry Smith
7477f296bb3SBarry Smith```
7487f296bb3SBarry SmithPetscDrawString(PetscDraw win, PetscReal x, PetscReal y, int color, char *text);
7497f296bb3SBarry SmithPetscDrawStringVertical(PetscDraw win, PetscReal x, PetscReal y, int color, const char *text);
7507f296bb3SBarry SmithPetscDrawStringCentered(PetscDraw win, PetscReal x, PetscReal y, int color, const char *text);
7517f296bb3SBarry SmithPetscDrawStringBoxed(PetscDraw draw, PetscReal sxl, PetscReal syl, int sc, int bc, const char text[], PetscReal *w, PetscReal *h);
7527f296bb3SBarry Smith```
7537f296bb3SBarry Smith
7547f296bb3SBarry SmithThe user can set the text font size or determine it with the commands
7557f296bb3SBarry Smith
7567f296bb3SBarry Smith```
7577f296bb3SBarry SmithPetscDrawStringSetSize(PetscDraw win, PetscReal width, PetscReal height);
7587f296bb3SBarry SmithPetscDrawStringGetSize(PetscDraw win, PetscReal *width, PetscReal *height);
7597f296bb3SBarry Smith```
7607f296bb3SBarry Smith
7617f296bb3SBarry Smith### Line Graphs
7627f296bb3SBarry Smith
7637f296bb3SBarry SmithPETSc includes a set of routines for manipulating simple two-dimensional
7647f296bb3SBarry Smithgraphs. These routines, which begin with `PetscDrawAxisDraw()`, are
7657f296bb3SBarry Smithusually not used directly by the application programmer. Instead, the
7667f296bb3SBarry Smithprogrammer employs the line graph routines to draw simple line graphs.
7677f296bb3SBarry SmithAs shown in the {ref}`listing below <listing_draw_test_ex3>`, line
7687f296bb3SBarry Smithgraphs are created with the command
7697f296bb3SBarry Smith
7707f296bb3SBarry Smith```
7717f296bb3SBarry SmithPetscDrawLGCreate(PetscDraw win, PetscInt ncurves, PetscDrawLG *ctx);
7727f296bb3SBarry Smith```
7737f296bb3SBarry Smith
7747f296bb3SBarry SmithThe argument `ncurves` indicates how many curves are to be drawn.
7757f296bb3SBarry SmithPoints can be added to each of the curves with the command
7767f296bb3SBarry Smith
7777f296bb3SBarry Smith```
7787f296bb3SBarry SmithPetscDrawLGAddPoint(PetscDrawLG ctx, PetscReal *x, PetscReal *y);
7797f296bb3SBarry Smith```
7807f296bb3SBarry Smith
7817f296bb3SBarry SmithThe arguments `x` and `y` are arrays containing the next point value
7827f296bb3SBarry Smithfor each curve. Several points for each curve may be added with
7837f296bb3SBarry Smith
7847f296bb3SBarry Smith```
7857f296bb3SBarry SmithPetscDrawLGAddPoints(PetscDrawLG ctx, PetscInt n, PetscReal **x, PetscReal **y);
7867f296bb3SBarry Smith```
7877f296bb3SBarry Smith
7887f296bb3SBarry SmithThe line graph is drawn (or redrawn) with the command
7897f296bb3SBarry Smith
7907f296bb3SBarry Smith```
7917f296bb3SBarry SmithPetscDrawLGDraw(PetscDrawLG ctx);
7927f296bb3SBarry Smith```
7937f296bb3SBarry Smith
7947f296bb3SBarry SmithA line graph that is no longer needed can be destroyed with the command
7957f296bb3SBarry Smith
7967f296bb3SBarry Smith```
7977f296bb3SBarry SmithPetscDrawLGDestroy(PetscDrawLG *ctx);
7987f296bb3SBarry Smith```
7997f296bb3SBarry Smith
8007f296bb3SBarry SmithTo plot new curves, one can reset a linegraph with the command
8017f296bb3SBarry Smith
8027f296bb3SBarry Smith```
8037f296bb3SBarry SmithPetscDrawLGReset(PetscDrawLG ctx);
8047f296bb3SBarry Smith```
8057f296bb3SBarry Smith
8067f296bb3SBarry SmithThe line graph automatically determines the range of values to display
8077f296bb3SBarry Smithon the two axes. The user can change these defaults with the command
8087f296bb3SBarry Smith
8097f296bb3SBarry Smith```
8107f296bb3SBarry SmithPetscDrawLGSetLimits(PetscDrawLG ctx, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax);
8117f296bb3SBarry Smith```
8127f296bb3SBarry Smith
8137f296bb3SBarry SmithIt is also possible to change the display of the axes and to label them.
8147f296bb3SBarry SmithThis procedure is done by first obtaining the axes context with the
8157f296bb3SBarry Smithcommand
8167f296bb3SBarry Smith
8177f296bb3SBarry Smith```
8187f296bb3SBarry SmithPetscDrawLGGetAxis(PetscDrawLG ctx, PetscDrawAxis *axis);
8197f296bb3SBarry Smith```
8207f296bb3SBarry Smith
8217f296bb3SBarry SmithOne can set the axes’ colors and labels, respectively, by using the
8227f296bb3SBarry Smithcommands
8237f296bb3SBarry Smith
8247f296bb3SBarry Smith```
8257f296bb3SBarry SmithPetscDrawAxisSetColors(PetscDrawAxis axis, int axis_lines, int ticks, int text);
8267f296bb3SBarry SmithPetscDrawAxisSetLabels(PetscDrawAxis axis, char *top, char *x, char *y);
8277f296bb3SBarry Smith```
8287f296bb3SBarry Smith
8297f296bb3SBarry SmithIt is possible to turn off all graphics with the option `-nox`. This
8307f296bb3SBarry Smithwill prevent any windows from being opened or any drawing actions to be
8317f296bb3SBarry Smithdone. This is useful for running large jobs when the graphics overhead
8327f296bb3SBarry Smithis too large, or for timing.
8337f296bb3SBarry Smith
8347f296bb3SBarry SmithThe full example, <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/draw/tests/ex3.c.html">Draw Test ex3</a>,
8357f296bb3SBarry Smithfollows.
8367f296bb3SBarry Smith
8377f296bb3SBarry Smith(listing_draw_test_ex3)=
8387f296bb3SBarry Smith
8397f296bb3SBarry Smith:::{admonition} Listing: `src/classes/draw/tests/ex3.c`
8407f296bb3SBarry Smith:name: snes-ex1
8417f296bb3SBarry Smith
8427f296bb3SBarry Smith```{literalinclude} /../src/sys/classes/draw/tests/ex3.c
8437f296bb3SBarry Smith:end-before: /*TEST
8447f296bb3SBarry Smith```
8457f296bb3SBarry Smith:::
8467f296bb3SBarry Smith
8477f296bb3SBarry Smith### Graphical Convergence Monitor
8487f296bb3SBarry Smith
8497f296bb3SBarry SmithFor both the linear and nonlinear solvers default routines allow one to
8507f296bb3SBarry Smithgraphically monitor convergence of the iterative method. These are
8517f296bb3SBarry Smithaccessed via the command line with `-ksp_monitor draw::draw_lg` and
8527f296bb3SBarry Smith`-snes_monitor draw::draw_lg`. See also
8537f296bb3SBarry Smith{any}`sec_kspmonitor` and {any}`sec_snesmonitor`.
8547f296bb3SBarry Smith
8557f296bb3SBarry Smith### Disabling Graphics at Compile Time
8567f296bb3SBarry Smith
8577f296bb3SBarry SmithTo disable all X-window-based graphics, run `configure` with the
8587f296bb3SBarry Smithadditional option `--with-x=0`
8597f296bb3SBarry Smith
8607f296bb3SBarry Smith(sec_developer_environments)=
8617f296bb3SBarry Smith
8627f296bb3SBarry Smith# Developer Environments
8637f296bb3SBarry Smith
864*2a28adfcSMartin DiehlCoding styles for most editors or integrated development environments are defined with [EditorConfig](https://editorconfig.org') in `.editorconfig`.
865*2a28adfcSMartin Diehl
8667f296bb3SBarry Smith## Emacs Users
8677f296bb3SBarry Smith
8687f296bb3SBarry SmithMany PETSc developers use Emacs, which can be used as a "simple" text editor or a comprehensive development environment.
8697f296bb3SBarry SmithFor a more integrated development environment, we recommend using [lsp-mode](https://emacs-lsp.github.io/lsp-mode/) (or [eglot](https://github.com/joaotavora/eglot)) with [clangd](https://clangd.llvm.org/).
8707f296bb3SBarry SmithThe most convenient way to teach clangd what compilation flags to use is to install [Bear](https://github.com/rizsotto/Bear) ("build ear") and run:
8717f296bb3SBarry Smith
8727f296bb3SBarry Smith```
8737f296bb3SBarry Smithbear make -B
8747f296bb3SBarry Smith```
8757f296bb3SBarry Smith
8767f296bb3SBarry Smithwhich will do a complete rebuild (`-B`) of PETSc and capture the compilation commands in a file named `compile_commands.json`, which will be automatically picked up by clangd.
8777f296bb3SBarry SmithYou can use the same procedure when building examples or your own project.
8787f296bb3SBarry SmithIt can also be used with any other editor that supports clangd, including VS Code and Vim.
8797f296bb3SBarry SmithWhen lsp-mode is accompanied by [flycheck](https://www.flycheck.org/en/latest/), Emacs will provide real-time feedback and syntax checking, along with refactoring tools provided by clangd.
8807f296bb3SBarry Smith
8817f296bb3SBarry SmithThe easiest way to install packages in recent Emacs is to use the "Options" menu to select "Manage Emacs Packages".
8827f296bb3SBarry Smith
8837f296bb3SBarry Smith### Tags
8847f296bb3SBarry Smith
8857f296bb3SBarry SmithIt is sometimes useful to cross-reference tags across projects.
8867f296bb3SBarry SmithRegardless of whether you use lsp-mode, it can be useful to use [GNU Global](https://www.gnu.org/software/global/) (install `gtags`) to provide reverse lookups (e.g. find all call sites
8877f296bb3SBarry Smithfor a given function) across all projects you might work on/browse.
8887f296bb3SBarry SmithTags for PETSc can be generated by running `make allgtags` from `$PETSC_DIR`, or one can generate tags for all projects by running a command such as
8897f296bb3SBarry Smith
8907f296bb3SBarry Smith```none
8917f296bb3SBarry Smithfind $PETSC_DIR/{include,src,tutorials,$PETSC_ARCH/include} any/other/paths \
8927f296bb3SBarry Smith   -regex '.*\.\(cc\|hh\|cpp\|cxx\|C\|hpp\|c\|h\|cu\)$' \
8937f296bb3SBarry Smith   | grep -v ftn-auto | gtags -f -
8947f296bb3SBarry Smith```
8957f296bb3SBarry Smith
8967f296bb3SBarry Smithfrom your home directory or wherever you keep source code. If you are
8977f296bb3SBarry Smithmaking large changes, it is useful to either set this up to run as a
8987f296bb3SBarry Smithcron job or to make a convenient alias so that refreshing is easy. Then
8997f296bb3SBarry Smithadd the following to `~/.emacs` to enable gtags and specify key bindings.
9007f296bb3SBarry Smith
9017f296bb3SBarry Smith```emacs
9027f296bb3SBarry Smith(when (require 'gtags)
9037f296bb3SBarry Smith  (global-set-key (kbd "C-c f") 'gtags-find-file)
9047f296bb3SBarry Smith  (global-set-key (kbd "C-c .") 'gtags-find-tag)
9057f296bb3SBarry Smith  (global-set-key (kbd "C-c r") 'gtags-find-rtag)
9067f296bb3SBarry Smith  (global-set-key (kbd "C-c ,") 'gtags-pop-stack))
9077f296bb3SBarry Smith(add-hook 'c-mode-common-hook
9087f296bb3SBarry Smith          '(lambda () (gtags-mode t))) ; Or add to existing hook
9097f296bb3SBarry Smith```
9107f296bb3SBarry Smith
9117f296bb3SBarry SmithA more basic alternative to the GNU Global (`gtags`) approach that does not require adding packages is to use
912343dd72aSBarry Smiththe builtin `etags` feature. First, run `make etags` from the
9137f296bb3SBarry SmithPETSc home directory to generate the file `$PETSC_DIR/TAGS`, and
9147f296bb3SBarry Smiththen from within Emacs, run
9157f296bb3SBarry Smith
9167f296bb3SBarry Smith```none
9177f296bb3SBarry SmithM-x visit-tags-table
9187f296bb3SBarry Smith```
9197f296bb3SBarry Smith
9207f296bb3SBarry Smithwhere `M` denotes the Emacs Meta key, and enter the name of the
9217f296bb3SBarry Smith`TAGS` file. Then the command `M-.` will cause Emacs to find the
9227f296bb3SBarry Smithfile and line number where a desired PETSc function is defined. Any
9237f296bb3SBarry Smithstring in any of the PETSc files can be found with the command `M-x tags-search`.
9247f296bb3SBarry SmithTo find repeated occurrences, one can simply use `M-,` to find the next occurrence.
9257f296bb3SBarry Smith
9267f296bb3SBarry Smith## VS Code Users
9277f296bb3SBarry Smith
9287f296bb3SBarry Smith[VS Code](https://code.visualstudio.com/) (unlike {ref}`sec_visual_studio`, described below) is an open-source editor with a rich extension ecosystem.
9297f296bb3SBarry SmithIt has [excellent integration](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd) with clangd and will automatically pick up `compile_commands.json`
9307f296bb3SBarry Smithas produced by a command such as `bear make -B` (see {ref}`sec_developer_environments`).
9317f296bb3SBarry SmithIf you have no prior attachment to a specific code editor, we recommend trying VS Code.
9327f296bb3SBarry Smith
9337f296bb3SBarry Smith## Vi and Vim Users
9347f296bb3SBarry Smith
9357f296bb3SBarry SmithThis section lists helpful Vim commands for PETSc. Ones that configure Vim can be placed
9367f296bb3SBarry Smithin a `.vimrc` file in the top of the PETSc directory and will be loaded automatically.
9377f296bb3SBarry Smith
9387f296bb3SBarry SmithVim has configurable keymaps: all of the "command mode" commands given that start with
9397f296bb3SBarry Smitha colon (such as `:help`) can be assigned to short sequences in "normal mode," which
9407f296bb3SBarry Smithis how most Vim users use their most frequently used commands.
9417f296bb3SBarry Smith
9427f296bb3SBarry SmithSee the {ref}`sec_developer_environments` discussion above for configuration of clangd, which
9437f296bb3SBarry Smithprovides integrated development environment.
9447f296bb3SBarry Smith
9457f296bb3SBarry Smith### Tags
9467f296bb3SBarry Smith
9477f296bb3SBarry SmithThe `tags` feature can be used to search PETSc files quickly and efficiently.
9487f296bb3SBarry SmithTo use this feature, one should first check if the file, `$PETSC_DIR/CTAGS`
9497f296bb3SBarry Smithexists. If this file is not present, it should be generated by running `make
950343dd72aSBarry Smithetags` from the PETSc home directory. Once the file exists, from Vi/Vim the
9517f296bb3SBarry Smithuser should issue the command
9527f296bb3SBarry Smith
9537f296bb3SBarry Smith```none
9547f296bb3SBarry Smith:set tags=CTAGS
9557f296bb3SBarry Smith```
9567f296bb3SBarry Smith
9577f296bb3SBarry Smithfrom the `$PETSC_DIR` directory and enter the name of the `CTAGS` file. The
9587f296bb3SBarry Smithcommand `:tag functionname` will cause Vi/Vim to open the file and line
9597f296bb3SBarry Smithnumber where a desired PETSc function is defined in the current window.
9607f296bb3SBarry Smith`<Ctrl-o>` will return the screen to your previous location.
9617f296bb3SBarry Smith
9627f296bb3SBarry SmithThe command `:stag functionname` will split the current window and then open
9637f296bb3SBarry Smiththe file and line number for that function in one half. Some prefer this because
9647f296bb3SBarry Smithit is easier to compare the file you are editing to the function definition this way.
9657f296bb3SBarry Smith
9667f296bb3SBarry Smith### Cscope and gtags
9677f296bb3SBarry Smith
9687f296bb3SBarry SmithVim can also use the `cscope` utility to navigate source code. One useful thing
9697f296bb3SBarry Smithit can do that the basic `tags` feature can't is search for references to a symbol,
9707f296bb3SBarry Smithrather than its definition, which can be useful for refactoring. The command
9717f296bb3SBarry Smith
9727f296bb3SBarry Smith```none
9737f296bb3SBarry Smith:cs find s functionname
9747f296bb3SBarry Smith```
9757f296bb3SBarry Smith
9767f296bb3SBarry Smithopens a list of all of the places the function is called in PETSc, and opens
9777f296bb3SBarry Smiththe file and line that you choose. The variant `:scs find s functionname`
9787f296bb3SBarry Smithdoes the same but splits the window like `stag`.
9797f296bb3SBarry Smith
9807f296bb3SBarry SmithThe PETSc makefile does not have a command for building a cscope database, but
9817f296bb3SBarry SmithGNU Global is cross-compatible with cscope: call `make allgtags` to make the
9827f296bb3SBarry Smithgtags database, and run the commands
9837f296bb3SBarry Smith
9847f296bb3SBarry Smith```none
9857f296bb3SBarry Smith:set csprg=gtags-cscope
9867f296bb3SBarry Smith:cs add GTAGS
9877f296bb3SBarry Smith```
9887f296bb3SBarry Smith
9897f296bb3SBarry Smith### Quickfix
9907f296bb3SBarry Smith
9917f296bb3SBarry SmithRather than exiting editing a file to build the library and check for errors or
9927f296bb3SBarry Smithwarnings, calling `:make` runs the make command without leaving Vim and
9937f296bb3SBarry Smithcollects the errors and warnings in a "quickfix" window. Move the cursor to
9947f296bb3SBarry Smithone of the errors or warnings in the quickfix window and press `<Enter>` and
9957f296bb3SBarry Smiththe main window will jump to the file and line with the error. The following
9967f296bb3SBarry Smithcommands filter lines of out PETSc's make output that can clutter the quickfix window:
9977f296bb3SBarry Smith
9987f296bb3SBarry Smith```none
9997f296bb3SBarry Smith:set efm^=%-GStarting\ make\ run\ on\ %.%#
10007f296bb3SBarry Smith:set efm^=%-GMachine\ characteristics:\ %.%#
10017f296bb3SBarry Smith:set efm^=%-G#define\ PETSC%.%#
10027f296bb3SBarry Smith```
10037f296bb3SBarry Smith
10047f296bb3SBarry Smith### Autocompletion and snippets
10057f296bb3SBarry Smith
10067f296bb3SBarry SmithAutocompletion of long function names can be helpful when working with PETSc.
10077f296bb3SBarry SmithIf you have a tags file, you can press `<Ctrl-N>` when you have partially
10087f296bb3SBarry Smithtyped a word to bring up a list of potential completions that you can choose
10097f296bb3SBarry Smithfrom with `<Tab>`.
10107f296bb3SBarry Smith
10117f296bb3SBarry SmithMore powerful autocompletion, such as completing the fieldname of a struct, is
10127f296bb3SBarry Smithavailable from external plugins that can be added to Vim, such as [SuperTab](https://github.com/ervandew/supertab), [VimCompletesMe](https://github.com/ackyshake/VimCompletesMe), or [YouCompleteMe](https://github.com/ycm-core/YouCompleteMe).
10137f296bb3SBarry Smith
10147f296bb3SBarry SmithAlong the same lines, plugins can be added that fill in the boilerplate
10157f296bb3SBarry Smithassociated with PETSc programming with code snippets. One such tool is
10167f296bb3SBarry Smith[UltiSnips](https://github.com/sirver/UltiSnips).
10177f296bb3SBarry Smith
10187f296bb3SBarry Smith### LSP for Vim
10197f296bb3SBarry Smith
10207f296bb3SBarry SmithSeveral plugins provide the equivalent of emacs' lsp-mode: YouCompleteMe,
10217f296bb3SBarry Smithmentioned above, is one; another popular one is [ale](https://github.com/dense-analysis/ale). These can check for syntax errors,
10227f296bb3SBarry Smithcheck for compilation errors in the background, and provide sophisticated tools
10237f296bb3SBarry Smithfor refactoring. Like lsp-mode, they also rely on a compilation database, so
10247f296bb3SBarry Smith`bear -- make -B` should be used as well to generate the file
10257f296bb3SBarry Smith`compile_commands.json`.
10267f296bb3SBarry Smith
10277f296bb3SBarry SmithSee [online tutorials](http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html)
10287f296bb3SBarry Smithfor additional Vi/Vim options.
10297f296bb3SBarry Smith
10307f296bb3SBarry Smith## Eclipse Users
10317f296bb3SBarry Smith
10327f296bb3SBarry SmithIf you are interested in developing code that uses PETSc from Eclipse or
10337f296bb3SBarry Smithdeveloping PETSc in Eclipse and have knowledge of how to do indexing and
10347f296bb3SBarry Smithbuild libraries in Eclipse, please contact us at
10357f296bb3SBarry Smith[petsc-dev@mcs.anl.gov](mailto:petsc-dev@mcs.anl.gov).
10367f296bb3SBarry Smith
10377f296bb3SBarry SmithOne way to index and build PETSc in Eclipse is as follows.
10387f296bb3SBarry Smith
10397f296bb3SBarry Smith1. Open
10407f296bb3SBarry Smith   “File$\rightarrow$Import$\rightarrow$Git$\rightarrow$Projects
10417f296bb3SBarry Smith   from Git”. In the next two panels, you can either add your existing
10427f296bb3SBarry Smith   local repository or download PETSc from Bitbucket by providing the
10437f296bb3SBarry Smith   URL. Most Eclipse distributions come with Git support. If not,
10447f296bb3SBarry Smith   install the EGit plugin. When importing the project, select the
10457f296bb3SBarry Smith   wizard “Import as general project”.
10467f296bb3SBarry Smith2. Right-click on the project (or the “File” menu on top) and select
10477f296bb3SBarry Smith   “New $\rightarrow$ Convert to a C/C++ Project (Adds C/C++
10487f296bb3SBarry Smith   Nature)”. In the setting window, choose “C Project” and specify the
10497f296bb3SBarry Smith   project type as “Shared Library”.
10507f296bb3SBarry Smith3. Right-click on the C project and open the “Properties” panel. Under
10517f296bb3SBarry Smith   “C/C++ Build $\rightarrow$ Builder Settings”, set the Build
10527f296bb3SBarry Smith   directory to `$PETSC_DIR` and make sure “Generate Makefiles
10537f296bb3SBarry Smith   automatically” is unselected. Under the section “C/C++
10547f296bb3SBarry Smith   General$\rightarrow$Paths and Symbols”, add the PETSc paths
10557f296bb3SBarry Smith   to “Includes”.
10567f296bb3SBarry Smith
10577f296bb3SBarry Smith> ```none
10587f296bb3SBarry Smith>      $PETSC_DIR/include
10597f296bb3SBarry Smith>      $PETSC_DIR/$PETSC_ARCH/include
10607f296bb3SBarry Smith>
10617f296bb3SBarry Smith> Under the section “C/C++ General\ :math:`\rightarrow`\ index”, choose
10627f296bb3SBarry Smith> “Use active build configuration”.
10637f296bb3SBarry Smith> ```
10647f296bb3SBarry Smith
10657f296bb3SBarry Smith1. Configure PETSc normally outside Eclipse to generate a makefile and
10667f296bb3SBarry Smith   then build the project in Eclipse. The source code will be parsed by
10677f296bb3SBarry Smith   Eclipse.
10687f296bb3SBarry Smith
10697f296bb3SBarry SmithIf you launch Eclipse from the Dock on Mac OS X, `.bashrc` will not be
10707f296bb3SBarry Smithloaded (a known OS X behavior, for security reasons). This will be a
10717f296bb3SBarry Smithproblem if you set the environment variables `$PETSC_DIR` and
10727f296bb3SBarry Smith`$PETSC_ARCH` in `.bashrc`. A solution which involves replacing the
10737f296bb3SBarry Smithexecutable can be found at
10747f296bb3SBarry Smith`` `/questions/829749/launch-mac-eclipse-with-environment-variables-set `` \</questions/829749/launch-mac-eclipse-with-environment-variables-set>\`\_\_.
10757f296bb3SBarry SmithAlternatively, you can add `$PETSC_DIR` and `$PETSC_ARCH` manually
10767f296bb3SBarry Smithunder “Properties $\rightarrow$ C/C++ Build $\rightarrow$
10777f296bb3SBarry SmithEnvironment”.
10787f296bb3SBarry Smith
10797f296bb3SBarry SmithTo allow an Eclipse code to compile with the PETSc include files and
10807f296bb3SBarry Smithlink with the PETSc libraries, a PETSc user has suggested the following.
10817f296bb3SBarry Smith
10827f296bb3SBarry Smith1. Right-click on your C project and select “Properties
10837f296bb3SBarry Smith   $\rightarrow$ C/C++ Build $\rightarrow$ Settings”
10847f296bb3SBarry Smith2. A new window on the righthand side appears with various settings
10857f296bb3SBarry Smith   options. Select “Includes” and add the required PETSc paths,
10867f296bb3SBarry Smith
10877f296bb3SBarry Smith```none
10887f296bb3SBarry Smith$PETSC_DIR/include
10897f296bb3SBarry Smith$PETSC_DIR/$PETSC_ARCH/include
10907f296bb3SBarry Smith```
10917f296bb3SBarry Smith
10927f296bb3SBarry Smith1. Select “Libraries” under the header Linker and set the library search
10937f296bb3SBarry Smith   path:
10947f296bb3SBarry Smith
10957f296bb3SBarry Smith```none
10967f296bb3SBarry Smith   $PETSC_DIR/$PETSC_ARCH/lib
10977f296bb3SBarry Smith
10987f296bb3SBarry Smithand the libraries, for example
10997f296bb3SBarry Smith```
11007f296bb3SBarry Smith
11017f296bb3SBarry Smith```none
11027f296bb3SBarry Smithm, petsc, stdc++, mpichxx, mpich, lapack, blas, gfortran, dl, rt,gcc_s, pthread, X11
11037f296bb3SBarry Smith```
11047f296bb3SBarry Smith
11057f296bb3SBarry SmithAnother PETSc user has provided the following steps to build an Eclipse
11067f296bb3SBarry Smithindex for PETSc that can be used with their own code, without compiling
11077f296bb3SBarry SmithPETSc source into their project.
11087f296bb3SBarry Smith
1109f605775fSPierre Jolivet1. In the user project source directory, create a symlink to the PETSc
11107f296bb3SBarry Smith   `src/` directory.
11117f296bb3SBarry Smith2. Refresh the project explorer in Eclipse, so the new symlink is
11127f296bb3SBarry Smith   followed.
11137f296bb3SBarry Smith3. Right-click on the project in the project explorer, and choose “Index
11147f296bb3SBarry Smith   $\rightarrow$ Rebuild”. The index should now be built.
11157f296bb3SBarry Smith4. Right-click on the PETSc symlink in the project explorer, and choose
11167f296bb3SBarry Smith   “Exclude from build...” to make sure Eclipse does not try to compile
11177f296bb3SBarry Smith   PETSc with the project.
11187f296bb3SBarry Smith
11197f296bb3SBarry SmithFor further examples of using Eclipse with a PETSc-based application,
11207f296bb3SBarry Smithsee the documentation for LaMEM [^lamem].
11217f296bb3SBarry Smith
11227f296bb3SBarry Smith## Qt Creator Users
11237f296bb3SBarry Smith
11247f296bb3SBarry SmithThis information was provided by Mohammad Mirzadeh. The Qt Creator IDE
11257f296bb3SBarry Smithis part of the Qt SDK, developed for cross-platform GUI programming
11267f296bb3SBarry Smithusing C++. It is available under GPL v3, LGPL v2 and a commercial
11277f296bb3SBarry Smithlicense and may be obtained, either as part of the Qt SDK or as
11287f296bb3SBarry Smithstand-alone software. It supports
11297f296bb3SBarry Smithautomatic makefile generation using cross-platform `qmake` and
11307f296bb3SBarry SmithCMake build systems as well as allowing one to import projects based
11317f296bb3SBarry Smithon existing, possibly hand-written, makefiles. Qt Creator has a visual
11327f296bb3SBarry Smithdebugger using GDB and LLDB (on Linux and OS X) or Microsoft’s CDB (on
11337f296bb3SBarry SmithMicrosoft Windows) as backends. It also has an interface to Valgrind’s “memcheck”
11347f296bb3SBarry Smithand “callgrind” tools to detect memory leaks and profile code. It has
11357f296bb3SBarry Smithbuilt-in support for a variety of version control systems including git,
11367f296bb3SBarry Smithmercurial, and subversion. Finally, Qt Creator comes fully equipped with
11377f296bb3SBarry Smithauto-completion, function look-up, and code refactoring tools. This
11387f296bb3SBarry Smithenables one to easily browse source files, find relevant functions, and
11397f296bb3SBarry Smithrefactor them across an entire project.
11407f296bb3SBarry Smith
11417f296bb3SBarry Smith### Creating a Project
11427f296bb3SBarry Smith
11437f296bb3SBarry SmithWhen using Qt Creator with `qmake`, one needs a `.pro` file. This
11447f296bb3SBarry Smithconfiguration file tells Qt Creator about all build/compile options and
11457f296bb3SBarry Smithlocations of source files. One may start with a blank `.pro` file and
11467f296bb3SBarry Smithfill in configuration options as needed. For example:
11477f296bb3SBarry Smith
11487f296bb3SBarry Smith```none
11497f296bb3SBarry Smith# The name of the application executable
11507f296bb3SBarry SmithTARGET = ex1
11517f296bb3SBarry Smith
11527f296bb3SBarry Smith# There are two ways to add PETSc functionality
11537f296bb3SBarry Smith# 1-Manual: Set all include path and libs required by PETSc
11547f296bb3SBarry SmithPETSC_INCLUDE = path/to/petsc_includes # e.g. obtained via running `make getincludedirs'
11557f296bb3SBarry SmithPETSC_LIBS = path/to/petsc_libs # e.g. obtained via running `make getlinklibs'
11567f296bb3SBarry Smith
11577f296bb3SBarry SmithINCLUDEPATH += $$PETSC_INCLUDES
11587f296bb3SBarry SmithLIBS += $$PETSC_LIBS
11597f296bb3SBarry Smith
11607f296bb3SBarry Smith# 2-Automatic: Use the PKGCONFIG functionality
11617f296bb3SBarry Smith# NOTE: petsc.pc must be in the pkgconfig path. You might need to adjust PKG_CONFIG_PATH
11627f296bb3SBarry SmithCONFIG += link_pkgconfig
11637f296bb3SBarry SmithPKGCONFIG += PETSc
11647f296bb3SBarry Smith
11657f296bb3SBarry Smith# Set appropriate compiler and its flags
11667f296bb3SBarry SmithQMAKE_CC = path/to/mpicc
11677f296bb3SBarry SmithQMAKE_CXX = path/to/mpicxx # if this is a cpp project
11687f296bb3SBarry SmithQMAKE_LINK = path/to/mpicxx # if this is a cpp project
11697f296bb3SBarry Smith
11707f296bb3SBarry SmithQMAKE_CFLAGS   += -O3 # add extra flags here
11717f296bb3SBarry SmithQMAKE_CXXFLAGS += -O3
11727f296bb3SBarry SmithQMAKE_LFLAGS   += -O3
11737f296bb3SBarry Smith
11747f296bb3SBarry Smith# Add all files that must be compiled
11757f296bb3SBarry SmithSOURCES += ex1.c source1.c source2.cpp
11767f296bb3SBarry Smith
11777f296bb3SBarry SmithHEADERS += source1.h source2.h
11787f296bb3SBarry Smith
11797f296bb3SBarry Smith# OTHER_FILES are ignored during compilation but will be shown in file panel in Qt Creator
11807f296bb3SBarry SmithOTHER_FILES += \
11817f296bb3SBarry Smith    path/to/resource_file \
11827f296bb3SBarry Smith    path/to/another_file
11837f296bb3SBarry Smith```
11847f296bb3SBarry Smith
11857f296bb3SBarry SmithIn this example, keywords include:
11867f296bb3SBarry Smith
11877f296bb3SBarry Smith- `TARGET`: The name of the application executable.
11887f296bb3SBarry Smith- `INCLUDEPATH`: Used at compile time to point to required include
11897f296bb3SBarry Smith  files. Essentially, it is used as an `-I \$\$INCLUDEPATH` flag for
11907f296bb3SBarry Smith  the compiler. This should include all application-specific header
11917f296bb3SBarry Smith  files and those related to PETSc (which may be found via
11927f296bb3SBarry Smith  `make getincludedirs`).
11937f296bb3SBarry Smith- `LIBS`: Defines all required external libraries to link with the
11947f296bb3SBarry Smith  application. To get PETSc’s linking libraries, use
11957f296bb3SBarry Smith  `make getlinklibs`.
11967f296bb3SBarry Smith- `CONFIG`: Configuration options to be used by `qmake`. Here, the
11977f296bb3SBarry Smith  option `link_pkgconfig` instructs `qmake` to internally use
11987f296bb3SBarry Smith  `pkgconfig` to resolve `INCLUDEPATH` and `LIBS` variables.
11997f296bb3SBarry Smith- `PKGCONFIG`: Name of the configuration file (the `.pc` file –
12007f296bb3SBarry Smith  here `petsc.pc`) to be passed to `pkgconfig`. Note that for this
12017f296bb3SBarry Smith  functionality to work, `petsc.pc` must be in path which might
12027f296bb3SBarry Smith  require adjusting the `PKG_CONFIG_PATH` environment variable. For
12037f296bb3SBarry Smith  more information see
12047f296bb3SBarry Smith  [the Qt Creator documentation](https://doc.qt.io/qtcreator/creator-build-settings.html).
12057f296bb3SBarry Smith- `QMAKE_CC` and `QMAKE_CXX`: Define which C/C++ compilers use.
12067f296bb3SBarry Smith- `QMAKE_LINK`: Defines the proper linker to be used. Relevant if
12077f296bb3SBarry Smith  compiling C++ projects.
12087f296bb3SBarry Smith- `QMAKE_CFLAGS`, `QMAKE_CXXFLAGS` and `QMAKE_LFLAGS`: Set the
12097f296bb3SBarry Smith  corresponding compile and linking flags.
12107f296bb3SBarry Smith- `SOURCES`: Source files to be compiled.
12117f296bb3SBarry Smith- `HEADERS`: Header files required by the application.
12127f296bb3SBarry Smith- `OTHER_FILES`: Other files to include (source, header, or any other
12137f296bb3SBarry Smith  extension). Note that none of the source files placed here are
12147f296bb3SBarry Smith  compiled.
12157f296bb3SBarry Smith
12167f296bb3SBarry SmithMore options can be included in a `.pro` file; see
12177f296bb3SBarry Smith<https://doc.qt.io/qt-5/qmake-project-files.html>. Once the `.pro` file
12187f296bb3SBarry Smithis generated, the user can simply open it via Qt Creator. Upon opening,
12197f296bb3SBarry Smithone has the option to create two different build options, debug and
12207f296bb3SBarry Smithrelease, and switch between the two. For more information on using the
12217f296bb3SBarry SmithQt Creator interface and other more advanced aspects of the IDE, refer
12227f296bb3SBarry Smithto <https://www.qt.io/qt-features-libraries-apis-tools-and-ide/>
12237f296bb3SBarry Smith
12247f296bb3SBarry Smith(sec_visual_studio)=
12257f296bb3SBarry Smith
12267f296bb3SBarry Smith## Visual Studio Users
12277f296bb3SBarry Smith
12287f296bb3SBarry SmithTo use PETSc from Microsoft Visual Studio, one would have to compile a PETSc
12297f296bb3SBarry Smithexample with its corresponding makefile and then transcribe all compiler
12307f296bb3SBarry Smithand linker options used in this build into a Visual Studio project file,
12317f296bb3SBarry Smithin the appropriate format in Visual Studio project settings.
12327f296bb3SBarry Smith
12337f296bb3SBarry Smith## Xcode IDE Users
12347f296bb3SBarry Smith
12357f296bb3SBarry SmithSee {any}`doc_macos_install` for the standard Unix command line tools approach to development on macOS. The information
12367f296bb3SBarry Smithbelow is only if you plan to write code within the Xcode IDE.
12377f296bb3SBarry Smith
12387f296bb3SBarry Smith### Apple Xcode IDE for macOS Applications
12397f296bb3SBarry Smith
12407f296bb3SBarry SmithFollow the instructions in `$PETSC_DIR/systems/Apple/OSX/bin/makeall`
12417f296bb3SBarry Smithto build the PETSc framework and documentation suitable for use in
12427f296bb3SBarry SmithXcode.
12437f296bb3SBarry Smith
12447f296bb3SBarry SmithYou can then use the PETSc framework in
12457f296bb3SBarry Smith`$PETSC_DIR/arch-osx/PETSc.framework` in the usual manner for Apple
12467f296bb3SBarry Smithframeworks. See the examples in
12477f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/OSX/examples`. When working in Xcode, things
12487f296bb3SBarry Smithlike function name completion should work for all PETSc functions as
12497f296bb3SBarry Smithwell as MPI functions. You must also link against the Apple
12507f296bb3SBarry Smith`Accelerate.framework`.
12517f296bb3SBarry Smith
12527f296bb3SBarry Smith### Apple Xcode IDE for iPhone/iPad iOS Applications
12537f296bb3SBarry Smith
12547f296bb3SBarry SmithFollow the instructions in
12557f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/iOS/bin/iosbuilder.py` to build the PETSc
12567f296bb3SBarry Smithlibrary for use on the iPhone/iPad.
12577f296bb3SBarry Smith
12587f296bb3SBarry SmithYou can then use the PETSc static library in
12597f296bb3SBarry Smith`$PETSC_DIR/arch-osx/libPETSc.a` in the usual manner for Apple
12607f296bb3SBarry Smithlibraries inside your iOS XCode projects; see the examples in
12617f296bb3SBarry Smith`$PETSC_DIR/systems/Apple/iOS/examples`. You must also link against
12627f296bb3SBarry Smiththe Apple `Accelerate.framework`.
12637f296bb3SBarry Smith
12647f296bb3SBarry SmithA thorough discussion of the
12657f296bb3SBarry Smithprocedure is given in [Comparison of Migration Techniques for High-Performance Code to Android and iOS](https://www.researchgate.net/publication/308973080_Comparison_of_Migration_Techniques_for_High-Performance_Code_to_Android_and_iOS).
12667f296bb3SBarry Smith
12677f296bb3SBarry SmithFor Android, you must have your standalone bin folder in the path, so that the compilers
12687f296bb3SBarry Smithare visible.
12697f296bb3SBarry Smith
12707f296bb3SBarry SmithThe installation process has not been tested for iOS or Android since 2017.
12717f296bb3SBarry Smith
12727f296bb3SBarry Smith```{rubric} Footnotes
12737f296bb3SBarry Smith```
12747f296bb3SBarry Smith
12757f296bb3SBarry Smith[^saws]: [Saws wiki on Bitbucket](https://bitbucket.org/saws/saws/wiki/Home)
12767f296bb3SBarry Smith
12777f296bb3SBarry Smith[^cxx-note]: Note that this option is not required to use PETSc with C++
12787f296bb3SBarry Smith
12797f296bb3SBarry Smith[^lamem]: See the `doc/` directory at <https://bitbucket.org/bkaus/lamem>
12807f296bb3SBarry Smith
12817f296bb3SBarry Smith[^json]: JSON is a subset of YAML
1282