xref: /petsc/src/ts/impls/explicit/euler/euler.c (revision 70f55243aafb320636e2a54ff30cab5d1e8d3d7b)
18b1af7b3SBarry Smith 
24b33d51aSBarry Smith #ifndef lint
3*70f55243SBarry Smith static char vcid[] = "$Id: euler.c,v 1.3 1996/03/23 18:34:49 bsmith Exp bsmith $";
44b33d51aSBarry Smith #endif
54b33d51aSBarry Smith /*
68b1af7b3SBarry Smith        Code for Time Stepping with explicit Euler.
74b33d51aSBarry Smith */
84b33d51aSBarry Smith #include <math.h>
9*70f55243SBarry Smith #include "src/ts/tsimpl.h"                /*I   "ts.h"   I*/
104b33d51aSBarry Smith #include "pinclude/pviewer.h"
114b33d51aSBarry Smith 
124b33d51aSBarry Smith 
138b1af7b3SBarry Smith typedef struct {
148b1af7b3SBarry Smith   Vec update;     /* work vector where F(t[i],u[i]) is stored */
158b1af7b3SBarry Smith } TS_Euler;
168b1af7b3SBarry Smith 
178b1af7b3SBarry Smith static int TSSetUp_Euler(TS ts)
184b33d51aSBarry Smith {
198b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
204b33d51aSBarry Smith   int      ierr;
214b33d51aSBarry Smith 
228b1af7b3SBarry Smith   ierr = VecDuplicate(ts->vec_sol,&euler->update); CHKERRQ(ierr);
234b33d51aSBarry Smith   return 0;
244b33d51aSBarry Smith }
254b33d51aSBarry Smith 
26c3e30b67SBarry Smith static int TSStep_Euler(TS ts,int *steps,double *time)
274b33d51aSBarry Smith {
288b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
298b1af7b3SBarry Smith   Vec      sol = ts->vec_sol,update = euler->update;
308b1af7b3SBarry Smith   int      ierr,i,max_steps = ts->max_steps;
31c3e30b67SBarry Smith   Scalar   dt = ts->time_step;
324b33d51aSBarry Smith 
338b1af7b3SBarry Smith   *steps = -ts->steps;
34c3e30b67SBarry Smith   ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr);
354b33d51aSBarry Smith 
368b1af7b3SBarry Smith   for ( i=0; i<max_steps; i++ ) {
378b1af7b3SBarry Smith     ts->ptime += ts->time_step;
38c3e30b67SBarry Smith     ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update); CHKERRQ(ierr);
39c3e30b67SBarry Smith     ierr = VecAXPY(&dt,update,sol); CHKERRQ(ierr);
408b1af7b3SBarry Smith     ts->steps++;
41c3e30b67SBarry Smith     ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr);
428b1af7b3SBarry Smith     if (ts->ptime > ts->max_time) break;
438b1af7b3SBarry Smith   }
444b33d51aSBarry Smith 
458b1af7b3SBarry Smith   *steps += ts->steps;
468b1af7b3SBarry Smith   *time  = ts->ptime;
474b33d51aSBarry Smith   return 0;
484b33d51aSBarry Smith }
494b33d51aSBarry Smith /*------------------------------------------------------------*/
508b1af7b3SBarry Smith static int TSDestroy_Euler(PetscObject obj )
514b33d51aSBarry Smith {
524b33d51aSBarry Smith   TS       ts = (TS) obj;
538b1af7b3SBarry Smith   TS_Euler *euler = (TS_Euler*) ts->data;
548b1af7b3SBarry Smith 
558b1af7b3SBarry Smith   VecDestroy(euler->update);
568b1af7b3SBarry Smith   PetscFree(euler);
578b1af7b3SBarry Smith   return 0;
588b1af7b3SBarry Smith }
598b1af7b3SBarry Smith /*------------------------------------------------------------*/
608b1af7b3SBarry Smith 
618b1af7b3SBarry Smith static int TSSetFromOptions_Euler(TS ts)
628b1af7b3SBarry Smith {
634b33d51aSBarry Smith 
644b33d51aSBarry Smith   return 0;
654b33d51aSBarry Smith }
664b33d51aSBarry Smith 
678b1af7b3SBarry Smith static int TSPrintHelp_Euler(TS ts)
684b33d51aSBarry Smith {
698b1af7b3SBarry Smith 
708b1af7b3SBarry Smith   return 0;
718b1af7b3SBarry Smith }
728b1af7b3SBarry Smith 
738b1af7b3SBarry Smith static int TSView_Euler(PetscObject obj,Viewer viewer)
748b1af7b3SBarry Smith {
758b1af7b3SBarry Smith   return 0;
768b1af7b3SBarry Smith }
778b1af7b3SBarry Smith 
788b1af7b3SBarry Smith /* ------------------------------------------------------------ */
798b1af7b3SBarry Smith int TSCreate_Euler(TS ts )
808b1af7b3SBarry Smith {
818b1af7b3SBarry Smith   TS_Euler *euler;
828b1af7b3SBarry Smith 
83c3e30b67SBarry Smith   ts->type 	      = TS_EULER;
848b1af7b3SBarry Smith   ts->setup	      = TSSetUp_Euler;
858b1af7b3SBarry Smith   ts->step            = TSStep_Euler;
868b1af7b3SBarry Smith   ts->destroy         = TSDestroy_Euler;
878b1af7b3SBarry Smith   ts->printhelp       = TSPrintHelp_Euler;
888b1af7b3SBarry Smith   ts->setfromoptions  = TSSetFromOptions_Euler;
898b1af7b3SBarry Smith   ts->view            = TSView_Euler;
908b1af7b3SBarry Smith 
918b1af7b3SBarry Smith   euler    = PetscNew(TS_Euler); CHKPTRQ(euler);
928b1af7b3SBarry Smith   ts->data = (void *) euler;
934b33d51aSBarry Smith 
944b33d51aSBarry Smith   return 0;
954b33d51aSBarry Smith }
96c3e30b67SBarry Smith 
97c3e30b67SBarry Smith 
98c3e30b67SBarry Smith 
99c3e30b67SBarry Smith 
100c3e30b67SBarry Smith 
101