Date: 2016-09-29T11:12:00+00:00

Actual source code: ex29.c
slepc-3.7.3 2016-09-29
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.

      8:    SLEPc is free software: you can redistribute it and/or modify it under  the
      9:    terms of version 3 of the GNU Lesser General Public License as published by
     10:    the Free Software Foundation.

     12:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
     13:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
     14:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
     15:    more details.

     17:    You  should have received a copy of the GNU Lesser General  Public  License
     18:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
     19:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     20: */

     22: static char help[] = "Solves the same problem as in ex5, with a user-defined stopping test."
     23:   "It is a standard nonsymmetric eigenproblem with real eigenvalues and the rightmost eigenvalue is known to be 1.\n"
     24:   "This example illustrates how the user can set a custom stopping test function.\n\n"
     25:   "The command line options are:\n"
     26:   "  -m <m>, where <m> = number of grid subdivisions in each dimension.\n"
     27:   "  -seconds <s>, where <s> = maximum time in seconds allowed for computation.\n\n";

     29: #include <slepceps.h>
     30: #include <petsctime.h>

     32: /*
     33:    User-defined routines
     34: */

     36: PetscErrorCode MyStoppingTest(EPS,PetscInt,PetscInt,PetscInt,PetscInt,EPSConvergedReason*,void*);
     37: PetscErrorCode MatMarkovModel(PetscInt,Mat);

     41: int main(int argc,char **argv)
     42: {
     43:   Mat                A;               /* operator matrix */
     44:   EPS                eps;             /* eigenproblem solver context */
     45:   PetscReal          seconds=2.5;     /* maximum time allowed for computation */
     46:   PetscLogDouble     deadline;        /* time to abort computation */
     47:   PetscInt           N,m=15,nconv;
     48:   PetscBool          terse;
     49:   PetscViewer        viewer;
     50:   EPSConvergedReason reason;
     51:   PetscErrorCode     ierr;

     53:   SlepcInitialize(&argc,&argv,(char*)0,help);

     55:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
     56:   N = m*(m+1)/2;
     57:   PetscPrintf(PETSC_COMM_WORLD,"\nMarkov Model, N=%D (m=%D)\n",N,m);
     58:   PetscOptionsGetReal(NULL,NULL,"-seconds",&seconds,NULL);
     59:   PetscPrintf(PETSC_COMM_WORLD,"Maximum time for computation is set to %g seconds.\n\n",(double)seconds);
     60:   deadline = seconds;
     61:   PetscTimeAdd(&deadline);

     63:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     64:      Compute the operator matrix that defines the eigensystem, Ax=kx
     65:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     67:   MatCreate(PETSC_COMM_WORLD,&A);
     68:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
     69:   MatSetFromOptions(A);
     70:   MatSetUp(A);
     71:   MatMarkovModel(m,A);

     73:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     74:                 Create the eigensolver and set various options
     75:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     77:   EPSCreate(PETSC_COMM_WORLD,&eps);
     78:   EPSSetOperators(eps,A,NULL);
     79:   EPSSetProblemType(eps,EPS_NHEP);
     80:   EPSSetStoppingTestFunction(eps,MyStoppingTest,&deadline,NULL);
     81:   EPSSetFromOptions(eps);

     83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     84:                       Solve the eigensystem
     85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     87:   EPSSolve(eps);

     89:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     90:                     Display solution and clean up
     91:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     93:   /* show detailed info unless -terse option is given by user */
     94:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
     95:   if (terse) {
     96:     EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
     97:   } else {
     98:     PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     99:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
    100:     EPSGetConvergedReason(eps,&reason);
    101:     if (reason!=EPS_CONVERGED_USER) {
    102:       EPSReasonView(eps,viewer);
    103:       EPSErrorView(eps,EPS_ERROR_RELATIVE,viewer);
    104:     } else {
    105:       EPSGetConverged(eps,&nconv);
    106:       PetscViewerASCIIPrintf(viewer,"Eigensolve finished with %D converged eigenpairs; reason=%s\n",nconv,EPSConvergedReasons[reason]);
    107:     }
    108:     PetscViewerPopFormat(viewer);
    109:   }
    110:   EPSDestroy(&eps);
    111:   MatDestroy(&A);
    112:   SlepcFinalize();
    113:   return ierr;
    114: }

    118: /*
    119:     Matrix generator for a Markov model of a random walk on a triangular grid.

    121:     This subroutine generates a test matrix that models a random walk on a
    122:     triangular grid. This test example was used by G. W. Stewart ["{SRRIT} - a
    123:     FORTRAN subroutine to calculate the dominant invariant subspaces of a real
    124:     matrix", Tech. report. TR-514, University of Maryland (1978).] and in a few
    125:     papers on eigenvalue problems by Y. Saad [see e.g. LAA, vol. 34, pp. 269-295
    126:     (1980) ]. These matrices provide reasonably easy test problems for eigenvalue
    127:     algorithms. The transpose of the matrix  is stochastic and so it is known
    128:     that one is an exact eigenvalue. One seeks the eigenvector of the transpose
    129:     associated with the eigenvalue unity. The problem is to calculate the steady
    130:     state probability distribution of the system, which is the eigevector
    131:     associated with the eigenvalue one and scaled in such a way that the sum all
    132:     the components is equal to one.

    134:     Note: the code will actually compute the transpose of the stochastic matrix
    135:     that contains the transition probabilities.
    136: */
    137: PetscErrorCode MatMarkovModel(PetscInt m,Mat A)
    138: {
    139:   const PetscReal cst = 0.5/(PetscReal)(m-1);
    140:   PetscReal       pd,pu;
    141:   PetscInt        Istart,Iend,i,j,jmax,ix=0;
    142:   PetscErrorCode  ierr;

    145:   MatGetOwnershipRange(A,&Istart,&Iend);
    146:   for (i=1;i<=m;i++) {
    147:     jmax = m-i+1;
    148:     for (j=1;j<=jmax;j++) {
    149:       ix = ix + 1;
    150:       if (ix-1<Istart || ix>Iend) continue;  /* compute only owned rows */
    151:       if (j!=jmax) {
    152:         pd = cst*(PetscReal)(i+j-1);
    153:         /* north */
    154:         if (i==1) {
    155:           MatSetValue(A,ix-1,ix,2*pd,INSERT_VALUES);
    156:         } else {
    157:           MatSetValue(A,ix-1,ix,pd,INSERT_VALUES);
    158:         }
    159:         /* east */
    160:         if (j==1) {
    161:           MatSetValue(A,ix-1,ix+jmax-1,2*pd,INSERT_VALUES);
    162:         } else {
    163:           MatSetValue(A,ix-1,ix+jmax-1,pd,INSERT_VALUES);
    164:         }
    165:       }
    166:       /* south */
    167:       pu = 0.5 - cst*(PetscReal)(i+j-3);
    168:       if (j>1) {
    169:         MatSetValue(A,ix-1,ix-2,pu,INSERT_VALUES);
    170:       }
    171:       /* west */
    172:       if (i>1) {
    173:         MatSetValue(A,ix-1,ix-jmax-2,pu,INSERT_VALUES);
    174:       }
    175:     }
    176:   }
    177:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
    178:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
    179:   return(0);
    180: }

    184: /*
    185:     Function for user-defined stopping test.

    187:     Checks that the computing time has not exceeded the deadline.
    188: */
    189: PetscErrorCode MyStoppingTest(EPS eps,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,EPSConvergedReason *reason,void *ctx)
    190: {
    192:   PetscLogDouble now,deadline = *(PetscLogDouble*)ctx;

    195:   /* check if usual termination conditions are met */
    196:   EPSStoppingBasic(eps,its,max_it,nconv,nev,reason,NULL);
    197:   if (*reason==EPS_CONVERGED_ITERATING) {
    198:     /* check if deadline has expired */
    199:     PetscTime(&now);
    200:     if (now>deadline) *reason = EPS_CONVERGED_USER;
    201:   }
    202:   return(0);
    203: }