Actual source code: ex7.c

petsc-3.8.3 2017-12-09
Report Typos and Errors

  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*);


 31: int main(int argc,char **argv)
 32: {
 33:   TS             ts;                  /* nonlinear solver */
 34:   Vec            U;                   /* solution, residual vectors */
 35:   Mat            J;                   /* Jacobian matrix */
 37:   DM             da;
 38:   AppCtx         user;
 39:   PetscInt       i;
 40:   char           Name[16];

 42:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 43:      Initialize program
 44:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 45:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 46:   user.N = 1;
 47:   PetscOptionsGetInt(NULL,NULL,"-N",&user.N,NULL);

 49:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 50:      Create distributed array (DMDA) to manage parallel grid and vectors
 51:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 52:   DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_MIRROR,-8,user.N,1,NULL,&da);
 53:   DMSetFromOptions(da);
 54:   DMSetUp(da);

 56:   for (i=0; i<user.N; i++) {
 57:     PetscSNPrintf(Name,16,"Void size %d",(int)(i+1));
 58:     DMDASetFieldName(da,i,Name);
 59:   }

 61:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 62:    Extract global vectors from DMDA; then duplicate for remaining
 63:      vectors that are the same types
 64:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 65:   DMCreateGlobalVector(da,&U);
 66:   DMSetMatType(da,MATAIJ);
 67:   DMCreateMatrix(da,&J);

 69:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 70:      Create timestepping solver context
 71:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 72:   TSCreate(PETSC_COMM_WORLD,&ts);
 73:   TSSetType(ts,TSARKIMEX);
 74:   TSSetDM(ts,da);
 75:   TSSetProblemType(ts,TS_NONLINEAR);
 76:   TSSetIFunction(ts,NULL,IFunction,&user);
 77:   TSSetIJacobian(ts,J,J,IJacobian,&user);


 80:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 81:      Set initial conditions
 82:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 83:   InitialConditions(da,U);
 84:   TSSetSolution(ts,U);

 86:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 87:      Set solver options
 88:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 89:   TSSetTimeStep(ts,.001);
 90:   TSSetMaxTime(ts,1.0);
 91:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
 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: /* ------------------------------------------------------------------- */
112: /*
113:    IFunction - Evaluates nonlinear function, F(U).

115:    Input Parameters:
116: .  ts - the TS context
117: .  U - input vector
118: .  ptr - optional user-defined context, as set by SNESSetFunction()

120:    Output Parameter:
121: .  F - function vector
122:  */
123: PetscErrorCode IFunction(TS ts,PetscReal ftime,Vec U,Vec Udot,Vec F,void *ptr)
124: {
125:   DM             da;
127:   PetscInt       i,c,Mx,xs,xm,N;
128:   PetscReal      hx,sx,x;
129:   PetscScalar    uxx;
130:   PetscScalar    **u,**f,**udot;
131:   Vec            localU;

134:   TSGetDM(ts,&da);
135:   DMGetLocalVector(da,&localU);
136:   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);

138:   hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);

140:   /*
141:      Scatter ghost points to local vector,using the 2-step process
142:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
143:      By placing code between these two statements, computations can be
144:      done while messages are in transition.
145:   */
146:   DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
147:   DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);

149:   /*
150:     Get pointers to vector data
151:   */
152:   DMDAVecGetArrayDOF(da,localU,&u);
153:   DMDAVecGetArrayDOF(da,Udot,&udot);
154:   DMDAVecGetArrayDOF(da,F,&f);

156:   /*
157:      Get local grid boundaries
158:   */
159:   DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);

161:   /*
162:      Compute function over the locally owned part of the grid
163:   */
164:   for (i=xs; i<xs+xm; i++) {
165:     x = i*hx;

167:     /*  diffusion term */
168:     for (c=0; c<N; c++) {
169:       uxx     = (-2.0*u[i][c] + u[i-1][c] + u[i+1][c])*sx;
170:       f[i][c] = udot[i][c] - uxx;
171:     }

173:     /* reaction terms */

175:     for (c=0; c<N/3; c++) {
176:       f[i][c]   +=  500*u[i][c]*u[i][c] + 500*u[i][c]*u[i][c+1];
177:       f[i][c+1] += -500*u[i][c]*u[i][c] + 500*u[i][c]*u[i][c+1];
178:       f[i][c+2] -=                        500*u[i][c]*u[i][c+1];
179:     }


182:     /* forcing term */

184:     f[i][0] -= 5*PetscExpScalar((1.0 - x)*(1.0 - x));

186:   }

