Actual source code: ex40.c

petsc-master 2015-04-27
Report Typos and Errors
  2: static char help[] = "Serial bouncing ball example to test TS event feature.\n";

  4: /*
  5:   The dynamics of the bouncing ball is described by the ODE
  6:                   u1_t = u2
  7:                   u2_t = -9.8

  9:   There are two events set in this example. The first one checks for the ball hitting the
 10:   ground (u1 = 0). Every time the ball hits the ground, its velocity u2 is attenuated by
 11:   a factor of 0.9. The second event sets a limit on the number of ball bounces.
 12: */

 14: #include <petscts.h>

 16: typedef struct {
 17:   PetscInt maxbounces;
 18:   PetscInt nbounces;
 19: } AppCtx;

 23: PetscErrorCode EventFunction(TS ts,PetscReal t,Vec U,PetscScalar *fvalue,void *ctx)
 24: {
 25:   AppCtx            *app=(AppCtx*)ctx;
 26:   PetscErrorCode    ierr;
 27:   const PetscScalar *u;

 30:   /* Event for ball height */
 31:   VecGetArrayRead(U,&u);
 32:   fvalue[0] = u[0];
 33:   /* Event for number of bounces */
 34:   fvalue[1] = app->maxbounces - app->nbounces;
 35:   VecRestoreArrayRead(U,&u);
 36:   return(0);
 37: }

 41: PetscErrorCode PostEventFunction(TS ts,PetscInt nevents,PetscInt event_list[],PetscReal t,Vec U,PetscBool forwardsolve,void* ctx)
 42: {
 43:   AppCtx         *app=(AppCtx*)ctx;
 45:   PetscScalar    *u;

 48:   if (event_list[0] == 0) {
 49:     VecGetArray(U,&u);
 50:     PetscPrintf(PETSC_COMM_SELF,"Ball hit the ground at t = %f seconds\n",t);
 51:     /* Set new initial conditions with .9 attenuation */
 52:     u[0] = 0.0;
 53:     u[1] = -0.9*u[1];
 54:     VecRestoreArray(U,&u);
 55:     TSSetSolution(ts,U);
 56:   } else if (event_list[0] == 1) {
 57:     PetscPrintf(PETSC_COMM_SELF,"Ball bounced %d times\n",app->nbounces);
 58:   }
 59:   app->nbounces++;
 60:   return(0);
 61: }

 65: /*
 66:      Defines the ODE passed to the ODE solver
 67: */
 68: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
 69: {
 70:   PetscErrorCode    ierr;
 71:   PetscScalar       *f;
 72:   const PetscScalar *u,*udot;

 75:   /*  The next three lines allow us to access the entries of the vectors directly */
 76:   VecGetArrayRead(U,&u);
 77:   VecGetArrayRead(Udot,&udot);
 78:   VecGetArray(F,&f);

 80:   f[0] = udot[0] - u[1];
 81:   f[1] = udot[1] + 9.8;

 83:   VecRestoreArrayRead(U,&u);
 84:   VecRestoreArrayRead(Udot,&udot);
 85:   VecRestoreArray(F,&f);
 86:   return(0);
 87: }

 91: /*
 92:      Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian.
 93: */
 94: static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat A,Mat B,void *ctx)
 95: {
 96:   PetscErrorCode    ierr;
 97:   PetscInt          rowcol[] = {0,1};
 98:   PetscScalar       J[2][2];
 99:   const PetscScalar *u,*udot;

102:   VecGetArrayRead(U,&u);
103:   VecGetArrayRead(Udot,&udot);

105:   J[0][0] = a;                       J[0][1] = -1;
106:   J[1][0] = 0.0;                     J[1][1] = a;
107:   MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES);

109:   VecRestoreArrayRead(U,&u);
110:   VecRestoreArrayRead(Udot,&udot);

112:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
113:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
114:   if (A != B) {
115:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
116:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
117:   }
118:   return(0);
119: }

123: int main(int argc,char **argv)
124: {
125:   TS             ts;            /* ODE integrator */
126:   Vec            U;             /* solution will be stored here */
127:   Mat            A;             /* Jacobian matrix */
129:   PetscMPIInt    size;
130:   PetscInt       n = 2;
131:   PetscScalar    *u;
132:   AppCtx         app;
133:   PetscInt       direction[2];
134:   PetscBool      terminate[2];

136:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137:      Initialize program
138:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
139:   PetscInitialize(&argc,&argv,(char*)0,help);
140:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
141:   if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");

143:   app.nbounces = 0;
144:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"ex21 options","");
145:   {
146:     app.maxbounces = 10;
147:     PetscOptionsInt("-maxbounces","","",app.maxbounces,&app.maxbounces,NULL);
148:   }
149:   PetscOptionsEnd();

151:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152:     Create necessary matrix and vectors
153:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154:   MatCreate(PETSC_COMM_WORLD,&A);
155:   MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
156:   MatSetType(A,MATDENSE);
157:   MatSetFromOptions(A);
158:   MatSetUp(A);

160:   MatCreateVecs(A,&U,NULL);

162:   VecGetArray(U,&u);
163:   u[0] = 0.0;
164:   u[1] = 20.0;
165:   VecRestoreArray(U,&u);

167:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168:      Create timestepping solver context
169:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
170:   TSCreate(PETSC_COMM_WORLD,&ts);
171:   TSSetSaveTrajectory(ts);
172:   TSSetProblemType(ts,TS_NONLINEAR);
173:   TSSetType(ts,TSROSW);
174:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,NULL);
175:   TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,NULL);

177:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
178:      Set initial conditions
179:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
180:   TSSetSolution(ts,U);

182:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183:      Set solver options
184:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
185:   TSSetDuration(ts,1000,30.0);
186:   TSSetInitialTimeStep(ts,0.0,0.1);
187:   TSSetFromOptions(ts);
188: 
189:   /* Set directions and terminate flags for the two events */
190:   direction[0] = -1;            direction[1] = -1;
191:   terminate[0] = PETSC_FALSE;   terminate[1] = PETSC_TRUE;
192:   TSSetEventMonitor(ts,2,direction,terminate,EventFunction,PostEventFunction,(void*)&app);

194:   TSAdapt adapt;
195:   TSGetAdapt(ts,&adapt);
196:   /* The adapative time step controller could take very large timesteps resulting in 
197:      the same event occuring multiple times in the same interval. A max. step 
198:      limit is enforced here to avoid this issue. 
199:   */
200:   TSAdaptSetStepLimits(adapt,0.0,0.5);
201:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202:      Run timestepping solver
203:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
204:     TSSolve(ts,U);

206:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
207:      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
208:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
209: 
210:   MatDestroy(&A);
211:   VecDestroy(&U);
212:   TSDestroy(&ts);
213: 
214:   PetscFinalize();
215:   return(0);
216: }