Actual source code: ex1.c
petsc-3.3-p7 2013-05-11
2: /* Program usage: mpiexec ex1 [-help] [all PETSc options] */
4: static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";
6: /*T
7: Concepts: KSP^solving a system of linear equations
8: Processors: 1
9: T*/
11: /*
12: Include "petscksp.h" so that we can use KSP solvers. Note that this file
13: automatically includes:
14: petscsys.h - base PETSc routines petscvec.h - vectors
15: petscmat.h - matrices
16: petscis.h - index sets petscksp.h - Krylov subspace methods
17: petscviewer.h - viewers petscpc.h - preconditioners
19: Note: The corresponding parallel example is ex23.c
20: */
21: #include <petscksp.h>
25: int main(int argc,char **args)
26: {
27: Vec x, b, u; /* approx solution, RHS, exact solution */
28: Mat A; /* linear system matrix */
29: KSP ksp; /* linear solver context */
30: PC pc; /* preconditioner context */
31: PetscReal norm,tol=1.e-14; /* norm of solution error */
33: PetscInt i,n = 10,col[3],its;
34: PetscMPIInt size;
35: PetscScalar neg_one = -1.0,one = 1.0,value[3];
36: PetscBool nonzeroguess = PETSC_FALSE;
38: PetscInitialize(&argc,&args,(char *)0,help);
39: MPI_Comm_size(PETSC_COMM_WORLD,&size);
40: if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");
41: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
42: PetscOptionsGetBool(PETSC_NULL,"-nonzero_guess",&nonzeroguess,PETSC_NULL);
45: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46: Compute the matrix and right-hand-side vector that define
47: the linear system, Ax = b.
48: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
50: /*
51: Create vectors. Note that we form 1 vector from scratch and
52: then duplicate as needed.
53: */
54: VecCreate(PETSC_COMM_WORLD,&x);
55: PetscObjectSetName((PetscObject) x, "Solution");
56: VecSetSizes(x,PETSC_DECIDE,n);
57: VecSetFromOptions(x);
58: VecDuplicate(x,&b);
59: VecDuplicate(x,&u);
61: /*
62: Create matrix. When using MatCreate(), the matrix format can
63: be specified at runtime.
65: Performance tuning note: For problems of substantial size,
66: preallocation of matrix memory is crucial for attaining good
67: performance. See the matrix chapter of the users manual for details.
68: */
69: MatCreate(PETSC_COMM_WORLD,&A);
70: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
71: MatSetFromOptions(A);
72: MatSetUp(A);
74: /*
75: Assemble matrix
76: */
77: value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
78: for (i=1; i<n-1; i++) {
79: col[0] = i-1; col[1] = i; col[2] = i+1;
80: MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
81: }
82: i = n - 1; col[0] = n - 2; col[1] = n - 1;
83: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
84: i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
85: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
86: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
87: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
89: /*
90: Set exact solution; then compute right-hand-side vector.
91: */
92: VecSet(u,one);
93: MatMult(A,u,b);
95: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96: Create the linear solver and set various options
97: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98: /*
99: Create linear solver context
100: */
101: KSPCreate(PETSC_COMM_WORLD,&ksp);
103: /*
104: Set operators. Here the matrix that defines the linear system
105: also serves as the preconditioning matrix.
106: */
107: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
109: /*
110: Set linear solver defaults for this problem (optional).
111: - By extracting the KSP and PC contexts from the KSP context,
112: we can then directly call any KSP and PC routines to set
113: various options.
114: - The following four statements are optional; all of these
115: parameters could alternatively be specified at runtime via
116: KSPSetFromOptions();
117: */
118: KSPGetPC(ksp,&pc);
119: PCSetType(pc,PCJACOBI);
120: KSPSetTolerances(ksp,1.e-5,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
122: /*
123: Set runtime options, e.g.,
124: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
125: These options will override those specified above as long as
126: KSPSetFromOptions() is called _after_ any other customization
127: routines.
128: */
129: KSPSetFromOptions(ksp);
131: if (nonzeroguess) {
132: PetscScalar p = .5;
133: VecSet(x,p);
134: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
135: }
136:
137: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138: Solve the linear system
139: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140: /*
141: Solve linear system
142: */
143: KSPSolve(ksp,b,x);
145: /*
146: View solver info; we could instead use the option -ksp_view to
147: print this info to the screen at the conclusion of KSPSolve().
148: */
149: KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
151: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152: Check solution and clean up
153: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154: /*
155: Check the error
156: */
157: VecAXPY(x,neg_one,u);
158: VecNorm(x,NORM_2,&norm);
159: KSPGetIterationNumber(ksp,&its);
160: if (norm > tol){
161: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %G, Iterations %D\n",
162: norm,its);
163: }
165: /*
166: Free work space. All PETSc objects should be destroyed when they
167: are no longer needed.
168: */
169: VecDestroy(&x); VecDestroy(&u);
170: VecDestroy(&b); MatDestroy(&A);
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_summary).
178: */
179: PetscFinalize();
180: return 0;
181: }