1*a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*a5eb4965SSatish Balay static char vcid[] = "$Id: euler.c,v 1.9 1997/06/05 12:56:16 bsmith Exp balay $"; 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 238b1af7b3SBarry Smith ierr = VecDuplicate(ts->vec_sol,&euler->update); CHKERRQ(ierr); 244b33d51aSBarry Smith return 0; 254b33d51aSBarry Smith } 264b33d51aSBarry Smith 2758562591SBarry Smith #undef __FUNC__ 2858562591SBarry Smith #define __FUNC__ "TSStep_Euler" 29c3e30b67SBarry Smith static int TSStep_Euler(TS ts,int *steps,double *time) 304b33d51aSBarry Smith { 318b1af7b3SBarry Smith TS_Euler *euler = (TS_Euler*) ts->data; 328b1af7b3SBarry Smith Vec sol = ts->vec_sol,update = euler->update; 338b1af7b3SBarry Smith int ierr,i,max_steps = ts->max_steps; 34c3e30b67SBarry Smith Scalar dt = ts->time_step; 354b33d51aSBarry Smith 368b1af7b3SBarry Smith *steps = -ts->steps; 37c3e30b67SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr); 384b33d51aSBarry Smith 398b1af7b3SBarry Smith for ( i=0; i<max_steps; i++ ) { 408b1af7b3SBarry Smith ts->ptime += ts->time_step; 41c3e30b67SBarry Smith ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update); CHKERRQ(ierr); 42c3e30b67SBarry Smith ierr = VecAXPY(&dt,update,sol); CHKERRQ(ierr); 438b1af7b3SBarry Smith ts->steps++; 44c3e30b67SBarry Smith ierr = TSMonitor(ts,ts->steps,ts->ptime,sol); CHKERRQ(ierr); 458b1af7b3SBarry Smith if (ts->ptime > ts->max_time) break; 468b1af7b3SBarry Smith } 474b33d51aSBarry Smith 488b1af7b3SBarry Smith *steps += ts->steps; 498b1af7b3SBarry Smith *time = ts->ptime; 504b33d51aSBarry Smith return 0; 514b33d51aSBarry Smith } 524b33d51aSBarry Smith /*------------------------------------------------------------*/ 5358562591SBarry Smith #undef __FUNC__ 5458562591SBarry Smith #define __FUNC__ "TSDestroy_Euler" 558b1af7b3SBarry Smith static int TSDestroy_Euler(PetscObject obj ) 564b33d51aSBarry Smith { 574b33d51aSBarry Smith TS ts = (TS) obj; 588b1af7b3SBarry Smith TS_Euler *euler = (TS_Euler*) ts->data; 598b1af7b3SBarry Smith 608b1af7b3SBarry Smith VecDestroy(euler->update); 618b1af7b3SBarry Smith PetscFree(euler); 628b1af7b3SBarry Smith return 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 { 704b33d51aSBarry Smith 714b33d51aSBarry Smith return 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 { 788b1af7b3SBarry Smith 798b1af7b3SBarry Smith return 0; 808b1af7b3SBarry Smith } 818b1af7b3SBarry Smith 8258562591SBarry Smith #undef __FUNC__ 8358562591SBarry Smith #define __FUNC__ "TSView_Euler" 848b1af7b3SBarry Smith static int TSView_Euler(PetscObject obj,Viewer viewer) 858b1af7b3SBarry Smith { 868b1af7b3SBarry Smith return 0; 878b1af7b3SBarry Smith } 888b1af7b3SBarry Smith 898b1af7b3SBarry Smith /* ------------------------------------------------------------ */ 9058562591SBarry Smith #undef __FUNC__ 9158562591SBarry Smith #define __FUNC__ "TSCreate_Euler" 928b1af7b3SBarry Smith int TSCreate_Euler(TS ts ) 938b1af7b3SBarry Smith { 948b1af7b3SBarry Smith TS_Euler *euler; 958b1af7b3SBarry Smith 96c3e30b67SBarry Smith ts->type = TS_EULER; 978b1af7b3SBarry Smith ts->setup = TSSetUp_Euler; 988b1af7b3SBarry Smith ts->step = TSStep_Euler; 998b1af7b3SBarry Smith ts->destroy = TSDestroy_Euler; 1008b1af7b3SBarry Smith ts->printhelp = TSPrintHelp_Euler; 1018b1af7b3SBarry Smith ts->setfromoptions = TSSetFromOptions_Euler; 1028b1af7b3SBarry Smith ts->view = TSView_Euler; 1038b1af7b3SBarry Smith 1048b1af7b3SBarry Smith euler = PetscNew(TS_Euler); CHKPTRQ(euler); 105eed86810SBarry Smith PLogObjectMemory(ts,sizeof(TS_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