Actual source code: ex6.c

  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
  8: */

 10: /*
 11:    f(U,V) = U + V
 12: */
 13: PetscErrorCode f(PetscReal t,Vec U,Vec V,Vec F)
 14: {

 18:   VecWAXPY(F,1.0,U,V);
 19:   return(0);
 20: }

 22: /*
 23:    F(U,V) = U - V
 24: */
 25: PetscErrorCode F(PetscReal t,Vec U,Vec V,Vec F)
 26: {

 30:   VecWAXPY(F,-1.0,V,U);
 31:   return(0);
 32: }

 34: typedef struct {
 35:   PetscReal      t;
 36:   SNES           snes;
 37:   Vec            U,V;
 38:   PetscErrorCode (*f)(PetscReal,Vec,Vec,Vec);
 39:   PetscErrorCode (*F)(PetscReal,Vec,Vec,Vec);
 40: } AppCtx;

 42: extern PetscErrorCode TSFunction(TS,PetscReal,Vec,Vec,void*);
 43: extern PetscErrorCode SNESFunction(SNES,Vec,Vec,void*);

 45: int main(int argc,char **argv)
 46: {
 48:   AppCtx         ctx;
 49:   TS             ts;
 50:   Vec            tsrhs,U;

 52:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 53:   TSCreate(PETSC_COMM_WORLD,&ts);
 54:   TSSetProblemType(ts,TS_NONLINEAR);
 55:   TSSetType(ts,TSEULER);
 56:   TSSetFromOptions(ts);
 57:   VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&tsrhs);
 58:   VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&U);
 59:   TSSetRHSFunction(ts,tsrhs,TSFunction,&ctx);
 60:   TSSetMaxTime(ts,1.0);
 61:   ctx.f = f;

 63:   SNESCreate(PETSC_COMM_WORLD,&ctx.snes);
 64:   SNESSetFromOptions(ctx.snes);
 65:   SNESSetFunction(ctx.snes,NULL,SNESFunction,&ctx);
 66:   SNESSetJacobian(ctx.snes,NULL,NULL,SNESComputeJacobianDefault,&ctx);
 67:   ctx.F = F;
 68:   VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,1,&ctx.V);

 70:   VecSet(U,1.0);
 71:   TSSolve(ts,U);

 73:   VecDestroy(&ctx.V);
 74:   VecDestroy(&tsrhs);
 75:   VecDestroy(&U);
 76:   SNESDestroy(&ctx.snes);
 77:   TSDestroy(&ts);
 78:   PetscFinalize();
 79:   return ierr;
 80: }

 82: /*
 83:    Defines the RHS function that is passed to the time-integrator.

 85:    Solves F(U,V) for V and then computes f(U,V)
 86: */
 87: PetscErrorCode TSFunction(TS ts,PetscReal t,Vec U,Vec F,void *actx)
 88: {
 89:   AppCtx         *ctx = (AppCtx*)actx;

 93:   ctx->t = t;
 94:   ctx->U = U;
 95:   SNESSolve(ctx->snes,NULL,ctx->V);
 96:   (*ctx->f)(t,U,ctx->V,F);
 97:   return(0);
 98: }

100: /*
101:    Defines the nonlinear function that is passed to the nonlinear solver
102: */
103: PetscErrorCode SNESFunction(SNES snes,Vec V,Vec F,void *actx)
104: {
105:   AppCtx         *ctx = (AppCtx*)actx;

109:   (*ctx->F)(ctx->t,ctx->U,V,F);
110:   return(0);
111: }

113: /*TEST

115:     test:
116:       args:  -ts_monitor -ts_view

118: TEST*/