PETSc Exercises

This set of exercises will lead your through many of the concepts and techniques used in PETSc. To run the actual examples requires you to have PETSc installed, but we do provide sample output so the exercises may still be valuable before you install PETSc.

0 - Setting up your environment

PETSc uses two environmental variables to determine where its libraries and include files are. 

PETSC_DIR indicates the directory where PETSc is installed. For example /usr/local/petsc-2.2.0
PETSC_ARCH indicates the architecture PETSc is installed for. For example linux

In csh or tcsh you can set these with, for example,

setenv PETSC_DIR /usr/local/petsc-2.2.0
setenv PETSC_ARCH linux

In sh or bash you can set them with

PETSC_DIR=/usr/local/petsc-2.2.0; export PETSC_DIR
PETSC_ARCH=linux; export PETSC_ARCH


1- compiling a PETSc program

PETSc provides a completely portable make system that works with all compilers on all systems, Unix, Linux, and Microsoft Windows. We highly recommend using this system by copying a PETSc makefile and editing as needed for your application. To compile a PETSc example simply type
   make exXX
For example,
   make   ex1

Try compiling a PETSc example

2 - running a PETSc program with simple output to the screen

PETSc programs are simply MPI programs. As with all MPI programs, calls to printf() are not well defined. If different processors call printf() the output can be jumbled, or even completely lost. PETSc provides several routines to allow one to print from their program in a rational way. 

Rational output from a PETSc program

3 - Vectors

PETSc uses vectors to store PDE solutions, field variables, right hand sides of linear systems, etc. In classic numerical computing these are store in simple arrays 

 Introduction to vectors

   A - using VecSetValues()

    B - accessing the array directly

        I - C/C++

        II - F77

        II - F90

4 - parallel data layout

    A - for structured grids using DA

Objective: To demonstrate use of distributed arrays (DA) to
manage a parallel structured mesh computation.
Program: ${PETSC_DIR}/src/snes/examples/tutorials/ex5.c
Runtime command: 
% mpiexec -np 4 ex5 -da_view_draw -snes_monitor -draw_pause -1
where
-da_view_draw : draw DA configuration

    B - for unstructured grids

Objective: To demonstrate use of vector scatters (VecScatter) 
to manage a parallel unstructured mesh computation.
Program: ${PETSC_DIR}/src/snes/examples/tutorials/ex10d/ex10.c and adj.in
Runtime command: 
% mpiexec -np 2 ex10 -snes_monitor

5 - parallel matrix assembly

PETSc provides a large number of sparse matrix formats and allows one to easily generate matrices without worrying about the details 

 Sample usage

6 - linear solves

KSP provides an easy-to-use interface to the combination of a
Krylov subspace iterative solver and a preconditioner (PC).

Sample usage

7 - nonlinear solves

SNES provides an easy-to-use interface to Newton-based methods for
solving systems of nonlinear equations. 

Sample usage

Advanced options

9 - time integration

The time-stepping (TS) provides simple ODE integrators and pseudo-time-stepping routines for difficult to solve nonlinear problems.

Sample usage of the ODE integrators

Sample usage of the pseudo-time-stepping