Actual source code: test1.c
slepc-3.20.1 2023-11-27
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[] = "Computes exp(t*A)*v for a matrix loaded from file.\n\n"
12: "The command line options are:\n"
13: " -t <sval>, where <sval> = scalar value that multiplies the argument.\n"
14: " -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";
16: #include <slepcmfn.h>
18: int main(int argc,char **argv)
19: {
20: Mat A; /* problem matrix */
21: MFN mfn;
22: FN f;
23: PetscReal norm;
24: PetscScalar t=2.0;
25: Vec v,y;
26: PetscViewer viewer;
27: PetscBool flg;
28: char filename[PETSC_MAX_PATH_LEN];
29: MFNConvergedReason reason;
31: PetscFunctionBeginUser;
32: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
34: PetscCall(PetscOptionsGetScalar(NULL,NULL,"-t",&t,NULL));
35: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nMatrix exponential y=exp(t*A)*e, loaded from file\n\n"));
37: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38: Load matrix A from binary file
39: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
41: PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
42: PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");
44: #if defined(PETSC_USE_COMPLEX)
45: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n"));
46: #else
47: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n"));
48: #endif
49: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
50: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
51: PetscCall(MatSetFromOptions(A));
52: PetscCall(MatLoad(A,viewer));
53: PetscCall(PetscViewerDestroy(&viewer));
55: /* set v = ones(n,1) */
56: PetscCall(MatCreateVecs(A,NULL,&y));
57: PetscCall(MatCreateVecs(A,NULL,&v));
58: PetscCall(VecSet(v,1.0));
60: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61: Create the solver and set various options
62: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64: PetscCall(MFNCreate(PETSC_COMM_WORLD,&mfn));
65: PetscCall(MFNSetOperator(mfn,A));
66: PetscCall(MFNGetFN(mfn,&f));
67: PetscCall(FNSetType(f,FNEXP));
68: PetscCall(FNSetScale(f,t,1.0));
69: PetscCall(MFNSetFromOptions(mfn));
71: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72: Solve the problem, y=exp(t*A)*v
73: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
75: PetscCall(MFNSolve(mfn,v,y));
76: PetscCall(MFNGetConvergedReason(mfn,&reason));
77: PetscCheck(reason>=0,PETSC_COMM_WORLD,PETSC_ERR_CONV_FAILED,"Solver did not converge");
78: PetscCall(VecNorm(y,NORM_2,&norm));
79: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Computed vector at time t=%.4g has norm %g\n",(double)PetscRealPart(t),(double)norm));
81: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82: Solve the problem, y=exp(t*A^T)*v
83: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85: PetscCall(MFNSolveTranspose(mfn,v,y));
86: PetscCall(MFNGetConvergedReason(mfn,&reason));
87: PetscCheck(reason>=0,PETSC_COMM_WORLD,PETSC_ERR_CONV_FAILED,"Solver did not converge");
88: PetscCall(VecNorm(y,NORM_2,&norm));
89: PetscCall(PetscPrintf(PETSC_COMM_WORLD," With transpose: computed vector has norm %g\n\n",(double)norm));
91: /*
92: Free work space
93: */
94: PetscCall(MFNDestroy(&mfn));
95: PetscCall(MatDestroy(&A));
96: PetscCall(VecDestroy(&v));
97: PetscCall(VecDestroy(&y));
98: PetscCall(SlepcFinalize());
99: return 0;
100: }
102: /*TEST
104: testset:
105: args: -file ${DATAFILESPATH}/matrices/real/bfw782a.petsc -mfn_type {{krylov expokit}} -t 0.05
106: requires: double !complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
107: output_file: output/test1_1.out
108: test:
109: suffix: 1
110: test:
111: suffix: 1_cuda
112: args: -mat_type aijcusparse
113: requires: cuda
115: testset:
116: args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -mfn_type {{krylov expokit}}
117: requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
118: output_file: output/test1_2.out
119: test:
120: suffix: 2
121: test:
122: suffix: 2_cuda
123: args: -mat_type aijcusparse
124: requires: cuda
126: TEST*/