Actual source code: ex13.c
1: /*$Id: ex13.c,v 1.29 2001/08/07 21:30:54 bsmith Exp $*/
3: static char help[] = "Solves a variable Poisson problem with KSP.\n\n";
5: /*T
6: Concepts: KSP^basic sequential example
7: Concepts: KSP^Laplacian, 2d
8: Concepts: Laplacian, 2d
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
19: */
20: #include petscksp.h
22: /*
23: User-defined context that contains all the data structures used
24: in the linear solution process.
25: */
26: typedef struct {
27: Vec x,b; /* solution vector, right-hand-side vector */
28: Mat A; /* sparse matrix */
29: KSP ksp; /* linear solver context */
30: int m,n; /* grid dimensions */
31: PetscScalar hx2,hy2; /* 1/(m+1)*(m+1) and 1/(n+1)*(n+1) */
32: } UserCtx;
34: extern int UserInitializeLinearSolver(int,int,UserCtx *);
35: extern int UserFinalizeLinearSolver(UserCtx *);
36: extern int UserDoLinearSolver(PetscScalar *,UserCtx *userctx,PetscScalar *b,PetscScalar *x);
40: int main(int argc,char **args)
41: {
42: UserCtx userctx;
43: int ierr,m = 6,n = 7,t,tmax = 2,i,I,j,N;
44: PetscScalar *userx,*rho,*solution,*userb,hx,hy,x,y;
45: PetscReal enorm;
46: /*
47: Initialize the PETSc libraries
48: */
49: PetscInitialize(&argc,&args,(char *)0,help);
51: /*
52: The next two lines are for testing only; these allow the user to
53: decide the grid size at runtime.
54: */
55: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
56: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
58: /*
59: Create the empty sparse matrix and linear solver data structures
60: */
61: UserInitializeLinearSolver(m,n,&userctx);
62: N = m*n;
64: /*
65: Allocate arrays to hold the solution to the linear system.
66: This is not normally done in PETSc programs, but in this case,
67: since we are calling these routines from a non-PETSc program, we
68: would like to reuse the data structures from another code. So in
69: the context of a larger application these would be provided by
70: other (non-PETSc) parts of the application code.
71: */
72: PetscMalloc(N*sizeof(PetscScalar),&userx);
73: PetscMalloc(N*sizeof(PetscScalar),&userb);
74: PetscMalloc(N*sizeof(PetscScalar),&solution);
76: /*
77: Allocate an array to hold the coefficients in the elliptic operator
78: */
79: PetscMalloc(N*sizeof(PetscScalar),&rho);
81: /*
82: Fill up the array rho[] with the function rho(x,y) = x; fill the
83: right-hand-side b[] and the solution with a known problem for testing.
84: */
85: hx = 1.0/(m+1);
86: hy = 1.0/(n+1);
87: y = hy;
88: I = 0;
89: for (j=0; j<n; j++) {
90: x = hx;
91: for (i=0; i<m; i++) {
92: rho[I] = x;
93: solution[I] = PetscSinScalar(2.*PETSC_PI*x)*PetscSinScalar(2.*PETSC_PI*y);
94: userb[I] = -2*PETSC_PI*PetscCosScalar(2*PETSC_PI*x)*PetscSinScalar(2*PETSC_PI*y) +
95: 8*PETSC_PI*PETSC_PI*x*PetscSinScalar(2*PETSC_PI*x)*PetscSinScalar(2*PETSC_PI*y);
96: x += hx;
97: I++;
98: }
99: y += hy;
100: }
102: /*
103: Loop over a bunch of timesteps, setting up and solver the linear
104: system for each time-step.
106: Note this is somewhat artificial. It is intended to demonstrate how
107: one may reuse the linear solver stuff in each time-step.
108: */
109: for (t=0; t<tmax; t++) {
110: UserDoLinearSolver(rho,&userctx,userb,userx);
112: /*
113: Compute error: Note that this could (and usually should) all be done
114: using the PETSc vector operations. Here we demonstrate using more
115: standard programming practices to show how they may be mixed with
116: PETSc.
117: */
118: enorm = 0.0;
119: for (i=0; i<N; i++) {
120: enorm += PetscRealPart(PetscConj(solution[i]-userx[i])*(solution[i]-userx[i]));
121: }
122: enorm *= PetscRealPart(hx*hy);
123: printf("m %d n %d error norm %g\n",m,n,enorm);
124: }
126: /*
127: We are all finished solving linear systems, so we clean up the
128: data structures.
129: */
130: PetscFree(rho);
131: PetscFree(solution);
132: PetscFree(userx);
133: PetscFree(userb);
134: UserFinalizeLinearSolver(&userctx);
135: PetscFinalize();
137: return 0;
138: }
140: /* ------------------------------------------------------------------------*/
143: int UserInitializeLinearSolver(int m,int n,UserCtx *userctx)
144: {
145: int N,ierr;
147: /*
148: Here we assume use of a grid of size m x n, with all points on the
149: interior of the domain, i.e., we do not include the points corresponding
150: to homogeneous Dirichlet boundary conditions. We assume that the domain
151: is [0,1]x[0,1].
152: */
153: userctx->m = m;
154: userctx->n = n;
155: userctx->hx2 = (m+1)*(m+1);
156: userctx->hy2 = (n+1)*(n+1);
157: N = m*n;
159: /*
160: Create the sparse matrix. Preallocate 5 nonzeros per row.
161: */
162: MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,5,0,&userctx->A);
164: /*
165: Create vectors. Here we create vectors with no memory allocated.
166: This way, we can use the data structures already in the program
167: by using VecPlaceArray() subroutine at a later stage.
168: */
169: VecCreateSeqWithArray(PETSC_COMM_SELF,N,PETSC_NULL,&userctx->b);
170: VecDuplicate(userctx->b,&userctx->x);
172: /*
173: Create linear solver context. This will be used repeatedly for all
174: the linear solves needed.
175: */
176: KSPCreate(PETSC_COMM_SELF,&userctx->ksp);
178: return 0;
179: }
183: /*
184: Solves -div (rho grad psi) = F using finite differences.
185: rho is a 2-dimensional array of size m by n, stored in Fortran
186: style by columns. userb is a standard one-dimensional array.
187: */
188: /* ------------------------------------------------------------------------*/
189: int UserDoLinearSolver(PetscScalar *rho,UserCtx *userctx,PetscScalar *userb,PetscScalar *userx)
190: {
191: int ierr,i,j,I,J,m = userctx->m,n = userctx->n;
192: Mat A = userctx->A;
193: PC pc;
194: PetscScalar v,hx2 = userctx->hx2,hy2 = userctx->hy2;
196: /*
197: This is not the most efficient way of generating the matrix
198: but let's not worry about it. We should have separate code for
199: the four corners, each edge and then the interior. Then we won't
200: have the slow if-tests inside the loop.
202: Computes the operator
203: -div rho grad
204: on an m by n grid with zero Dirichlet boundary conditions. The rho
205: is assumed to be given on the same grid as the finite difference
206: stencil is applied. For a staggered grid, one would have to change
207: things slightly.
208: */
209: I = 0;
210: for (j=0; j<n; j++) {
211: for (i=0; i<m; i++) {
212: if (j>0) {
213: J = I - m;
214: v = -.5*(rho[I] + rho[J])*hy2;
215: MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);
216: }
217: if (j<n-1) {
218: J = I + m;
219: v = -.5*(rho[I] + rho[J])*hy2;
220: MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);
221: }
222: if (i>0) {
223: J = I - 1;
224: v = -.5*(rho[I] + rho[J])*hx2;
225: MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);
226: }
227: if (i<m-1) {
228: J = I + 1;
229: v = -.5*(rho[I] + rho[J])*hx2;
230: MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);
231: }
232: v = 2.0*rho[I]*(hx2+hy2);
233: MatSetValues(A,1,&I,1,&I,&v,INSERT_VALUES);
234: I++;
235: }
236: }
238: /*
239: Assemble matrix
240: */
241: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
242: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
244: /*
245: Set operators. Here the matrix that defines the linear system
246: also serves as the preconditioning matrix. Since all the matrices
247: will have the same nonzero pattern here, we indicate this so the
248: linear solvers can take advantage of this.
249: */
250: KSPSetOperators(userctx->ksp,A,A,SAME_NONZERO_PATTERN);
252: /*
253: Set linear solver defaults for this problem (optional).
254: - Here we set it to use direct LU factorization for the solution
255: */
256: KSPGetPC(userctx->ksp,&pc);
257: PCSetType(pc,PCLU);
259: /*
260: Set runtime options, e.g.,
261: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
262: These options will override those specified above as long as
263: KSPSetFromOptions() is called _after_ any other customization
264: routines.
265:
266: Run the program with the option -help to see all the possible
267: linear solver options.
268: */
269: KSPSetFromOptions(userctx->ksp);
271: /*
272: This allows the PETSc linear solvers to compute the solution
273: directly in the user's array rather than in the PETSc vector.
274:
275: This is essentially a hack and not highly recommend unless you
276: are quite comfortable with using PETSc. In general, users should
277: write their entire application using PETSc vectors rather than
278: arrays.
279: */
280: VecPlaceArray(userctx->x,userx);
281: VecPlaceArray(userctx->b,userb);
283: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284: Solve the linear system
285: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
287: KSPSetRhs(userctx->ksp,userctx->b);
288: KSPSetSolution(userctx->ksp,userctx->x);
289: KSPSolve(userctx->ksp);
291: /*
292: Put back the PETSc array that belongs in the vector xuserctx->x
293: */
295: return 0;
296: }
298: /* ------------------------------------------------------------------------*/
301: int UserFinalizeLinearSolver(UserCtx *userctx)
302: {
304: /*
305: We are all done and don't need to solve any more linear systems, so
306: we free the work space. All PETSc objects should be destroyed when
307: they are no longer needed.
308: */
309: KSPDestroy(userctx->ksp);
310: VecDestroy(userctx->x);
311: VecDestroy(userctx->b);
312: MatDestroy(userctx->A);
313: return 0;
314: }