Docsity

User Documentation for KINSOL - Engineering Numerical Methods | ENGR 364, Lab Reports of Engineering

Partial preview of the text

Download User Documentation for KINSOL - Engineering Numerical Methods | ENGR 364 and more Lab Reports Engineering in PDF only on Docsity!

User Documentation for kinsol v2.5.

Aaron M. Collier, Alan C. Hindmarsh, Radu Serban, and Carol S. Woodward

Center for Applied Scientific Computing

Lawrence Livermore National Laboratory

November 6, 2006

UCRL-SM-

DISCLAIMER

This document was prepared as an account of work sponsored by an agency of the United States Government. Neither the United States Government nor the University of California nor any of their employees, makes any warranty, express or implied, or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness of any information, apparatus, product, or process disclosed, or represents that its use would not infringe privately owned rights. Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, or otherwise, does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United States Government or the University of California. The views and opinions of authors expressed herein do not necessarily state or reflect those of the United States Government or the University of California, and shall not be used for advertising or product endorsement purposes.

This research was supported under the auspices of the U.S. Department of Energy by the University of California, Lawrence Livermore National Laboratory under contract No. W-7405-Eng-48.

Approved for public release; further dissemination unlimited

  • 1 Introduction List of Figures vii
    • 1.1 Historical Background
    • 1.2 Changes from previous versions
    • 1.3 Reading this User Guide
  • 2 KINSOL Installation Procedure
    • 2.1 Configuration options
    • 2.2 Configuration examples
    • 2.3 Installed libraries and exported header files
    • 2.4 Building SUNDIALS without the configure script
  • 3 Mathematical Considerations
  • 4 Code Organization
    • 4.1 SUNDIALS organization
    • 4.2 KINSOL organization
  • 5 Using KINSOL for C Applications
    • 5.1 Access to library and header files
    • 5.2 Data types
    • 5.3 Header files
    • 5.4 A skeleton of the user’s main program
    • 5.5 User-callable functions
      • 5.5.1 KINSOL initialization and deallocation functions
      • 5.5.2 Linear solver specification functions
      • 5.5.3 KINSOL solver function
      • 5.5.4 Optional input functions
        • 5.5.4.1 Main solver optional input functions
        • 5.5.4.2 Dense linear solver
        • 5.5.4.3 Band linear solver
        • 5.5.4.4 SPILS linear solvers
      • 5.5.5 Optional output functions
        • 5.5.5.1 Main solver optional output functions
        • 5.5.5.2 Dense linear solver
        • 5.5.5.3 Band linear solver
        • 5.5.5.4 SPILS linear solvers
    • 5.6 User-supplied functions
      • 5.6.1 Problem-defining function
      • 5.6.2 Error message handler function
      • 5.6.3 Informational message handler function
      • 5.6.4 Jacobian information (direct method with dense Jacobian)
      • 5.6.5 Jacobian information (direct method with banded Jacobian)
      • 5.6.6 Jacobian information (matrix-vector product)
      • 5.6.7 Preconditioning (linear system solution)
      • 5.6.8 Preconditioning (Jacobian data)
    • 5.7 A parallel band-block-diagonal preconditioner module
  • 6 FKINSOL, an Interface Module for FORTRAN Applications
    • 6.1 Important note on portability
    • 6.2 FKINSOL routines
    • 6.3 Usage of the FKINSOL interface module
    • 6.4 FKINSOL optional input and output
    • 6.5 Usage of the FKINBBD interface to KINBBDPRE
  • 7 Description of the NVECTOR module
    • 7.1 The NVECTOR SERIAL implementation
    • 7.2 The NVECTOR PARALLEL implementation
    • 7.3 NVECTOR functions used by KINSOL
  • 8 Providing Alternate Linear Solver Modules
    • 8.1 Initialization function
    • 8.2 Setup function
    • 8.3 Solve function
    • 8.4 Memory deallocation function
  • 9 Generic Linear Solvers in SUNDIALS
    • 9.1 The DENSE module
      • 9.1.1 Type DenseMat
      • 9.1.2 Accessor Macros
      • 9.1.3 Functions
      • 9.1.4 Small Dense Matrix Functions
    • 9.2 The BAND module
      • 9.2.1 Type BandMat
      • 9.2.2 Accessor Macros
      • 9.2.3 Functions
    • 9.3 The SPGMR module
      • 9.3.1 Functions
    • 9.4 The SPBCG module
      • 9.4.1 Functions
    • 9.5 The SPTFQMR module
      • 9.5.1 Functions
  • 10 KINSOL Constants
    • 10.1 KINSOL input constants
    • 10.2 KINSOL output constants
  • Bibliography
  • Index

