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