Actual source code: ex1.c
1: /*$Id: ex1.c,v 1.90 2001/08/07 21:30:54 bsmith Exp $*/
3: /* Program usage: mpirun ex1 [-help] [all PETSc options] */
5: static char help[] = "Solves a tridiagonal linear system with KSP.\n\n";
7: /*T
8: Concepts: KSP^solving a system of linear equations
9: Processors: 1
10: T*/
12: /*
13: Include "petscksp.h" so that we can use KSP solvers. Note that this file
14: automatically includes:
15: petsc.h - base PETSc routines petscvec.h - vectors
16: petscsys.h - system routines petscmat.h - matrices
17: petscis.h - index sets petscksp.h - Krylov subspace methods
18: petscviewer.h - viewers petscpc.h - preconditioners
20: Note: The corresponding parallel example is ex23.c
21: */
22: #include petscksp.h
26: int main(int argc,char **args)
27: {
28: Vec x, b, u; /* approx solution, RHS, exact solution */
29: Mat A; /* linear system matrix */
30: KSP ksp; /* linear solver context */
31: PC pc; /* preconditioner context */
32: PetscReal norm; /* norm of solution error */
33: int ierr,i,n = 10,col[3],its,size;
34: PetscScalar neg_one = -1.0,one = 1.0,value[3];
36: PetscInitialize(&argc,&args,(char *)0,help);
37: MPI_Comm_size(PETSC_COMM_WORLD,&size);
38: if (size != 1) SETERRQ(1,"This is a uniprocessor example only!");
39: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
41: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42: Compute the matrix and right-hand-side vector that define
43: the linear system, Ax = b.
44: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46: /*
47: Create vectors. Note that we form 1 vector from scratch and
48: then duplicate as needed.
49: */
50: VecCreate(PETSC_COMM_WORLD,&x);
51: PetscObjectSetName((PetscObject) x, "Solution");
52: VecSetSizes(x,PETSC_DECIDE,n);
53: VecSetFromOptions(x);
54: VecDuplicate(x,&b);
55: VecDuplicate(x,&u);
57: /*
58: Create matrix. When using MatCreate(), the matrix format can
59: be specified at runtime.
61: Performance tuning note: For problems of substantial size,
62: preallocation of matrix memory is crucial for attaining good
63: performance. Since preallocation is not possible via the generic
64: matrix creation routine MatCreate(), we recommend for practical
65: problems instead to use the creation routine for a particular matrix
66: format, e.g.,
67: MatCreateSeqAIJ() - sequential AIJ (compressed sparse row)
68: MatCreateSeqBAIJ() - block AIJ
69: See the matrix chapter of the users manual for details.
70: */
71: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,&A);
72: MatSetFromOptions(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(&one,u);
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-7,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);
130:
131: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132: Solve the linear system
133: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
134: /*
135: Solve linear system
136: */
137: KSPSetRhs(ksp,b);
138: KSPSetSolution(ksp,x);
139: KSPSolve(ksp);
141: /*
142: View solver info; we could instead use the option -ksp_view to
143: print this info to the screen at the conclusion of KSPSolve().
144: */
145: KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
147: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148: Check solution and clean up
149: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150: /*
151: Check the error
152: */
153: VecAXPY(&neg_one,u,x);
154: VecNorm(x,NORM_2,&norm);
155: KSPGetIterationNumber(ksp,&its);
156: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A, Iterations %d\n",
157: norm,its);
158: /*
159: Free work space. All PETSc objects should be destroyed when they
160: are no longer needed.
161: */
162: VecDestroy(x); VecDestroy(u);
163: VecDestroy(b); MatDestroy(A);
164: KSPDestroy(ksp);
166: /*
167: Always call PetscFinalize() before exiting a program. This routine
168: - finalizes the PETSc libraries as well as MPI
169: - provides summary and diagnostic information if certain runtime
170: options are chosen (e.g., -log_summary).
171: */
172: PetscFinalize();
173: return 0;
174: }