18b1af7b3SBarry Smith 24b33d51aSBarry Smith #ifndef lint 3*58562591SBarry Smith static char vcid[] = "$Id: euler.c,v 1.6 1996/12/18 17:46:07 balay Exp bsmith $"; 44b33d51aSBarry Smith #endif 54b33d51aSBarry Smith /* 6fb4a63b6SLois Curfman McInnes Code for Timestepping with explicit Euler. 74b33d51aSBarry Smith */ 84b33d51aSBarry Smith #include <math.h> 970f55243SBarry 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 17*58562591SBarry Smith #undef __FUNC__ 18*58562591SBarry Smith #define __FUNC__ "TSSetUp_Euler" 198b1af7b3SBarry Smith static int TSSetUp_Euler(TS ts) 204b33d51aSBarry Smith { 218b1af7b3SBarry Smith TS_Euler *euler = (TS_Euler*) ts->data; 224b33d51aSBarry Smith int ierr; 234b33d51aSBarry Smith 248b1af7b3SBarry Smith ierr = VecDuplicate(ts->vec_sol,&euler->update); CHKERRQ(ierr); 254b33d51aSBarry Smith return 0; 264b33d51aSBarry Smith } 274b33d51aSBarry Smith 28*58562591SBarry Smith #undef __FUNC__ 29*58562591SBarry 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 378b1af7b3SBarry Smith *steps = -ts->steps; 38c3e30b67SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr); 394b33d51aSBarry Smith 408b1af7b3SBarry Smith for ( i=0; i<max_steps; i++ ) { 418b1af7b3SBarry Smith ts->ptime += ts->time_step; 42c3e30b67SBarry Smith ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update); CHKERRQ(ierr); 43c3e30b67SBarry Smith ierr = VecAXPY(&dt,update,sol); CHKERRQ(ierr); 448b1af7b3SBarry Smith ts->steps++; 45c3e30b67SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr); 468b1af7b3SBarry Smith if (ts->ptime > ts->max_time) break; 478b1af7b3SBarry Smith } 484b33d51aSBarry Smith 498b1af7b3SBarry Smith *steps += ts->steps; 508b1af7b3SBarry Smith *time = ts->ptime; 514b33d51aSBarry Smith return 0; 524b33d51aSBarry Smith } 534b33d51aSBarry Smith /*------------------------------------------------------------*/ 54*58562591SBarry Smith #undef __FUNC__ 55*58562591SBarry Smith #define __FUNC__ "TSDestroy_Euler" 568b1af7b3SBarry Smith static int TSDestroy_Euler(PetscObject obj ) 574b33d51aSBarry Smith { 584b33d51aSBarry Smith TS ts = (TS) obj; 598b1af7b3SBarry Smith TS_Euler *euler = (TS_Euler*) ts->data; 608b1af7b3SBarry Smith 618b1af7b3SBarry Smith VecDestroy(euler->update); 628b1af7b3SBarry Smith PetscFree(euler); 638b1af7b3SBarry Smith return 0; 648b1af7b3SBarry Smith } 658b1af7b3SBarry Smith /*------------------------------------------------------------*/ 668b1af7b3SBarry Smith 67*58562591SBarry Smith #undef __FUNC__ 68*58562591SBarry Smith #define __FUNC__ "TSSetFromOptions_Euler" 698b1af7b3SBarry Smith static int TSSetFromOptions_Euler(TS ts) 708b1af7b3SBarry Smith { 714b33d51aSBarry Smith 724b33d51aSBarry Smith return 0; 734b33d51aSBarry Smith } 744b33d51aSBarry Smith 75*58562591SBarry Smith #undef __FUNC__ 76*58562591SBarry Smith #define __FUNC__ "TSPrintHelp_Euler" 778b1af7b3SBarry Smith static int TSPrintHelp_Euler(TS ts) 784b33d51aSBarry Smith { 798b1af7b3SBarry Smith 808b1af7b3SBarry Smith return 0; 818b1af7b3SBarry Smith } 828b1af7b3SBarry Smith 83*58562591SBarry Smith #undef __FUNC__ 84*58562591SBarry Smith #define __FUNC__ "TSView_Euler" 858b1af7b3SBarry Smith static int TSView_Euler(PetscObject obj,Viewer viewer) 868b1af7b3SBarry Smith { 878b1af7b3SBarry Smith return 0; 888b1af7b3SBarry Smith } 898b1af7b3SBarry Smith 908b1af7b3SBarry Smith /* ------------------------------------------------------------ */ 91*58562591SBarry Smith #undef __FUNC__ 92*58562591SBarry Smith #define __FUNC__ "TSCreate_Euler" 938b1af7b3SBarry Smith int TSCreate_Euler(TS ts ) 948b1af7b3SBarry Smith { 958b1af7b3SBarry Smith TS_Euler *euler; 968b1af7b3SBarry Smith 97c3e30b67SBarry Smith ts->type = TS_EULER; 988b1af7b3SBarry Smith ts->setup = TSSetUp_Euler; 998b1af7b3SBarry Smith ts->step = TSStep_Euler; 1008b1af7b3SBarry Smith ts->destroy = TSDestroy_Euler; 1018b1af7b3SBarry Smith ts->printhelp = TSPrintHelp_Euler; 1028b1af7b3SBarry Smith ts->setfromoptions = TSSetFromOptions_Euler; 1038b1af7b3SBarry Smith ts->view = TSView_Euler; 1048b1af7b3SBarry Smith 1058b1af7b3SBarry Smith euler = PetscNew(TS_Euler); CHKPTRQ(euler); 1068b1af7b3SBarry Smith ts->data = (void *) euler; 1074b33d51aSBarry Smith 1084b33d51aSBarry Smith return 0; 1094b33d51aSBarry Smith } 110c3e30b67SBarry Smith 111c3e30b67SBarry Smith 112c3e30b67SBarry Smith 113c3e30b67SBarry Smith 114c3e30b67SBarry Smith 115