1 /*$Id: euler.c,v 1.26 2001/03/23 23:24:37 balay Exp bsmith $*/ 2 /* 3 Code for Timestepping with explicit Euler. 4 */ 5 #include "src/ts/tsimpl.h" /*I "petscts.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 __FUNCT__ 12 #define __FUNCT__ "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 __FUNCT__ 24 #define __FUNCT__ "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 __FUNCT__ 51 #define __FUNCT__ "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 if (euler->update) {ierr = VecDestroy(euler->update);CHKERRQ(ierr);} 59 ierr = PetscFree(euler);CHKERRQ(ierr); 60 PetscFunctionReturn(0); 61 } 62 /*------------------------------------------------------------*/ 63 64 #undef __FUNCT__ 65 #define __FUNCT__ "TSSetFromOptions_Euler" 66 static int TSSetFromOptions_Euler(TS ts) 67 { 68 PetscFunctionBegin; 69 PetscFunctionReturn(0); 70 } 71 72 #undef __FUNCT__ 73 #define __FUNCT__ "TSView_Euler" 74 static int TSView_Euler(TS ts,PetscViewer viewer) 75 { 76 PetscFunctionBegin; 77 PetscFunctionReturn(0); 78 } 79 80 /* ------------------------------------------------------------ */ 81 EXTERN_C_BEGIN 82 #undef __FUNCT__ 83 #define __FUNCT__ "TSCreate_Euler" 84 int TSCreate_Euler(TS ts) 85 { 86 TS_Euler *euler; 87 int ierr; 88 89 PetscFunctionBegin; 90 ts->setup = TSSetUp_Euler; 91 ts->step = TSStep_Euler; 92 ts->destroy = TSDestroy_Euler; 93 ts->setfromoptions = TSSetFromOptions_Euler; 94 ts->view = TSView_Euler; 95 96 ierr = PetscNew(TS_Euler,&euler);CHKERRQ(ierr); 97 PetscLogObjectMemory(ts,sizeof(TS_Euler)); 98 ts->data = (void*)euler; 99 100 PetscFunctionReturn(0); 101 } 102 EXTERN_C_END 103 104 105 106 107