Actual source code: pdde_stability.c

slepc-3.16.1 2021-11-17
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */
 10: /*
 11:    This example implements one of the problems found at
 12:        NLEVP: A Collection of Nonlinear Eigenvalue Problems,
 13:        The University of Manchester.
 14:    The details of the collection can be found at:
 15:        [1] T. Betcke et al., "NLEVP: A Collection of Nonlinear Eigenvalue
 16:            Problems", ACM Trans. Math. Software 39(2), Article 7, 2013.

 18:    The pdde_stability problem is a complex-symmetric QEP from the stability
 19:    analysis of a discretized partial delay-differential equation. It requires
 20:    complex scalars.
 21: */

 23: static char help[] = "Stability analysis of a discretized partial delay-differential equation.\n\n"
 24:   "The command line options are:\n"
 25:   "  -m <m>, grid size, the matrices have dimension n=m*m.\n"
 26:   "  -c <a0,b0,a1,b1,a2,b2,phi1>, comma-separated list of 7 real parameters.\n\n";

 28: #include <slepcpep.h>

 30: #define NMAT 3

 32: /*
 33:     Function for user-defined eigenvalue ordering criterion.

 35:     Given two eigenvalues ar+i*ai and br+i*bi, the subroutine must choose
 36:     one of them as the preferred one according to the criterion.
 37:     In this example, the preferred value is the one with absolute value closest to 1.
 38: */
 39: PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
 40: {
 41:   PetscReal aa,ab;

 44:   aa = PetscAbsReal(SlepcAbsEigenvalue(ar,ai)-PetscRealConstant(1.0));
 45:   ab = PetscAbsReal(SlepcAbsEigenvalue(br,bi)-PetscRealConstant(1.0));
 46:   *r = aa > ab ? 1 : (aa < ab ? -1 : 0);
 47:   return(0);
 48: }

 50: int main(int argc,char **argv)
 51: {
 52:   Mat            A[NMAT];         /* problem matrices */
 53:   PEP            pep;             /* polynomial eigenproblem solver context */
 54:   PetscInt       m=15,n,II,Istart,Iend,i,j,k;
 55:   PetscReal      h,xi,xj,c[7] = { 2, .3, -2, .2, -2, -.3, -PETSC_PI/2 };
 56:   PetscScalar    alpha,beta,gamma;
 57:   PetscBool      flg,terse;

 60:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 61: #if !defined(PETSC_USE_COMPLEX)
 62:   SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This example requires complex scalars");
 63: #endif

 65:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 66:   n = m*m;
 67:   h = PETSC_PI/(m+1);
 68:   gamma = PetscExpScalar(PETSC_i*c[6]);
 69:   gamma = gamma/PetscAbsScalar(gamma);
 70:   k = 7;
 71:   PetscOptionsGetRealArray(NULL,NULL,"-c",c,&k,&flg);
 72:   if (flg && k!=7) SETERRQ1(PETSC_COMM_WORLD,1,"The number of parameters -c should be 7, you provided %D",k);
 73:   PetscPrintf(PETSC_COMM_WORLD,"\nPDDE stability, n=%D (m=%D)\n\n",n,m);

 75:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 76:                      Compute the polynomial matrices
 77:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 79:   /* initialize matrices */
 80:   for (i=0;i<NMAT;i++) {
 81:     MatCreate(PETSC_COMM_WORLD,&A[i]);
 82:     MatSetSizes(A[i],PETSC_DECIDE,PETSC_DECIDE,n,n);
 83:     MatSetFromOptions(A[i]);
 84:     MatSetUp(A[i]);
 85:   }
 86:   MatGetOwnershipRange(A[0],&Istart,&Iend);

 88:   /* A[1] has a pattern similar to the 2D Laplacian */
 89:   for (II=Istart;II<Iend;II++) {
 90:     i = II/m; j = II-i*m;
 91:     xi = (i+1)*h; xj = (j+1)*h;
 92:     alpha = c[0]+c[1]*PetscSinReal(xi)+gamma*(c[2]+c[3]*xi*(1.0-PetscExpReal(xi-PETSC_PI)));
 93:     beta = c[0]+c[1]*PetscSinReal(xj)-gamma*(c[2]+c[3]*xj*(1.0-PetscExpReal(xj-PETSC_PI)));
 94:     MatSetValue(A[1],II,II,alpha+beta-4.0/(h*h),INSERT_VALUES);
 95:     if (j>0) { MatSetValue(A[1],II,II-1,1.0/(h*h),INSERT_VALUES); }
 96:     if (j<m-1) { MatSetValue(A[1],II,II+1,1.0/(h*h),INSERT_VALUES); }
 97:     if (i>0) { MatSetValue(A[1],II,II-m,1.0/(h*h),INSERT_VALUES); }
 98:     if (i<m-1) { MatSetValue(A[1],II,II+m,1.0/(h*h),INSERT_VALUES); }
 99:   }

101:   /* A[0] and A[2] are diagonal */
102:   for (II=Istart;II<Iend;II++) {
103:     i = II/m; j = II-i*m;
104:     xi = (i+1)*h; xj = (j+1)*h;
105:     alpha = c[4]+c[5]*xi*(PETSC_PI-xi);
106:     beta = c[4]+c[5]*xj*(PETSC_PI-xj);
107:     MatSetValue(A[0],II,II,alpha,INSERT_VALUES);
108:     MatSetValue(A[2],II,II,beta,INSERT_VALUES);
109:   }

111:   /* assemble matrices */
112:   for (i=0;i<NMAT;i++) {
113:     MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY);
114:   }
115:   for (i=0;i<NMAT;i++) {
116:     MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY);
117:   }

119:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120:                 Create the eigensolver and solve the problem
121:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

123:   PEPCreate(PETSC_COMM_WORLD,&pep);
124:   PEPSetOperators(pep,NMAT,A);
125:   PEPSetEigenvalueComparison(pep,MyEigenSort,NULL);
126:   PEPSetDimensions(pep,4,PETSC_DEFAULT,PETSC_DEFAULT);
127:   PEPSetFromOptions(pep);
128:   PEPSolve(pep);

130:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131:                     Display solution and clean up
132:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

134:   /* show detailed info unless -terse option is given by user */
135:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
136:   if (terse) {
137:     PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
138:   } else {
139:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
140:     PEPConvergedReasonView(pep,PETSC_VIEWER_STDOUT_WORLD);
141:     PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD);
142:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
143:   }
144:   PEPDestroy(&pep);
145:   for (i=0;i<NMAT;i++) {
146:     MatDestroy(&A[i]);
147:   }
148:   SlepcFinalize();
149:   return ierr;
150: }

152: /*TEST

154:    build:
155:       requires: complex

157:    test:
158:       suffix: 1
159:       args: -pep_type {{toar qarnoldi linear}} -pep_ncv 25 -terse
160:       requires: complex !single

162: TEST*/