Actual source code: euler.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  1: /*
  2:        Code for Timestepping with explicit Euler.
  3: */
  4: #include <petsc/private/tsimpl.h>                /*I   "petscts.h"   I*/

  6: typedef struct {
  7:   Vec update;     /* work vector where new solution is formed  */
  8: } TS_Euler;

 12: static PetscErrorCode TSStep_Euler(TS ts)
 13: {
 14:   TS_Euler       *euler = (TS_Euler*)ts->data;
 15:   Vec            sol    = ts->vec_sol,update = euler->update;

 19:   TSPreStep(ts);
 20:   TSPreStage(ts,ts->ptime);
 21:   TSComputeRHSFunction(ts,ts->ptime,sol,update);
 22:   VecAXPY(sol,ts->time_step,update);
 23:   TSPostStage(ts,ts->ptime,0,&sol);
 24:   ts->ptime += ts->time_step;
 25:   ts->steps++;
 26:   return(0);
 27: }
 28: /*------------------------------------------------------------*/

 32: static PetscErrorCode TSSetUp_Euler(TS ts)
 33: {
 34:   TS_Euler       *euler = (TS_Euler*)ts->data;

 38:   VecDuplicate(ts->vec_sol,&euler->update);
 39:   return(0);
 40: }

 44: static PetscErrorCode TSReset_Euler(TS ts)
 45: {
 46:   TS_Euler       *euler = (TS_Euler*)ts->data;

 50:   VecDestroy(&euler->update);
 51:   return(0);
 52: }

 56: static PetscErrorCode TSDestroy_Euler(TS ts)
 57: {

 61:   TSReset_Euler(ts);
 62:   PetscFree(ts->data);
 63:   return(0);
 64: }
 65: /*------------------------------------------------------------*/

 69: static PetscErrorCode TSSetFromOptions_Euler(PetscOptions *PetscOptionsObject,TS ts)
 70: {
 72:   return(0);
 73: }

 77: static PetscErrorCode TSView_Euler(TS ts,PetscViewer viewer)
 78: {
 80:   return(0);
 81: }

 85: static PetscErrorCode TSInterpolate_Euler(TS ts,PetscReal t,Vec X)
 86: {
 87:   TS_Euler       *euler = (TS_Euler*)ts->data;
 88:   Vec            update = euler->update;
 89:   PetscReal      alpha = (ts->ptime - t)/ts->time_step;

 93:   VecWAXPY(X,-ts->time_step,update,ts->vec_sol);
 94:   VecAXPBY(X,1.0-alpha,alpha,ts->vec_sol);
 95:   return(0);
 96: }

100: PetscErrorCode TSComputeLinearStability_Euler(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
101: {
103:   *yr = 1.0 + xr;
104:   *yi = xi;
105:   return(0);
106: }
107: /* ------------------------------------------------------------ */

109: /*MC
110:       TSEULER - ODE solver using the explicit forward Euler method

112:   Level: beginner

114: .seealso:  TSCreate(), TS, TSSetType(), TSBEULER

116: M*/
119: PETSC_EXTERN PetscErrorCode TSCreate_Euler(TS ts)
120: {
121:   TS_Euler       *euler;

125:   ts->ops->setup           = TSSetUp_Euler;
126:   ts->ops->step            = TSStep_Euler;
127:   ts->ops->reset           = TSReset_Euler;
128:   ts->ops->destroy         = TSDestroy_Euler;
129:   ts->ops->setfromoptions  = TSSetFromOptions_Euler;
130:   ts->ops->view            = TSView_Euler;
131:   ts->ops->interpolate     = TSInterpolate_Euler;
132:   ts->ops->linearstability = TSComputeLinearStability_Euler;

134:   PetscNewLog(ts,&euler);
135:   ts->data = (void*)euler;
136:   return(0);
137: }