xref: /petsc/src/ts/impls/explicit/euler/euler.c (revision 606d414c6e034e02e67059b83ebaefc3ebe99698)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*606d414cSSatish Balay static char vcid[] = "$Id: euler.c,v 1.16 1999/05/04 20:36:43 balay Exp balay $";
34b33d51aSBarry Smith #endif
44b33d51aSBarry Smith /*
5fb4a63b6SLois Curfman McInnes        Code for Timestepping with explicit Euler.
64b33d51aSBarry Smith */
770f55243SBarry Smith #include "src/ts/tsimpl.h"                /*I   "ts.h"   I*/
84b33d51aSBarry Smith 
98b1af7b3SBarry Smith typedef struct {
108b1af7b3SBarry Smith   Vec update;     /* work vector where F(t[i],u[i]) is stored */
118b1af7b3SBarry Smith } TS_Euler;
128b1af7b3SBarry Smith 
1358562591SBarry Smith #undef __FUNC__
1458562591SBarry Smith #define __FUNC__ "TSSetUp_Euler"
158b1af7b3SBarry Smith static int TSSetUp_Euler(TS ts)
164b33d51aSBarry Smith {
178b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
184b33d51aSBarry Smith   int      ierr;
194b33d51aSBarry Smith 
203a40ed3dSBarry Smith   PetscFunctionBegin;
218b1af7b3SBarry Smith   ierr = VecDuplicate(ts->vec_sol,&euler->update);CHKERRQ(ierr);
223a40ed3dSBarry Smith   PetscFunctionReturn(0);
234b33d51aSBarry Smith }
244b33d51aSBarry Smith 
2558562591SBarry Smith #undef __FUNC__
2658562591SBarry Smith #define __FUNC__ "TSStep_Euler"
27c3e30b67SBarry Smith static int TSStep_Euler(TS ts,int *steps,double *time)
284b33d51aSBarry Smith {
298b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
308b1af7b3SBarry Smith   Vec      sol = ts->vec_sol,update = euler->update;
318b1af7b3SBarry Smith   int      ierr,i,max_steps = ts->max_steps;
32c3e30b67SBarry Smith   Scalar   dt = ts->time_step;
334b33d51aSBarry Smith 
343a40ed3dSBarry Smith   PetscFunctionBegin;
358b1af7b3SBarry Smith   *steps = -ts->steps;
36c3e30b67SBarry Smith   ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr);
374b33d51aSBarry Smith 
388b1af7b3SBarry Smith   for ( i=0; i<max_steps; i++ ) {
398b1af7b3SBarry Smith     ts->ptime += ts->time_step;
40c3e30b67SBarry Smith     ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update);CHKERRQ(ierr);
41c3e30b67SBarry Smith     ierr = VecAXPY(&dt,update,sol);CHKERRQ(ierr);
428b1af7b3SBarry Smith     ts->steps++;
43c3e30b67SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr);
448b1af7b3SBarry Smith     if (ts->ptime > ts->max_time) break;
458b1af7b3SBarry Smith   }
464b33d51aSBarry Smith 
478b1af7b3SBarry Smith   *steps += ts->steps;
488b1af7b3SBarry Smith   *time  = ts->ptime;
493a40ed3dSBarry Smith   PetscFunctionReturn(0);
504b33d51aSBarry Smith }
514b33d51aSBarry Smith /*------------------------------------------------------------*/
5258562591SBarry Smith #undef __FUNC__
5358562591SBarry Smith #define __FUNC__ "TSDestroy_Euler"
54e1311b90SBarry Smith static int TSDestroy_Euler(TS ts)
554b33d51aSBarry Smith {
568b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
57*606d414cSSatish Balay   int      ierr;
588b1af7b3SBarry Smith 
593a40ed3dSBarry Smith   PetscFunctionBegin;
608b1af7b3SBarry Smith   VecDestroy(euler->update);
61*606d414cSSatish Balay   ierr = PetscFree(euler);CHKERRQ(ierr);
623a40ed3dSBarry Smith   PetscFunctionReturn(0);
638b1af7b3SBarry Smith }
648b1af7b3SBarry Smith /*------------------------------------------------------------*/
658b1af7b3SBarry Smith 
6658562591SBarry Smith #undef __FUNC__
6758562591SBarry Smith #define __FUNC__ "TSSetFromOptions_Euler"
688b1af7b3SBarry Smith static int TSSetFromOptions_Euler(TS ts)
698b1af7b3SBarry Smith {
703a40ed3dSBarry Smith   PetscFunctionBegin;
713a40ed3dSBarry Smith   PetscFunctionReturn(0);
724b33d51aSBarry Smith }
734b33d51aSBarry Smith 
7458562591SBarry Smith #undef __FUNC__
7558562591SBarry Smith #define __FUNC__ "TSPrintHelp_Euler"
76ca4b7087SBarry Smith static int TSPrintHelp_Euler(TS ts,char *p)
774b33d51aSBarry Smith {
783a40ed3dSBarry Smith   PetscFunctionBegin;
793a40ed3dSBarry Smith   PetscFunctionReturn(0);
808b1af7b3SBarry Smith }
818b1af7b3SBarry Smith 
8258562591SBarry Smith #undef __FUNC__
8358562591SBarry Smith #define __FUNC__ "TSView_Euler"
84e1311b90SBarry Smith static int TSView_Euler(TS ts,Viewer viewer)
858b1af7b3SBarry Smith {
863a40ed3dSBarry Smith   PetscFunctionBegin;
873a40ed3dSBarry Smith   PetscFunctionReturn(0);
888b1af7b3SBarry Smith }
898b1af7b3SBarry Smith 
908b1af7b3SBarry Smith /* ------------------------------------------------------------ */
91fb2e594dSBarry Smith EXTERN_C_BEGIN
9258562591SBarry Smith #undef __FUNC__
9358562591SBarry Smith #define __FUNC__ "TSCreate_Euler"
948b1af7b3SBarry Smith int TSCreate_Euler(TS ts )
958b1af7b3SBarry Smith {
968b1af7b3SBarry Smith   TS_Euler *euler;
978b1af7b3SBarry Smith 
983a40ed3dSBarry Smith   PetscFunctionBegin;
998b1af7b3SBarry Smith   ts->setup	      = TSSetUp_Euler;
1008b1af7b3SBarry Smith   ts->step            = TSStep_Euler;
1018b1af7b3SBarry Smith   ts->destroy         = TSDestroy_Euler;
1028b1af7b3SBarry Smith   ts->printhelp       = TSPrintHelp_Euler;
1038b1af7b3SBarry Smith   ts->setfromoptions  = TSSetFromOptions_Euler;
1048b1af7b3SBarry Smith   ts->view            = TSView_Euler;
1058b1af7b3SBarry Smith 
1068b1af7b3SBarry Smith   euler    = PetscNew(TS_Euler);CHKPTRQ(euler);
107eed86810SBarry Smith   PLogObjectMemory(ts,sizeof(TS_Euler));
1088b1af7b3SBarry Smith   ts->data = (void *) euler;
1094b33d51aSBarry Smith 
1103a40ed3dSBarry Smith   PetscFunctionReturn(0);
1114b33d51aSBarry Smith }
112fb2e594dSBarry Smith EXTERN_C_END
113c3e30b67SBarry Smith 
114c3e30b67SBarry Smith 
115c3e30b67SBarry Smith 
116c3e30b67SBarry Smith 
117