Date: 2024-09-25T09:34:05+00:00

Actual source code: test12.c
slepc-3.21.2 2024-09-25
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, 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: */

     11: static char help[] = "Illustrates region filtering in PEP (based on spring).\n"
     12:   "The command line options are:\n"
     13:   "  -n <n> ... number of grid subdivisions.\n"
     14:   "  -mu <value> ... mass (default 1).\n"
     15:   "  -tau <value> ... damping constant of the dampers (default 10).\n"
     16:   "  -kappa <value> ... damping constant of the springs (default 5).\n\n";

     18: #include <slepcpep.h>

     20: int main(int argc,char **argv)
     21: {
     22:   Mat            M,C,K,A[3];      /* problem matrices */
     23:   PEP            pep;             /* polynomial eigenproblem solver context */
     24:   RG             rg;
     25:   PetscInt       n=30,Istart,Iend,i;
     26:   PetscReal      mu=1.0,tau=10.0,kappa=5.0;

     28:   PetscFunctionBeginUser;
     29:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

     31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     32:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&mu,NULL));
     33:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
     34:   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
     35:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDamped mass-spring system, n=%" PetscInt_FMT " mu=%g tau=%g kappa=%g\n\n",n,(double)mu,(double)tau,(double)kappa));

     37:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     38:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
     39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     41:   /* K is a tridiagonal */
     42:   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
     43:   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
     44:   PetscCall(MatSetFromOptions(K));

     46:   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
     47:   for (i=Istart;i<Iend;i++) {
     48:     if (i>0) PetscCall(MatSetValue(K,i,i-1,-kappa,INSERT_VALUES));
     49:     PetscCall(MatSetValue(K,i,i,kappa*3.0,INSERT_VALUES));
     50:     if (i<n-1) PetscCall(MatSetValue(K,i,i+1,-kappa,INSERT_VALUES));
     51:   }

     53:   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
     54:   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));

     56:   /* C is a tridiagonal */
     57:   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
     58:   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
     59:   PetscCall(MatSetFromOptions(C));

     61:   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
     62:   for (i=Istart;i<Iend;i++) {
     63:     if (i>0) PetscCall(MatSetValue(C,i,i-1,-tau,INSERT_VALUES));
     64:     PetscCall(MatSetValue(C,i,i,tau*3.0,INSERT_VALUES));
     65:     if (i<n-1) PetscCall(MatSetValue(C,i,i+1,-tau,INSERT_VALUES));
     66:   }

     68:   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
     69:   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));

     71:   /* M is a diagonal matrix */
     72:   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
     73:   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
     74:   PetscCall(MatSetFromOptions(M));
     75:   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
     76:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(M,i,i,mu,INSERT_VALUES));
     77:   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
     78:   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));

     80:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     81:                     Create a region for filtering
     82:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     84:   PetscCall(RGCreate(PETSC_COMM_WORLD,&rg));
     85:   PetscCall(RGSetType(rg,RGINTERVAL));
     86: #if defined(PETSC_USE_COMPLEX)
     87:   PetscCall(RGIntervalSetEndpoints(rg,-47.0,-35.0,-0.001,0.001));
     88: #else
     89:   PetscCall(RGIntervalSetEndpoints(rg,-47.0,-35.0,-0.0,0.0));
     90: #endif

     92:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     93:                 Create the eigensolver and solve the problem
     94:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     96:   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
     97:   PetscCall(PEPSetRG(pep,rg));
     98:   A[0] = K; A[1] = C; A[2] = M;
     99:   PetscCall(PEPSetOperators(pep,3,A));
    100:   PetscCall(PEPSetTolerances(pep,PETSC_SMALL,PETSC_DEFAULT));
    101:   PetscCall(PEPSetFromOptions(pep));
    102:   PetscCall(PEPSolve(pep));

    104:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    105:                     Display solution and clean up
    106:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    108:   PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
    109:   PetscCall(PEPDestroy(&pep));
    110:   PetscCall(MatDestroy(&M));
    111:   PetscCall(MatDestroy(&C));
    112:   PetscCall(MatDestroy(&K));
    113:   PetscCall(RGDestroy(&rg));
    114:   PetscCall(SlepcFinalize());
    115:   return 0;
    116: }

    118: /*TEST

    120:    test:
    121:       args: -pep_nev 8 -pep_type {{toar linear qarnoldi}}
    122:       suffix: 1
    123:       requires: !single
    124:       output_file: output/test12_1.out

    126: TEST*/