1 2 static char help[] = "Solves the van der Pol equation and demonstrate IMEX.\n\ 3 Input parameters include:\n\ 4 -mu : stiffness parameter\n\n"; 5 6 /* ------------------------------------------------------------------------ 7 8 This program solves the van der Pol equation 9 y'' - \mu ((1-y^2)*y' - y) = 0 (1) 10 on the domain 0 <= x <= 1, with the boundary conditions 11 y(0) = 2, y'(0) = - 2/3 +10/(81*\mu) - 292/(2187*\mu^2), 12 This is a nonlinear equation. The well prepared initial condition gives errors that are not dominated by the first few steps of the method when \mu is large. 13 14 Notes: 15 This code demonstrates the TS solver interface to two variants of 16 linear problems, u_t = f(u,t), namely turning (1) into a system of 17 first order differential equations, 18 19 [ y' ] = [ z ] 20 [ z' ] [ \mu ((1 - y^2) z - y) ] 21 22 which then we can write as a vector equation 23 24 [ u_1' ] = [ u_2 ] (2) 25 [ u_2' ] [ \mu (1 - u_1^2) u_2 - u_1 ] 26 27 which is now in the desired form of u_t = f(u,t). One way that we 28 can split f(u,t) in (2) is to split by component, 29 30 [ u_1' ] = [ u_2 ] + [ 0 ] 31 [ u_2' ] [ 0 ] [ \mu ((1 - u_1^2) u_2 - u_1) ] 32 33 where 34 35 [ G(u,t) ] = [ u_2 ] 36 [ 0 ] 37 38 and 39 40 [ F(u',u,t) ] = [ u_1' ] - [ 0 ] 41 [ u_2' ] [ \mu ((1 - u_1^2) u_2 - u_1) ] 42 43 Using the definition of the Jacobian of F (from the PETSc user manual), 44 in the equation F(u',u,t) = G(u,t), 45 46 dF dF 47 J(F) = a * -- - -- 48 du' du 49 50 where d is the partial derivative. In this example, 51 52 dF [ 1 ; 0 ] 53 -- = [ ] 54 du' [ 0 ; 1 ] 55 56 dF [ 0 ; 0 ] 57 -- = [ ] 58 du [ -\mu (2*u_1*u_2 + 1); \mu (1 - u_1^2) ] 59 60 Hence, 61 62 [ a ; 0 ] 63 J(F) = [ ] 64 [ \mu (2*u_1*u_2 + 1); a - \mu (1 - u_1^2) ] 65 66 ------------------------------------------------------------------------- */ 67 68 #include <petscts.h> 69 70 typedef struct _n_User *User; 71 struct _n_User { 72 PetscReal mu; 73 PetscBool imex; 74 PetscReal next_output; 75 }; 76 77 /* 78 User-defined routines 79 */ 80 static PetscErrorCode RHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ctx) 81 { 82 User user = (User)ctx; 83 PetscScalar *f; 84 const PetscScalar *x; 85 86 PetscFunctionBeginUser; 87 PetscCall(VecGetArrayRead(X,&x)); 88 PetscCall(VecGetArray(F,&f)); 89 f[0] = (user->imex ? x[1] : 0); 90 f[1] = 0.0; 91 PetscCall(VecRestoreArrayRead(X,&x)); 92 PetscCall(VecRestoreArray(F,&f)); 93 PetscFunctionReturn(0); 94 } 95 96 static PetscErrorCode IFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx) 97 { 98 User user = (User)ctx; 99 const PetscScalar *x,*xdot; 100 PetscScalar *f; 101 102 PetscFunctionBeginUser; 103 PetscCall(VecGetArrayRead(X,&x)); 104 PetscCall(VecGetArrayRead(Xdot,&xdot)); 105 PetscCall(VecGetArray(F,&f)); 106 f[0] = xdot[0] + (user->imex ? 0 : x[1]); 107 f[1] = xdot[1] - user->mu*((1. - x[0]*x[0])*x[1] - x[0]); 108 PetscCall(VecRestoreArrayRead(X,&x)); 109 PetscCall(VecRestoreArrayRead(Xdot,&xdot)); 110 PetscCall(VecRestoreArray(F,&f)); 111 PetscFunctionReturn(0); 112 } 113 114 static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat A,Mat B,void *ctx) 115 { 116 User user = (User)ctx; 117 PetscReal mu = user->mu; 118 PetscInt rowcol[] = {0,1}; 119 const PetscScalar *x; 120 PetscScalar J[2][2]; 121 122 PetscFunctionBeginUser; 123 PetscCall(VecGetArrayRead(X,&x)); 124 J[0][0] = a; J[0][1] = (user->imex ? 0 : 1.); 125 J[1][0] = mu*(2.*x[0]*x[1]+1.); J[1][1] = a - mu*(1. - x[0]*x[0]); 126 PetscCall(MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES)); 127 PetscCall(VecRestoreArrayRead(X,&x)); 128 129 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 130 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 131 if (A != B) { 132 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 133 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 134 } 135 PetscFunctionReturn(0); 136 } 137 138 static PetscErrorCode RegisterMyARK2(void) 139 { 140 PetscFunctionBeginUser; 141 { 142 const PetscReal 143 A[3][3] = {{0,0,0}, 144 {0.41421356237309504880,0,0}, 145 {0.75,0.25,0}}, 146 At[3][3] = {{0,0,0}, 147 {0.12132034355964257320,0.29289321881345247560,0}, 148 {0.20710678118654752440,0.50000000000000000000,0.29289321881345247560}}, 149 *bembedt = NULL,*bembed = NULL; 150 PetscCall(TSARKIMEXRegister("myark2",2,3,&At[0][0],NULL,NULL,&A[0][0],NULL,NULL,bembedt,bembed,0,NULL,NULL)); 151 } 152 PetscFunctionReturn(0); 153 } 154 155 /* Monitor timesteps and use interpolation to output at integer multiples of 0.1 */ 156 static PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal t,Vec X,void *ctx) 157 { 158 const PetscScalar *x; 159 PetscReal tfinal, dt; 160 User user = (User)ctx; 161 Vec interpolatedX; 162 163 PetscFunctionBeginUser; 164 PetscCall(TSGetTimeStep(ts,&dt)); 165 PetscCall(TSGetMaxTime(ts,&tfinal)); 166 167 while (user->next_output <= t && user->next_output <= tfinal) { 168 PetscCall(VecDuplicate(X,&interpolatedX)); 169 PetscCall(TSInterpolate(ts,user->next_output,interpolatedX)); 170 PetscCall(VecGetArrayRead(interpolatedX,&x)); 171 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"[%.1f] %D TS %.6f (dt = %.6f) X % 12.6e % 12.6e\n",user->next_output,step,t,dt,(double)PetscRealPart(x[0]),(double)PetscRealPart(x[1]))); 172 PetscCall(VecRestoreArrayRead(interpolatedX,&x)); 173 PetscCall(VecDestroy(&interpolatedX)); 174 175 user->next_output += 0.1; 176 } 177 PetscFunctionReturn(0); 178 } 179 180 int main(int argc,char **argv) 181 { 182 TS ts; /* nonlinear solver */ 183 Vec x; /* solution, residual vectors */ 184 Mat A; /* Jacobian matrix */ 185 PetscInt steps; 186 PetscReal ftime = 0.5; 187 PetscBool monitor = PETSC_FALSE; 188 PetscScalar *x_ptr; 189 PetscMPIInt size; 190 struct _n_User user; 191 192 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 193 Initialize program 194 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 195 PetscCall(PetscInitialize(&argc,&argv,NULL,help)); 196 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 197 PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!"); 198 199 PetscCall(RegisterMyARK2()); 200 201 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 202 Set runtime options 203 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 204 user.mu = 1000.0; 205 user.imex = PETSC_TRUE; 206 user.next_output = 0.0; 207 208 PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&user.mu,NULL)); 209 PetscCall(PetscOptionsGetBool(NULL,NULL,"-imex",&user.imex,NULL)); 210 PetscCall(PetscOptionsGetBool(NULL,NULL,"-monitor",&monitor,NULL)); 211 212 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 213 Create necessary matrix and vectors, solve same ODE on every process 214 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 215 PetscCall(MatCreate(PETSC_COMM_WORLD,&A)); 216 PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,2,2)); 217 PetscCall(MatSetFromOptions(A)); 218 PetscCall(MatSetUp(A)); 219 PetscCall(MatCreateVecs(A,&x,NULL)); 220 221 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 222 Create timestepping solver context 223 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 224 PetscCall(TSCreate(PETSC_COMM_WORLD,&ts)); 225 PetscCall(TSSetType(ts,TSBEULER)); 226 PetscCall(TSSetRHSFunction(ts,NULL,RHSFunction,&user)); 227 PetscCall(TSSetIFunction(ts,NULL,IFunction,&user)); 228 PetscCall(TSSetIJacobian(ts,A,A,IJacobian,&user)); 229 PetscCall(TSSetMaxTime(ts,ftime)); 230 PetscCall(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER)); 231 if (monitor) { 232 PetscCall(TSMonitorSet(ts,Monitor,&user,NULL)); 233 } 234 235 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 236 Set initial conditions 237 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 238 PetscCall(VecGetArray(x,&x_ptr)); 239 x_ptr[0] = 2.0; 240 x_ptr[1] = -2.0/3.0 + 10.0/(81.0*user.mu) - 292.0/(2187.0*user.mu*user.mu); 241 PetscCall(VecRestoreArray(x,&x_ptr)); 242 PetscCall(TSSetTimeStep(ts,0.01)); 243 244 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 245 Set runtime options 246 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 247 PetscCall(TSSetFromOptions(ts)); 248 249 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 250 Solve nonlinear system 251 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 252 PetscCall(TSSolve(ts,x)); 253 PetscCall(TSGetSolveTime(ts,&ftime)); 254 PetscCall(TSGetStepNumber(ts,&steps)); 255 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"mu %g, steps %D, ftime %g\n",(double)user.mu,steps,(double)ftime)); 256 PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 257 258 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 259 Free work space. All PETSc objects should be destroyed when they 260 are no longer needed. 261 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 262 PetscCall(MatDestroy(&A)); 263 PetscCall(VecDestroy(&x)); 264 PetscCall(TSDestroy(&ts)); 265 266 PetscCall(PetscFinalize()); 267 return 0; 268 } 269 270 /*TEST 271 272 test: 273 args: -ts_type arkimex -ts_arkimex_type myark2 -ts_adapt_type none 274 requires: !single 275 276 TEST*/ 277