Actual source code: ex1.c
1: /*
2: Formatted test for TS routines.
4: Solves U_t = U_xx
5: F(t,u) = (u_i+1 - 2u_i + u_i-1)/h^2
6: using several different schemes.
7: */
9: static char help[] = "Solves 1D heat equation.\n\n";
11: #include petscda.h
12: #include petscsys.h
13: #include petscts.h
15: #define PETSC_NEAR(a,b,c) (!(PetscAbsReal((a)-(b)) > (c)*PetscMax(PetscAbsReal(a),PetscAbsReal(b))))
17: typedef struct {
18: Vec global,local,localwork,solution; /* location for local work (with ghost points) vector */
19: DA da; /* manages ghost point communication */
20: PetscViewer viewer1,viewer2;
21: PetscInt M; /* total number of grid points */
22: PetscReal h; /* mesh width h = 1/(M-1) */
23: PetscReal norm_2,norm_max;
24: PetscTruth nox; /* indicates problem is to be run without graphics */
25: } AppCtx;
34: #define linear_no_matrix 0
35: #define linear_no_time 1
36: #define linear 2
37: #define nonlinear_no_jacobian 3
38: #define nonlinear 4
42: int main(int argc,char **argv)
43: {
45: PetscInt time_steps = 100,steps,m;
46: PetscMPIInt size;
47: PetscInt problem = linear_no_matrix;
48: PetscTruth flg;
49: AppCtx appctx;
50: PetscReal dt,ftime;
51: TS ts;
52: Mat A = 0;
53: MatStructure A_structure;
54: TSProblemType tsproblem = TS_LINEAR;
55: PetscDraw draw;
56: PetscViewer viewer;
57: char tsinfo[120];
58:
59: PetscInitialize(&argc,&argv,(char*)0,help);
60: MPI_Comm_size(PETSC_COMM_WORLD,&size);
62: appctx.M = 60;
63: PetscOptionsGetInt(PETSC_NULL,"-M",&appctx.M,PETSC_NULL);
64: PetscOptionsGetInt(PETSC_NULL,"-time",&time_steps,PETSC_NULL);
65:
66: PetscOptionsHasName(PETSC_NULL,"-nox",&appctx.nox);
67: appctx.norm_2 = 0.0; appctx.norm_max = 0.0;
69: /* Set up the ghost point communication pattern */
70: DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,appctx.M,1,1,PETSC_NULL,&appctx.da);
71: DACreateGlobalVector(appctx.da,&appctx.global);
72: VecGetLocalSize(appctx.global,&m);
73: DACreateLocalVector(appctx.da,&appctx.local);
75: /* Set up display to show wave graph */
77: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",80,380,400,160,&appctx.viewer1);
78: PetscViewerDrawGetDraw(appctx.viewer1,0,&draw);
79: PetscDrawSetDoubleBuffer(draw);
80: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",80,0,400,160,&appctx.viewer2);
81: PetscViewerDrawGetDraw(appctx.viewer2,0,&draw);
82: PetscDrawSetDoubleBuffer(draw);
85: /* make work array for evaluating right hand side function */
86: VecDuplicate(appctx.local,&appctx.localwork);
88: /* make work array for storing exact solution */
89: VecDuplicate(appctx.global,&appctx.solution);
91: appctx.h = 1.0/(appctx.M-1.0);
93: /* set initial conditions */
94: Initial(appctx.global,&appctx);
95:
96: /*
97: This example is written to allow one to easily test parts
98: of TS, we do not expect users to generally need to use more
99: then a single TSProblemType
100: */
101: PetscOptionsHasName(PETSC_NULL,"-linear_no_matrix",&flg);
102: if (flg) {
103: tsproblem = TS_LINEAR;
104: problem = linear_no_matrix;
105: }
106: PetscOptionsHasName(PETSC_NULL,"-linear_constant_matrix",&flg);
107: if (flg) {
108: tsproblem = TS_LINEAR;
109: problem = linear_no_time;
110: }
111: PetscOptionsHasName(PETSC_NULL,"-linear_variable_matrix",&flg);
112: if (flg) {
113: tsproblem = TS_LINEAR;
114: problem = linear;
115: }
116: PetscOptionsHasName(PETSC_NULL,"-nonlinear_no_jacobian",&flg);
117: if (flg) {
118: tsproblem = TS_NONLINEAR;
119: problem = nonlinear_no_jacobian;
120: }
121: PetscOptionsHasName(PETSC_NULL,"-nonlinear_jacobian",&flg);
122: if (flg) {
123: tsproblem = TS_NONLINEAR;
124: problem = nonlinear;
125: }
126:
127: /* make timestep context */
128: TSCreate(PETSC_COMM_WORLD,&ts);
129: TSSetProblemType(ts,tsproblem);
130: TSSetMonitor(ts,Monitor,&appctx,PETSC_NULL);
132: dt = appctx.h*appctx.h/2.01;
134: if (problem == linear_no_matrix) {
135: /*
136: The user provides the RHS as a Shell matrix.
137: */
138: MatCreateShell(PETSC_COMM_WORLD,m,appctx.M,appctx.M,appctx.M,&appctx,&A);
139: MatShellSetOperation(A,MATOP_MULT,(void(*)(void))RHSMatrixFree);
140: TSSetRHSMatrix(ts,A,A,PETSC_NULL,&appctx);
141: } else if (problem == linear_no_time) {
142: /*
143: The user provides the RHS as a matrix
144: */
145: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,appctx.M,appctx.M,&A);
146: MatSetFromOptions(A);
147: RHSMatrixHeat(ts,0.0,&A,&A,&A_structure,&appctx);
148: TSSetRHSMatrix(ts,A,A,PETSC_NULL,&appctx);
149: } else if (problem == linear) {
150: /*
151: The user provides the RHS as a time dependent matrix
152: */
153: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,appctx.M,appctx.M,&A);
154: MatSetFromOptions(A);
155: RHSMatrixHeat(ts,0.0,&A,&A,&A_structure,&appctx);
156: TSSetRHSMatrix(ts,A,A,RHSMatrixHeat,&appctx);
157: } else if (problem == nonlinear_no_jacobian) {
158: /*
159: The user provides the RHS and a Shell Jacobian
160: */
161: TSSetRHSFunction(ts,RHSFunctionHeat,&appctx);
162: MatCreateShell(PETSC_COMM_WORLD,m,appctx.M,appctx.M,appctx.M,&appctx,&A);
163: MatShellSetOperation(A,MATOP_MULT,(void(*)(void))RHSMatrixFree);
164: TSSetRHSJacobian(ts,A,A,PETSC_NULL,&appctx);
165: } else if (problem == nonlinear) {
166: /*
167: The user provides the RHS and Jacobian
168: */
169: TSSetRHSFunction(ts,RHSFunctionHeat,&appctx);
170: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,appctx.M,appctx.M,&A);
171: MatSetFromOptions(A);
172: RHSMatrixHeat(ts,0.0,&A,&A,&A_structure,&appctx);
173: TSSetRHSJacobian(ts,A,A,RHSJacobianHeat,&appctx);
174: }
176: TSSetFromOptions(ts);
178: TSSetInitialTimeStep(ts,0.0,dt);
179: TSSetDuration(ts,time_steps,100.);
180: TSSetSolution(ts,appctx.global);
183: TSSetUp(ts);
184: TSStep(ts,&steps,&ftime);
185: PetscViewerStringOpen(PETSC_COMM_WORLD,tsinfo,120,&viewer);
186: TSView(ts,viewer);
188: PetscOptionsHasName(PETSC_NULL,"-test",&flg);
189: if (flg) {
190: PetscTruth iseuler;
191: PetscTypeCompare((PetscObject)ts,"euler",&iseuler);
192: if (iseuler) {
193: if (!PETSC_NEAR(appctx.norm_2/steps,0.00257244,1.e-4)) {
194: fprintf(stdout,"Error in Euler method: 2-norm %g expecting: 0.00257244\n",appctx.norm_2/steps);
195: }
196: } else {
197: if (!PETSC_NEAR(appctx.norm_2/steps,0.00506174,1.e-4)) {
198: fprintf(stdout,"Error in %s method: 2-norm %g expecting: 0.00506174\n",tsinfo,appctx.norm_2/steps);
199: }
200: }
201: } else {
202: PetscPrintf(PETSC_COMM_WORLD,"%D Procs Avg. error 2 norm %g max norm %g %s\n",
203: size,appctx.norm_2/steps,appctx.norm_max/steps,tsinfo);
204: }
206: PetscViewerDestroy(viewer);
207: TSDestroy(ts);
208: PetscViewerDestroy(appctx.viewer1);
209: PetscViewerDestroy(appctx.viewer2);
210: VecDestroy(appctx.localwork);
211: VecDestroy(appctx.solution);
212: VecDestroy(appctx.local);
213: VecDestroy(appctx.global);
214: DADestroy(appctx.da);
215: if (A) {ierr= MatDestroy(A);}
217: PetscFinalize();
218: return 0;
219: }
221: /* -------------------------------------------------------------------*/
224: PetscErrorCode Initial(Vec global,void *ctx)
225: {
226: AppCtx *appctx = (AppCtx*) ctx;
227: PetscScalar *localptr,h = appctx->h;
228: PetscInt i,mybase,myend;
231: /* determine starting point of each processor */
232: VecGetOwnershipRange(global,&mybase,&myend);
234: /* Initialize the array */
235: VecGetArray(global,&localptr);
236: for (i=mybase; i<myend; i++) {
237: localptr[i-mybase] = PetscSinScalar(PETSC_PI*i*6.*h) + 3.*PetscSinScalar(PETSC_PI*i*2.*h);
238: }
239: VecRestoreArray(global,&localptr);
240: return 0;
241: }
245: /*
246: Exact solution
247: */
248: PetscErrorCode Solution(PetscReal t,Vec solution,void *ctx)
249: {
250: AppCtx * appctx = (AppCtx*) ctx;
251: PetscScalar *localptr,h = appctx->h,ex1,ex2,sc1,sc2;
252: PetscInt i,mybase,myend;
255: /* determine starting point of each processor */
256: VecGetOwnershipRange(solution,&mybase,&myend);
258: ex1 = exp(-36.*PETSC_PI*PETSC_PI*t);
259: ex2 = exp(-4.*PETSC_PI*PETSC_PI*t);
260: sc1 = PETSC_PI*6.*h; sc2 = PETSC_PI*2.*h;
261: VecGetArray(solution,&localptr);
262: for (i=mybase; i<myend; i++) {
263: localptr[i-mybase] = PetscSinScalar(sc1*(PetscReal)i)*ex1 + 3.*PetscSinScalar(sc2*(PetscReal)i)*ex2;
264: }
265: VecRestoreArray(solution,&localptr);
266: return 0;
267: }
271: PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal ltime,Vec global,void *ctx)
272: {
273: AppCtx *appctx = (AppCtx*) ctx;
275: PetscReal norm_2,norm_max;
276: PetscScalar mone = -1.0;
277: MPI_Comm comm;
279: PetscObjectGetComm((PetscObject)ts,&comm);
281: VecView(global,appctx->viewer2);
283: Solution(ltime,appctx->solution,ctx);
284: VecAXPY(&mone,global,appctx->solution);
285: VecNorm(appctx->solution,NORM_2,&norm_2);
286: norm_2 = sqrt(appctx->h)*norm_2;
287: VecNorm(appctx->solution,NORM_MAX,&norm_max);
289: if (!appctx->nox) {
290: PetscPrintf(comm,"timestep %D time %g norm of error %g %g\n",step,ltime,norm_2,norm_max);
291: }
293: appctx->norm_2 += norm_2;
294: appctx->norm_max += norm_max;
296: VecView(appctx->solution,appctx->viewer1);
298: return 0;
299: }
301: /* -----------------------------------------------------------------------*/
304: PetscErrorCode RHSMatrixFree(Mat mat,Vec x,Vec y)
305: {
306: PetscErrorCode ierr;
307: void *ctx;
309: MatShellGetContext(mat,(void **)&ctx);
310: RHSFunctionHeat(0,0.0,x,y,ctx);
311: return 0;
312: }
316: PetscErrorCode RHSFunctionHeat(TS ts,PetscReal t,Vec globalin,Vec globalout,void *ctx)
317: {
318: AppCtx *appctx = (AppCtx*) ctx;
319: DA da = appctx->da;
320: Vec local = appctx->local,localwork = appctx->localwork;
322: PetscInt i,localsize;
323: PetscScalar *copyptr,*localptr,sc;
325: /*Extract local array */
326: DAGlobalToLocalBegin(da,globalin,INSERT_VALUES,local);
327: DAGlobalToLocalEnd(da,globalin,INSERT_VALUES,local);
328: VecGetArray(local,&localptr);
330: /* Extract work vector */
331: VecGetArray(localwork,©ptr);
333: /* Update Locally - Make array of new values */
334: /* Note: For the first and last entry I copy the value */
335: /* if this is an interior node it is irrelevant */
336: sc = 1.0/(appctx->h*appctx->h);
337: VecGetLocalSize(local,&localsize);
338: copyptr[0] = localptr[0];
339: for (i=1; i<localsize-1; i++) {
340: copyptr[i] = sc * (localptr[i+1] + localptr[i-1] - 2.0*localptr[i]);
341: }
342: copyptr[localsize-1] = localptr[localsize-1];
343: VecRestoreArray(local,&localptr);
344: VecRestoreArray(localwork,©ptr);
346: /* Local to Global */
347: DALocalToGlobal(da,localwork,INSERT_VALUES,globalout);
348: return 0;
349: }
351: /* ---------------------------------------------------------------------*/
354: PetscErrorCode RHSMatrixHeat(TS ts,PetscReal t,Mat *AA,Mat *BB,MatStructure *str,void *ctx)
355: {
356: Mat A = *AA;
357: AppCtx *appctx = (AppCtx*) ctx;
359: PetscInt i,mstart,mend,idx[3];
360: PetscMPIInt size,rank;
361: PetscScalar v[3],stwo = -2./(appctx->h*appctx->h),sone = -.5*stwo;
363: *str = SAME_NONZERO_PATTERN;
365: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
366: MPI_Comm_size(PETSC_COMM_WORLD,&size);
368: MatGetOwnershipRange(A,&mstart,&mend);
369: if (mstart == 0) {
370: v[0] = 1.0;
371: MatSetValues(A,1,&mstart,1,&mstart,v,INSERT_VALUES);
372: mstart++;
373: }
374: if (mend == appctx->M) {
375: mend--;
376: v[0] = 1.0;
377: MatSetValues(A,1,&mend,1,&mend,v,INSERT_VALUES);
378: }
380: /*
381: Construct matrice one row at a time
382: */
383: v[0] = sone; v[1] = stwo; v[2] = sone;
384: for (i=mstart; i<mend; i++) {
385: idx[0] = i-1; idx[1] = i; idx[2] = i+1;
386: MatSetValues(A,1,&i,3,idx,v,INSERT_VALUES);
387: }
389: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
390: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
391: return 0;
392: }
396: PetscErrorCode RHSJacobianHeat(TS ts,PetscReal t,Vec x,Mat *AA,Mat *BB,MatStructure *str,void *ctx)
397: {
398: return RHSMatrixHeat(ts,t,AA,BB,str,ctx);
399: }