1 static char help[] = "Adjoint sensitivity of a hybrid system with state-dependent switchings.\n"; 2 3 /* 4 The dynamics is described by the ODE 5 u_t = A_i u 6 7 where A_1 = [ 1 -100 8 10 1 ], 9 A_2 = [ 1 10 10 -100 1 ]. 11 The index i changes from 1 to 2 when u[1]=2.75u[0] and from 2 to 1 when u[1]=0.36u[0]. 12 Initially u=[0 1]^T and i=1. 13 14 References: 15 + * - H. Zhang, S. Abhyankar, E. Constantinescu, M. Mihai, Discrete Adjoint Sensitivity Analysis of Hybrid Dynamical Systems With Switching, IEEE Transactions on Circuits and Systems I: Regular Papers, 64(5), May 2017 16 - * - I. A. Hiskens, M.A. Pai, Trajectory Sensitivity Analysis of Hybrid Systems, IEEE Transactions on Circuits and Systems, Vol 47, No 2, February 2000 17 */ 18 19 #include <petscts.h> 20 21 typedef struct { 22 PetscScalar lambda1; 23 PetscScalar lambda2; 24 PetscInt mode; /* mode flag*/ 25 } AppCtx; 26 27 PetscErrorCode EventFunction(TS ts,PetscReal t,Vec U,PetscScalar *fvalue,void *ctx) 28 { 29 AppCtx *actx=(AppCtx*)ctx; 30 const PetscScalar *u; 31 32 PetscFunctionBegin; 33 CHKERRQ(VecGetArrayRead(U,&u)); 34 if (actx->mode == 1) { 35 fvalue[0] = u[1]-actx->lambda1*u[0]; 36 }else if (actx->mode == 2) { 37 fvalue[0] = u[1]-actx->lambda2*u[0]; 38 } 39 CHKERRQ(VecRestoreArrayRead(U,&u)); 40 PetscFunctionReturn(0); 41 } 42 43 PetscErrorCode ShiftGradients(TS ts,Vec U,AppCtx *actx) 44 { 45 Vec *lambda,*mu; 46 PetscScalar *x,*y; 47 const PetscScalar *u; 48 PetscScalar tmp[2],A1[2][2],A2[2],denorm; 49 PetscInt numcost; 50 51 PetscFunctionBegin; 52 CHKERRQ(TSGetCostGradients(ts,&numcost,&lambda,&mu)); 53 CHKERRQ(VecGetArrayRead(U,&u)); 54 55 if (actx->mode==2) { 56 denorm = -actx->lambda1*(u[0]-100.*u[1])+1.*(10.*u[0]+u[1]); 57 A1[0][0] = 110.*u[1]*(-actx->lambda1)/denorm+1.; 58 A1[0][1] = -110.*u[0]*(-actx->lambda1)/denorm; 59 A1[1][0] = 110.*u[1]*1./denorm; 60 A1[1][1] = -110.*u[0]*1./denorm+1.; 61 62 A2[0] = 110.*u[1]*(-u[0])/denorm; 63 A2[1] = -110.*u[0]*(-u[0])/denorm; 64 } else { 65 denorm = -actx->lambda2*(u[0]+10.*u[1])+1.*(-100.*u[0]+u[1]); 66 A1[0][0] = 110.*u[1]*(actx->lambda2)/denorm+1; 67 A1[0][1] = -110.*u[0]*(actx->lambda2)/denorm; 68 A1[1][0] = -110.*u[1]*1./denorm; 69 A1[1][1] = 110.*u[0]*1./denorm+1.; 70 71 A2[0] = 0; 72 A2[1] = 0; 73 } 74 75 CHKERRQ(VecRestoreArrayRead(U,&u)); 76 77 CHKERRQ(VecGetArray(lambda[0],&x)); 78 CHKERRQ(VecGetArray(mu[0],&y)); 79 tmp[0] = A1[0][0]*x[0]+A1[0][1]*x[1]; 80 tmp[1] = A1[1][0]*x[0]+A1[1][1]*x[1]; 81 y[0] = y[0] + A2[0]*x[0]+A2[1]*x[1]; 82 x[0] = tmp[0]; 83 x[1] = tmp[1]; 84 CHKERRQ(VecRestoreArray(mu[0],&y)); 85 CHKERRQ(VecRestoreArray(lambda[0],&x)); 86 87 CHKERRQ(VecGetArray(lambda[1],&x)); 88 CHKERRQ(VecGetArray(mu[1],&y)); 89 tmp[0] = A1[0][0]*x[0]+A1[0][1]*x[1]; 90 tmp[1] = A1[1][0]*x[0]+A1[1][1]*x[1]; 91 y[0] = y[0] + A2[0]*x[0]+A2[1]*x[1]; 92 x[0] = tmp[0]; 93 x[1] = tmp[1]; 94 CHKERRQ(VecRestoreArray(mu[1],&y)); 95 CHKERRQ(VecRestoreArray(lambda[1],&x)); 96 PetscFunctionReturn(0); 97 } 98 99 PetscErrorCode PostEventFunction(TS ts,PetscInt nevents,PetscInt event_list[],PetscReal t,Vec U,PetscBool forwardsolve,void* ctx) 100 { 101 AppCtx *actx=(AppCtx*)ctx; 102 103 PetscFunctionBegin; 104 /* CHKERRQ(VecView(U,PETSC_VIEWER_STDOUT_WORLD)); */ 105 if (!forwardsolve) { 106 CHKERRQ(ShiftGradients(ts,U,actx)); 107 } 108 if (actx->mode == 1) { 109 actx->mode = 2; 110 /* CHKERRQ(PetscPrintf(PETSC_COMM_SELF,"Change from mode 1 to 2 at t = %f \n",(double)t)); */ 111 } else if (actx->mode == 2) { 112 actx->mode = 1; 113 /* CHKERRQ(PetscPrintf(PETSC_COMM_SELF,"Change from mode 2 to 1 at t = %f \n",(double)t)); */ 114 } 115 PetscFunctionReturn(0); 116 } 117 118 /* 119 Defines the ODE passed to the ODE solver 120 */ 121 static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 122 { 123 AppCtx *actx=(AppCtx*)ctx; 124 PetscScalar *f; 125 const PetscScalar *u,*udot; 126 127 PetscFunctionBegin; 128 /* The next three lines allow us to access the entries of the vectors directly */ 129 CHKERRQ(VecGetArrayRead(U,&u)); 130 CHKERRQ(VecGetArrayRead(Udot,&udot)); 131 CHKERRQ(VecGetArray(F,&f)); 132 133 if (actx->mode == 1) { 134 f[0] = udot[0]-u[0]+100*u[1]; 135 f[1] = udot[1]-10*u[0]-u[1]; 136 } else if (actx->mode == 2) { 137 f[0] = udot[0]-u[0]-10*u[1]; 138 f[1] = udot[1]+100*u[0]-u[1]; 139 } 140 141 CHKERRQ(VecRestoreArrayRead(U,&u)); 142 CHKERRQ(VecRestoreArrayRead(Udot,&udot)); 143 CHKERRQ(VecRestoreArray(F,&f)); 144 PetscFunctionReturn(0); 145 } 146 147 /* 148 Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian. 149 */ 150 static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat A,Mat B,void *ctx) 151 { 152 AppCtx *actx=(AppCtx*)ctx; 153 PetscInt rowcol[] = {0,1}; 154 PetscScalar J[2][2]; 155 const PetscScalar *u,*udot; 156 157 PetscFunctionBegin; 158 CHKERRQ(VecGetArrayRead(U,&u)); 159 CHKERRQ(VecGetArrayRead(Udot,&udot)); 160 161 if (actx->mode == 1) { 162 J[0][0] = a-1; J[0][1] = 100; 163 J[1][0] = -10; J[1][1] = a-1; 164 } else if (actx->mode == 2) { 165 J[0][0] = a-1; J[0][1] = -10; 166 J[1][0] = 100; J[1][1] = a-1; 167 } 168 CHKERRQ(MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES)); 169 170 CHKERRQ(VecRestoreArrayRead(U,&u)); 171 CHKERRQ(VecRestoreArrayRead(Udot,&udot)); 172 173 CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 174 CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 175 if (A != B) { 176 CHKERRQ(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 177 CHKERRQ(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 178 } 179 PetscFunctionReturn(0); 180 } 181 182 /* Matrix JacobianP is constant so that it only needs to be evaluated once */ 183 static PetscErrorCode RHSJacobianP(TS ts,PetscReal t,Vec X,Mat A, void *ctx) 184 { 185 PetscFunctionBeginUser; 186 PetscFunctionReturn(0); 187 } 188 189 int main(int argc,char **argv) 190 { 191 TS ts; /* ODE integrator */ 192 Vec U; /* solution will be stored here */ 193 Mat A; /* Jacobian matrix */ 194 Mat Ap; /* dfdp */ 195 PetscErrorCode ierr; 196 PetscMPIInt size; 197 PetscInt n = 2; 198 PetscScalar *u,*v; 199 AppCtx app; 200 PetscInt direction[1]; 201 PetscBool terminate[1]; 202 Vec lambda[2],mu[2]; 203 PetscReal tend; 204 205 FILE *f; 206 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 207 Initialize program 208 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 209 ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; 210 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 211 PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Only for sequential runs"); 212 app.mode = 1; 213 app.lambda1 = 2.75; 214 app.lambda2 = 0.36; 215 tend = 0.125; 216 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"ex1adj options","");CHKERRQ(ierr); 217 { 218 CHKERRQ(PetscOptionsReal("-lambda1","","",app.lambda1,&app.lambda1,NULL)); 219 CHKERRQ(PetscOptionsReal("-lambda2","","",app.lambda2,&app.lambda2,NULL)); 220 CHKERRQ(PetscOptionsReal("-tend","","",tend,&tend,NULL)); 221 } 222 ierr = PetscOptionsEnd();CHKERRQ(ierr); 223 224 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 225 Create necessary matrix and vectors 226 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 227 CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A)); 228 CHKERRQ(MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE)); 229 CHKERRQ(MatSetType(A,MATDENSE)); 230 CHKERRQ(MatSetFromOptions(A)); 231 CHKERRQ(MatSetUp(A)); 232 233 CHKERRQ(MatCreateVecs(A,&U,NULL)); 234 235 CHKERRQ(MatCreate(PETSC_COMM_WORLD,&Ap)); 236 CHKERRQ(MatSetSizes(Ap,n,1,PETSC_DETERMINE,PETSC_DETERMINE)); 237 CHKERRQ(MatSetType(Ap,MATDENSE)); 238 CHKERRQ(MatSetFromOptions(Ap)); 239 CHKERRQ(MatSetUp(Ap)); 240 CHKERRQ(MatZeroEntries(Ap)); /* initialize to zeros */ 241 242 CHKERRQ(VecGetArray(U,&u)); 243 u[0] = 0; 244 u[1] = 1; 245 CHKERRQ(VecRestoreArray(U,&u)); 246 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 247 Create timestepping solver context 248 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 249 CHKERRQ(TSCreate(PETSC_COMM_WORLD,&ts)); 250 CHKERRQ(TSSetProblemType(ts,TS_NONLINEAR)); 251 CHKERRQ(TSSetType(ts,TSCN)); 252 CHKERRQ(TSSetIFunction(ts,NULL,(TSIFunction)IFunction,&app)); 253 CHKERRQ(TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&app)); 254 CHKERRQ(TSSetRHSJacobianP(ts,Ap,RHSJacobianP,&app)); 255 256 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 257 Set initial conditions 258 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 259 CHKERRQ(TSSetSolution(ts,U)); 260 261 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 262 Save trajectory of solution so that TSAdjointSolve() may be used 263 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 264 CHKERRQ(TSSetSaveTrajectory(ts)); 265 266 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 267 Set solver options 268 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 269 CHKERRQ(TSSetMaxTime(ts,tend)); 270 CHKERRQ(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP)); 271 CHKERRQ(TSSetTimeStep(ts,1./256.)); 272 CHKERRQ(TSSetFromOptions(ts)); 273 274 /* Set directions and terminate flags for the two events */ 275 direction[0] = 0; 276 terminate[0] = PETSC_FALSE; 277 CHKERRQ(TSSetEventHandler(ts,1,direction,terminate,EventFunction,PostEventFunction,(void*)&app)); 278 279 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 280 Run timestepping solver 281 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 282 CHKERRQ(TSSolve(ts,U)); 283 284 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 285 Adjoint model starts here 286 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 287 CHKERRQ(MatCreateVecs(A,&lambda[0],NULL)); 288 CHKERRQ(MatCreateVecs(A,&lambda[1],NULL)); 289 /* Set initial conditions for the adjoint integration */ 290 CHKERRQ(VecZeroEntries(lambda[0])); 291 CHKERRQ(VecZeroEntries(lambda[1])); 292 CHKERRQ(VecGetArray(lambda[0],&u)); 293 u[0] = 1.; 294 CHKERRQ(VecRestoreArray(lambda[0],&u)); 295 CHKERRQ(VecGetArray(lambda[1],&u)); 296 u[1] = 1.; 297 CHKERRQ(VecRestoreArray(lambda[1],&u)); 298 299 CHKERRQ(MatCreateVecs(Ap,&mu[0],NULL)); 300 CHKERRQ(MatCreateVecs(Ap,&mu[1],NULL)); 301 CHKERRQ(VecZeroEntries(mu[0])); 302 CHKERRQ(VecZeroEntries(mu[1])); 303 CHKERRQ(TSSetCostGradients(ts,2,lambda,mu)); 304 305 CHKERRQ(TSAdjointSolve(ts)); 306 307 /* 308 CHKERRQ(VecView(lambda[0],PETSC_VIEWER_STDOUT_WORLD)); 309 CHKERRQ(VecView(lambda[1],PETSC_VIEWER_STDOUT_WORLD)); 310 CHKERRQ(VecView(mu[0],PETSC_VIEWER_STDOUT_WORLD)); 311 CHKERRQ(VecView(mu[1],PETSC_VIEWER_STDOUT_WORLD)); 312 */ 313 CHKERRQ(VecGetArray(mu[0],&u)); 314 CHKERRQ(VecGetArray(mu[1],&v)); 315 f = fopen("adj_mu.out", "a"); 316 CHKERRQ(PetscFPrintf(PETSC_COMM_WORLD,f,"%20.15lf %20.15lf %20.15lf\n",tend,u[0],v[0])); 317 CHKERRQ(VecRestoreArray(mu[0],&u)); 318 CHKERRQ(VecRestoreArray(mu[1],&v)); 319 fclose(f); 320 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 321 Free work space. All PETSc objects should be destroyed when they are no longer needed. 322 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 323 CHKERRQ(MatDestroy(&A)); 324 CHKERRQ(VecDestroy(&U)); 325 CHKERRQ(TSDestroy(&ts)); 326 327 CHKERRQ(MatDestroy(&Ap)); 328 CHKERRQ(VecDestroy(&lambda[0])); 329 CHKERRQ(VecDestroy(&lambda[1])); 330 CHKERRQ(VecDestroy(&mu[0])); 331 CHKERRQ(VecDestroy(&mu[1])); 332 ierr = PetscFinalize(); 333 return ierr; 334 } 335 336 /*TEST 337 338 build: 339 requires: !complex 340 341 test: 342 args: -ts_monitor -ts_adjoint_monitor 343 344 TEST*/ 345