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 if ((*ctx)->transformdestroy) { 3479 ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 3480 } 3481 ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 3482 ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 3483 ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 3484 ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3485 ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3486 ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3487 ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 3488 ierr = PetscFree(*ctx);CHKERRQ(ierr); 3489 PetscFunctionReturn(0); 3490 } 3491 3492 #undef __FUNCT__ 3493 #define __FUNCT__ "TSGetTime" 3494 /*@ 3495 TSGetTime - Gets the time of the most recently completed step. 3496 3497 Not Collective 3498 3499 Input Parameter: 3500 . ts - the TS context obtained from TSCreate() 3501 3502 Output Parameter: 3503 . t - the current time 3504 3505 Level: beginner 3506 3507 Note: 3508 When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 3509 TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3510 3511 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3512 3513 .keywords: TS, get, time 3514 @*/ 3515 PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3516 { 3517 PetscFunctionBegin; 3518 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3519 PetscValidRealPointer(t,2); 3520 *t = ts->ptime; 3521 PetscFunctionReturn(0); 3522 } 3523 3524 #undef __FUNCT__ 3525 #define __FUNCT__ "TSGetPrevTime" 3526 /*@ 3527 TSGetPrevTime - Gets the starting time of the previously completed step. 3528 3529 Not Collective 3530 3531 Input Parameter: 3532 . ts - the TS context obtained from TSCreate() 3533 3534 Output Parameter: 3535 . t - the previous time 3536 3537 Level: beginner 3538 3539 .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3540 3541 .keywords: TS, get, time 3542 @*/ 3543 PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3544 { 3545 PetscFunctionBegin; 3546 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3547 PetscValidRealPointer(t,2); 3548 *t = ts->ptime_prev; 3549 PetscFunctionReturn(0); 3550 } 3551 3552 #undef __FUNCT__ 3553 #define __FUNCT__ "TSSetTime" 3554 /*@ 3555 TSSetTime - Allows one to reset the time. 3556 3557 Logically Collective on TS 3558 3559 Input Parameters: 3560 + ts - the TS context obtained from TSCreate() 3561 - time - the time 3562 3563 Level: intermediate 3564 3565 .seealso: TSGetTime(), TSSetDuration() 3566 3567 .keywords: TS, set, time 3568 @*/ 3569 PetscErrorCode TSSetTime(TS ts, PetscReal t) 3570 { 3571 PetscFunctionBegin; 3572 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3573 PetscValidLogicalCollectiveReal(ts,t,2); 3574 ts->ptime = t; 3575 PetscFunctionReturn(0); 3576 } 3577 3578 #undef __FUNCT__ 3579 #define __FUNCT__ "TSSetOptionsPrefix" 3580 /*@C 3581 TSSetOptionsPrefix - Sets the prefix used for searching for all 3582 TS options in the database. 3583 3584 Logically Collective on TS 3585 3586 Input Parameter: 3587 + ts - The TS context 3588 - prefix - The prefix to prepend to all option names 3589 3590 Notes: 3591 A hyphen (-) must NOT be given at the beginning of the prefix name. 3592 The first character of all runtime options is AUTOMATICALLY the 3593 hyphen. 3594 3595 Level: advanced 3596 3597 .keywords: TS, set, options, prefix, database 3598 3599 .seealso: TSSetFromOptions() 3600 3601 @*/ 3602 PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3603 { 3604 PetscErrorCode ierr; 3605 SNES snes; 3606 3607 PetscFunctionBegin; 3608 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3609 ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3610 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3611 ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3612 PetscFunctionReturn(0); 3613 } 3614 3615 3616 #undef __FUNCT__ 3617 #define __FUNCT__ "TSAppendOptionsPrefix" 3618 /*@C 3619 TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3620 TS options in the database. 3621 3622 Logically Collective on TS 3623 3624 Input Parameter: 3625 + ts - The TS context 3626 - prefix - The prefix to prepend to all option names 3627 3628 Notes: 3629 A hyphen (-) must NOT be given at the beginning of the prefix name. 3630 The first character of all runtime options is AUTOMATICALLY the 3631 hyphen. 3632 3633 Level: advanced 3634 3635 .keywords: TS, append, options, prefix, database 3636 3637 .seealso: TSGetOptionsPrefix() 3638 3639 @*/ 3640 PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3641 { 3642 PetscErrorCode ierr; 3643 SNES snes; 3644 3645 PetscFunctionBegin; 3646 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3647 ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3648 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3649 ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3650 PetscFunctionReturn(0); 3651 } 3652 3653 #undef __FUNCT__ 3654 #define __FUNCT__ "TSGetOptionsPrefix" 3655 /*@C 3656 TSGetOptionsPrefix - Sets the prefix used for searching for all 3657 TS options in the database. 3658 3659 Not Collective 3660 3661 Input Parameter: 3662 . ts - The TS context 3663 3664 Output Parameter: 3665 . prefix - A pointer to the prefix string used 3666 3667 Notes: On the fortran side, the user should pass in a string 'prifix' of 3668 sufficient length to hold the prefix. 3669 3670 Level: intermediate 3671 3672 .keywords: TS, get, options, prefix, database 3673 3674 .seealso: TSAppendOptionsPrefix() 3675 @*/ 3676 PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3677 { 3678 PetscErrorCode ierr; 3679 3680 PetscFunctionBegin; 3681 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3682 PetscValidPointer(prefix,2); 3683 ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3684 PetscFunctionReturn(0); 3685 } 3686 3687 #undef __FUNCT__ 3688 #define __FUNCT__ "TSGetRHSJacobian" 3689 /*@C 3690 TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3691 3692 Not Collective, but parallel objects are returned if TS is parallel 3693 3694 Input Parameter: 3695 . ts - The TS context obtained from TSCreate() 3696 3697 Output Parameters: 3698 + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3699 . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3700 . func - Function to compute the Jacobian of the RHS (or NULL) 3701 - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3702 3703 Notes: You can pass in NULL for any return argument you do not need. 3704 3705 Level: intermediate 3706 3707 .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3708 3709 .keywords: TS, timestep, get, matrix, Jacobian 3710 @*/ 3711 PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3712 { 3713 PetscErrorCode ierr; 3714 SNES snes; 3715 DM dm; 3716 3717 PetscFunctionBegin; 3718 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3719 ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 3720 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 3721 ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3722 PetscFunctionReturn(0); 3723 } 3724 3725 #undef __FUNCT__ 3726 #define __FUNCT__ "TSGetIJacobian" 3727 /*@C 3728 TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 3729 3730 Not Collective, but parallel objects are returned if TS is parallel 3731 3732 Input Parameter: 3733 . ts - The TS context obtained from TSCreate() 3734 3735 Output Parameters: 3736 + Amat - The (approximate) Jacobian of F(t,U,U_t) 3737 . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 3738 . f - The function to compute the matrices 3739 - ctx - User-defined context for Jacobian evaluation routine 3740 3741 Notes: You can pass in NULL for any return argument you do not need. 3742 3743 Level: advanced 3744 3745 .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3746 3747 .keywords: TS, timestep, get, matrix, Jacobian 3748 @*/ 3749 PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 3750 { 3751 PetscErrorCode ierr; 3752 SNES snes; 3753 DM dm; 3754 3755 PetscFunctionBegin; 3756 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3757 ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3758 ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 3759 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 3760 ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 3761 PetscFunctionReturn(0); 3762 } 3763 3764 3765 #undef __FUNCT__ 3766 #define __FUNCT__ "TSMonitorDrawSolution" 3767 /*@C 3768 TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 3769 VecView() for the solution at each timestep 3770 3771 Collective on TS 3772 3773 Input Parameters: 3774 + ts - the TS context 3775 . step - current time-step 3776 . ptime - current time 3777 - dummy - either a viewer or NULL 3778 3779 Options Database: 3780 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 3781 3782 Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 3783 will look bad 3784 3785 Level: intermediate 3786 3787 .keywords: TS, vector, monitor, view 3788 3789 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3790 @*/ 3791 PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 3792 { 3793 PetscErrorCode ierr; 3794 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3795 PetscDraw draw; 3796 3797 PetscFunctionBegin; 3798 if (!step && ictx->showinitial) { 3799 if (!ictx->initialsolution) { 3800 ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 3801 } 3802 ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 3803 } 3804 if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 3805 3806 if (ictx->showinitial) { 3807 PetscReal pause; 3808 ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 3809 ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 3810 ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 3811 ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 3812 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 3813 } 3814 ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3815 if (ictx->showtimestepandtime) { 3816 PetscReal xl,yl,xr,yr,tw,w,h; 3817 char time[32]; 3818 size_t len; 3819 3820 ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3821 ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 3822 ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3823 ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 3824 ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3825 w = xl + .5*(xr - xl) - .5*len*tw; 3826 h = yl + .95*(yr - yl); 3827 ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3828 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3829 } 3830 3831 if (ictx->showinitial) { 3832 ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 3833 } 3834 PetscFunctionReturn(0); 3835 } 3836 3837 #undef __FUNCT__ 3838 #define __FUNCT__ "TSMonitorDrawSolutionPhase" 3839 /*@C 3840 TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 3841 3842 Collective on TS 3843 3844 Input Parameters: 3845 + ts - the TS context 3846 . step - current time-step 3847 . ptime - current time 3848 - dummy - either a viewer or NULL 3849 3850 Level: intermediate 3851 3852 .keywords: TS, vector, monitor, view 3853 3854 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3855 @*/ 3856 PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 3857 { 3858 PetscErrorCode ierr; 3859 TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3860 PetscDraw draw; 3861 MPI_Comm comm; 3862 PetscInt n; 3863 PetscMPIInt size; 3864 PetscReal xl,yl,xr,yr,tw,w,h; 3865 char time[32]; 3866 size_t len; 3867 const PetscScalar *U; 3868 3869 PetscFunctionBegin; 3870 ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 3871 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3872 if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 3873 ierr = VecGetSize(u,&n);CHKERRQ(ierr); 3874 if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 3875 3876 ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 3877 3878 ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 3879 ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 3880 if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 3881 ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3882 PetscFunctionReturn(0); 3883 } 3884 if (!step) ictx->color++; 3885 ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 3886 ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 3887 3888 if (ictx->showtimestepandtime) { 3889 ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3890 ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 3891 ierr = PetscStrlen(time,&len);CHKERRQ(ierr); 3892 ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr); 3893 w = xl + .5*(xr - xl) - .5*len*tw; 3894 h = yl + .95*(yr - yl); 3895 ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3896 } 3897 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3898 PetscFunctionReturn(0); 3899 } 3900 3901 3902 #undef __FUNCT__ 3903 #define __FUNCT__ "TSMonitorDrawCtxDestroy" 3904 /*@C 3905 TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 3906 3907 Collective on TS 3908 3909 Input Parameters: 3910 . ctx - the monitor context 3911 3912 Level: intermediate 3913 3914 .keywords: TS, vector, monitor, view 3915 3916 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 3917 @*/ 3918 PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 3919 { 3920 PetscErrorCode ierr; 3921 3922 PetscFunctionBegin; 3923 ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 3924 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 3925 ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 3926 ierr = PetscFree(*ictx);CHKERRQ(ierr); 3927 PetscFunctionReturn(0); 3928 } 3929 3930 #undef __FUNCT__ 3931 #define __FUNCT__ "TSMonitorDrawCtxCreate" 3932 /*@C 3933 TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 3934 3935 Collective on TS 3936 3937 Input Parameter: 3938 . ts - time-step context 3939 3940 Output Patameter: 3941 . ctx - the monitor context 3942 3943 Options Database: 3944 . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 3945 3946 Level: intermediate 3947 3948 .keywords: TS, vector, monitor, view 3949 3950 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 3951 @*/ 3952 PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 3953 { 3954 PetscErrorCode ierr; 3955 3956 PetscFunctionBegin; 3957 ierr = PetscNew(ctx);CHKERRQ(ierr); 3958 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 3959 ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 3960 3961 (*ctx)->howoften = howoften; 3962 (*ctx)->showinitial = PETSC_FALSE; 3963 ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 3964 3965 (*ctx)->showtimestepandtime = PETSC_FALSE; 3966 ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 3967 (*ctx)->color = PETSC_DRAW_WHITE; 3968 PetscFunctionReturn(0); 3969 } 3970 3971 #undef __FUNCT__ 3972 #define __FUNCT__ "TSMonitorDrawError" 3973 /*@C 3974 TSMonitorDrawError - Monitors progress of the TS solvers by calling 3975 VecView() for the error at each timestep 3976 3977 Collective on TS 3978 3979 Input Parameters: 3980 + ts - the TS context 3981 . step - current time-step 3982 . ptime - current time 3983 - dummy - either a viewer or NULL 3984 3985 Level: intermediate 3986 3987 .keywords: TS, vector, monitor, view 3988 3989 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 3990 @*/ 3991 PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 3992 { 3993 PetscErrorCode ierr; 3994 TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 3995 PetscViewer viewer = ctx->viewer; 3996 Vec work; 3997 3998 PetscFunctionBegin; 3999 if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 4000 ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 4001 ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 4002 ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 4003 ierr = VecView(work,viewer);CHKERRQ(ierr); 4004 ierr = VecDestroy(&work);CHKERRQ(ierr); 4005 PetscFunctionReturn(0); 4006 } 4007 4008 #include <petsc-private/dmimpl.h> 4009 #undef __FUNCT__ 4010 #define __FUNCT__ "TSSetDM" 4011 /*@ 4012 TSSetDM - Sets the DM that may be used by some preconditioners 4013 4014 Logically Collective on TS and DM 4015 4016 Input Parameters: 4017 + ts - the preconditioner context 4018 - dm - the dm 4019 4020 Level: intermediate 4021 4022 4023 .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 4024 @*/ 4025 PetscErrorCode TSSetDM(TS ts,DM dm) 4026 { 4027 PetscErrorCode ierr; 4028 SNES snes; 4029 DMTS tsdm; 4030 4031 PetscFunctionBegin; 4032 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4033 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4034 if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 4035 if (ts->dm->dmts && !dm->dmts) { 4036 ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4037 ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 4038 if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 4039 tsdm->originaldm = dm; 4040 } 4041 } 4042 ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 4043 } 4044 ts->dm = dm; 4045 4046 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4047 ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 4048 PetscFunctionReturn(0); 4049 } 4050 4051 #undef __FUNCT__ 4052 #define __FUNCT__ "TSGetDM" 4053 /*@ 4054 TSGetDM - Gets the DM that may be used by some preconditioners 4055 4056 Not Collective 4057 4058 Input Parameter: 4059 . ts - the preconditioner context 4060 4061 Output Parameter: 4062 . dm - the dm 4063 4064 Level: intermediate 4065 4066 4067 .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 4068 @*/ 4069 PetscErrorCode TSGetDM(TS ts,DM *dm) 4070 { 4071 PetscErrorCode ierr; 4072 4073 PetscFunctionBegin; 4074 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4075 if (!ts->dm) { 4076 ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4077 if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4078 } 4079 *dm = ts->dm; 4080 PetscFunctionReturn(0); 4081 } 4082 4083 #undef __FUNCT__ 4084 #define __FUNCT__ "SNESTSFormFunction" 4085 /*@ 4086 SNESTSFormFunction - Function to evaluate nonlinear residual 4087 4088 Logically Collective on SNES 4089 4090 Input Parameter: 4091 + snes - nonlinear solver 4092 . U - the current state at which to evaluate the residual 4093 - ctx - user context, must be a TS 4094 4095 Output Parameter: 4096 . F - the nonlinear residual 4097 4098 Notes: 4099 This function is not normally called by users and is automatically registered with the SNES used by TS. 4100 It is most frequently passed to MatFDColoringSetFunction(). 4101 4102 Level: advanced 4103 4104 .seealso: SNESSetFunction(), MatFDColoringSetFunction() 4105 @*/ 4106 PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 4107 { 4108 TS ts = (TS)ctx; 4109 PetscErrorCode ierr; 4110 4111 PetscFunctionBegin; 4112 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4113 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4114 PetscValidHeaderSpecific(F,VEC_CLASSID,3); 4115 PetscValidHeaderSpecific(ts,TS_CLASSID,4); 4116 ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 4117 PetscFunctionReturn(0); 4118 } 4119 4120 #undef __FUNCT__ 4121 #define __FUNCT__ "SNESTSFormJacobian" 4122 /*@ 4123 SNESTSFormJacobian - Function to evaluate the Jacobian 4124 4125 Collective on SNES 4126 4127 Input Parameter: 4128 + snes - nonlinear solver 4129 . U - the current state at which to evaluate the residual 4130 - ctx - user context, must be a TS 4131 4132 Output Parameter: 4133 + A - the Jacobian 4134 . B - the preconditioning matrix (may be the same as A) 4135 - flag - indicates any structure change in the matrix 4136 4137 Notes: 4138 This function is not normally called by users and is automatically registered with the SNES used by TS. 4139 4140 Level: developer 4141 4142 .seealso: SNESSetJacobian() 4143 @*/ 4144 PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 4145 { 4146 TS ts = (TS)ctx; 4147 PetscErrorCode ierr; 4148 4149 PetscFunctionBegin; 4150 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4151 PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4152 PetscValidPointer(A,3); 4153 PetscValidHeaderSpecific(A,MAT_CLASSID,3); 4154 PetscValidPointer(B,4); 4155 PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4156 PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4157 ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 4158 PetscFunctionReturn(0); 4159 } 4160 4161 #undef __FUNCT__ 4162 #define __FUNCT__ "TSComputeRHSFunctionLinear" 4163 /*@C 4164 TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 4165 4166 Collective on TS 4167 4168 Input Arguments: 4169 + ts - time stepping context 4170 . t - time at which to evaluate 4171 . U - state at which to evaluate 4172 - ctx - context 4173 4174 Output Arguments: 4175 . F - right hand side 4176 4177 Level: intermediate 4178 4179 Notes: 4180 This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 4181 The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 4182 4183 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 4184 @*/ 4185 PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 4186 { 4187 PetscErrorCode ierr; 4188 Mat Arhs,Brhs; 4189 4190 PetscFunctionBegin; 4191 ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4192 ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 4193 ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 4194 PetscFunctionReturn(0); 4195 } 4196 4197 #undef __FUNCT__ 4198 #define __FUNCT__ "TSComputeRHSJacobianConstant" 4199 /*@C 4200 TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 4201 4202 Collective on TS 4203 4204 Input Arguments: 4205 + ts - time stepping context 4206 . t - time at which to evaluate 4207 . U - state at which to evaluate 4208 - ctx - context 4209 4210 Output Arguments: 4211 + A - pointer to operator 4212 . B - pointer to preconditioning matrix 4213 - flg - matrix structure flag 4214 4215 Level: intermediate 4216 4217 Notes: 4218 This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 4219 4220 .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 4221 @*/ 4222 PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 4223 { 4224 PetscFunctionBegin; 4225 PetscFunctionReturn(0); 4226 } 4227 4228 #undef __FUNCT__ 4229 #define __FUNCT__ "TSComputeIFunctionLinear" 4230 /*@C 4231 TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 4232 4233 Collective on TS 4234 4235 Input Arguments: 4236 + ts - time stepping context 4237 . t - time at which to evaluate 4238 . U - state at which to evaluate 4239 . Udot - time derivative of state vector 4240 - ctx - context 4241 4242 Output Arguments: 4243 . F - left hand side 4244 4245 Level: intermediate 4246 4247 Notes: 4248 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 4249 user is required to write their own TSComputeIFunction. 4250 This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 4251 The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 4252 4253 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 4254 @*/ 4255 PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 4256 { 4257 PetscErrorCode ierr; 4258 Mat A,B; 4259 4260 PetscFunctionBegin; 4261 ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4262 ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 4263 ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 4264 PetscFunctionReturn(0); 4265 } 4266 4267 #undef __FUNCT__ 4268 #define __FUNCT__ "TSComputeIJacobianConstant" 4269 /*@C 4270 TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 4271 4272 Collective on TS 4273 4274 Input Arguments: 4275 + ts - time stepping context 4276 . t - time at which to evaluate 4277 . U - state at which to evaluate 4278 . Udot - time derivative of state vector 4279 . shift - shift to apply 4280 - ctx - context 4281 4282 Output Arguments: 4283 + A - pointer to operator 4284 . B - pointer to preconditioning matrix 4285 - flg - matrix structure flag 4286 4287 Level: advanced 4288 4289 Notes: 4290 This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 4291 4292 It is only appropriate for problems of the form 4293 4294 $ M Udot = F(U,t) 4295 4296 where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4297 works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4298 an implicit operator of the form 4299 4300 $ shift*M + J 4301 4302 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 4303 a copy of M or reassemble it when requested. 4304 4305 .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 4306 @*/ 4307 PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 4308 { 4309 PetscErrorCode ierr; 4310 4311 PetscFunctionBegin; 4312 ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4313 ts->ijacobian.shift = shift; 4314 PetscFunctionReturn(0); 4315 } 4316 4317 #undef __FUNCT__ 4318 #define __FUNCT__ "TSGetEquationType" 4319 /*@ 4320 TSGetEquationType - Gets the type of the equation that TS is solving. 4321 4322 Not Collective 4323 4324 Input Parameter: 4325 . ts - the TS context 4326 4327 Output Parameter: 4328 . equation_type - see TSEquationType 4329 4330 Level: beginner 4331 4332 .keywords: TS, equation type 4333 4334 .seealso: TSSetEquationType(), TSEquationType 4335 @*/ 4336 PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4337 { 4338 PetscFunctionBegin; 4339 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4340 PetscValidPointer(equation_type,2); 4341 *equation_type = ts->equation_type; 4342 PetscFunctionReturn(0); 4343 } 4344 4345 #undef __FUNCT__ 4346 #define __FUNCT__ "TSSetEquationType" 4347 /*@ 4348 TSSetEquationType - Sets the type of the equation that TS is solving. 4349 4350 Not Collective 4351 4352 Input Parameter: 4353 + ts - the TS context 4354 . equation_type - see TSEquationType 4355 4356 Level: advanced 4357 4358 .keywords: TS, equation type 4359 4360 .seealso: TSGetEquationType(), TSEquationType 4361 @*/ 4362 PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4363 { 4364 PetscFunctionBegin; 4365 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4366 ts->equation_type = equation_type; 4367 PetscFunctionReturn(0); 4368 } 4369 4370 #undef __FUNCT__ 4371 #define __FUNCT__ "TSGetConvergedReason" 4372 /*@ 4373 TSGetConvergedReason - Gets the reason the TS iteration was stopped. 4374 4375 Not Collective 4376 4377 Input Parameter: 4378 . ts - the TS context 4379 4380 Output Parameter: 4381 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4382 manual pages for the individual convergence tests for complete lists 4383 4384 Level: beginner 4385 4386 Notes: 4387 Can only be called after the call to TSSolve() is complete. 4388 4389 .keywords: TS, nonlinear, set, convergence, test 4390 4391 .seealso: TSSetConvergenceTest(), TSConvergedReason 4392 @*/ 4393 PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 4394 { 4395 PetscFunctionBegin; 4396 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4397 PetscValidPointer(reason,2); 4398 *reason = ts->reason; 4399 PetscFunctionReturn(0); 4400 } 4401 4402 #undef __FUNCT__ 4403 #define __FUNCT__ "TSSetConvergedReason" 4404 /*@ 4405 TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4406 4407 Not Collective 4408 4409 Input Parameter: 4410 + ts - the TS context 4411 . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4412 manual pages for the individual convergence tests for complete lists 4413 4414 Level: advanced 4415 4416 Notes: 4417 Can only be called during TSSolve() is active. 4418 4419 .keywords: TS, nonlinear, set, convergence, test 4420 4421 .seealso: TSConvergedReason 4422 @*/ 4423 PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4424 { 4425 PetscFunctionBegin; 4426 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4427 ts->reason = reason; 4428 PetscFunctionReturn(0); 4429 } 4430 4431 #undef __FUNCT__ 4432 #define __FUNCT__ "TSGetSolveTime" 4433 /*@ 4434 TSGetSolveTime - Gets the time after a call to TSSolve() 4435 4436 Not Collective 4437 4438 Input Parameter: 4439 . ts - the TS context 4440 4441 Output Parameter: 4442 . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4443 4444 Level: beginner 4445 4446 Notes: 4447 Can only be called after the call to TSSolve() is complete. 4448 4449 .keywords: TS, nonlinear, set, convergence, test 4450 4451 .seealso: TSSetConvergenceTest(), TSConvergedReason 4452 @*/ 4453 PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4454 { 4455 PetscFunctionBegin; 4456 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4457 PetscValidPointer(ftime,2); 4458 *ftime = ts->solvetime; 4459 PetscFunctionReturn(0); 4460 } 4461 4462 #undef __FUNCT__ 4463 #define __FUNCT__ "TSGetTotalSteps" 4464 /*@ 4465 TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 4466 4467 Not Collective 4468 4469 Input Parameter: 4470 . ts - the TS context 4471 4472 Output Parameter: 4473 . steps - the number of steps 4474 4475 Level: beginner 4476 4477 Notes: 4478 Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 4479 4480 .keywords: TS, nonlinear, set, convergence, test 4481 4482 .seealso: TSSetConvergenceTest(), TSConvergedReason 4483 @*/ 4484 PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 4485 { 4486 PetscFunctionBegin; 4487 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4488 PetscValidPointer(steps,2); 4489 *steps = ts->total_steps; 4490 PetscFunctionReturn(0); 4491 } 4492 4493 #undef __FUNCT__ 4494 #define __FUNCT__ "TSGetSNESIterations" 4495 /*@ 4496 TSGetSNESIterations - Gets the total number of nonlinear iterations 4497 used by the time integrator. 4498 4499 Not Collective 4500 4501 Input Parameter: 4502 . ts - TS context 4503 4504 Output Parameter: 4505 . nits - number of nonlinear iterations 4506 4507 Notes: 4508 This counter is reset to zero for each successive call to TSSolve(). 4509 4510 Level: intermediate 4511 4512 .keywords: TS, get, number, nonlinear, iterations 4513 4514 .seealso: TSGetKSPIterations() 4515 @*/ 4516 PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 4517 { 4518 PetscFunctionBegin; 4519 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4520 PetscValidIntPointer(nits,2); 4521 *nits = ts->snes_its; 4522 PetscFunctionReturn(0); 4523 } 4524 4525 #undef __FUNCT__ 4526 #define __FUNCT__ "TSGetKSPIterations" 4527 /*@ 4528 TSGetKSPIterations - Gets the total number of linear iterations 4529 used by the time integrator. 4530 4531 Not Collective 4532 4533 Input Parameter: 4534 . ts - TS context 4535 4536 Output Parameter: 4537 . lits - number of linear iterations 4538 4539 Notes: 4540 This counter is reset to zero for each successive call to TSSolve(). 4541 4542 Level: intermediate 4543 4544 .keywords: TS, get, number, linear, iterations 4545 4546 .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 4547 @*/ 4548 PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 4549 { 4550 PetscFunctionBegin; 4551 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4552 PetscValidIntPointer(lits,2); 4553 *lits = ts->ksp_its; 4554 PetscFunctionReturn(0); 4555 } 4556 4557 #undef __FUNCT__ 4558 #define __FUNCT__ "TSGetStepRejections" 4559 /*@ 4560 TSGetStepRejections - Gets the total number of rejected steps. 4561 4562 Not Collective 4563 4564 Input Parameter: 4565 . ts - TS context 4566 4567 Output Parameter: 4568 . rejects - number of steps rejected 4569 4570 Notes: 4571 This counter is reset to zero for each successive call to TSSolve(). 4572 4573 Level: intermediate 4574 4575 .keywords: TS, get, number 4576 4577 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4578 @*/ 4579 PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4580 { 4581 PetscFunctionBegin; 4582 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4583 PetscValidIntPointer(rejects,2); 4584 *rejects = ts->reject; 4585 PetscFunctionReturn(0); 4586 } 4587 4588 #undef __FUNCT__ 4589 #define __FUNCT__ "TSGetSNESFailures" 4590 /*@ 4591 TSGetSNESFailures - Gets the total number of failed SNES solves 4592 4593 Not Collective 4594 4595 Input Parameter: 4596 . ts - TS context 4597 4598 Output Parameter: 4599 . fails - number of failed nonlinear solves 4600 4601 Notes: 4602 This counter is reset to zero for each successive call to TSSolve(). 4603 4604 Level: intermediate 4605 4606 .keywords: TS, get, number 4607 4608 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4609 @*/ 4610 PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4611 { 4612 PetscFunctionBegin; 4613 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4614 PetscValidIntPointer(fails,2); 4615 *fails = ts->num_snes_failures; 4616 PetscFunctionReturn(0); 4617 } 4618 4619 #undef __FUNCT__ 4620 #define __FUNCT__ "TSSetMaxStepRejections" 4621 /*@ 4622 TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4623 4624 Not Collective 4625 4626 Input Parameter: 4627 + ts - TS context 4628 - rejects - maximum number of rejected steps, pass -1 for unlimited 4629 4630 Notes: 4631 The counter is reset to zero for each step 4632 4633 Options Database Key: 4634 . -ts_max_reject - Maximum number of step rejections before a step fails 4635 4636 Level: intermediate 4637 4638 .keywords: TS, set, maximum, number 4639 4640 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4641 @*/ 4642 PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4643 { 4644 PetscFunctionBegin; 4645 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4646 ts->max_reject = rejects; 4647 PetscFunctionReturn(0); 4648 } 4649 4650 #undef __FUNCT__ 4651 #define __FUNCT__ "TSSetMaxSNESFailures" 4652 /*@ 4653 TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4654 4655 Not Collective 4656 4657 Input Parameter: 4658 + ts - TS context 4659 - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4660 4661 Notes: 4662 The counter is reset to zero for each successive call to TSSolve(). 4663 4664 Options Database Key: 4665 . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4666 4667 Level: intermediate 4668 4669 .keywords: TS, set, maximum, number 4670 4671 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4672 @*/ 4673 PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4674 { 4675 PetscFunctionBegin; 4676 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4677 ts->max_snes_failures = fails; 4678 PetscFunctionReturn(0); 4679 } 4680 4681 #undef __FUNCT__ 4682 #define __FUNCT__ "TSSetErrorIfStepFails" 4683 /*@ 4684 TSSetErrorIfStepFails - Error if no step succeeds 4685 4686 Not Collective 4687 4688 Input Parameter: 4689 + ts - TS context 4690 - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4691 4692 Options Database Key: 4693 . -ts_error_if_step_fails - Error if no step succeeds 4694 4695 Level: intermediate 4696 4697 .keywords: TS, set, error 4698 4699 .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4700 @*/ 4701 PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4702 { 4703 PetscFunctionBegin; 4704 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4705 ts->errorifstepfailed = err; 4706 PetscFunctionReturn(0); 4707 } 4708 4709 #undef __FUNCT__ 4710 #define __FUNCT__ "TSMonitorSolutionBinary" 4711 /*@C 4712 TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4713 4714 Collective on TS 4715 4716 Input Parameters: 4717 + ts - the TS context 4718 . step - current time-step 4719 . ptime - current time 4720 . u - current state 4721 - viewer - binary viewer 4722 4723 Level: intermediate 4724 4725 .keywords: TS, vector, monitor, view 4726 4727 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4728 @*/ 4729 PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4730 { 4731 PetscErrorCode ierr; 4732 PetscViewer v = (PetscViewer)viewer; 4733 4734 PetscFunctionBegin; 4735 ierr = VecView(u,v);CHKERRQ(ierr); 4736 PetscFunctionReturn(0); 4737 } 4738 4739 #undef __FUNCT__ 4740 #define __FUNCT__ "TSMonitorSolutionVTK" 4741 /*@C 4742 TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4743 4744 Collective on TS 4745 4746 Input Parameters: 4747 + ts - the TS context 4748 . step - current time-step 4749 . ptime - current time 4750 . u - current state 4751 - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4752 4753 Level: intermediate 4754 4755 Notes: 4756 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. 4757 These are named according to the file name template. 4758 4759 This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4760 4761 .keywords: TS, vector, monitor, view 4762 4763 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4764 @*/ 4765 PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4766 { 4767 PetscErrorCode ierr; 4768 char filename[PETSC_MAX_PATH_LEN]; 4769 PetscViewer viewer; 4770 4771 PetscFunctionBegin; 4772 ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4773 ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 4774 ierr = VecView(u,viewer);CHKERRQ(ierr); 4775 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4776 PetscFunctionReturn(0); 4777 } 4778 4779 #undef __FUNCT__ 4780 #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4781 /*@C 4782 TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4783 4784 Collective on TS 4785 4786 Input Parameters: 4787 . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4788 4789 Level: intermediate 4790 4791 Note: 4792 This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 4793 4794 .keywords: TS, vector, monitor, view 4795 4796 .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 4797 @*/ 4798 PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 4799 { 4800 PetscErrorCode ierr; 4801 4802 PetscFunctionBegin; 4803 ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 4804 PetscFunctionReturn(0); 4805 } 4806 4807 #undef __FUNCT__ 4808 #define __FUNCT__ "TSGetAdapt" 4809 /*@ 4810 TSGetAdapt - Get the adaptive controller context for the current method 4811 4812 Collective on TS if controller has not been created yet 4813 4814 Input Arguments: 4815 . ts - time stepping context 4816 4817 Output Arguments: 4818 . adapt - adaptive controller 4819 4820 Level: intermediate 4821 4822 .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 4823 @*/ 4824 PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 4825 { 4826 PetscErrorCode ierr; 4827 4828 PetscFunctionBegin; 4829 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4830 PetscValidPointer(adapt,2); 4831 if (!ts->adapt) { 4832 ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 4833 ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 4834 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 4835 } 4836 *adapt = ts->adapt; 4837 PetscFunctionReturn(0); 4838 } 4839 4840 #undef __FUNCT__ 4841 #define __FUNCT__ "TSSetTolerances" 4842 /*@ 4843 TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 4844 4845 Logically Collective 4846 4847 Input Arguments: 4848 + ts - time integration context 4849 . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 4850 . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 4851 . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 4852 - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 4853 4854 Options Database keys: 4855 + -ts_rtol <rtol> - relative tolerance for local truncation error 4856 - -ts_atol <atol> Absolute tolerance for local truncation error 4857 4858 Level: beginner 4859 4860 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 4861 @*/ 4862 PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 4863 { 4864 PetscErrorCode ierr; 4865 4866 PetscFunctionBegin; 4867 if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 4868 if (vatol) { 4869 ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 4870 ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 4871 4872 ts->vatol = vatol; 4873 } 4874 if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 4875 if (vrtol) { 4876 ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 4877 ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 4878 4879 ts->vrtol = vrtol; 4880 } 4881 PetscFunctionReturn(0); 4882 } 4883 4884 #undef __FUNCT__ 4885 #define __FUNCT__ "TSGetTolerances" 4886 /*@ 4887 TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4888 4889 Logically Collective 4890 4891 Input Arguments: 4892 . ts - time integration context 4893 4894 Output Arguments: 4895 + atol - scalar absolute tolerances, NULL to ignore 4896 . vatol - vector of absolute tolerances, NULL to ignore 4897 . rtol - scalar relative tolerances, NULL to ignore 4898 - vrtol - vector of relative tolerances, NULL to ignore 4899 4900 Level: beginner 4901 4902 .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 4903 @*/ 4904 PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4905 { 4906 PetscFunctionBegin; 4907 if (atol) *atol = ts->atol; 4908 if (vatol) *vatol = ts->vatol; 4909 if (rtol) *rtol = ts->rtol; 4910 if (vrtol) *vrtol = ts->vrtol; 4911 PetscFunctionReturn(0); 4912 } 4913 4914 #undef __FUNCT__ 4915 #define __FUNCT__ "TSErrorNormWRMS" 4916 /*@ 4917 TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state 4918 4919 Collective on TS 4920 4921 Input Arguments: 4922 + ts - time stepping context 4923 - Y - state vector to be compared to ts->vec_sol 4924 4925 Output Arguments: 4926 . norm - weighted norm, a value of 1.0 is considered small 4927 4928 Level: developer 4929 4930 .seealso: TSSetTolerances() 4931 @*/ 4932 PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm) 4933 { 4934 PetscErrorCode ierr; 4935 PetscInt i,n,N; 4936 const PetscScalar *u,*y; 4937 Vec U; 4938 PetscReal sum,gsum; 4939 4940 PetscFunctionBegin; 4941 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4942 PetscValidHeaderSpecific(Y,VEC_CLASSID,2); 4943 PetscValidPointer(norm,3); 4944 U = ts->vec_sol; 4945 PetscCheckSameTypeAndComm(U,1,Y,2); 4946 if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector"); 4947 4948 ierr = VecGetSize(U,&N);CHKERRQ(ierr); 4949 ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 4950 ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 4951 ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 4952 sum = 0.; 4953 if (ts->vatol && ts->vrtol) { 4954 const PetscScalar *atol,*rtol; 4955 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4956 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4957 for (i=0; i<n; i++) { 4958 PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 4959 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4960 } 4961 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4962 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4963 } else if (ts->vatol) { /* vector atol, scalar rtol */ 4964 const PetscScalar *atol; 4965 ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4966 for (i=0; i<n; i++) { 4967 PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 4968 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4969 } 4970 ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 4971 } else if (ts->vrtol) { /* scalar atol, vector rtol */ 4972 const PetscScalar *rtol; 4973 ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4974 for (i=0; i<n; i++) { 4975 PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 4976 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4977 } 4978 ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 4979 } else { /* scalar atol, scalar rtol */ 4980 for (i=0; i<n; i++) { 4981 PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 4982 sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 4983 } 4984 } 4985 ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 4986 ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 4987 4988 ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 4989 *norm = PetscSqrtReal(gsum / N); 4990 if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 4991 PetscFunctionReturn(0); 4992 } 4993 4994 #undef __FUNCT__ 4995 #define __FUNCT__ "TSSetCFLTimeLocal" 4996 /*@ 4997 TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 4998 4999 Logically Collective on TS 5000 5001 Input Arguments: 5002 + ts - time stepping context 5003 - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 5004 5005 Note: 5006 After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 5007 5008 Level: intermediate 5009 5010 .seealso: TSGetCFLTime(), TSADAPTCFL 5011 @*/ 5012 PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 5013 { 5014 PetscFunctionBegin; 5015 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5016 ts->cfltime_local = cfltime; 5017 ts->cfltime = -1.; 5018 PetscFunctionReturn(0); 5019 } 5020 5021 #undef __FUNCT__ 5022 #define __FUNCT__ "TSGetCFLTime" 5023 /*@ 5024 TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 5025 5026 Collective on TS 5027 5028 Input Arguments: 5029 . ts - time stepping context 5030 5031 Output Arguments: 5032 . cfltime - maximum stable time step for forward Euler 5033 5034 Level: advanced 5035 5036 .seealso: TSSetCFLTimeLocal() 5037 @*/ 5038 PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 5039 { 5040 PetscErrorCode ierr; 5041 5042 PetscFunctionBegin; 5043 if (ts->cfltime < 0) { 5044 ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 5045 } 5046 *cfltime = ts->cfltime; 5047 PetscFunctionReturn(0); 5048 } 5049 5050 #undef __FUNCT__ 5051 #define __FUNCT__ "TSVISetVariableBounds" 5052 /*@ 5053 TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5054 5055 Input Parameters: 5056 . ts - the TS context. 5057 . xl - lower bound. 5058 . xu - upper bound. 5059 5060 Notes: 5061 If this routine is not called then the lower and upper bounds are set to 5062 PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5063 5064 Level: advanced 5065 5066 @*/ 5067 PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5068 { 5069 PetscErrorCode ierr; 5070 SNES snes; 5071 5072 PetscFunctionBegin; 5073 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5074 ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5075 PetscFunctionReturn(0); 5076 } 5077 5078 #if defined(PETSC_HAVE_MATLAB_ENGINE) 5079 #include <mex.h> 5080 5081 typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5082 5083 #undef __FUNCT__ 5084 #define __FUNCT__ "TSComputeFunction_Matlab" 5085 /* 5086 TSComputeFunction_Matlab - Calls the function that has been set with 5087 TSSetFunctionMatlab(). 5088 5089 Collective on TS 5090 5091 Input Parameters: 5092 + snes - the TS context 5093 - u - input vector 5094 5095 Output Parameter: 5096 . y - function vector, as set by TSSetFunction() 5097 5098 Notes: 5099 TSComputeFunction() is typically used within nonlinear solvers 5100 implementations, so most users would not generally call this routine 5101 themselves. 5102 5103 Level: developer 5104 5105 .keywords: TS, nonlinear, compute, function 5106 5107 .seealso: TSSetFunction(), TSGetFunction() 5108 */ 5109 PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5110 { 5111 PetscErrorCode ierr; 5112 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5113 int nlhs = 1,nrhs = 7; 5114 mxArray *plhs[1],*prhs[7]; 5115 long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5116 5117 PetscFunctionBegin; 5118 PetscValidHeaderSpecific(snes,TS_CLASSID,1); 5119 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5120 PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5121 PetscValidHeaderSpecific(y,VEC_CLASSID,5); 5122 PetscCheckSameComm(snes,1,u,3); 5123 PetscCheckSameComm(snes,1,y,5); 5124 5125 ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 5126 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5127 ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 5128 ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5129 5130 prhs[0] = mxCreateDoubleScalar((double)ls); 5131 prhs[1] = mxCreateDoubleScalar(time); 5132 prhs[2] = mxCreateDoubleScalar((double)lx); 5133 prhs[3] = mxCreateDoubleScalar((double)lxdot); 5134 prhs[4] = mxCreateDoubleScalar((double)ly); 5135 prhs[5] = mxCreateString(sctx->funcname); 5136 prhs[6] = sctx->ctx; 5137 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5138 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5139 mxDestroyArray(prhs[0]); 5140 mxDestroyArray(prhs[1]); 5141 mxDestroyArray(prhs[2]); 5142 mxDestroyArray(prhs[3]); 5143 mxDestroyArray(prhs[4]); 5144 mxDestroyArray(prhs[5]); 5145 mxDestroyArray(plhs[0]); 5146 PetscFunctionReturn(0); 5147 } 5148 5149 5150 #undef __FUNCT__ 5151 #define __FUNCT__ "TSSetFunctionMatlab" 5152 /* 5153 TSSetFunctionMatlab - Sets the function evaluation routine and function 5154 vector for use by the TS routines in solving ODEs 5155 equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5156 5157 Logically Collective on TS 5158 5159 Input Parameters: 5160 + ts - the TS context 5161 - func - function evaluation routine 5162 5163 Calling sequence of func: 5164 $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5165 5166 Level: beginner 5167 5168 .keywords: TS, nonlinear, set, function 5169 5170 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5171 */ 5172 PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5173 { 5174 PetscErrorCode ierr; 5175 TSMatlabContext *sctx; 5176 5177 PetscFunctionBegin; 5178 /* currently sctx is memory bleed */ 5179 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5180 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5181 /* 5182 This should work, but it doesn't 5183 sctx->ctx = ctx; 5184 mexMakeArrayPersistent(sctx->ctx); 5185 */ 5186 sctx->ctx = mxDuplicateArray(ctx); 5187 5188 ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5189 PetscFunctionReturn(0); 5190 } 5191 5192 #undef __FUNCT__ 5193 #define __FUNCT__ "TSComputeJacobian_Matlab" 5194 /* 5195 TSComputeJacobian_Matlab - Calls the function that has been set with 5196 TSSetJacobianMatlab(). 5197 5198 Collective on TS 5199 5200 Input Parameters: 5201 + ts - the TS context 5202 . u - input vector 5203 . A, B - the matrices 5204 - ctx - user context 5205 5206 Level: developer 5207 5208 .keywords: TS, nonlinear, compute, function 5209 5210 .seealso: TSSetFunction(), TSGetFunction() 5211 @*/ 5212 PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5213 { 5214 PetscErrorCode ierr; 5215 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5216 int nlhs = 2,nrhs = 9; 5217 mxArray *plhs[2],*prhs[9]; 5218 long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5219 5220 PetscFunctionBegin; 5221 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5222 PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5223 5224 /* call Matlab function in ctx with arguments u and y */ 5225 5226 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 5227 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5228 ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 5229 ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 5230 ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5231 5232 prhs[0] = mxCreateDoubleScalar((double)ls); 5233 prhs[1] = mxCreateDoubleScalar((double)time); 5234 prhs[2] = mxCreateDoubleScalar((double)lx); 5235 prhs[3] = mxCreateDoubleScalar((double)lxdot); 5236 prhs[4] = mxCreateDoubleScalar((double)shift); 5237 prhs[5] = mxCreateDoubleScalar((double)lA); 5238 prhs[6] = mxCreateDoubleScalar((double)lB); 5239 prhs[7] = mxCreateString(sctx->funcname); 5240 prhs[8] = sctx->ctx; 5241 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5242 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5243 mxDestroyArray(prhs[0]); 5244 mxDestroyArray(prhs[1]); 5245 mxDestroyArray(prhs[2]); 5246 mxDestroyArray(prhs[3]); 5247 mxDestroyArray(prhs[4]); 5248 mxDestroyArray(prhs[5]); 5249 mxDestroyArray(prhs[6]); 5250 mxDestroyArray(prhs[7]); 5251 mxDestroyArray(plhs[0]); 5252 mxDestroyArray(plhs[1]); 5253 PetscFunctionReturn(0); 5254 } 5255 5256 5257 #undef __FUNCT__ 5258 #define __FUNCT__ "TSSetJacobianMatlab" 5259 /* 5260 TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5261 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 5262 5263 Logically Collective on TS 5264 5265 Input Parameters: 5266 + ts - the TS context 5267 . A,B - Jacobian matrices 5268 . func - function evaluation routine 5269 - ctx - user context 5270 5271 Calling sequence of func: 5272 $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5273 5274 5275 Level: developer 5276 5277 .keywords: TS, nonlinear, set, function 5278 5279 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5280 */ 5281 PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5282 { 5283 PetscErrorCode ierr; 5284 TSMatlabContext *sctx; 5285 5286 PetscFunctionBegin; 5287 /* currently sctx is memory bleed */ 5288 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5289 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5290 /* 5291 This should work, but it doesn't 5292 sctx->ctx = ctx; 5293 mexMakeArrayPersistent(sctx->ctx); 5294 */ 5295 sctx->ctx = mxDuplicateArray(ctx); 5296 5297 ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5298 PetscFunctionReturn(0); 5299 } 5300 5301 #undef __FUNCT__ 5302 #define __FUNCT__ "TSMonitor_Matlab" 5303 /* 5304 TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5305 5306 Collective on TS 5307 5308 .seealso: TSSetFunction(), TSGetFunction() 5309 @*/ 5310 PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5311 { 5312 PetscErrorCode ierr; 5313 TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5314 int nlhs = 1,nrhs = 6; 5315 mxArray *plhs[1],*prhs[6]; 5316 long long int lx = 0,ls = 0; 5317 5318 PetscFunctionBegin; 5319 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5320 PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5321 5322 ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 5323 ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5324 5325 prhs[0] = mxCreateDoubleScalar((double)ls); 5326 prhs[1] = mxCreateDoubleScalar((double)it); 5327 prhs[2] = mxCreateDoubleScalar((double)time); 5328 prhs[3] = mxCreateDoubleScalar((double)lx); 5329 prhs[4] = mxCreateString(sctx->funcname); 5330 prhs[5] = sctx->ctx; 5331 ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5332 ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5333 mxDestroyArray(prhs[0]); 5334 mxDestroyArray(prhs[1]); 5335 mxDestroyArray(prhs[2]); 5336 mxDestroyArray(prhs[3]); 5337 mxDestroyArray(prhs[4]); 5338 mxDestroyArray(plhs[0]); 5339 PetscFunctionReturn(0); 5340 } 5341 5342 5343 #undef __FUNCT__ 5344 #define __FUNCT__ "TSMonitorSetMatlab" 5345 /* 5346 TSMonitorSetMatlab - Sets the monitor function from Matlab 5347 5348 Level: developer 5349 5350 .keywords: TS, nonlinear, set, function 5351 5352 .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5353 */ 5354 PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5355 { 5356 PetscErrorCode ierr; 5357 TSMatlabContext *sctx; 5358 5359 PetscFunctionBegin; 5360 /* currently sctx is memory bleed */ 5361 ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5362 ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5363 /* 5364 This should work, but it doesn't 5365 sctx->ctx = ctx; 5366 mexMakeArrayPersistent(sctx->ctx); 5367 */ 5368 sctx->ctx = mxDuplicateArray(ctx); 5369 5370 ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5371 PetscFunctionReturn(0); 5372 } 5373 #endif 5374 5375 #undef __FUNCT__ 5376 #define __FUNCT__ "TSMonitorLGSolution" 5377 /*@C 5378 TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5379 in a time based line graph 5380 5381 Collective on TS 5382 5383 Input Parameters: 5384 + ts - the TS context 5385 . step - current time-step 5386 . ptime - current time 5387 - lg - a line graph object 5388 5389 Options Database: 5390 . -ts_lg_monitor_solution_variables 5391 5392 Level: intermediate 5393 5394 Notes: each process in a parallel run displays its component solutions in a separate window 5395 5396 .keywords: TS, vector, monitor, view 5397 5398 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5399 @*/ 5400 PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5401 { 5402 PetscErrorCode ierr; 5403 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5404 const PetscScalar *yy; 5405 PetscInt dim; 5406 Vec v; 5407 5408 PetscFunctionBegin; 5409 if (!step) { 5410 PetscDrawAxis axis; 5411 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5412 ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5413 if (ctx->names && !ctx->displaynames) { 5414 char **displaynames; 5415 PetscBool flg; 5416 5417 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5418 ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5419 ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 5420 ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_lg_monitor_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5421 if (flg) { 5422 ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5423 } 5424 ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5425 } 5426 if (ctx->displaynames) { 5427 ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5428 ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5429 } else if (ctx->names) { 5430 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5431 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5432 ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5433 } 5434 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5435 } 5436 if (ctx->transform) { 5437 ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 5438 } else { 5439 v = u; 5440 } 5441 ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5442 #if defined(PETSC_USE_COMPLEX) 5443 { 5444 PetscReal *yreal; 5445 PetscInt i,n; 5446 ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5447 ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5448 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 5449 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5450 ierr = PetscFree(yreal);CHKERRQ(ierr); 5451 } 5452 #else 5453 if (ctx->displaynames) { 5454 PetscInt i; 5455 for (i=0; i<ctx->ndisplayvariables; i++) { 5456 ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5457 } 5458 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5459 } else { 5460 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5461 } 5462 #endif 5463 ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 5464 if (ctx->transform) { 5465 ierr = VecDestroy(&v);CHKERRQ(ierr); 5466 } 5467 if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 5468 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5469 } 5470 PetscFunctionReturn(0); 5471 } 5472 5473 5474 #undef __FUNCT__ 5475 #define __FUNCT__ "TSMonitorLGSetVariableNames" 5476 /*@C 5477 TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5478 5479 Collective on TS 5480 5481 Input Parameters: 5482 + ts - the TS context 5483 - names - the names of the components, final string must be NULL 5484 5485 Level: intermediate 5486 5487 .keywords: TS, vector, monitor, view 5488 5489 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5490 @*/ 5491 PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5492 { 5493 PetscErrorCode ierr; 5494 PetscInt i; 5495 5496 PetscFunctionBegin; 5497 for (i=0; i<ts->numbermonitors; i++) { 5498 if (ts->monitor[i] == TSMonitorLGSolution) { 5499 ierr = TSMonitorLGCtxSetVariableNames(ts->monitorcontext[i],names);CHKERRQ(ierr); 5500 break; 5501 } 5502 } 5503 PetscFunctionReturn(0); 5504 } 5505 5506 #undef __FUNCT__ 5507 #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 5508 /*@C 5509 TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5510 5511 Collective on TS 5512 5513 Input Parameters: 5514 + ts - the TS context 5515 - names - the names of the components, final string must be NULL 5516 5517 Level: intermediate 5518 5519 .keywords: TS, vector, monitor, view 5520 5521 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 5522 @*/ 5523 PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 5524 { 5525 PetscErrorCode ierr; 5526 5527 PetscFunctionBegin; 5528 ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 5529 ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 5530 PetscFunctionReturn(0); 5531 } 5532 5533 #undef __FUNCT__ 5534 #define __FUNCT__ "TSMonitorLGGetVariableNames" 5535 /*@C 5536 TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 5537 5538 Collective on TS 5539 5540 Input Parameter: 5541 . ts - the TS context 5542 5543 Output Parameter: 5544 . names - the names of the components, final string must be NULL 5545 5546 Level: intermediate 5547 5548 .keywords: TS, vector, monitor, view 5549 5550 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5551 @*/ 5552 PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 5553 { 5554 PetscInt i; 5555 5556 PetscFunctionBegin; 5557 *names = NULL; 5558 for (i=0; i<ts->numbermonitors; i++) { 5559 if (ts->monitor[i] == TSMonitorLGSolution) { 5560 TSMonitorLGCtx ctx = ts->monitorcontext[i]; 5561 *names = (const char *const *)ctx->names; 5562 break; 5563 } 5564 } 5565 PetscFunctionReturn(0); 5566 } 5567 5568 #undef __FUNCT__ 5569 #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 5570 /*@C 5571 TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 5572 5573 Collective on TS 5574 5575 Input Parameters: 5576 + ctx - the TSMonitorLG context 5577 . displaynames - the names of the components, final string must be NULL 5578 5579 Level: intermediate 5580 5581 .keywords: TS, vector, monitor, view 5582 5583 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5584 @*/ 5585 PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 5586 { 5587 PetscInt j = 0,k; 5588 PetscErrorCode ierr; 5589 5590 PetscFunctionBegin; 5591 if (!ctx->names) PetscFunctionReturn(0); 5592 ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 5593 ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 5594 while (displaynames[j]) j++; 5595 ctx->ndisplayvariables = j; 5596 ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 5597 ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 5598 j = 0; 5599 while (displaynames[j]) { 5600 k = 0; 5601 while (ctx->names[k]) { 5602 PetscBool flg; 5603 ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 5604 if (flg) { 5605 ctx->displayvariables[j] = k; 5606 break; 5607 } 5608 k++; 5609 } 5610 j++; 5611 } 5612 PetscFunctionReturn(0); 5613 } 5614 5615 5616 #undef __FUNCT__ 5617 #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 5618 /*@C 5619 TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 5620 5621 Collective on TS 5622 5623 Input Parameters: 5624 + ts - the TS context 5625 . displaynames - the names of the components, final string must be NULL 5626 5627 Level: intermediate 5628 5629 .keywords: TS, vector, monitor, view 5630 5631 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5632 @*/ 5633 PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 5634 { 5635 PetscInt i; 5636 PetscErrorCode ierr; 5637 5638 PetscFunctionBegin; 5639 for (i=0; i<ts->numbermonitors; i++) { 5640 if (ts->monitor[i] == TSMonitorLGSolution) { 5641 ierr = TSMonitorLGCtxSetDisplayVariables(ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 5642 break; 5643 } 5644 } 5645 PetscFunctionReturn(0); 5646 } 5647 5648 #undef __FUNCT__ 5649 #define __FUNCT__ "TSMonitorLGSetTransform" 5650 /*@C 5651 TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 5652 5653 Collective on TS 5654 5655 Input Parameters: 5656 + ts - the TS context 5657 . transform - the transform function 5658 . destroy - function to destroy the optional context 5659 - ctx - optional context used by transform function 5660 5661 Level: intermediate 5662 5663 .keywords: TS, vector, monitor, view 5664 5665 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 5666 @*/ 5667 PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 5668 { 5669 PetscInt i; 5670 PetscErrorCode ierr; 5671 5672 PetscFunctionBegin; 5673 for (i=0; i<ts->numbermonitors; i++) { 5674 if (ts->monitor[i] == TSMonitorLGSolution) { 5675 ierr = TSMonitorLGCtxSetTransform(ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 5676 } 5677 } 5678 PetscFunctionReturn(0); 5679 } 5680 5681 #undef __FUNCT__ 5682 #define __FUNCT__ "TSMonitorLGCtxSetTransform" 5683 /*@C 5684 TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 5685 5686 Collective on TSLGCtx 5687 5688 Input Parameters: 5689 + ts - the TS context 5690 . transform - the transform function 5691 . destroy - function to destroy the optional context 5692 - ctx - optional context used by transform function 5693 5694 Level: intermediate 5695 5696 .keywords: TS, vector, monitor, view 5697 5698 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 5699 @*/ 5700 PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 5701 { 5702 PetscFunctionBegin; 5703 ctx->transform = transform; 5704 ctx->transformdestroy = destroy; 5705 ctx->transformctx = tctx; 5706 PetscFunctionReturn(0); 5707 } 5708 5709 #undef __FUNCT__ 5710 #define __FUNCT__ "TSMonitorLGError" 5711 /*@C 5712 TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 5713 in a time based line graph 5714 5715 Collective on TS 5716 5717 Input Parameters: 5718 + ts - the TS context 5719 . step - current time-step 5720 . ptime - current time 5721 - lg - a line graph object 5722 5723 Level: intermediate 5724 5725 Notes: 5726 Only for sequential solves. 5727 5728 The user must provide the solution using TSSetSolutionFunction() to use this monitor. 5729 5730 Options Database Keys: 5731 . -ts_monitor_lg_error - create a graphical monitor of error history 5732 5733 .keywords: TS, vector, monitor, view 5734 5735 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 5736 @*/ 5737 PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5738 { 5739 PetscErrorCode ierr; 5740 TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5741 const PetscScalar *yy; 5742 Vec y; 5743 PetscInt dim; 5744 5745 PetscFunctionBegin; 5746 if (!step) { 5747 PetscDrawAxis axis; 5748 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5749 ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 5750 ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5751 ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5752 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5753 } 5754 ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 5755 ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 5756 ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 5757 ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 5758 #if defined(PETSC_USE_COMPLEX) 5759 { 5760 PetscReal *yreal; 5761 PetscInt i,n; 5762 ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 5763 ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5764 for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 5765 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5766 ierr = PetscFree(yreal);CHKERRQ(ierr); 5767 } 5768 #else 5769 ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5770 #endif 5771 ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 5772 ierr = VecDestroy(&y);CHKERRQ(ierr); 5773 if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 5774 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5775 } 5776 PetscFunctionReturn(0); 5777 } 5778 5779 #undef __FUNCT__ 5780 #define __FUNCT__ "TSMonitorLGSNESIterations" 5781 PetscErrorCode TSMonitorLGSNESIterations(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,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 5794 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5795 5796 ctx->snes_its = 0; 5797 } 5798 ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 5799 y = its - ctx->snes_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->snes_its = its; 5805 PetscFunctionReturn(0); 5806 } 5807 5808 #undef __FUNCT__ 5809 #define __FUNCT__ "TSMonitorLGKSPIterations" 5810 PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 5811 { 5812 TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 5813 PetscReal x = ptime,y; 5814 PetscErrorCode ierr; 5815 PetscInt its; 5816 5817 PetscFunctionBegin; 5818 if (!n) { 5819 PetscDrawAxis axis; 5820 5821 ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5822 ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 5823 ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 5824 5825 ctx->ksp_its = 0; 5826 } 5827 ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 5828 y = its - ctx->ksp_its; 5829 ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 5830 if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 5831 ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 5832 } 5833 ctx->ksp_its = its; 5834 PetscFunctionReturn(0); 5835 } 5836 5837 #undef __FUNCT__ 5838 #define __FUNCT__ "TSComputeLinearStability" 5839 /*@ 5840 TSComputeLinearStability - computes the linear stability function at a point 5841 5842 Collective on TS and Vec 5843 5844 Input Parameters: 5845 + ts - the TS context 5846 - xr,xi - real and imaginary part of input arguments 5847 5848 Output Parameters: 5849 . yr,yi - real and imaginary part of function value 5850 5851 Level: developer 5852 5853 .keywords: TS, compute 5854 5855 .seealso: TSSetRHSFunction(), TSComputeIFunction() 5856 @*/ 5857 PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5858 { 5859 PetscErrorCode ierr; 5860 5861 PetscFunctionBegin; 5862 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5863 if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5864 ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5865 PetscFunctionReturn(0); 5866 } 5867 5868 /* ------------------------------------------------------------------------*/ 5869 #undef __FUNCT__ 5870 #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 5871 /*@C 5872 TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 5873 5874 Collective on TS 5875 5876 Input Parameters: 5877 . ts - the ODE solver object 5878 5879 Output Parameter: 5880 . ctx - the context 5881 5882 Level: intermediate 5883 5884 .keywords: TS, monitor, line graph, residual, seealso 5885 5886 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 5887 5888 @*/ 5889 PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 5890 { 5891 PetscErrorCode ierr; 5892 5893 PetscFunctionBegin; 5894 ierr = PetscNew(ctx);CHKERRQ(ierr); 5895 PetscFunctionReturn(0); 5896 } 5897 5898 #undef __FUNCT__ 5899 #define __FUNCT__ "TSMonitorEnvelope" 5900 /*@C 5901 TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 5902 5903 Collective on TS 5904 5905 Input Parameters: 5906 + ts - the TS context 5907 . step - current time-step 5908 . ptime - current time 5909 - ctx - the envelope context 5910 5911 Options Database: 5912 . -ts_monitor_envelope 5913 5914 Level: intermediate 5915 5916 Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 5917 5918 .keywords: TS, vector, monitor, view 5919 5920 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds() 5921 @*/ 5922 PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5923 { 5924 PetscErrorCode ierr; 5925 TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy; 5926 5927 PetscFunctionBegin; 5928 if (!ctx->max) { 5929 ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 5930 ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 5931 ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 5932 ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 5933 } else { 5934 ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 5935 ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 5936 } 5937 PetscFunctionReturn(0); 5938 } 5939 5940 5941 #undef __FUNCT__ 5942 #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 5943 /*@C 5944 TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 5945 5946 Collective on TS 5947 5948 Input Parameter: 5949 . ts - the TS context 5950 5951 Output Parameter: 5952 + max - the maximum values 5953 - min - the minimum values 5954 5955 Level: intermediate 5956 5957 .keywords: TS, vector, monitor, view 5958 5959 .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5960 @*/ 5961 PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 5962 { 5963 PetscInt i; 5964 5965 PetscFunctionBegin; 5966 if (max) *max = NULL; 5967 if (min) *min = NULL; 5968 for (i=0; i<ts->numbermonitors; i++) { 5969 if (ts->monitor[i] == TSMonitorEnvelope) { 5970 TSMonitorEnvelopeCtx ctx = ts->monitorcontext[i]; 5971 if (max) *max = ctx->max; 5972 if (min) *min = ctx->min; 5973 break; 5974 } 5975 } 5976 PetscFunctionReturn(0); 5977 } 5978 5979 #undef __FUNCT__ 5980 #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 5981 /*@C 5982 TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 5983 5984 Collective on TSMonitorEnvelopeCtx 5985 5986 Input Parameter: 5987 . ctx - the monitor context 5988 5989 Level: intermediate 5990 5991 .keywords: TS, monitor, line graph, destroy 5992 5993 .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 5994 @*/ 5995 PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 5996 { 5997 PetscErrorCode ierr; 5998 5999 PetscFunctionBegin; 6000 ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6001 ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6002 ierr = PetscFree(*ctx);CHKERRQ(ierr); 6003 PetscFunctionReturn(0); 6004 } 6005 6006 #undef __FUNCT__ 6007 #define __FUNCT__ "TSRollBack" 6008 /*@ 6009 TSRollBack - Rolls back one time step 6010 6011 Collective on TS 6012 6013 Input Parameter: 6014 . ts - the TS context obtained from TSCreate() 6015 6016 Level: advanced 6017 6018 .keywords: TS, timestep, rollback 6019 6020 .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 6021 @*/ 6022 PetscErrorCode TSRollBack(TS ts) 6023 { 6024 PetscErrorCode ierr; 6025 6026 PetscFunctionBegin; 6027 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6028 6029 if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 6030 ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 6031 ts->time_step = ts->ptime - ts->ptime_prev; 6032 ts->ptime = ts->ptime_prev; 6033 PetscFunctionReturn(0); 6034 } 6035 6036 #undef __FUNCT__ 6037 #define __FUNCT__ "TSGetStages" 6038 /*@ 6039 TSGetStages - Get the number of stages and stage values 6040 6041 Input Parameter: 6042 . ts - the TS context obtained from TSCreate() 6043 6044 Level: advanced 6045 6046 .keywords: TS, getstages 6047 6048 .seealso: TSCreate() 6049 @*/ 6050 PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6051 { 6052 PetscErrorCode ierr; 6053 6054 PetscFunctionBegin; 6055 PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6056 PetscValidPointer(ns,2); 6057 6058 if (!ts->ops->getstages) *ns=0; 6059 else { 6060 ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6061 } 6062 PetscFunctionReturn(0); 6063 } 6064 6065