Actual source code: ex7.c
petsc-3.5.4 2015-05-23
2: static char help[] = ".\n";
4: /*
6: u_t = u_xx + R(u)
8: Where u(t,x,i) for i=0, .... N-1 where i+1 represents the void size
10: ex9.c is the 2d version of this code
11: */
13: #include <petscdm.h>
14: #include <petscdmda.h>
15: #include <petscts.h>
17: /*
18: User-defined data structures and routines
19: */
21: /* AppCtx */
22: typedef struct {
23: PetscInt N; /* number of dofs */
24: } AppCtx;
26: extern PetscErrorCode IFunction(TS,PetscReal,Vec,Vec,Vec,void*);
27: extern PetscErrorCode InitialConditions(DM,Vec);
28: extern PetscErrorCode IJacobian(TS,PetscReal,Vec,Vec,PetscReal,Mat,Mat,void*);
33: int main(int argc,char **argv)
34: {
35: TS ts; /* nonlinear solver */
36: Vec U; /* solution, residual vectors */
37: Mat J; /* Jacobian matrix */
38: PetscInt maxsteps = 1000;
40: DM da;
41: AppCtx user;
42: PetscInt i;
43: char Name[16];
45: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46: Initialize program
47: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48: PetscInitialize(&argc,&argv,(char*)0,help);
49: user.N = 1;
50: PetscOptionsGetInt(NULL,"-N",&user.N,NULL);
52: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53: Create distributed array (DMDA) to manage parallel grid and vectors
54: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_MIRROR,-8,user.N,1,NULL,&da);
57: for (i=0; i<user.N; i++) {
58: PetscSNPrintf(Name,16,"Void size %d",(int)(i+1));
59: DMDASetFieldName(da,i,Name);
60: }
62: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63: Extract global vectors from DMDA; then duplicate for remaining
64: vectors that are the same types
65: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
66: DMCreateGlobalVector(da,&U);
67: DMSetMatType(da,MATAIJ);
68: DMCreateMatrix(da,&J);
70: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71: Create timestepping solver context
72: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
73: TSCreate(PETSC_COMM_WORLD,&ts);
74: TSSetType(ts,TSARKIMEX);
75: TSSetDM(ts,da);
76: TSSetProblemType(ts,TS_NONLINEAR);
77: TSSetIFunction(ts,NULL,IFunction,&user);
78: TSSetIJacobian(ts,J,J,IJacobian,&user);
81: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82: Set initial conditions
83: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84: InitialConditions(da,U);
85: TSSetSolution(ts,U);
87: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88: Set solver options
89: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
90: TSSetInitialTimeStep(ts,0.0,.001);
91: TSSetDuration(ts,maxsteps,1.0);
92: TSSetFromOptions(ts);
94: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95: Solve nonlinear system
96: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
97: TSSolve(ts,U);
99: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100: Free work space. All PETSc objects should be destroyed when they
101: are no longer needed.
102: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
103: VecDestroy(&U);
104: MatDestroy(&J);
105: TSDestroy(&ts);
106: DMDestroy(&da);
108: PetscFinalize();
109: return(0);
110: }
111: /* ------------------------------------------------------------------- */
114: /*
115: IFunction - Evaluates nonlinear function, F(U).
117: Input Parameters:
118: . ts - the TS context
119: . U - input vector
120: . ptr - optional user-defined context, as set by SNESSetFunction()
122: Output Parameter:
123: . F - function vector
124: */
125: PetscErrorCode IFunction(TS ts,PetscReal ftime,Vec U,Vec Udot,Vec F,void *ptr)
126: {
127: DM da;
129: PetscInt i,c,Mx,xs,xm,N;
130: PetscReal hx,sx,x;
131: PetscScalar uxx;
132: PetscScalar **u,**f,**udot;
133: Vec localU;
136: TSGetDM(ts,&da);
137: DMGetLocalVector(da,&localU);
138: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,&N,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
140: hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
142: /*
143: Scatter ghost points to local vector,using the 2-step process
144: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
145: By placing code between these two statements, computations can be
146: done while messages are in transition.
147: */
148: DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
149: DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);
151: /*
152: Get pointers to vector data
153: */
154: DMDAVecGetArrayDOF(da,localU,&u);
155: DMDAVecGetArrayDOF(da,Udot,&udot);
156: DMDAVecGetArrayDOF(da,F,&f);
158: /*
159: Get local grid boundaries
160: */
161: DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);
163: /*
164: Compute function over the locally owned part of the grid
165: */
166: for (i=xs; i<xs+xm; i++) {
167: x = i*hx;
169: /* diffusion term */
170: for (c=0; c<N; c++) {
171: uxx = (-2.0*u[i][c] + u[i-1][c] + u[i+1][c])*sx;
172: f[i][c] = udot[i][c] - uxx;
173: }
175: /* reaction terms */
177: for (c=0; c<N/3; c++) {
178: f[i][c] += 500*u[i][c]*u[i][c] + 500*u[i][c]*u[i][c+1];
179: f[i][c+1] += -500*u[i][c]*u[i][c] + 500*u[i][c]*u[i][c+1];
180: f[i][c+2] -= 500*u[i][c]*u[i][c+1];
181: }
184: /* forcing term */
186: f[i][0] -= 5*PetscExpScalar((1.0 - x)*(1.0 - x));
188: }
190: /*
191: Restore vectors
192: */
193: DMDAVecRestoreArrayDOF(da,localU,&u);
194: DMDAVecRestoreArrayDOF(da,Udot,&udot);
195: DMDAVecRestoreArrayDOF(da,F,&f);
196: DMRestoreLocalVector(da,&localU);
197: return(0);
198: }
202: PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat J,Mat Jpre,void *ctx)
203: {
205: PetscInt i,c,Mx,xs,xm,nc;
206: DM da;
207: MatStencil col[3],row;
208: PetscScalar vals[3],hx,sx;
209: AppCtx *user = (AppCtx*)ctx;
210: PetscInt N = user->N;
211: PetscScalar **u;
214: TSGetDM(ts,&da);
215: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
216: DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);
218: hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
220: DMDAVecGetArrayDOF(da,U,&u);
222: MatZeroEntries(Jpre);
223: for (i=xs; i<xs+xm; i++) {
224: for (c=0; c<N; c++) {
225: nc = 0;
226: row.c = c; row.i = i;
227: col[nc].c = c; col[nc].i = i-1; vals[nc++] = -sx;
228: col[nc].c = c; col[nc].i = i; vals[nc++] = 2.0*sx + a;
229: col[nc].c = c; col[nc].i = i+1; vals[nc++] = -sx;
230: MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);
231: }
233: for (c=0; c<N/3; c++) {
234: nc = 0;
235: row.c = c; row.i = i;
236: col[nc].c = c; col[nc].i = i; vals[nc++] = 1000*u[i][c] + 500*u[i][c+1];
237: col[nc].c = c+1; col[nc].i = i; vals[nc++] = 500*u[i][c];
238: MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);
240: nc = 0;
241: row.c = c+1; row.i = i;
242: col[nc].c = c; col[nc].i = i; vals[nc++] = -1000*u[i][c] + 500*u[i][c+1];
243: col[nc].c = c+1; col[nc].i = i; vals[nc++] = 500*u[i][c];
244: MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);
246: nc = 0;
247: row.c = c+2; row.i = i;
248: col[nc].c = c; col[nc].i = i; vals[nc++] = -500*u[i][c+1];
249: col[nc].c = c+1; col[nc].i = i; vals[nc++] = -500*u[i][c];
250: MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);
252: }
253: }
254: MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY);
255: MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY);
256: if (J != Jpre) {
257: MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
258: MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
259: }
260: DMDAVecRestoreArrayDOF(da,U,&u);
261: return(0);
262: }
264: /* ------------------------------------------------------------------- */
267: PetscErrorCode InitialConditions(DM da,Vec U)
268: {
270: PetscInt i,c,xs,xm,Mx,N;
271: PetscScalar **u;
272: PetscReal hx,x;
275: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,&N,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
277: hx = 1.0/(PetscReal)(Mx-1);
279: /*
280: Get pointers to vector data
281: */
282: DMDAVecGetArrayDOF(da,U,&u);
284: /*
285: Get local grid boundaries
286: */
287: DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);
289: /*
290: Compute function over the locally owned part of the grid
291: */
292: for (i=xs; i<xs+xm; i++) {
293: x = i*hx;
294: for (c=0; c<N; c++) u[i][c] = 0.0; /*PetscCosScalar(PETSC_PI*x);*/
295: }
297: /*
298: Restore vectors
299: */
300: DMDAVecRestoreArrayDOF(da,U,&u);
301: return(0);
302: }