xref: /petsc/src/ts/impls/explicit/euler/euler.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*3a40ed3dSBarry Smith static char vcid[] = "$Id: euler.c,v 1.10 1997/07/09 20:58:26 balay 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 
23*3a40ed3dSBarry Smith   PetscFunctionBegin;
248b1af7b3SBarry Smith   ierr = VecDuplicate(ts->vec_sol,&euler->update); CHKERRQ(ierr);
25*3a40ed3dSBarry 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 
37*3a40ed3dSBarry 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;
52*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
534b33d51aSBarry Smith }
544b33d51aSBarry Smith /*------------------------------------------------------------*/
5558562591SBarry Smith #undef __FUNC__
5658562591SBarry Smith #define __FUNC__ "TSDestroy_Euler"
578b1af7b3SBarry Smith static int TSDestroy_Euler(PetscObject obj )
584b33d51aSBarry Smith {
594b33d51aSBarry Smith   TS       ts = (TS) obj;
608b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
618b1af7b3SBarry Smith 
62*3a40ed3dSBarry Smith   PetscFunctionBegin;
638b1af7b3SBarry Smith   VecDestroy(euler->update);
648b1af7b3SBarry Smith   PetscFree(euler);
65*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
668b1af7b3SBarry Smith }
678b1af7b3SBarry Smith /*------------------------------------------------------------*/
688b1af7b3SBarry Smith 
6958562591SBarry Smith #undef __FUNC__
7058562591SBarry Smith #define __FUNC__ "TSSetFromOptions_Euler"
718b1af7b3SBarry Smith static int TSSetFromOptions_Euler(TS ts)
728b1af7b3SBarry Smith {
73*3a40ed3dSBarry Smith   PetscFunctionBegin;
74*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
754b33d51aSBarry Smith }
764b33d51aSBarry Smith 
7758562591SBarry Smith #undef __FUNC__
7858562591SBarry Smith #define __FUNC__ "TSPrintHelp_Euler"
79ca4b7087SBarry Smith static int TSPrintHelp_Euler(TS ts,char *p)
804b33d51aSBarry Smith {
81*3a40ed3dSBarry Smith   PetscFunctionBegin;
82*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
838b1af7b3SBarry Smith }
848b1af7b3SBarry Smith 
8558562591SBarry Smith #undef __FUNC__
8658562591SBarry Smith #define __FUNC__ "TSView_Euler"
878b1af7b3SBarry Smith static int TSView_Euler(PetscObject obj,Viewer viewer)
888b1af7b3SBarry Smith {
89*3a40ed3dSBarry Smith   PetscFunctionBegin;
90*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
918b1af7b3SBarry Smith }
928b1af7b3SBarry Smith 
938b1af7b3SBarry Smith /* ------------------------------------------------------------ */
9458562591SBarry Smith #undef __FUNC__
9558562591SBarry Smith #define __FUNC__ "TSCreate_Euler"
968b1af7b3SBarry Smith int TSCreate_Euler(TS ts )
978b1af7b3SBarry Smith {
988b1af7b3SBarry Smith   TS_Euler *euler;
998b1af7b3SBarry Smith 
100*3a40ed3dSBarry Smith   PetscFunctionBegin;
101c3e30b67SBarry Smith   ts->type 	      = TS_EULER;
1028b1af7b3SBarry Smith   ts->setup	      = TSSetUp_Euler;
1038b1af7b3SBarry Smith   ts->step            = TSStep_Euler;
1048b1af7b3SBarry Smith   ts->destroy         = TSDestroy_Euler;
1058b1af7b3SBarry Smith   ts->printhelp       = TSPrintHelp_Euler;
1068b1af7b3SBarry Smith   ts->setfromoptions  = TSSetFromOptions_Euler;
1078b1af7b3SBarry Smith   ts->view            = TSView_Euler;
1088b1af7b3SBarry Smith 
1098b1af7b3SBarry Smith   euler    = PetscNew(TS_Euler); CHKPTRQ(euler);
110eed86810SBarry Smith   PLogObjectMemory(ts,sizeof(TS_Euler));
1118b1af7b3SBarry Smith   ts->data = (void *) euler;
1124b33d51aSBarry Smith 
113*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
1144b33d51aSBarry Smith }
115c3e30b67SBarry Smith 
116c3e30b67SBarry Smith 
117c3e30b67SBarry Smith 
118c3e30b67SBarry Smith 
119c3e30b67SBarry Smith 
120