xref: /petsc/src/ts/impls/explicit/euler/euler.c (revision e1311b9049e89cb3452dcd306fde571f4b440ff2)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*e1311b90SBarry Smith static char vcid[] = "$Id: euler.c,v 1.12 1998/03/06 00:17:32 bsmith Exp bsmith $";
34b33d51aSBarry Smith #endif
44b33d51aSBarry Smith /*
5fb4a63b6SLois Curfman McInnes        Code for Timestepping with explicit Euler.
64b33d51aSBarry Smith */
74b33d51aSBarry Smith #include <math.h>
870f55243SBarry Smith #include "src/ts/tsimpl.h"                /*I   "ts.h"   I*/
94b33d51aSBarry Smith #include "pinclude/pviewer.h"
104b33d51aSBarry Smith 
114b33d51aSBarry Smith 
128b1af7b3SBarry Smith typedef struct {
138b1af7b3SBarry Smith   Vec update;     /* work vector where F(t[i],u[i]) is stored */
148b1af7b3SBarry Smith } TS_Euler;
158b1af7b3SBarry Smith 
1658562591SBarry Smith #undef __FUNC__
1758562591SBarry Smith #define __FUNC__ "TSSetUp_Euler"
188b1af7b3SBarry Smith static int TSSetUp_Euler(TS ts)
194b33d51aSBarry Smith {
208b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
214b33d51aSBarry Smith   int      ierr;
224b33d51aSBarry Smith 
233a40ed3dSBarry Smith   PetscFunctionBegin;
248b1af7b3SBarry Smith   ierr = VecDuplicate(ts->vec_sol,&euler->update); CHKERRQ(ierr);
253a40ed3dSBarry Smith   PetscFunctionReturn(0);
264b33d51aSBarry Smith }
274b33d51aSBarry Smith 
2858562591SBarry Smith #undef __FUNC__
2958562591SBarry Smith #define __FUNC__ "TSStep_Euler"
30c3e30b67SBarry Smith static int TSStep_Euler(TS ts,int *steps,double *time)
314b33d51aSBarry Smith {
328b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
338b1af7b3SBarry Smith   Vec      sol = ts->vec_sol,update = euler->update;
348b1af7b3SBarry Smith   int      ierr,i,max_steps = ts->max_steps;
35c3e30b67SBarry Smith   Scalar   dt = ts->time_step;
364b33d51aSBarry Smith 
373a40ed3dSBarry Smith   PetscFunctionBegin;
388b1af7b3SBarry Smith   *steps = -ts->steps;
39c3e30b67SBarry Smith   ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr);
404b33d51aSBarry Smith 
418b1af7b3SBarry Smith   for ( i=0; i<max_steps; i++ ) {
428b1af7b3SBarry Smith     ts->ptime += ts->time_step;
43c3e30b67SBarry Smith     ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update); CHKERRQ(ierr);
44c3e30b67SBarry Smith     ierr = VecAXPY(&dt,update,sol); CHKERRQ(ierr);
458b1af7b3SBarry Smith     ts->steps++;
46c3e30b67SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr);
478b1af7b3SBarry Smith     if (ts->ptime > ts->max_time) break;
488b1af7b3SBarry Smith   }
494b33d51aSBarry Smith 
508b1af7b3SBarry Smith   *steps += ts->steps;
518b1af7b3SBarry Smith   *time  = ts->ptime;
523a40ed3dSBarry Smith   PetscFunctionReturn(0);
534b33d51aSBarry Smith }
544b33d51aSBarry Smith /*------------------------------------------------------------*/
5558562591SBarry Smith #undef __FUNC__
5658562591SBarry Smith #define __FUNC__ "TSDestroy_Euler"
57*e1311b90SBarry Smith static int TSDestroy_Euler(TS ts)
584b33d51aSBarry Smith {
598b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
608b1af7b3SBarry Smith 
613a40ed3dSBarry Smith   PetscFunctionBegin;
628b1af7b3SBarry Smith   VecDestroy(euler->update);
638b1af7b3SBarry Smith   PetscFree(euler);
643a40ed3dSBarry Smith   PetscFunctionReturn(0);
658b1af7b3SBarry Smith }
668b1af7b3SBarry Smith /*------------------------------------------------------------*/
678b1af7b3SBarry Smith 
6858562591SBarry Smith #undef __FUNC__
6958562591SBarry Smith #define __FUNC__ "TSSetFromOptions_Euler"
708b1af7b3SBarry Smith static int TSSetFromOptions_Euler(TS ts)
718b1af7b3SBarry Smith {
723a40ed3dSBarry Smith   PetscFunctionBegin;
733a40ed3dSBarry Smith   PetscFunctionReturn(0);
744b33d51aSBarry Smith }
754b33d51aSBarry Smith 
7658562591SBarry Smith #undef __FUNC__
7758562591SBarry Smith #define __FUNC__ "TSPrintHelp_Euler"
78ca4b7087SBarry Smith static int TSPrintHelp_Euler(TS ts,char *p)
794b33d51aSBarry Smith {
803a40ed3dSBarry Smith   PetscFunctionBegin;
813a40ed3dSBarry Smith   PetscFunctionReturn(0);
828b1af7b3SBarry Smith }
838b1af7b3SBarry Smith 
8458562591SBarry Smith #undef __FUNC__
8558562591SBarry Smith #define __FUNC__ "TSView_Euler"
86*e1311b90SBarry Smith static int TSView_Euler(TS ts,Viewer viewer)
878b1af7b3SBarry Smith {
883a40ed3dSBarry Smith   PetscFunctionBegin;
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
908b1af7b3SBarry Smith }
918b1af7b3SBarry Smith 
928b1af7b3SBarry Smith /* ------------------------------------------------------------ */
9358562591SBarry Smith #undef __FUNC__
9458562591SBarry Smith #define __FUNC__ "TSCreate_Euler"
958b1af7b3SBarry Smith int TSCreate_Euler(TS ts )
968b1af7b3SBarry Smith {
978b1af7b3SBarry Smith   TS_Euler *euler;
988b1af7b3SBarry Smith 
993a40ed3dSBarry Smith   PetscFunctionBegin;
1008b1af7b3SBarry Smith   ts->setup	      = TSSetUp_Euler;
1018b1af7b3SBarry Smith   ts->step            = TSStep_Euler;
1028b1af7b3SBarry Smith   ts->destroy         = TSDestroy_Euler;
1038b1af7b3SBarry Smith   ts->printhelp       = TSPrintHelp_Euler;
1048b1af7b3SBarry Smith   ts->setfromoptions  = TSSetFromOptions_Euler;
1058b1af7b3SBarry Smith   ts->view            = TSView_Euler;
1068b1af7b3SBarry Smith 
1078b1af7b3SBarry Smith   euler    = PetscNew(TS_Euler); CHKPTRQ(euler);
108eed86810SBarry Smith   PLogObjectMemory(ts,sizeof(TS_Euler));
1098b1af7b3SBarry Smith   ts->data = (void *) euler;
1104b33d51aSBarry Smith 
1113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1124b33d51aSBarry Smith }
113c3e30b67SBarry Smith 
114c3e30b67SBarry Smith 
115c3e30b67SBarry Smith 
116c3e30b67SBarry Smith 
117c3e30b67SBarry Smith 
118