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