Actual source code: test3.c
slepc-3.20.2 2024-03-15
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 MFN interface functions.\n\n"
12: "The command line options are:\n"
13: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n\n";
15: #include <slepcmfn.h>
17: int main(int argc,char **argv)
18: {
19: Mat A,B;
20: MFN mfn;
21: FN f;
22: MFNConvergedReason reason;
23: MFNType type;
24: PetscReal norm,tol;
25: Vec v,y;
26: PetscInt N,n=4,Istart,Iend,i,j,II,ncv,its,maxit;
27: PetscBool flg,testprefix=PETSC_FALSE;
28: const char *prefix;
29: PetscViewerAndFormat *vf;
31: PetscFunctionBeginUser;
32: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
33: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
34: N = n*n;
35: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSquare root of Laplacian y=sqrt(A)*e_1, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,n));
36: PetscCall(PetscOptionsGetBool(NULL,NULL,"-test_prefix",&testprefix,NULL));
38: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39: Compute the discrete 2-D Laplacian, A
40: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
42: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
43: PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
44: PetscCall(MatSetFromOptions(A));
45: PetscCall(MatSetUp(A));
47: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
48: for (II=Istart;II<Iend;II++) {
49: i = II/n; j = II-i*n;
50: if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
51: if (i<n-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
52: if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
53: if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
54: PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
55: }
57: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
58: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
59: PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));
61: PetscCall(MatCreateVecs(A,NULL,&v));
62: PetscCall(VecSetValue(v,0,1.0,INSERT_VALUES));
63: PetscCall(VecAssemblyBegin(v));
64: PetscCall(VecAssemblyEnd(v));
65: PetscCall(VecDuplicate(v,&y));
67: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68: Create the solver, set the matrix and the function
69: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
70: PetscCall(MFNCreate(PETSC_COMM_WORLD,&mfn));
71: PetscCall(MFNSetOperator(mfn,A));
72: PetscCall(MFNGetFN(mfn,&f));
73: PetscCall(FNSetType(f,FNSQRT));
75: PetscCall(MFNSetType(mfn,MFNKRYLOV));
76: PetscCall(MFNGetType(mfn,&type));
77: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type));
79: /* test prefix usage */
80: if (testprefix) {
81: PetscCall(MFNSetOptionsPrefix(mfn,"check_"));
82: PetscCall(MFNAppendOptionsPrefix(mfn,"myprefix_"));
83: PetscCall(MFNGetOptionsPrefix(mfn,&prefix));
84: PetscCall(PetscPrintf(PETSC_COMM_WORLD," MFN prefix is currently: %s\n",prefix));
85: }
87: /* test some interface functions */
88: PetscCall(MFNGetOperator(mfn,&B));
89: PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD));
90: PetscCall(MFNSetTolerances(mfn,1e-4,500));
91: PetscCall(MFNSetDimensions(mfn,6));
92: PetscCall(MFNSetErrorIfNotConverged(mfn,PETSC_TRUE));
93: /* test monitors */
94: PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
95: PetscCall(MFNMonitorSet(mfn,(PetscErrorCode (*)(MFN,PetscInt,PetscReal,void*))MFNMonitorDefault,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy));
96: /* PetscCall(MFNMonitorCancel(mfn)); */
97: PetscCall(MFNSetFromOptions(mfn));
99: /* query properties and print them */
100: PetscCall(MFNGetTolerances(mfn,&tol,&maxit));
101: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Tolerance: %g, max iterations: %" PetscInt_FMT "\n",(double)tol,maxit));
102: PetscCall(MFNGetDimensions(mfn,&ncv));
103: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Subspace dimension: %" PetscInt_FMT "\n",ncv));
104: PetscCall(MFNGetErrorIfNotConverged(mfn,&flg));
105: if (flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Erroring out if convergence fails\n"));
107: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108: Solve y=sqrt(A)*v
109: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111: PetscCall(MFNSolve(mfn,v,y));
112: PetscCall(MFNGetConvergedReason(mfn,&reason));
113: PetscCall(MFNGetIterationNumber(mfn,&its));
114: PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));
115: /* PetscCall(PetscPrintf(PETSC_COMM_WORLD," its = %" PetscInt_FMT "\n",its)); */
116: PetscCall(VecNorm(y,NORM_2,&norm));
117: PetscCall(PetscPrintf(PETSC_COMM_WORLD," sqrt(A)*v has norm %g\n",(double)norm));
119: /*
120: Free work space
121: */
122: PetscCall(MFNDestroy(&mfn));
123: PetscCall(MatDestroy(&A));
124: PetscCall(VecDestroy(&v));
125: PetscCall(VecDestroy(&y));
126: PetscCall(SlepcFinalize());
127: return 0;
128: }
130: /*TEST
132: test:
133: suffix: 1
134: args: -mfn_monitor_cancel -mfn_converged_reason -mfn_view -log_exclude mfn,bv,fn -mfn_monitor draw::draw_lg -draw_virtual
135: requires: x
137: test:
138: suffix: 2
139: args: -test_prefix -check_myprefix_mfn_monitor
140: filter: sed -e "s/estimate [0-9]\.[0-9]*e[+-]\([0-9]*\)/estimate (removed)/g" | sed -e "s/4.0[0-9]*e-10/4.03e-10/"
142: TEST*/