1 /*$Id: euler.c,v 1.19 1999/11/24 21:55:24 bsmith Exp bsmith $*/ 2 /* 3 Code for Timestepping with explicit Euler. 4 */ 5 #include "src/ts/tsimpl.h" /*I "ts.h" I*/ 6 7 typedef struct { 8 Vec update; /* work vector where F(t[i],u[i]) is stored */ 9 } TS_Euler; 10 11 #undef __FUNC__ 12 #define __FUNC__ "TSSetUp_Euler" 13 static int TSSetUp_Euler(TS ts) 14 { 15 TS_Euler *euler = (TS_Euler*)ts->data; 16 int ierr; 17 18 PetscFunctionBegin; 19 ierr = VecDuplicate(ts->vec_sol,&euler->update);CHKERRQ(ierr); 20 PetscFunctionReturn(0); 21 } 22 23 #undef __FUNC__ 24 #define __FUNC__ "TSStep_Euler" 25 static int TSStep_Euler(TS ts,int *steps,double *time) 26 { 27 TS_Euler *euler = (TS_Euler*)ts->data; 28 Vec sol = ts->vec_sol,update = euler->update; 29 int ierr,i,max_steps = ts->max_steps; 30 Scalar dt = ts->time_step; 31 32 PetscFunctionBegin; 33 *steps = -ts->steps; 34 ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr); 35 36 for (i=0; i<max_steps; i++) { 37 ts->ptime += ts->time_step; 38 ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update);CHKERRQ(ierr); 39 ierr = VecAXPY(&dt,update,sol);CHKERRQ(ierr); 40 ts->steps++; 41 ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr); 42 if (ts->ptime > ts->max_time) break; 43 } 44 45 *steps += ts->steps; 46 *time = ts->ptime; 47 PetscFunctionReturn(0); 48 } 49 /*------------------------------------------------------------*/ 50 #undef __FUNC__ 51 #define __FUNC__ "TSDestroy_Euler" 52 static int TSDestroy_Euler(TS ts) 53 { 54 TS_Euler *euler = (TS_Euler*)ts->data; 55 int ierr; 56 57 PetscFunctionBegin; 58 ierr = VecDestroy(euler->update);CHKERRQ(ierr); 59 ierr = PetscFree(euler);CHKERRQ(ierr); 60 PetscFunctionReturn(0); 61 } 62 /*------------------------------------------------------------*/ 63 64 #undef __FUNC__ 65 #define __FUNC__ "TSSetFromOptions_Euler" 66 static int TSSetFromOptions_Euler(TS ts) 67 { 68 PetscFunctionBegin; 69 PetscFunctionReturn(0); 70 } 71 72 #undef __FUNC__ 73 #define __FUNC__ "TSPrintHelp_Euler" 74 static int TSPrintHelp_Euler(TS ts,char *p) 75 { 76 PetscFunctionBegin; 77 PetscFunctionReturn(0); 78 } 79 80 #undef __FUNC__ 81 #define __FUNC__ "TSView_Euler" 82 static int TSView_Euler(TS ts,Viewer viewer) 83 { 84 PetscFunctionBegin; 85 PetscFunctionReturn(0); 86 } 87 88 /* ------------------------------------------------------------ */ 89 EXTERN_C_BEGIN 90 #undef __FUNC__ 91 #define __FUNC__ "TSCreate_Euler" 92 int TSCreate_Euler(TS ts) 93 { 94 TS_Euler *euler; 95 96 PetscFunctionBegin; 97 ts->setup = TSSetUp_Euler; 98 ts->step = TSStep_Euler; 99 ts->destroy = TSDestroy_Euler; 100 ts->printhelp = TSPrintHelp_Euler; 101 ts->setfromoptions = TSSetFromOptions_Euler; 102 ts->view = TSView_Euler; 103 104 euler = PetscNew(TS_Euler);CHKPTRQ(euler); 105 PLogObjectMemory(ts,sizeof(TS_Euler)); 106 ts->data = (void*)euler; 107 108 PetscFunctionReturn(0); 109 } 110 EXTERN_C_END 111 112 113 114 115