Date: 2013-09-02T10:09:47+00:00

Actual source code: test9.c

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2013, 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[] = "Test DSGHEP.\n\n";

     24: #include <slepcds.h>
     25: #include <slepc-private/dsimpl.h>    /* for the definition of SlepcCompare* */

     29: int main(int argc,char **argv)
     30: {
     32:   DS             ds;
     33:   PetscReal      re;
     34:   PetscScalar    *A,*B,*eig;
     35:   PetscInt       i,j,n=10,ld;
     36:   PetscViewer    viewer;
     37:   PetscBool      verbose;

     39:   SlepcInitialize(&argc,&argv,(char*)0,help);
     40:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     41:   PetscPrintf(PETSC_COMM_WORLD,"Solve a System of type GHEP - dimension %D.\n",n);
     42:   PetscOptionsHasName(NULL,"-verbose",&verbose);

     44:   /* Create DS object */
     45:   DSCreate(PETSC_COMM_WORLD,&ds);
     46:   DSSetType(ds,DSGHEP);
     47:   DSSetFromOptions(ds);
     48:   ld = n+2;  /* test leading dimension larger than n */
     49:   DSAllocate(ds,ld);
     50:   DSSetDimensions(ds,n,0,0,0);

     52:   /* Set up viewer */
     53:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     54:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
     55:   DSView(ds,viewer);
     56:   PetscViewerPopFormat(viewer);
     57:   if (verbose) {
     58:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
     59:   }

     61:   /* Fill with a symmetric Toeplitz matrix */
     62:   DSGetArray(ds,DS_MAT_A,&A);
     63:   DSGetArray(ds,DS_MAT_B,&B);
     64:   for (i=0;i<n;i++) A[i+i*ld]=2.0;
     65:   for (j=1;j<3;j++) {
     66:     for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
     67:   }
     68:   for (j=1;j<3;j++) { A[0+j*ld]=-1.0*(j+2); A[j+0*ld]=-1.0*(j+2); }
     69:   /* Diagonal matrix */
     70:   for (i=0;i<n;i++) B[i+i*ld]=0.1*(i+1);
     71:   DSRestoreArray(ds,DS_MAT_A,&A);
     72:   DSRestoreArray(ds,DS_MAT_B,&B);
     73:   DSSetState(ds,DS_STATE_RAW);
     74:   if (verbose) {
     75:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
     76:     DSView(ds,viewer);
     77:   }

     79:   /* Solve */
     80:   PetscMalloc(n*sizeof(PetscScalar),&eig);
     81:   DSSetEigenvalueComparison(ds,SlepcCompareLargestMagnitude,NULL);
     82:   DSSolve(ds,eig,NULL);
     83:   DSSort(ds,eig,NULL,NULL,NULL,NULL);
     84:   if (verbose) {
     85:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
     86:     DSView(ds,viewer);
     87:   }

     89:   /* Print eigenvalues */
     90:   PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n",n);
     91:   for (i=0;i<n;i++) {
     92:     re = PetscRealPart(eig[i]);
     93:     PetscViewerASCIIPrintf(viewer,"  %.5F\n",re);
     94:   }
     95:   PetscFree(eig);
     96:   DSDestroy(&ds);
     97:   SlepcFinalize();
     98:   return 0;
     99: }