Actual source code: ex8.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1: static char help[] = "Solves DAE with integrator only on non-algebraic terms \n";

  3:  #include <petscts.h>

  5: /*
  6:         \dot{U} = f(U,V)
  7:         F(U,V)  = 0

  9:     Same as ex6.c and ex7.c except calls the ARKIMEX integrator on the entire DAE
 10: */


 13: /*
 14:    f(U,V) = U + V

 16: */
 17: PetscErrorCode f(PetscReal t,Vec UV,Vec F)
 18: {
 19:   PetscErrorCode    ierr;
 20:   const PetscScalar *u,*v;
 21:   PetscScalar       *f;
 22:   PetscInt          n,i;

 25:   VecGetLocalSize(UV,&n);
 26:   n    = n/2;
 27:   VecGetArrayRead(UV,&u);
 28:   v    = u + n;
 29:   VecGetArray(F,&f);
 30:   for (i=0; i<n; i++) f[i] = u[i] + v[i];
 31:   VecRestoreArrayRead(UV,&u);
 32:   VecRestoreArray(F,&f);
 33:   return(0);
 34: }

 36: /*
 37:    F(U,V) = U - V

 39: */
 40: PetscErrorCode F(PetscReal t,Vec UV,Vec F)
 41: {
 42:   PetscErrorCode    ierr;
 43:   const PetscScalar *u,*v;
 44:   PetscScalar       *f;
 45:   PetscInt          n,i;

 48:   VecGetLocalSize(UV,&n);
 49:   n    = n/2;
 50:   VecGetArrayRead(UV,&u);
 51:   v    = u + n;
 52:   VecGetArray(F,&f);
 53:   f    = f + n;
 54:   for (i=0; i<n; i++) f[i] = u[i] - v[i];
 55:   f    = f - n;
 56:   VecRestoreArrayRead(UV,&u);
 57:   VecRestoreArray(F,&f);
 58:   return(0);
 59: }

 61: typedef struct {
 62:   PetscErrorCode (*f)(PetscReal,Vec,Vec);
 63:   PetscErrorCode (*F)(PetscReal,Vec,Vec);
 64: } AppCtx;

 66: extern PetscErrorCode TSFunctionRHS(TS,PetscReal,Vec,Vec,void*);
 67: extern PetscErrorCode TSFunctionI(TS,PetscReal,Vec,Vec,Vec,void*);

 69: int main(int argc,char **argv)
 70: {
 72:   AppCtx         ctx;
 73:   TS             ts;
 74:   Vec            tsrhs,UV;

 76:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 77:   TSCreate(PETSC_COMM_WORLD,&ts);
 78:   TSSetProblemType(ts,TS_NONLINEAR);
 79:   TSSetType(ts,TSROSW);
 80:   TSSetFromOptions(ts);
 81:   VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&tsrhs);
 82:   VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&UV);
 83:   TSSetRHSFunction(ts,tsrhs,TSFunctionRHS,&ctx);
 84:   TSSetIFunction(ts,NULL,TSFunctionI,&ctx);
 85:   ctx.f = f;
 86:   ctx.F = F;

 88:   VecSet(UV,1.0);
 89:   TSSolve(ts,UV);
 90:   VecDestroy(&tsrhs);
 91:   VecDestroy(&UV);
 92:   TSDestroy(&ts);
 93:   PetscFinalize();
 94:   return ierr;
 95: }

 97: /*
 98:    Defines the RHS function that is passed to the time-integrator. 



102: */
103: PetscErrorCode TSFunctionRHS(TS ts,PetscReal t,Vec UV,Vec F,void *actx)
104: {
105:   AppCtx         *ctx = (AppCtx*)actx;

109:   VecSet(F,0.0);
110:   (*ctx->f)(t,UV,F);
111:   return(0);
112: }

114: /*
115:    Defines the nonlinear function that is passed to the time-integrator

117: */
118: PetscErrorCode TSFunctionI(TS ts,PetscReal t,Vec UV,Vec UVdot,Vec F,void *actx)
119: {
120:   AppCtx         *ctx = (AppCtx*)actx;

124:   VecCopy(UVdot,F);
125:   (*ctx->F)(t,UV,F);
126:   return(0);
127: }