1 #define PETSCTS_DLL 2 3 /* 4 Code for Timestepping with explicit Euler. 5 */ 6 #include "private/tsimpl.h" /*I "petscts.h" I*/ 7 8 typedef struct { 9 Vec update; /* work vector where F(t[i],u[i]) is stored */ 10 } TS_Euler; 11 12 #undef __FUNCT__ 13 #define __FUNCT__ "TSSetUp_Euler" 14 static PetscErrorCode TSSetUp_Euler(TS ts) 15 { 16 TS_Euler *euler = (TS_Euler*)ts->data; 17 PetscErrorCode ierr; 18 19 PetscFunctionBegin; 20 ierr = VecDuplicate(ts->vec_sol,&euler->update);CHKERRQ(ierr); 21 PetscFunctionReturn(0); 22 } 23 24 #undef __FUNCT__ 25 #define __FUNCT__ "TSStep_Euler" 26 static PetscErrorCode TSStep_Euler(TS ts,PetscInt *steps,PetscReal *ptime) 27 { 28 TS_Euler *euler = (TS_Euler*)ts->data; 29 Vec sol = ts->vec_sol,update = euler->update; 30 PetscErrorCode ierr; 31 PetscInt i,max_steps = ts->max_steps; 32 33 PetscFunctionBegin; 34 *steps = -ts->steps; 35 ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr); 36 37 for (i=0; i<max_steps; i++) { 38 PetscReal dt = ts->time_step; 39 40 ts->ptime += dt; 41 ierr = TSComputeRHSFunction(ts,ts->ptime,sol,update);CHKERRQ(ierr); 42 ierr = VecAXPY(sol,dt,update);CHKERRQ(ierr); 43 ts->steps++; 44 ierr = TSMonitor(ts,ts->steps,ts->ptime,sol);CHKERRQ(ierr); 45 if (ts->ptime > ts->max_time) break; 46 } 47 48 *steps += ts->steps; 49 *ptime = ts->ptime; 50 PetscFunctionReturn(0); 51 } 52 /*------------------------------------------------------------*/ 53 #undef __FUNCT__ 54 #define __FUNCT__ "TSDestroy_Euler" 55 static PetscErrorCode TSDestroy_Euler(TS ts) 56 { 57 TS_Euler *euler = (TS_Euler*)ts->data; 58 PetscErrorCode ierr; 59 60 PetscFunctionBegin; 61 if (euler->update) {ierr = VecDestroy(euler->update);CHKERRQ(ierr);} 62 ierr = PetscFree(euler);CHKERRQ(ierr); 63 PetscFunctionReturn(0); 64 } 65 /*------------------------------------------------------------*/ 66 67 #undef __FUNCT__ 68 #define __FUNCT__ "TSSetFromOptions_Euler" 69 static PetscErrorCode TSSetFromOptions_Euler(TS ts) 70 { 71 PetscFunctionBegin; 72 PetscFunctionReturn(0); 73 } 74 75 #undef __FUNCT__ 76 #define __FUNCT__ "TSView_Euler" 77 static PetscErrorCode TSView_Euler(TS ts,PetscViewer viewer) 78 { 79 PetscFunctionBegin; 80 PetscFunctionReturn(0); 81 } 82 83 /* ------------------------------------------------------------ */ 84 85 /*MC 86 TSEULER - ODE solver using the explicit forward Euler method 87 88 Level: beginner 89 90 .seealso: TSCreate(), TS, TSSetType(), TSBEULER 91 92 M*/ 93 EXTERN_C_BEGIN 94 #undef __FUNCT__ 95 #define __FUNCT__ "TSCreate_Euler" 96 PetscErrorCode PETSCTS_DLLEXPORT TSCreate_Euler(TS ts) 97 { 98 TS_Euler *euler; 99 PetscErrorCode ierr; 100 101 PetscFunctionBegin; 102 ts->ops->setup = TSSetUp_Euler; 103 ts->ops->step = TSStep_Euler; 104 ts->ops->destroy = TSDestroy_Euler; 105 ts->ops->setfromoptions = TSSetFromOptions_Euler; 106 ts->ops->view = TSView_Euler; 107 108 ierr = PetscNewLog(ts,TS_Euler,&euler);CHKERRQ(ierr); 109 ts->data = (void*)euler; 110 111 PetscFunctionReturn(0); 112 } 113 EXTERN_C_END 114 115 116 117 118