1 2 #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3 #include <petscdmshell.h> 4 #include <petscdmda.h> 5 #include <petscviewer.h> 6 #include <petscdraw.h> 7 8 /* Logging support */ 9 PetscClassId TS_CLASSID, DMTS_CLASSID; 10 PetscLogEvent TS_AdjointStep, TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11 12 const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 13 14 struct _n_TSMonitorDrawCtx { 15 PetscViewer viewer; 16 PetscDrawAxis axis; 17 Vec initialsolution; 18 PetscBool showinitial; 19 PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 20 PetscBool showtimestepandtime; 21 int color; 22 }; 23 24 #undef __FUNCT__ 25 #define __FUNCT__ "TSMonitorSetFromOptions" 26 /*@C 27 TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 28 29 Collective on TS 30 31 Input Parameters: 32 + ts - TS object you wish to monitor 33 . name - the monitor type one is seeking 34 . help - message indicating what monitoring is done 35 . manual - manual page for the monitor 36 . monitor - the monitor function 37 - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 38 39 Level: developer 40 41 .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 42 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 43 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 44 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 45 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 46 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 47 PetscOptionsFList(), PetscOptionsEList() 48 @*/ 49 PetscErrorCode TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer)) 50 { 51 PetscErrorCode ierr; 52 PetscViewer viewer; 53 PetscViewerFormat format; 54 PetscBool flg; 55 56 PetscFunctionBegin; 57 ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 58 if (flg) { 59 ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 60 if (monitorsetup) { 61 ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 62 } 63 ierr = TSMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 64 } 65 PetscFunctionReturn(0); 66 } 67 68 #undef __FUNCT__ 69 #define __FUNCT__ "TSAdjointMonitorSetFromOptions" 70 /*@C 71 TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 72 73 Collective on TS 74 75 Input Parameters: 76 + ts - TS object you wish to monitor 77 . name - the monitor type one is seeking 78 . help - message indicating what monitoring is done 79 . manual - manual page for the monitor 80 . monitor - the monitor function 81 - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects 82 83 Level: developer 84 85 .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 86 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 87 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 88 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 89 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 90 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 91 PetscOptionsFList(), PetscOptionsEList() 92 @*/ 93 PetscErrorCode TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),PetscErrorCode (*monitorsetup)(TS,PetscViewer)) 94 { 95 PetscErrorCode ierr; 96 PetscViewer viewer; 97 PetscViewerFormat format; 98 PetscBool flg; 99 100 PetscFunctionBegin; 101 ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 102 if (flg) { 103 ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 104 if (monitorsetup) { 105 ierr = (*monitorsetup)(ts,viewer);CHKERRQ(ierr); 106 } 107 ierr = TSAdjointMonitorSet(ts,monitor,viewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 108 } 109 PetscFunctionReturn(0); 110 } 111 112 #undef __FUNCT__ 113 #define __FUNCT__ "TSSetFromOptions" 114 /*@ 115 TSSetFromOptions - Sets various TS parameters from user options. 116 117 Collective on TS 118 119 Input Parameter: 120 . ts - the TS context obtained from TSCreate() 121 122 Options Database Keys: 123 + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 124 . -ts_save_trajectory - checkpoint the solution at each time-step 125 . -ts_max_steps <maxsteps> - maximum number of time-steps to take 126 . -ts_final_time <time> - maximum time to compute to 127 . -ts_dt <dt> - initial time step 128 . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 129 . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 130 . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 131 . -ts_error_if_step_fails <true,false> - Error if no step succeeds 132 . -ts_rtol <rtol> - relative tolerance for local truncation error 133 . -ts_atol <atol> Absolute tolerance for local truncation error 134 . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 135 . -ts_fd_color - Use finite differences with coloring to compute IJacobian 136 . -ts_monitor - print information at each timestep 137 . -ts_monitor_lg_timestep - Monitor timestep size graphically 138 . -ts_monitor_lg_solution - Monitor solution graphically 139 . -ts_monitor_lg_error - Monitor error graphically 140 . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 141 . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 142 . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 143 . -ts_monitor_draw_solution - Monitor solution graphically 144 . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 145 . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 146 . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 147 . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 148 . -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 149 . -ts_adjoint_monitor - print information at each adjoint time step 150 - -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically 151 152 Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 153 154 Level: beginner 155 156 .keywords: TS, timestep, set, options, database 157 158 .seealso: TSGetType() 159 @*/ 160 PetscErrorCode TSSetFromOptions(TS ts) 161 { 162 PetscBool opt,flg,tflg; 163 PetscErrorCode ierr; 164 char monfilename[PETSC_MAX_PATH_LEN]; 165 SNES snes; 166 PetscReal time_step; 167 TSExactFinalTimeOption eftopt; 168 char dir[16]; 169 const char *defaultType; 170 char typeName[256]; 171 172 PetscFunctionBegin; 173 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 174 ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 175 if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 176 else defaultType = TSEULER; 177 178 ierr = TSRegisterAll();CHKERRQ(ierr); 179 ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 180 if (opt) { 181 ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 182 } else { 183 ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 184 } 185 186 /* Handle generic TS options */ 187 ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 188 ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 189 ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 190 ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 191 if (flg) { 192 ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 193 } 194 ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr); 195 if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 196 ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr); 197 ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 198 ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 199 ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 200 ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 201 202 #if defined(PETSC_HAVE_SAWS) 203 { 204 PetscBool set; 205 flg = PETSC_FALSE; 206 ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 207 if (set) { 208 ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 209 } 210 } 211 #endif 212 213 /* Monitor options */ 214 ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 215 ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 216 ierr = TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);CHKERRQ(ierr); 217 218 ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 219 if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 220 221 ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 222 if (opt) { 223 TSMonitorLGCtx ctx; 224 PetscInt howoften = 1; 225 226 ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 227 ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 228 ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 229 } 230 ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 231 if (opt) { 232 TSMonitorLGCtx ctx; 233 PetscInt howoften = 1; 234 235 ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 236 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 237 ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 238 } 239 ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 240 if (opt) { 241 TSMonitorLGCtx ctx; 242 PetscInt howoften = 1; 243 244 ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 245 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 246 ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 247 } 248 ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 249 if (opt) { 250 TSMonitorLGCtx ctx; 251 PetscInt howoften = 1; 252 253 ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 254 ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 255 ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 256 } 257 ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 258 if (opt) { 259 TSMonitorLGCtx ctx; 260 PetscInt howoften = 1; 261 262 ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 263 ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 264 ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 265 } 266 ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 267 if (opt) { 268 TSMonitorSPEigCtx ctx; 269 PetscInt howoften = 1; 270 271 ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 272 ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 273 ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 274 } 275 opt = PETSC_FALSE; 276 ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 277 if (opt) { 278 TSMonitorDrawCtx ctx; 279 PetscInt howoften = 1; 280 281 ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 282 ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 283 ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 284 } 285 opt = PETSC_FALSE; 286 ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 287 if (opt) { 288 TSMonitorDrawCtx ctx; 289 PetscInt howoften = 1; 290 291 ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 292 ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 293 ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 294 } 295 opt = PETSC_FALSE; 296 ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 297 if (opt) { 298 TSMonitorDrawCtx ctx; 299 PetscReal bounds[4]; 300 PetscInt n = 4; 301 PetscDraw draw; 302 303 ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 304 if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 305 ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 306 ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 307 ierr = PetscDrawClear(draw);CHKERRQ(ierr); 308 ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 309 ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 310 ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 311 ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 312 /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 313 ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 314 } 315 opt = PETSC_FALSE; 316 ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 317 if (opt) { 318 TSMonitorDrawCtx ctx; 319 PetscInt howoften = 1; 320 321 ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 322 ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 323 ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 324 } 325 326 opt = PETSC_FALSE; 327 ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 328 if (flg) { 329 const char *ptr,*ptr2; 330 char *filetemplate; 331 if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 332 /* Do some cursory validation of the input. */ 333 ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 334 if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 335 for (ptr++; ptr && *ptr; ptr++) { 336 ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 337 if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 338 if (ptr2) break; 339 } 340 ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 341 ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 342 } 343 344 ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 345 if (flg) { 346 TSMonitorDMDARayCtx *rayctx; 347 int ray = 0; 348 DMDADirection ddir; 349 DM da; 350 PetscMPIInt rank; 351 352 if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 353 if (dir[0] == 'x') ddir = DMDA_X; 354 else if (dir[0] == 'y') ddir = DMDA_Y; 355 else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 356 sscanf(dir+2,"%d",&ray); 357 358 ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 359 ierr = PetscNew(&rayctx);CHKERRQ(ierr); 360 ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 361 ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 362 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 363 if (!rank) { 364 ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 365 } 366 rayctx->lgctx = NULL; 367 ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 368 } 369 ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 370 if (flg) { 371 TSMonitorDMDARayCtx *rayctx; 372 int ray = 0; 373 DMDADirection ddir; 374 DM da; 375 PetscInt howoften = 1; 376 377 if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 378 if (dir[0] == 'x') ddir = DMDA_X; 379 else if (dir[0] == 'y') ddir = DMDA_Y; 380 else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 381 sscanf(dir+2, "%d", &ray); 382 383 ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 384 ierr = PetscNew(&rayctx);CHKERRQ(ierr); 385 ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 386 ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 387 ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 388 ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 389 } 390 391 ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 392 if (opt) { 393 TSMonitorEnvelopeCtx ctx; 394 395 ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 396 ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 397 } 398 399 flg = PETSC_FALSE; 400 ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 401 if (flg) { 402 DM dm; 403 DMTS tdm; 404 405 ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 406 ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 407 tdm->ijacobianctx = NULL; 408 ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 409 ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 410 } 411 412 if (ts->adapt) { 413 ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 414 } 415 416 /* Handle specific TS options */ 417 if (ts->ops->setfromoptions) { 418 ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 419 } 420 421 /* TS trajectory must be set after TS, since it may use some TS options above */ 422 if (ts->trajectory) tflg = PETSC_TRUE; 423 else tflg = PETSC_FALSE; 424 ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 425 if (tflg) { 426 ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 427 } 428 if (ts->adjoint_solve) tflg = PETSC_TRUE; 429 else tflg = PETSC_FALSE; 430 ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 431 if (flg) { 432 ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 433 ts->adjoint_solve = tflg; 434 } 435 if (ts->trajectory) { 436 ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 437 } 438 439 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 440 ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 441 ierr = PetscOptionsEnd();CHKERRQ(ierr); 442 443 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 444 if (snes) { 445 if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 446 ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 447 } 448 449 PetscFunctionReturn(0); 450 } 451 452 #undef __FUNCT__ 453 #define __FUNCT__ "TSSetSaveTrajectory" 454 /*@ 455 TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 456 457 Collective on TS 458 459 Input Parameters: 460 . ts - the TS context obtained from TSCreate() 461 462 Note: This routine should be called after all TS options have been set 463 464 Level: intermediate 465 466 .seealso: TSGetTrajectory(), TSAdjointSolve() 467 468 .keywords: TS, set, checkpoint, 469 @*/ 470 PetscErrorCode TSSetSaveTrajectory(TS ts) 471 { 472 PetscErrorCode ierr; 473 474 PetscFunctionBegin; 475 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 476 if (!ts->trajectory) { 477 ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 478 ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 479 } 480 PetscFunctionReturn(0); 481 } 482 483 #undef __FUNCT__ 484 #define __FUNCT__ "TSComputeRHSJacobian" 485 /*@ 486 TSComputeRHSJacobian - Computes the Jacobian matrix that has been 487 set with TSSetRHSJacobian(). 488 489 Collective on TS and Vec 490 491 Input Parameters: 492 + ts - the TS context 493 . t - current timestep 494 - U - input vector 495 496 Output Parameters: 497 + A - Jacobian matrix 498 . B - optional preconditioning matrix 499 - flag - flag indicating matrix structure 500 501 Notes: 502 Most users should not need to explicitly call this routine, as it 503 is used internally within the nonlinear solvers. 504 505 See KSPSetOperators() for important information about setting the 506 flag parameter. 507 508 Level: developer 509 510 .keywords: SNES, compute, Jacobian, matrix 511 512 .seealso: TSSetRHSJacobian(), KSPSetOperators() 513 @*/ 514 PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 515 { 516 PetscErrorCode ierr; 517 PetscObjectState Ustate; 518 DM dm; 519 DMTS tsdm; 520 TSRHSJacobian rhsjacobianfunc; 521 void *ctx; 522 TSIJacobian ijacobianfunc; 523 TSRHSFunction rhsfunction; 524 525 PetscFunctionBegin; 526 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 527 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 528 PetscCheckSameComm(ts,1,U,3); 529 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 530 ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 531 ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 532 ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 533 ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 534 ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 535 if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 536 PetscFunctionReturn(0); 537 } 538 539 if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 540 541 if (ts->rhsjacobian.reuse) { 542 ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 543 ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 544 if (A != B) { 545 ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 546 ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 547 } 548 ts->rhsjacobian.shift = 0; 549 ts->rhsjacobian.scale = 1.; 550 } 551 552 if (rhsjacobianfunc) { 553 PetscBool missing; 554 ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 555 PetscStackPush("TS user Jacobian function"); 556 ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 557 PetscStackPop; 558 ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 559 if (A) { 560 ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 561 if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 562 } 563 if (B && B != A) { 564 ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 565 if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 566 } 567 } else { 568 ierr = MatZeroEntries(A);CHKERRQ(ierr); 569 if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 570 } 571 ts->rhsjacobian.time = t; 572 ts->rhsjacobian.X = U; 573 ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 574 PetscFunctionReturn(0); 575 } 576 577 #undef __FUNCT__ 578 #define __FUNCT__ "TSComputeRHSFunction" 579 /*@ 580 TSComputeRHSFunction - Evaluates the right-hand-side function. 581 582 Collective on TS and Vec 583 584 Input Parameters: 585 + ts - the TS context 586 . t - current time 587 - U - state vector 588 589 Output Parameter: 590 . y - right hand side 591 592 Note: 593 Most users should not need to explicitly call this routine, as it 594 is used internally within the nonlinear solvers. 595 596 Level: developer 597 598 .keywords: TS, compute 599 600 .seealso: TSSetRHSFunction(), TSComputeIFunction() 601 @*/ 602 PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 603 { 604 PetscErrorCode ierr; 605 TSRHSFunction rhsfunction; 606 TSIFunction ifunction; 607 void *ctx; 608 DM dm; 609 610 PetscFunctionBegin; 611 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 612 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 613 PetscValidHeaderSpecific(y,VEC_CLASSID,4); 614 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 615 ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 616 ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 617 618 if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 619 620 ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 621 if (rhsfunction) { 622 PetscStackPush("TS user right-hand-side function"); 623 ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 624 PetscStackPop; 625 } else { 626 ierr = VecZeroEntries(y);CHKERRQ(ierr); 627 } 628 629 ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 630 PetscFunctionReturn(0); 631 } 632 633 #undef __FUNCT__ 634 #define __FUNCT__ "TSComputeSolutionFunction" 635 /*@ 636 TSComputeSolutionFunction - Evaluates the solution function. 637 638 Collective on TS and Vec 639 640 Input Parameters: 641 + ts - the TS context 642 - t - current time 643 644 Output Parameter: 645 . U - the solution 646 647 Note: 648 Most users should not need to explicitly call this routine, as it 649 is used internally within the nonlinear solvers. 650 651 Level: developer 652 653 .keywords: TS, compute 654 655 .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 656 @*/ 657 PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 658 { 659 PetscErrorCode ierr; 660 TSSolutionFunction solutionfunction; 661 void *ctx; 662 DM dm; 663 664 PetscFunctionBegin; 665 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 666 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 667 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 668 ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 669 670 if (solutionfunction) { 671 PetscStackPush("TS user solution function"); 672 ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 673 PetscStackPop; 674 } 675 PetscFunctionReturn(0); 676 } 677 #undef __FUNCT__ 678 #define __FUNCT__ "TSComputeForcingFunction" 679 /*@ 680 TSComputeForcingFunction - Evaluates the forcing function. 681 682 Collective on TS and Vec 683 684 Input Parameters: 685 + ts - the TS context 686 - t - current time 687 688 Output Parameter: 689 . U - the function value 690 691 Note: 692 Most users should not need to explicitly call this routine, as it 693 is used internally within the nonlinear solvers. 694 695 Level: developer 696 697 .keywords: TS, compute 698 699 .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 700 @*/ 701 PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 702 { 703 PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 704 void *ctx; 705 DM dm; 706 707 PetscFunctionBegin; 708 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 709 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 710 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 711 ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 712 713 if (forcing) { 714 PetscStackPush("TS user forcing function"); 715 ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 716 PetscStackPop; 717 } 718 PetscFunctionReturn(0); 719 } 720 721 #undef __FUNCT__ 722 #define __FUNCT__ "TSGetRHSVec_Private" 723 static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 724 { 725 Vec F; 726 PetscErrorCode ierr; 727 728 PetscFunctionBegin; 729 *Frhs = NULL; 730 ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 731 if (!ts->Frhs) { 732 ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 733 } 734 *Frhs = ts->Frhs; 735 PetscFunctionReturn(0); 736 } 737 738 #undef __FUNCT__ 739 #define __FUNCT__ "TSGetRHSMats_Private" 740 static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 741 { 742 Mat A,B; 743 PetscErrorCode ierr; 744 745 PetscFunctionBegin; 746 if (Arhs) *Arhs = NULL; 747 if (Brhs) *Brhs = NULL; 748 ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 749 if (Arhs) { 750 if (!ts->Arhs) { 751 ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 752 } 753 *Arhs = ts->Arhs; 754 } 755 if (Brhs) { 756 if (!ts->Brhs) { 757 if (A != B) { 758 ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 759 } else { 760 ts->Brhs = ts->Arhs; 761 ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 762 } 763 } 764 *Brhs = ts->Brhs; 765 } 766 PetscFunctionReturn(0); 767 } 768 769 #undef __FUNCT__ 770 #define __FUNCT__ "TSComputeIFunction" 771 /*@ 772 TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 773 774 Collective on TS and Vec 775 776 Input Parameters: 777 + ts - the TS context 778 . t - current time 779 . U - state vector 780 . Udot - time derivative of state vector 781 - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 782 783 Output Parameter: 784 . Y - right hand side 785 786 Note: 787 Most users should not need to explicitly call this routine, as it 788 is used internally within the nonlinear solvers. 789 790 If the user did did not write their equations in implicit form, this 791 function recasts them in implicit form. 792 793 Level: developer 794 795 .keywords: TS, compute 796 797 .seealso: TSSetIFunction(), TSComputeRHSFunction() 798 @*/ 799 PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 800 { 801 PetscErrorCode ierr; 802 TSIFunction ifunction; 803 TSRHSFunction rhsfunction; 804 void *ctx; 805 DM dm; 806 807 PetscFunctionBegin; 808 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 809 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 810 PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 811 PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 812 813 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 814 ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 815 ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 816 817 if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 818 819 ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 820 if (ifunction) { 821 PetscStackPush("TS user implicit function"); 822 ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 823 PetscStackPop; 824 } 825 if (imex) { 826 if (!ifunction) { 827 ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 828 } 829 } else if (rhsfunction) { 830 if (ifunction) { 831 Vec Frhs; 832 ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 833 ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 834 ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 835 } else { 836 ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 837 ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 838 } 839 } 840 ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 841 PetscFunctionReturn(0); 842 } 843 844 #undef __FUNCT__ 845 #define __FUNCT__ "TSComputeIJacobian" 846 /*@ 847 TSComputeIJacobian - Evaluates the Jacobian of the DAE 848 849 Collective on TS and Vec 850 851 Input 852 Input Parameters: 853 + ts - the TS context 854 . t - current timestep 855 . U - state vector 856 . Udot - time derivative of state vector 857 . shift - shift to apply, see note below 858 - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 859 860 Output Parameters: 861 + A - Jacobian matrix 862 . B - optional preconditioning matrix 863 - flag - flag indicating matrix structure 864 865 Notes: 866 If F(t,U,Udot)=0 is the DAE, the required Jacobian is 867 868 dF/dU + shift*dF/dUdot 869 870 Most users should not need to explicitly call this routine, as it 871 is used internally within the nonlinear solvers. 872 873 Level: developer 874 875 .keywords: TS, compute, Jacobian, matrix 876 877 .seealso: TSSetIJacobian() 878 @*/ 879 PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 880 { 881 PetscErrorCode ierr; 882 TSIJacobian ijacobian; 883 TSRHSJacobian rhsjacobian; 884 DM dm; 885 void *ctx; 886 887 PetscFunctionBegin; 888 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 889 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 890 PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 891 PetscValidPointer(A,6); 892 PetscValidHeaderSpecific(A,MAT_CLASSID,6); 893 PetscValidPointer(B,7); 894 PetscValidHeaderSpecific(B,MAT_CLASSID,7); 895 896 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 897 ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 898 ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 899 900 if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 901 902 ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 903 if (ijacobian) { 904 PetscBool missing; 905 PetscStackPush("TS user implicit Jacobian"); 906 ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 907 PetscStackPop; 908 if (A) { 909 ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr); 910 if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 911 } 912 if (B && B != A) { 913 ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr); 914 if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value"); 915 } 916 } 917 if (imex) { 918 if (!ijacobian) { /* system was written as Udot = G(t,U) */ 919 ierr = MatZeroEntries(A);CHKERRQ(ierr); 920 ierr = MatShift(A,shift);CHKERRQ(ierr); 921 if (A != B) { 922 ierr = MatZeroEntries(B);CHKERRQ(ierr); 923 ierr = MatShift(B,shift);CHKERRQ(ierr); 924 } 925 } 926 } else { 927 Mat Arhs = NULL,Brhs = NULL; 928 if (rhsjacobian) { 929 if (ijacobian) { 930 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 931 } else { 932 ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 933 } 934 ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 935 } 936 if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 937 ts->rhsjacobian.scale = -1; 938 ts->rhsjacobian.shift = shift; 939 ierr = MatScale(A,-1);CHKERRQ(ierr); 940 ierr = MatShift(A,shift);CHKERRQ(ierr); 941 if (A != B) { 942 ierr = MatScale(B,-1);CHKERRQ(ierr); 943 ierr = MatShift(B,shift);CHKERRQ(ierr); 944 } 945 } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 946 MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 947 if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 948 ierr = MatZeroEntries(A);CHKERRQ(ierr); 949 ierr = MatShift(A,shift);CHKERRQ(ierr); 950 if (A != B) { 951 ierr = MatZeroEntries(B);CHKERRQ(ierr); 952 ierr = MatShift(B,shift);CHKERRQ(ierr); 953 } 954 } 955 ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 956 if (A != B) { 957 ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 958 } 959 } 960 } 961 ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 962 PetscFunctionReturn(0); 963 } 964 965 #undef __FUNCT__ 966 #define __FUNCT__ "TSSetRHSFunction" 967 /*@C 968 TSSetRHSFunction - Sets the routine for evaluating the function, 969 where U_t = G(t,u). 970 971 Logically Collective on TS 972 973 Input Parameters: 974 + ts - the TS context obtained from TSCreate() 975 . r - vector to put the computed right hand side (or NULL to have it created) 976 . f - routine for evaluating the right-hand-side function 977 - ctx - [optional] user-defined context for private data for the 978 function evaluation routine (may be NULL) 979 980 Calling sequence of func: 981 $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 982 983 + t - current timestep 984 . u - input vector 985 . F - function vector 986 - ctx - [optional] user-defined function context 987 988 Level: beginner 989 990 Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 991 992 .keywords: TS, timestep, set, right-hand-side, function 993 994 .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 995 @*/ 996 PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 997 { 998 PetscErrorCode ierr; 999 SNES snes; 1000 Vec ralloc = NULL; 1001 DM dm; 1002 1003 PetscFunctionBegin; 1004 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1005 if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1006 1007 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1008 ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1009 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1010 if (!r && !ts->dm && ts->vec_sol) { 1011 ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1012 r = ralloc; 1013 } 1014 ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1015 ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1016 PetscFunctionReturn(0); 1017 } 1018 1019 #undef __FUNCT__ 1020 #define __FUNCT__ "TSSetSolutionFunction" 1021 /*@C 1022 TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1023 1024 Logically Collective on TS 1025 1026 Input Parameters: 1027 + ts - the TS context obtained from TSCreate() 1028 . f - routine for evaluating the solution 1029 - ctx - [optional] user-defined context for private data for the 1030 function evaluation routine (may be NULL) 1031 1032 Calling sequence of func: 1033 $ func (TS ts,PetscReal t,Vec u,void *ctx); 1034 1035 + t - current timestep 1036 . u - output vector 1037 - ctx - [optional] user-defined function context 1038 1039 Notes: 1040 This routine is used for testing accuracy of time integration schemes when you already know the solution. 1041 If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1042 create closed-form solutions with non-physical forcing terms. 1043 1044 For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1045 1046 Level: beginner 1047 1048 .keywords: TS, timestep, set, right-hand-side, function 1049 1050 .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 1051 @*/ 1052 PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1053 { 1054 PetscErrorCode ierr; 1055 DM dm; 1056 1057 PetscFunctionBegin; 1058 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1059 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1060 ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1061 PetscFunctionReturn(0); 1062 } 1063 1064 #undef __FUNCT__ 1065 #define __FUNCT__ "TSSetForcingFunction" 1066 /*@C 1067 TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 1068 1069 Logically Collective on TS 1070 1071 Input Parameters: 1072 + ts - the TS context obtained from TSCreate() 1073 . f - routine for evaluating the forcing function 1074 - ctx - [optional] user-defined context for private data for the 1075 function evaluation routine (may be NULL) 1076 1077 Calling sequence of func: 1078 $ func (TS ts,PetscReal t,Vec u,void *ctx); 1079 1080 + t - current timestep 1081 . u - output vector 1082 - ctx - [optional] user-defined function context 1083 1084 Notes: 1085 This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1086 create closed-form solutions with a non-physical forcing term. 1087 1088 For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1089 1090 Level: beginner 1091 1092 .keywords: TS, timestep, set, right-hand-side, function 1093 1094 .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 1095 @*/ 1096 PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1097 { 1098 PetscErrorCode ierr; 1099 DM dm; 1100 1101 PetscFunctionBegin; 1102 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1103 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1104 ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 1105 PetscFunctionReturn(0); 1106 } 1107 1108 #undef __FUNCT__ 1109 #define __FUNCT__ "TSSetRHSJacobian" 1110 /*@C 1111 TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1112 where U_t = G(U,t), as well as the location to store the matrix. 1113 1114 Logically Collective on TS 1115 1116 Input Parameters: 1117 + ts - the TS context obtained from TSCreate() 1118 . Amat - (approximate) Jacobian matrix 1119 . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1120 . f - the Jacobian evaluation routine 1121 - ctx - [optional] user-defined context for private data for the 1122 Jacobian evaluation routine (may be NULL) 1123 1124 Calling sequence of f: 1125 $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1126 1127 + t - current timestep 1128 . u - input vector 1129 . Amat - (approximate) Jacobian matrix 1130 . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1131 - ctx - [optional] user-defined context for matrix evaluation routine 1132 1133 Notes: 1134 You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 1135 1136 The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1137 You should not assume the values are the same in the next call to f() as you set them in the previous call. 1138 1139 Level: beginner 1140 1141 .keywords: TS, timestep, set, right-hand-side, Jacobian 1142 1143 .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1144 1145 @*/ 1146 PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1147 { 1148 PetscErrorCode ierr; 1149 SNES snes; 1150 DM dm; 1151 TSIJacobian ijacobian; 1152 1153 PetscFunctionBegin; 1154 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1155 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1156 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1157 if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1158 if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1159 1160 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1161 ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1162 if (f == TSComputeRHSJacobianConstant) { 1163 /* Handle this case automatically for the user; otherwise user should call themselves. */ 1164 ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1165 } 1166 ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1167 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1168 if (!ijacobian) { 1169 ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1170 } 1171 if (Amat) { 1172 ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 1173 ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1174 1175 ts->Arhs = Amat; 1176 } 1177 if (Pmat) { 1178 ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 1179 ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1180 1181 ts->Brhs = Pmat; 1182 } 1183 PetscFunctionReturn(0); 1184 } 1185 1186 1187 #undef __FUNCT__ 1188 #define __FUNCT__ "TSSetIFunction" 1189 /*@C 1190 TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1191 1192 Logically Collective on TS 1193 1194 Input Parameters: 1195 + ts - the TS context obtained from TSCreate() 1196 . r - vector to hold the residual (or NULL to have it created internally) 1197 . f - the function evaluation routine 1198 - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1199 1200 Calling sequence of f: 1201 $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1202 1203 + t - time at step/stage being solved 1204 . u - state vector 1205 . u_t - time derivative of state vector 1206 . F - function vector 1207 - ctx - [optional] user-defined context for matrix evaluation routine 1208 1209 Important: 1210 The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1211 1212 Level: beginner 1213 1214 .keywords: TS, timestep, set, DAE, Jacobian 1215 1216 .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1217 @*/ 1218 PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1219 { 1220 PetscErrorCode ierr; 1221 SNES snes; 1222 Vec resalloc = NULL; 1223 DM dm; 1224 1225 PetscFunctionBegin; 1226 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1227 if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 1228 1229 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1230 ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 1231 1232 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1233 if (!res && !ts->dm && ts->vec_sol) { 1234 ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1235 res = resalloc; 1236 } 1237 ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1238 ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1239 PetscFunctionReturn(0); 1240 } 1241 1242 #undef __FUNCT__ 1243 #define __FUNCT__ "TSGetIFunction" 1244 /*@C 1245 TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1246 1247 Not Collective 1248 1249 Input Parameter: 1250 . ts - the TS context 1251 1252 Output Parameter: 1253 + r - vector to hold residual (or NULL) 1254 . func - the function to compute residual (or NULL) 1255 - ctx - the function context (or NULL) 1256 1257 Level: advanced 1258 1259 .keywords: TS, nonlinear, get, function 1260 1261 .seealso: TSSetIFunction(), SNESGetFunction() 1262 @*/ 1263 PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1264 { 1265 PetscErrorCode ierr; 1266 SNES snes; 1267 DM dm; 1268 1269 PetscFunctionBegin; 1270 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1271 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1272 ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1273 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1274 ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1275 PetscFunctionReturn(0); 1276 } 1277 1278 #undef __FUNCT__ 1279 #define __FUNCT__ "TSGetRHSFunction" 1280 /*@C 1281 TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1282 1283 Not Collective 1284 1285 Input Parameter: 1286 . ts - the TS context 1287 1288 Output Parameter: 1289 + r - vector to hold computed right hand side (or NULL) 1290 . func - the function to compute right hand side (or NULL) 1291 - ctx - the function context (or NULL) 1292 1293 Level: advanced 1294 1295 .keywords: TS, nonlinear, get, function 1296 1297 .seealso: TSSetRHSFunction(), SNESGetFunction() 1298 @*/ 1299 PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1300 { 1301 PetscErrorCode ierr; 1302 SNES snes; 1303 DM dm; 1304 1305 PetscFunctionBegin; 1306 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1307 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1308 ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1309 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1310 ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1311 PetscFunctionReturn(0); 1312 } 1313 1314 #undef __FUNCT__ 1315 #define __FUNCT__ "TSSetIJacobian" 1316 /*@C 1317 TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1318 provided with TSSetIFunction(). 1319 1320 Logically Collective on TS 1321 1322 Input Parameters: 1323 + ts - the TS context obtained from TSCreate() 1324 . Amat - (approximate) Jacobian matrix 1325 . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1326 . f - the Jacobian evaluation routine 1327 - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1328 1329 Calling sequence of f: 1330 $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1331 1332 + t - time at step/stage being solved 1333 . U - state vector 1334 . U_t - time derivative of state vector 1335 . a - shift 1336 . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1337 . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1338 - ctx - [optional] user-defined context for matrix evaluation routine 1339 1340 Notes: 1341 The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1342 1343 If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1344 space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1345 1346 The matrix dF/dU + a*dF/dU_t you provide turns out to be 1347 the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1348 The time integrator internally approximates U_t by W+a*U where the positive "shift" 1349 a and vector W depend on the integration method, step size, and past states. For example with 1350 the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1351 W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1352 1353 You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 1354 1355 The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1356 You should not assume the values are the same in the next call to f() as you set them in the previous call. 1357 1358 Level: beginner 1359 1360 .keywords: TS, timestep, DAE, Jacobian 1361 1362 .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1363 1364 @*/ 1365 PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1366 { 1367 PetscErrorCode ierr; 1368 SNES snes; 1369 DM dm; 1370 1371 PetscFunctionBegin; 1372 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1373 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1374 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1375 if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1376 if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1377 1378 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1379 ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 1380 1381 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1382 ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1383 PetscFunctionReturn(0); 1384 } 1385 1386 #undef __FUNCT__ 1387 #define __FUNCT__ "TSRHSJacobianSetReuse" 1388 /*@ 1389 TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1390 shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1391 the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1392 not been changed by the TS. 1393 1394 Logically Collective 1395 1396 Input Arguments: 1397 + ts - TS context obtained from TSCreate() 1398 - reuse - PETSC_TRUE if the RHS Jacobian 1399 1400 Level: intermediate 1401 1402 .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1403 @*/ 1404 PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1405 { 1406 PetscFunctionBegin; 1407 ts->rhsjacobian.reuse = reuse; 1408 PetscFunctionReturn(0); 1409 } 1410 1411 #undef __FUNCT__ 1412 #define __FUNCT__ "TSLoad" 1413 /*@C 1414 TSLoad - Loads a KSP that has been stored in binary with KSPView(). 1415 1416 Collective on PetscViewer 1417 1418 Input Parameters: 1419 + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 1420 some related function before a call to TSLoad(). 1421 - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 1422 1423 Level: intermediate 1424 1425 Notes: 1426 The type is determined by the data in the file, any type set into the TS before this call is ignored. 1427 1428 Notes for advanced users: 1429 Most users should not need to know the details of the binary storage 1430 format, since TSLoad() and TSView() completely hide these details. 1431 But for anyone who's interested, the standard binary matrix storage 1432 format is 1433 .vb 1434 has not yet been determined 1435 .ve 1436 1437 .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 1438 @*/ 1439 PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1440 { 1441 PetscErrorCode ierr; 1442 PetscBool isbinary; 1443 PetscInt classid; 1444 char type[256]; 1445 DMTS sdm; 1446 DM dm; 1447 1448 PetscFunctionBegin; 1449 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1450 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1451 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1452 if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 1453 1454 ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1455 if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1456 ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1457 ierr = TSSetType(ts, type);CHKERRQ(ierr); 1458 if (ts->ops->load) { 1459 ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1460 } 1461 ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1462 ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1463 ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1464 ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1465 ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 1466 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1467 ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 1468 PetscFunctionReturn(0); 1469 } 1470 1471 #include <petscdraw.h> 1472 #if defined(PETSC_HAVE_SAWS) 1473 #include <petscviewersaws.h> 1474 #endif 1475 #undef __FUNCT__ 1476 #define __FUNCT__ "TSView" 1477 /*@C 1478 TSView - Prints the TS data structure. 1479 1480 Collective on TS 1481 1482 Input Parameters: 1483 + ts - the TS context obtained from TSCreate() 1484 - viewer - visualization context 1485 1486 Options Database Key: 1487 . -ts_view - calls TSView() at end of TSStep() 1488 1489 Notes: 1490 The available visualization contexts include 1491 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1492 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1493 output where only the first processor opens 1494 the file. All other processors send their 1495 data to the first processor to print. 1496 1497 The user can open an alternative visualization context with 1498 PetscViewerASCIIOpen() - output to a specified file. 1499 1500 Level: beginner 1501 1502 .keywords: TS, timestep, view 1503 1504 .seealso: PetscViewerASCIIOpen() 1505 @*/ 1506 PetscErrorCode TSView(TS ts,PetscViewer viewer) 1507 { 1508 PetscErrorCode ierr; 1509 TSType type; 1510 PetscBool iascii,isstring,isundials,isbinary,isdraw; 1511 DMTS sdm; 1512 #if defined(PETSC_HAVE_SAWS) 1513 PetscBool issaws; 1514 #endif 1515 1516 PetscFunctionBegin; 1517 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1518 if (!viewer) { 1519 ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 1520 } 1521 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1522 PetscCheckSameComm(ts,1,viewer,2); 1523 1524 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1525 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 1526 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1527 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1528 #if defined(PETSC_HAVE_SAWS) 1529 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1530 #endif 1531 if (iascii) { 1532 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 1533 ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 1534 ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1535 if (ts->problem_type == TS_NONLINEAR) { 1536 ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1537 ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1538 } 1539 ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1540 ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 1541 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1542 ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1543 if (ts->ops->view) { 1544 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1545 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1546 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1547 } 1548 } else if (isstring) { 1549 ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1550 ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 1551 } else if (isbinary) { 1552 PetscInt classid = TS_FILE_CLASSID; 1553 MPI_Comm comm; 1554 PetscMPIInt rank; 1555 char type[256]; 1556 1557 ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 1558 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1559 if (!rank) { 1560 ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 1561 ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 1562 ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 1563 } 1564 if (ts->ops->view) { 1565 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1566 } 1567 ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1568 ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 1569 ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 1570 ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1571 } else if (isdraw) { 1572 PetscDraw draw; 1573 char str[36]; 1574 PetscReal x,y,bottom,h; 1575 1576 ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1577 ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 1578 ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 1579 ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 1580 ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 1581 bottom = y - h; 1582 ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 1583 if (ts->ops->view) { 1584 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1585 } 1586 ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1587 #if defined(PETSC_HAVE_SAWS) 1588 } else if (issaws) { 1589 PetscMPIInt rank; 1590 const char *name; 1591 1592 ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1593 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1594 if (!((PetscObject)ts)->amsmem && !rank) { 1595 char dir[1024]; 1596 1597 ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1598 ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 1599 PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 1600 ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 1601 PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1602 } 1603 if (ts->ops->view) { 1604 ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1605 } 1606 #endif 1607 } 1608 1609 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1610 ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1611 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1612 PetscFunctionReturn(0); 1613 } 1614 1615 1616 #undef __FUNCT__ 1617 #define __FUNCT__ "TSSetApplicationContext" 1618 /*@ 1619 TSSetApplicationContext - Sets an optional user-defined context for 1620 the timesteppers. 1621 1622 Logically Collective on TS 1623 1624 Input Parameters: 1625 + ts - the TS context obtained from TSCreate() 1626 - usrP - optional user context 1627 1628 Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1629 function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1630 1631 Level: intermediate 1632 1633 .keywords: TS, timestep, set, application, context 1634 1635 .seealso: TSGetApplicationContext() 1636 @*/ 1637 PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1638 { 1639 PetscFunctionBegin; 1640 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1641 ts->user = usrP; 1642 PetscFunctionReturn(0); 1643 } 1644 1645 #undef __FUNCT__ 1646 #define __FUNCT__ "TSGetApplicationContext" 1647 /*@ 1648 TSGetApplicationContext - Gets the user-defined context for the 1649 timestepper. 1650 1651 Not Collective 1652 1653 Input Parameter: 1654 . ts - the TS context obtained from TSCreate() 1655 1656 Output Parameter: 1657 . usrP - user context 1658 1659 Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 1660 function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1661 1662 Level: intermediate 1663 1664 .keywords: TS, timestep, get, application, context 1665 1666 .seealso: TSSetApplicationContext() 1667 @*/ 1668 PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1669 { 1670 PetscFunctionBegin; 1671 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1672 *(void**)usrP = ts->user; 1673 PetscFunctionReturn(0); 1674 } 1675 1676 #undef __FUNCT__ 1677 #define __FUNCT__ "TSGetTimeStepNumber" 1678 /*@ 1679 TSGetTimeStepNumber - Gets the number of time steps completed. 1680 1681 Not Collective 1682 1683 Input Parameter: 1684 . ts - the TS context obtained from TSCreate() 1685 1686 Output Parameter: 1687 . iter - number of steps completed so far 1688 1689 Level: intermediate 1690 1691 .keywords: TS, timestep, get, iteration, number 1692 .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1693 @*/ 1694 PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1695 { 1696 PetscFunctionBegin; 1697 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1698 PetscValidIntPointer(iter,2); 1699 *iter = ts->steps; 1700 PetscFunctionReturn(0); 1701 } 1702 1703 #undef __FUNCT__ 1704 #define __FUNCT__ "TSSetInitialTimeStep" 1705 /*@ 1706 TSSetInitialTimeStep - Sets the initial timestep to be used, 1707 as well as the initial time. 1708 1709 Logically Collective on TS 1710 1711 Input Parameters: 1712 + ts - the TS context obtained from TSCreate() 1713 . initial_time - the initial time 1714 - time_step - the size of the timestep 1715 1716 Level: intermediate 1717 1718 .seealso: TSSetTimeStep(), TSGetTimeStep() 1719 1720 .keywords: TS, set, initial, timestep 1721 @*/ 1722 PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1723 { 1724 PetscErrorCode ierr; 1725 1726 PetscFunctionBegin; 1727 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1728 ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1729 ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1730 PetscFunctionReturn(0); 1731 } 1732 1733 #undef __FUNCT__ 1734 #define __FUNCT__ "TSSetTimeStep" 1735 /*@ 1736 TSSetTimeStep - Allows one to reset the timestep at any time, 1737 useful for simple pseudo-timestepping codes. 1738 1739 Logically Collective on TS 1740 1741 Input Parameters: 1742 + ts - the TS context obtained from TSCreate() 1743 - time_step - the size of the timestep 1744 1745 Level: intermediate 1746 1747 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1748 1749 .keywords: TS, set, timestep 1750 @*/ 1751 PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1752 { 1753 PetscFunctionBegin; 1754 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1755 PetscValidLogicalCollectiveReal(ts,time_step,2); 1756 ts->time_step = time_step; 1757 ts->time_step_orig = time_step; 1758 PetscFunctionReturn(0); 1759 } 1760 1761 #undef __FUNCT__ 1762 #define __FUNCT__ "TSSetExactFinalTime" 1763 /*@ 1764 TSSetExactFinalTime - Determines whether to adapt the final time step to 1765 match the exact final time, interpolate solution to the exact final time, 1766 or just return at the final time TS computed. 1767 1768 Logically Collective on TS 1769 1770 Input Parameter: 1771 + ts - the time-step context 1772 - eftopt - exact final time option 1773 1774 $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 1775 $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 1776 $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 1777 1778 Options Database: 1779 . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 1780 1781 Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 1782 then the final time you selected. 1783 1784 Level: beginner 1785 1786 .seealso: TSExactFinalTimeOption 1787 @*/ 1788 PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1789 { 1790 PetscFunctionBegin; 1791 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1792 PetscValidLogicalCollectiveEnum(ts,eftopt,2); 1793 ts->exact_final_time = eftopt; 1794 PetscFunctionReturn(0); 1795 } 1796 1797 #undef __FUNCT__ 1798 #define __FUNCT__ "TSGetTimeStep" 1799 /*@ 1800 TSGetTimeStep - Gets the current timestep size. 1801 1802 Not Collective 1803 1804 Input Parameter: 1805 . ts - the TS context obtained from TSCreate() 1806 1807 Output Parameter: 1808 . dt - the current timestep size 1809 1810 Level: intermediate 1811 1812 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1813 1814 .keywords: TS, get, timestep 1815 @*/ 1816 PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1817 { 1818 PetscFunctionBegin; 1819 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1820 PetscValidRealPointer(dt,2); 1821 *dt = ts->time_step; 1822 PetscFunctionReturn(0); 1823 } 1824 1825 #undef __FUNCT__ 1826 #define __FUNCT__ "TSGetSolution" 1827 /*@ 1828 TSGetSolution - Returns the solution at the present timestep. It 1829 is valid to call this routine inside the function that you are evaluating 1830 in order to move to the new timestep. This vector not changed until 1831 the solution at the next timestep has been calculated. 1832 1833 Not Collective, but Vec returned is parallel if TS is parallel 1834 1835 Input Parameter: 1836 . ts - the TS context obtained from TSCreate() 1837 1838 Output Parameter: 1839 . v - the vector containing the solution 1840 1841 Level: intermediate 1842 1843 .seealso: TSGetTimeStep() 1844 1845 .keywords: TS, timestep, get, solution 1846 @*/ 1847 PetscErrorCode TSGetSolution(TS ts,Vec *v) 1848 { 1849 PetscFunctionBegin; 1850 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1851 PetscValidPointer(v,2); 1852 *v = ts->vec_sol; 1853 PetscFunctionReturn(0); 1854 } 1855 1856 #undef __FUNCT__ 1857 #define __FUNCT__ "TSGetCostGradients" 1858 /*@ 1859 TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1860 1861 Not Collective, but Vec returned is parallel if TS is parallel 1862 1863 Input Parameter: 1864 . ts - the TS context obtained from TSCreate() 1865 1866 Output Parameter: 1867 + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1868 - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1869 1870 Level: intermediate 1871 1872 .seealso: TSGetTimeStep() 1873 1874 .keywords: TS, timestep, get, sensitivity 1875 @*/ 1876 PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1877 { 1878 PetscFunctionBegin; 1879 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1880 if (numcost) *numcost = ts->numcost; 1881 if (lambda) *lambda = ts->vecs_sensi; 1882 if (mu) *mu = ts->vecs_sensip; 1883 PetscFunctionReturn(0); 1884 } 1885 1886 /* ----- Routines to initialize and destroy a timestepper ---- */ 1887 #undef __FUNCT__ 1888 #define __FUNCT__ "TSSetProblemType" 1889 /*@ 1890 TSSetProblemType - Sets the type of problem to be solved. 1891 1892 Not collective 1893 1894 Input Parameters: 1895 + ts - The TS 1896 - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1897 .vb 1898 U_t - A U = 0 (linear) 1899 U_t - A(t) U = 0 (linear) 1900 F(t,U,U_t) = 0 (nonlinear) 1901 .ve 1902 1903 Level: beginner 1904 1905 .keywords: TS, problem type 1906 .seealso: TSSetUp(), TSProblemType, TS 1907 @*/ 1908 PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1909 { 1910 PetscErrorCode ierr; 1911 1912 PetscFunctionBegin; 1913 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1914 ts->problem_type = type; 1915 if (type == TS_LINEAR) { 1916 SNES snes; 1917 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1918 ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 1919 } 1920 PetscFunctionReturn(0); 1921 } 1922 1923 #undef __FUNCT__ 1924 #define __FUNCT__ "TSGetProblemType" 1925 /*@C 1926 TSGetProblemType - Gets the type of problem to be solved. 1927 1928 Not collective 1929 1930 Input Parameter: 1931 . ts - The TS 1932 1933 Output Parameter: 1934 . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1935 .vb 1936 M U_t = A U 1937 M(t) U_t = A(t) U 1938 F(t,U,U_t) 1939 .ve 1940 1941 Level: beginner 1942 1943 .keywords: TS, problem type 1944 .seealso: TSSetUp(), TSProblemType, TS 1945 @*/ 1946 PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1947 { 1948 PetscFunctionBegin; 1949 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1950 PetscValidIntPointer(type,2); 1951 *type = ts->problem_type; 1952 PetscFunctionReturn(0); 1953 } 1954 1955 #undef __FUNCT__ 1956 #define __FUNCT__ "TSSetUp" 1957 /*@ 1958 TSSetUp - Sets up the internal data structures for the later use 1959 of a timestepper. 1960 1961 Collective on TS 1962 1963 Input Parameter: 1964 . ts - the TS context obtained from TSCreate() 1965 1966 Notes: 1967 For basic use of the TS solvers the user need not explicitly call 1968 TSSetUp(), since these actions will automatically occur during 1969 the call to TSStep(). However, if one wishes to control this 1970 phase separately, TSSetUp() should be called after TSCreate() 1971 and optional routines of the form TSSetXXX(), but before TSStep(). 1972 1973 Level: advanced 1974 1975 .keywords: TS, timestep, setup 1976 1977 .seealso: TSCreate(), TSStep(), TSDestroy() 1978 @*/ 1979 PetscErrorCode TSSetUp(TS ts) 1980 { 1981 PetscErrorCode ierr; 1982 DM dm; 1983 PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1984 PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 1985 TSIJacobian ijac; 1986 TSRHSJacobian rhsjac; 1987 1988 PetscFunctionBegin; 1989 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1990 if (ts->setupcalled) PetscFunctionReturn(0); 1991 1992 ts->total_steps = 0; 1993 if (!((PetscObject)ts)->type_name) { 1994 ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1995 } 1996 1997 if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1998 1999 2000 if (ts->rhsjacobian.reuse) { 2001 Mat Amat,Pmat; 2002 SNES snes; 2003 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2004 ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2005 /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2006 * have displaced the RHS matrix */ 2007 if (Amat == ts->Arhs) { 2008 ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2009 ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2010 ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2011 } 2012 if (Pmat == ts->Brhs) { 2013 ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2014 ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2015 ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2016 } 2017 } 2018 if (ts->ops->setup) { 2019 ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2020 } 2021 2022 /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 2023 to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 2024 */ 2025 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2026 ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 2027 if (!func) { 2028 ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 2029 } 2030 /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 2031 Otherwise, the SNES will use coloring internally to form the Jacobian. 2032 */ 2033 ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 2034 ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2035 ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2036 if (!jac && (ijac || rhsjac)) { 2037 ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2038 } 2039 ts->setupcalled = PETSC_TRUE; 2040 PetscFunctionReturn(0); 2041 } 2042 2043 #undef __FUNCT__ 2044 #define __FUNCT__ "TSAdjointSetUp" 2045 /*@ 2046 TSAdjointSetUp - Sets up the internal data structures for the later use 2047 of an adjoint solver 2048 2049 Collective on TS 2050 2051 Input Parameter: 2052 . ts - the TS context obtained from TSCreate() 2053 2054 Level: advanced 2055 2056 .keywords: TS, timestep, setup 2057 2058 .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 2059 @*/ 2060 PetscErrorCode TSAdjointSetUp(TS ts) 2061 { 2062 PetscErrorCode ierr; 2063 2064 PetscFunctionBegin; 2065 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2066 if (ts->adjointsetupcalled) PetscFunctionReturn(0); 2067 if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 2068 2069 if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 2070 ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2071 if (ts->vecs_sensip){ 2072 ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2073 } 2074 } 2075 2076 if (ts->ops->adjointsetup) { 2077 ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 2078 } 2079 ts->adjointsetupcalled = PETSC_TRUE; 2080 PetscFunctionReturn(0); 2081 } 2082 2083 #undef __FUNCT__ 2084 #define __FUNCT__ "TSReset" 2085 /*@ 2086 TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2087 2088 Collective on TS 2089 2090 Input Parameter: 2091 . ts - the TS context obtained from TSCreate() 2092 2093 Level: beginner 2094 2095 .keywords: TS, timestep, reset 2096 2097 .seealso: TSCreate(), TSSetup(), TSDestroy() 2098 @*/ 2099 PetscErrorCode TSReset(TS ts) 2100 { 2101 PetscErrorCode ierr; 2102 2103 PetscFunctionBegin; 2104 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2105 2106 if (ts->ops->reset) { 2107 ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2108 } 2109 if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2110 if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2111 2112 ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 2113 ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2114 ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 2115 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2116 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2117 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 2118 ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2119 2120 if (ts->vec_costintegral) { 2121 ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2122 if (ts->vecs_drdp){ 2123 ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2124 } 2125 } 2126 ts->vecs_sensi = NULL; 2127 ts->vecs_sensip = NULL; 2128 ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2129 ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 2130 ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2131 ts->setupcalled = PETSC_FALSE; 2132 PetscFunctionReturn(0); 2133 } 2134 2135 #undef __FUNCT__ 2136 #define __FUNCT__ "TSDestroy" 2137 /*@ 2138 TSDestroy - Destroys the timestepper context that was created 2139 with TSCreate(). 2140 2141 Collective on TS 2142 2143 Input Parameter: 2144 . ts - the TS context obtained from TSCreate() 2145 2146 Level: beginner 2147 2148 .keywords: TS, timestepper, destroy 2149 2150 .seealso: TSCreate(), TSSetUp(), TSSolve() 2151 @*/ 2152 PetscErrorCode TSDestroy(TS *ts) 2153 { 2154 PetscErrorCode ierr; 2155 2156 PetscFunctionBegin; 2157 if (!*ts) PetscFunctionReturn(0); 2158 PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 2159 if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2160 2161 ierr = TSReset((*ts));CHKERRQ(ierr); 2162 2163 /* if memory was published with SAWs then destroy it */ 2164 ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 2165 if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 2166 2167 ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2168 2169 ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2170 if ((*ts)->event) { 2171 ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2172 } 2173 ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 2174 ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 2175 ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 2176 ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 2177 2178 ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2179 PetscFunctionReturn(0); 2180 } 2181 2182 #undef __FUNCT__ 2183 #define __FUNCT__ "TSGetSNES" 2184 /*@ 2185 TSGetSNES - Returns the SNES (nonlinear solver) associated with 2186 a TS (timestepper) context. Valid only for nonlinear problems. 2187 2188 Not Collective, but SNES is parallel if TS is parallel 2189 2190 Input Parameter: 2191 . ts - the TS context obtained from TSCreate() 2192 2193 Output Parameter: 2194 . snes - the nonlinear solver context 2195 2196 Notes: 2197 The user can then directly manipulate the SNES context to set various 2198 options, etc. Likewise, the user can then extract and manipulate the 2199 KSP, KSP, and PC contexts as well. 2200 2201 TSGetSNES() does not work for integrators that do not use SNES; in 2202 this case TSGetSNES() returns NULL in snes. 2203 2204 Level: beginner 2205 2206 .keywords: timestep, get, SNES 2207 @*/ 2208 PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2209 { 2210 PetscErrorCode ierr; 2211 2212 PetscFunctionBegin; 2213 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2214 PetscValidPointer(snes,2); 2215 if (!ts->snes) { 2216 ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 2217 ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 2218 ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2219 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2220 if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 2221 if (ts->problem_type == TS_LINEAR) { 2222 ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 2223 } 2224 } 2225 *snes = ts->snes; 2226 PetscFunctionReturn(0); 2227 } 2228 2229 #undef __FUNCT__ 2230 #define __FUNCT__ "TSSetSNES" 2231 /*@ 2232 TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2233 2234 Collective 2235 2236 Input Parameter: 2237 + ts - the TS context obtained from TSCreate() 2238 - snes - the nonlinear solver context 2239 2240 Notes: 2241 Most users should have the TS created by calling TSGetSNES() 2242 2243 Level: developer 2244 2245 .keywords: timestep, set, SNES 2246 @*/ 2247 PetscErrorCode TSSetSNES(TS ts,SNES snes) 2248 { 2249 PetscErrorCode ierr; 2250 PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2251 2252 PetscFunctionBegin; 2253 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2254 PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2255 ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2256 ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2257 2258 ts->snes = snes; 2259 2260 ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 2261 ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2262 if (func == SNESTSFormJacobian) { 2263 ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2264 } 2265 PetscFunctionReturn(0); 2266 } 2267 2268 #undef __FUNCT__ 2269 #define __FUNCT__ "TSGetKSP" 2270 /*@ 2271 TSGetKSP - Returns the KSP (linear solver) associated with 2272 a TS (timestepper) context. 2273 2274 Not Collective, but KSP is parallel if TS is parallel 2275 2276 Input Parameter: 2277 . ts - the TS context obtained from TSCreate() 2278 2279 Output Parameter: 2280 . ksp - the nonlinear solver context 2281 2282 Notes: 2283 The user can then directly manipulate the KSP context to set various 2284 options, etc. Likewise, the user can then extract and manipulate the 2285 KSP and PC contexts as well. 2286 2287 TSGetKSP() does not work for integrators that do not use KSP; 2288 in this case TSGetKSP() returns NULL in ksp. 2289 2290 Level: beginner 2291 2292 .keywords: timestep, get, KSP 2293 @*/ 2294 PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2295 { 2296 PetscErrorCode ierr; 2297 SNES snes; 2298 2299 PetscFunctionBegin; 2300 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2301 PetscValidPointer(ksp,2); 2302 if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2303 if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2304 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2305 ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2306 PetscFunctionReturn(0); 2307 } 2308 2309 /* ----------- Routines to set solver parameters ---------- */ 2310 2311 #undef __FUNCT__ 2312 #define __FUNCT__ "TSGetDuration" 2313 /*@ 2314 TSGetDuration - Gets the maximum number of timesteps to use and 2315 maximum time for iteration. 2316 2317 Not Collective 2318 2319 Input Parameters: 2320 + ts - the TS context obtained from TSCreate() 2321 . maxsteps - maximum number of iterations to use, or NULL 2322 - maxtime - final time to iterate to, or NULL 2323 2324 Level: intermediate 2325 2326 .keywords: TS, timestep, get, maximum, iterations, time 2327 @*/ 2328 PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2329 { 2330 PetscFunctionBegin; 2331 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2332 if (maxsteps) { 2333 PetscValidIntPointer(maxsteps,2); 2334 *maxsteps = ts->max_steps; 2335 } 2336 if (maxtime) { 2337 PetscValidScalarPointer(maxtime,3); 2338 *maxtime = ts->max_time; 2339 } 2340 PetscFunctionReturn(0); 2341 } 2342 2343 #undef __FUNCT__ 2344 #define __FUNCT__ "TSSetDuration" 2345 /*@ 2346 TSSetDuration - Sets the maximum number of timesteps to use and 2347 maximum time for iteration. 2348 2349 Logically Collective on TS 2350 2351 Input Parameters: 2352 + ts - the TS context obtained from TSCreate() 2353 . maxsteps - maximum number of iterations to use 2354 - maxtime - final time to iterate to 2355 2356 Options Database Keys: 2357 . -ts_max_steps <maxsteps> - Sets maxsteps 2358 . -ts_final_time <maxtime> - Sets maxtime 2359 2360 Notes: 2361 The default maximum number of iterations is 5000. Default time is 5.0 2362 2363 Level: intermediate 2364 2365 .keywords: TS, timestep, set, maximum, iterations 2366 2367 .seealso: TSSetExactFinalTime() 2368 @*/ 2369 PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2370 { 2371 PetscFunctionBegin; 2372 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2373 PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2374 PetscValidLogicalCollectiveReal(ts,maxtime,2); 2375 if (maxsteps >= 0) ts->max_steps = maxsteps; 2376 if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2377 PetscFunctionReturn(0); 2378 } 2379 2380 #undef __FUNCT__ 2381 #define __FUNCT__ "TSSetSolution" 2382 /*@ 2383 TSSetSolution - Sets the initial solution vector 2384 for use by the TS routines. 2385 2386 Logically Collective on TS and Vec 2387 2388 Input Parameters: 2389 + ts - the TS context obtained from TSCreate() 2390 - u - the solution vector 2391 2392 Level: beginner 2393 2394 .keywords: TS, timestep, set, solution, initial conditions 2395 @*/ 2396 PetscErrorCode TSSetSolution(TS ts,Vec u) 2397 { 2398 PetscErrorCode ierr; 2399 DM dm; 2400 2401 PetscFunctionBegin; 2402 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2403 PetscValidHeaderSpecific(u,VEC_CLASSID,2); 2404 ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 2405 ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2406 2407 ts->vec_sol = u; 2408 2409 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2410 ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2411 PetscFunctionReturn(0); 2412 } 2413 2414 #undef __FUNCT__ 2415 #define __FUNCT__ "TSAdjointSetSteps" 2416 /*@ 2417 TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 2418 2419 Logically Collective on TS 2420 2421 Input Parameters: 2422 + ts - the TS context obtained from TSCreate() 2423 . steps - number of steps to use 2424 2425 Level: intermediate 2426 2427 Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 2428 so as to integrate back to less than the original timestep 2429 2430 .keywords: TS, timestep, set, maximum, iterations 2431 2432 .seealso: TSSetExactFinalTime() 2433 @*/ 2434 PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 2435 { 2436 PetscFunctionBegin; 2437 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2438 PetscValidLogicalCollectiveInt(ts,steps,2); 2439 if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 2440 if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps"); 2441 ts->adjoint_max_steps = steps; 2442 PetscFunctionReturn(0); 2443 } 2444 2445 #undef __FUNCT__ 2446 #define __FUNCT__ "TSSetCostGradients" 2447 /*@ 2448 TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 2449 for use by the TSAdjoint routines. 2450 2451 Logically Collective on TS and Vec 2452 2453 Input Parameters: 2454 + ts - the TS context obtained from TSCreate() 2455 . lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector 2456 - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2457 2458 Level: beginner 2459 2460 Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 2461 2462 .keywords: TS, timestep, set, sensitivity, initial conditions 2463 @*/ 2464 PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2465 { 2466 PetscFunctionBegin; 2467 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2468 PetscValidPointer(lambda,2); 2469 ts->vecs_sensi = lambda; 2470 ts->vecs_sensip = mu; 2471 if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand"); 2472 ts->numcost = numcost; 2473 PetscFunctionReturn(0); 2474 } 2475 2476 #undef __FUNCT__ 2477 #define __FUNCT__ "TSAdjointSetRHSJacobian" 2478 /*@C 2479 TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix. 2480 2481 Logically Collective on TS 2482 2483 Input Parameters: 2484 + ts - The TS context obtained from TSCreate() 2485 - func - The function 2486 2487 Calling sequence of func: 2488 $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 2489 + t - current timestep 2490 . y - input vector (current ODE solution) 2491 . A - output matrix 2492 - ctx - [optional] user-defined function context 2493 2494 Level: intermediate 2495 2496 Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p 2497 2498 .keywords: TS, sensitivity 2499 .seealso: 2500 @*/ 2501 PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2502 { 2503 PetscErrorCode ierr; 2504 2505 PetscFunctionBegin; 2506 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2507 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2508 2509 ts->rhsjacobianp = func; 2510 ts->rhsjacobianpctx = ctx; 2511 if(Amat) { 2512 ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2513 ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2514 ts->Jacp = Amat; 2515 } 2516 PetscFunctionReturn(0); 2517 } 2518 2519 #undef __FUNCT__ 2520 #define __FUNCT__ "TSAdjointComputeRHSJacobian" 2521 /*@C 2522 TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2523 2524 Collective on TS 2525 2526 Input Parameters: 2527 . ts - The TS context obtained from TSCreate() 2528 2529 Level: developer 2530 2531 .keywords: TS, sensitivity 2532 .seealso: TSAdjointSetRHSJacobian() 2533 @*/ 2534 PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2535 { 2536 PetscErrorCode ierr; 2537 2538 PetscFunctionBegin; 2539 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2540 PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2541 PetscValidPointer(Amat,4); 2542 2543 PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2544 ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2545 PetscStackPop; 2546 PetscFunctionReturn(0); 2547 } 2548 2549 #undef __FUNCT__ 2550 #define __FUNCT__ "TSSetCostIntegrand" 2551 /*@C 2552 TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 2553 2554 Logically Collective on TS 2555 2556 Input Parameters: 2557 + ts - the TS context obtained from TSCreate() 2558 . numcost - number of gradients to be computed, this is the number of cost functions 2559 . rf - routine for evaluating the integrand function 2560 . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2561 . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2562 . fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run 2563 - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 2564 2565 Calling sequence of rf: 2566 $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 2567 2568 + t - current timestep 2569 . y - input vector 2570 . f - function result; one vector entry for each cost function 2571 - ctx - [optional] user-defined function context 2572 2573 Calling sequence of drdyf: 2574 $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2575 2576 Calling sequence of drdpf: 2577 $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2578 2579 Level: intermediate 2580 2581 Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 2582 2583 .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 2584 2585 .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 2586 @*/ 2587 PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2588 PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2589 PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*), 2590 PetscBool fwd,void *ctx) 2591 { 2592 PetscErrorCode ierr; 2593 2594 PetscFunctionBegin; 2595 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2596 if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()"); 2597 if (!ts->numcost) ts->numcost=numcost; 2598 2599 ts->costintegralfwd = fwd; /* Evaluate the cost integral in forward run if fwd is true */ 2600 ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 2601 ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 2602 ts->costintegrand = rf; 2603 ts->costintegrandctx = ctx; 2604 ts->drdyfunction = drdyf; 2605 ts->drdpfunction = drdpf; 2606 PetscFunctionReturn(0); 2607 } 2608 2609 #undef __FUNCT__ 2610 #define __FUNCT__ "TSGetCostIntegral" 2611 /*@ 2612 TSGetCostIntegral - Returns the values of the integral term in the cost functions. 2613 It is valid to call the routine after a backward run. 2614 2615 Not Collective 2616 2617 Input Parameter: 2618 . ts - the TS context obtained from TSCreate() 2619 2620 Output Parameter: 2621 . v - the vector containing the integrals for each cost function 2622 2623 Level: intermediate 2624 2625 .seealso: TSSetCostIntegrand() 2626 2627 .keywords: TS, sensitivity analysis 2628 @*/ 2629 PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 2630 { 2631 PetscFunctionBegin; 2632 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2633 PetscValidPointer(v,2); 2634 *v = ts->vec_costintegral; 2635 PetscFunctionReturn(0); 2636 } 2637 2638 #undef __FUNCT__ 2639 #define __FUNCT__ "TSAdjointComputeCostIntegrand" 2640 /*@ 2641 TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 2642 2643 Input Parameters: 2644 + ts - the TS context 2645 . t - current time 2646 - y - state vector, i.e. current solution 2647 2648 Output Parameter: 2649 . q - vector of size numcost to hold the outputs 2650 2651 Note: 2652 Most users should not need to explicitly call this routine, as it 2653 is used internally within the sensitivity analysis context. 2654 2655 Level: developer 2656 2657 .keywords: TS, compute 2658 2659 .seealso: TSSetCostIntegrand() 2660 @*/ 2661 PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 2662 { 2663 PetscErrorCode ierr; 2664 2665 PetscFunctionBegin; 2666 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2667 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2668 PetscValidHeaderSpecific(q,VEC_CLASSID,4); 2669 2670 ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2671 if (ts->costintegrand) { 2672 PetscStackPush("TS user integrand in the cost function"); 2673 ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 2674 PetscStackPop; 2675 } else { 2676 ierr = VecZeroEntries(q);CHKERRQ(ierr); 2677 } 2678 2679 ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2680 PetscFunctionReturn(0); 2681 } 2682 2683 #undef __FUNCT__ 2684 #define __FUNCT__ "TSAdjointComputeDRDYFunction" 2685 /*@ 2686 TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 2687 2688 Collective on TS 2689 2690 Input Parameters: 2691 . ts - The TS context obtained from TSCreate() 2692 2693 Notes: 2694 TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 2695 so most users would not generally call this routine themselves. 2696 2697 Level: developer 2698 2699 .keywords: TS, sensitivity 2700 .seealso: TSAdjointComputeDRDYFunction() 2701 @*/ 2702 PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 2703 { 2704 PetscErrorCode ierr; 2705 2706 PetscFunctionBegin; 2707 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2708 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2709 2710 PetscStackPush("TS user DRDY function for sensitivity analysis"); 2711 ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 2712 PetscStackPop; 2713 PetscFunctionReturn(0); 2714 } 2715 2716 #undef __FUNCT__ 2717 #define __FUNCT__ "TSAdjointComputeDRDPFunction" 2718 /*@ 2719 TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 2720 2721 Collective on TS 2722 2723 Input Parameters: 2724 . ts - The TS context obtained from TSCreate() 2725 2726 Notes: 2727 TSDRDPFunction() is typically used for sensitivity implementation, 2728 so most users would not generally call this routine themselves. 2729 2730 Level: developer 2731 2732 .keywords: TS, sensitivity 2733 .seealso: TSAdjointSetDRDPFunction() 2734 @*/ 2735 PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 2736 { 2737 PetscErrorCode ierr; 2738 2739 PetscFunctionBegin; 2740 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2741 PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2742 2743 PetscStackPush("TS user DRDP function for sensitivity analysis"); 2744 ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 2745 PetscStackPop; 2746 PetscFunctionReturn(0); 2747 } 2748 2749 #undef __FUNCT__ 2750 #define __FUNCT__ "TSSetPreStep" 2751 /*@C 2752 TSSetPreStep - Sets the general-purpose function 2753 called once at the beginning of each time step. 2754 2755 Logically Collective on TS 2756 2757 Input Parameters: 2758 + ts - The TS context obtained from TSCreate() 2759 - func - The function 2760 2761 Calling sequence of func: 2762 . func (TS ts); 2763 2764 Level: intermediate 2765 2766 Note: 2767 If a step is rejected, TSStep() will call this routine again before each attempt. 2768 The last completed time step number can be queried using TSGetTimeStepNumber(), the 2769 size of the step being attempted can be obtained using TSGetTimeStep(). 2770 2771 .keywords: TS, timestep 2772 .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2773 @*/ 2774 PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2775 { 2776 PetscFunctionBegin; 2777 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2778 ts->prestep = func; 2779 PetscFunctionReturn(0); 2780 } 2781 2782 #undef __FUNCT__ 2783 #define __FUNCT__ "TSPreStep" 2784 /*@ 2785 TSPreStep - Runs the user-defined pre-step function. 2786 2787 Collective on TS 2788 2789 Input Parameters: 2790 . ts - The TS context obtained from TSCreate() 2791 2792 Notes: 2793 TSPreStep() is typically used within time stepping implementations, 2794 so most users would not generally call this routine themselves. 2795 2796 Level: developer 2797 2798 .keywords: TS, timestep 2799 .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 2800 @*/ 2801 PetscErrorCode TSPreStep(TS ts) 2802 { 2803 PetscErrorCode ierr; 2804 2805 PetscFunctionBegin; 2806 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2807 if (ts->prestep) { 2808 PetscStackCallStandard((*ts->prestep),(ts)); 2809 } 2810 PetscFunctionReturn(0); 2811 } 2812 2813 #undef __FUNCT__ 2814 #define __FUNCT__ "TSSetPreStage" 2815 /*@C 2816 TSSetPreStage - Sets the general-purpose function 2817 called once at the beginning of each stage. 2818 2819 Logically Collective on TS 2820 2821 Input Parameters: 2822 + ts - The TS context obtained from TSCreate() 2823 - func - The function 2824 2825 Calling sequence of func: 2826 . PetscErrorCode func(TS ts, PetscReal stagetime); 2827 2828 Level: intermediate 2829 2830 Note: 2831 There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2832 The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2833 attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2834 2835 .keywords: TS, timestep 2836 .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2837 @*/ 2838 PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2839 { 2840 PetscFunctionBegin; 2841 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2842 ts->prestage = func; 2843 PetscFunctionReturn(0); 2844 } 2845 2846 #undef __FUNCT__ 2847 #define __FUNCT__ "TSSetPostStage" 2848 /*@C 2849 TSSetPostStage - Sets the general-purpose function 2850 called once at the end of each stage. 2851 2852 Logically Collective on TS 2853 2854 Input Parameters: 2855 + ts - The TS context obtained from TSCreate() 2856 - func - The function 2857 2858 Calling sequence of func: 2859 . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 2860 2861 Level: intermediate 2862 2863 Note: 2864 There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2865 The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2866 attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2867 2868 .keywords: TS, timestep 2869 .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2870 @*/ 2871 PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 2872 { 2873 PetscFunctionBegin; 2874 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2875 ts->poststage = func; 2876 PetscFunctionReturn(0); 2877 } 2878 2879 #undef __FUNCT__ 2880 #define __FUNCT__ "TSPreStage" 2881 /*@ 2882 TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2883 2884 Collective on TS 2885 2886 Input Parameters: 2887 . ts - The TS context obtained from TSCreate() 2888 stagetime - The absolute time of the current stage 2889 2890 Notes: 2891 TSPreStage() is typically used within time stepping implementations, 2892 most users would not generally call this routine themselves. 2893 2894 Level: developer 2895 2896 .keywords: TS, timestep 2897 .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2898 @*/ 2899 PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2900 { 2901 PetscErrorCode ierr; 2902 2903 PetscFunctionBegin; 2904 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2905 if (ts->prestage) { 2906 PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2907 } 2908 PetscFunctionReturn(0); 2909 } 2910 2911 #undef __FUNCT__ 2912 #define __FUNCT__ "TSPostStage" 2913 /*@ 2914 TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 2915 2916 Collective on TS 2917 2918 Input Parameters: 2919 . ts - The TS context obtained from TSCreate() 2920 stagetime - The absolute time of the current stage 2921 stageindex - Stage number 2922 Y - Array of vectors (of size = total number 2923 of stages) with the stage solutions 2924 2925 Notes: 2926 TSPostStage() is typically used within time stepping implementations, 2927 most users would not generally call this routine themselves. 2928 2929 Level: developer 2930 2931 .keywords: TS, timestep 2932 .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2933 @*/ 2934 PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 2935 { 2936 PetscErrorCode ierr; 2937 2938 PetscFunctionBegin; 2939 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2940 if (ts->poststage) { 2941 PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 2942 } 2943 PetscFunctionReturn(0); 2944 } 2945 2946 #undef __FUNCT__ 2947 #define __FUNCT__ "TSSetPostStep" 2948 /*@C 2949 TSSetPostStep - Sets the general-purpose function 2950 called once at the end of each time step. 2951 2952 Logically Collective on TS 2953 2954 Input Parameters: 2955 + ts - The TS context obtained from TSCreate() 2956 - func - The function 2957 2958 Calling sequence of func: 2959 $ func (TS ts); 2960 2961 Level: intermediate 2962 2963 .keywords: TS, timestep 2964 .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2965 @*/ 2966 PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2967 { 2968 PetscFunctionBegin; 2969 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2970 ts->poststep = func; 2971 PetscFunctionReturn(0); 2972 } 2973 2974 #undef __FUNCT__ 2975 #define __FUNCT__ "TSPostStep" 2976 /*@ 2977 TSPostStep - Runs the user-defined post-step function. 2978 2979 Collective on TS 2980 2981 Input Parameters: 2982 . ts - The TS context obtained from TSCreate() 2983 2984 Notes: 2985 TSPostStep() is typically used within time stepping implementations, 2986 so most users would not generally call this routine themselves. 2987 2988 Level: developer 2989 2990 .keywords: TS, timestep 2991 @*/ 2992 PetscErrorCode TSPostStep(TS ts) 2993 { 2994 PetscErrorCode ierr; 2995 2996 PetscFunctionBegin; 2997 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2998 if (ts->poststep) { 2999 PetscStackCallStandard((*ts->poststep),(ts)); 3000 } 3001 PetscFunctionReturn(0); 3002 } 3003 3004 /* ------------ Routines to set performance monitoring options ----------- */ 3005 3006 #undef __FUNCT__ 3007 #define __FUNCT__ "TSMonitorSet" 3008 /*@C 3009 TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 3010 timestep to display the iteration's progress. 3011 3012 Logically Collective on TS 3013 3014 Input Parameters: 3015 + ts - the TS context obtained from TSCreate() 3016 . monitor - monitoring routine 3017 . mctx - [optional] user-defined context for private data for the 3018 monitor routine (use NULL if no context is desired) 3019 - monitordestroy - [optional] routine that frees monitor context 3020 (may be NULL) 3021 3022 Calling sequence of monitor: 3023 $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 3024 3025 + ts - the TS context 3026 . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 3027 been interpolated to) 3028 . time - current time 3029 . u - current iterate 3030 - mctx - [optional] monitoring context 3031 3032 Notes: 3033 This routine adds an additional monitor to the list of monitors that 3034 already has been loaded. 3035 3036 Fortran notes: Only a single monitor function can be set for each TS object 3037 3038 Level: intermediate 3039 3040 .keywords: TS, timestep, set, monitor 3041 3042 .seealso: TSMonitorDefault(), TSMonitorCancel() 3043 @*/ 3044 PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 3045 { 3046 PetscFunctionBegin; 3047 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3048 if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 3049 ts->monitor[ts->numbermonitors] = monitor; 3050 ts->monitordestroy[ts->numbermonitors] = mdestroy; 3051 ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 3052 PetscFunctionReturn(0); 3053 } 3054 3055 #undef __FUNCT__ 3056 #define __FUNCT__ "TSMonitorCancel" 3057 /*@C 3058 TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 3059 3060 Logically Collective on TS 3061 3062 Input Parameters: 3063 . ts - the TS context obtained from TSCreate() 3064 3065 Notes: 3066 There is no way to remove a single, specific monitor. 3067 3068 Level: intermediate 3069 3070 .keywords: TS, timestep, set, monitor 3071 3072 .seealso: TSMonitorDefault(), TSMonitorSet() 3073 @*/ 3074 PetscErrorCode TSMonitorCancel(TS ts) 3075 { 3076 PetscErrorCode ierr; 3077 PetscInt i; 3078 3079 PetscFunctionBegin; 3080 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3081 for (i=0; i<ts->numbermonitors; i++) { 3082 if (ts->monitordestroy[i]) { 3083 ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 3084 } 3085 } 3086 ts->numbermonitors = 0; 3087 PetscFunctionReturn(0); 3088 } 3089 3090 #undef __FUNCT__ 3091 #define __FUNCT__ "TSMonitorDefault" 3092 /*@ 3093 TSMonitorDefault - Sets the Default monitor 3094 3095 Level: intermediate 3096 3097 .keywords: TS, set, monitor 3098 3099 .seealso: TSMonitorDefault(), TSMonitorSet() 3100 @*/ 3101 PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 3102 { 3103 PetscErrorCode ierr; 3104 PetscViewer viewer = (PetscViewer) dummy; 3105 PetscBool iascii,ibinary; 3106 3107 PetscFunctionBegin; 3108 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3109 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 3110 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr); 3111 if (iascii) { 3112 ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3113 ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3114 ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3115 } else if (ibinary) { 3116 PetscMPIInt rank; 3117 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 3118 if (!rank) { 3119 ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr); 3120 } else { 3121 ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr); 3122 } 3123 } 3124 PetscFunctionReturn(0); 3125 } 3126 3127 #undef __FUNCT__ 3128 #define __FUNCT__ "TSAdjointMonitorSet" 3129 /*@C 3130 TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 3131 timestep to display the iteration's progress. 3132 3133 Logically Collective on TS 3134 3135 Input Parameters: 3136 + ts - the TS context obtained from TSCreate() 3137 . adjointmonitor - monitoring routine 3138 . adjointmctx - [optional] user-defined context for private data for the 3139 monitor routine (use NULL if no context is desired) 3140 - adjointmonitordestroy - [optional] routine that frees monitor context 3141 (may be NULL) 3142 3143 Calling sequence of monitor: 3144 $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 3145 3146 + ts - the TS context 3147 . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 3148 been interpolated to) 3149 . time - current time 3150 . u - current iterate 3151 . numcost - number of cost functionos 3152 . lambda - sensitivities to initial conditions 3153 . mu - sensitivities to parameters 3154 - adjointmctx - [optional] adjoint monitoring context 3155 3156 Notes: 3157 This routine adds an additional monitor to the list of monitors that 3158 already has been loaded. 3159 3160 Fortran notes: Only a single monitor function can be set for each TS object 3161 3162 Level: intermediate 3163 3164 .keywords: TS, timestep, set, adjoint, monitor 3165 3166 .seealso: TSAdjointMonitorCancel() 3167 @*/ 3168 PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 3169 { 3170 PetscFunctionBegin; 3171 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3172 if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 3173 ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 3174 ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 3175 ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 3176 PetscFunctionReturn(0); 3177 } 3178 3179 #undef __FUNCT__ 3180 #define __FUNCT__ "TSAdjointMonitorCancel" 3181 /*@C 3182 TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 3183 3184 Logically Collective on TS 3185 3186 Input Parameters: 3187 . ts - the TS context obtained from TSCreate() 3188 3189 Notes: 3190 There is no way to remove a single, specific monitor. 3191 3192 Level: intermediate 3193 3194 .keywords: TS, timestep, set, adjoint, monitor 3195 3196 .seealso: TSAdjointMonitorSet() 3197 @*/ 3198 PetscErrorCode TSAdjointMonitorCancel(TS ts) 3199 { 3200 PetscErrorCode ierr; 3201 PetscInt i; 3202 3203 PetscFunctionBegin; 3204 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3205 for (i=0; i<ts->numberadjointmonitors; i++) { 3206 if (ts->adjointmonitordestroy[i]) { 3207 ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 3208 } 3209 } 3210 ts->numberadjointmonitors = 0; 3211 PetscFunctionReturn(0); 3212 } 3213 3214 #undef __FUNCT__ 3215 #define __FUNCT__ "TSAdjointMonitorDefault" 3216 /*@ 3217 TSAdjointMonitorDefault - Sets the Default monitor 3218 3219 Level: intermediate 3220 3221 .keywords: TS, set, monitor 3222 3223 .seealso: TSAdjointMonitorSet() 3224 @*/ 3225 PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 3226 { 3227 PetscErrorCode ierr; 3228 PetscViewer viewer = (PetscViewer) dummy; 3229 3230 PetscFunctionBegin; 3231 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 3232 ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3233 ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 3234 ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 3235 PetscFunctionReturn(0); 3236 } 3237 3238 #undef __FUNCT__ 3239 #define __FUNCT__ "TSSetRetainStages" 3240 /*@ 3241 TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3242 3243 Logically Collective on TS 3244 3245 Input Argument: 3246 . ts - time stepping context 3247 3248 Output Argument: 3249 . flg - PETSC_TRUE or PETSC_FALSE 3250 3251 Level: intermediate 3252 3253 .keywords: TS, set 3254 3255 .seealso: TSInterpolate(), TSSetPostStep() 3256 @*/ 3257 PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3258 { 3259 PetscFunctionBegin; 3260 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3261 ts->retain_stages = flg; 3262 PetscFunctionReturn(0); 3263 } 3264 3265 #undef __FUNCT__ 3266 #define __FUNCT__ "TSInterpolate" 3267 /*@ 3268 TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3269 3270 Collective on TS 3271 3272 Input Argument: 3273 + ts - time stepping context 3274 - t - time to interpolate to 3275 3276 Output Argument: 3277 . U - state at given time 3278 3279 Notes: 3280 The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3281 3282 Level: intermediate 3283 3284 Developer Notes: 3285 TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3286 3287 .keywords: TS, set 3288 3289 .seealso: TSSetRetainStages(), TSSetPostStep() 3290 @*/ 3291 PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3292 { 3293 PetscErrorCode ierr; 3294 3295 PetscFunctionBegin; 3296 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3297 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3298 if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime); 3299 if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 3300 ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3301 PetscFunctionReturn(0); 3302 } 3303 3304 #undef __FUNCT__ 3305 #define __FUNCT__ "TSStep" 3306 /*@ 3307 TSStep - Steps one time step 3308 3309 Collective on TS 3310 3311 Input Parameter: 3312 . ts - the TS context obtained from TSCreate() 3313 3314 Level: developer 3315 3316 Notes: 3317 The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 3318 3319 The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3320 be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3321 3322 This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 3323 time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 3324 3325 .keywords: TS, timestep, solve 3326 3327 .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3328 @*/ 3329 PetscErrorCode TSStep(TS ts) 3330 { 3331 DM dm; 3332 PetscErrorCode ierr; 3333 static PetscBool cite = PETSC_FALSE; 3334 3335 PetscFunctionBegin; 3336 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3337 if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()"); 3338 3339 ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3340 " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3341 " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3342 " type = {Preprint},\n" 3343 " number = {ANL/MCS-P5061-0114},\n" 3344 " institution = {Argonne National Laboratory},\n" 3345 " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3346 3347 ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3348 ierr = TSSetUp(ts);CHKERRQ(ierr); 3349 ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3350 3351 ts->reason = TS_CONVERGED_ITERATING; 3352 ts->ptime_prev = ts->ptime; 3353 ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3354 3355 if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3356 ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3357 ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3358 ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3359 3360 ts->time_step_prev = ts->ptime - ts->ptime_prev; 3361 ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3362 3363 if (ts->reason < 0) { 3364 if (ts->errorifstepfailed) { 3365 if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3366 else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3367 } 3368 } else if (!ts->reason) { 3369 if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3370 else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3371 } 3372 ts->total_steps++; 3373 ts->steprollback = PETSC_FALSE; 3374 PetscFunctionReturn(0); 3375 } 3376 3377 #undef __FUNCT__ 3378 #define __FUNCT__ "TSAdjointStep" 3379 /*@ 3380 TSAdjointStep - Steps one time step backward in the adjoint run 3381 3382 Collective on TS 3383 3384 Input Parameter: 3385 . ts - the TS context obtained from TSCreate() 3386 3387 Level: intermediate 3388 3389 .keywords: TS, adjoint, step 3390 3391 .seealso: TSAdjointSetUp(), TSAdjointSolve() 3392 @*/ 3393 PetscErrorCode TSAdjointStep(TS ts) 3394 { 3395 DM dm; 3396 PetscErrorCode ierr; 3397 3398 PetscFunctionBegin; 3399 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3400 ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3401 ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3402 3403 ts->reason = TS_CONVERGED_ITERATING; 3404 ts->ptime_prev = ts->ptime; 3405 ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3406 ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3407 3408 ierr = PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3409 if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name); 3410 ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3411 ierr = PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);CHKERRQ(ierr); 3412 3413 ts->time_step_prev = ts->ptime - ts->ptime_prev; 3414 ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3415 3416 if (ts->reason < 0) { 3417 if (ts->errorifstepfailed) { 3418 if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3419 else if (ts->reason == TS_DIVERGED_STEP_REJECTED) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3420 else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3421 } 3422 } else if (!ts->reason) { 3423 if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3424 } 3425 ts->total_steps--; 3426 PetscFunctionReturn(0); 3427 } 3428 3429 #undef __FUNCT__ 3430 #define __FUNCT__ "TSEvaluateStep" 3431 /*@ 3432 TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 3433 3434 Collective on TS 3435 3436 Input Arguments: 3437 + ts - time stepping context 3438 . order - desired order of accuracy 3439 - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 3440 3441 Output Arguments: 3442 . U - state at the end of the current step 3443 3444 Level: advanced 3445 3446 Notes: 3447 This function cannot be called until all stages have been evaluated. 3448 It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after TSStep() has returned. 3449 3450 .seealso: TSStep(), TSAdapt 3451 @*/ 3452 PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 3453 { 3454 PetscErrorCode ierr; 3455 3456 PetscFunctionBegin; 3457 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3458 PetscValidType(ts,1); 3459 PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3460 if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3461 ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 3462 PetscFunctionReturn(0); 3463 } 3464 3465 #undef __FUNCT__ 3466 #define __FUNCT__ "TSForwardCostIntegral" 3467 /*@ 3468 TSForwardCostIntegral - Evaluate the cost integral in the forward run. 3469 3470 Collective on TS 3471 3472 Input Arguments: 3473 . ts - time stepping context 3474 3475 Level: advanced 3476 3477 Notes: 3478 This function cannot be called until TSStep() has been completed. 3479 3480 .seealso: TSSolve(), TSAdjointCostIntegral() 3481 @*/ 3482 PetscErrorCode TSForwardCostIntegral(TS ts) 3483 { 3484 PetscErrorCode ierr; 3485 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3486 if (!ts->ops->forwardintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the forward run",((PetscObject)ts)->type_name); 3487 ierr = (*ts->ops->forwardintegral)(ts);CHKERRQ(ierr); 3488 PetscFunctionReturn(0); 3489 } 3490 3491 #undef __FUNCT__ 3492 #define __FUNCT__ "TSSolve" 3493 /*@ 3494 TSSolve - Steps the requested number of timesteps. 3495 3496 Collective on TS 3497 3498 Input Parameter: 3499 + ts - the TS context obtained from TSCreate() 3500 - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 3501 3502 Level: beginner 3503 3504 Notes: 3505 The final time returned by this function may be different from the time of the internally 3506 held state accessible by TSGetSolution() and TSGetTime() because the method may have 3507 stepped over the final time. 3508 3509 .keywords: TS, timestep, solve 3510 3511 .seealso: TSCreate(), TSSetSolution(), TSStep() 3512 @*/ 3513 PetscErrorCode TSSolve(TS ts,Vec u) 3514 { 3515 Vec solution; 3516 PetscErrorCode ierr; 3517 3518 PetscFunctionBegin; 3519 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3520 if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3521 if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()"); 3522 if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 3523 3524 if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 3525 PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3526 if (!ts->vec_sol || u == ts->vec_sol) { 3527 ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3528 ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3529 ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 3530 } 3531 ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3532 } else if (u) { 3533 ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 3534 } 3535 ierr = TSSetUp(ts);CHKERRQ(ierr); 3536 ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3537 /* reset time step and iteration counters */ 3538 ts->steps = 0; 3539 ts->ksp_its = 0; 3540 ts->snes_its = 0; 3541 ts->num_snes_failures = 0; 3542 ts->reject = 0; 3543 ts->reason = TS_CONVERGED_ITERATING; 3544 3545 ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3546 { 3547 DM dm; 3548 ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3549 ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3550 } 3551 3552 if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3553 ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3554 ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3555 ts->solvetime = ts->ptime; 3556 } else { 3557 /* steps the requested number of timesteps. */ 3558 if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3559 else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3560 ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3561 if(ts->event) { 3562 ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 3563 } 3564 while (!ts->reason) { 3565 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3566 ierr = TSStep(ts);CHKERRQ(ierr); 3567 if (!ts->steprollback && ts->vec_costintegral && ts->costintegralfwd) { 3568 ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 3569 } 3570 if (ts->event) { 3571 ierr = TSEventMonitor(ts);CHKERRQ(ierr); 3572 } 3573 if(!ts->steprollback) { 3574 ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3575 ierr = TSPostStep(ts);CHKERRQ(ierr); 3576 } 3577 } 3578 if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 3579 ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3580 ts->solvetime = ts->max_time; 3581 solution = u; 3582 } else { 3583 if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3584 ts->solvetime = ts->ptime; 3585 solution = ts->vec_sol; 3586 } 3587 ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3588 ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3589 } 3590 3591 ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3592 ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 3593 ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3594 if (ts->adjoint_solve) { 3595 ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 3596 } 3597 PetscFunctionReturn(0); 3598 } 3599 3600 #undef __FUNCT__ 3601 #define __FUNCT__ "TSAdjointCostIntegral" 3602 /*@ 3603 TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run. 3604 3605 Collective on TS 3606 3607 Input Arguments: 3608 . ts - time stepping context 3609 3610 Level: advanced 3611 3612 Notes: 3613 This function cannot be called until TSAdjointStep() has been completed. 3614 3615 .seealso: TSAdjointSolve(), TSAdjointStep 3616 @*/ 3617 PetscErrorCode TSAdjointCostIntegral(TS ts) 3618 { 3619 PetscErrorCode ierr; 3620 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3621 if (!ts->ops->adjointintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the adjoint run",((PetscObject)ts)->type_name); 3622 ierr = (*ts->ops->adjointintegral)(ts);CHKERRQ(ierr); 3623 PetscFunctionReturn(0); 3624 } 3625 3626 #undef __FUNCT__ 3627 #define __FUNCT__ "TSAdjointSolve" 3628 /*@ 3629 TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3630 3631 Collective on TS 3632 3633 Input Parameter: 3634 . ts - the TS context obtained from TSCreate() 3635 3636 Options Database: 3637 . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3638 3639 Level: intermediate 3640 3641 Notes: 3642 This must be called after a call to TSSolve() that solves the forward problem 3643 3644 By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 3645 3646 .keywords: TS, timestep, solve 3647 3648 .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3649 @*/ 3650 PetscErrorCode TSAdjointSolve(TS ts) 3651 { 3652 PetscErrorCode ierr; 3653 3654 PetscFunctionBegin; 3655 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3656 ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3657 3658 /* reset time step and iteration counters */ 3659 ts->steps = 0; 3660 ts->ksp_its = 0; 3661 ts->snes_its = 0; 3662 ts->num_snes_failures = 0; 3663 ts->reject = 0; 3664 ts->reason = TS_CONVERGED_ITERATING; 3665 3666 if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3667 3668 if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3669 while (!ts->reason) { 3670 ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 3671 ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3672 if (ts->event) { 3673 ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3674 } 3675 ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3676 if (ts->vec_costintegral && !ts->costintegralfwd) { 3677 ierr = TSAdjointCostIntegral(ts);CHKERRQ(ierr); 3678 } 3679 } 3680 ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 3681 ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3682 ts->solvetime = ts->ptime; 3683 ierr = TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-tstrajectory_view");CHKERRQ(ierr); 3684 ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3685 PetscFunctionReturn(0); 3686 } 3687 3688 #undef __FUNCT__ 3689 #define __FUNCT__ "TSMonitor" 3690 /*@C 3691 TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3692 3693 Collective on TS 3694 3695 Input Parameters: 3696 + ts - time stepping context obtained from TSCreate() 3697 . step - step number that has just completed 3698 . ptime - model time of the state 3699 - u - state at the current model time 3700 3701 Notes: 3702 TSMonitor() is typically used automatically within the time stepping implementations. 3703 Users would almost never call this routine directly. 3704 3705 Level: developer 3706 3707 .keywords: TS, timestep 3708 @*/ 3709 PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3710 { 3711 PetscErrorCode ierr; 3712 PetscInt i,n = ts->numbermonitors; 3713 3714 PetscFunctionBegin; 3715 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3716 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 3717 ierr = VecLockPush(u);CHKERRQ(ierr); 3718 for (i=0; i<n; i++) { 3719 ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3720 } 3721 ierr = VecLockPop(u);CHKERRQ(ierr); 3722 PetscFunctionReturn(0); 3723 } 3724 3725 #undef __FUNCT__ 3726 #define __FUNCT__ "TSAdjointMonitor" 3727 /*@C 3728 TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 3729 3730 Collective on TS 3731 3732 Input Parameters: 3733 + ts - time stepping context obtained from TSCreate() 3734 . step - step number that has just completed 3735 . ptime - model time of the state 3736 . u - state at the current model time 3737 . numcost - number of cost functions (dimension of lambda or mu) 3738 . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 3739 - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 3740 3741 Notes: 3742 TSAdjointMonitor() is typically used automatically within the time stepping implementations. 3743 Users would almost never call this routine directly. 3744 3745 Level: developer 3746 3747 .keywords: TS, timestep 3748 @*/ 3749 PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 3750 { 3751 PetscErrorCode ierr; 3752 PetscInt i,n = ts->numberadjointmonitors; 3753 3754 PetscFunctionBegin; 3755 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3756 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 3757 ierr = VecLockPush(u);CHKERRQ(ierr); 3758 for (i=0; i<n; i++) { 3759 ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 3760 } 3761 ierr = VecLockPop(u);CHKERRQ(ierr); 3762 PetscFunctionReturn(0); 3763 } 3764 3765 /* ------------------------------------------------------------------------*/ 3766 #undef __FUNCT__ 3767 #define __FUNCT__ "TSMonitorLGCtxCreate" 3768 /*@C 3769 TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with 3770 TS to monitor the solution process graphically in various ways 3771 3772 Collective on TS 3773 3774 Input Parameters: 3775 + host - the X display to open, or null for the local machine 3776 . label - the title to put in the title bar 3777 . x, y - the screen coordinates of the upper left coordinate of the window 3778 . m, n - the screen width and height in pixels 3779 - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3780 3781 Output Parameter: 3782 . ctx - the context 3783 3784 Options Database Key: 3785 + -ts_monitor_lg_timestep - automatically sets line graph monitor 3786 . -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables()) 3787 . -ts_monitor_lg_error - monitor the error 3788 . -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep 3789 . -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep 3790 - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3791 3792 Notes: 3793 Use TSMonitorLGCtxDestroy() to destroy. 3794 3795 One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform() 3796 3797 Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the 3798 first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object 3799 as the first argument. 3800 3801 One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames() 3802 3803 3804 Level: intermediate 3805 3806 .keywords: TS, monitor, line graph, residual 3807 3808 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(), 3809 TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 3810 TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 3811 TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 3812 TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 3813 3814 @*/ 3815 PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3816 { 3817 PetscDraw draw; 3818 PetscErrorCode ierr; 3819 3820 PetscFunctionBegin; 3821 ierr = PetscNew(ctx);CHKERRQ(ierr); 3822 ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 3823 ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 3824 ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr); 3825 ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3826 ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 3827 ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3828 (*ctx)->howoften = howoften; 3829 PetscFunctionReturn(0); 3830 } 3831 3832 #undef __FUNCT__ 3833 #define __FUNCT__ "TSMonitorLGTimeStep" 3834 PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3835 { 3836 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3837 PetscReal x = ptime,y; 3838 PetscErrorCode ierr; 3839 3840 PetscFunctionBegin; 3841 if (!step) { 3842 PetscDrawAxis axis; 3843 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3844 ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3845 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3846 } 3847 ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 3848 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3849 if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 3850 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 3851 } 3852 PetscFunctionReturn(0); 3853 } 3854 3855 #undef __FUNCT__ 3856 #define __FUNCT__ "TSMonitorLGCtxDestroy" 3857 /*@C 3858 TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3859 with TSMonitorLGCtxCreate(). 3860 3861 Collective on TSMonitorLGCtx 3862 3863 Input Parameter: 3864 . ctx - the monitor context 3865 3866 Level: intermediate 3867 3868 .keywords: TS, monitor, line graph, destroy 3869 3870 .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3871 @*/ 3872 PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3873 { 3874 PetscErrorCode ierr; 3875 3876 PetscFunctionBegin; 3877 if ((*ctx)->transformdestroy) { 3878 ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 3879 } 3880 ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 3881 ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3882 ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3883 ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3884 ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 3885 ierr = PetscFree(*ctx);CHKERRQ(ierr); 3886 PetscFunctionReturn(0); 3887 } 3888 3889 #undef __FUNCT__ 3890 #define __FUNCT__ "TSGetTime" 3891 /*@ 3892 TSGetTime - Gets the time of the most recently completed step. 3893 3894 Not Collective 3895 3896 Input Parameter: 3897 . ts - the TS context obtained from TSCreate() 3898 3899 Output Parameter: 3900 . t - the current time 3901 3902 Level: beginner 3903 3904 Note: 3905 When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 3906 TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3907 3908 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3909 3910 .keywords: TS, get, time 3911 @*/ 3912 PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3913 { 3914 PetscFunctionBegin; 3915 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3916 PetscValidRealPointer(t,2); 3917 *t = ts->ptime; 3918 PetscFunctionReturn(0); 3919 } 3920 3921 #undef __FUNCT__ 3922 #define __FUNCT__ "TSGetPrevTime" 3923 /*@ 3924 TSGetPrevTime - Gets the starting time of the previously completed step. 3925 3926 Not Collective 3927 3928 Input Parameter: 3929 . ts - the TS context obtained from TSCreate() 3930 3931 Output Parameter: 3932 . t - the previous time 3933 3934 Level: beginner 3935 3936 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3937 3938 .keywords: TS, get, time 3939 @*/ 3940 PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3941 { 3942 PetscFunctionBegin; 3943 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3944 PetscValidRealPointer(t,2); 3945 *t = ts->ptime_prev; 3946 PetscFunctionReturn(0); 3947 } 3948 3949 #undef __FUNCT__ 3950 #define __FUNCT__ "TSSetTime" 3951 /*@ 3952 TSSetTime - Allows one to reset the time. 3953 3954 Logically Collective on TS 3955 3956 Input Parameters: 3957 + ts - the TS context obtained from TSCreate() 3958 - time - the time 3959 3960 Level: intermediate 3961 3962 .seealso: TSGetTime(), TSSetDuration() 3963 3964 .keywords: TS, set, time 3965 @*/ 3966 PetscErrorCode TSSetTime(TS ts, PetscReal t) 3967 { 3968 PetscFunctionBegin; 3969 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3970 PetscValidLogicalCollectiveReal(ts,t,2); 3971 ts->ptime = t; 3972 PetscFunctionReturn(0); 3973 } 3974 3975 #undef __FUNCT__ 3976 #define __FUNCT__ "TSSetOptionsPrefix" 3977 /*@C 3978 TSSetOptionsPrefix - Sets the prefix used for searching for all 3979 TS options in the database. 3980 3981 Logically Collective on TS 3982 3983 Input Parameter: 3984 + ts - The TS context 3985 - prefix - The prefix to prepend to all option names 3986 3987 Notes: 3988 A hyphen (-) must NOT be given at the beginning of the prefix name. 3989 The first character of all runtime options is AUTOMATICALLY the 3990 hyphen. 3991 3992 Level: advanced 3993 3994 .keywords: TS, set, options, prefix, database 3995 3996 .seealso: TSSetFromOptions() 3997 3998 @*/ 3999 PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4000 { 4001 PetscErrorCode ierr; 4002 SNES snes; 4003 4004 PetscFunctionBegin; 4005 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4006 ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4007 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4008 ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4009 PetscFunctionReturn(0); 4010 } 4011 4012 4013 #undef __FUNCT__ 4014 #define __FUNCT__ "TSAppendOptionsPrefix" 4015 /*@C 4016 TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4017 TS options in the database. 4018 4019 Logically Collective on TS 4020 4021 Input Parameter: 4022 + ts - The TS context 4023 - prefix - The prefix to prepend to all option names 4024 4025 Notes: 4026 A hyphen (-) must NOT be given at the beginning of the prefix name. 4027 The first character of all runtime options is AUTOMATICALLY the 4028 hyphen. 4029 4030 Level: advanced 4031 4032 .keywords: TS, append, options, prefix, database 4033 4034 .seealso: TSGetOptionsPrefix() 4035 4036 @*/ 4037 PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4038 { 4039 PetscErrorCode ierr; 4040 SNES snes; 4041 4042 PetscFunctionBegin; 4043 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4044 ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4045 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4046 ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4047 PetscFunctionReturn(0); 4048 } 4049 4050 #undef __FUNCT__ 4051 #define __FUNCT__ "TSGetOptionsPrefix" 4052 /*@C 4053 TSGetOptionsPrefix - Sets the prefix used for searching for all 4054 TS options in the database. 4055 4056 Not Collective 4057 4058 Input Parameter: 4059 . ts - The TS context 4060 4061 Output Parameter: 4062 . prefix - A pointer to the prefix string used 4063 4064 Notes: On the fortran side, the user should pass in a string 'prifix' of 4065 sufficient length to hold the prefix. 4066 4067 Level: intermediate 4068 4069 .keywords: TS, get, options, prefix, database 4070 4071 .seealso: TSAppendOptionsPrefix() 4072 @*/ 4073 PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4074 { 4075 PetscErrorCode ierr; 4076 4077 PetscFunctionBegin; 4078 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4079 PetscValidPointer(prefix,2); 4080 ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4081 PetscFunctionReturn(0); 4082 } 4083 4084 #undef __FUNCT__ 4085 #define __FUNCT__ "TSGetRHSJacobian" 4086 /*@C 4087 TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4088 4089 Not Collective, but parallel objects are returned if TS is parallel 4090 4091 Input Parameter: 4092 . ts - The TS context obtained from TSCreate() 4093 4094 Output Parameters: 4095 + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4096 . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4097 . func - Function to compute the Jacobian of the RHS (or NULL) 4098 - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4099 4100 Notes: You can pass in NULL for any return argument you do not need. 4101 4102 Level: intermediate 4103 4104 .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4105 4106 .keywords: TS, timestep, get, matrix, Jacobian 4107 @*/ 4108 PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4109 { 4110 PetscErrorCode ierr; 4111 SNES snes; 4112 DM dm; 4113 4114 PetscFunctionBegin; 4115 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4116 ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4117 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4118 ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4119 PetscFunctionReturn(0); 4120 } 4121 4122 #undef __FUNCT__ 4123 #define __FUNCT__ "TSGetIJacobian" 4124 /*@C 4125 TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 4126 4127 Not Collective, but parallel objects are returned if TS is parallel 4128 4129 Input Parameter: 4130 . ts - The TS context obtained from TSCreate() 4131 4132 Output Parameters: 4133 + Amat - The (approximate) Jacobian of F(t,U,U_t) 4134 . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 4135 . f - The function to compute the matrices 4136 - ctx - User-defined context for Jacobian evaluation routine 4137 4138 Notes: You can pass in NULL for any return argument you do not need. 4139 4140 Level: advanced 4141 4142 .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 4143 4144 .keywords: TS, timestep, get, matrix, Jacobian 4145 @*/ 4146 PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 4147 { 4148 PetscErrorCode ierr; 4149 SNES snes; 4150 DM dm; 4151 4152 PetscFunctionBegin; 4153 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4154 ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4155 ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4156 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 4157 ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 4158 PetscFunctionReturn(0); 4159 } 4160 4161 4162 #undef __FUNCT__ 4163 #define __FUNCT__ "TSMonitorDrawSolution" 4164 /*@C 4165 TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 4166 VecView() for the solution at each timestep 4167 4168 Collective on TS 4169 4170 Input Parameters: 4171 + ts - the TS context 4172 . step - current time-step 4173 . ptime - current time 4174 - dummy - either a viewer or NULL 4175 4176 Options Database: 4177 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 4178 4179 Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 4180 will look bad 4181 4182 Level: intermediate 4183 4184 .keywords: TS, vector, monitor, view 4185 4186 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4187 @*/ 4188 PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4189 { 4190 PetscErrorCode ierr; 4191 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4192 PetscDraw draw; 4193 4194 PetscFunctionBegin; 4195 if (!step && ictx->showinitial) { 4196 if (!ictx->initialsolution) { 4197 ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 4198 } 4199 ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 4200 } 4201 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 4202 4203 if (ictx->showinitial) { 4204 PetscReal pause; 4205 ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 4206 ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 4207 ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 4208 ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 4209 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 4210 } 4211 ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 4212 if (ictx->showtimestepandtime) { 4213 PetscReal xl,yl,xr,yr,h; 4214 char time[32]; 4215 4216 ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 4217 ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4218 ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4219 h = yl + .95*(yr - yl); 4220 ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4221 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4222 } 4223 4224 if (ictx->showinitial) { 4225 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 4226 } 4227 PetscFunctionReturn(0); 4228 } 4229 4230 #undef __FUNCT__ 4231 #define __FUNCT__ "TSAdjointMonitorDrawSensi" 4232 /*@C 4233 TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 4234 VecView() for the sensitivities to initial states at each timestep 4235 4236 Collective on TS 4237 4238 Input Parameters: 4239 + ts - the TS context 4240 . step - current time-step 4241 . ptime - current time 4242 . u - current state 4243 . numcost - number of cost functions 4244 . lambda - sensitivities to initial conditions 4245 . mu - sensitivities to parameters 4246 - dummy - either a viewer or NULL 4247 4248 Level: intermediate 4249 4250 .keywords: TS, vector, adjoint, monitor, view 4251 4252 .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 4253 @*/ 4254 PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 4255 { 4256 PetscErrorCode ierr; 4257 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4258 PetscDraw draw; 4259 PetscReal xl,yl,xr,yr,h; 4260 char time[32]; 4261 4262 PetscFunctionBegin; 4263 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 4264 4265 ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 4266 ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 4267 ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4268 ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4269 h = yl + .95*(yr - yl); 4270 ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4271 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4272 4273 PetscFunctionReturn(0); 4274 } 4275 4276 #undef __FUNCT__ 4277 #define __FUNCT__ "TSMonitorDrawSolutionPhase" 4278 /*@C 4279 TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 4280 4281 Collective on TS 4282 4283 Input Parameters: 4284 + ts - the TS context 4285 . step - current time-step 4286 . ptime - current time 4287 - dummy - either a viewer or NULL 4288 4289 Level: intermediate 4290 4291 .keywords: TS, vector, monitor, view 4292 4293 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4294 @*/ 4295 PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4296 { 4297 PetscErrorCode ierr; 4298 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 4299 PetscDraw draw; 4300 MPI_Comm comm; 4301 PetscInt n; 4302 PetscMPIInt size; 4303 PetscReal xl,yl,xr,yr,h; 4304 char time[32]; 4305 const PetscScalar *U; 4306 4307 PetscFunctionBegin; 4308 ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 4309 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4310 if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 4311 ierr = VecGetSize(u,&n);CHKERRQ(ierr); 4312 if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 4313 4314 ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 4315 4316 ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 4317 ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 4318 if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 4319 ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4320 PetscFunctionReturn(0); 4321 } 4322 if (!step) ictx->color++; 4323 ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 4324 ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4325 4326 if (ictx->showtimestepandtime) { 4327 ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 4328 ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 4329 h = yl + .95*(yr - yl); 4330 ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 4331 } 4332 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 4333 PetscFunctionReturn(0); 4334 } 4335 4336 4337 #undef __FUNCT__ 4338 #define __FUNCT__ "TSMonitorDrawCtxDestroy" 4339 /*@C 4340 TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 4341 4342 Collective on TS 4343 4344 Input Parameters: 4345 . ctx - the monitor context 4346 4347 Level: intermediate 4348 4349 .keywords: TS, vector, monitor, view 4350 4351 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 4352 @*/ 4353 PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 4354 { 4355 PetscErrorCode ierr; 4356 4357 PetscFunctionBegin; 4358 ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 4359 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 4360 ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 4361 ierr = PetscFree(*ictx);CHKERRQ(ierr); 4362 PetscFunctionReturn(0); 4363 } 4364 4365 #undef __FUNCT__ 4366 #define __FUNCT__ "TSMonitorDrawCtxCreate" 4367 /*@C 4368 TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 4369 4370 Collective on TS 4371 4372 Input Parameter: 4373 . ts - time-step context 4374 4375 Output Patameter: 4376 . ctx - the monitor context 4377 4378 Options Database: 4379 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 4380 4381 Level: intermediate 4382 4383 .keywords: TS, vector, monitor, view 4384 4385 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 4386 @*/ 4387 PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 4388 { 4389 PetscErrorCode ierr; 4390 4391 PetscFunctionBegin; 4392 ierr = PetscNew(ctx);CHKERRQ(ierr); 4393 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4394 ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4395 4396 (*ctx)->howoften = howoften; 4397 (*ctx)->showinitial = PETSC_FALSE; 4398 ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4399 4400 (*ctx)->showtimestepandtime = PETSC_FALSE; 4401 ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 4402 (*ctx)->color = PETSC_DRAW_WHITE; 4403 PetscFunctionReturn(0); 4404 } 4405 4406 #undef __FUNCT__ 4407 #define __FUNCT__ "TSMonitorDrawError" 4408 /*@C 4409 TSMonitorDrawError - Monitors progress of the TS solvers by calling 4410 VecView() for the error at each timestep 4411 4412 Collective on TS 4413 4414 Input Parameters: 4415 + ts - the TS context 4416 . step - current time-step 4417 . ptime - current time 4418 - dummy - either a viewer or NULL 4419 4420 Level: intermediate 4421 4422 .keywords: TS, vector, monitor, view 4423 4424 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4425 @*/ 4426 PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 4427 { 4428 PetscErrorCode ierr; 4429 TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 4430 PetscViewer viewer = ctx->viewer; 4431 Vec work; 4432 4433 PetscFunctionBegin; 4434 if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 4435 ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 4436 ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 4437 ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 4438 ierr = VecView(work,viewer);CHKERRQ(ierr); 4439 ierr = VecDestroy(&work);CHKERRQ(ierr); 4440 PetscFunctionReturn(0); 4441 } 4442 4443 #include <petsc/private/dmimpl.h> 4444 #undef __FUNCT__ 4445 #define __FUNCT__ "TSSetDM" 4446 /*@ 4447 TSSetDM - Sets the DM that may be used by some preconditioners 4448 4449 Logically Collective on TS and DM 4450 4451 Input Parameters: 4452 + ts - the preconditioner context 4453 - dm - the dm 4454 4455 Level: intermediate 4456 4457 4458 .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 4459 @*/ 4460 PetscErrorCode TSSetDM(TS ts,DM dm) 4461 { 4462 PetscErrorCode ierr; 4463 SNES snes; 4464 DMTS tsdm; 4465 4466 PetscFunctionBegin; 4467 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4468 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4469 if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 4470 if (ts->dm->dmts && !dm->dmts) { 4471 ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4472 ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 4473 if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 4474 tsdm->originaldm = dm; 4475 } 4476 } 4477 ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 4478 } 4479 ts->dm = dm; 4480 4481 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4482 ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 4483 PetscFunctionReturn(0); 4484 } 4485 4486 #undef __FUNCT__ 4487 #define __FUNCT__ "TSGetDM" 4488 /*@ 4489 TSGetDM - Gets the DM that may be used by some preconditioners 4490 4491 Not Collective 4492 4493 Input Parameter: 4494 . ts - the preconditioner context 4495 4496 Output Parameter: 4497 . dm - the dm 4498 4499 Level: intermediate 4500 4501 4502 .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 4503 @*/ 4504 PetscErrorCode TSGetDM(TS ts,DM *dm) 4505 { 4506 PetscErrorCode ierr; 4507 4508 PetscFunctionBegin; 4509 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4510 if (!ts->dm) { 4511 ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4512 if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4513 } 4514 *dm = ts->dm; 4515 PetscFunctionReturn(0); 4516 } 4517 4518 #undef __FUNCT__ 4519 #define __FUNCT__ "SNESTSFormFunction" 4520 /*@ 4521 SNESTSFormFunction - Function to evaluate nonlinear residual 4522 4523 Logically Collective on SNES 4524 4525 Input Parameter: 4526 + snes - nonlinear solver 4527 . U - the current state at which to evaluate the residual 4528 - ctx - user context, must be a TS 4529 4530 Output Parameter: 4531 . F - the nonlinear residual 4532 4533 Notes: 4534 This function is not normally called by users and is automatically registered with the SNES used by TS. 4535 It is most frequently passed to MatFDColoringSetFunction(). 4536 4537 Level: advanced 4538 4539 .seealso: SNESSetFunction(), MatFDColoringSetFunction() 4540 @*/ 4541 PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 4542 { 4543 TS ts = (TS)ctx; 4544 PetscErrorCode ierr; 4545 4546 PetscFunctionBegin; 4547 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4548 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4549 PetscValidHeaderSpecific(F,VEC_CLASSID,3); 4550 PetscValidHeaderSpecific(ts,TS_CLASSID,4); 4551 ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 4552 PetscFunctionReturn(0); 4553 } 4554 4555 #undef __FUNCT__ 4556 #define __FUNCT__ "SNESTSFormJacobian" 4557 /*@ 4558 SNESTSFormJacobian - Function to evaluate the Jacobian 4559 4560 Collective on SNES 4561 4562 Input Parameter: 4563 + snes - nonlinear solver 4564 . U - the current state at which to evaluate the residual 4565 - ctx - user context, must be a TS 4566 4567 Output Parameter: 4568 + A - the Jacobian 4569 . B - the preconditioning matrix (may be the same as A) 4570 - flag - indicates any structure change in the matrix 4571 4572 Notes: 4573 This function is not normally called by users and is automatically registered with the SNES used by TS. 4574 4575 Level: developer 4576 4577 .seealso: SNESSetJacobian() 4578 @*/ 4579 PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 4580 { 4581 TS ts = (TS)ctx; 4582 PetscErrorCode ierr; 4583 4584 PetscFunctionBegin; 4585 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4586 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4587 PetscValidPointer(A,3); 4588 PetscValidHeaderSpecific(A,MAT_CLASSID,3); 4589 PetscValidPointer(B,4); 4590 PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4591 PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4592 ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 4593 PetscFunctionReturn(0); 4594 } 4595 4596 #undef __FUNCT__ 4597 #define __FUNCT__ "TSComputeRHSFunctionLinear" 4598 /*@C 4599 TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 4600 4601 Collective on TS 4602 4603 Input Arguments: 4604 + ts - time stepping context 4605 . t - time at which to evaluate 4606 . U - state at which to evaluate 4607 - ctx - context 4608 4609 Output Arguments: 4610 . F - right hand side 4611 4612 Level: intermediate 4613 4614 Notes: 4615 This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 4616 The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 4617 4618 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 4619 @*/ 4620 PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 4621 { 4622 PetscErrorCode ierr; 4623 Mat Arhs,Brhs; 4624 4625 PetscFunctionBegin; 4626 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4627 ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 4628 ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 4629 PetscFunctionReturn(0); 4630 } 4631 4632 #undef __FUNCT__ 4633 #define __FUNCT__ "TSComputeRHSJacobianConstant" 4634 /*@C 4635 TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 4636 4637 Collective on TS 4638 4639 Input Arguments: 4640 + ts - time stepping context 4641 . t - time at which to evaluate 4642 . U - state at which to evaluate 4643 - ctx - context 4644 4645 Output Arguments: 4646 + A - pointer to operator 4647 . B - pointer to preconditioning matrix 4648 - flg - matrix structure flag 4649 4650 Level: intermediate 4651 4652 Notes: 4653 This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 4654 4655 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 4656 @*/ 4657 PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 4658 { 4659 PetscFunctionBegin; 4660 PetscFunctionReturn(0); 4661 } 4662 4663 #undef __FUNCT__ 4664 #define __FUNCT__ "TSComputeIFunctionLinear" 4665 /*@C 4666 TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 4667 4668 Collective on TS 4669 4670 Input Arguments: 4671 + ts - time stepping context 4672 . t - time at which to evaluate 4673 . U - state at which to evaluate 4674 . Udot - time derivative of state vector 4675 - ctx - context 4676 4677 Output Arguments: 4678 . F - left hand side 4679 4680 Level: intermediate 4681 4682 Notes: 4683 The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the 4684 user is required to write their own TSComputeIFunction. 4685 This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 4686 The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 4687 4688 Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 4689 4690 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 4691 @*/ 4692 PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 4693 { 4694 PetscErrorCode ierr; 4695 Mat A,B; 4696 4697 PetscFunctionBegin; 4698 ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4699 ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 4700 ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 4701 PetscFunctionReturn(0); 4702 } 4703 4704 #undef __FUNCT__ 4705 #define __FUNCT__ "TSComputeIJacobianConstant" 4706 /*@C 4707 TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 4708 4709 Collective on TS 4710 4711 Input Arguments: 4712 + ts - time stepping context 4713 . t - time at which to evaluate 4714 . U - state at which to evaluate 4715 . Udot - time derivative of state vector 4716 . shift - shift to apply 4717 - ctx - context 4718 4719 Output Arguments: 4720 + A - pointer to operator 4721 . B - pointer to preconditioning matrix 4722 - flg - matrix structure flag 4723 4724 Level: advanced 4725 4726 Notes: 4727 This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 4728 4729 It is only appropriate for problems of the form 4730 4731 $ M Udot = F(U,t) 4732 4733 where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4734 works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4735 an implicit operator of the form 4736 4737 $ shift*M + J 4738 4739 where J is the Jacobian of -F(U). Support may be added in a future version of PETSc, but for now, the user must store 4740 a copy of M or reassemble it when requested. 4741 4742 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 4743 @*/ 4744 PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 4745 { 4746 PetscErrorCode ierr; 4747 4748 PetscFunctionBegin; 4749 ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4750 ts->ijacobian.shift = shift; 4751 PetscFunctionReturn(0); 4752 } 4753 4754 #undef __FUNCT__ 4755 #define __FUNCT__ "TSGetEquationType" 4756 /*@ 4757 TSGetEquationType - Gets the type of the equation that TS is solving. 4758 4759 Not Collective 4760 4761 Input Parameter: 4762 . ts - the TS context 4763 4764 Output Parameter: 4765 . equation_type - see TSEquationType 4766 4767 Level: beginner 4768 4769 .keywords: TS, equation type 4770 4771 .seealso: TSSetEquationType(), TSEquationType 4772 @*/ 4773 PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4774 { 4775 PetscFunctionBegin; 4776 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4777 PetscValidPointer(equation_type,2); 4778 *equation_type = ts->equation_type; 4779 PetscFunctionReturn(0); 4780 } 4781 4782 #undef __FUNCT__ 4783 #define __FUNCT__ "TSSetEquationType" 4784 /*@ 4785 TSSetEquationType - Sets the type of the equation that TS is solving. 4786 4787 Not Collective 4788 4789 Input Parameter: 4790 + ts - the TS context 4791 - equation_type - see TSEquationType 4792 4793 Level: advanced 4794 4795 .keywords: TS, equation type 4796 4797 .seealso: TSGetEquationType(), TSEquationType 4798 @*/ 4799 PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4800 { 4801 PetscFunctionBegin; 4802 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4803 ts->equation_type = equation_type; 4804 PetscFunctionReturn(0); 4805 } 4806 4807 #undef __FUNCT__ 4808 #define __FUNCT__ "TSGetConvergedReason" 4809 /*@ 4810 TSGetConvergedReason - Gets the reason the TS iteration was stopped. 4811 4812 Not Collective 4813 4814 Input Parameter: 4815 . ts - the TS context 4816 4817 Output Parameter: 4818 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4819 manual pages for the individual convergence tests for complete lists 4820 4821 Level: beginner 4822 4823 Notes: 4824 Can only be called after the call to TSSolve() is complete. 4825 4826 .keywords: TS, nonlinear, set, convergence, test 4827 4828 .seealso: TSSetConvergenceTest(), TSConvergedReason 4829 @*/ 4830 PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 4831 { 4832 PetscFunctionBegin; 4833 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4834 PetscValidPointer(reason,2); 4835 *reason = ts->reason; 4836 PetscFunctionReturn(0); 4837 } 4838 4839 #undef __FUNCT__ 4840 #define __FUNCT__ "TSSetConvergedReason" 4841 /*@ 4842 TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4843 4844 Not Collective 4845 4846 Input Parameter: 4847 + ts - the TS context 4848 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4849 manual pages for the individual convergence tests for complete lists 4850 4851 Level: advanced 4852 4853 Notes: 4854 Can only be called during TSSolve() is active. 4855 4856 .keywords: TS, nonlinear, set, convergence, test 4857 4858 .seealso: TSConvergedReason 4859 @*/ 4860 PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4861 { 4862 PetscFunctionBegin; 4863 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4864 ts->reason = reason; 4865 PetscFunctionReturn(0); 4866 } 4867 4868 #undef __FUNCT__ 4869 #define __FUNCT__ "TSGetSolveTime" 4870 /*@ 4871 TSGetSolveTime - Gets the time after a call to TSSolve() 4872 4873 Not Collective 4874 4875 Input Parameter: 4876 . ts - the TS context 4877 4878 Output Parameter: 4879 . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4880 4881 Level: beginner 4882 4883 Notes: 4884 Can only be called after the call to TSSolve() is complete. 4885 4886 .keywords: TS, nonlinear, set, convergence, test 4887 4888 .seealso: TSSetConvergenceTest(), TSConvergedReason 4889 @*/ 4890 PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4891 { 4892 PetscFunctionBegin; 4893 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4894 PetscValidPointer(ftime,2); 4895 *ftime = ts->solvetime; 4896 PetscFunctionReturn(0); 4897 } 4898 4899 #undef __FUNCT__ 4900 #define __FUNCT__ "TSGetTotalSteps" 4901 /*@ 4902 TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 4903 4904 Not Collective 4905 4906 Input Parameter: 4907 . ts - the TS context 4908 4909 Output Parameter: 4910 . steps - the number of steps 4911 4912 Level: beginner 4913 4914 Notes: 4915 Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 4916 4917 .keywords: TS, nonlinear, set, convergence, test 4918 4919 .seealso: TSSetConvergenceTest(), TSConvergedReason 4920 @*/ 4921 PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 4922 { 4923 PetscFunctionBegin; 4924 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4925 PetscValidPointer(steps,2); 4926 *steps = ts->total_steps; 4927 PetscFunctionReturn(0); 4928 } 4929 4930 #undef __FUNCT__ 4931 #define __FUNCT__ "TSGetSNESIterations" 4932 /*@ 4933 TSGetSNESIterations - Gets the total number of nonlinear iterations 4934 used by the time integrator. 4935 4936 Not Collective 4937 4938 Input Parameter: 4939 . ts - TS context 4940 4941 Output Parameter: 4942 . nits - number of nonlinear iterations 4943 4944 Notes: 4945 This counter is reset to zero for each successive call to TSSolve(). 4946 4947 Level: intermediate 4948 4949 .keywords: TS, get, number, nonlinear, iterations 4950 4951 .seealso: TSGetKSPIterations() 4952 @*/ 4953 PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 4954 { 4955 PetscFunctionBegin; 4956 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4957 PetscValidIntPointer(nits,2); 4958 *nits = ts->snes_its; 4959 PetscFunctionReturn(0); 4960 } 4961 4962 #undef __FUNCT__ 4963 #define __FUNCT__ "TSGetKSPIterations" 4964 /*@ 4965 TSGetKSPIterations - Gets the total number of linear iterations 4966 used by the time integrator. 4967 4968 Not Collective 4969 4970 Input Parameter: 4971 . ts - TS context 4972 4973 Output Parameter: 4974 . lits - number of linear iterations 4975 4976 Notes: 4977 This counter is reset to zero for each successive call to TSSolve(). 4978 4979 Level: intermediate 4980 4981 .keywords: TS, get, number, linear, iterations 4982 4983 .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 4984 @*/ 4985 PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 4986 { 4987 PetscFunctionBegin; 4988 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4989 PetscValidIntPointer(lits,2); 4990 *lits = ts->ksp_its; 4991 PetscFunctionReturn(0); 4992 } 4993 4994 #undef __FUNCT__ 4995 #define __FUNCT__ "TSGetStepRejections" 4996 /*@ 4997 TSGetStepRejections - Gets the total number of rejected steps. 4998 4999 Not Collective 5000 5001 Input Parameter: 5002 . ts - TS context 5003 5004 Output Parameter: 5005 . rejects - number of steps rejected 5006 5007 Notes: 5008 This counter is reset to zero for each successive call to TSSolve(). 5009 5010 Level: intermediate 5011 5012 .keywords: TS, get, number 5013 5014 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 5015 @*/ 5016 PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 5017 { 5018 PetscFunctionBegin; 5019 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5020 PetscValidIntPointer(rejects,2); 5021 *rejects = ts->reject; 5022 PetscFunctionReturn(0); 5023 } 5024 5025 #undef __FUNCT__ 5026 #define __FUNCT__ "TSGetSNESFailures" 5027 /*@ 5028 TSGetSNESFailures - Gets the total number of failed SNES solves 5029 5030 Not Collective 5031 5032 Input Parameter: 5033 . ts - TS context 5034 5035 Output Parameter: 5036 . fails - number of failed nonlinear solves 5037 5038 Notes: 5039 This counter is reset to zero for each successive call to TSSolve(). 5040 5041 Level: intermediate 5042 5043 .keywords: TS, get, number 5044 5045 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 5046 @*/ 5047 PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 5048 { 5049 PetscFunctionBegin; 5050 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5051 PetscValidIntPointer(fails,2); 5052 *fails = ts->num_snes_failures; 5053 PetscFunctionReturn(0); 5054 } 5055 5056 #undef __FUNCT__ 5057 #define __FUNCT__ "TSSetMaxStepRejections" 5058 /*@ 5059 TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 5060 5061 Not Collective 5062 5063 Input Parameter: 5064 + ts - TS context 5065 - rejects - maximum number of rejected steps, pass -1 for unlimited 5066 5067 Notes: 5068 The counter is reset to zero for each step 5069 5070 Options Database Key: 5071 . -ts_max_reject - Maximum number of step rejections before a step fails 5072 5073 Level: intermediate 5074 5075 .keywords: TS, set, maximum, number 5076 5077 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5078 @*/ 5079 PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 5080 { 5081 PetscFunctionBegin; 5082 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5083 ts->max_reject = rejects; 5084 PetscFunctionReturn(0); 5085 } 5086 5087 #undef __FUNCT__ 5088 #define __FUNCT__ "TSSetMaxSNESFailures" 5089 /*@ 5090 TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 5091 5092 Not Collective 5093 5094 Input Parameter: 5095 + ts - TS context 5096 - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 5097 5098 Notes: 5099 The counter is reset to zero for each successive call to TSSolve(). 5100 5101 Options Database Key: 5102 . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5103 5104 Level: intermediate 5105 5106 .keywords: TS, set, maximum, number 5107 5108 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 5109 @*/ 5110 PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 5111 { 5112 PetscFunctionBegin; 5113 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5114 ts->max_snes_failures = fails; 5115 PetscFunctionReturn(0); 5116 } 5117 5118 #undef __FUNCT__ 5119 #define __FUNCT__ "TSSetErrorIfStepFails" 5120 /*@ 5121 TSSetErrorIfStepFails - Error if no step succeeds 5122 5123 Not Collective 5124 5125 Input Parameter: 5126 + ts - TS context 5127 - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 5128 5129 Options Database Key: 5130 . -ts_error_if_step_fails - Error if no step succeeds 5131 5132 Level: intermediate 5133 5134 .keywords: TS, set, error 5135 5136 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 5137 @*/ 5138 PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 5139 { 5140 PetscFunctionBegin; 5141 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5142 ts->errorifstepfailed = err; 5143 PetscFunctionReturn(0); 5144 } 5145 5146 #undef __FUNCT__ 5147 #define __FUNCT__ "TSMonitorSolution" 5148 /*@C 5149 TSMonitorSolution - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file or a PetscDraw object 5150 5151 Collective on TS 5152 5153 Input Parameters: 5154 + ts - the TS context 5155 . step - current time-step 5156 . ptime - current time 5157 . u - current state 5158 - viewer - binary viewer 5159 5160 Level: intermediate 5161 5162 .keywords: TS, vector, monitor, view 5163 5164 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5165 @*/ 5166 PetscErrorCode TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 5167 { 5168 PetscErrorCode ierr; 5169 PetscViewer v = (PetscViewer)viewer; 5170 5171 PetscFunctionBegin; 5172 ierr = VecView(u,v);CHKERRQ(ierr); 5173 PetscFunctionReturn(0); 5174 } 5175 5176 #undef __FUNCT__ 5177 #define __FUNCT__ "TSMonitorSolutionVTK" 5178 /*@C 5179 TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 5180 5181 Collective on TS 5182 5183 Input Parameters: 5184 + ts - the TS context 5185 . step - current time-step 5186 . ptime - current time 5187 . u - current state 5188 - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5189 5190 Level: intermediate 5191 5192 Notes: 5193 The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step. 5194 These are named according to the file name template. 5195 5196 This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 5197 5198 .keywords: TS, vector, monitor, view 5199 5200 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5201 @*/ 5202 PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 5203 { 5204 PetscErrorCode ierr; 5205 char filename[PETSC_MAX_PATH_LEN]; 5206 PetscViewer viewer; 5207 5208 PetscFunctionBegin; 5209 ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 5210 ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 5211 ierr = VecView(u,viewer);CHKERRQ(ierr); 5212 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 5213 PetscFunctionReturn(0); 5214 } 5215 5216 #undef __FUNCT__ 5217 #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 5218 /*@C 5219 TSMonitorSolutionVTKDestroy - Destroy context for monitoring 5220 5221 Collective on TS 5222 5223 Input Parameters: 5224 . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 5225 5226 Level: intermediate 5227 5228 Note: 5229 This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5230 5231 .keywords: TS, vector, monitor, view 5232 5233 .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5234 @*/ 5235 PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5236 { 5237 PetscErrorCode ierr; 5238 5239 PetscFunctionBegin; 5240 ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5241 PetscFunctionReturn(0); 5242 } 5243 5244 #undef __FUNCT__ 5245 #define __FUNCT__ "TSGetAdapt" 5246 /*@ 5247 TSGetAdapt - Get the adaptive controller context for the current method 5248 5249 Collective on TS if controller has not been created yet 5250 5251 Input Arguments: 5252 . ts - time stepping context 5253 5254 Output Arguments: 5255 . adapt - adaptive controller 5256 5257 Level: intermediate 5258 5259 .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 5260 @*/ 5261 PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 5262 { 5263 PetscErrorCode ierr; 5264 5265 PetscFunctionBegin; 5266 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5267 if (adapt) PetscValidPointer(adapt,2); 5268 if (!ts->adapt) { 5269 ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 5270 ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 5271 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 5272 } 5273 if (adapt) *adapt = ts->adapt; 5274 PetscFunctionReturn(0); 5275 } 5276 5277 #undef __FUNCT__ 5278 #define __FUNCT__ "TSSetTolerances" 5279 /*@ 5280 TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 5281 5282 Logically Collective 5283 5284 Input Arguments: 5285 + ts - time integration context 5286 . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 5287 . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 5288 . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 5289 - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 5290 5291 Options Database keys: 5292 + -ts_rtol <rtol> - relative tolerance for local truncation error 5293 - -ts_atol <atol> Absolute tolerance for local truncation error 5294 5295 Notes: 5296 With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 5297 (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 5298 computed only for the differential or the algebraic part then this can be done using the vector of 5299 tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 5300 differential part and infinity for the algebraic part, the LTE calculation will include only the 5301 differential variables. 5302 5303 Level: beginner 5304 5305 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 5306 @*/ 5307 PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 5308 { 5309 PetscErrorCode ierr; 5310 5311 PetscFunctionBegin; 5312 if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 5313 if (vatol) { 5314 ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 5315 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5316 5317 ts->vatol = vatol; 5318 } 5319 if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 5320 if (vrtol) { 5321 ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 5322 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5323 5324 ts->vrtol = vrtol; 5325 } 5326 PetscFunctionReturn(0); 5327 } 5328 5329 #undef __FUNCT__ 5330 #define __FUNCT__ "TSGetTolerances" 5331 /*@ 5332 TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5333 5334 Logically Collective 5335 5336 Input Arguments: 5337 . ts - time integration context 5338 5339 Output Arguments: 5340 + atol - scalar absolute tolerances, NULL to ignore 5341 . vatol - vector of absolute tolerances, NULL to ignore 5342 . rtol - scalar relative tolerances, NULL to ignore 5343 - vrtol - vector of relative tolerances, NULL to ignore 5344 5345 Level: beginner 5346 5347 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5348 @*/ 5349 PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5350 { 5351 PetscFunctionBegin; 5352 if (atol) *atol = ts->atol; 5353 if (vatol) *vatol = ts->vatol; 5354 if (rtol) *rtol = ts->rtol; 5355 if (vrtol) *vrtol = ts->vrtol; 5356 PetscFunctionReturn(0); 5357 } 5358 5359 #undef __FUNCT__ 5360 #define __FUNCT__ "TSErrorWeightedNorm2" 5361 /*@ 5362 TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 5363 5364 Collective on TS 5365 5366 Input Arguments: 5367 + ts - time stepping context 5368 . U - state vector, usually ts->vec_sol 5369 - Y - state vector to be compared to U 5370 5371 Output Arguments: 5372 . norm - weighted norm, a value of 1.0 is considered small 5373 5374 Level: developer 5375 5376 .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 5377 @*/ 5378 PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 5379 { 5380 PetscErrorCode ierr; 5381 PetscInt i,n,N,rstart; 5382 const PetscScalar *u,*y; 5383 PetscReal sum,gsum; 5384 PetscReal tol; 5385 5386 PetscFunctionBegin; 5387 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5388 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5389 PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5390 PetscValidType(U,2); 5391 PetscValidType(Y,3); 5392 PetscCheckSameComm(U,2,Y,3); 5393 PetscValidPointer(norm,4); 5394 if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 5395 5396 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 5397 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 5398 ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 5399 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 5400 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 5401 sum = 0.; 5402 if (ts->vatol && ts->vrtol) { 5403 const PetscScalar *atol,*rtol; 5404 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5405 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5406 for (i=0; i<n; i++) { 5407 tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5408 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5409 } 5410 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5411 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5412 } else if (ts->vatol) { /* vector atol, scalar rtol */ 5413 const PetscScalar *atol; 5414 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5415 for (i=0; i<n; i++) { 5416 tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5417 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5418 } 5419 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5420 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 5421 const PetscScalar *rtol; 5422 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5423 for (i=0; i<n; i++) { 5424 tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5425 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5426 } 5427 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5428 } else { /* scalar atol, scalar rtol */ 5429 for (i=0; i<n; i++) { 5430 tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5431 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 5432 } 5433 } 5434 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 5435 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 5436 5437 ierr = MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 5438 *norm = PetscSqrtReal(gsum / N); 5439 5440 if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 5441 PetscFunctionReturn(0); 5442 } 5443 5444 #undef __FUNCT__ 5445 #define __FUNCT__ "TSErrorWeightedNormInfinity" 5446 /*@ 5447 TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 5448 5449 Collective on TS 5450 5451 Input Arguments: 5452 + ts - time stepping context 5453 . U - state vector, usually ts->vec_sol 5454 - Y - state vector to be compared to U 5455 5456 Output Arguments: 5457 . norm - weighted norm, a value of 1.0 is considered small 5458 5459 Level: developer 5460 5461 .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 5462 @*/ 5463 PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 5464 { 5465 PetscErrorCode ierr; 5466 PetscInt i,n,N,rstart,k; 5467 const PetscScalar *u,*y; 5468 PetscReal max,gmax; 5469 PetscReal tol; 5470 5471 PetscFunctionBegin; 5472 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5473 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5474 PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5475 PetscValidType(U,2); 5476 PetscValidType(Y,3); 5477 PetscCheckSameComm(U,2,Y,3); 5478 PetscValidPointer(norm,4); 5479 if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 5480 5481 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 5482 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 5483 ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 5484 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 5485 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 5486 if (ts->vatol && ts->vrtol) { 5487 const PetscScalar *atol,*rtol; 5488 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5489 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5490 k = 0; 5491 tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 5492 max = PetscAbsScalar(y[k] - u[k]) / tol; 5493 for (i=1; i<n; i++) { 5494 tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5495 max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 5496 } 5497 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5498 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5499 } else if (ts->vatol) { /* vector atol, scalar rtol */ 5500 const PetscScalar *atol; 5501 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5502 k = 0; 5503 tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 5504 max = PetscAbsScalar(y[k] - u[k]) / tol; 5505 for (i=1; i<n; i++) { 5506 tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5507 max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 5508 } 5509 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 5510 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 5511 const PetscScalar *rtol; 5512 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5513 k = 0; 5514 tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 5515 max = PetscAbsScalar(y[k] - u[k]) / tol; 5516 for (i=1; i<n; i++) { 5517 tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5518 max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 5519 } 5520 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 5521 } else { /* scalar atol, scalar rtol */ 5522 k = 0; 5523 tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 5524 max = PetscAbsScalar(y[k] - u[k]) / tol; 5525 for (i=1; i<n; i++) { 5526 tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 5527 max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 5528 } 5529 } 5530 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 5531 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 5532 5533 ierr = MPIU_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 5534 *norm = gmax; 5535 5536 if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 5537 PetscFunctionReturn(0); 5538 } 5539 5540 #undef __FUNCT__ 5541 #define __FUNCT__ "TSErrorWeightedNorm" 5542 /*@ 5543 TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 5544 5545 Collective on TS 5546 5547 Input Arguments: 5548 + ts - time stepping context 5549 . U - state vector, usually ts->vec_sol 5550 . Y - state vector to be compared to U 5551 - wnormtype - norm type, either NORM_2 or NORM_INFINITY 5552 5553 Output Arguments: 5554 . norm - weighted norm, a value of 1.0 is considered small 5555 5556 5557 Options Database Keys: 5558 . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5559 5560 Level: developer 5561 5562 .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 5563 @*/ 5564 PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 5565 { 5566 PetscErrorCode ierr; 5567 5568 PetscFunctionBegin; 5569 if (wnormtype == NORM_2) { 5570 ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5571 } else if(wnormtype == NORM_INFINITY) { 5572 ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5573 } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 5574 PetscFunctionReturn(0); 5575 } 5576 5577 #undef __FUNCT__ 5578 #define __FUNCT__ "TSSetCFLTimeLocal" 5579 /*@ 5580 TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 5581 5582 Logically Collective on TS 5583 5584 Input Arguments: 5585 + ts - time stepping context 5586 - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 5587 5588 Note: 5589 After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 5590 5591 Level: intermediate 5592 5593 .seealso: TSGetCFLTime(), TSADAPTCFL 5594 @*/ 5595 PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 5596 { 5597 PetscFunctionBegin; 5598 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5599 ts->cfltime_local = cfltime; 5600 ts->cfltime = -1.; 5601 PetscFunctionReturn(0); 5602 } 5603 5604 #undef __FUNCT__ 5605 #define __FUNCT__ "TSGetCFLTime" 5606 /*@ 5607 TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 5608 5609 Collective on TS 5610 5611 Input Arguments: 5612 . ts - time stepping context 5613 5614 Output Arguments: 5615 . cfltime - maximum stable time step for forward Euler 5616 5617 Level: advanced 5618 5619 .seealso: TSSetCFLTimeLocal() 5620 @*/ 5621 PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 5622 { 5623 PetscErrorCode ierr; 5624 5625 PetscFunctionBegin; 5626 if (ts->cfltime < 0) { 5627 ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 5628 } 5629 *cfltime = ts->cfltime; 5630 PetscFunctionReturn(0); 5631 } 5632 5633 #undef __FUNCT__ 5634 #define __FUNCT__ "TSVISetVariableBounds" 5635 /*@ 5636 TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5637 5638 Input Parameters: 5639 . ts - the TS context. 5640 . xl - lower bound. 5641 . xu - upper bound. 5642 5643 Notes: 5644 If this routine is not called then the lower and upper bounds are set to 5645 PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5646 5647 Level: advanced 5648 5649 @*/ 5650 PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5651 { 5652 PetscErrorCode ierr; 5653 SNES snes; 5654 5655 PetscFunctionBegin; 5656 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5657 ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5658 PetscFunctionReturn(0); 5659 } 5660 5661 #if defined(PETSC_HAVE_MATLAB_ENGINE) 5662 #include <mex.h> 5663 5664 typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5665 5666 #undef __FUNCT__ 5667 #define __FUNCT__ "TSComputeFunction_Matlab" 5668 /* 5669 TSComputeFunction_Matlab - Calls the function that has been set with 5670 TSSetFunctionMatlab(). 5671 5672 Collective on TS 5673 5674 Input Parameters: 5675 + snes - the TS context 5676 - u - input vector 5677 5678 Output Parameter: 5679 . y - function vector, as set by TSSetFunction() 5680 5681 Notes: 5682 TSComputeFunction() is typically used within nonlinear solvers 5683 implementations, so most users would not generally call this routine 5684 themselves. 5685 5686 Level: developer 5687 5688 .keywords: TS, nonlinear, compute, function 5689 5690 .seealso: TSSetFunction(), TSGetFunction() 5691 */ 5692 PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5693 { 5694 PetscErrorCode ierr; 5695 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5696 int nlhs = 1,nrhs = 7; 5697 mxArray *plhs[1],*prhs[7]; 5698 long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5699 5700 PetscFunctionBegin; 5701 PetscValidHeaderSpecific(snes,TS_CLASSID,1); 5702 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5703 PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5704 PetscValidHeaderSpecific(y,VEC_CLASSID,5); 5705 PetscCheckSameComm(snes,1,u,3); 5706 PetscCheckSameComm(snes,1,y,5); 5707 5708 ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 5709 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5710 ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 5711 ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5712 5713 prhs[0] = mxCreateDoubleScalar((double)ls); 5714 prhs[1] = mxCreateDoubleScalar(time); 5715 prhs[2] = mxCreateDoubleScalar((double)lx); 5716 prhs[3] = mxCreateDoubleScalar((double)lxdot); 5717 prhs[4] = mxCreateDoubleScalar((double)ly); 5718 prhs[5] = mxCreateString(sctx->funcname); 5719 prhs[6] = sctx->ctx; 5720 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5721 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5722 mxDestroyArray(prhs[0]); 5723 mxDestroyArray(prhs[1]); 5724 mxDestroyArray(prhs[2]); 5725 mxDestroyArray(prhs[3]); 5726 mxDestroyArray(prhs[4]); 5727 mxDestroyArray(prhs[5]); 5728 mxDestroyArray(plhs[0]); 5729 PetscFunctionReturn(0); 5730 } 5731 5732 5733 #undef __FUNCT__ 5734 #define __FUNCT__ "TSSetFunctionMatlab" 5735 /* 5736 TSSetFunctionMatlab - Sets the function evaluation routine and function 5737 vector for use by the TS routines in solving ODEs 5738 equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5739 5740 Logically Collective on TS 5741 5742 Input Parameters: 5743 + ts - the TS context 5744 - func - function evaluation routine 5745 5746 Calling sequence of func: 5747 $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5748 5749 Level: beginner 5750 5751 .keywords: TS, nonlinear, set, function 5752 5753 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5754 */ 5755 PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5756 { 5757 PetscErrorCode ierr; 5758 TSMatlabContext *sctx; 5759 5760 PetscFunctionBegin; 5761 /* currently sctx is memory bleed */ 5762 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5763 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5764 /* 5765 This should work, but it doesn't 5766 sctx->ctx = ctx; 5767 mexMakeArrayPersistent(sctx->ctx); 5768 */ 5769 sctx->ctx = mxDuplicateArray(ctx); 5770 5771 ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5772 PetscFunctionReturn(0); 5773 } 5774 5775 #undef __FUNCT__ 5776 #define __FUNCT__ "TSComputeJacobian_Matlab" 5777 /* 5778 TSComputeJacobian_Matlab - Calls the function that has been set with 5779 TSSetJacobianMatlab(). 5780 5781 Collective on TS 5782 5783 Input Parameters: 5784 + ts - the TS context 5785 . u - input vector 5786 . A, B - the matrices 5787 - ctx - user context 5788 5789 Level: developer 5790 5791 .keywords: TS, nonlinear, compute, function 5792 5793 .seealso: TSSetFunction(), TSGetFunction() 5794 @*/ 5795 PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5796 { 5797 PetscErrorCode ierr; 5798 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5799 int nlhs = 2,nrhs = 9; 5800 mxArray *plhs[2],*prhs[9]; 5801 long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5802 5803 PetscFunctionBegin; 5804 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5805 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5806 5807 /* call Matlab function in ctx with arguments u and y */ 5808 5809 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 5810 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5811 ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 5812 ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 5813 ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5814 5815 prhs[0] = mxCreateDoubleScalar((double)ls); 5816 prhs[1] = mxCreateDoubleScalar((double)time); 5817 prhs[2] = mxCreateDoubleScalar((double)lx); 5818 prhs[3] = mxCreateDoubleScalar((double)lxdot); 5819 prhs[4] = mxCreateDoubleScalar((double)shift); 5820 prhs[5] = mxCreateDoubleScalar((double)lA); 5821 prhs[6] = mxCreateDoubleScalar((double)lB); 5822 prhs[7] = mxCreateString(sctx->funcname); 5823 prhs[8] = sctx->ctx; 5824 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5825 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5826 mxDestroyArray(prhs[0]); 5827 mxDestroyArray(prhs[1]); 5828 mxDestroyArray(prhs[2]); 5829 mxDestroyArray(prhs[3]); 5830 mxDestroyArray(prhs[4]); 5831 mxDestroyArray(prhs[5]); 5832 mxDestroyArray(prhs[6]); 5833 mxDestroyArray(prhs[7]); 5834 mxDestroyArray(plhs[0]); 5835 mxDestroyArray(plhs[1]); 5836 PetscFunctionReturn(0); 5837 } 5838 5839 5840 #undef __FUNCT__ 5841 #define __FUNCT__ "TSSetJacobianMatlab" 5842 /* 5843 TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5844 vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function 5845 5846 Logically Collective on TS 5847 5848 Input Parameters: 5849 + ts - the TS context 5850 . A,B - Jacobian matrices 5851 . func - function evaluation routine 5852 - ctx - user context 5853 5854 Calling sequence of func: 5855 $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5856 5857 5858 Level: developer 5859 5860 .keywords: TS, nonlinear, set, function 5861 5862 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5863 */ 5864 PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5865 { 5866 PetscErrorCode ierr; 5867 TSMatlabContext *sctx; 5868 5869 PetscFunctionBegin; 5870 /* currently sctx is memory bleed */ 5871 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5872 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5873 /* 5874 This should work, but it doesn't 5875 sctx->ctx = ctx; 5876 mexMakeArrayPersistent(sctx->ctx); 5877 */ 5878 sctx->ctx = mxDuplicateArray(ctx); 5879 5880 ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5881 PetscFunctionReturn(0); 5882 } 5883 5884 #undef __FUNCT__ 5885 #define __FUNCT__ "TSMonitor_Matlab" 5886 /* 5887 TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5888 5889 Collective on TS 5890 5891 .seealso: TSSetFunction(), TSGetFunction() 5892 @*/ 5893 PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5894 { 5895 PetscErrorCode ierr; 5896 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5897 int nlhs = 1,nrhs = 6; 5898 mxArray *plhs[1],*prhs[6]; 5899 long long int lx = 0,ls = 0; 5900 5901 PetscFunctionBegin; 5902 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5903 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5904 5905 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 5906 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5907 5908 prhs[0] = mxCreateDoubleScalar((double)ls); 5909 prhs[1] = mxCreateDoubleScalar((double)it); 5910 prhs[2] = mxCreateDoubleScalar((double)time); 5911 prhs[3] = mxCreateDoubleScalar((double)lx); 5912 prhs[4] = mxCreateString(sctx->funcname); 5913 prhs[5] = sctx->ctx; 5914 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5915 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5916 mxDestroyArray(prhs[0]); 5917 mxDestroyArray(prhs[1]); 5918 mxDestroyArray(prhs[2]); 5919 mxDestroyArray(prhs[3]); 5920 mxDestroyArray(prhs[4]); 5921 mxDestroyArray(plhs[0]); 5922 PetscFunctionReturn(0); 5923 } 5924 5925 5926 #undef __FUNCT__ 5927 #define __FUNCT__ "TSMonitorSetMatlab" 5928 /* 5929 TSMonitorSetMatlab - Sets the monitor function from Matlab 5930 5931 Level: developer 5932 5933 .keywords: TS, nonlinear, set, function 5934 5935 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5936 */ 5937 PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5938 { 5939 PetscErrorCode ierr; 5940 TSMatlabContext *sctx; 5941 5942 PetscFunctionBegin; 5943 /* currently sctx is memory bleed */ 5944 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5945 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5946 /* 5947 This should work, but it doesn't 5948 sctx->ctx = ctx; 5949 mexMakeArrayPersistent(sctx->ctx); 5950 */ 5951 sctx->ctx = mxDuplicateArray(ctx); 5952 5953 ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5954 PetscFunctionReturn(0); 5955 } 5956 #endif 5957 5958 #undef __FUNCT__ 5959 #define __FUNCT__ "TSMonitorLGSolution" 5960 /*@C 5961 TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5962 in a time based line graph 5963 5964 Collective on TS 5965 5966 Input Parameters: 5967 + ts - the TS context 5968 . step - current time-step 5969 . ptime - current time 5970 . u - current solution 5971 - dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate() 5972 5973 Options Database: 5974 . -ts_monitor_lg_solution_variables 5975 5976 Level: intermediate 5977 5978 Notes: each process in a parallel run displays its component solutions in a separate window 5979 5980 .keywords: TS, vector, monitor, view 5981 5982 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(), 5983 TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(), 5984 TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(), 5985 TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop() 5986 @*/ 5987 PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 5988 { 5989 PetscErrorCode ierr; 5990 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dctx; 5991 const PetscScalar *yy; 5992 PetscInt dim; 5993 Vec v; 5994 5995 PetscFunctionBegin; 5996 if (!step) { 5997 PetscDrawAxis axis; 5998 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5999 ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 6000 if (ctx->names && !ctx->displaynames) { 6001 char **displaynames; 6002 PetscBool flg; 6003 6004 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6005 ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 6006 ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 6007 ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 6008 if (flg) { 6009 ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 6010 } 6011 ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 6012 } 6013 if (ctx->displaynames) { 6014 ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 6015 ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 6016 } else if (ctx->names) { 6017 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6018 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6019 ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 6020 } else { 6021 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6022 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6023 } 6024 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6025 } 6026 if (ctx->transform) { 6027 ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 6028 } else { 6029 v = u; 6030 } 6031 ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 6032 #if defined(PETSC_USE_COMPLEX) 6033 { 6034 PetscReal *yreal; 6035 PetscInt i,n; 6036 ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 6037 ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6038 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 6039 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6040 ierr = PetscFree(yreal);CHKERRQ(ierr); 6041 } 6042 #else 6043 if (ctx->displaynames) { 6044 PetscInt i; 6045 for (i=0; i<ctx->ndisplayvariables; i++) { 6046 ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 6047 } 6048 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 6049 } else { 6050 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6051 } 6052 #endif 6053 ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 6054 if (ctx->transform) { 6055 ierr = VecDestroy(&v);CHKERRQ(ierr); 6056 } 6057 if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 6058 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6059 } 6060 PetscFunctionReturn(0); 6061 } 6062 6063 6064 #undef __FUNCT__ 6065 #define __FUNCT__ "TSMonitorLGSetVariableNames" 6066 /*@C 6067 TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6068 6069 Collective on TS 6070 6071 Input Parameters: 6072 + ts - the TS context 6073 - names - the names of the components, final string must be NULL 6074 6075 Level: intermediate 6076 6077 Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 6078 6079 .keywords: TS, vector, monitor, view 6080 6081 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 6082 @*/ 6083 PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 6084 { 6085 PetscErrorCode ierr; 6086 PetscInt i; 6087 6088 PetscFunctionBegin; 6089 for (i=0; i<ts->numbermonitors; i++) { 6090 if (ts->monitor[i] == TSMonitorLGSolution) { 6091 ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 6092 break; 6093 } 6094 } 6095 PetscFunctionReturn(0); 6096 } 6097 6098 #undef __FUNCT__ 6099 #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 6100 /*@C 6101 TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 6102 6103 Collective on TS 6104 6105 Input Parameters: 6106 + ts - the TS context 6107 - names - the names of the components, final string must be NULL 6108 6109 Level: intermediate 6110 6111 .keywords: TS, vector, monitor, view 6112 6113 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 6114 @*/ 6115 PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 6116 { 6117 PetscErrorCode ierr; 6118 6119 PetscFunctionBegin; 6120 ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 6121 ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 6122 PetscFunctionReturn(0); 6123 } 6124 6125 #undef __FUNCT__ 6126 #define __FUNCT__ "TSMonitorLGGetVariableNames" 6127 /*@C 6128 TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 6129 6130 Collective on TS 6131 6132 Input Parameter: 6133 . ts - the TS context 6134 6135 Output Parameter: 6136 . names - the names of the components, final string must be NULL 6137 6138 Level: intermediate 6139 6140 Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 6141 6142 .keywords: TS, vector, monitor, view 6143 6144 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6145 @*/ 6146 PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 6147 { 6148 PetscInt i; 6149 6150 PetscFunctionBegin; 6151 *names = NULL; 6152 for (i=0; i<ts->numbermonitors; i++) { 6153 if (ts->monitor[i] == TSMonitorLGSolution) { 6154 TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 6155 *names = (const char *const *)ctx->names; 6156 break; 6157 } 6158 } 6159 PetscFunctionReturn(0); 6160 } 6161 6162 #undef __FUNCT__ 6163 #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 6164 /*@C 6165 TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 6166 6167 Collective on TS 6168 6169 Input Parameters: 6170 + ctx - the TSMonitorLG context 6171 . displaynames - the names of the components, final string must be NULL 6172 6173 Level: intermediate 6174 6175 .keywords: TS, vector, monitor, view 6176 6177 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6178 @*/ 6179 PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 6180 { 6181 PetscInt j = 0,k; 6182 PetscErrorCode ierr; 6183 6184 PetscFunctionBegin; 6185 if (!ctx->names) PetscFunctionReturn(0); 6186 ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 6187 ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 6188 while (displaynames[j]) j++; 6189 ctx->ndisplayvariables = j; 6190 ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 6191 ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 6192 j = 0; 6193 while (displaynames[j]) { 6194 k = 0; 6195 while (ctx->names[k]) { 6196 PetscBool flg; 6197 ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 6198 if (flg) { 6199 ctx->displayvariables[j] = k; 6200 break; 6201 } 6202 k++; 6203 } 6204 j++; 6205 } 6206 PetscFunctionReturn(0); 6207 } 6208 6209 6210 #undef __FUNCT__ 6211 #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 6212 /*@C 6213 TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 6214 6215 Collective on TS 6216 6217 Input Parameters: 6218 + ts - the TS context 6219 . displaynames - the names of the components, final string must be NULL 6220 6221 Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 6222 6223 Level: intermediate 6224 6225 .keywords: TS, vector, monitor, view 6226 6227 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 6228 @*/ 6229 PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 6230 { 6231 PetscInt i; 6232 PetscErrorCode ierr; 6233 6234 PetscFunctionBegin; 6235 for (i=0; i<ts->numbermonitors; i++) { 6236 if (ts->monitor[i] == TSMonitorLGSolution) { 6237 ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 6238 break; 6239 } 6240 } 6241 PetscFunctionReturn(0); 6242 } 6243 6244 #undef __FUNCT__ 6245 #define __FUNCT__ "TSMonitorLGSetTransform" 6246 /*@C 6247 TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 6248 6249 Collective on TS 6250 6251 Input Parameters: 6252 + ts - the TS context 6253 . transform - the transform function 6254 . destroy - function to destroy the optional context 6255 - ctx - optional context used by transform function 6256 6257 Notes: If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored 6258 6259 Level: intermediate 6260 6261 .keywords: TS, vector, monitor, view 6262 6263 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 6264 @*/ 6265 PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6266 { 6267 PetscInt i; 6268 PetscErrorCode ierr; 6269 6270 PetscFunctionBegin; 6271 for (i=0; i<ts->numbermonitors; i++) { 6272 if (ts->monitor[i] == TSMonitorLGSolution) { 6273 ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 6274 } 6275 } 6276 PetscFunctionReturn(0); 6277 } 6278 6279 #undef __FUNCT__ 6280 #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6281 /*@C 6282 TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6283 6284 Collective on TSLGCtx 6285 6286 Input Parameters: 6287 + ts - the TS context 6288 . transform - the transform function 6289 . destroy - function to destroy the optional context 6290 - ctx - optional context used by transform function 6291 6292 Level: intermediate 6293 6294 .keywords: TS, vector, monitor, view 6295 6296 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6297 @*/ 6298 PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6299 { 6300 PetscFunctionBegin; 6301 ctx->transform = transform; 6302 ctx->transformdestroy = destroy; 6303 ctx->transformctx = tctx; 6304 PetscFunctionReturn(0); 6305 } 6306 6307 #undef __FUNCT__ 6308 #define __FUNCT__ "TSMonitorLGError" 6309 /*@C 6310 TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6311 in a time based line graph 6312 6313 Collective on TS 6314 6315 Input Parameters: 6316 + ts - the TS context 6317 . step - current time-step 6318 . ptime - current time 6319 . u - current solution 6320 - dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate() 6321 6322 Level: intermediate 6323 6324 Notes: 6325 Only for sequential solves. 6326 6327 The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6328 6329 Options Database Keys: 6330 . -ts_monitor_lg_error - create a graphical monitor of error history 6331 6332 .keywords: TS, vector, monitor, view 6333 6334 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6335 @*/ 6336 PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6337 { 6338 PetscErrorCode ierr; 6339 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6340 const PetscScalar *yy; 6341 Vec y; 6342 PetscInt dim; 6343 6344 PetscFunctionBegin; 6345 if (!step) { 6346 PetscDrawAxis axis; 6347 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6348 ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 6349 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6350 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6351 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6352 } 6353 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6354 ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 6355 ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6356 ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6357 #if defined(PETSC_USE_COMPLEX) 6358 { 6359 PetscReal *yreal; 6360 PetscInt i,n; 6361 ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6362 ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6363 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 6364 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6365 ierr = PetscFree(yreal);CHKERRQ(ierr); 6366 } 6367 #else 6368 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6369 #endif 6370 ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6371 ierr = VecDestroy(&y);CHKERRQ(ierr); 6372 if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 6373 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6374 } 6375 PetscFunctionReturn(0); 6376 } 6377 6378 #undef __FUNCT__ 6379 #define __FUNCT__ "TSMonitorLGSNESIterations" 6380 PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6381 { 6382 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6383 PetscReal x = ptime,y; 6384 PetscErrorCode ierr; 6385 PetscInt its; 6386 6387 PetscFunctionBegin; 6388 if (!n) { 6389 PetscDrawAxis axis; 6390 6391 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6392 ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6393 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6394 6395 ctx->snes_its = 0; 6396 } 6397 ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6398 y = its - ctx->snes_its; 6399 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 6400 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6401 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6402 } 6403 ctx->snes_its = its; 6404 PetscFunctionReturn(0); 6405 } 6406 6407 #undef __FUNCT__ 6408 #define __FUNCT__ "TSMonitorLGKSPIterations" 6409 PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6410 { 6411 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6412 PetscReal x = ptime,y; 6413 PetscErrorCode ierr; 6414 PetscInt its; 6415 6416 PetscFunctionBegin; 6417 if (!n) { 6418 PetscDrawAxis axis; 6419 6420 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6421 ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6422 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6423 6424 ctx->ksp_its = 0; 6425 } 6426 ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6427 y = its - ctx->ksp_its; 6428 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 6429 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6430 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6431 } 6432 ctx->ksp_its = its; 6433 PetscFunctionReturn(0); 6434 } 6435 6436 #undef __FUNCT__ 6437 #define __FUNCT__ "TSComputeLinearStability" 6438 /*@ 6439 TSComputeLinearStability - computes the linear stability function at a point 6440 6441 Collective on TS and Vec 6442 6443 Input Parameters: 6444 + ts - the TS context 6445 - xr,xi - real and imaginary part of input arguments 6446 6447 Output Parameters: 6448 . yr,yi - real and imaginary part of function value 6449 6450 Level: developer 6451 6452 .keywords: TS, compute 6453 6454 .seealso: TSSetRHSFunction(), TSComputeIFunction() 6455 @*/ 6456 PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6457 { 6458 PetscErrorCode ierr; 6459 6460 PetscFunctionBegin; 6461 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6462 if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6463 ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6464 PetscFunctionReturn(0); 6465 } 6466 6467 /* ------------------------------------------------------------------------*/ 6468 #undef __FUNCT__ 6469 #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6470 /*@C 6471 TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6472 6473 Collective on TS 6474 6475 Input Parameters: 6476 . ts - the ODE solver object 6477 6478 Output Parameter: 6479 . ctx - the context 6480 6481 Level: intermediate 6482 6483 .keywords: TS, monitor, line graph, residual, seealso 6484 6485 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6486 6487 @*/ 6488 PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6489 { 6490 PetscErrorCode ierr; 6491 6492 PetscFunctionBegin; 6493 ierr = PetscNew(ctx);CHKERRQ(ierr); 6494 PetscFunctionReturn(0); 6495 } 6496 6497 #undef __FUNCT__ 6498 #define __FUNCT__ "TSMonitorEnvelope" 6499 /*@C 6500 TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6501 6502 Collective on TS 6503 6504 Input Parameters: 6505 + ts - the TS context 6506 . step - current time-step 6507 . ptime - current time 6508 . u - current solution 6509 - dctx - the envelope context 6510 6511 Options Database: 6512 . -ts_monitor_envelope 6513 6514 Level: intermediate 6515 6516 Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6517 6518 .keywords: TS, vector, monitor, view 6519 6520 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate() 6521 @*/ 6522 PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx) 6523 { 6524 PetscErrorCode ierr; 6525 TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx; 6526 6527 PetscFunctionBegin; 6528 if (!ctx->max) { 6529 ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6530 ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6531 ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6532 ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6533 } else { 6534 ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6535 ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6536 } 6537 PetscFunctionReturn(0); 6538 } 6539 6540 6541 #undef __FUNCT__ 6542 #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6543 /*@C 6544 TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6545 6546 Collective on TS 6547 6548 Input Parameter: 6549 . ts - the TS context 6550 6551 Output Parameter: 6552 + max - the maximum values 6553 - min - the minimum values 6554 6555 Notes: If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored 6556 6557 Level: intermediate 6558 6559 .keywords: TS, vector, monitor, view 6560 6561 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6562 @*/ 6563 PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6564 { 6565 PetscInt i; 6566 6567 PetscFunctionBegin; 6568 if (max) *max = NULL; 6569 if (min) *min = NULL; 6570 for (i=0; i<ts->numbermonitors; i++) { 6571 if (ts->monitor[i] == TSMonitorEnvelope) { 6572 TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6573 if (max) *max = ctx->max; 6574 if (min) *min = ctx->min; 6575 break; 6576 } 6577 } 6578 PetscFunctionReturn(0); 6579 } 6580 6581 #undef __FUNCT__ 6582 #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6583 /*@C 6584 TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6585 6586 Collective on TSMonitorEnvelopeCtx 6587 6588 Input Parameter: 6589 . ctx - the monitor context 6590 6591 Level: intermediate 6592 6593 .keywords: TS, monitor, line graph, destroy 6594 6595 .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep() 6596 @*/ 6597 PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6598 { 6599 PetscErrorCode ierr; 6600 6601 PetscFunctionBegin; 6602 ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6603 ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6604 ierr = PetscFree(*ctx);CHKERRQ(ierr); 6605 PetscFunctionReturn(0); 6606 } 6607 6608 #undef __FUNCT__ 6609 #define __FUNCT__ "TSRollBack" 6610 /*@ 6611 TSRollBack - Rolls back one time step 6612 6613 Collective on TS 6614 6615 Input Parameter: 6616 . ts - the TS context obtained from TSCreate() 6617 6618 Level: advanced 6619 6620 .keywords: TS, timestep, rollback 6621 6622 .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 6623 @*/ 6624 PetscErrorCode TSRollBack(TS ts) 6625 { 6626 PetscErrorCode ierr; 6627 6628 PetscFunctionBegin; 6629 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6630 6631 if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 6632 ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 6633 ts->time_step = ts->ptime - ts->ptime_prev; 6634 ts->ptime = ts->ptime_prev; 6635 ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 6636 PetscFunctionReturn(0); 6637 } 6638 6639 #undef __FUNCT__ 6640 #define __FUNCT__ "TSGetStages" 6641 /*@ 6642 TSGetStages - Get the number of stages and stage values 6643 6644 Input Parameter: 6645 . ts - the TS context obtained from TSCreate() 6646 6647 Level: advanced 6648 6649 .keywords: TS, getstages 6650 6651 .seealso: TSCreate() 6652 @*/ 6653 PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6654 { 6655 PetscErrorCode ierr; 6656 6657 PetscFunctionBegin; 6658 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6659 PetscValidPointer(ns,2); 6660 6661 if (!ts->ops->getstages) *ns=0; 6662 else { 6663 ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6664 } 6665 PetscFunctionReturn(0); 6666 } 6667 6668 #undef __FUNCT__ 6669 #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6670 /*@C 6671 TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6672 6673 Collective on SNES 6674 6675 Input Parameters: 6676 + ts - the TS context 6677 . t - current timestep 6678 . U - state vector 6679 . Udot - time derivative of state vector 6680 . shift - shift to apply, see note below 6681 - ctx - an optional user context 6682 6683 Output Parameters: 6684 + J - Jacobian matrix (not altered in this routine) 6685 - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6686 6687 Level: intermediate 6688 6689 Notes: 6690 If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6691 6692 dF/dU + shift*dF/dUdot 6693 6694 Most users should not need to explicitly call this routine, as it 6695 is used internally within the nonlinear solvers. 6696 6697 This will first try to get the coloring from the DM. If the DM type has no coloring 6698 routine, then it will try to get the coloring from the matrix. This requires that the 6699 matrix have nonzero entries precomputed. 6700 6701 .keywords: TS, finite differences, Jacobian, coloring, sparse 6702 .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6703 @*/ 6704 PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6705 { 6706 SNES snes; 6707 MatFDColoring color; 6708 PetscBool hascolor, matcolor = PETSC_FALSE; 6709 PetscErrorCode ierr; 6710 6711 PetscFunctionBegin; 6712 ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6713 ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6714 if (!color) { 6715 DM dm; 6716 ISColoring iscoloring; 6717 6718 ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6719 ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6720 if (hascolor && !matcolor) { 6721 ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6722 ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6723 ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6724 ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6725 ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6726 ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6727 } else { 6728 MatColoring mc; 6729 6730 ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6731 ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6732 ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6733 ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6734 ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6735 ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6736 ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6737 ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6738 ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6739 ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6740 ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6741 } 6742 ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6743 ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6744 } 6745 ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6746 ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6747 if (J != B) { 6748 ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6749 ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6750 } 6751 PetscFunctionReturn(0); 6752 } 6753 6754 #undef __FUNCT__ 6755 #define __FUNCT__ "TSSetFunctionDomainError" 6756 /*@ 6757 TSSetFunctionDomainError - Set the function testing if the current state vector is valid 6758 6759 Input Parameters: 6760 ts - the TS context 6761 func - function called within TSFunctionDomainError 6762 6763 Level: intermediate 6764 6765 .keywords: TS, state, domain 6766 .seealso: TSAdaptCheckStage(), TSFunctionDomainError() 6767 @*/ 6768 6769 PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 6770 { 6771 PetscFunctionBegin; 6772 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6773 ts->functiondomainerror = func; 6774 PetscFunctionReturn(0); 6775 } 6776 6777 #undef __FUNCT__ 6778 #define __FUNCT__ "TSFunctionDomainError" 6779 /*@ 6780 TSFunctionDomainError - Check if the current state is valid 6781 6782 Input Parameters: 6783 ts - the TS context 6784 stagetime - time of the simulation 6785 Y - state vector to check. 6786 6787 Output Parameter: 6788 accept - Set to PETSC_FALSE if the current state vector is valid. 6789 6790 Note: 6791 This function should be used to ensure the state is in a valid part of the space. 6792 For example, one can ensure here all values are positive. 6793 6794 Level: advanced 6795 @*/ 6796 PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6797 { 6798 PetscErrorCode ierr; 6799 6800 PetscFunctionBegin; 6801 6802 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6803 *accept = PETSC_TRUE; 6804 if (ts->functiondomainerror) { 6805 PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 6806 } 6807 PetscFunctionReturn(0); 6808 } 6809 6810 #undef __FUNCT__ 6811 #define __FUNCT__ "TSClone" 6812 /*@C 6813 TSClone - This function clones a time step object. 6814 6815 Collective on MPI_Comm 6816 6817 Input Parameter: 6818 . tsin - The input TS 6819 6820 Output Parameter: 6821 . tsout - The output TS (cloned) 6822 6823 Notes: 6824 This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly. 6825 6826 When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup); 6827 6828 Level: developer 6829 6830 .keywords: TS, clone 6831 .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 6832 @*/ 6833 PetscErrorCode TSClone(TS tsin, TS *tsout) 6834 { 6835 TS t; 6836 PetscErrorCode ierr; 6837 SNES snes_start; 6838 DM dm; 6839 TSType type; 6840 6841 PetscFunctionBegin; 6842 PetscValidPointer(tsin,1); 6843 *tsout = NULL; 6844 6845 ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 6846 6847 /* General TS description */ 6848 t->numbermonitors = 0; 6849 t->setupcalled = 0; 6850 t->ksp_its = 0; 6851 t->snes_its = 0; 6852 t->nwork = 0; 6853 t->rhsjacobian.time = -1e20; 6854 t->rhsjacobian.scale = 1.; 6855 t->ijacobian.shift = 1.; 6856 6857 ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 6858 ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6859 6860 ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 6861 ierr = TSSetDM(t,dm); CHKERRQ(ierr); 6862 6863 t->adapt=tsin->adapt; 6864 PetscObjectReference((PetscObject)t->adapt); 6865 6866 t->problem_type = tsin->problem_type; 6867 t->ptime = tsin->ptime; 6868 t->time_step = tsin->time_step; 6869 t->time_step_orig = tsin->time_step_orig; 6870 t->max_time = tsin->max_time; 6871 t->steps = tsin->steps; 6872 t->max_steps = tsin->max_steps; 6873 t->equation_type = tsin->equation_type; 6874 t->atol = tsin->atol; 6875 t->rtol = tsin->rtol; 6876 t->max_snes_failures = tsin->max_snes_failures; 6877 t->max_reject = tsin->max_reject; 6878 t->errorifstepfailed = tsin->errorifstepfailed; 6879 6880 ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 6881 ierr = TSSetType(t,type); CHKERRQ(ierr); 6882 6883 t->vec_sol = NULL; 6884 6885 t->cfltime = tsin->cfltime; 6886 t->cfltime_local = tsin->cfltime_local; 6887 t->exact_final_time = tsin->exact_final_time; 6888 6889 ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 6890 6891 if (((PetscObject)tsin)->fortran_func_pointers) { 6892 PetscInt i; 6893 ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 6894 for (i=0; i<10; i++) { 6895 ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 6896 } 6897 } 6898 *tsout = t; 6899 PetscFunctionReturn(0); 6900 } 6901