List of Tables

2.1 SUNDIALS libraries and header files (names are relative to libdir for libraries and to includedir for header files)................................. 12

5.1 Optional inputs for kinsol, kindense, and kinspils................... 33 5.2 Optional outputs from kinsol, kindense, kinband, and kinspils........... 43

6.1 Keys for setting fkinsol optional inputs.......................... 70 6.2 Description of the fkinsol optional output arrays IOUT and ROUT........... 71

7.1 Description of the NVECTOR operations......................... 77 7.2 List of vector functions usage by KINSOL code modules................. 84

v

List of Figures

4.1 Organization of the SUNDIALS suite............................ 20 4.2 Overall structure diagram of the KINSOL package.................... 21

9.1 Diagram of the storage for a matrix of type BandMat................... 94

vii

2 Introduction

There are several motivations for choosing the C language for kinsol. First, a general movement away from Fortran and toward C in scientific computing is apparent. Second, the pointer, structure, and dynamic memory allocation features in C are extremely useful in software of this complexity, with the great variety of method options offered. Finally, we prefer C over C++^ for kinsol because of the wider availability of C compilers, the potentially greater efficiency of C, and the greater ease of interfacing the solver to applications written in Fortran.

1.2 Changes from previous versions

Changes in v2.5.

The main changes in this release involve a rearrangement of the entire sundials source tree (see §4.1). At the user interface level, the main impact is in the mechanism of including sundials header files which must now include the relative path (e.g. #include <cvode/cvode.h>). Additional changes were made to the build system: all exported header files are now installed in separate subdirectories of the instaltion include directory. The functions in the generic dense linear solver (sundials dense and sundials smalldense) were modified to work for rectangular m×n matrices (m ≤ n), while the factorization and solution functions were renamed to DenseGETRF/denGETRF and DenseGETRS/denGETRS, respectively. The factorization and solution functions in the generic band linear solver were renamed BandGBTRF and BandGBTRS, respectively.

Changes in v2.4.

kinspbcg, kinsptfqmr, kindense, and kinband modules have been added to interface with the Scaled Preconditioned Bi-CGStab (spbcg), Scaled Preconditioned Transpose-Free Quasi-Minimal Residual (sptfqmr), dense, and band linear solver modules, respectively (for details see Chap- ter 5 ). Corresponding additions were made to the Fortran interface module fkinsol. At the same time, function type names for Scaled Preconditioned Iterative Linear Solvers were added for the user-supplied Jacobian-times-vector and preconditioner setup and solve functions.

Regarding the Fortran interface module fkinsol, optional inputs are now set using FKINSETIIN (integer inputs), FKINSETRIN (real inputs), and FKINSETVIN (vector inputs). Optional outputs are still obtained from the IOUT and ROUT arrays which are owned by the user and passed as arguments to FKINMALLOC.

The kindense and kinband linear solver modules include support for nonlinear residual moni- toring which can be used to control Jacobian updating. To reduce the possibility of conflicts, the names of all header files have been changed by adding unique prefixes (kinsol and sundials ). When using the default installation procedure, the header files are exported under various subdirectories of the target include directory. For more details see § 2.

Changes in v2.3.

The user interface has been further refined. Several functions used for setting optional inputs were combined into a single one. Additionally, to resolve potential variable scope issues, all SUNDIALS solvers release user data right after its use. The build systems has been further improved to make it more robust.

Changes in v2.2.

The changes in this minor sundials release affect only the build system.

1.3 Reading this User Guide 3

Changes in v2.2.

The major changes from the previous version involve a redesign of the user interface across the entire sundials suite. We have eliminated the mechanism of providing optional inputs and extracting optional statistics from the solver through the iopt and ropt arrays. Instead, kinsol now provides a set of routines (with prefix KINSet) to change the default values for various quantities controlling the solver and a set of extraction routines (with prefix KINGet) to extract statistics after return from the main solver routine. Similarly, each linear solver module provides its own set of set- and get-type routines. For more details see Chapter 5. Additionally, the interfaces to several user-supplied routines (such as those providing Jacobian- vector products and preconditioner information) were simplified by reducing the number of arguments. The same information that was previously accessible through such arguments can now be obtained through set-type functions.

1.3 Reading this User Guide

This user guide is a combination of general usage instructions and specific examples. We expect that some readers will want to concentrate on the general instructions, while others will refer mostly to the examples, and the organization is intended to accommodate both styles. There are different possible levels of usage of kinsol. The most casual user, with a small nonlinear system, can get by with reading all of Chapter 3 , then Chapter 5 through §5.5.3 only, and looking at examples in [ 3 ]. In a different direction, a more expert user with a nonlinear system may want to (a) use a package preconditioner (§5.7), (b) supply his/her own Jacobian or preconditioner routines (§5.6), (c) supply a new nvector module (Chapter 7 ), or even (d) supply a different linear solver module (§4.2 and Chapter 9 ). The structure of this document is as follows:

  • In Chapter 2 we begin with instructions for the installation of kinsol, within the structure of sundials.
  • In Chapter 3 , we provide short descriptions of the numerical methods implemented by kinsol for the solution of nonlinear systems.
  • The following chapter describes the structure of the sundials suite of solvers (§4.1) and the software organization of the kinsol solver (§4.2).
  • Chapter 5 is the main usage document for kinsol for C applications. It includes a complete description of the user interface for the solution of nonlinear algebraic systems.
  • In Chapter 6 , we describe fkinsol, an interface module for the use of kinsol with Fortran applications.
  • Chapter 7 gives a brief overview of the generic nvector module shared among the various com- ponents of sundials, and details on the two nvector implementations provided with sundials: a serial implementation (§7.1) and a parallel implementation based on MPI (§7.2).
  • Chapter 8 describes the interfaces to the linear solver modules, so that a user can provide his/her own such module.
  • Chapter 9 describes in detail the generic linear solvers shared by all sundials solvers.
  • Finally, Chapter 10 lists the constants used for input to and output from kinsol.

Finally, the reader should be aware of the following notational conventions in this user guide: program listings and identifiers (such as KINMalloc) within textual explanations appear in typewriter type style; fields in C structures (such as content) appear in italics; and packages or modules are written in all capitals. In the index, page numbers that appear in bold indicate the main reference for that entry.

Chapter 2

KINSOL Installation Procedure

The installation of kinsol is accomplished by installing the sundials suite as a whole, according to the instructions that follow. The same procedure applies whether or not the downloaded file contains solvers other than kinsol. 1 The sundials suite (or individual solvers) are distributed as compressed archives (.tgz). The name of the distribution archive is of the form solver-x.y.z.tgz, where solver is one of: sundials, cvode, cvodes, ida, or kinsol, and x.y.z represents the version number (of the sundials suite or of the individual solver). To begin the installation, first uncompress and expand the sources, by issuing % tar xzf solver-x.y.z.tgz

This will extract source files under a directory solver-x.y.z. The installation procedure outlined below will work on commodity LINUX/UNIX systems with- out modification. However, users are still encouraged to carefully read the entire chapter before attempting to install the sundials suite, in case non-default choices are desired for compilers, compi- lation options, or the like. In lieu of reading the option list below, the user may invoke the configuration script with the help flag to view a complete listing of available options, which may be done by issuing

% ./configure --help

from within the directory created above. In the remainder of this chapter, we make the following distinctions:

  • srcdir is the directory solver-x.y.z created above; i.e., the directory containing the sundials sources.
  • builddir is the directory under which sundials is built; i.e., the directory from within which the configure command is issued. Usually, this is the same as srcdir.
  • instdir is the directory under which the sundials exported header files and libraries will be installed. Typically, header files are exported under a directory instdir/include while libraries are installed under instdir/lib, with instdir specified with the --prefix flag to configure. See §2.1 for more details on the installation directories, including the special cases of the sundials examples and the sundialsTB toolbox.

Note: The installation directory instdir should not be the same as the source directory srcdir.! (^1) Files for both the serial and parallel versions of kinsol are included in the distribution. For users in a serial comput- ing environment, the files specific to parallel environments (which may be deleted) are as follows: all files in src/nvec par; kinbbdpre.c, kinbbdpre impl.h (in src/kinsol/); kinbbdpre.h (in include/kinsol/); fkinbbd.c, fkinbbd.h (in src/kinsol/fcmix/); all files in examples/kinsol/parallel/; all files in examples/kinsol/fcmix parallel/. (By “serial version” of kinsol we mean the kinsol solver with the serial nvector module attached, and similarly for “parallel version”.)

6 KINSOL Installation Procedure

The installation steps for sundials can be as simple as

% tar xzf solver-x.y.z.tgz % cd solver-x.y.z % ./configure % make % make install

in which case the sundials header files and libraries are installed under /usr/local/include and /usr/local/lib, respectively. Note that, by default, neither the example programs nor the sundi- alsTB toolbox are built and installed. If disk space is a priority, then to delete all temporary files created by building sundials, issue

% make clean To prepare the sundials distribution for a new install (using, for example, different options and/or installation destinations), issue

% make distclean

2.1 Configuration options

The installation procedure given above will generally work without modification; however, if the system includes multiple MPI implementations, then certain configure script-related options may be used to indicate which MPI implementation should be used. Also, if the user wants to use non-default language compilers, then, again, the necessary shell environment variables must be appropriately redefined. The remainder of this section provides explanations of available configure script options.

General options

--prefix=PREFIX

Location for architecture-independent files. Default: PREFIX=/usr/local

--exec-prefix=EPREFIX

Location for architecture-dependent files. Default: EPREFIX=/usr/local

--includedir=DIR

Alternate location for installation of header files. Default: DIR=PREFIX/include

--libdir=DIR

Alternate location for installation of libraries. Default: DIR=EPREFIX/lib

--disable-solver

Although each existing solver module is built by default, support for a given solver can be explicitly disabled using this option. The valid values for solver are: cvode, cvodes, ida, and kinsol.

--enable-examples

Available example programs are not built by default. Use this option to enable compilation of all pertinent example programs. Upon completion of the make command, the example executables will be created under solver-specific subdirectories of builddir/examples:

8 KINSOL Installation Procedure

--with-f77underscore=ARG

This option pertains to the fcvode, fkinsol, fida, and fnvector Fortran-C interface mod- ules and is used to specify the number of underscores to append to function names so Fortran routines can properly link with the associated sundials libraries. Valid values for ARG are: none, one and two. Default: ARG=one

--with-f77case=ARG

Use this option to specify whether the external names of the fcvode, fkinsol, fida, and fn- vector Fortran-C interface functions should be lowercase or uppercase so Fortran routines can properly link with the associated sundials libraries. Valid values for ARG are: lower and upper. Default: ARG=lower

Options for MPI support

The following configuration options are only applicable to the parallel sundials packages:

--disable-mpi

Using this option will completely disable MPI support.

--with-mpicc=ARG

--with-mpif77=ARG

By default, the configuration utility script will use the MPI compiler scripts named mpicc and mpif77 to compile the parallelized sundials subroutines; however, for reasons of compatibility, different executable names may be specified via the above options. Also, ARG=no can be used to disable the use of MPI compiler scripts, thus causing the serial C and Fortran compilers to be used to compile the parallelized sundials functions and examples.

--with-mpi-root=MPIDIR

This option may be used to specify which MPI implementation should be used. The sundi- als configuration script will automatically check under the subdirectories MPIDIR/include and MPIDIR/lib for the necessary header files and libraries. The subdirectory MPIDIR/bin will also be searched for the C and Fortran MPI compiler scripts, unless the user uses --with-mpicc=no or --with-mpif77=no.

--with-mpi-incdir=INCDIR

--with-mpi-libdir=LIBDIR

--with-mpi-libs=LIBS

These options may be used if the user would prefer not to use a preexisting MPI compiler script, but instead would rather use a serial complier and provide the flags necessary to compile the MPI-aware subroutines in sundials. Often an MPI implementation will have unique library names and so it may be necessary to specify the appropriate libraries to use (e.g., LIBS=-lmpich). Default: INCDIR=MPIDIR/include and LIBDIR=MPIDIR/lib

--with-mpi-flags=ARG

Specify additional MPI-specific flags.

2.1 Configuration options 9

Options for library support

By default, only static libraries are built, but the following option may be used to build shared libraries on supported platforms.

--enable-shared

Using this particular option will result in both static and shared versions of the available sundials libraries being built if the system supports shared libraries. To build only shared libraries also specify --disable-static. Note: The fcvode, fkinsol, and fida libraries can only be built as static libraries because they contain references to externally defined symbols, namely user-supplied Fortran subroutines. Although the Fortran interfaces to the serial and parallel implementations of the supplied nvector module do not contain any unresolvable external symbols, the libraries are still built as static libraries for the purpose of consistency.

Options for Matlab support

The following options are relevant only for configuring and building the sundialsTB Matlab toolbox:

--enable-sundialsTB

The sundialsTB Matlab toolbox is not built by default. Use this option to enable configuration and compilation of the mex files. Upon completion of the make command, the following mex files will be created:

builddir/sundialsTB/cvodes/cvm/cvm.mexext builddir/sundialsTB/idas/idm/idm.mexext builddir/sundialsTB/kinsol/kim/kim.mexext

where mexext is the platform-specific extension of mex files.

--with-sundialsTB-instdir=STBINSTDIR

Alternate location for the installed sundialsTB toolbox (valid only if sundialsTB is enabled). As for the example programs, installation of sundialsTB can be completely disabled by issuing STBINSTDIR=no (in case building the toolbox is desired but its installtion will be done manu- ally afterwards). Otherwise, all required sundialsTB files will be installed under the directory STBINSTDIR/sundialsTB. Default: DIR=MATLAB/toolbox (see below for the definition of MATLAB).

--with-matlab=MATLAB

This option can be used to specify the location of the Matlab executable. The default is to search the path.

--with-mexopts=ARG

Specify the mex options file to be used. Default: Standard Matlab mex options file.

--with-mexflags=ARG

Specify the mex compiler flags to be used. Default: ARG=-O

--with-mexldadd=ARG

Specify additional mex linker flags. Default: none

2.3 Installed libraries and exported header files 11

2.3 Installed libraries and exported header files

Using the standard sundials build system, the command

% make install

will install the libraries under libdir and the public header files under includedir. The default values for these directories are instdir/lib and instdir/include, respectively, but can be changed using the configure script options --prefix, --exec-prefix, --includedir and --libdir (see §2.1). For example, a global installation of sundials on a *NIX system could be accomplished using

% configure --prefix=/opt/sundials-2.1.

Although all installed libraries reside under libdir, the public header files are further organized into subdirectories under includedir. The installed libraries and exported header files are listed for reference in Table 2.1. The file extension .lib is typically .so for shared libraries and .a for static libraries (see Options for library support for additional details). A typical user program need not explicitly include any of the shared sundials header files from under the includedir/sundials directory since they are explicitly included by the appropriate solver header files (e.g., cvode dense.h includes sundials dense.h). However, it is both legal and safe to do so (e.g., the functions declared in sundials smalldense.h could be used in building a preconditioner).

2.4 Building SUNDIALS without the configure script

If the configure script cannot be used (e.g., when building sundials under Microsoft Windows with- out using Cygwin), or if the user prefers to own the build process (e.g., when sundials is incorporated into a larger project with its own build system), then the header and source files for a given module can be copied from the srcdir to some other location and compiled separately. The following files are required to compile a sundials solver module:

  • public header files located under srcdir/include/solver
  • implementation header files and source files located under srcdir/src/solver
  • (optional) Fortran/C interface files located under srcdir/src/solver/fcmix
  • shared public header files located under srcdir/include/sundials
  • shared source files located under srcdir/src/sundials
  • (optional) nvector serial header and source files located under srcdir/include/nvector and srcdir/src/nvec ser
  • (optional) nvector parallel header and source files located under srcdir/include/nvector and srcdir/src/nvec par
  • configuration header file sundials config.h (see below)

A sample header file that, appropriately modified, can be used as sundials config.h (otherwise created automatically by the configure script) is provided below. The various preprocessor macros defined within sundials config.h have the following uses:

  • Precision of the sundials realtype type

Only one of the macros SUNDIALS SINGLE PRECISION, SUNDIALS DOUBLE PRECISION and SUNDIALS EXTENDED PRECISION should be defined to indicate if the sundials realtype type is an alias for float, double, or long double, respectively.

12 KINSOL Installation Procedure

Table 2.1: SUNDIALS libraries and header files (names are relative to libdir for libraries and to includedir for header files)

shared Libraries n/a Header files sundials/sundials types.h sundials/sundials math.h sundials/sundials config.h sundias/sundials nvector.h sundials/sunials smalldense.h sundials/sundials dense.h sundials/sundials iterative.h sundials/sundials band.h sundials/sundials spbcgs.h sundials/sundials sptfqmr.h sundials/sundials spgmr.h nvector serial Libraries libsundials nvecserial.lib libsundials fnvecserial.a Header files nvector/nvector serial.h nvector parallel Libraries libsundials nvecparallel.lib libsundials fnvecparallel.a Header files nvector/nvector parallel.h cvode Libraries libsundials cvode.lib libsundials fcvode.a Header files cvode/cvode.h cvode/cvode dense.h cvode/cvode band.h cvode/cvode diag.h cvode/cvode spils.h cvode/cvode bandpre.h cvode/cvode bbdpre.h cvode/cvode spgmr.h cvode/cvode spbcgs.h cvode/cvode sptfqmr.h cvode/cvode impl.h cvodes Libraries libsundials cvodes.lib Header files cvodes/cvodes.h cvodes/cvodes dense.h cvodes/cvodes band.h cvodes/cvodes diag.h cvodes/cvodes spils.h cvodes/cvodes bandpre.h cvodes/cvodes bbdpre.h cvodes/cvodes spgmr.h cvodes/cvodes spbcgs.h cvodes/cvodes sptfqmr.h cvodes/cvodes impl.h cvodes/cvodea impl.h ida Libraries libsundials ida.lib libsundials fida.a Header files ida/ida.h ida/ida dense.h ida/ida band.h ida/ida spils.h ida/ida spgmr.h ida/ida spbcgs.h ida/ida sptfqmr.h ida/ida bbdpre.h ida/ida impl.h kinsol Libraries libsundials kinsol.lib libsundials fkinsol.a Header files kinsol/kinsol.h kinsol/kinsol dense.h kinsol/kinsol band.h kinsol/kinsol spils.h kinsol/kinsol spgmr.h kinsol/kinsol spbcgs.h kinsol/kinsol sptfqmr.h kinsol/kinsol bbdpre.h kinsol/kinsol impl.h