188:   /*
189:      Restore vectors
190:   */
191:   DMDAVecRestoreArrayDOF(da,localU,&u);
192:   DMDAVecRestoreArrayDOF(da,Udot,&udot);
193:   DMDAVecRestoreArrayDOF(da,F,&f);
194:   DMRestoreLocalVector(da,&localU);
195:   return(0);
196: }

198: PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat J,Mat Jpre,void *ctx)
199: {
201:   PetscInt       i,c,Mx,xs,xm,nc;
202:   DM             da;
203:   MatStencil     col[3],row;
204:   PetscScalar    vals[3],hx,sx;
205:   AppCtx         *user = (AppCtx*)ctx;
206:   PetscInt       N     = user->N;
207:   PetscScalar    **u;

210:   TSGetDM(ts,&da);
211:   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);
212:   DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);

214:   hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);

216:   DMDAVecGetArrayDOF(da,U,&u);

218:   MatZeroEntries(Jpre);
219:   for (i=xs; i<xs+xm; i++) {
220:     for (c=0; c<N; c++) {
221:       nc        = 0;
222:       row.c     = c; row.i = i;
223:       col[nc].c = c; col[nc].i = i-1; vals[nc++] = -sx;
224:       col[nc].c = c; col[nc].i = i;   vals[nc++] = 2.0*sx + a;
225:       col[nc].c = c; col[nc].i = i+1; vals[nc++] = -sx;
226:       MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);
227:     }

229:     for (c=0; c<N/3; c++) {
230:       nc        = 0;
231:       row.c     = c;   row.i = i;
232:       col[nc].c = c;   col[nc].i = i; vals[nc++] = 1000*u[i][c] + 500*u[i][c+1];
233:       col[nc].c = c+1; col[nc].i = i; vals[nc++] =  500*u[i][c];
234:       MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);

236:       nc        = 0;
237:       row.c     = c+1; row.i = i;
238:       col[nc].c = c;   col[nc].i = i; vals[nc++] = -1000*u[i][c] + 500*u[i][c+1];
239:       col[nc].c = c+1; col[nc].i = i; vals[nc++] =   500*u[i][c];
240:       MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);

242:       nc        = 0;
243:       row.c     = c+2; row.i = i;
244:       col[nc].c = c;   col[nc].i = i; vals[nc++] =  -500*u[i][c+1];
245:       col[nc].c = c+1; col[nc].i = i; vals[nc++] =  -500*u[i][c];
246:       MatSetValuesStencil(Jpre,1,&row,nc,col,vals,ADD_VALUES);

248:     }
249:   }
250:   MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY);
251:   MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY);
252:   if (J != Jpre) {
253:     MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
254:     MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
255:   }
256:   DMDAVecRestoreArrayDOF(da,U,&u);
257:   return(0);
258: }

260: /* ------------------------------------------------------------------- */
261: PetscErrorCode InitialConditions(DM da,Vec U)
262: {
264:   PetscInt       i,c,xs,xm,Mx,N;
265:   PetscScalar    **u;
266:   PetscReal      hx,x;

269:   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);

271:   hx = 1.0/(PetscReal)(Mx-1);

273:   /*
274:      Get pointers to vector data
275:   */
276:   DMDAVecGetArrayDOF(da,U,&u);

278:   /*
279:      Get local grid boundaries
280:   */
281:   DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);

283:   /*
284:      Compute function over the locally owned part of the grid
285:   */
286:   for (i=xs; i<xs+xm; i++) {
287:     x = i*hx;
288:     for (c=0; c<N; c++) u[i][c] = 0.0; /*PetscCosScalar(PETSC_PI*x);*/
289:   }

291:   /*
292:      Restore vectors
293:   */
294:   DMDAVecRestoreArrayDOF(da,U,&u);
295:   return(0);
296: }