Actual source code: ex58.c
2: static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";
4: /*T
5: Concepts: KSP^solving a system of linear equations
6: Processors: 1
7: T*/
9: /*
10: Modified from ex1.c for testing matrix operations when matrix structure is changed.
11: Contributed by Jose E. Roman, Feb. 2012.
12: */
13: #include <petscksp.h>
15: int main(int argc,char **args)
16: {
17: Vec x, b, u; /* approx solution, RHS, exact solution */
18: Mat A,B,C; /* linear system matrix */
19: KSP ksp; /* linear solver context */
20: PC pc; /* preconditioner context */
21: PetscReal norm; /* norm of solution error */
22: PetscInt i,n = 20,col[3],its;
23: PetscMPIInt size;
24: PetscScalar one = 1.0,value[3];
25: PetscBool nonzeroguess = PETSC_FALSE;
27: PetscInitialize(&argc,&args,(char*)0,help);
28: MPI_Comm_size(PETSC_COMM_WORLD,&size);
30: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
31: PetscOptionsGetBool(NULL,NULL,"-nonzero_guess",&nonzeroguess,NULL);
33: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: Compute the matrix and right-hand-side vector that define
35: the linear system, Ax = b.
36: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38: /*
39: Create vectors. Note that we form 1 vector from scratch and
40: then duplicate as needed.
41: */
42: VecCreate(PETSC_COMM_WORLD,&x);
43: PetscObjectSetName((PetscObject) x, "Solution");
44: VecSetSizes(x,PETSC_DECIDE,n);
45: VecSetFromOptions(x);
46: VecDuplicate(x,&b);
47: VecDuplicate(x,&u);
49: /*
50: Create matrix. When using MatCreate(), the matrix format can
51: be specified at runtime.
53: Performance tuning note: For problems of substantial size,
54: preallocation of matrix memory is crucial for attaining good
55: performance. See the matrix chapter of the users manual for details.
56: */
57: MatCreate(PETSC_COMM_WORLD,&A);
58: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
59: MatSetFromOptions(A);
60: MatSetUp(A);
62: /*
63: Assemble matrix
64: */
65: value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
66: for (i=1; i<n-1; i++) {
67: col[0] = i-1; col[1] = i; col[2] = i+1;
68: MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
69: }
70: i = n - 1; col[0] = n - 2; col[1] = n - 1;
71: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
72: i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
73: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
74: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
75: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
77: /*
78: jroman: added matrices
79: */
80: MatCreate(PETSC_COMM_WORLD,&B);
81: MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);
82: MatSetFromOptions(B);
83: MatSetUp(B);
84: for (i=0; i<n; i++) {
85: MatSetValue(B,i,i,value[1],INSERT_VALUES);
86: if (n-i+n/3<n) {
87: MatSetValue(B,n-i+n/3,i,value[0],INSERT_VALUES);
88: MatSetValue(B,i,n-i+n/3,value[0],INSERT_VALUES);
89: }
90: }
91: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
92: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
93: MatDuplicate(A,MAT_COPY_VALUES,&C);
94: MatAXPY(C,2.0,B,DIFFERENT_NONZERO_PATTERN);
96: /*
97: Set exact solution; then compute right-hand-side vector.
98: */
99: VecSet(u,one);
100: MatMult(C,u,b);
102: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103: Create the linear solver and set various options
104: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105: /*
106: Create linear solver context
107: */
108: KSPCreate(PETSC_COMM_WORLD,&ksp);
110: /*
111: Set operators. Here the matrix that defines the linear system
112: also serves as the preconditioning matrix.
113: */
114: KSPSetOperators(ksp,C,C);
116: /*
117: Set linear solver defaults for this problem (optional).
118: - By extracting the KSP and PC contexts from the KSP context,
119: we can then directly call any KSP and PC routines to set
120: various options.
121: - The following four statements are optional; all of these
122: parameters could alternatively be specified at runtime via
123: KSPSetFromOptions();
124: */
125: KSPGetPC(ksp,&pc);
126: PCSetType(pc,PCJACOBI);
127: KSPSetTolerances(ksp,1.e-5,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
129: /*
130: Set runtime options, e.g.,
131: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
132: These options will override those specified above as long as
133: KSPSetFromOptions() is called _after_ any other customization
134: routines.
135: */
136: KSPSetFromOptions(ksp);
138: if (nonzeroguess) {
139: PetscScalar p = .5;
140: VecSet(x,p);
141: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
142: }
144: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
145: Solve the linear system
146: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
147: /*
148: Solve linear system
149: */
150: KSPSolve(ksp,b,x);
152: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153: Check solution and clean up
154: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155: /*
156: Check the error
157: */
158: VecAXPY(x,-1.0,u);
159: VecNorm(x,NORM_2,&norm);
160: KSPGetIterationNumber(ksp,&its);
161: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its);
163: /*
164: Free work space. All PETSc objects should be destroyed when they
165: are no longer needed.
166: */
167: VecDestroy(&x)); PetscCall(VecDestroy(&u);
168: VecDestroy(&b)); PetscCall(MatDestroy(&A);
169: MatDestroy(&B);
170: MatDestroy(&C);
171: KSPDestroy(&ksp);
173: /*
174: Always call PetscFinalize() before exiting a program. This routine
175: - finalizes the PETSc libraries as well as MPI
176: - provides summary and diagnostic information if certain runtime
177: options are chosen (e.g., -log_view).
178: */
179: PetscFinalize();
180: return 0;
181: }
183: /*TEST
185: test:
186: args: -mat_type aij
187: output_file: output/ex58.out
189: test:
190: suffix: baij
191: args: -mat_type baij
192: output_file: output/ex58.out
194: test:
195: suffix: sbaij
196: args: -mat_type sbaij
197: output_file: output/ex58.out
199: TEST*/