Actual source code: test13.c
slepc-3.17.1 2022-04-11
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[] = "Test the NEPProjectOperator() operator.\n\n"
12: "This is based on ex22.\n"
13: "The command line options are:\n"
14: " -n <n>, where <n> = number of grid subdivisions.\n"
15: " -tau <tau>, where <tau> is the delay parameter.\n";
17: /*
18: Solve parabolic partial differential equation with time delay tau
20: u_t = u_xx + a*u(t) + b*u(t-tau)
21: u(0,t) = u(pi,t) = 0
23: with a = 20 and b(x) = -4.1+x*(1-exp(x-pi)).
25: Discretization leads to a DDE of dimension n
27: -u' = A*u(t) + B*u(t-tau)
29: which results in the nonlinear eigenproblem
31: (-lambda*I + A + exp(-tau*lambda)*B)*u = 0
32: */
34: #include <slepcnep.h>
36: int main(int argc,char **argv)
37: {
38: NEP nep;
39: Mat Id,A,B,mats[3];
40: FN f1,f2,f3,funs[3];
41: BV V;
42: DS ds;
43: Vec v;
44: PetscScalar coeffs[2],b,*M;
45: PetscInt n=32,Istart,Iend,i,j,k,nc;
46: PetscReal tau=0.001,h,a=20,xi;
48: SlepcInitialize(&argc,&argv,(char*)0,help);
49: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
50: PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL);
51: PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g\n",n,(double)tau);
52: h = PETSC_PI/(PetscReal)(n+1);
54: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55: Create nonlinear eigensolver context
56: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
58: NEPCreate(PETSC_COMM_WORLD,&nep);
60: /* Identity matrix */
61: MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&Id);
62: MatSetOption(Id,MAT_HERMITIAN,PETSC_TRUE);
64: /* A = 1/h^2*tridiag(1,-2,1) + a*I */
65: MatCreate(PETSC_COMM_WORLD,&A);
66: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
67: MatSetFromOptions(A);
68: MatSetUp(A);
69: MatGetOwnershipRange(A,&Istart,&Iend);
70: for (i=Istart;i<Iend;i++) {
71: if (i>0) MatSetValue(A,i,i-1,1.0/(h*h),INSERT_VALUES);
72: if (i<n-1) MatSetValue(A,i,i+1,1.0/(h*h),INSERT_VALUES);
73: MatSetValue(A,i,i,-2.0/(h*h)+a,INSERT_VALUES);
74: }
75: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
76: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
77: MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);
79: /* B = diag(b(xi)) */
80: MatCreate(PETSC_COMM_WORLD,&B);
81: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
82: MatSetFromOptions(B);
83: MatSetUp(B);
84: MatGetOwnershipRange(B,&Istart,&Iend);
85: for (i=Istart;i<Iend;i++) {
86: xi = (i+1)*h;
87: b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
88: MatSetValues(B,1,&i,1,&i,&b,INSERT_VALUES);
89: }
90: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
91: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
92: MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);
94: /* Functions: f1=-lambda, f2=1.0, f3=exp(-tau*lambda) */
95: FNCreate(PETSC_COMM_WORLD,&f1);
96: FNSetType(f1,FNRATIONAL);
97: coeffs[0] = -1.0; coeffs[1] = 0.0;
98: FNRationalSetNumerator(f1,2,coeffs);
100: FNCreate(PETSC_COMM_WORLD,&f2);
101: FNSetType(f2,FNRATIONAL);
102: coeffs[0] = 1.0;
103: FNRationalSetNumerator(f2,1,coeffs);
105: FNCreate(PETSC_COMM_WORLD,&f3);
106: FNSetType(f3,FNEXP);
107: FNSetScale(f3,-tau,1.0);
109: /* Set the split operator */
110: mats[0] = A; funs[0] = f2;
111: mats[1] = Id; funs[1] = f1;
112: mats[2] = B; funs[2] = f3;
113: NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN);
114: NEPSetType(nep,NEPNARNOLDI);
115: NEPSetFromOptions(nep);
117: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118: Project the NEP
119: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
121: NEPSetUp(nep);
122: NEPGetBV(nep,&V);
123: BVGetSizes(V,NULL,NULL,&nc);
124: for (i=0;i<nc;i++) {
125: BVGetColumn(V,i,&v);
126: VecSetValue(v,i,1.0,INSERT_VALUES);
127: VecAssemblyBegin(v);
128: VecAssemblyEnd(v);
129: BVRestoreColumn(V,i,&v);
130: }
131: NEPGetDS(nep,&ds);
132: DSSetType(ds,DSNEP);
133: DSNEPSetFN(ds,3,funs);
134: DSAllocate(ds,nc);
135: DSSetDimensions(ds,nc,0,0);
136: NEPProjectOperator(nep,0,nc);
138: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139: Display projected matrices and clean up
140: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
142: for (k=0;k<3;k++) {
143: DSGetArray(ds,DSMatExtra[k],&M);
144: PetscPrintf(PETSC_COMM_WORLD,"\nMatrix E%" PetscInt_FMT " = \n",k);
145: for (i=0;i<nc;i++) {
146: for (j=0;j<nc;j++) PetscPrintf(PETSC_COMM_WORLD," %.5g",(double)PetscRealPart(M[i+j*nc]));
147: PetscPrintf(PETSC_COMM_WORLD,"\n");
148: }
149: DSRestoreArray(ds,DSMatExtra[k],&M);
150: }
152: NEPDestroy(&nep);
153: MatDestroy(&Id);
154: MatDestroy(&A);
155: MatDestroy(&B);
156: FNDestroy(&f1);
157: FNDestroy(&f2);
158: FNDestroy(&f3);
159: SlepcFinalize();
160: return 0;
161: }
163: /*TEST
165: test:
166: suffix: 1
167: args: -nep_ncv 5
169: TEST*/